2323import numpy as np
2424import pandas as pd
2525import xarray as xr
26+ from numpy .dtypes import StringDType
2627
2728from pyCIAM .io import (
2829 _load_lslr_for_ciam ,
2930 check_finished_zarr_workflow ,
30- save_to_zarr_region ,
3131)
3232from pyCIAM .surge ._calc import (
3333 _calc_storm_damages_no_resilience ,
@@ -65,7 +65,9 @@ def _get_lslr_rhdiff_range(
6565 include_ncc = True ,
6666 slr_0_years = 2005 ,
6767 mc_dim = "mc_sample_id" ,
68- storage_options = {},
68+ site_id_dim = "site_id" ,
69+ lsl_var = "lsl_msl05" ,
70+ storage_options = None ,
6971):
7072 """Get range of lslr and rhdiff that we need to model to cover the full range.
7173
@@ -82,7 +84,7 @@ def _get_lslr_rhdiff_range(
8284 pc_in = _s2d (
8385 xr .open_zarr (
8486 str (sliiders_store ), chunks = None , storage_options = storage_options
85- ).sel ({seg_var : seg_vals })
87+ ).sel ({seg_var : seg_vals , "year" : slice ( min ( slr_0_years ), None ) })
8688 )
8789
8890 if interp_years is None :
@@ -104,9 +106,11 @@ def _get_lslr_rhdiff_range(
104106 include_cc = include_cc ,
105107 include_ncc = include_ncc ,
106108 mc_dim = mc_dim ,
109+ lsl_var = lsl_var ,
107110 slr_0_year = slr_0_years [sx ],
108111 storage_options = storage_options ,
109- quantiles = quantiles ,
112+ site_id_dim = site_id_dim ,
113+ quantiles = [0 , 1 ] if mc_dim != "quantile" else quantiles ,
110114 )
111115
112116 # get the max LSLR experienced
@@ -179,7 +183,7 @@ def _create_surge_lookup_skeleton_store(
179183 seg_var = "seg" ,
180184 seg_var_subset = None ,
181185 force_overwrite = True ,
182- storage_options = {} ,
186+ storage_options = None ,
183187):
184188 pc_in = subset_econ_inputs (
185189 xr .open_zarr (str (sliiders_store ), storage_options = storage_options ),
@@ -190,15 +194,17 @@ def _create_surge_lookup_skeleton_store(
190194 to_save = xr .DataArray (
191195 da .empty (
192196 (len (pc_in [seg_var ]), n_interp_pts_lslr , n_interp_pts_rhdiff , 2 , 2 ),
193- chunks = (seg_chunksize , - 1 , - 1 , - 1 , - 1 ),
197+ chunks = (seg_chunksize , - 1 , - 1 , - 1 , - 1 ), dtype = "float64"
194198 ),
195199 dims = [seg_var , "lslr" , "rh_diff" , "costtype" , "adapttype" ],
196200 coords = {
197201 seg_var : pc_in [seg_var ].values ,
198202 "lslr" : np .arange (n_interp_pts_lslr ),
199203 "rh_diff" : np .arange (n_interp_pts_rhdiff ),
200- "adapttype" : ["retreat" , "protect" ],
201- "costtype" : ["stormCapital" , "stormPopulation" ],
204+ "adapttype" : np .array (["retreat" , "protect" ], dtype = StringDType ),
205+ "costtype" : np .array (
206+ ["stormCapital" , "stormPopulation" ], dtype = StringDType
207+ ),
202208 },
203209 ).to_dataset (name = "frac_losses" )
204210 to_save ["rh_diff_by_seg" ] = (
@@ -232,6 +238,7 @@ def _create_surge_lookup_skeleton_store(
232238def _save_storm_dam (
233239 seg_vals ,
234240 seg_var = "seg" ,
241+ lsl_var = "lsl_msl05" ,
235242 sliiders_store = None ,
236243 slr_stores = None ,
237244 surge_lookup_store = None ,
@@ -243,10 +250,11 @@ def _save_storm_dam(
243250 quantiles = None ,
244251 scen_mc_filter = None ,
245252 mc_dim = "mc_sample_id" ,
253+ slr_site_id_dim = "site_id" ,
246254 start_year = None ,
247255 slr_0_years = 2005 ,
248256 overwrite = False ,
249- storage_options = {} ,
257+ storage_options = None ,
250258):
251259 """Map over each chunk to run through damage calcs."""
252260
@@ -268,6 +276,8 @@ def _save_storm_dam(
268276 quantiles = quantiles ,
269277 scen_mc_filter = scen_mc_filter ,
270278 mc_dim = mc_dim ,
279+ lsl_var = lsl_var ,
280+ site_id_dim = slr_site_id_dim ,
271281 slr_0_years = slr_0_years ,
272282 storage_options = storage_options ,
273283 )
@@ -281,18 +291,26 @@ def _save_storm_dam(
281291 # these must be unique otherwise interp function will raise error
282292 template ["lslr_by_seg" ] = (
283293 (seg_var , "lslr" ),
284- np .tile (np .arange (len (template .lslr ))[np .newaxis , :], (len (seg_vals ), 1 )),
294+ np .tile (
295+ np .arange (len (template .lslr ), dtype = template .lslr_by_seg .dtype )[
296+ np .newaxis , :
297+ ],
298+ (len (seg_vals ), 1 ),
299+ ),
285300 )
286301 template ["rh_diff_by_seg" ] = (
287302 (seg_var , "rh_diff" ),
288303 np .tile (
289- np .arange (len (template .rh_diff ))[np .newaxis , :], (len (seg_vals ), 1 )
304+ np .arange (len (template .rh_diff ), dtype = template .rh_diff_by_seg .dtype )[
305+ np .newaxis , :
306+ ],
307+ (len (seg_vals ), 1 ),
290308 ),
291309 )
292310 if surge_lookup_store is None :
293311 return template
294- save_to_zarr_region (
295- template , surge_lookup_store , storage_options = storage_options
312+ template . to_zarr (
313+ str ( surge_lookup_store ) , storage_options = storage_options , region = "auto"
296314 )
297315 return None
298316
@@ -367,7 +385,7 @@ def _save_storm_dam(
367385 return res
368386
369387 # identify which index to save to in template zarr
370- save_to_zarr_region ( res , surge_lookup_store , storage_options = storage_options )
388+ res . to_zarr ( str ( surge_lookup_store ) , storage_options = storage_options , region = "auto" )
371389
372390
373391def create_surge_lookup (
@@ -387,10 +405,12 @@ def create_surge_lookup(
387405 scen_mc_filter = None ,
388406 quantiles = None ,
389407 mc_dim = "mc_sample_id" ,
408+ slr_site_id_dim = "site_id" ,
409+ lsl_var = "lsl_msl05" ,
390410 force_overwrite = False ,
391411 client = None ,
392412 client_kwargs = {},
393- storage_options = {} ,
413+ storage_options = None ,
394414):
395415 """Create storm surge lookup table.
396416
@@ -418,6 +438,10 @@ def create_surge_lookup(
418438 equivalent to segments. The reason you may wish to have nested regions is to
419439 be able to aggregate impacts to a different regions than those that are defined
420440 by the segments.
441+ lsl_var : str, default "lsl_msl05"
442+ The name of the variable in ``slr_stores`` containing local SLR values
443+ slr_site_id_dim : str, default "site_id"
444+ The name of the location dimension in ``slr_stores``.
421445 at_start : list of int
422446 A list specifying the starting years of each adpatation period. In pyCIAM, each
423447 segment chooses a new retreat or protection height at the start of each of these
@@ -505,6 +529,8 @@ def create_surge_lookup(
505529 n_interp_pts_lslr = n_interp_pts_lslr ,
506530 n_interp_pts_rhdiff = n_interp_pts_rhdiff ,
507531 mc_dim = mc_dim ,
532+ lsl_var = lsl_var ,
533+ slr_site_id_dim = slr_site_id_dim ,
508534 ddf_i = ddf_i ,
509535 dmf_i = dmf_i ,
510536 quantiles = quantiles ,
0 commit comments