Skip to content

Commit

Permalink
several improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Oct 14, 2023
1 parent ca35344 commit a2cb479
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 9 additions & 5 deletions dfm_tools/hydrolib_helpers.py
Original file line number Diff line number Diff line change
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, conversion_dict:dict, npoints=None) -> xr.Dataset:
def ForcingModel_to_plipointsDataset(forcingmodel:hcdfm.ForcingModel, npoints=None) -> 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 @@ -281,11 +281,15 @@ def ForcingModel_to_plipointsDataset(forcingmodel:hcdfm.ForcingModel, conversion
for forcinglike in forcingmodel.forcing[:npoints]:
ds_onepoint = forcinglike_to_Dataset(forcinglike)
ds_onepoint = ds_onepoint.expand_dims(dimn_point)
datavar0 = list(ds_onepoint.data_vars)[0] #TODO: this is tricky, preferrably loop over datavars?
for datavar in ds_onepoint.data_vars:
longname = datavar
if datavar in ['ux','uy']: #TODO: hardcoded behaviour is consitent with maybe_convert_fews_to_dfmt() and elsewhere in dfm_tools, but not desireable
longname = 'uxuyadvectionvelocitybnd'
var_attrs = {'long_name': longname, #TODO: add long_name/units attrs per variable?

Check warning on line 288 in dfm_tools/hydrolib_helpers.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/hydrolib_helpers.py#L277-L288

Added lines #L277 - L288 were not covered by tests
'units': ds_onepoint[datavar].attrs['units']}
ds_onepoint[datavar] = ds_onepoint[datavar].assign_attrs(var_attrs)
datavar0 = list(ds_onepoint.data_vars)[0] #TODO: is ordering of two variables the same?
pointname = ds_onepoint[datavar0].attrs['locationname']
var_attrs = {'long_name': ds_onepoint[datavar0].attrs['units'],
'units': ds_onepoint[datavar0].attrs['units'],
}
ds_onepoint[varn_pointname] = xr.DataArray([pointname],dims=dimn_point)
ds_onepoint = ds_onepoint.set_coords(varn_pointname)
plipointsDataset_list.append(ds_onepoint)
Expand Down
6 changes: 4 additions & 2 deletions dfm_tools/interpolate_grid2bnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def interp_hisnc_to_plipoints(data_xr_his, file_pli, kdtree_k=3, load=True):
return data_interp


def _maybe_convert_fews_to_dfmt(ds):
def maybe_convert_fews_to_dfmt(ds):
ncbnd_construct = get_ncbnd_construct()
varn_pointname = ncbnd_construct['varn_pointname']

Check warning on line 546 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L545-L546

Added lines #L545 - L546 were not covered by tests

Expand All @@ -552,6 +552,8 @@ def _maybe_convert_fews_to_dfmt(ds):

# rename data_vars to long_name
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 559 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L554-L559

Added lines #L554 - L559 were not covered by tests
Expand All @@ -573,7 +575,7 @@ def plipointsDataset_to_ForcingModel(plipointsDataset):
dimn_depth = ncbnd_construct['dimn_depth']
varn_pointname = ncbnd_construct['varn_pointname']

Check warning on line 576 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L573-L576

Added lines #L573 - L576 were not covered by tests

plipointsDataset = _maybe_convert_fews_to_dfmt(plipointsDataset)
plipointsDataset = maybe_convert_fews_to_dfmt(plipointsDataset)

Check warning on line 578 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L578

Added line #L578 was not covered by tests

quantity_list = list(plipointsDataset.data_vars)
npoints = len(plipointsDataset[dimn_point])

Check warning on line 581 in dfm_tools/interpolate_grid2bnd.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/interpolate_grid2bnd.py#L581

Added line #L581 was not covered by tests
Expand Down

0 comments on commit a2cb479

Please sign in to comment.