Skip to content

Commit

Permalink
990 remove manual adjustment of start index (#991)
Browse files Browse the repository at this point in the history
* replaced start_index workaround by new xugrid setting

* updated minimal xugrid version

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Sep 9, 2024
1 parent 1d0ed3d commit 46c0bdf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
12 changes: 4 additions & 8 deletions dfm_tools/meshkernel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,13 @@ def meshkernel_to_UgridDataset(mk:meshkernel.MeshKernel, crs:(int,str) = None) -
raise ValueError(f"crs has is_geographic={crs_is_geographic} and grid has is_geographic={grid_is_geographic}. This is not allowed.")

# TODO: below is not correctly handled by xugrid yet, projected=False does not give is_geographic=True
# related issue is https://github.com/Deltares/dfm_tools/issues/686
# related issue is https://github.com/Deltares/xugrid/issues/187
xu_grid = xu.Ugrid2d.from_meshkernel(mesh2d_grid, projected= not crs_is_geographic, crs=crs)

#convert 0-based to 1-based indices for connectivity variables like face_node_connectivity
# convert 0-based to 1-based indices for connectivity variables like face_node_connectivity
# this is required by delft3dfm
xu_grid.start_index = 1
xu_grid_ds = xu_grid.to_dataset()
xu_grid_ds = xr.decode_cf(xu_grid_ds) #decode_cf is essential since it replaces fillvalues with nans
ds_idx = xu_grid_ds.filter_by_attrs(start_index=0)
for varn_conn in ds_idx.data_vars:
xu_grid_ds[varn_conn] += 1 #from startindex 0 to 1 (fillvalues are now nans)
xu_grid_ds[varn_conn].attrs["start_index"] += 1
xu_grid_ds[varn_conn].encoding["_FillValue"] = -1 #can be any value <=0, but not 0 is currently the most convenient for proper xugrid plots.

# convert to uds and add attrs and crs
xu_grid_uds = xu.UgridDataset(xu_grid_ds)
Expand Down
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- deprecated `dfmt.open_dataset_extra()` (partly replaced by `dfmt.open_prepare_dataset()`) in [#974](https://github.com/Deltares/dfm_tools/pull/974)
- improved nan-conversion in `dfmt.forcinglike_to_Dataset()` in [#982](https://github.com/Deltares/dfm_tools/pull/982)
- improved performance of `dfmt.open_partitioned_dataset()` for datasets with many variables in [#984](https://github.com/Deltares/dfm_tools/pull/984)
- simplified `dfmt.meshkernel_to_UgridDataset()` by using new xugrid version in [#991](https://github.com/Deltares/dfm_tools/pull/991)


## 0.25.0 (2024-08-16)
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ dependencies = [
"netcdf4>=1.5.4",
#bottleneck<1.3.3 pip install fails in py39
"bottleneck>=1.3.3",
#xugrid<0.12.0 has sub-optimal performance because of accessing dataarrays of variables
"xugrid>=0.12.0",
#xugrid<0.12.1 has issue with fill_value and no flexible start_index
"xugrid>=0.12.1",
#cdsapi<0.7.2 has different error upon dummy dataset
"cdsapi>=0.7.2",
#pydap<3.4.0 is from May 2017 and does not support newer python versions
Expand Down

0 comments on commit 46c0bdf

Please sign in to comment.