Skip to content

Commit b73a47e

Browse files
committed
refactor(make_registry.py): shortcut to create default registry
1 parent 134fb4c commit b73a47e

4 files changed

Lines changed: 83 additions & 30 deletions

File tree

docs/md/models.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ The script can be executed with `python -m modflow_devtools.make_registry`. It a
106106
- `--model-name-prefix`: Optionally specify a string to prepend to model names. Useful for avoiding collisions.
107107
- `--namefile`: Optionally specify the glob pattern for namefiles. By default, only `mfsim.nam` (MF6) are found.
108108

109-
For example, to create a registry of models in the MF6 examples and test models repositories, assuming each is checked out next to this project:
109+
For example, to create the "default" registry of models in the MF6 examples and test models repositories, assuming each is checked out next to this project:
110110

111111
```shell
112-
python -m modflow_devtools.make_registry ../modflow6-examples/examples --url https://github.com/MODFLOW-ORG/modflow6-examples/releases/download/current/mf6examples.zip --model-name-prefix mf6/example
113-
python -m modflow_devtools.make_registry ../modflow6-testmodels/mf6 --append --url https://github.com/MODFLOW-ORG/modflow6-testmodels/raw/master/mf6 --model-name-prefix mf6/test
114-
python -m modflow_devtools.make_registry ../modflow6-largetestmodels --append --url https://github.com/MODFLOW-ORG/modflow6-largetestmodels/raw/master --model-name-prefix mf6/large
115-
python -m modflow_devtools.make_registry ../modflow6-testmodels/mf5to6 --append --url https://github.com/MODFLOW-ORG/modflow6-testmodels/raw/master/mf5to6 --model-name-prefix mf2005 --namefile "*.nam"
112+
python -m modflow_devtools.make_registry -p ../modflow6-examples/examples --url https://github.com/MODFLOW-ORG/modflow6-examples/releases/download/current/mf6examples.zip --model-name-prefix mf6/example
113+
python -m modflow_devtools.make_registry -p ../modflow6-testmodels/mf6 --append --url https://github.com/MODFLOW-ORG/modflow6-testmodels/raw/master/mf6 --model-name-prefix mf6/test
114+
python -m modflow_devtools.make_registry -p ../modflow6-largetestmodels --append --url https://github.com/MODFLOW-ORG/modflow6-largetestmodels/raw/master --model-name-prefix mf6/large
115+
python -m modflow_devtools.make_registry -p ../modflow6-testmodels/mf5to6 --append --url https://github.com/MODFLOW-ORG/modflow6-testmodels/raw/master/mf5to6 --model-name-prefix mf2005 --namefile "*.nam"
116116
```
117117

118-
Above we adopt a convention of prefixing model names with the model type (i.e. the program used to run it), e.g. "mf6/" or "mf2005/". Relative path parts below the initial prefix reflect the model's relative path within its repository.
118+
As a shortcut to create the default registry, the script can be run with no arguments: `python -m modflow_devtools.make_registry`.

modflow_devtools/make_registry.py

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
11
import argparse
2+
from pathlib import Path
23

34
import modflow_devtools.models as models
45

