Skip to content

Commit

Permalink
prevented duplicate locationname in single forcing dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Oct 16, 2023
1 parent 5e5e214 commit a6d7240
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 5 additions & 1 deletion dfm_tools/hydrolib_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ def ForcingModel_to_plipointsDataset(forcingmodel:hcdfm.ForcingModel, npoints=No
plipointsDataset_list.append(ds_onepoint)
ds = xr.concat(plipointsDataset_list, dim=dimn_point)

# prevent circular import #TODO: solve underlaying issue
from dfm_tools.interpolate_grid2bnd import maybe_convert_fews_to_dfmt
ds = maybe_convert_fews_to_dfmt(ds)

return ds


Expand Down Expand Up @@ -372,7 +376,7 @@ def forcinglike_to_Dataset(forcingobj, convertnan=False):
data_xr_var.attrs['units'] = var_unit[iQ]
forcingobj_keys = forcingobj.__dict__.keys()
for key in forcingobj_keys: #['comments','name','function','offset','factor','vertinterpolation','vertpositiontype','timeinterpolation']:
if key in ['datablock','quantityunitpair','vertpositions']: #skipping these since they are in the DataArray already
if key in ['datablock','quantityunitpair','vertpositions','name']: #skipping these since they are in the DataArray already
continue
data_xr_var.attrs[key] = str(forcingobj.__dict__[key])

Expand Down
7 changes: 4 additions & 3 deletions dfm_tools/interpolate_grid2bnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,10 @@ def maybe_convert_fews_to_dfmt(ds):
ds = ds.transpose("time", dimn_point, ...)

# convert station names to string format (keep attrs and encoding)
# if not ds[varn_pointname].dtype.str.startswith('<'):
# with xr.set_options(keep_attrs=True):
# ds[varn_pointname] = ds[varn_pointname].load().str.decode('utf-8',errors='ignore').str.strip() #.load() is essential to convert not only first letter of string.
# also needed to properly export, since we cannot encode it at dtype S1 properly otherwise
if not ds[varn_pointname].dtype.str.startswith('<'):
with xr.set_options(keep_attrs=True):
ds[varn_pointname] = ds[varn_pointname].load().str.decode('utf-8',errors='ignore').str.strip() #.load() is essential to convert not only first letter of string.

# add relevant encoding if not present
ds[varn_pointname].encoding.update({'dtype': 'S1', 'char_dim_name': 'char_leng_id'})
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/preprocess_hydrolib_readbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
forcing_xr[data_vars[0]].T.plot(ax=ax)
elif forcingobj.function=='timeseries': #waterlevelbnd
forcing_ts = dfmt.Dataset_to_TimeSeries(forcing_xr)
forcing_xr[data_vars[0]].plot(ax=ax, label=forcing_xr[data_vars[0]].attrs['name'], linewidth=0.8)
forcing_xr[data_vars[0]].plot(ax=ax, label=forcing_xr[data_vars[0]].attrs['locationname'], linewidth=0.8)
ax.legend(loc=1)
elif forcingobj.function=='astronomic': #eg tidal components
#forcing_ts = Dataset_to_Astronomic(forcing_xr) #TODO: Dataset_to_Astronomic() expects different vars amplitude and phase_new
ax2 = ax.twinx()
forcing_xr[data_vars[0]].plot(ax=ax, linewidth=0.8)
forcing_xr[data_vars[1]].plot(ax=ax2, linewidth=0.8)
else:
forcing_xr.plot(ax=ax, label=forcing_xr.attrs['name'], linewidth=0.8)
forcing_xr.plot(ax=ax, label=forcing_xr.attrs['locationname'], linewidth=0.8)
ax.legend(loc=1)
raise Exception(f'non-defined function: {forcingobj.function}')
#ForcingModel_object_out.forcing.append(forcing_ts)
Expand Down

0 comments on commit a6d7240

Please sign in to comment.