-
Notifications
You must be signed in to change notification settings - Fork 2
/
__main__.py
102 lines (82 loc) · 3.11 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""The entry point for the myproject program."""
from argparse import ArgumentParser
from pathlib import Path
from typing import Any, cast
import pandas as pd
from wsimod.orchestration.model import Model
from wsimod.validation import (
_validate_input_dir,
_validate_output_dir,
assign_data_to_settings,
evaluate_input_file,
load_data_files,
validate_io_args,
)
def create_parser() -> ArgumentParser:
"""Create the CLI argument parser."""
parser = ArgumentParser(prog="WSIMOD")
parser.add_argument(
"settings",
type=Path,
help="Path to the WSIMOD input file, in YAML format.",
)
parser.add_argument(
"--inputs",
"-i",
type=Path,
help="Base directory for all input files. If present, overwrites value in the"
" settings file.",
)
parser.add_argument(
"--outputs",
"-o",
type=Path,
help="Base directory for all output files. If present, overwrites value in the"
" settings file.",
)
return parser
def run_model(settings: dict[str, Any], outputs: Path) -> None:
"""Runs the mode with the chosen settings and saves the outputs as csv.
Args:
settings (dict[str, Any]): Settings dictionary with loaded data.
outputs(Path): Directory where to save the outputs.
"""
model = Model()
model.dates = cast(pd.Series, settings["dates"]).drop_duplicates()
model.add_nodes(settings["nodes"])
model.add_arcs(settings["arcs"])
flows, tanks, _, surfaces = model.run()
pd.DataFrame(flows).to_csv(outputs / "flows.csv")
pd.DataFrame(tanks).to_csv(outputs / "tanks.csv")
pd.DataFrame(surfaces).to_csv(outputs / "surfaces.csv")
def run_saved_model(settings: str, inputs: Path, outputs: Path) -> None:
"""Runs a previously saved model.
Args:
settings (str): The name of the settings file to load.
inputs (Path): The location of that file, as well as any other input file.
outputs (Path): Directory where to save the outputs.
"""
model = Model()
model.load(inputs, config_name=settings)
flows, tanks, _, surfaces = model.run()
pd.DataFrame(flows).to_csv(outputs / "flows.csv")
pd.DataFrame(tanks).to_csv(outputs / "tanks.csv")
pd.DataFrame(surfaces).to_csv(outputs / "surfaces.csv")
def run() -> None:
"""Main entry point of the application."""
args = vars(create_parser().parse_args())
settings_type = evaluate_input_file(args["settings"])
if settings_type == "custom":
settings = validate_io_args(**args)
inputs = settings.pop("inputs")
outputs = settings.pop("outputs")
loaded_data = load_data_files(settings.pop("data", {}), inputs)
loaded_settings = assign_data_to_settings(settings, loaded_data)
run_model(loaded_settings, outputs)
else:
settings_file: Path = args["settings"]
inputs = _validate_input_dir(args["inputs"], settings_file.parent)
outputs = _validate_output_dir(args["outputs"], settings_file.parent)
run_saved_model(settings_file.name, inputs, outputs)
if __name__ == "__main__":
run()