Python Reference#
The pyproject.toml of this repository primarily contains requirements for building documentation. The build sequence of the nwm-rte image is dynamic/configurable and involves multiple Python virtual environments.
CLI Help Menus#
These are raw outputs of the --help menu generated by Python's argparse library.
To update these, run: ./docs/update_python_cli_ref.sh
CLI Executable Modules#
run_default.py#
Called by run_default.sh
run_default
#
Command-line executable to build and run a "default" realization. Supports realtime forcing configurations, e.g. "short_range", as well as historical/retrospective sources, e.g. "aorc".
This runs inside the ngen runtime environment.
The CLI structure is mimicked in part by configs.RTEDefaultConfig.
For settings that are not exposed by CLI arguments, see primarily consts.py.
See run_default.sh for example calls.
build_default_realization
#
build_default_realization(
cfg: RTEDefaultConfig,
) -> RealizationBuilder
Build and return a non-coldstart forecast realization
Source code in bin_mounted/run_default.py
get_ngen_cmd
#
get_ngen_cmd(
cfg: RTEDefaultConfig, rb: RealizationBuilder
) -> list[str]
Build and return the ngen command as a list of strings. rb must have already been built, e.g. rb.build_default_realization() already called.
Source code in bin_mounted/run_default.py
run_calibration.py#
Called by run_calib.sh
run_calibration
#
Command-line executable to build and run a "calibration" realization.
This runs inside the ngen runtime environment.
The CLI structure is mimicked in part by configs.RTECalibConfig.
For settings that are not exposed by CLI arguments, see primarily consts.py.
See run_calib.sh for example calls.
calibration__build_and_run
#
calibration__build_and_run(cfg: RTECalibConfig) -> None
Build calibration realizations and run them as tests.
Source code in bin_mounted/run_calibration.py
run_forecast.py#
Called by run_fcst.sh
run_forecast
#
Command-line executable to build and run a "forecast" realization, optionally with a coldstart.
This runs inside the ngen runtime environment.
The CLI structure is mimicked in part by configs.RTEForecastConfig.
For settings that are not exposed by CLI arguments, see primarily consts.py.
See run_fcst.sh for example calls.
build_coldstart_realization
#
build_coldstart_realization(
cfg: RTEForecastConfig,
) -> RealizationBuilder
Build and return a coldstart forecast realization
Source code in bin_mounted/run_forecast.py
build_forecast_realization
#
build_forecast_realization(
cfg: RTEForecastConfig,
) -> RealizationBuilder
Build and return a non-coldstart forecast realization
Source code in bin_mounted/run_forecast.py
run_tests.py#
Called by run_tests.sh
run_tests
#
Command-line executable to build and run a series of "forecast" realizations, optionally with a "calibration" realization preceding them.
This runs inside the ngen runtime environment.
The CLI structure is mimicked in part by configs.RTETestConfig.
For settings that are not exposed by CLI arguments, see primarily consts.py.
When realizations fail, this program does not halt, but rather moves to the next configuration type in the list, with the goal of "trying" many different realization configurations in one call. The status of each configuration's build step and run step is reported and written to a json file at the end.
This includes options for stopping realizations mid-way through their run, rather than waiting for them to complete.
See run_tests.sh for example calls.
calibrations__build_and_run
#
calibrations__build_and_run(
cfg: RTETestConfig, tm: TestsManager
) -> None
Build calibration realizations and run them as tests.
Source code in bin_mounted/run_tests.py
forecasts__build_and_run
#
forecasts__build_and_run(
cfg: RTETestConfig, tm: TestsManager, cs: bool
) -> None
Using ForecastTest, build and execute a list of forecast realizations.
tests_manager is modified in-place, so some test results may be available if this function is interrupted.
cs controls whether coldstart is used (not cfg.do_coldstart).
Source code in bin_mounted/run_tests.py
run_noop_mode
#
Run noop mode - verify imports and basic setup without executing workflows.
Source code in bin_mounted/run_tests.py
Python Constants#
Currently bin_mounted/consts.py contains variables which rarely need editing, except for the MODELS formulation list. Soon, the MODELS variable will be parameterized as CLI argument(s).
Configuration Classes#
These mimic the CLI interfaces and perform some additional argument parsing and preparation of classes that are passed to other components of the system.
configs
#
TestPaths
dataclass
#
Paths dependent on calibration settings. If iterating over a list of objective functions or optimization algorithms, obj_func and optim_algo may need to be replaced on the fly during the iterations.
Source code in bin_mounted/configs.py
RTESetup
#
Bases: BaseModel
Used to set up a RTE run. Triggers certain setup actions, such as creation of WCOSS-path symlinks. Classes that inherit from this should call super().model_post_init(__context) inside their own model_post_init() method, if they have that method also defined in the child.
Source code in bin_mounted/configs.py
RTEDefaultConfig
#
Bases: RTESetup
Configuration class for building and running one default realization (realtime forcing configuration or historical / retrospective forcing configuration).
| ATTRIBUTE | DESCRIPTION |
|---|---|
delete_scratch_and_mesh_first |
Causes scratch dir and intermediary mesh to be deleted first
TYPE:
|
delete_forcing_raw_input_first |
Causes realtime forcing data cache dir to be deleted first
TYPE:
|
gage_id__gage_vintage |
Gage ID and vintage
TYPE:
|
global_domain |
e.g. "CONUS", "Hawaii", "Alaska", "PuertoRico"
TYPE:
|
forcing_static_dir |
Forcing static directory
TYPE:
|
forcing_provider |
Forcing provider, i.e. "bmi" or "csv"
TYPE:
|
cycle_datetime |
Start time of the realization
TYPE:
|
historical_sim_duration |
Duration of the simulation (only used for historical / retrospective forcing configurations)
TYPE:
|
forcing_configuration |
Forcing configuration, e.g. "aorc" or "short_range"
TYPE:
|
fcst_run_name |
Name of the forecast realization run. Affects a directory name.
TYPE:
|
nprocs |
Number of processors to use
TYPE:
|
# The following are set after init during self.model_post_init(). Do not provide |
|
gage_id |
Gage ID
TYPE:
|
gage_vintage |
Gage vintage
TYPE:
|
realtime_mode |
Realtime mode
TYPE:
|
realization_builder_kwargs |
Realization builder kwargs (passed to
TYPE:
|
Source code in bin_mounted/configs.py
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
RTECalibConfig
#
Bases: RTESetup
Configuration class for building and running one calibration realization.
| ATTRIBUTE | DESCRIPTION |
|---|---|
delete_scratch_and_mesh_first |
Causes scratch dir and intermediary mesh to be deleted first
TYPE:
|
delete_forcing_raw_input_first |
Causes realtime forcing data cache dir to be deleted first
TYPE:
|
objective_function |
Objective function, e.g. "kge"
TYPE:
|
optimization_algorithm |
Optimization algorithm, e.g. "dds"
TYPE:
|
nprocs |
Number of processors to use
TYPE:
|
gage_id__gage_vintage |
Gage ID and vintage
TYPE:
|
calib_sim_start |
Calibration start time
TYPE:
|
calib_sim_duration |
Calibration simulation duration
TYPE:
|
calib_eval_delayment |
Used for evaluation / validation time windowing
TYPE:
|
valid_sim_advancement |
Used for evaluation / validation time windowing
TYPE:
|
valid_eval_curtailment |
Used for evaluation / validation time windowing
TYPE:
|
forcing_source |
Source of forcing data, e.g. "aorc" or "nwm"
TYPE:
|
global_domain |
e.g. "CONUS", "Hawaii", "Alaska", "PuertoRico"
TYPE:
|
forcing_provider |
Forcing provider, i.e. "bmi" or "csv"
TYPE:
|
forcing_static_dir |
Forcing static directory
TYPE:
|
worker_name |
Name of the ngen worker (used to build a directory name)
TYPE:
|
# The following are set after init during self.model_post_init(). Do not provide. |
|
gage_id |
Gage ID
TYPE:
|
gage_vintage |
Gage vintage
TYPE:
|
obs_dir |
Directory of observed flow data
TYPE:
|
nwmretro_file |
File containing retrospective NWM flow data
TYPE:
|
Source code in bin_mounted/configs.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
RTEForecastConfig
#
Bases: RTESetup
Configuration class for building and running one forecast realization.
| ATTRIBUTE | DESCRIPTION |
|---|---|
delete_scratch_and_mesh_first |
Causes scratch dir and intermediary mesh to be deleted first
TYPE:
|
delete_forcing_raw_input_first |
Causes realtime forcing data cache dir to be deleted first
TYPE:
|
objective_function |
Affects input realization path. Objective function of previously-ran calibration realization, e.g. "kge"
TYPE:
|
optimization_algorithm |
Affects input realization path. Optimization algorithm of previously-ran calibration realization, e.g. "dds"
TYPE:
|
gage_id |
Gage ID
TYPE:
|
global_domain |
e.g. "CONUS", "Hawaii", "Alaska", "PuertoRico"
TYPE:
|
forcing_static_dir |
Forcing static directory
TYPE:
|
forcing_provider |
Forcing provider, i.e. "bmi" or "csv"
TYPE:
|
cycle_datetime |
Start time of the realization (or end time for coldstart, if
TYPE:
|
cold_start_datetime |
Start time of the coldstart realization. If None, coldstart is not performed.
TYPE:
|
forcing_configuration |
Forcing configuration, e.g. "aorc" or "short_range"
TYPE:
|
fcst_run_name |
Name of the forecast realization run
TYPE:
|
nprocs |
Number of processors to use
TYPE:
|
# The following are set after init during self.model_post_init(). Do not provide. |
|
run_dir_base |
Run directory root
TYPE:
|
run_dir_input |
Input run directory
TYPE:
|
run_dir_output |
Output run directory
TYPE:
|
ngen_log_file |
ngen stdout + stderr stream log file
TYPE:
|
valid_best_yaml |
Validation yaml file (output from previously-ran calibration realization)
TYPE:
|
realization_builder_kwargs |
Realization builder kwargs (passed to
TYPE:
|
Source code in bin_mounted/configs.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
RTETestConfig
#
Bases: RTESetup
Configuration class for building and running a set of test realizations.
| ATTRIBUTE | DESCRIPTION |
|---|---|
delete_scratch_and_mesh_first |
Causes scratch dir and intermediary mesh to be deleted first
TYPE:
|
delete_forcing_raw_input_first |
Causes realtime forcing data cache dir to be deleted first
TYPE:
|
skip_forecast |
Causes forecast to be skipped (only do calibration)
TYPE:
|
quit_forecast_after_forcing_running |
Causes forecasts to be stopped midway once log files indicate that the model is well underway
TYPE:
|
quit_forecast_after_duration |
Causes forecasts to be stopped midway after a set duration (seconds of processing time)
TYPE:
|
do_calibration |
Causes calibration to be ran, before forecasts (needed if a calibration has not yet been ran for the gage)
TYPE:
|
quit_calibration_after_duration |
Causes calibrations to be stopped midway after a set duration (seconds of processing time)
TYPE:
|
objective_functions |
For calibration, list of objective functions to run, e.g. "kge". Replaced with full list when do_all_objective_functions = True
TYPE:
|
do_all_objective_functions |
For calibration, causes all objective functions to be used.
TYPE:
|
optimization_algorithms |
For calibration, list of optimization algorithms to run, e.g. "dds". Replaced with full list when do_all_optimization_algorithms = True
TYPE:
|
do_all_optimization_algorithms |
For calibration, causes all optimization algorithms to be used.
TYPE:
|
do_all_forcing_configs |
Causes all forcing configurations to be used, e.g. "short_range", "standard_ana", "medium_range_blend", "extended_ana", "short_range_hawaii", etc.
TYPE:
|
do_coldstart |
Causes coldstart to be ran before forecast.
TYPE:
|
fcst_run_name |
Name of the forecast realization run. Affects a directory name.
TYPE:
|
nprocs |
Number of processors to use
TYPE:
|
gage_id__gage_vintage |
Gage ID and vintage
TYPE:
|
global_domain |
e.g. "CONUS", "Hawaii", "Alaska", "PuertoRico"
TYPE:
|
forcing_provider |
Forcing provider, i.e. "bmi" or "csv"
TYPE:
|
forcing_static_dir |
Forcing static directory
TYPE:
|
noop |
Causes a noop to occur (for confirming that Python packages are importable).
TYPE:
|
gage_id |
Gage ID
TYPE:
|
gage_vintage |
Gage vintage
TYPE:
|
Source code in bin_mounted/configs.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | |
get_calib_permutations
#
get_calib_permutations() -> (
list[
tuple[CalObjective, CalOptimizationAlgo, TestPaths]
]
)
Returns the permutations of objective function and optimization algorithm specified in the config, as well as a TestPaths instance for each. If only_first, then only the first permutation will be returned. Else all permutations will be returned.
Source code in bin_mounted/configs.py
ForcingProviderPaths
#
Bases: BaseModel
Helper class for managing model paths.
Source code in bin_mounted/configs.py
CalibTimeWindows
#
Bases: BaseModel
Calibration time windows defined by a start time and some timedelta offsets.
Source code in bin_mounted/configs.py
make_parallel_config
#
Build and return the ParallelConfig instance.
Source code in bin_mounted/configs.py
parse_gage_id__gage_vintage
#
parse_gage_id__gage_vintage(
gage_id__gage_vintage: tuple[str, str],
) -> tuple[str | None, str | None, list[Exception]]
Parse the provided string and split it into two strings: gage_id and gage_vintage
Source code in bin_mounted/configs.py
parse_fcst_run_name
#
Validate the provided forecast run name, and return a list of errors.
Source code in bin_mounted/configs.py
get_data_paths_for_lstm
#
get_data_paths_for_lstm(
global_domain: str, gage_id: str
) -> tuple[str | None, str | None, list[Exception]]
Build and return two data paths needed for LSTM model, as well as a list of errors encountered during this function. Return None for obs_dir and nwmretro_file if not LSTM.
Source code in bin_mounted/configs.py
find_obs_dir
#
Search the grandparent directory of observed flow csv files to determine the directory that contains one of them. Assert that only one such csv file is found. If multiple are found, then this function needs to be reworked to handle more complex situations, such as multiple vintages of this data existing on disk.
Source code in bin_mounted/configs.py
Regionalization Workflows#
The regionalization workflows run via command-line interface (CLI) Scripts.
Here is the argparse help menu of run_regionalization.py:
run_regionalization is called by run_region.sh
For more information, see the nwm-region-mgr repository.
run_regionalization
#
RTE regionalization workflow runner script.