-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_calibration.py
More file actions
208 lines (193 loc) · 7.13 KB
/
run_calibration.py
File metadata and controls
208 lines (193 loc) · 7.13 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import argparse
import functools
import subprocess
import time
from mswm.build_inputs import RealizationBuilder
from utils import (
datetime_from_str,
str_from_datetime,
timedelta_from_effective_days,
effective_days_from_timedelta,
timedelta_from_pandas_str,
get_calibration_log_file_overwrite_path,
configure_ngen_log,
)
import utils_testing_setup
from execution_tests import (
get_test_configs__calibration,
make_parallel_config,
)
import consts as c
from configs import RTECalibConfig, CalibTimeWindows
print = functools.partial(print, flush=True)
def calibration__build_and_run(cfg: RTECalibConfig) -> None:
"""Build calibration realizations and run them as tests."""
windows = CalibTimeWindows(
calib_sim_start=cfg.calib_sim_start,
calib_sim_duration=cfg.calib_sim_duration,
calib_eval_delayment=cfg.calib_eval_delayment,
valid_sim_advancement=cfg.valid_sim_advancement,
valid_eval_curtailment=cfg.valid_eval_curtailment,
)
all_config_overrides = get_test_configs__calibration(
nprocs=cfg.nprocs,
gage_id=cfg.gage_id,
gage_vintage=cfg.gage_vintage,
obj_func=cfg.objective_function,
optim_algo=cfg.optimization_algorithm,
forcing_config_types=[cfg.forcing_source],
global_domain=cfg.global_domain,
forcing_provider=cfg.forcing_provider,
forcing_static_dir=cfg.forcing_static_dir,
windows=windows,
obs_dir=cfg.obs_dir,
nwmretro_file=cfg.nwmretro_file,
)
assert (
len(all_config_overrides) == 1
) # Can be > 1 in test runner, not in atomic calibration runner
config_overrides = all_config_overrides[0]
rb_kwargs = {"config_overrides": config_overrides}
rb = RealizationBuilder(**rb_kwargs)
rb.build_calib_realization()
configure_ngen_log(rb.work_dir, "cal")
log_path = get_calibration_log_file_overwrite_path(rb)
cmd = [
"calibration",
str(rb.calib_config_file),
"--log_path_overwrite",
log_path,
# "--worker_name",
# "000test",
]
print(
f"\n\nStarting calibration with configuration: {cfg.model_dump_json(indent=2)}\nand logging to: {log_path}\n\nvia command args: {cmd}"
)
start = time.perf_counter()
proc = subprocess.run(cmd, check=False)
print(
f"\nFinished calibration with configuration: {cfg.model_dump_json(indent=2)},\nfinished in {((time.perf_counter() - start) / 60):.1f} minutes.\nLog path: {log_path}\nReturn code {proc.returncode}."
)
proc.check_returncode()
def main(cfg: RTECalibConfig):
if cfg.delete_scratch_and_mesh_first:
utils_testing_setup.delete_scratch_and_esmf_outputs(cfg)
if cfg.delete_forcing_raw_input_first:
utils_testing_setup.delete_forcing_raw_inputs()
calibration__build_and_run(cfg)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-delscratch",
"--delete_scratch_and_mesh_first",
action="store_true",
help="Delete scratch dir and ESMF mesh files before the run, which forces ESMF and NetCDF actions to occur.",
)
parser.add_argument(
"-delraw",
"--delete_forcing_raw_input_first",
action="store_true",
help=f"Delete contents of {repr(c.DIR_FORCING_RAW_INPUT)} before the run, which forces forcing data to be re-downloaded.",
)
parser.add_argument(
"-ofunc",
"--objective_function",
type=c.CalObjective,
default=c.CALIB_OBJECTIVE_FUNCTION,
help=f"Objective function for calibration. Default: {c.CALIB_OBJECTIVE_FUNCTION}",
)
parser.add_argument(
"-optalgo",
"--optimization_algorithm",
type=c.CalOptimizationAlgo,
default=c.CALIB_OPTIMIZATION_ALGO,
help=f"Optimization algorithm for calibration. Default: {c.CALIB_OPTIMIZATION_ALGO}",
)
parser.add_argument(
"-n",
"--nprocs",
type=int,
default=c.DEFAULT_NPROCS,
help=f"""
Currently only affects Calibration. Replaces default value for nprocs ({repr(c.DEFAULT_NPROCS)}) and subsequently the ParallelConfig instance.
When nprocs is 1, Calibration's ParallelConfig is: {make_parallel_config(nprocs=1)}.
When nprocs > 1, Calibration's ParallelConfig is like: {make_parallel_config(nprocs=2)}
""",
)
parser.add_argument(
"-g",
"--gage_id__gage_vintage",
type=str,
nargs=2,
default=[c.DEFAULT_GAGE_ID, c.DEFAULT_GAGE_VINTAGE],
help=f"Calibration gage ID and gage vintage (2 args). Defaults={c.DEFAULT_GAGE_ID}, {c.DEFAULT_GAGE_VINTAGE}",
)
parser.add_argument(
"-start",
"--calib_sim_start",
type=datetime_from_str,
default=str_from_datetime(c.CALIB_SIM_START_DEFAULT),
help=f"Start time for calibration. Default={str_from_datetime(c.CALIB_SIM_START_DEFAULT)},",
)
parser.add_argument(
"-dur",
"--calib_sim_duration",
type=timedelta_from_effective_days,
default=c.CALIB_SIM_DURATION_DEFAULT,
help=f"Duration of calibration, in days (provide integer). Default={effective_days_from_timedelta(c.CALIB_SIM_DURATION_DEFAULT)}",
)
parser.add_argument(
"-evaldelay",
"--calib_eval_delayment",
type=timedelta_from_pandas_str,
default=c.CALIB_EVAL_DELAYMENT_DEFAULT,
help=f"Pandas-style timedelta string. Default={c.CALIB_EVAL_DELAYMENT_DEFAULT}",
)
parser.add_argument(
"-validadvance",
"--valid_sim_advancement",
type=timedelta_from_pandas_str,
default=c.VALID_SIM_ADVANCEMENT_DEFAULT,
help=f"Pandas-style timedelta string. Default={c.VALID_SIM_ADVANCEMENT_DEFAULT}",
)
parser.add_argument(
"-evalcurtail",
"--valid_eval_curtailment",
type=timedelta_from_pandas_str,
default=c.VALID_EVAL_CURTAILMENT_DEFAULT,
help=f"Pandas-style timedelta string. Default={c.VALID_EVAL_CURTAILMENT_DEFAULT}",
)
parser.add_argument(
"-fsrc",
"--forcing_source",
type=str,
default=c.CALIB_FORCING_CONFIGURATION_TYPE_DEFAULT,
choices=c.CALIB_FORCING_CONFIGURATION_TYPES,
help=f"Source of forcing data. Default={c.CALIB_FORCING_CONFIGURATION_TYPE_DEFAULT}",
)
parser.add_argument(
"-gdomain",
"--global_domain",
type=str,
default=c.CALIB_GLOBAL_DOMAIN_DEFAULT,
choices=c.CALIB_GLOBAL_DOMAIN_CHOICES,
help=f"Region of forcing data. Default={c.CALIB_GLOBAL_DOMAIN_DEFAULT}",
)
parser.add_argument(
"-fprovider",
"--forcing_provider",
type=str,
default=c.FORCING_PROVIDER_DEFAULT,
choices=c.FORCING_PROVIDER_CHOICES,
help=f"Forcing provider. Default={c.FORCING_PROVIDER_DEFAULT}",
)
parser.add_argument(
"-fstatic",
"--forcing_static_dir",
type=str,
default=c.FORCING_STATIC_DIR_DEFAULT,
help=f"Directory for static forcing files, used when forcing_provider is 'bmi'. Default={c.FORCING_STATIC_DIR_DEFAULT}",
)
args = parser.parse_args()
cfg = RTECalibConfig(**vars(args))
main(cfg)