rcutils
ROMS Communication Module utility functions
This module provides a number of small helper functions used by the primary romscom functions.
bool2str(x)
Formats input boolean as string 'T' or 'F'
Returns:
Type | Description |
---|---|
string
|
'T' or 'F', corresponding to True or False, respectively |
Source code in src/romscom/rcutils.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
checkforstring(x, prefix='')
Check that all dictionary entries have been stringified, and print the keys cooresponding to non-stringified values (primary diagnostic)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict
|
ROMS parameter dictionary |
required |
prefix
|
string
|
prefix applied to printout of non-stringified value's key |
''
|
Source code in src/romscom/rcutils.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
consecutive(data, stepsize=0)
Groups values in list based on difference between consecutive elements
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
list
|
list of numeric values |
required |
stepsize
|
numeric
|
difference between consecutive elements to use for grouping. Default = 0, i.e. identical values grouped |
0
|
Returns:
Type | Description |
---|---|
list of lists
|
values grouped. |
Examples:
>>> consecutive([1, 1, 1, 2, 2, 4, 5])
[[1, 1, 1], [2, 2], [4], [5]]
>>> consecutive([1, 1, 1, 2, 2, 4, 5], 1)
[[1], [1], [1, 2], [2], [4, 5]]
Source code in src/romscom/rcutils.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
fieldsaretime(d)
True if all time-related fields are in datetime/timedelta format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
parameter dictionary |
required |
Returns:
Type | Description |
---|---|
boolean
|
True if all time-related fields are in datetime/timedelta format, False if all are numeric. |
Raises:
Type | Description |
---|---|
Exception
|
if a mix of numeric and datetime/timedelta values are found |
Source code in src/romscom/rcutils.py
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 |
|
findclosesttime(folder, targetdate, pattern='*his*.nc')
Search folder for history file with time closest to the target date
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder
|
string
|
pathname to folder holding output of a BESTNPZ ROMS simulation, with history files matching the pattern provided. Alternatively, can be a list of history filenames (useful if you want to include a smaller subset from within a folder that can't be isolated via pattern) |
required |
targetdate
|
datetime
|
target date |
required |
pattern
|
string
|
pattern-matching string appended to folder name as search string to identify history files Default = 'his.nc' |
'*his*.nc'
|
Returns:
Type | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dict
|
with the following keys/values:
|
Source code in src/romscom/rcutils.py
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 |
|
flatten(A)
Recursively flatten a list of lists (of lists of lists...)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A
|
list
|
list that may contains nested lists |
required |
Returns:
Type | Description |
---|---|
list
|
contents of A where all sub-lists have been flattened to a single level |
Source code in src/romscom/rcutils.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
float2str(x)
Formats input float as Fortran-style double-precision string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float
|
input value |
required |
Returns:
Type | Description |
---|---|
string
|
x in Fortran double-precision syntax (e.g., "1.0d0") |
Source code in src/romscom/rcutils.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
formatkeyvalue(kw, val, singular)
Format dictionary entries as ROMS parameter assignments
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kw
|
string
|
key |
required |
val
|
string
|
stringified value |
required |
singular
|
list of strings
|
list of keys that should be treated as unvarying across ROMS grids (i.e. uses a = assignment vs ==) |
required |
Returns:
Type | Description |
---|---|
string
|
key/value as a line of text, e.g. 'key == value' |
Source code in src/romscom/rcutils.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
inputfilesexist(ocean)
Check that all ROMS input files exist. If a filename starts with the string "placeholder", it is ignored in this check (this allows you to keep unused parameters in the YAML files, but clearly indicates that these files will not be required)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ocean
|
dict
|
ROMS parameter dictionary |
required |
Returns:
Type | Description |
---|---|
boolean
|
True if all files exist (or are marked as placeholders), False otherwise |
Source code in src/romscom/rcutils.py
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 |
|
list2str(tmp, consecstep=-99999)
Convert list of bools, floats, or integers to string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tmp
|
list
|
a list of either all bools, all floats, all integers, or all strings |
required |
consecstep
|
numeric
|
step size to use for compression. Default = -99999, i.e. no compression; use 0 for ROMS-style compression (i.e. T T T F -> 3*T F). Not applicable to string lists. |
-99999
|
Returns:
Type | Description |
---|---|
string
|
ROMS-appropriate string version of list values |
Source code in src/romscom/rcutils.py
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 |
|
multifile2str(tmp)
Convert a multifile list of filenames (with possible nesting) to string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tmp
|
string or list of strings
|
names of files and multi-files |
required |
Returns:
Type | Description |
---|---|
string
|
ROMS multi-file formatted as string |
Source code in src/romscom/rcutils.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
ordered_load(stream, Loader=yaml.SafeLoader, object_pairs_hook=OrderedDict)
This function was pulled from https://stackoverflow.com/questions/5121931/. It makes sure YAML dictionary loads preserve order, even in older versions of python.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stream
|
input stream |
required | |
loader
|
loader (default: yaml.SafeLoader) |
required |
usage example: ordered_load(stream, yaml.SafeLoader)
Returns:
Type | Description |
---|---|
OrderedDict
|
dictionary |
Source code in src/romscom/rcutils.py
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 |
|
parseromslog(fname)
Parse ROMS standard output log for some details about the success (or not) of a ROMS simulation
Args fname (string): name of file with ROMS standard output
Returns:
Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dict
|
dictionary with the following fields:
|
Source code in src/romscom/rcutils.py
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 |
|
parserst(filebase)
Parse restart counters from ROMS simulation restart files
This function finds the name of, and parses the simulation counter, from a series of ROMS restart files. It assumes that those files were using the naming scheme from runtodate, i.e. filebase_XX_rst.nc where XX is the counter for number of restarts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filebase
|
string
|
base name for restart files (can include full path) |
required |
Returns:
Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
dict
|
with the following keys:
|
Source code in src/romscom/rcutils.py
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 |
|
timefieldlist(d)
Get list of time-related dictionary keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
ROMS parameter dictionary |
required |
Returns:
Type | Description |
---|---|
list of strings
|
keys in d that are time-related |
Source code in src/romscom/rcutils.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|