Skip to content

Commit 821802e

Browse files
committed
allow for restarting of surge lookup creation
1 parent f296a2e commit 821802e

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

pyCIAM/run.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ def execute_pyciam(
936936
tmp_output_path=AnyPath("pyciam_tmp_results.zarr"),
937937
remove_tmpfile=True,
938938
overwrite=False,
939+
no_surge_check=False,
939940
mc_dim="quantile",
940941
seg_var="seg_adm",
941942
seg_var_subset=None,
@@ -1019,6 +1020,8 @@ def execute_pyciam(
10191020
want to examine seg-adm level results.
10201021
ovewrwrite : bool, default False
10211022
If True, overwrite all intermediate output files
1023+
no_surge_check : bool, default False
1024+
If True, assume surge lookup tables are complete and do not load to check.
10221025
mc_dim : str, default "quantile"
10231026
The dimension of the sea level rise datasets specified at `slr_input_paths` that
10241027
indexes different simulations within the same scenario. This could reflect Monte
@@ -1079,12 +1082,12 @@ def execute_pyciam(
10791082
some environment variables in order for the :py:mod:`cloudpathlib` objects to
10801083
function correctly. For example, if your data exists on Google Cloud Storage and
10811084
requires authentication, you would need to set the
1082-
`GOOGLE_APPLICATION_CREDENTIALS` environment variable to the same path as
1083-
reflected in `storage_options["token"]`. Other cloud storage providers will have
1085+
``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to the same path as
1086+
reflected in ``storage_options["token"]``. Other cloud storage providers will have
10841087
different authentication methods and have not yet been tested with this
10851088
function.
10861089
params_override : dict, default {}
1087-
Used to override params specified in `params_path`
1090+
Used to override params specified in ``params_path``.
10881091
**model_kwargs
10891092
Passed directly to :py:func:`pyCIAM.calc_costs`
10901093
"""
@@ -1176,7 +1179,7 @@ def execute_pyciam(
11761179
for var, path in surge_input_paths.items():
11771180
if path is None:
11781181
continue
1179-
if overwrite or not path.is_dir():
1182+
if not no_surge_check:
11801183
if var == seg_var:
11811184
this_econ_input = econ_input_path
11821185
elif var == "seg":
@@ -1199,7 +1202,7 @@ def execute_pyciam(
11991202
slr_0_years=params.slr_0_year,
12001203
client=client,
12011204
client_kwargs={"batch_size": surge_batchsize},
1202-
force_overwrite=True,
1205+
force_overwrite=overwrite,
12031206
seg_chunksize=surge_seg_chunksize,
12041207
mc_dim=mc_dim,
12051208
storage_options=storage_options,
@@ -1272,7 +1275,7 @@ def execute_pyciam(
12721275

12731276
# add attrs
12741277
out_ds.attrs.update(attr_dict)
1275-
out_ds = add_attrs_to_result(out_ds)
1278+
out_ds = add_attrs_to_result(out_ds, seg_var)
12761279

12771280
if overwrite or not tmp_output_path.is_dir():
12781281
out_ds.to_zarr(
@@ -1340,7 +1343,10 @@ def execute_pyciam(
13401343
i += most_segadm
13411344
else:
13421345
this_group = this_group.isel(
1343-
{seg_var: this_group.seg != this_group.seg.isel(seg_adm=-1, drop=True)}
1346+
{
1347+
seg_var: this_group.seg
1348+
!= this_group.seg.isel({seg_var: -1}, drop=True)
1349+
}
13441350
)
13451351
i += len(this_group[seg_var])
13461352

@@ -1378,7 +1384,17 @@ def execute_pyciam(
13781384
**model_kwargs,
13791385
)
13801386
)
1381-
1387+
return xr.concat(
1388+
client.gather(
1389+
client.map(
1390+
optimize_case_seg,
1391+
ciam_futs,
1392+
dfact=test_inputs.dfact,
1393+
npv_start=test_inputs.npv_start,
1394+
)
1395+
),
1396+
dim="seg",
1397+
)
13821398
if output_path is None and seg_var == "seg":
13831399
out = add_attrs_to_result(
13841400
xr.concat(
@@ -1391,7 +1407,8 @@ def execute_pyciam(
13911407
)
13921408
),
13931409
dim="seg",
1394-
)
1410+
),
1411+
seg_var,
13951412
)
13961413
out.attrs.update(attr_dict)
13971414
return out

pyCIAM/surge/lookup.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
import pandas as pd
2525
import xarray as xr
2626

27-
from pyCIAM.io import _load_lslr_for_ciam, save_to_zarr_region
27+
from pyCIAM.io import (
28+
_load_lslr_for_ciam,
29+
check_finished_zarr_workflow,
30+
save_to_zarr_region,
31+
)
2832
from pyCIAM.surge._calc import (
2933
_calc_storm_damages_no_resilience,
3034
_get_surge_heights_probs,
@@ -241,9 +245,18 @@ def _save_storm_dam(
241245
mc_dim="mc_sample_id",
242246
start_year=None,
243247
slr_0_years=2005,
248+
overwrite=False,
244249
storage_options={},
245250
):
246251
"""Map over each chunk to run through damage calcs."""
252+
253+
if not overwrite and check_finished_zarr_workflow(
254+
surge_lookup_store,
255+
varname="frac_losses",
256+
final_selector={seg_var: seg_vals},
257+
storage_options=storage_options,
258+
):
259+
return None
247260
diff_ranges = _get_lslr_rhdiff_range(
248261
sliiders_store,
249262
slr_stores,
@@ -499,6 +512,7 @@ def create_surge_lookup(
499512
start_year=start_year,
500513
slr_0_years=slr_0_years,
501514
storage_options=storage_options,
515+
overwrite=force_overwrite,
502516
**client_kwargs,
503517
)
504518
)

0 commit comments

Comments
 (0)