From f14f6ead1a77394aff39107c137ea2ff1905f2fa Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Fri, 13 Dec 2024 13:16:33 +0100 Subject: [PATCH 1/4] Add tutorial 3 data --- imod/data/__init__.py | 1 + imod/data/registry.txt | 1 + imod/data/sample_data.py | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/imod/data/__init__.py b/imod/data/__init__.py index 6221d6857..c6b0ca1b7 100644 --- a/imod/data/__init__.py +++ b/imod/data/__init__.py @@ -16,5 +16,6 @@ hondsrug_simulation, imod5_projectfile_data, lakes_shp, + tutorial_03, twri_output, ) diff --git a/imod/data/registry.txt b/imod/data/registry.txt index 6efa33248..a5f7b19ed 100644 --- a/imod/data/registry.txt +++ b/imod/data/registry.txt @@ -13,3 +13,4 @@ lakes_shp.zip 564c20d722639f1ca19bb78e32bfe575e1e0779f3c922a1aa1a386c2a43db10d circle-nodes.txt 1ff03b8721c6e7bb6289251373d66d5b8ee2388c03493dfc75c1bf1fe7e586d2 circle-triangles.txt 546d5e6ba6f9d16bb5330313edf36d4a9fc4821c9a89eedfa76ac2266dd2a5fa iMOD5_model.zip e5808579bd4f0663f0b04f8333b3e2edde785a92c83156837d29bbaed595940f +iMOD-Documentation-tutorial_03.zip e4184f08c53d263939e1ead3bb66a9701e0a326ffaeda9b34a54999912a118cb \ No newline at end of file diff --git a/imod/data/sample_data.py b/imod/data/sample_data.py index ab9d36b8b..3568c438d 100644 --- a/imod/data/sample_data.py +++ b/imod/data/sample_data.py @@ -275,3 +275,16 @@ def colleagues_river_data(path: Union[str, Path]): x_preserve | y_preserve, riv_bot_da + 0.15 ) return riv_ds + + +def tutorial_03(path: Path | str) -> None: + """ + Starting dataset for tutorial 3 in the iMOD Documentation. + """ + path = Path(path) + filename = "iMOD-Documentation-tutorial_03.zip" + lock = FileLock(REGISTRY.path / f"{filename}.lock") + with lock: + _ = REGISTRY.fetch(filename, processor=Unzip(extract_dir=path)) + + return path / "tutorial_03" / "GWF_model_Hondsrug.prj" From ffb3968473ea3ba58355b7f2419f9805c0ef9060 Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Fri, 13 Dec 2024 13:17:05 +0100 Subject: [PATCH 2/4] Add unittests for tutorial 03 --- imod/tests/test_data.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/imod/tests/test_data.py b/imod/tests/test_data.py index 19d07ce6c..4b7ea8ec4 100644 --- a/imod/tests/test_data.py +++ b/imod/tests/test_data.py @@ -1,4 +1,5 @@ import os +from pathlib import Path import xarray as xr @@ -26,3 +27,46 @@ def test_hondsrug_data(): imod.data.hondsrug_drainage, ]: assert isinstance(f(), xr.Dataset) + + +def test_tutorial_03_data__unzipped(tmp_path): + """ + Test if tutorial 03 material can be unzipped. + """ + # Act + prj_path = imod.data.tutorial_03(tmp_path) + + # Assert + assert isinstance(prj_path, Path) + assert prj_path.suffix == ".prj" + n_files = sum(1 for x in prj_path.parent.rglob("*") if x.is_file()) + assert n_files == 107 + + +def test_tutorial_03_data__open_data(tmp_path): + """ + Test if tutorial 03 material can be opened. + """ + # Act + prj_path = imod.data.tutorial_03(tmp_path) + imod5_data, _ = imod.formats.prj.open_projectfile_data(prj_path) + + # Assert + expected_keys = { + "bnd", + "top", + "bot", + "khv", + "kva", + "sto", + "shd", + "rch", + "riv", + "drn-1", + "drn-2", + "drn-3", + "drn-4", + "pcg", + } + missing_keys = expected_keys - set(imod5_data.keys()) + assert len(missing_keys) == 0 From 56848d0158d5b62a7fefa8828cad87bdc5149e09 Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Fri, 13 Dec 2024 13:21:23 +0100 Subject: [PATCH 3/4] Update changelog --- docs/api/changelog.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/api/changelog.rst b/docs/api/changelog.rst index 32c686b0c..0fe651f9d 100644 --- a/docs/api/changelog.rst +++ b/docs/api/changelog.rst @@ -9,6 +9,12 @@ The format is based on `Keep a Changelog`_, and this project adheres to [Unreleased] ------------ +Added +~~~~~ + +- :func:`imod.data.tutorial_03` to load data for the iMOD Documentation + tutorial. + Fixed ~~~~~ From 0049bd3a9ec2bb6d1cb024149e62db18310d0c86 Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Fri, 13 Dec 2024 14:53:07 +0100 Subject: [PATCH 4/4] Add comments and avoid race condition in hondsrug_layermodel --- imod/data/sample_data.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/imod/data/sample_data.py b/imod/data/sample_data.py index 3568c438d..36738ea41 100644 --- a/imod/data/sample_data.py +++ b/imod/data/sample_data.py @@ -46,6 +46,8 @@ def load_pooch_registry(registry: pooch.core.Pooch) -> pooch.core.Pooch: def twri_output(path: Union[str, Path]) -> None: + # Unzips TWRI output to ``path``. Has a race condition when executed + # multiple times with the same path. lock = FileLock(REGISTRY.path / "ex01-twri-output.zip.lock") with lock: _ = REGISTRY.fetch("ex01-twri-output.zip", processor=Unzip(extract_dir=path)) @@ -63,8 +65,9 @@ def hondsrug_layermodel() -> xr.Dataset: lock = FileLock(REGISTRY.path / "hondsrug-layermodel.nc.lock") with lock: fname = REGISTRY.fetch("hondsrug-layermodel.nc") + hondsrug_layermodel = xr.open_dataset(fname) - return xr.open_dataset(fname) + return hondsrug_layermodel def hondsrug_meteorology() -> xr.Dataset: @@ -281,6 +284,8 @@ def tutorial_03(path: Path | str) -> None: """ Starting dataset for tutorial 3 in the iMOD Documentation. """ + # Unzips tutorial content to ``path``. Has a race condition when executed + # multiple times with the same path. path = Path(path) filename = "iMOD-Documentation-tutorial_03.zip" lock = FileLock(REGISTRY.path / f"{filename}.lock")