From badcecd2b2515dbda4ec13399dc87c5c5ff73c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dav=C3=ADd=20Brakenhoff?= Date: Mon, 23 Dec 2024 11:20:39 +0100 Subject: [PATCH 1/2] feat(mf2005,mf2k): allow loading custom flopy packages - add extra_pkgs kwarg, which updates the mfnam_packages dictionary. --- flopy/modflow/mf.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/flopy/modflow/mf.py b/flopy/modflow/mf.py index dbf717ea3..25d2a99b7 100644 --- a/flopy/modflow/mf.py +++ b/flopy/modflow/mf.py @@ -82,6 +82,9 @@ class Modflow(BaseModel): Location for external files. verbose : bool, default False Print additional information to the screen. + extra_pkgs : dict, optional + Add custom packages classes to mfnam_packages. Allows for loading models + with custom packages not contained in the standard flopy distribution. Attributes ---------- @@ -113,6 +116,7 @@ def __init__( model_ws: Union[str, os.PathLike] = os.curdir, external_path: Optional[Union[str, os.PathLike]] = None, verbose=False, + extra_pkgs: Optional[dict] = None, **kwargs, ): super().__init__( @@ -224,6 +228,8 @@ def __init__( "vdf": flopy.seawat.SeawatVdf, "vsc": flopy.seawat.SeawatVsc, } + if extra_pkgs: + self.mfnam_packages.update(extra_pkgs) def __repr__(self): nrow, ncol, nlay, nper = self.get_nrow_ncol_nlay_nper() @@ -632,6 +638,7 @@ def load( load_only=None, forgive=False, check=True, + extra_pkgs: Optional[dict] = None, ): """ Load an existing MODFLOW model. @@ -661,6 +668,10 @@ def load( useful for debugging. Default False. check : boolean, optional Check model input for common errors. Default True. + extra_pkgs : dict, optional + Add custom packages classes to mfnam_packages. Allows for loading models + with custom packages not contained in the standard flopy distribution. + Returns ------- @@ -692,6 +703,7 @@ def load( exe_name=exe_name, verbose=verbose, model_ws=model_ws, + extra_pkgs=extra_pkgs, **attribs, ) From 950e53494c4c3649fff3cc2c032a69570cf502c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dav=C3=ADd=20Brakenhoff?= Date: Mon, 6 Jan 2025 14:30:48 +0100 Subject: [PATCH 2/2] add test for load with extra pkg --- autotest/test_modflow.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/autotest/test_modflow.py b/autotest/test_modflow.py index 304b4f6f9..8563736ad 100644 --- a/autotest/test_modflow.py +++ b/autotest/test_modflow.py @@ -77,6 +77,22 @@ def test_modflow_load(namfile, example_data_path): assert model.model_ws == str(mpath) +def test_modflow_load_with_extra_pkg(example_data_path): + namfile = Path("freyberg") / "freyberg.nam" + mpath = Path(example_data_path / namfile).parent + + # extra pkg + dummy_extra_pkg_for_test = {"DUM": "Dummy"} + + model = Modflow.load( + mpath / namfile.name, + verbose=True, + model_ws=mpath, + extra_pkgs=dummy_extra_pkg_for_test, + ) + assert model.mfnam_packages["DUM"] == "Dummy" + + @pytest.mark.parametrize( "path,expected", [