Skip to content

Issue #366: make mask method available to MetaSWAP model class#1871

Open
rleander73 wants to merge 6 commits into
masterfrom
Issue_#366_Add_mask_method_to_MetaSWAP_model
Open

Issue #366: make mask method available to MetaSWAP model class#1871
rleander73 wants to merge 6 commits into
masterfrom
Issue_#366_Add_mask_method_to_MetaSWAP_model

Conversation

@rleander73

@rleander73 rleander73 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #366

Description

Checklist

  • [*] Links to correct issue
  • [-] Update changelog, if changes affect users
  • [*] PR title starts with Issue #nr, e.g. Issue #737
  • [*] Unit test was added:
  • [*] If feature added: Added/extended example (in docstring)
  • [*] If feature added: Added feature to API documentation
  • [-] If pixi.lock was changed: Ran pixi run generate-sbom and committed changes

@rleander73 rleander73 marked this pull request as ready for review June 25, 2026 14:49

@JoerivanEngelen JoerivanEngelen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some comments on the public API, after which we have a very useful method!

Comment thread imod/tests/test_msw/test_model.py Outdated
# Check that the mask has been applied correctly to each package
for pkgname, pkg in msw_model.items():
if isinstance(pkg, msw.meteo_mapping.PrecipitationMapping):
continue # Skip PrecipitationMapping package for this test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we skipping the PrecipitationMapping? The actual mappings are derived upon writing, if I recall correctly, so I think these objects should be identical? Or am I missing something?

Comment thread imod/msw/model.py Outdated

Parameters
----------
msw_active: MetaSwapActive, dictionary of xr.DataArray

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MetaSwapActive currenlty is a developer utility, not public API.

Adding extra dataclasses to the API adds extra entry barriers for users, which I like to avoid: They need to import additional python objects, which they need to look up in the docs.

The "all" mask usually is an .any(dim="subunit"), so I think we can derive that for the user, I would do something like:

def mask_all_packages(self, mask: GridDataArray):
     ...
     mask_all = mask.any(dim="subunit")
     msw_active = MetaSwapActive(per_subunit=mask, all=mask_all)
     ...

Comment thread imod/msw/model.py Outdated
@sonarqubecloud

sonarqubecloud Bot commented Jul 2, 2026

Copy link
Copy Markdown

Comment thread imod/msw/model.py
Comment on lines +562 to +566
nsub = 1
for pkg in self.values():
if "subunit" in pkg.dataset.dims:
nsub = pkg.dataset.dims["subunit"]
break

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Please move this to a helper method, it might come of use to call in other methods as well.

@JoerivanEngelen JoerivanEngelen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I have 2 minor comments, than this is ready to merge!

Comment thread imod/msw/model.py
Comment on lines +527 to +532
Parameters
----------
mask: GridDataArray
idomain-like integers. >0 sets cells to active, 0 sets cells to inactive,
mask is applied on a per-subunit basis if the package has a subunit dimension.
If the package does not have a subunit dimension, the mask is applied to all subunits of the package.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Parameters
----------
mask: GridDataArray
idomain-like integers. >0 sets cells to active, 0 sets cells to inactive,
mask is applied on a per-subunit basis if the package has a subunit dimension.
If the package does not have a subunit dimension, the mask is applied to all subunits of the package.
Parameters
----------
mask: GridDataArray
idomain-like integers. >0 sets cells to active, 0 sets cells to inactive,
mask is applied on a per-subunit basis if the mask grid has a subunit dimension.
If the package does not have a subunit dimension, the mask grid is applied to all subunits of the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add mask method to MetaSWAP model

3 participants