Skip to content

Commit f81d034

Browse files
committed
Bug fix writing clipped idf_svat.
1 parent d652758 commit f81d034

5 files changed

Lines changed: 58 additions & 24 deletions

File tree

docs/api/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Fixed
4545
produced incorrect results when point water heads were below elevation levels
4646
for unstructured grids.
4747
- Support pandas 3.0.
48+
- :class:`imod.msw.IdfMapping` when model clipping is applied, the global
49+
row/column indices are converted to local indices, as writen in idf_svat.inp.
4850

4951

5052
Changed

imod/msw/idf_mapping.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import Optional
33

44
import numpy as np
5-
import xarray as xr
65

76
from imod.common.interfaces.iregridpackage import IRegridPackage
87
from imod.common.utilities.dataclass_type import DataclassType
@@ -47,25 +46,6 @@ def __init__(self, area, nodata):
4746
self.dataset["area"] = area
4847
self.dataset["nodata"] = nodata
4948

50-
nrow = self.dataset.coords["y"].size
51-
ncol = self.dataset.coords["x"].size
52-
53-
y_index = xr.DataArray(
54-
np.arange(1, nrow + 1), coords={"y": self.dataset.coords["y"]}, dims=("y",)
55-
)
56-
x_index = xr.DataArray(
57-
np.arange(1, ncol + 1), coords={"x": self.dataset.coords["x"]}, dims=("x",)
58-
)
59-
rows, columns = xr.broadcast(y_index, x_index)
60-
61-
self.dataset["rows"] = rows
62-
self.dataset["columns"] = columns
63-
64-
y_grid, x_grid = xr.broadcast(self.dataset["y"], self.dataset["x"])
65-
66-
self.dataset["x_grid"] = x_grid
67-
self.dataset["y_grid"] = y_grid
68-
6949
def _get_output_settings(self):
7050
grid = self.dataset["area"]
7151
dx, xmin, _, dy, ymin, _ = spatial_reference(grid)

imod/msw/pkgbase.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ def _render(
139139
Collect to be written data in a DataFrame and call
140140
``self.write_dataframe_fixed_width``
141141
"""
142+
if self.__class__.__name__ == "IdfMapping":
143+
nrow = self.dataset.coords["y"].size
144+
ncol = self.dataset.coords["x"].size
145+
146+
y_index = xr.DataArray(
147+
np.arange(1, nrow + 1),
148+
coords={"y": self.dataset.coords["y"]},
149+
dims=("y",),
150+
)
151+
x_index = xr.DataArray(
152+
np.arange(1, ncol + 1),
153+
coords={"x": self.dataset.coords["x"]},
154+
dims=("x",),
155+
)
156+
rows, columns = xr.broadcast(y_index, x_index)
157+
158+
self.dataset["rows"] = rows
159+
self.dataset["columns"] = columns
160+
161+
y_grid, x_grid = xr.broadcast(self.dataset["y"], self.dataset["x"])
162+
163+
self.dataset["x_grid"] = x_grid
164+
self.dataset["y_grid"] = y_grid
165+
142166
data_dict: DataDictType = {"svat": svat.values.ravel()[index]}
143167

144168
subunit = svat.coords["subunit"]

imod/tests/test_msw/test_idf_mapping.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test_idf_mapping_regrid():
1616

1717
idf_mapping_regridded = idf_mapping.regrid_like(new_grid, regrid_context)
1818

19-
assert len(idf_mapping_regridded.dataset["rows"]) == 5
20-
assert len(idf_mapping_regridded.dataset["columns"]) == 5
19+
assert len(idf_mapping_regridded.dataset["y"]) == 5
20+
assert len(idf_mapping_regridded.dataset["x"]) == 5
2121
assert np.isnan(idf_mapping_regridded.dataset["nodata"].values[()])
2222

2323

@@ -27,8 +27,8 @@ def test_idf_mapping_clip():
2727
idf_mapping = IdfMapping(area, np.nan)
2828

2929
idf_mapping_selected = idf_mapping.clip_box(
30-
x_min=1.0, x_max=2.5, y_min=1.0, y_max=2.5
30+
x_min=2.0, x_max=3.5, y_min=1.0, y_max=2.5
3131
)
3232

33-
expected_area = area.sel(x=slice(1.0, 2.5), y=slice(2.5, 1.0))
33+
expected_area = area.sel(x=slice(2.0, 3.5), y=slice(2.5, 1.0))
3434
xr.testing.assert_allclose(idf_mapping_selected.dataset["area"], expected_area)

imod/tests/test_msw/test_output_control.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,34 @@ def test_idf_oc_write_simple_model(fixed_format_parser):
114114
assert_almost_equal(results["x_grid"], np.array([2.0, 2.0, 2.0, 2.0, 4.0]))
115115

116116

117+
def test_idf_oc_write_simple_model_clip(fixed_format_parser):
118+
area, index, svat = grid()
119+
nodata = -9999.0
120+
x_min, x_max, y_min, y_max = 1.5, 4.5, 1.5, 3.5
121+
122+
idf_output_control = IdfMapping(area, nodata)
123+
idf_output_control_clip = idf_output_control.clip_box(
124+
x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max
125+
)
126+
127+
svat_clip = svat.sel(x=slice(x_min, x_max), y=slice(y_min, y_max))
128+
index_clip = (svat_clip != 0).values.ravel()
129+
130+
with tempfile.TemporaryDirectory() as output_dir:
131+
output_dir = Path(output_dir)
132+
idf_output_control_clip.write(output_dir, index_clip, svat_clip, None, None)
133+
134+
results = fixed_format_parser(
135+
output_dir / IdfMapping._file_name, IdfMapping._metadata_dict
136+
)
137+
138+
assert_equal(results["svat"], np.array([2, 4, 5]))
139+
assert_equal(results["rows"], np.array([2, 1, 1]))
140+
assert_equal(results["columns"], np.array([1, 1, 3]))
141+
assert_almost_equal(results["y_grid"], np.array([3.0, 2.0, 2.0]))
142+
assert_almost_equal(results["x_grid"], np.array([2.0, 2.0, 4.0]))
143+
144+
117145
def test_idf_oc_mapping_order(fixed_format_parser):
118146
area, index, svat = grid()
119147
nodata = -9999.0

0 commit comments

Comments
 (0)