Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Oct 16, 2023
1 parent a6d7240 commit 445d2bf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
10 changes: 5 additions & 5 deletions dfm_tools/hydrolib_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def get_ncbnd_construct():
"axis":"Z",
}

ncbnd_construct = {"varn_depth":"depth",
"dimn_depth":"depth",
ncbnd_construct = {"varn_depth":"z",
"dimn_depth":"z",
"varn_pointx":"lon",
"varn_pointy":"lat",
"varn_pointname":"station_id",
"dimn_point":"location",
"dimn_point":"node",
"attrs_pointx":attrs_pointx,
"attrs_pointy":attrs_pointy,
"attrs_depth":attrs_depth,
Expand Down Expand Up @@ -270,7 +270,7 @@ def DataFrame_to_TimModel(tim_pd, refdate:(dt.datetime, pd.Timestamp, str)):
return timmodel


def ForcingModel_to_plipointsDataset(forcingmodel:hcdfm.ForcingModel, npoints=None) -> xr.Dataset:
def ForcingModel_to_plipointsDataset(forcingmodel:hcdfm.ForcingModel, npoints=None, convertnan=False) -> xr.Dataset:
if not isinstance(forcingmodel, hcdfm.ForcingModel):
raise TypeError('ForcingModel_to_plipointsDataset expects type hcdfm.ForcingModel, not type {type(forcingobj)}')

Check warning on line 275 in dfm_tools/hydrolib_helpers.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/hydrolib_helpers.py#L274-L275

Added lines #L274 - L275 were not covered by tests

Expand All @@ -279,7 +279,7 @@ def ForcingModel_to_plipointsDataset(forcingmodel:hcdfm.ForcingModel, npoints=No
varn_pointname = ncbnd_construct['varn_pointname']
plipointsDataset_list = []
for forcinglike in forcingmodel.forcing[:npoints]:
ds_onepoint = forcinglike_to_Dataset(forcinglike)
ds_onepoint = forcinglike_to_Dataset(forcinglike, convertnan=convertnan)
ds_onepoint = ds_onepoint.expand_dims(dimn_point)
for datavar in ds_onepoint.data_vars:
longname = datavar
Expand Down
18 changes: 5 additions & 13 deletions dfm_tools/interpolate_grid2bnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,36 +545,28 @@ def maybe_convert_fews_to_dfmt(ds):
ncbnd_construct = get_ncbnd_construct()
dimn_point = ncbnd_construct['dimn_point']
varn_pointname = ncbnd_construct['varn_pointname']

Check warning on line 547 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L545-L547

Added lines #L545 - L547 were not covered by tests
dimn_depth = ncbnd_construct['dimn_depth']
varn_depth = ncbnd_construct['varn_depth']

# potential FEWS converts
# convert station data_vars to coords to avoid dfmt issues
for var_to_coord in [varn_pointname,'station_names']:
if var_to_coord in ds.data_vars:
ds = ds.set_coords(var_to_coord)

Check warning on line 552 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L550-L552

Added lines #L550 - L552 were not covered by tests
# assign timeseries_id cf_role to let FM read the station names
ds[varn_pointname] = ds[varn_pointname].assign_attrs({'cf_role': 'timeseries_id'})

Check warning on line 554 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L554

Added line #L554 was not covered by tests

# rename data_vars to long_name (e.g. renames so to salinitybnd)
# rename data_vars to long_name (e.g. renames FEWS so to salinitybnd)
for datavar in ds.data_vars:
if datavar in ['ux','uy']: #TODO: keeping these is consistent with hardcoded behaviour in dfm_tools elsewhere, but not desireable
continue
if hasattr(ds[datavar],'long_name'):
longname = ds[datavar].attrs['long_name']
ds = ds.rename_vars({datavar:longname})

Check warning on line 562 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L557-L562

Added lines #L557 - L562 were not covered by tests

# rename dims/vars
if 'node' in ds.dims:
ds = ds.rename_dims({'node':dimn_point})
if 'z' in ds.dims:
ds = ds.rename_dims({'z':dimn_depth})
if 'z' in ds.variables:
ds = ds.rename_vars({'z':varn_depth})

# transpose dims #TODO: the order impacts the model results: https://issuetracker.deltares.nl/browse/UNST-7402
# dfmt (arbitrary) dimension ordering is node/time/z
# required to reorder to FEWS time/node/z order for comparable results
# also time/z/node will result in unexpected results
ds = ds.transpose("time", dimn_point, ...)
if "time" in ds.dims: # check if time dimension is present (astronomic does not have time)
ds = ds.transpose("time", dimn_point, ...)

Check warning on line 569 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L568-L569

Added lines #L568 - L569 were not covered by tests

# convert station names to string format (keep attrs and encoding)
# also needed to properly export, since we cannot encode it at dtype S1 properly otherwise
Expand Down
2 changes: 2 additions & 0 deletions tests/examples/preprocess_hydrolib_readbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
fig, ax = plt.subplots(figsize=(12, 6))
forcingobj = m.forcing[1]
forcing_xr = dfmt.forcinglike_to_Dataset(forcingobj, convertnan=True)
forcing_xr_all = dfmt.ForcingModel_to_plipointsDataset(m, convertnan=True)
forcing_xr2 = forcing_xr_all.sel(node=1)
data_vars = list(forcing_xr.data_vars)
if forcingobj.function=='t3d':
forcing_ts = dfmt.Dataset_to_T3D(forcing_xr)
Expand Down

0 comments on commit 445d2bf

Please sign in to comment.