Skip to content
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

Add from_imod5_data method and test #1325

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
26 changes: 26 additions & 0 deletions imod/msw/scaling_factors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import cast

from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.msw.fixed_format import VariableMetaData
from imod.msw.pkgbase import MetaSwapPackage
from imod.msw.regrid.regrid_schemes import ScalingRegridMethod
from imod.msw.utilities.common import concat_imod5
from imod.typing import GridDataDict, Imod5DataDict
from imod.typing.grid import ones_like


class ScalingFactors(MetaSwapPackage, IRegridPackage):
Expand Down Expand Up @@ -76,3 +81,24 @@ def __init__(
self.dataset["depth_perched_water_table"] = depth_perched_water_table

self._pkgcheck()

@classmethod
def from_imod5_data(cls, imod5_data: Imod5DataDict) -> "ScalingFactors":
"""
Import ScalingFactors from iMOD5 data. Pressure head factor is set to
one for all factors, as well as all factors for urban areas.
"""
cap_data = cast(GridDataDict, imod5_data["cap"])
grid_ones = ones_like(cap_data["boundary"])

data = {}
data["scale_soil_moisture"] = concat_imod5(
cap_data["soil_moisture_fraction"], grid_ones
)
data["scale_hydraulic_conductivity"] = concat_imod5(
cap_data["conductivitiy_factor"], grid_ones
)
data["scale_pressure_head"] = concat_imod5(grid_ones, grid_ones)
data["depth_perched_water_table"] = cap_data["perched_water_table_level"]

return cls(**data)
60 changes: 50 additions & 10 deletions imod/tests/test_msw/test_scaling_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
RegridderWeightsCache,
)
from imod.msw import ScalingFactors
from imod.typing.grid import ones_like


def setup_scaling_factor():
def setup_scaling_factor_grids():
x = [1.0, 2.0, 3.0]
y = [1.0, 2.0, 3.0]
y = [3.0, 2.0, 1.0]
subunit = [0, 1]
dx = 1.0
dy = 1.0
Expand Down Expand Up @@ -63,19 +64,19 @@ def setup_scaling_factor():
# fmt: on
index = (svat != 0).values.ravel()

return scale, depth_perched_water_table, index, svat


def test_simple_model(fixed_format_parser):
scale, depth_perched_water_table, index, svat = setup_scaling_factor_grids()

scaling_factors = ScalingFactors(
scale_soil_moisture=scale,
scale_hydraulic_conductivity=scale,
scale_pressure_head=scale,
depth_perched_water_table=depth_perched_water_table,
)

return scaling_factors, index, svat


def test_simple_model(fixed_format_parser):
scaling_factors, index, svat = setup_scaling_factor()

with tempfile.TemporaryDirectory() as output_dir:
output_dir = Path(output_dir)
scaling_factors.write(output_dir, index, svat, None, None)
Expand All @@ -95,8 +96,16 @@ def test_simple_model(fixed_format_parser):
)


def test_regrid_scaling_factor(fixed_format_parser, simple_2d_grid_with_subunits):
scaling_factors, _, _ = setup_scaling_factor()
def test_regrid_scaling_factor(simple_2d_grid_with_subunits):
scale, depth_perched_water_table, _, _ = setup_scaling_factor_grids()

scaling_factors = ScalingFactors(
scale_soil_moisture=scale,
scale_hydraulic_conductivity=scale,
scale_pressure_head=scale,
depth_perched_water_table=depth_perched_water_table,
)

new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()
Expand All @@ -105,3 +114,34 @@ def test_regrid_scaling_factor(fixed_format_parser, simple_2d_grid_with_subunits

assert np.all(regridded_scaling_factor.dataset["x"].values == new_grid["x"].values)
assert np.all(regridded_scaling_factor.dataset["y"].values == new_grid["y"].values)


def test_from_imod5_data(fixed_format_parser):
scale, depth_perched_water_table, index, svat = setup_scaling_factor_grids()

imod5_data = {"cap": {}}
scale_rural = scale.sel(subunit=0, drop=True)
imod5_data["cap"]["boundary"] = ones_like(scale_rural)
imod5_data["cap"]["soil_moisture_fraction"] = scale_rural
imod5_data["cap"]["conductivitiy_factor"] = scale_rural
imod5_data["cap"]["perched_water_table_level"] = depth_perched_water_table

scaling_factors = ScalingFactors.from_imod5_data(imod5_data)

with tempfile.TemporaryDirectory() as output_dir:
output_dir = Path(output_dir)
scaling_factors.write(output_dir, index, svat, None, None)

results = fixed_format_parser(
output_dir / ScalingFactors._file_name, ScalingFactors._metadata_dict
)

assert_equal(results["svat"], np.array([1, 2, 3, 4]))
assert_almost_equal(results["scale_soil_moisture"], np.array([0.5, 1.0, 1.0, 1.0]))
assert_almost_equal(
results["scale_hydraulic_conductivity"], np.array([0.5, 1.0, 1.0, 1.0])
)
assert_almost_equal(results["scale_pressure_head"], np.array([1.0, 1.0, 1.0, 1.0]))
assert_almost_equal(
results["depth_perched_water_table"], np.array([0.5, 1.0, 0.5, 0.7])
)