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

Issue #1001 more msw regrid packages simplified #1151

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,4 @@ examples/data
# pixi environments
.pixi

/imod/tests/unittest_report.xml
8 changes: 5 additions & 3 deletions imod/msw/grid_data.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import numpy as np
import xarray as xr

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 GridDataRegridMethod
from imod.util.spatial import spatial_reference
from imod.util.spatial import get_cell_area, spatial_reference


class GridData(MetaSwapPackage):
class GridData(MetaSwapPackage, IRegridPackage):
"""
This contains the grid data of MetaSWAP.

Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(
active: xr.DataArray,
):
super().__init__()

self.dataset["area"] = area
self.dataset["landuse"] = landuse
self.dataset["rootzone_depth"] = rootzone_depth
Expand Down Expand Up @@ -98,7 +100,7 @@ def _pkgcheck(self):

active = self.dataset["active"]

cell_area = active.astype(float) * dx * abs(dy)
cell_area = get_cell_area(active)
total_area = self.dataset["area"].sum(dim="subunit")

# Apparently all regional models intentionally provided area grids
Expand Down
3 changes: 2 additions & 1 deletion imod/msw/meteo_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import xarray as xr

import imod
from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.msw.pkgbase import MetaSwapPackage
from imod.msw.regrid.regrid_schemes import MeteoGridRegridMethod
from imod.msw.timeutil import to_metaswap_timeformat


class MeteoGrid(MetaSwapPackage):
class MeteoGrid(MetaSwapPackage, IRegridPackage):
"""
This contains the meteorological grid data. Grids are written to ESRI ASCII
files. The meteorological data requires a time coordinate. Next to a
Expand Down
2 changes: 1 addition & 1 deletion imod/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@
well_test_data_stationary,
well_test_data_transient,
)
from .fixtures.msw_fixture import fixed_format_parser
from .fixtures.msw_fixture import fixed_format_parser, simple_2d_grid_with_subunits
from .fixtures.msw_model_fixture import coupled_mf6_model, msw_model
17 changes: 17 additions & 0 deletions imod/tests/fixtures/msw_fixture.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import xarray as xr


# Seperate function from the one in fixed_format module. This one fails if not
Expand Down Expand Up @@ -27,3 +28,19 @@ def function(file, metadata_dict):
return results

return function


@pytest.fixture(scope="session")
def simple_2d_grid_with_subunits():
x = [1.0, 1.5, 2.0, 2.5, 3.0]
y = [3.0, 2.5, 2.0, 1.5, 1.0]
subunit = [0, 1]
dx = 0.5
dy = 0.5
# fmt: off
new_grid = xr.DataArray(
dims=("subunit", "y", "x"),
coords={"subunit": subunit, "y": y, "x": x, "dx": dx, "dy": dy}
)
new_grid.values[:,:,:] = 1
return new_grid
19 changes: 2 additions & 17 deletions imod/tests/test_msw/test_annual_crop_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,9 @@ def setup_cropfactors():
return cropfactors


def get_new_grid():
x = list(range(100))
y = list(range(100, 0, -1))
subunit = [0, 1]
dx = 0.5
dy = 0.5
# fmt: off
new_grid = xr.DataArray(
dims=("subunit", "y", "x"),
coords={"subunit": subunit, "y": y, "x": x, "dx": dx, "dy": dy}
)
new_grid.values[:,:,:] = 1
return new_grid


def test_cropfactor_regrid():
def test_cropfactor_regrid(simple_2d_grid_with_subunits):
crop_factors = setup_cropfactors()
new_grid = get_new_grid()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()
regridded = crop_factors.regrid_like(new_grid, regrid_context)
Expand Down
27 changes: 24 additions & 3 deletions imod/tests/test_msw/test_grid_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from numpy import nan
from numpy.testing import assert_almost_equal, assert_equal

from imod.mf6.utilities.regrid import RegridderWeightsCache
from imod.msw import GridData
from imod.msw.fixed_format import format_fixed_width
from imod.util.spatial import get_total_grid_area


@given(
Expand Down Expand Up @@ -234,9 +236,9 @@ def test_generate_index_array():
assert_equal(svat.values, svat_expected.values)


def test_simple_model(fixed_format_parser):
def simple_model():
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 @@ -324,6 +326,12 @@ def test_simple_model(fixed_format_parser):
active,
)

