Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix get_mesh after xugrid update #848

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Fixed
-----
- Bug in `raster.transform` with lazy coordinates. (#801)
- Bug in `workflows.mesh.mesh2d_from_rasterdataset` with multi-dimensional coordinates. (#843)
- Bug in `MeshModel.get_mesh` after xugrid update to 0.9.0. (#848)


v0.9.4 (2024-02-26)
Expand Down
26 changes: 18 additions & 8 deletions hydromt/models/model_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,29 @@ def get_mesh(
if grid_name not in self.mesh_names:
raise ValueError(f"Grid {grid_name} not found in mesh.")
if include_data:
grid = self.mesh_grids[grid_name]
uds = xu.UgridDataset(grid.to_dataset(optional_attributes=True))
uds.ugrid.grid.set_crs(grid.crs)
# Look for data_vars that are defined on grid_name
variables = []
for var in self.mesh.data_vars:
if hasattr(self.mesh[var], "ugrid"):
if self.mesh[var].ugrid.grid.name == grid_name:
uds[var] = self.mesh[var]
# additionnal topology properties
elif var.startswith(grid_name):
uds[var] = self.mesh[var]
if self.mesh[var].ugrid.grid.name != grid_name:
variables.append(var)
# additional topology properties
elif not var.startswith(grid_name):
variables.append(var)
# else is global property (not grid specific)

if variables and len(variables) < len(self.mesh.data_vars):
uds = self.mesh.drop_vars(variables)
# Drop coords as well
drop_coords = [c for c in uds.coords if not c.startswith(grid_name)]
uds = uds.drop_vars(drop_coords)
elif variables and len(variables) == len(self.mesh.data_vars):
grid = self.mesh_grids[grid_name]
uds = xu.UgridDataset(grid.to_dataset(optional_attributes=True))
uds.ugrid.grid.set_crs(grid.crs)
else:
uds = self.mesh.copy()

return uds

else:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,5 +840,7 @@ def test_meshmodel_setup(griduda, world):
resampling_method=["mode", "centroid"],
grid_name="mesh2d",
)
ds_mesh2d = mod1.get_mesh("mesh2d", include_data=True)
assert "vito" in ds_mesh2d
assert "roughness_manning" in mod1.mesh.data_vars
assert np.all(mod1.mesh["landuse"].values == mod1.mesh["vito"].values)
Loading