|
10 | 10 |
|
11 | 11 | import imod
|
12 | 12 | from imod.mf6.dis import StructuredDiscretization
|
| 13 | +from imod.mf6.utilities.regrid import RegridderWeightsCache |
13 | 14 | from imod.mf6.write_context import WriteContext
|
14 | 15 | from imod.schemata import ValidationError
|
15 | 16 | from imod.typing.grid import is_planar_grid, is_transient_data_grid, nan_like
|
@@ -467,18 +468,72 @@ def test_from_imod5_cap_data(imod5_dataset):
|
467 | 468 | data["cap"]["boundary"] = msw_bound
|
468 | 469 | data["cap"]["wetted_area"] = xr.ones_like(msw_bound) * 100
|
469 | 470 | data["cap"]["urban_area"] = xr.ones_like(msw_bound) * 200
|
| 471 | + # Compute midpoint of grid and set areas such, that cells need to be |
| 472 | + # deactivated. |
| 473 | + midpoint = tuple((int(x / 2) for x in msw_bound.shape)) |
470 | 474 | # Set to total cellsize, cell needs to be deactivated.
|
471 |
| - data["cap"]["wetted_area"][100, 100] = 625.0 |
| 475 | + data["cap"]["wetted_area"][midpoint] = 625.0 |
472 | 476 | # Set to zero, cell needs to be deactivated.
|
473 |
| - data["cap"]["urban_area"][100, 100] = 0.0 |
| 477 | + data["cap"]["urban_area"][midpoint] = 0.0 |
474 | 478 | # Act
|
475 | 479 | rch = imod.mf6.Recharge.from_imod5_cap_data(data, target_discretization)
|
476 | 480 | rate = rch.dataset["rate"]
|
477 | 481 | # Assert
|
| 482 | + # Shape |
| 483 | + assert rate.dims == ("y", "x") |
| 484 | + assert "layer" in rate.coords |
| 485 | + assert rate.coords["layer"] == 1 |
| 486 | + # Values |
478 | 487 | np.testing.assert_array_equal(np.unique(rate), np.array([0.0, np.nan]))
|
479 | 488 | # Boundaries inactive in MetaSWAP
|
480 |
| - assert np.isnan(rate[0, :, 0]).all() |
481 |
| - assert np.isnan(rate[0, :, -1]).all() |
482 |
| - assert np.isnan(rate[0, 0, :]).all() |
483 |
| - assert np.isnan(rate[0, -1, :]).all() |
484 |
| - assert np.isnan(rate[:, 100, 100]).all() |
| 489 | + assert np.isnan(rate[:, 0]).all() |
| 490 | + assert np.isnan(rate[:, -1]).all() |
| 491 | + assert np.isnan(rate[0, :]).all() |
| 492 | + assert np.isnan(rate[-1, :]).all() |
| 493 | + assert np.isnan(rate[midpoint]).all() |
| 494 | + |
| 495 | + |
| 496 | +@pytest.mark.unittest_jit |
| 497 | +def test_from_imod5_cap_data__regrid(imod5_dataset): |
| 498 | + # Arrange |
| 499 | + data = deepcopy(imod5_dataset[0]) |
| 500 | + target_discretization = StructuredDiscretization.from_imod5_data(data) |
| 501 | + data["cap"] = {} |
| 502 | + msw_bound = data["bnd"]["ibound"].isel(layer=0, drop=False) |
| 503 | + data["cap"]["boundary"] = msw_bound |
| 504 | + data["cap"]["wetted_area"] = xr.ones_like(msw_bound) * 100 |
| 505 | + data["cap"]["urban_area"] = xr.ones_like(msw_bound) * 200 |
| 506 | + # Setup template grid |
| 507 | + dx_small, xmin, xmax, dy_small, ymin, ymax = imod.util.spatial_reference(msw_bound) |
| 508 | + dx = dx_small * 2 |
| 509 | + dy = dy_small * 2 |
| 510 | + expected_spatial_ref = dx, xmin, xmax, dy, ymin, ymax |
| 511 | + like = imod.util.empty_2d(*expected_spatial_ref) |
| 512 | + # Act |
| 513 | + rch = imod.mf6.Recharge.from_imod5_cap_data(data, target_discretization) |
| 514 | + rch_coarse = rch.regrid_like(like, regrid_cache=RegridderWeightsCache()) |
| 515 | + # Assert |
| 516 | + actual_spatial_ref = imod.util.spatial_reference(rch_coarse.dataset["rate"]) |
| 517 | + assert actual_spatial_ref == expected_spatial_ref |
| 518 | + |
| 519 | + |
| 520 | +@pytest.mark.unittest_jit |
| 521 | +def test_from_imod5_cap_data__clip_box(imod5_dataset): |
| 522 | + # Arrange |
| 523 | + data = deepcopy(imod5_dataset[0]) |
| 524 | + target_discretization = StructuredDiscretization.from_imod5_data(data) |
| 525 | + data["cap"] = {} |
| 526 | + msw_bound = data["bnd"]["ibound"].isel(layer=0, drop=False) |
| 527 | + data["cap"]["boundary"] = msw_bound |
| 528 | + data["cap"]["wetted_area"] = xr.ones_like(msw_bound) * 100 |
| 529 | + data["cap"]["urban_area"] = xr.ones_like(msw_bound) * 200 |
| 530 | + # Setup template grid |
| 531 | + dx, xmin, xmax, dy, ymin, ymax = imod.util.spatial_reference(msw_bound) |
| 532 | + xmin_to_clip = xmin + 10 * dx |
| 533 | + expected_spatial_ref = dx, xmin_to_clip, xmax, dy, ymin, ymax |
| 534 | + # Act |
| 535 | + rch = imod.mf6.Recharge.from_imod5_cap_data(data, target_discretization) |
| 536 | + rch_clipped = rch.clip_box(x_min=xmin_to_clip) |
| 537 | + # Assert |
| 538 | + actual_spatial_ref = imod.util.spatial_reference(rch_clipped.dataset["rate"]) |
| 539 | + assert actual_spatial_ref == expected_spatial_ref |
0 commit comments