From 0e3120a9d1cf53ecc98b861aeb05e3a8fa7afb71 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Tue, 21 May 2024 07:59:45 -0400 Subject: [PATCH] fix(get_model_paths): fix model order within scenario (#156) --- autotest/test_misc.py | 8 ++++---- modflow_devtools/misc.py | 42 +++++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/autotest/test_misc.py b/autotest/test_misc.py index c3034a3..5997bbc 100644 --- a/autotest/test_misc.py +++ b/autotest/test_misc.py @@ -151,12 +151,12 @@ def get_expected_namefiles(path, pattern="mfsim.nam") -> List[Path]: def test_get_model_paths_examples(): expected_paths = get_expected_model_dirs(_examples_path) paths = get_model_paths(_examples_path) - assert paths == sorted(list(set(paths))) # no duplicates + assert sorted(paths) == sorted(list(set(paths))) # no duplicates assert set(expected_paths) == set(paths) expected_paths = get_expected_model_dirs(_examples_path, "*.nam") paths = get_model_paths(_examples_path, namefile="*.nam") - assert paths == sorted(list(set(paths))) + assert sorted(paths) == sorted(list(set(paths))) assert set(expected_paths) == set(paths) @@ -166,12 +166,12 @@ def test_get_model_paths_examples(): def test_get_model_paths_largetestmodels(): expected_paths = get_expected_model_dirs(_examples_path) paths = get_model_paths(_examples_path) - assert paths == sorted(list(set(paths))) + assert sorted(paths) == sorted(list(set(paths))) assert set(expected_paths) == set(paths) expected_paths = get_expected_model_dirs(_examples_path) paths = get_model_paths(_examples_path) - assert paths == sorted(list(set(paths))) + assert sorted(paths) == sorted(list(set(paths))) assert set(expected_paths) == set(paths) diff --git a/modflow_devtools/misc.py b/modflow_devtools/misc.py index 19931ee..08a03f4 100644 --- a/modflow_devtools/misc.py +++ b/modflow_devtools/misc.py @@ -284,13 +284,41 @@ def get_model_paths( Find model directories recursively in the given location. A model directory is any directory containing one or more namefiles. Model directories can be filtered or excluded, - by prefix, pattern, namefile name, or packages used. - """ - - namefile_paths = get_namefile_paths( - path, prefix, namefile, excluded, selected, packages - ) - model_paths = sorted(list(set([p.parent for p in namefile_paths if p.parent.name]))) + by prefix, pattern, namefile name, or packages used. The + directories are returned in order within scenario folders + such that groundwater flow model workspaces precede other + model types. This allows models which depend on the flow + model's outputs to consume its head or budget, and models + should successfully run in the sequence returned provided + input files (e.g. FMI) refer to output via relative paths. + """ + + def keyfunc(v): + v = str(v) + if "gwf" in v: + return 0 + else: + return 1 + + model_paths = [] + globbed = path.rglob(f"{prefix if prefix else ''}*") + example_paths = [p for p in globbed if p.is_dir()] + for p in example_paths: + for mp in sorted( + list( + set( + [ + p.parent + for p in get_namefile_paths( + p, prefix, namefile, excluded, selected, packages + ) + ] + ) + ), + key=keyfunc, + ): + if mp not in model_paths: + model_paths.append(mp) return model_paths