return grid_data


def test_simple_model(fixed_format_parser):
grid_data = simple_model()

index, svat = grid_data.generate_index_array()

with tempfile.TemporaryDirectory() as output_dir:
Expand All @@ -342,6 +350,19 @@ def test_simple_model(fixed_format_parser):
assert_almost_equal(results["rootzone_depth"], np.array([1.0, 1.0, 1.0, 1.0]))


def test_simple_model_regrid(simple_2d_grid_with_subunits):
grid_data = simple_model()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()

regridded_griddata = grid_data.regrid_like(new_grid, regrid_context)

regridded_area = regridded_griddata.dataset["area"].sum(dim="subunit")
regridded_total_area = get_total_grid_area(new_grid.sel({"subunit": 0}, drop=True))
assert np.sum(regridded_area.values) == regridded_total_area


def test_simple_model_1_subunit(fixed_format_parser):
x = [1.0, 2.0, 3.0]
y = [1.0, 2.0, 3.0]
Expand Down Expand Up @@ -443,7 +464,7 @@ def test_area_grid_exceeds_cell_area():
"""
Test where provided area grid exceeds total cell area, should throw error.
"""
x = [1.0, 2.0, 3.0]
x = [1.0, 3.0, 5.0]
y = [1.0, 2.0, 3.0]
subunit = [0, 1]
dx = 1.0
Expand Down
31 changes: 7 additions & 24 deletions imod/tests/test_msw/test_initial_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pathlib import Path

import pytest
import xarray as xr

