-
Notifications
You must be signed in to change notification settings - Fork 9
Issue #366: make mask method available to MetaSWAP model class #1871
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
Open
rleander73
wants to merge
6
commits into
master
Choose a base branch
from
Issue_#366_Add_mask_method_to_MetaSWAP_model
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
550768d
first attempt: connected method for masking all packages in metaswap …
rleander 691efa8
ongoing work
rleander 8b63f31
added example to docstring
rleander a98dce7
remaining ruff error
rleander 25b4b78
processing review comments
rleander 9bd922c
adjust desciption of the mask method in the method header
rleander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,9 @@ | |
| from imod.common.utilities.version import prepend_content_with_version_info | ||
| from imod.mf6.dis import StructuredDiscretization | ||
| from imod.mf6.mf6_wel_adapter import Mf6Wel | ||
| from imod.msw import GridData | ||
| from imod.msw.copy_files import FileCopier | ||
| from imod.msw.coupler_mapping import CouplerMapping | ||
| from imod.msw.grid_data import GridData | ||
| from imod.msw.idf_mapping import IdfMapping | ||
| from imod.msw.infiltration import Infiltration | ||
| from imod.msw.initial_conditions import ( | ||
|
|
@@ -52,7 +52,11 @@ | |
| from imod.msw.utilities.imod5_converter import ( | ||
| has_active_scaling_factor, | ||
| ) | ||
| from imod.msw.utilities.mask import mask_and_broadcast_cap_data | ||
| from imod.msw.utilities.mask import ( | ||
| MetaSwapActive, | ||
| mask_and_broadcast_cap_data, | ||
| mask_and_broadcast_pkg_data, | ||
| ) | ||
| from imod.msw.utilities.parse import read_para_sim | ||
| from imod.msw.vegetation import AnnualCropFactors | ||
| from imod.typing import GridDataArray, Imod5DataDict | ||
|
|
@@ -511,6 +515,64 @@ | |
|
|
||
| return regridded_model | ||
|
|
||
| def mask_all_packages( | ||
| self, | ||
| mask: GridDataArray, | ||
| ): | ||
| """ | ||
| This function applies a mask to all packages in a model. The mask must | ||
| be presented as a GridDataArray, which contains idomain-like integers. | ||
| The mask is applied to all packages in the model, and the values in the mask determine which cells are active and which are inactive. The mask is applied to all packages, regardless of whether they have a subunit dimension or not. | ||
|
|
||
| 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. | ||
|
|
||
| Example | ||
| ------- | ||
| >>> mask = xr.DataArray( | ||
| >>> np.array( | ||
| >>> [ | ||
| >>> [[0, 0, 0], [0, 1, 1], [0, 0, 0]], | ||
| >>> [[1, 1, 1], [0, 1, 1], [0, 0, 0]], | ||
| >>> ] | ||
| >>> ).astype(bool), | ||
| >>> dims=("subunit", "y", "x"), | ||
| >>> coords = { | ||
| >>> "x" : [1.0, 2.0, 3.0], | ||
| >>> "y" : [3.0, 2.0, 1.0], | ||
| >>> "dx" : 1.0, | ||
| >>> "dy" : 1.0, | ||
| >>> "subunit" : [0, 1] | ||
| >>> } | ||
| >>> ) | ||
| """ | ||
|
|
||
| if "subunit" in mask.dims: | ||
| mask_all = mask.any(dim="subunit") | ||
| msw_active = MetaSwapActive(all=mask_all, per_subunit=mask) | ||
| else: | ||
| nsub = 1 | ||
| for pkg in self.values(): | ||
| if "subunit" in pkg.dataset.dims: | ||
| nsub = pkg.dataset.dims["subunit"] | ||
| break | ||
|
Comment on lines
+562
to
+566
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| mask_per_subunit = mask.expand_dims(dim={"subunit": nsub}) | ||
| msw_active = MetaSwapActive(all=mask, per_subunit=mask_per_subunit) | ||
|
|
||
| for pkg in self.values(): | ||
| if "x" in pkg.dataset.dims and "y" in pkg.dataset.dims: | ||
| data_dict = { | ||
| key: pkg.dataset[key] for key in pkg.dataset.data_vars.keys() | ||
| } | ||
| masked_data = mask_and_broadcast_pkg_data(pkg, data_dict, msw_active) | ||
| for key, data in masked_data.items(): | ||
| pkg.dataset[key] = data | ||
| return | ||
|
|
||
| def clip_box( | ||
| self, | ||
| time_min: Optional[cftime.datetime | np.datetime64 | str] = None, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.