From 226fa3aed0c1228017182321bc93023351948d0b Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Wed, 4 Dec 2024 11:01:06 +0100 Subject: [PATCH] Add from_imod5_data method and test --- imod/msw/scaling_factors.py | 26 +++++++++ imod/tests/test_msw/test_scaling_factors.py | 60 +++++++++++++++++---- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/imod/msw/scaling_factors.py b/imod/msw/scaling_factors.py index 6e544d1c5..87dccaf61 100644 --- a/imod/msw/scaling_factors.py +++ b/imod/msw/scaling_factors.py @@ -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): @@ -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) diff --git a/imod/tests/test_msw/test_scaling_factors.py b/imod/tests/test_msw/test_scaling_factors.py index d04f071de..9b9f8c36f 100644 --- a/imod/tests/test_msw/test_scaling_factors.py +++ b/imod/tests/test_msw/test_scaling_factors.py @@ -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 @@ -63,6 +64,12 @@ 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, @@ -70,12 +77,6 @@ def setup_scaling_factor(): 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) @@ -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() @@ -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]) + )