diff --git a/autotest/test_mf6.py b/autotest/test_mf6.py index 4a73f90ba..7a455ba75 100644 --- a/autotest/test_mf6.py +++ b/autotest/test_mf6.py @@ -499,6 +499,50 @@ def test_subdir(function_tmpdir): ) +@pytest.mark.parametrize("sep", ["win", "posix"]) +def test_load_subdir_ext_files_with_any_sep(function_tmpdir, sep): + if sep == "win" and platform.system() != "Windows": + pytest.skip("backslash is not a path separator on this OS") + + # model in a subdirectory with external array and list files + sim = MFSimulation(sim_ws=function_tmpdir) + ModflowTdis(sim) + ims = ModflowIms(sim) + gwf = ModflowGwf(sim, modelname="gwf", model_rel_path="level2") + sim.register_ims_package(ims, [gwf.name]) + botm = np.array([[[-1.0, -2.0, -3.0], [-4.0, -5.0, -6.0]]]) + ModflowGwfdis(gwf, nlay=1, nrow=2, ncol=3, top=10.0, botm=botm) + spd = [[(0, 0, 0), 1.0], [(0, 1, 2), 0.0]] + ModflowGwfchd(gwf, stress_period_data={0: spd}) + sim.set_all_data_external(external_data_folder="dat") + sim.write_simulation() + + # move external files to level2/dat and reference them relative to the + # simulation folder with the given separator, e.g. "level2/dat/botm.txt" + # as written by other tools such as iMOD Python + ext_dir = function_tmpdir / "level2" / "dat" + for ext_file in (function_tmpdir / "dat").glob("*"): + ext_file.rename(ext_dir / ext_file.name) + s = "\\" if sep == "win" else "/" + for pkg_file in (function_tmpdir / "level2").glob("gwf.*"): + lines = pkg_file.read_text().splitlines(keepends=True) + with open(pkg_file, "w") as f: + for l in lines: + tokens = l.split() + if tokens and tokens[0].lower() in ("open/close", "open\\close"): + name = Path(to_posix_sep(tokens[1].strip("'"))).name + tokens[:2] = ["open/close", s.join(["level2", "dat", name])] + l = f" {' '.join(tokens)}\n" + f.write(l) + + # external file paths must resolve to the files written above + sim_r = MFSimulation.load(sim_ws=function_tmpdir) + gwf_r = sim_r.get_model("gwf") + np.testing.assert_allclose(gwf_r.dis.botm.array, botm) + spd_r = gwf_r.chd.stress_period_data.get_data(0) + assert [tuple(r) for r in spd_r] == [tuple(r) for r in spd] + + @requires_exe("mf6") @pytest.mark.parametrize("layered", [True, False]) def test_binary_write(function_tmpdir, layered): diff --git a/flopy/mf6/data/mfdataplist.py b/flopy/mf6/data/mfdataplist.py index 3b7472fde..46e5d7331 100644 --- a/flopy/mf6/data/mfdataplist.py +++ b/flopy/mf6/data/mfdataplist.py @@ -5,6 +5,7 @@ import sys import warnings from collections.abc import Iterable +from pathlib import PureWindowsPath import numpy as np import pandas @@ -1480,11 +1481,10 @@ def _resolve_ext_file_path(self, data_storage): # include model relative path in external file path # only if model relative path is not already in external # file path i.e. when reading! - fp_rp_l = fp_relative.split(os.path.sep) - rp_l_r = rel_path.split(os.path.sep)[::-1] - for i, rp in enumerate(rp_l_r): - if rp != fp_rp_l[len(rp_l_r) - i - 1]: - fp_relative = os.path.join(rp, fp_relative) + fp_parts = PureWindowsPath(fp_relative).parts + rp_parts = PureWindowsPath(rel_path).parts + if fp_parts[: len(rp_parts)] != rp_parts: + fp_relative = os.path.join(rel_path, fp_relative) fp = self._simulation_data.mfpath.resolve_path(fp_relative, model_name) else: if fp_relative is not None: @@ -1794,11 +1794,10 @@ def _get_file_path(self): # include model relative path in external file path # only if model relative path is not already in external # file path i.e. when reading! - fp_rp_l = fp_relative.split(os.path.sep) - rp_l_r = rel_path.split(os.path.sep)[::-1] - for i, rp in enumerate(rp_l_r): - if rp != fp_rp_l[len(rp_l_r) - i - 1]: - fp_relative = os.path.join(rp, fp_relative) + fp_parts = PureWindowsPath(fp_relative).parts + rp_parts = PureWindowsPath(rel_path).parts + if fp_parts[: len(rp_parts)] != rp_parts: + fp_relative = os.path.join(rel_path, fp_relative) return self._simulation_data.mfpath.resolve_path( fp_relative, self._model_or_sim.name ) diff --git a/flopy/mf6/data/mfdatastorage.py b/flopy/mf6/data/mfdatastorage.py index afcf2596a..307b98f2c 100644 --- a/flopy/mf6/data/mfdatastorage.py +++ b/flopy/mf6/data/mfdatastorage.py @@ -3,6 +3,7 @@ import sys from copy import deepcopy from enum import Enum +from pathlib import PureWindowsPath from shutil import copyfile import numpy as np @@ -1710,11 +1711,10 @@ def store_external( # include model relative path in external file path # only if model relative path is not already in external # file path i.e. when reading! - fp_rp_l = fp_relative.split(os.path.sep) - rp_l_r = rel_path.split(os.path.sep)[::-1] - for i, rp in enumerate(rp_l_r): - if rp != fp_rp_l[len(rp_l_r) - i - 1]: - fp_relative = os.path.join(rp, fp_relative) + fp_parts = PureWindowsPath(fp_relative).parts + rp_parts = PureWindowsPath(rel_path).parts + if fp_parts[: len(rp_parts)] != rp_parts: + fp_relative = os.path.join(rel_path, fp_relative) fp = self._simulation_data.mfpath.resolve_path( fp_relative, model_name )