-
Notifications
You must be signed in to change notification settings - Fork 4
Issue #965 npf from imod5 #1010
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
Changes from all commits
a87e366
b3f618d
7e8c250
6abad93
a7fb3c9
2d4051c
7b7aefd
a43c3ce
1425ac9
a6d0b2a
f270fd5
f3c6d99
6fdd729
d9cb6a8
03ea1a5
3cea476
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
import warnings | ||
from copy import deepcopy | ||
from typing import Optional, Tuple | ||
|
||
import numpy as np | ||
import xarray as xr | ||
|
||
from imod.logging import init_log_decorator | ||
from imod.mf6.interfaces.iregridpackage import IRegridPackage | ||
from imod.mf6.package import Package | ||
from imod.mf6.utilities.regrid import RegridderType | ||
from imod.mf6.utilities.imod5_converter import fill_missing_layers | ||
from imod.mf6.utilities.regrid import ( | ||
RegridderType, | ||
RegridderWeightsCache, | ||
_regrid_package_data, | ||
) | ||
from imod.mf6.validation import PKG_DIMS_SCHEMA | ||
from imod.schemata import ( | ||
AllValueSchema, | ||
|
@@ -17,6 +24,7 @@ | |
IndexesSchema, | ||
) | ||
from imod.typing import GridDataArray | ||
from imod.typing.grid import zeros_like | ||
|
||
|
||
def _dataarray_to_bool(griddataarray: GridDataArray) -> bool: | ||
|
@@ -461,3 +469,74 @@ def _validate(self, schemata, **kwargs): | |
|
||
def get_regrid_methods(self) -> Optional[dict[str, Tuple[RegridderType, str]]]: | ||
return self._regrid_method | ||
|
||
@classmethod | ||
def from_imod5_data( | ||
cls, | ||
imod5_data: dict[str, dict[str, GridDataArray]], | ||
target_grid: GridDataArray, | ||
regridder_types: Optional[dict[str, tuple[RegridderType, str]]] = None, | ||
) -> "NodePropertyFlow": | ||
""" | ||
Construct an npf-package from iMOD5 data, loaded with the | ||
:func:`imod.formats.prj.open_projectfile_data` function. | ||
|
||
.. note:: | ||
|
||
The method expects the iMOD5 model to be fully 3D, not quasi-3D. | ||
|
||
Parameters | ||
---------- | ||
imod5_data: dict | ||
Dictionary with iMOD5 data. This can be constructed from the | ||
:func:`imod.formats.prj.open_projectfile_data` method. | ||
target_grid: GridDataArray | ||
The grid that should be used for the new package. Does not | ||
need to be identical to one of the input grids. | ||
regridder_types: dict, optional | ||
Optional dictionary with regridder types for a specific variable. | ||
Use this to override default regridding methods. | ||
|
||
Returns | ||
------- | ||
Modflow 6 npf package. | ||
|
||
""" | ||
|
||
data = { | ||
"k": imod5_data["khv"]["kh"], | ||
} | ||
has_vertical_anisotropy = ( | ||
"kva" in imod5_data.keys() | ||
and "vertical_anisotropy" in imod5_data["kva"].keys() | ||
) | ||
has_horizontal_anisotropy = "ani" in imod5_data.keys() | ||
|
||
if has_vertical_anisotropy: | ||
data["k33"] = data["k"] * imod5_data["kva"]["vertical_anisotropy"] | ||
if has_horizontal_anisotropy: | ||
if not np.all(np.isnan(imod5_data["ani"]["factor"].values)): | ||
factor = imod5_data["ani"]["factor"] | ||
factor = fill_missing_layers(factor, target_grid, 1) | ||
data["k22"] = data["k"] * factor | ||
if not np.all(np.isnan(imod5_data["ani"]["angle"].values)): | ||
angle1 = imod5_data["ani"]["angle"] | ||
angle1 = 90.0 - angle1 | ||
angle1 = xr.where(angle1 < 0, 360.0 + angle1, angle1) | ||
angle1 = fill_missing_layers(angle1, target_grid, 0) | ||
data["angle1"] = angle1 | ||
|
||
icelltype = zeros_like(target_grid, dtype=int) | ||
|
||
regridder_settings = deepcopy(cls._regrid_method) | ||
if regridder_types is not None: | ||
regridder_settings.update(regridder_types) | ||
|
||
regrid_context = RegridderWeightsCache(data["k"], target_grid) | ||
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. Just a note: I think we should be able to optionally provide regrid contexts when we implement complete conversion of models. 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. we can just provide an optional parameter there which has a default value of an empty RegridderCache. Then it's up to the user. 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. Yeah I think that is a good resolution. You could implement immediately so we don't forget. If you decide to do so, please don't forget to update the method for the discretization package as well. |
||
|
||
new_package_data = _regrid_package_data( | ||
data, target_grid, regridder_settings, regrid_context, {} | ||
) | ||
new_package_data["icelltype"] = icelltype | ||
|
||
return NodePropertyFlow(**new_package_data) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
import xarray as xr | ||
|
||
import imod | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def imod5_dataset(): | ||
tmp_path = imod.util.temporary_directory() | ||
data = imod.data.imod5_projectfile_data(tmp_path) | ||
_load_imod5_data_in_memory(data[0]) | ||
return data[0] | ||
|
||
|
||
def _load_imod5_data_in_memory(imod5_data): | ||
"""For debugging purposes, load everything in memory""" | ||
for pkg in imod5_data.values(): | ||
for vardata in pkg.values(): | ||
if isinstance(vardata, xr.DataArray): | ||
vardata.load() |
Uh oh!
There was an error while loading. Please reload this page.