-
Notifications
You must be signed in to change notification settings - Fork 9
Issue #963 dump meta swap model nc #1852
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
10
commits into
master
Choose a base branch
from
Issue_#963_dump_MetaSWAP_model_nc
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
10 commits
Select commit
Hold shift + click to select a range
b894f7a
the dump model method of ModflowModel now moved to a common function …
rleander d6eb9a1
try out imodel interface
rleander 54d1301
roundtrip test dumping and loading msw packages added
rleander 32d94ad
Merging master and resolving merge conflicts
rleander 0bf6290
fixing lint
rleander 817b309
restored mf6 model.py, accidentally changed
rleander aaa56ba
Extending the checks on the restored metaswap model
rleander 5fb4dac
update api ref
rleander b5d33a1
added tests for other formats and example line to docstring
rleander 4801485
processing review comments
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
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import collections | ||
| from pathlib import Path | ||
| from typing import Any, Optional | ||
|
|
||
| import tomli_w | ||
|
|
||
| from imod.common.interfaces.imodel import IModel | ||
| from imod.common.serializer import EngineType | ||
| from imod.logging.logging_decorators import standard_log_decorator | ||
| from imod.mf6.validation_settings import ValidationSettings | ||
| from imod.schemata import ValidationError | ||
|
|
||
|
|
||
| @standard_log_decorator() | ||
| def dump_model( | ||
| model: IModel, | ||
| directory, | ||
| modelname, | ||
| validate: bool = True, | ||
| mdal_compliant: bool = False, | ||
| crs: Optional[Any] = None, | ||
| engine: EngineType = "netcdf4", | ||
| ) -> Path: | ||
| """ | ||
| Dump model to files. Writes a model definition as .TOML file, which | ||
| points to data for each package. Each package is stored as a separate | ||
| NetCDF. Structured grids are saved as regular NetCDFs, unstructured | ||
| grids are saved as UGRID NetCDF. Structured grids are always made GDAL | ||
| compliant, unstructured grids can be made MDAL compliant optionally. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| directory: str or Path | ||
| directory to dump simulation into. | ||
| modelname: str | ||
| modelname, will be used to create a subdirectory. | ||
| validate: bool, optional | ||
| Whether to validate simulation data. Defaults to True. | ||
| mdal_compliant: bool, optional | ||
| Convert data with | ||
| :func:`imod.prepare.spatial.mdal_compliant_ugrid2d` to MDAL | ||
| compliant unstructured grids. Defaults to False. | ||
| crs: Any, optional | ||
| Anything accepted by rasterio.crs.CRS.from_user_input | ||
| Requires ``rioxarray`` installed. | ||
| engine : str, optional | ||
| File engine used to write packages. Options are ``'netcdf4'``, | ||
| ``'zarr'``, and ``'zarr.zip'``. NetCDF4 is readable by many other | ||
| softwares, for example QGIS. Zarr is optimized for big data, cloud | ||
| storage and parallel access. The ``'zarr.zip'`` option is an | ||
| experimental option which creates a zipped zarr store in a single | ||
| file, which is easier to copy and automatically compresses data as | ||
| well. Default is ``'netcdf4'``. | ||
|
|
||
| """ | ||
| modeldirectory = Path(directory) / modelname | ||
| modeldirectory.mkdir(exist_ok=True, parents=True) | ||
|
|
||
| # validation currently only supports MF6, but we want to keep the option to turn it on for other | ||
| validation_context = ValidationSettings(validate=validate) | ||
| if validation_context.validate: | ||
| statusinfo = model.validate(modelname, validation_context) | ||
| if statusinfo.has_errors(): | ||
| raise ValidationError(statusinfo.to_string()) | ||
|
|
||
| toml_content: dict = collections.defaultdict(dict) | ||
|
|
||
| for pkgname, pkg in model.items(): | ||
| pkg_path = pkg.to_file( | ||
| modeldirectory, | ||
| pkgname, | ||
| mdal_compliant=mdal_compliant, | ||
| crs=crs, | ||
| engine=engine, | ||
| ) | ||
| toml_content[type(pkg).__name__][pkgname] = pkg_path.name | ||
|
|
||
| # simulation settings are only relevant/present for MetaSwap models (msw) | ||
| if hasattr(model, "simulation_settings"): | ||
| toml_content["simulation_settings"] = model.simulation_settings | ||
|
|
||
| toml_path = modeldirectory / f"{modelname}.toml" | ||
| with open(toml_path, "wb") as f: | ||
| tomli_w.dump(toml_content, f) | ||
|
|
||
| return toml_path | ||
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
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.
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.
I think this only necessary for MetaSWAP models, right? If so, please add a comment that this is intended for MSW.
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.
Only applies to MetaSWAP models, I would rather do isinstance and ask if model is a MetaSWAP model, but that introduces a cyclic problem, therefore this indirect approach. I added a comment stating that this line is only relevant for MetaSWAP models.