Skip to content

Commit 07a2d27

Browse files
committed
improve dict conversions in prep_sliiders
1 parent 344b84b commit 07a2d27

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

pyCIAM/io.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def prep_sliiders(
3636
seg_var="seg_adm",
3737
selectors={},
3838
calc_popdens_with_wetland_area=True,
39+
expand_exposure=True,
3940
storage_options={},
4041
):
4142
"""Import the SLIIDERS dataset (or a different dataset formatted analogously),
@@ -66,6 +67,11 @@ def prep_sliiders(
6667
If True, assume that population can also exist in Wetland area. This is
6768
observed empirically, but presumably at a lower density. Diaz 2016 assumes False
6869
but Depsky 2023 assumes True.
70+
expand_exposure : bool, default True
71+
If the input contains population ("pop") and capital ("K") for a fixed year,
72+
plus a country-level scaling factor for each year, setting this to True
73+
(default) expands this to a panel dataset of each variable. This substantially
74+
increases size of the dataset, so can be set to False if not Needed
6975
storage_options : dict, optional
7076
Passed to :py:function:`xarray.open_zarr`
7177
@@ -99,14 +105,14 @@ def prep_sliiders(
99105
).sel(selectors, drop=True)
100106

101107
inputs = inputs_all.sel({seg_var: seg_vals})
102-
inputs = _s2d(inputs).assign(constants.to_dict())
108+
inputs = _s2d(inputs).assign(constants)
103109

104110
# assign country level vars to each segment
105111
for v in inputs.data_vars:
106112
if "country" in inputs[v].dims:
107113
inputs[v] = inputs[v].sel(country=inputs.seg_country).drop("country")
108114

109-
if "vsl" not in inputs.data_vars:
115+
if "vsl" not in inputs.data_vars and "vsl_ypc_mult" in inputs.data_vars:
110116
if "ref_income" in inputs:
111117
ref_income = inputs.ref_income
112118
else:
@@ -118,7 +124,7 @@ def prep_sliiders(
118124
* (inputs.ypcc / ref_income) ** inputs.vsl_inc_elast
119125
)
120126

121-
if "pop" not in inputs.data_vars:
127+
if expand_exposure and "pop" not in inputs.data_vars:
122128
exp_year = [
123129
v for v in inputs.data_vars if v.startswith("pop_") and "scale" not in v
124130
]
@@ -127,19 +133,23 @@ def prep_sliiders(
127133
pop_var = "pop_" + exp_year
128134
inputs["pop"] = inputs[pop_var] * inputs.pop_scale
129135
inputs = inputs.drop(pop_var)
130-
if "K" not in inputs.data_vars:
136+
if expand_exposure and "K" not in inputs.data_vars:
131137
K_var = "K_" + exp_year
132138
inputs["K"] = inputs[K_var] * inputs.K_scale
133139
inputs = inputs.drop(K_var)
134-
if "dfact" not in inputs.data_vars:
140+
if "dfact" not in inputs.data_vars and "npv_start" in inputs.data_vars:
135141
inputs["dfact"] = (1 / (1 + inputs.dr)) ** (inputs.year - inputs.npv_start)
136142

137143
if "landrent" or "ypc" not in inputs.data_vars:
138144
area = inputs.landarea
139145
if calc_popdens_with_wetland_area:
140146
area = area + inputs.wetland
141147
popdens = (inputs.pop / area).fillna(0)
142-
if "landrent" not in inputs.data_vars:
148+
if (
149+
"landrent" not in inputs.data_vars
150+
and "min_coastland_scale" in inputs.data_vars
151+
and "dr" in inputs.data_vars
152+
):
143153
coastland_scale = np.minimum(
144154
1,
145155
np.maximum(
@@ -149,26 +159,32 @@ def prep_sliiders(
149159
)
150160
inputs["landrent"] = inputs.interior * coastland_scale * inputs.dr
151161

152-
if "ypc" not in inputs.data_vars:
162+
if (
163+
"ypc" not in inputs.data_vars
164+
and "min_pyc_scale" in inputs.data_vars
165+
and "ypc_scale_denom" in inputs.data_vars
166+
and "ypc_scale_elast" in inputs.data_vars
167+
):
153168
ypc_scale = np.maximum(
154169
inputs.min_ypc_scale,
155170
(popdens / inputs.ypc_scale_denom) ** inputs.ypc_scale_elast,
156171
)
157172
inputs["ypc"] = ypc_scale * inputs.ypcc
158173

174+
to_drop = [
175+
"interior",
176+
"dr",
177+
"min_coastland_scale",
178+
"min_ypc_scale",
179+
"ypc_scale_denom",
180+
"ypc_scale_elast",
181+
"vsl_ypc_mult",
182+
"vsl_inc_elast",
183+
]
184+
if expand_exposure:
185+
to_drop += ["pop_scale", "K_scale"]
159186
return inputs.drop(
160-
[
161-
"pop_scale",
162-
"K_scale",
163-
"interior",
164-
"dr",
165-
"min_coastland_scale",
166-
"min_ypc_scale",
167-
"ypc_scale_denom",
168-
"ypc_scale_elast",
169-
"vsl_ypc_mult",
170-
"vsl_inc_elast",
171-
],
187+
to_drop,
172188
errors="ignore",
173189
)
174190

@@ -555,7 +571,8 @@ def get_nearest_slrs(slr_ds, lonlats, x1="seg_lon", y1="seg_lat"):
555571

556572
def add_nearest_slrs(sliiders_ds, slr_ds):
557573
"""Add a variable to ``sliiders_ds`` called `SLR_site_id` that contains the nearest
558-
SLR site to each segment."""
574+
SLR site to each segment.
575+
"""
559576
sliiders_lonlat = sliiders_ds[["seg_lon", "seg_lat"]].to_dataframe()
560577
return sliiders_ds.assign(
561578
SLR_site_id=get_nearest_slrs(slr_ds, sliiders_lonlat).to_xarray()
@@ -672,7 +689,7 @@ def load_ciam_inputs(
672689
seg_vals,
673690
# dropping the "refA_scenario_selectors" b/c this doesn't need to be added to
674691
# the input dataset object
675-
constants=params[params.map(type) != dict],
692+
constants=params[params.map(type) != dict].to_dict(),
676693
seg_var=seg_var,
677694
selectors=selectors,
678695
storage_options=storage_options,
@@ -771,7 +788,7 @@ def load_diaz_inputs(
771788
inputs = prep_sliiders(
772789
input_store,
773790
seg_vals,
774-
constants=params[params.map(type) != dict],
791+
constants=params[params.map(type) != dict].to_dict(),
775792
seg_var="seg",
776793
calc_popdens_with_wetland_area=False,
777794
storage_options=storage_options,

0 commit comments

Comments
 (0)