Skip to content

Commit 266e2a8

Browse files
committed
Add eta to input damages
1 parent 0cc033c commit 266e2a8

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

src/dscim/menu/simple_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ def gdppc(self):
384384
def adding_up_damages(self):
385385
"""This property calls pre-calculated adding-up IR-level 'mean' over batches."""
386386

387-
mean_cc = f"{self.ce_path}/adding_up_cc.zarr"
388-
mean_no_cc = f"{self.ce_path}/adding_up_no_cc.zarr"
387+
mean_cc = f"{self.ce_path}/adding_up_cc_eta{self.eta}.zarr"
388+
mean_no_cc = f"{self.ce_path}/adding_up_no_cc_eta{self.eta}.zarr"
389389

390390
if os.path.exists(mean_cc) and os.path.exists(mean_no_cc):
391391
self.logger.info(

src/dscim/preprocessing/input_damages.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ def prep_mortality_damages(
710710
outpath,
711711
mortality_version,
712712
path_econ,
713+
etas,
713714
):
714715
ec = EconVars(path_econ=path_econ)
715716

@@ -732,6 +733,10 @@ def prep_mortality_damages(
732733
scaling_deaths = "epa_row"
733734
scaling_costs = "epa_scaled"
734735
valuation = "vsl"
736+
elif mortality_version == 9:
737+
scaling_deaths = "epa_popavg"
738+
scaling_costs = "epa_scaled"
739+
valuation = "vly"
735740
else:
736741
raise ValueError("Mortality version not valid: ", str(mortality_version))
737742

@@ -764,8 +769,14 @@ def prep(
764769
valuation=valuation,
765770
).drop(["gcm", "valuation"])
766771

767-
data = xr.open_mfdataset(paths, preprocess=prep, parallel=True, engine="zarr")
768-
772+
d_ls = []
773+
for eta in etas:
774+
paths_ls = [paths.format(i,eta) for i in range(15)]
775+
data = xr.open_mfdataset(paths_ls, preprocess=prep, parallel=True, engine="zarr").assign_coords({'eta': eta}).expand_dims('eta')
776+
d_ls.append(data)
777+
778+
data = xr.merge(d_ls)
779+
769780
damages = xr.Dataset(
770781
{
771782
"delta": (
@@ -786,12 +797,14 @@ def prep(
786797
damages = damages.chunk(
787798
{
788799
"batch": 15,
800+
"eta": 1,
789801
"ssp": 1,
790802
"model": 1,
791803
"rcp": 1,
792804
"gcm": 1,
793805
"year": 10,
794806
"region": -1,
807+
"eta": 1,
795808
}
796809
)
797810
damages.coords.update({"batch": [f"batch{i}" for i in damages.batch.values]})
@@ -808,6 +821,8 @@ def prep(
808821
if damages[v].dtype == object:
809822
damages[v] = damages[v].astype("unicode")
810823

824+
damages.coords['gcm'] = damages.coords['gcm'].astype('object')
825+
811826
if i == 0:
812827
damages.to_zarr(
813828
f"{outpath}/impacts-darwin-montecarlo-damages-v{mortality_version}.zarr",

src/dscim/preprocessing/preprocessing.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def ce_from_chunk(
4343
else:
4444
raise NotImplementedError("Pass 'cc' or 'no_cc' to reduction.")
4545

46+
calculation = xr.where(calculation > 1e8, np.nan, calculation)
47+
4648
if recipe == "adding_up":
4749
result = mean_func(
4850
np.maximum(
@@ -74,12 +76,6 @@ def reduce_damages(
7476
bottom_coding_gdppc=39.39265060424805,
7577
zero=False,
7678
):
77-
if recipe == "adding_up":
78-
assert (
79-
eta is None
80-
), "Adding up does not take an eta argument. Please set to None."
81-
# client = Client(n_workers=35, memory_limit="9G", threads_per_worker=1)
82-
8379
with open(config) as stream:
8480
c = yaml.safe_load(stream)
8581
params = c["sectors"][sector]
@@ -114,7 +110,7 @@ def reduce_damages(
114110
}
115111

116112
ce_batch_dims = [i for i in gdppc.dims] + [
117-
i for i in ds.dims if i not in gdppc.dims and i != "batch"
113+
i for i in ds.dims if i not in gdppc.dims and i != "batch" and i != "eta"
118114
]
119115
ce_batch_coords = {c: ds[c].values for c in ce_batch_dims}
120116
ce_batch_coords["region"] = [
@@ -129,7 +125,9 @@ def reduce_damages(
129125
).chunk(chunkies)
130126

131127
other = xr.open_zarr(damages).chunk(chunkies)
132-
128+
if "eta" in other.coords:
129+
other = other.sel(eta = eta, drop=True)
130+
133131
out = other.map_blocks(
134132
ce_from_chunk,
135133
kwargs=dict(
@@ -155,7 +153,7 @@ def reduce_damages(
155153

156154
if recipe == "adding_up":
157155
out.to_zarr(
158-
f"{outpath}/{recipe}_{reduction}.zarr",
156+
f"{outpath}/{recipe}_{reduction}_eta{eta}.zarr",
159157
consolidated=True,
160158
mode="w",
161159
)

src/dscim/utils/menu_runs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def run_ssps(
104104
"damage_function_path": f"{conf['paths']['ssp_damage_function_library']}/{sector}/2020/unmasked",
105105
"save_files": [
106106
"damage_function_points",
107+
#"uncollapsed_marginal_damages",
108+
#"uncollapsed_discount_factors",
107109
"marginal_damages",
108110
"discount_factors",
109111
"uncollapsed_sccs",

0 commit comments

Comments
 (0)