register_derivation¶
- register_derivation(dest, /, *, assign_name=True)[source]¶
Register a function that derives one variable from one or more others, for use with
ClimoDatasetAccessor.get
.- Parameters
dest (str, tuple, or re.Pattern) – The destination variable name, a tuple of valid destination names, or an
re.compile
’d pattern matching a set of valid destination names. In the latter two cases, the function must accept aname
keyword argument. This is useful if you want to register a single function capable of deriving multiple related variables (e.g., registering the regexr'\Ad.*dy\Z'
to return the meridional gradient of an arbitrary variable).assign_name (bool, optional) – Whether to assign the user-input string as the output
xarray.DataArray.name
. Default isTrue
.
Examples
>>> import climopy as climo >>> from climopy import const >>> @climo.register_derivation('pot_temp') ... def potential_temp(self): ... return self.temp * (const.p0 / self.pres).climo.to_units('') ** (2 / 7) >>> ds = xr.Dataset( ... { ... 'temp': ((), 273, {'units': 'K'}), ... 'pres': ((), 100, {'units': 'hPa'}) ... } ... ) >>> ds.climo['pot_temp'] # or ds.climo.pt <xarray.DataArray 'pot_temp' ()> <Quantity(527.08048, 'kelvin')>