from imod.mf6.utilities.regrid import (
RegridderWeightsCache,
Expand All @@ -16,21 +15,6 @@
from imod.typing.grid import is_empty


def get_new_grid():
x = [1.0, 1.5, 2.0, 2.5, 3.0]
y = [3.0, 2.5, 2.0, 1.5, 1.0]
subunit = [0, 1]
dx = 0.5
dy = 0.5
# fmt: off
new_grid = xr.DataArray(
dims=("subunit", "y", "x"),
coords={"subunit": subunit, "y": y, "x": x, "dx": dx, "dy": dy}
)
new_grid.values[:,:,:] = 1
return new_grid


def test_initial_conditions_equilibrium():
ic = InitialConditionsEquilibrium()
dummy = None, None
Expand All @@ -45,14 +29,13 @@ def test_initial_conditions_equilibrium():
assert lines == ["Equilibrium\n"]


def test_initial_conditions_equilibrium_regrid():
def test_initial_conditions_equilibrium_regrid(simple_2d_grid_with_subunits):
ic = InitialConditionsEquilibrium()

# fmt: off
new_grid = get_new_grid()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()
regridded = ic.regrid_like(new_grid, regrid_context )
regridded = ic.regrid_like(new_grid, regrid_context)

assert is_empty(regridded.dataset)

Expand All @@ -71,10 +54,10 @@ def test_initial_conditions_percolation():
assert lines == ["MeteoInputP\n"]


def test_initial_conditions_percolation_regrid():
def test_initial_conditions_percolation_regrid(simple_2d_grid_with_subunits):
ic = InitialConditionsPercolation()

new_grid = get_new_grid()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()
regridded = ic.regrid_like(new_grid, regrid_context)
Expand All @@ -95,10 +78,10 @@ def test_initial_conditions_rootzone_pressure_head():
assert lines == ["Rootzone_pF\n", " 2.200\n"]


def test_initial_conditions_rootzone_regrid():
def test_initial_conditions_rootzone_regrid(simple_2d_grid_with_subunits):
ic = InitialConditionsRootzonePressureHead(2.2)

new_grid = get_new_grid()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()
regridded = ic.regrid_like(new_grid, regrid_context)
Expand Down
19 changes: 2 additions & 17 deletions imod/tests/test_msw/test_landuse_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ def create_landuse_dict():
return options


def get_new_grid():
x = [1.0, 1.5, 2.0, 2.5, 3.0]
y = [3.0, 2.5, 2.0, 1.5, 1.0]
subunit = [0, 1]
dx = 0.5
dy = 0.5
# fmt: off
new_grid = xr.DataArray(
dims=("subunit", "y", "x"),
coords={"subunit": subunit, "y": y, "x": x, "dx": dx, "dy": dy}
)
new_grid.values[:,:,:] = 1
return new_grid


def test_landuse_options(fixed_format_parser):
options = create_landuse_dict()
lu_options = LanduseOptions(**options)
Expand Down Expand Up @@ -91,8 +76,8 @@ def test_landuse_options(fixed_format_parser):
assert_equal(results[option], value.values)


def test_landuse_options_regrid():
new_grid = get_new_grid()
def test_landuse_options_regrid(simple_2d_grid_with_subunits):
new_grid = simple_2d_grid_with_subunits
options = create_landuse_dict()
lu_options = LanduseOptions(**options)

Expand Down
21 changes: 20 additions & 1 deletion imod/tests/test_msw/test_meteo_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from numpy import nan
from numpy.testing import assert_equal

from imod.mf6.utilities.regrid import RegridderWeightsCache
from imod.msw import MeteoGrid


def test_meteo_grid():
def setup_meteo_grid():
x = [1.0, 2.0, 3.0]
y = [1.0, 2.0, 3.0]
time = pd.date_range(start="2000-01-01", end="2000-01-02", freq="D")
Expand Down Expand Up @@ -48,6 +49,12 @@ def test_meteo_grid():

meteo_grid = MeteoGrid(precipitation, evapotranspiration)

return meteo_grid


def test_meteo_grid():
meteo_grid = setup_meteo_grid()

with tempfile.TemporaryDirectory() as output_dir:
output_dir = Path(output_dir)
meteo_grid.write(output_dir)
Expand Down Expand Up @@ -104,3 +111,15 @@ def test_meteo_no_time_grid():

with pytest.raises(ValueError):
MeteoGrid(precipitation, evapotranspiration)


def test_regrid_meteo(simple_2d_grid_with_subunits):
meteo = setup_meteo_grid()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()

regridded_ponding = meteo.regrid_like(new_grid, regrid_context)

assert np.all(regridded_ponding.dataset["x"].values == new_grid["x"].values)
assert np.all(regridded_ponding.dataset["y"].values == new_grid["y"].values)
19 changes: 2 additions & 17 deletions imod/tests/test_msw/test_ponding.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ def setup_ponding():
return ponding, index, svat


def get_new_grid():
x = [1.0, 1.5, 2.0, 2.5, 3.0]
y = [3.0, 2.5, 2.0, 1.5, 1.0]
subunit = [0, 1]
dx = 0.5
dy = 0.5
# fmt: off
new_grid = xr.DataArray(
dims=("subunit", "y", "x"),
coords={"subunit": subunit, "y": y, "x": x, "dx": dx, "dy": dy}
)
new_grid.values[:,:,:] = 1
return new_grid


def test_simple_model(fixed_format_parser):
ponding, index, svat = setup_ponding()
with tempfile.TemporaryDirectory() as output_dir:
Expand All @@ -109,9 +94,9 @@ def test_simple_model(fixed_format_parser):
assert_almost_equal(results["runoff_resistance"], np.array([0.5, 1.0, 0.5, 1.0]))


def test_regrid_ponding():
def test_regrid_ponding(simple_2d_grid_with_subunits):
ponding, _, _ = setup_ponding()
new_grid = get_new_grid()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()

Expand Down
21 changes: 3 additions & 18 deletions imod/tests/test_msw/test_scaling_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,6 @@ def setup_scaling_factor():
return scaling_factors, index, svat


def get_new_grid():
x = [1.0, 1.5, 2.0, 2.5, 3.0]
y = [3.0, 2.5, 2.0, 1.5, 1.0]
subunit = [0, 1]
dx = 0.5
dy = 0.5
# fmt: off
new_grid = xr.DataArray(
dims=("subunit", "y", "x"),
coords={"subunit": subunit, "y": y, "x": x, "dx": dx, "dy": dy}
)
new_grid.values[:,:,:] = 1
return new_grid


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

Expand All @@ -110,9 +95,9 @@ def test_simple_model(fixed_format_parser):
)


def test_regrid_scaling_factor(fixed_format_parser):
scaling_factors, index, svat = setup_scaling_factor()
new_grid = get_new_grid()
def test_regrid_scaling_factor(fixed_format_parser, simple_2d_grid_with_subunits):
scaling_factors, _, _ = setup_scaling_factor()
new_grid = simple_2d_grid_with_subunits

regrid_context = RegridderWeightsCache()

Expand Down
Loading