romscom
ROMS Communication Module primary functions
This module holds the primary functions expected to be called by end-users in a typical romscom workflow, which includes importing ROMS parameters from .yaml files, manipulating values as needed for an experiment, and then exporting to ROMS .in files:
readparamfile(filename,...)
reads a parameter YAML file into an ordered dictionarystringifyvalues(d,...)
reformats the values in a parameter dictionary to ROMS syntax stringsdict2standardin(d,...)
converts a parameter dictionary to standard input text, and optionally writes to fileconverttimes(d,direction)
converts time-related parameter fields between ROMS format and datetimes/timedeltas.
Additional functions focus on a specific ROMS workflow; this includes a standard folder setup and file-naming conventions that facilitate robust automation of simulations.
runtodate(ocean,simdir,simname,enddate,...)
sets up I/O and runs ROMS simulation through indicated date, with options to restart and work past blowupssimfolders(simdir)
generates folder path names for, and optionally creates, the 3 I/O folders used by runtodatesetoutfilenames(ocean,base,...)
resets the values of output file name parameters in a ROMS parameter dictionary to use a systematic naming scheme
converttimes(d, direction)
Converts time-related parameter fields between ROMS format and datetimes/timedeltas. The conversions apply to the following keys in the input dictionary d:
Key | 'ROMS' format | 'time' format |
---|---|---|
DSTART | float, days since initialization | datetime, starting date+time |
TIME_REF | float, reference time (yyyymmdd.f) | datetime, reference date+time |
NTIMES* | integer, number of time steps in simulation | timedelta, duration of simulation |
N### | integer, number of time steps | timedelta, length of time |
NDEF### | integer, number of time steps | timedelta, length of time |
DT: | integer, number of seconds | timedelta, length of time |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
ROMS parameter dictionary |
required |
direction
|
string
|
'ROMS' to convert to ROMS standard input units or 'time' to convert to datetime/timedelta values |
required |
Source code in src/romscom/romscom.py
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 645 646 647 648 649 650 651 652 653 654 655 656 |
|
dict2standardin(d, compress=False, file=None)
Converts a parameter dictionary to standard input text, and optionally writes to file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
parameter dictionary compress (logical, optional): True to compress repreated values (e.g., T T T -> 3*T), False (default) to leave as is. |
required |
file
|
string or None
|
name of output file. If None (default), text is returned; otherwise, text will be printed to file indicated |
None
|
Returns:
Type | Description |
---|---|
string
|
standard input text (only if output file not provided) |
Source code in src/romscom/romscom.py
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 |
|
readparamfile(filename, tconvert=False)
Reads parameter YAML file into an ordered dictionary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
string
|
name of parameter file |
required |
tconvert
|
logical
|
True to convert time-related fields to datetimes and timedeltas, False (default) to keep in native ROMS format. |
False
|
Returns:
Type | Description |
---|---|
OrderedDict
|
ROMS parameter dictionary |
Source code in src/romscom/romscom.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
runtodate(ocean, simdir, simname, enddate, dtslow=None, addcounter='most', compress=False, romscmd=['mpirun', 'romsM'], dryrunflag=True, permissions=493, count=1, runpastblowup=True)
Sets up I/O and runs ROMS simulation through indicated date
This function provides a wrapper to set up a ROMS simulation and run through the desired date, allowing for robust restarts when necessary. It organizes ROMS I/O under a 3-folder system (under the user-specified simdir folder):
/In: holds all stardard input (.in) files for the simulation /Log: holds standard output and standard error files (redirected to file from the call to ROMS) as well as step-size tracking to support this function's runpastblowup option /Out: holds all netCDF output files from the ROMS simulation
Before calling the ROMS executable, this function looks for an
appropriately-named restart file under the
This procedure allows a simulation to be restarted using the same call to runtodate regardless of whether it has been partially completed or not; this can be useful when running simulations on computer clusters where jobs may be cancelled and resubmitted for various queue management reasons, or to extend existing simulations with new forcing.
This function also provides the option to work through ROMS blowups. These occur when physical conditions lead to numeric instabilities. Blowups can sometimes be mitigated by reducing the model time step. When the runpastblowup option is True and runtodate encounters a blowup, it will adjust the DT parameter to the user-provided slow time step, restart the simulation from the last history file, and run for 30 days. It will then return to the original time step and resume. Note that this time step reduction will only be attempted once; if the model still blows up, the simulation will exit and the user will need to troubleshoot the situation.
Each time the model is restarted, output file counters are incremented as specified by the addcounter option. This preserves output that would otherwise be overwritten on restart with the same simulation name. By default, the counter is only added to file types that modern ROMS does not check for on restart.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ocean
|
dict
|
ROMS parameter dictionary for standard input simdir |
required |
(string)
|
folder where I/O subfolders are found/created simname |
required | |
(string)
|
base name for simulation, used as prefix for auto-generated input, standard output and error files, and .nc output. |
required | |
enddate
|
datetime
|
datetime, simulation end date dtslow (timedelta, |
required |
optional)
|
length of time step used during slow-stepping (blowup) periods. If None (default), this will be set to half the primary (i.e., ocean['DT']) time step |
required | |
addcounter
|
string or list of strings
|
list of output filename prefixes corresponding to those where a counter index should be added to the name, or one of the following special strings
|
'most'
|
compress
|
logical
|
True to compress repreated values (e.g., T T T -> 3*T), False (default) to leave as is. |
False
|
romscmd
|
list of strings
|
components of command used to call the ROMS executable (see subprocess.run). Default is ["mpirun","romsM"], which would be appropriate to call a compiled-for-parallel ROMS executable via MPI. |
['mpirun', 'romsM']
|
dryrunflag
|
logical
|
True to perform a dry run, where I/O is prepped but the ROMS executable is not called, False to call ROMS. Defaults to True |
True
|
permissions
|
octal
|
folder permissions applied to I/O subfolders if they don't already exist (see os.chmod). Default is 0o755 |
493
|
count
|
int
|
Starting index for file counter. runpastblowup |
1
|
(logical,optional)
|
True to attempt time step reduction if the model blows up, false otherwise |
required |
Returns:
Type | Description |
---|---|
string
|
indicator of ROMS simulation results, will be one of:
|
Source code in src/romscom/romscom.py
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 278 279 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 371 372 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 |
|
setoutfilenames(ocean, base, cnt=1, outtype='all', addcounter='none')
Resets the values of output file name parameters in a ROMS parameter dictionary
This function systematically resets the output file name values using the
pattern {base}_{prefix}.nc
, where prefix is a lowercase version of the 3- or
4-letter prefix of the various XXXNAME parameters, e.g. ocean['AVGNAME'] = 'base_avg.nc'
.
Where specified, a 2-digit counter may also be added, e.g. ocean['STANAME'] = 'base_01_sta.nc'
.
Note that these extra counters apply an additional increment beyond those that ROMS
itself applies to any file type with an NDEFXXX option; for example, history files are
typically incremented internally, so ocean['HISNAME'] = 'base_his.nc'
with ocean['NDEFHIS'] > 0
and addcounter="none"
will still produce files named <simdir>/Out/base_his_00001.nc
,
<simdir>/Out/base_his_00002.nc
, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ocean
|
dict
|
parameter dictionary |
required |
base
|
string
|
base name for output files (including path when applicable) |
required |
cnt
|
int
|
counter to be added to filenames, if requested. Default = 1 |
1
|
outtype
|
string or list of strings
|
list of output filename prefixes corresponding to those to be modified (e.g., ['AVG', 'HIS']), or one of the following special strings: all: modify all output types (default) |
'all'
|
addcounter
|
string or list of strings
|
list of output filename prefixes corresponding to those where a counter index should be added to the name, or one of the following special strings:
|
'none'
|
Source code in src/romscom/romscom.py
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 |
|
simfolders(simdir, create=False, permissions=493)
Generate path names for, and if requested, create folders for the the 3 I/O
folders used by runtodate (
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simdir
|
string
|
path to location where folders will be located. |
required |
create
|
logical
|
True to create the folders if they do not exist. Defaults to False |
False
|
permissions
|
octal
|
folder permissions applied to I/O subfolders if they don't already exist (see os.chmod). Default is 0o755 |
493
|
Returns:
Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dict
|
with the following fields:
|
Source code in src/romscom/romscom.py
493 494 495 496 497 498 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 |
|
stringifyvalues(d, compress=False)
Formats all dictionary values to ROMS standard input syntax
This function converts values to the Fortran-ish syntax used in ROMS standard input files. Floats are converted to the double-precision format of Fortran read/write statements, booleans are converted to T/F, integers are converted straight to strings, and lists are converted to space- delimited strings of the above (compressed using * for repeated values where applicable). Values corresponding to a few special KEYWORDS (e.g., the 'POS' station table, multi-file parameters, 'LBC' boundary conditions) receive the appropriate formatting
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
ROMS parameter dictionary compress (logical, optional): True to compress repeated values (e.g., T T T -> 3*T), False (default) to leave as is. |
required |
Returns:
Type | Description |
---|---|
dict
|
deep copy of d with all values replaced by ROMS-formatted strings |
Source code in src/romscom/romscom.py
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 |
|