From 437263b510c4a6f82a4fb0f41f76ef8273ae231d Mon Sep 17 00:00:00 2001 From: Briochh Date: Tue, 16 Nov 2021 09:26:46 +1300 Subject: [PATCH 1/5] adding test to t501 to catch subdir issue --- autotest/t501_test.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/autotest/t501_test.py b/autotest/t501_test.py index 773fb6c6cc..3366b882cc 100644 --- a/autotest/t501_test.py +++ b/autotest/t501_test.py @@ -165,6 +165,31 @@ def test_mf6_string_to_file_path(): raise AssertionError("Relative path error") +def test_mf6_subdir(): + base_dir = base_test_dir(__file__, rel_path="temp", verbose=True) + test_setup = FlopyTestSetup(verbose=True, test_dirs=base_dir) + + sim = flopy.mf6.MFSimulation(sim_ws=base_dir) + tdis = flopy.mf6.modflow.mftdis.ModflowTdis(sim) + gwf = flopy.mf6.ModflowGwf(sim, model_rel_path='level2') + ims = flopy.mf6.modflow.mfims.ModflowIms(sim) + sim.register_ims_package(ims, []) + dis = flopy.mf6.modflow.mfgwfdis.ModflowGwfdis(gwf) + sim.set_all_data_external(external_data_folder='dat') + sim.write_simulation() + + sim_r = flopy.mf6.MFSimulation.load( + 'mfsim.nam', + sim_ws=sim.simulation_data.mfpath.get_sim_path(), + ) + gwf_r = sim_r.get_model() + assert gwf.dis.delc.get_file_entry() == gwf_r.dis.delc.get_file_entry(), ( + "Something wrong with model external paths") + + + + if __name__ == "__main__": - test_mf6() - test_mf6_string_to_file_path() + # test_mf6() + # test_mf6_string_to_file_path() + test_mf6_subdir() From a365100e557a845279b57da2e3b502d729133095 Mon Sep 17 00:00:00 2001 From: Briochh Date: Tue, 16 Nov 2021 17:52:09 +1300 Subject: [PATCH 2/5] quick fix for model with externals in sim sub dirs --- autotest/t501_test.py | 11 +++++++++++ flopy/mf6/data/mfdatastorage.py | 8 +++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/autotest/t501_test.py b/autotest/t501_test.py index 3366b882cc..791b37183f 100644 --- a/autotest/t501_test.py +++ b/autotest/t501_test.py @@ -186,7 +186,18 @@ def test_mf6_subdir(): assert gwf.dis.delc.get_file_entry() == gwf_r.dis.delc.get_file_entry(), ( "Something wrong with model external paths") + sim_r.set_all_data_internal() + sim_r.set_all_data_external( + external_data_folder=os.path.join('dat', "dat_l2")) + sim_r.write_simulation() + sim_r2 = flopy.mf6.MFSimulation.load( + 'mfsim.nam', + sim_ws=sim_r.simulation_data.mfpath.get_sim_path(), + ) + gwf_r2 = sim_r.get_model() + assert gwf_r.dis.delc.get_file_entry() == gwf_r2.dis.delc.get_file_entry(), ( + "Something wrong with model external paths") if __name__ == "__main__": diff --git a/flopy/mf6/data/mfdatastorage.py b/flopy/mf6/data/mfdatastorage.py index c59cb3de5b..ecc1c54aae 100644 --- a/flopy/mf6/data/mfdatastorage.py +++ b/flopy/mf6/data/mfdatastorage.py @@ -1511,7 +1511,13 @@ def store_external( ] if rel_path is not None and len(rel_path) > 0 and rel_path != ".": # include model relative path in external file path - fp_relative = os.path.join(rel_path, file_path) + # only if model relative path is not already in external + # file path i.e. when reading! + fp_rp_l = os.path.normpath(fp_relative).split(os.path.sep) + rp_l_r = os.path.normpath(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 = self._simulation_data.mfpath.resolve_path(fp_relative, model_name) if data is not None: if self.data_structure_type == DataStructureType.recarray: From 6d2c61d041a85d3c993e0783675d46ed78646473 Mon Sep 17 00:00:00 2001 From: Briochh Date: Tue, 16 Nov 2021 18:08:24 +1300 Subject: [PATCH 3/5] clean up paths --- flopy/mf6/data/mfdatastorage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flopy/mf6/data/mfdatastorage.py b/flopy/mf6/data/mfdatastorage.py index ecc1c54aae..0056716a04 100644 --- a/flopy/mf6/data/mfdatastorage.py +++ b/flopy/mf6/data/mfdatastorage.py @@ -1513,8 +1513,8 @@ 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 = os.path.normpath(fp_relative).split(os.path.sep) - rp_l_r = os.path.normpath(rel_path).split(os.path.sep)[::-1] + 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) From 6ec3083d9a47e2dc719cbdcff1f321dafae6d4f9 Mon Sep 17 00:00:00 2001 From: Briochh Date: Tue, 16 Nov 2021 18:26:46 +1300 Subject: [PATCH 4/5] blacked --- flopy/mf6/data/mfdatastorage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flopy/mf6/data/mfdatastorage.py b/flopy/mf6/data/mfdatastorage.py index 0056716a04..cd0627dfae 100644 --- a/flopy/mf6/data/mfdatastorage.py +++ b/flopy/mf6/data/mfdatastorage.py @@ -1516,7 +1516,7 @@ def store_external( 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]: + if rp != fp_rp_l[len(rp_l_r) - i - 1]: fp_relative = os.path.join(rp, fp_relative) fp = self._simulation_data.mfpath.resolve_path(fp_relative, model_name) if data is not None: From 7b438a598ce82d80263b871359812f75cdaf8821 Mon Sep 17 00:00:00 2001 From: Briochh Date: Tue, 16 Nov 2021 18:28:42 +1300 Subject: [PATCH 5/5] blacked more --- autotest/t501_test.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/autotest/t501_test.py b/autotest/t501_test.py index 791b37183f..aca0b65aac 100644 --- a/autotest/t501_test.py +++ b/autotest/t501_test.py @@ -171,33 +171,36 @@ def test_mf6_subdir(): sim = flopy.mf6.MFSimulation(sim_ws=base_dir) tdis = flopy.mf6.modflow.mftdis.ModflowTdis(sim) - gwf = flopy.mf6.ModflowGwf(sim, model_rel_path='level2') + gwf = flopy.mf6.ModflowGwf(sim, model_rel_path="level2") ims = flopy.mf6.modflow.mfims.ModflowIms(sim) sim.register_ims_package(ims, []) dis = flopy.mf6.modflow.mfgwfdis.ModflowGwfdis(gwf) - sim.set_all_data_external(external_data_folder='dat') + sim.set_all_data_external(external_data_folder="dat") sim.write_simulation() sim_r = flopy.mf6.MFSimulation.load( - 'mfsim.nam', + "mfsim.nam", sim_ws=sim.simulation_data.mfpath.get_sim_path(), ) gwf_r = sim_r.get_model() - assert gwf.dis.delc.get_file_entry() == gwf_r.dis.delc.get_file_entry(), ( - "Something wrong with model external paths") + assert ( + gwf.dis.delc.get_file_entry() == gwf_r.dis.delc.get_file_entry() + ), "Something wrong with model external paths" sim_r.set_all_data_internal() sim_r.set_all_data_external( - external_data_folder=os.path.join('dat', "dat_l2")) + external_data_folder=os.path.join("dat", "dat_l2") + ) sim_r.write_simulation() sim_r2 = flopy.mf6.MFSimulation.load( - 'mfsim.nam', + "mfsim.nam", sim_ws=sim_r.simulation_data.mfpath.get_sim_path(), ) gwf_r2 = sim_r.get_model() - assert gwf_r.dis.delc.get_file_entry() == gwf_r2.dis.delc.get_file_entry(), ( - "Something wrong with model external paths") + assert ( + gwf_r.dis.delc.get_file_entry() == gwf_r2.dis.delc.get_file_entry() + ), "Something wrong with model external paths" if __name__ == "__main__":