Skip to content

Issue #1510 Print unexpected coordinate names in mask_all_packages #1513

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

Merged
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
8 changes: 8 additions & 0 deletions docs/api/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ The format is based on `Keep a Changelog`_, and this project adheres to
[Unreleased]
------------

Fixed
~~~~~

- Upon providing an unexpected coordinate in the mask or regridding grid,
:meth:`imod.mf6.Modflow6Simulation.regrid_like` and
:meth:`imod.mf6.Modflow6Simulation.mask` now present the unexpected
coordinates in the error message.

Changed
~~~~~~~

Expand Down
29 changes: 18 additions & 11 deletions imod/common/utilities/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@
dispatch = Dispatcher()


def _validate_coords_mask(mask: GridDataArray) -> None:
"""
Validate that the coordinates of the mask are valid.
"""
spatial_dimension_names = get_spatial_dimension_names(mask)
# Add any additional dimensions that are not part of the spatial dimensions.
# These are dimensions that are returned by xugrid
additional_dimension_names = ["dx", "dy"]
dimension_names = spatial_dimension_names + additional_dimension_names
unexpected_coords = set(mask.coords) - set(dimension_names)
if len(unexpected_coords) > 0:
raise ValueError(
f"Unexpected coordinates in masking domain: {unexpected_coords}"
)


def _mask_all_models(
simulation: ISimulation,
mask: GridDataArray,
):
spatial_dims = get_spatial_dimension_names(mask)
if any(coord not in spatial_dims for coord in mask.coords):
raise ValueError("unexpected coordinate dimension in masking domain")

_validate_coords_mask(mask)
if simulation.is_split():
raise ValueError(
"masking can only be applied to simulations that have not been split. Apply masking before splitting."
Expand All @@ -46,13 +59,7 @@ def _mask_all_packages(
model: IModel,
mask: GridDataArray,
):
spatial_dimension_names = get_spatial_dimension_names(mask)
# Add any additional dimensions that are not part of the spatial dimensions. These are dimensions that are returned by xugrid
additional_dimension_names = ["dx", "dy"]
dimension_names = spatial_dimension_names + additional_dimension_names
if any(coord not in dimension_names for coord in mask.coords):
raise ValueError("unexpected coordinate dimension in masking domain")

_validate_coords_mask(mask)
for pkgname, pkg in model.items():
model[pkgname] = pkg.mask(mask)
model.purge_empty_packages()
Expand Down
5 changes: 3 additions & 2 deletions imod/tests/test_mf6/test_mf6_model_masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ def test_mask_unstructured(


def test_mask_with_time_coordinate(
tmp_path: Path,
unstructured_flow_model: GroundwaterFlowModel,
):
nlayer = 3
Expand All @@ -251,7 +250,9 @@ def test_mask_with_time_coordinate(
mask.sel(time=1).values = np.array([1, 1, 0])
mask.sel(time=2).values = np.array([1, 0, 1])

with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Unexpected coordinates in masking domain: {'time'}"
):
unstructured_flow_model.mask_all_packages(mask)


Expand Down
14 changes: 5 additions & 9 deletions imod/tests/test_mf6/test_mf6_regrid_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,11 @@ def test_regrid_unstructured():

new_idomain = new_packages[0].dataset["icelltype"]
for new_package in new_packages:
# TODO github-398: package write validation crashes for VerticesDiscretization so we skip that one
if not isinstance(new_package, imod.mf6.VerticesDiscretization):
errors = new_package._validate(
new_package._write_schemata,
idomain=new_idomain,
)
assert len(errors) == 0
else:
continue
errors = new_package._validate(
new_package._write_schemata,
idomain=new_idomain,
)
assert len(errors) == 0


def test_regrid_structured_missing_dx_and_dy():
Expand Down