6+
_REPOS_PATH = Path(__file__).parents[2]
7+
_DEFAULT_REGISTRY_OPTIONS = [
8+
{
9+
"path": _REPOS_PATH / "modflow6-examples" / "examples",
10+
"url": "https://github.com/MODFLOW-ORG/modflow6-examples/releases/download/current/mf6examples.zip",
11+
"model-name-prefix": "mf6/example",
12+
},
13+
{
14+
"path": _REPOS_PATH / "modflow6-testmodels" / "mf6",
15+
"url": "https://github.com/MODFLOW-ORG/modflow6-testmodels/raw/master/mf6",
16+
"model-name-prefix": "mf6/test",
17+
},
18+
{
19+
"path": _REPOS_PATH / "modflow6-largetestmodels",
20+
"url": "https://github.com/MODFLOW-ORG/modflow6-largetestmodels/raw/master",
21+
"model-name-prefix": "mf6/large",
22+
},
23+
{
24+
"path": _REPOS_PATH / "modflow6-testmodels" / "mf5to6",
25+
"url": "https://github.com/MODFLOW-ORG/modflow6-testmodels/raw/master/mf5to6",
26+
"model-name-prefix": "mf2005",
27+
"namefile": "*.nam",
28+
},
29+
]
30+
31+
532
if __name__ == "__main__":
633
parser = argparse.ArgumentParser(description="Make a registry of models.")
7-
parser.add_argument("path")
834
parser.add_argument(
9-
"--append",
10-
"-a",
11-
action="store_true",
12-
help="Append instead of overwriting.",
35+
"--path",
36+
"-p",
37+
required=False,
38+
default=None,
39+
type=str,
40+
help="Path to the model directory.",
1341
)
1442
parser.add_argument(
1543
"--model-name-prefix",
16-
"-p",
1744
type=str,
1845
help="Prefix for model names.",
1946
default="",
@@ -32,14 +59,31 @@
3259
help="Namefile pattern to look for in the model directories.",
3360
default="mfsim.nam",
3461
)
62+
parser.add_argument(
63+
"--verbose",
64+
"-v",
65+
action="store_true",
66+
help="Show verbose output.",
67+
)
3568
args = parser.parse_args()
36-
if not args.append:
37-
models.DEFAULT_REGISTRY = models.PoochRegistry(
38-
base_url=args.url, env=models._DEFAULT_ENV
69+
if args.path:
70+
if args.verbose:
71+
print(f"Adding {args.path} to the registry.")
72+
models.DEFAULT_REGISTRY.index(
73+
path=args.path,
74+
url=args.url,
75+
prefix=args.model_name_prefix,
76+
namefile=args.namefile,
3977
)
40-
models.DEFAULT_REGISTRY.index(
41-
path=args.path,
42-
url=args.url,
43-
prefix=args.model_name_prefix,
44-
namefile=args.namefile,
45-
)
78+
else:
79+
if args.verbose:
80+
print("No path provided, creating default registry.")
81+
for options in _DEFAULT_REGISTRY_OPTIONS:
82+
if args.verbose:
83+
print(f"Adding {options.path} to the registry.")
84+
models.DEFAULT_REGISTRY.index(
85+
path=options["path"],
86+
url=options["url"],
87+
prefix=options["model-name-prefix"],
88+
namefile=options.get("namefile", "mfsim.nam"),
89+
)

modflow_devtools/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def _drop_none_or_empty(path, key, value):
3030
return True
3131

3232

33+
def _model_sort_key(k) -> int:
34+
if "gwf" in k:
35+
return 0
36+
return 1
37+
38+
3339
def _sha256(path: Path) -> str:
3440
"""
3541
Compute the SHA256 hash of the given file.
@@ -414,6 +420,9 @@ def index(
414420
files[name] = {"hash": hash, "url": url_}
415421
models[model_name].append(name)
416422

423+
for example_name in examples.keys():
424+
examples[example_name] = sorted(examples[example_name], key=_model_sort_key)
425+
417426
with self._registry_file_path.open("ab+") as registry_file:
418427
tomli_w.dump(
419428
remap(dict(sorted(files.items())), visit=_drop_none_or_empty),

modflow_devtools/registry/examples.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ ex-gwf-csub-p02b = [
5959
"mf6/example/ex-gwf-csub-p02b",
6060
]
6161
ex-gwf-csub-p02c = [
62+
"mf6/example/ex-gwf-csub-p02c/hb-020",
63+
"mf6/example/ex-gwf-csub-p02c/hb-100",
6264
"mf6/example/ex-gwf-csub-p02c/hb-010",
63-
"mf6/example/ex-gwf-csub-p02c/es-100",
64-
"mf6/example/ex-gwf-csub-p02c/hb-005",
65-
"mf6/example/ex-gwf-csub-p02c/es-050",
66-
"mf6/example/ex-gwf-csub-p02c/hb-002",
67-
"mf6/example/ex-gwf-csub-p02c/es-010",
6865
"mf6/example/ex-gwf-csub-p02c/es-020",
69-
"mf6/example/ex-gwf-csub-p02c/hb-100",
7066
"mf6/example/ex-gwf-csub-p02c/es-005",
67+
"mf6/example/ex-gwf-csub-p02c/es-010",
68+
"mf6/example/ex-gwf-csub-p02c/hb-050",
7169
"mf6/example/ex-gwf-csub-p02c/hb-001",
7270
"mf6/example/ex-gwf-csub-p02c/es-001",
73-
"mf6/example/ex-gwf-csub-p02c/hb-020",
74-
"mf6/example/ex-gwf-csub-p02c/hb-050",
7571
"mf6/example/ex-gwf-csub-p02c/es-002",
72+
"mf6/example/ex-gwf-csub-p02c/es-050",
73+
"mf6/example/ex-gwf-csub-p02c/es-100",
74+
"mf6/example/ex-gwf-csub-p02c/hb-002",
75+
"mf6/example/ex-gwf-csub-p02c/hb-005",
7676
]
7777
ex-gwf-csub-p03a = [
7878
"mf6/example/ex-gwf-csub-p03a",
@@ -228,8 +228,8 @@ ex-gwt-henry-b = [
228228
]
229229
ex-gwt-keating = [
230230
"mf6/example/ex-gwt-keating/mf6gwf",
231-
"mf6/example/ex-gwt-keating/mf6gwt",
232231
"mf6/example/ex-gwt-keating/mf6prt",
232+
"mf6/example/ex-gwt-keating/mf6gwt",
233233
]
234234
ex-gwt-moc3d-p01a = [
235235
"mf6/example/ex-gwt-moc3d-p01a/mf6gwf",

0 commit comments

Comments
 (0)