From 46c0bdf94a4739cb91f27c4e1f115a79d495d51b Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:57:04 +0200 Subject: [PATCH] 990 remove manual adjustment of start index (#991) * replaced start_index workaround by new xugrid setting * updated minimal xugrid version * updated whatsnew --- dfm_tools/meshkernel_helpers.py | 12 ++++-------- docs/whats-new.md | 1 + pyproject.toml | 4 ++-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/dfm_tools/meshkernel_helpers.py b/dfm_tools/meshkernel_helpers.py index f41238464..e8925ca51 100644 --- a/dfm_tools/meshkernel_helpers.py +++ b/dfm_tools/meshkernel_helpers.py @@ -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) diff --git a/docs/whats-new.md b/docs/whats-new.md index 57d831a98..907f68eec 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 8e38e4b5b..63f843dcd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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