Skip to content

Commit

Permalink
removed grid variable and attrs from delft3d4 uds (#844)
Browse files Browse the repository at this point in the history
* removed grid variable and attrs from delft3d4 uds

* added d3d testcase

* updated whatsnew
  • Loading branch information
veenstrajelmer committed May 15, 2024
1 parent 889d054 commit d3079be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
18 changes: 17 additions & 1 deletion dfm_tools/xugrid_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,25 @@ def open_dataset_delft3d4(file_nc, **kwargs):
ds_stacked = ds_stacked.drop_vars(['M','N','mesh2d_nFaces'])
uds = xu.UgridDataset(ds_stacked,grids=[grid])

uds = uds.drop_vars(['XCOR','YCOR'])
uds = uds.drop_vars(['XCOR','YCOR','grid'])
uds = uds.drop_dims(['MC','NC']) #clean up dataset by dropping corner dims (drops also variabes with U/V masks and U/V/C bedlevel)

# convert to xarray.Dataset to update/remove attrs
ds_temp = uds.ugrid.to_dataset()

# set vertical dimensions attr
# TODO: would be more convenient to do within xu.Ugrid2d(): https://github.com/Deltares/xugrid/issues/195#issuecomment-2111841390
grid_attrs = {"vertical_dimensions": ds.grid.attrs["vertical_dimensions"]}
ds_temp["mesh2d"] = ds_temp["mesh2d"].assign_attrs(grid_attrs)

# drop attrs pointing to the removed grid variable (topology is now in mesh2d)
# TODO: this is not possible on the xu.UgridDataset directly
for varn in ds_temp.data_vars:
if "grid" in ds_temp[varn].attrs.keys():
del ds_temp[varn].attrs["grid"]

uds = xu.UgridDataset(ds_temp)

return uds


Expand Down
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Fix
- improved `dfmt.download.copernicusmarine_reset()` by changing order of tasks in [#833](https://github.com/Deltares/dfm_tools/pull/833)
- improved performance of `dfmt.merge_meteofiles()` by using more generic chunking method in [#840](https://github.com/Deltares/dfm_tools/pull/840)
- improved `dfmt.open_dataset_delft3d4()` by dropping original `grid` variable and attrs in [#844](https://github.com/Deltares/dfm_tools/pull/844)


## 0.22.0 (2024-04-09)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_xugrid_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,17 @@ def test_enrich_rst_with_map():

uda_rst = uds_rst['DetCS1']
assert "mesh2d_face_x" in uda_rst.coords


@pytest.mark.unittest
def test_open_dataset_delft3d4():
file_nc = dfmt.data.d3d_curvedbend_trim(return_filepath=True)

uds = dfmt.open_dataset_delft3d4(file_nc)

assert "mesh2d" in uds.grid.to_dataset().data_vars
assert "vertical_dimensions" in uds.grid.attrs
assert "grid" not in uds.data_vars

# test if plotting works, this is a basic validation of whether it is a proper ugrid dataset
uds.umag.isel(time=-1, KMAXOUT_RESTR=-1).ugrid.plot()

0 comments on commit d3079be

Please sign in to comment.