MCPcopy Create free account
hub / github.com/NCAR/wrf-python / either

Class either

src/wrf/util.py:456–508  ·  view source on GitHub ↗

A callable class that determines which variable is present in the file. This is used in situations where the same variable type has different names depending on the type of file used. For example, in a WRF output file, 'P' is used for pressure, whereas in a met_em file, pressure is

Source from the content-addressed store, hash-verified

454
455# Helper utilities for metadata
456class either(object):
457 """A callable class that determines which variable is present in the
458 file.
459
460 This is used in situations where the same variable type has different names
461 depending on the type of file used. For example, in a WRF output file,
462 'P' is used for pressure, whereas in a met_em file, pressure is named
463 'PRES'.
464
465 Methods:
466
467 __call__(wrfin): Return the variable that is present in the file.
468
469 Args:
470
471 wrfin (:class:`netCDF4.Dataset`, :class:`Nio.NioFile`, or an \
472 iterable): WRF-ARW NetCDF
473 data as a :class:`netCDF4.Dataset`, :class:`Nio.NioFile`
474 or an iterable sequence of the aforementioned types.
475
476 Returns:
477
478 :obj:`str`: The variable name that is present in the file.
479
480 Attributes:
481
482 varnames (sequence): A sequence of possible variable names.
483
484 """
485 def __init__(self, *varnames):
486 """Initialize an :class:`either` object.
487
488 Args:
489
490 *varnames (sequence): A sequence of possible variable names.
491
492 """
493 self.varnames = varnames
494
495 def __call__(self, wrfin):
496 if is_multi_file(wrfin):
497 if not is_mapping(wrfin):
498 wrfin = next(iter(wrfin))
499 else:
500 entry = wrfin[next(iter(viewkeys(wrfin)))]
501 return self(entry)
502
503 for varname in self.varnames:
504 if varname in wrfin.variables:
505 return varname
506
507 raise ValueError("{} are not valid variable names".format(
508 self.varnames))
509
510
511# This should look like:

Callers 15

_get_geohtFunction · 0.85
get_pressureFunction · 0.85
get_terrainFunction · 0.85
_lat_varnameFunction · 0.85
_lon_varnameFunction · 0.85
func_wrapperFunction · 0.85
get_srhFunction · 0.85
get_uhFunction · 0.85
get_u_destagFunction · 0.85
get_v_destagFunction · 0.85
get_destag_wspd_wdirFunction · 0.85
get_destag_wspd_wdir10Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected