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 #1320 Add CopyFiles package #1321

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions docs/api/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Added

- :class:`imod.msw.MeteoGridCopy` to copy existing `mete_grid.inp` files, so
ASCII grids in large existing meteo databases do not have to be read.
- :class:`imod.msw.CopyFiles` to copy settings and lookup tables in existing
``.inp`` files.
- :meth:`imod.mf6.LayeredWell.from_imod5_cap_data` to construct a
:class:`imod.mf6.LayeredWell` package from iMOD5 data in the CAP package (for
MetaSWAP). Currently only griddata (IDF) is supported.
Expand Down
1 change: 1 addition & 0 deletions imod/msw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from imod.msw.copy_files import CopyFiles
from imod.msw.coupler_mapping import CouplerMapping
from imod.msw.grid_data import GridData
from imod.msw.idf_mapping import IdfMapping
Expand Down
55 changes: 55 additions & 0 deletions imod/msw/copy_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pathlib import Path
from shutil import copy2
from typing import cast

import numpy as np
import xarray as xr

from imod.logging import logger
from imod.logging.loglevel import LogLevel
from imod.msw.pkgbase import MetaSwapPackage
from imod.typing import Imod5DataDict

_LOG_MESSAGE_TEMPLATE = """\
Will not copy files {filtered}, these will be generated by iMOD Python
instead."""


class CopyFiles(MetaSwapPackage):
JoerivanEngelen marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this class inherit from the MetaSwapPackage?
It seems like a really general class. I don't know how it is being used but is it possible to use composition over inheritance?

If it is possible to have this class without MetaSwapPackage as its parent class then you also don't need xarray as the data container and you have a member variable that is a list of paths

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, my main reason why I decided to inherit MetaSwapPackage is that it makes the package object function consistently to other packages. For instance, we have plans to create a MetaSwapModel.dump method, which would dump all packages to file. To have a functional MetaSWAP model, we also need to store the paths to files that need to be copied somewhere. So by wrenching this list of strings/paths in a xarray Dataset, I'm quite sure no special-cased logic is required for this specific filecopy package and keeps things stored consistently in NetCDFs, instead of some custom textfile.

def __init__(self, paths: list[str]):
super().__init__()
paths_da = xr.DataArray(
paths, coords={"file_nr": np.arange(len(paths))}, dims=("file_nr",)
)
self.dataset["paths"] = paths_da

@classmethod
def from_imod5_data(cls, imod5_data: Imod5DataDict):
paths = cast(list[list[str]], imod5_data["extra"]["paths"])
paths_unpacked = {Path(p[0]) for p in paths}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using curly brackets and not block brackets? I thought curly brackets are only for dictionaries, but this seems to be a list.
The same thing occurs on line 39

Copy link
Contributor Author

@JoerivanEngelen JoerivanEngelen Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Python builtin type set. Sets are very useful to check values in iterables for uniqueness. And if one set contains values not present, or present, in another set. For docs, see: https://docs.python.org/3/library/stdtypes.html#set

I wanted to make this more explicit by doing set([Path(p[0]) for p in paths]), but Ruff disagreed with me. I guess the Ruff devs think the set is common knowledge as it is a Python builtin (though a bit more niche).

files_to_filter = (
"mete_grid.inp",
"para_sim.inp",
"svat2precgrid.inp",
"svat2etrefgrid.inp",
)
paths_filtered = [
str(p) for p in paths_unpacked if p.name.lower() not in files_to_filter
]
paths_filtered_away = {str(p) for p in paths_unpacked} - set(paths_filtered)
JoerivanEngelen marked this conversation as resolved.
Show resolved Hide resolved
if paths_filtered_away:
log_message = _LOG_MESSAGE_TEMPLATE.format(filtered=paths_filtered_away)
logger.log(
loglevel=LogLevel.INFO,
message=log_message,
)
return cls(paths_filtered)

def write(self, directory: str | Path, *_):
directory = Path(directory)

src_paths = [Path(p) for p in self.dataset["paths"].to_numpy()]
dst_paths = [directory / p.name for p in src_paths]

for src_path, dst_path in zip(src_paths, dst_paths):
copy2(src_path, dst_path)
69 changes: 69 additions & 0 deletions imod/tests/test_msw/test_copy_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from pytest_cases import parametrize_with_cases

from imod.msw.copy_files import CopyFiles


def setup_src_files(directory, filenames):
JoerivanEngelen marked this conversation as resolved.
Show resolved Hide resolved
paths = [directory / filename for filename in filenames]
for p in paths:
with open(p, mode="w") as f:
f.write("test")
return paths


def case_simple_files(tmp_path_factory):
directory = tmp_path_factory.mktemp("simple_files")
filenames = [
"a.inp",
"b.inp",
"c.inp",
]
return setup_src_files(directory, filenames)


def case_imod5_extra_files(tmp_path_factory):
directory = tmp_path_factory.mktemp("imod5_extra_files")
filenames = [
"a.inp",
"b.inp",
"c.inp",
"mete_grid.inp",
"para_sim.inp",
"svat2precgrid.inp",
"svat2etrefgrid.inp",
]
return setup_src_files(directory, filenames)


@parametrize_with_cases("src_files", cases=".")
def test_copyfile_init(src_files):
# Act
copyfiles = CopyFiles(src_files)
# Arrange
assert "paths" in copyfiles.dataset.keys()
assert len(copyfiles.dataset["paths"]) == len(src_files)


@parametrize_with_cases("src_files", cases=".")
def test_copyfile_write(src_files, tmp_path):
# Arrange
expected_filenames = {f.name for f in src_files}
# Act
copyfiles = CopyFiles(src_files)
copyfiles.write(tmp_path)
# Assert
actual_filepaths = tmp_path.glob("*.inp")
actual_filenames = {f.name for f in actual_filepaths}
diff = expected_filenames ^ actual_filenames
assert len(diff) == 0


@parametrize_with_cases("src_files", cases=".")
def test_from_imod5_data(src_files):
# Arrange
imod5_ls = [[p] for p in src_files]
imod5_data = {"extra": {"paths": imod5_ls}}
# Act
copyfiles = CopyFiles.from_imod5_data(imod5_data)
# Assert
len(copyfiles.dataset["paths"]) == 3