-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocessing.py
404 lines (334 loc) · 11 KB
/
preprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
from math import ceil
from pathlib import Path
import os
import sys
USER = os.getenv("USER")
import dask
import dask.array as da
import dask.config
import numpy as np
import xarray as xr
from dask.distributed import Client, progress
from dscim.utils.functions import ce_func, mean_func
import yaml
import time
import argparse
def ce_from_chunk(
chunk,
filepath,
reduction,
bottom_code,
histclim,
delta,
recipe,
eta,
zero,
socioec,
ce_batch_coords,
):
year = chunk.year.values
ssp = chunk.ssp.values
model = chunk.model.values
gdppc = (
xr.open_zarr(socioec, chunks=None)
.sel(
year=year, ssp=ssp, model=model, region=ce_batch_coords["region"], drop=True
)
.gdppc
)
if reduction == "no_cc":
if zero:
chunk[histclim] = xr.where(chunk[histclim] == 0, 0, 0)
calculation = gdppc + chunk[histclim].mean("batch") - chunk[histclim]
elif reduction == "cc":
calculation = gdppc - chunk[delta]
else:
raise NotImplementedError("Pass 'cc' or 'no_cc' to reduction.")
if recipe == "adding_up":
result = mean_func(
np.maximum(
calculation,
bottom_code,
),
"batch",
)
elif recipe == "risk_aversion":
result = ce_func(
np.maximum(
calculation,
bottom_code,
),
"batch",
eta=eta,
)
return result
def reduce_damages(
recipe,
reduction,
eta,
sector,
config,
socioec,
bottom_coding_gdppc=39.39265060424805,
zero=False,
):
if recipe == "adding_up":
assert (
eta is None
), "Adding up does not take an eta argument. Please set to None."
# client = Client(n_workers=35, memory_limit="9G", threads_per_worker=1)
with open(config, "r") as stream:
c = yaml.safe_load(stream)
params = c["sectors"][sector]
damages = Path(params["sector_path"])
histclim = params["histclim"]
delta = params["delta"]
outpath = f"{c['paths']['reduced_damages_library']}/{sector}"
with xr.open_zarr(damages, chunks=None)[histclim] as ds:
with xr.open_zarr(socioec, chunks=None) as gdppc:
assert (
xr.open_zarr(damages).chunks["batch"][0] == 15
), "'batch' dim on damages does not have chunksize of 15. Please rechunk."
ce_batch_dims = [i for i in gdppc.dims] + [
i for i in ds.dims if i not in gdppc.dims and i != "batch"
]
ce_batch_coords = {c: ds[c].values for c in ce_batch_dims}
ce_batch_coords["region"] = [
i for i in gdppc.region.values if i in ce_batch_coords["region"]
]
ce_shapes = [len(ce_batch_coords[c]) for c in ce_batch_dims]
ce_chunks = [xr.open_zarr(damages).chunks[c][0] for c in ce_batch_dims]
template = xr.DataArray(
da.empty(ce_shapes, chunks=ce_chunks),
dims=ce_batch_dims,
coords=ce_batch_coords,
)
other = xr.open_zarr(damages)
out = other.map_blocks(
ce_from_chunk,
kwargs=dict(
filepath=damages,
reduction=reduction,
bottom_code=bottom_coding_gdppc,
histclim=histclim,
delta=delta,
eta=eta,
recipe=recipe,
zero=zero,
socioec=socioec,
ce_batch_coords=ce_batch_coords,
),
template=template,
)
out = out.astype(np.float32).rename(reduction).to_dataset()
out.attrs["bottom code"] = bottom_coding_gdppc
out.attrs["histclim=0"] = zero
out.attrs["filepath"] = str(damages)
if recipe == "adding_up":
out.to_zarr(
f"{outpath}/{recipe}_{reduction}.zarr",
consolidated=True,
mode="w",
)
elif recipe == "risk_aversion":
out.attrs["eta"] = eta
out.to_zarr(
f"{outpath}/{recipe}_{reduction}_eta{eta}.zarr",
consolidated=True,
mode="w",
)
def reformat_climate_files():
from dscim.preprocessing.climate.reformat import (
convert_old_to_newformat_AR,
stack_gases,
)
# convert AR6 files
bd = "/shares/gcp/integration/float32/dscim_input_data/climate/AR6"
pathdt = {
"median": f"{bd}/ar6_fair162_medianparams_control_pulse_2020-2080_10yrincrements_conc_rf_temp_lambdaeff_emissions-driven_2naturalfix_v4.0_Jan212022.nc",
"sims": f"{bd}/ar6_fair162_control_pulse_2020-2030-2040-2050-2060-2070-2080_emis_conc_rf_temp_lambdaeff_emissions-driven_naturalfix_v4.0_Jan212022.nc",
}
newds = convert_old_to_newformat_AR(
pathdt,
gas="CO2_Fossil",
pulseyrs=[2020, 2030, 2040, 2050, 2060, 2070, 2080],
var="temperature",
)
newds.to_netcdf(
f"{bd}/ar6_fair162_sim_and_medianparams_control_pulse_2030-2040-2050-2060-2070-2080_emis_conc_rf_temp_lambdaeff_emissions-driven_naturalfix_v4.0_Jan212022.nc"
)
# convert RFF files
gases = {"CO2_Fossil": "Feb072022", "CH4": "Feb072022", "N2O": "Feb072022"}
stack_gases(gas_dict=gases)
def sum_AMEL(
sectors,
config,
AMEL,
):
# load config
with open(config, "r") as stream:
loaded_config = yaml.safe_load(stream)
params = loaded_config["sectors"]
output = params[AMEL]["sector_path"]
# save summed variables to zarr one by one
for i, var in enumerate(["delta", "histclim"]):
datasets = []
for sector in sectors:
print(f"Opening {sector},{params[sector]['sector_path']}")
ds = xr.open_zarr(params[sector]["sector_path"], consolidated=True)
ds = ds[params[sector][var]].rename(var)
ds = xr.where(np.isinf(ds), np.nan, ds)
datasets.append(ds)
summed = (
xr.concat(datasets, dim="variable")
.sum("variable")
.rename(f"summed_{var}")
.astype(np.float32)
.chunk(
{
"batch": 15,
"ssp": 1,
"model": 1,
"rcp": 1,
"gcm": 1,
"year": 10,
"region": 24378,
}
)
.to_dataset()
)
summed.attrs["paths"] = str({s: params[s]["sector_path"] for s in sectors})
summed.attrs["delta"] = str({s: params[s]["delta"] for s in sectors})
summed.attrs["histclim"] = str({s: params[s]["histclim"] for s in sectors})
for v in summed.variables:
summed[v].encoding.clear()
if i == 0:
summed.to_zarr(output, consolidated=True, mode="w")
else:
summed.to_zarr(output, consolidated=True, mode="a")
def subset_USA_reduced_damages(
sector,
reduction,
recipe,
eta,
input_path,
):
if recipe == "adding_up":
ds = xr.open_zarr(
f"{input_path}/{sector}/{recipe}_{reduction}.zarr",
)
elif recipe == "risk_aversion":
ds = xr.open_zarr(
f"{input_path}/{sector}/{recipe}_{reduction}_eta{eta}.zarr",
)
subset = ds.sel(region=[i for i in ds.region.values if "USA" in i])
for var in subset.variables:
subset[var].encoding.clear()
if recipe == "adding_up":
subset.to_zarr(
f"{input_path}/{sector}_USA/{recipe}_{reduction}.zarr",
consolidated=True,
mode="w",
)
elif recipe == "risk_aversion":
subset.to_zarr(
f"{input_path}/{sector}_USA/{recipe}_{reduction}_eta{eta}.zarr",
consolidated=True,
mode="w",
)
def subset_USA_ssp_econ(
in_path,
out_path,
):
zarr = xr.open_zarr(
in_path,
consolidated=True,
)
zarr = zarr.sel(region=[i for i in zarr.region.values if "USA" in i])
for var in zarr.variables:
zarr[var].encoding.clear()
zarr.to_zarr(
out_path,
consolidated=True,
mode="w",
)
def clip_damages(
config,
sector,
econ_path="/shares/gcp/integration/float32/dscim_input_data/econvars/zarrs/integration-econ-bc39.zarr",
):
"""This function is no longer in use.
To operationalize, make sure to get a Dask client running with the following code:
# set up dask
dask.config.set(
{
"distributed.worker.memory.target": 0.7,
"distributed.worker.memory.spill": 0.8,
"distributed.worker.memory.pause": 0.9,
}
)
client = Client(n_workers=40, memory_limit="9G", threads_per_worker=1)
"""
# load config
with open(config, "r") as stream:
loaded_config = yaml.safe_load(stream)
params = loaded_config["sectors"][sector]
# get sector paths and variable names
path = Path(params["sector_path"])
histclim = params["histclim"]
delta = params["delta"]
with xr.open_zarr(path, chunks=None)[delta] as ds:
with xr.open_zarr(econ_path, chunks=None) as gdppc:
ce_batch_dims = [i for i in ds.dims]
ce_batch_coords = {c: ds[c].values for c in ce_batch_dims}
ce_batch_coords["region"] = [
i for i in ds.region.values if i in gdppc.region.values
]
ce_shapes = [len(ce_batch_coords[c]) for c in ce_batch_dims]
ce_chunks = [xr.open_zarr(path).chunks[c][0] for c in ce_batch_dims]
print(ce_chunks)
template = xr.DataArray(
da.empty(ce_shapes, chunks=ce_chunks),
dims=ce_batch_dims,
coords=ce_batch_coords,
)
def chunk_func(
damages,
):
year = damages.year.values
ssp = damages.ssp.values
model = damages.model.values
region = damages.region.values
gdppc = (
xr.open_zarr(econ_path, chunks=None)
.sel(year=year, ssp=ssp, model=model, region=region, drop=True)
.gdppc
)
# get damages as % of GDPpc
shares = damages / gdppc
# find the 1st/99th percentile of damage share
# across batches and regions
quantile = shares.quantile([0.01, 0.99], ["batch", "region"])
# find the equivalent damages
# if damage share is capped to 1st/99th percentile
quantdams = quantile * gdppc
# keep damages that are within cutoff,
# otherwise replace with capped damages
damages = xr.where(
(shares <= quantile.sel(quantile=0.99, drop=True)),
damages,
quantdams.sel(quantile=0.99, drop=True),
)
damages = xr.where(
(shares >= quantile.sel(quantile=0.01, drop=True)),
damages,
quantdams.sel(quantile=0.01, drop=True),
)
return damages
data = xr.open_zarr(path)
for var in [delta, histclim]:
out = (
data[var].map_blocks(chunk_func, template=template).rename(var).to_dataset()
)
outpath = path.replace(".zarr", "_clipped.zarr")
out.to_zarr(outpath, mode="a", consolidated=True)