register_transformation¶
- register_transformation(src, dest, /, *, assign_name=True)[source]¶
Register a function that transforms one variable to another, for use with
ClimoDataArrayAccessor.to_variable
. Transformations should depend only on the initial variable and (optionally) the coordinates.- Parameters
src (str) – The source variable name.
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
In this example, we define a simple derivation to convert pressure to the log-pressure height.
>>> import climopy as climo >>> from climopy import const >>> @climo.register_transformation('p', 'z_logp') ... def meridional_coordinate(da): ... return (const.H * np.log(const.p0 / da)).climo.to_units('km') >>> da = xr.DataArray([1000, 800, 600, 400], name='p', attrs={'units': 'hPa'}) >>> da.climo.to_variable('z_logp') <xarray.DataArray 'z_logp' (dim_0: 4)> array([0. , 1.56200486, 3.57577937, 6.41403512]) Dimensions without coordinates: dim_0 Attributes: units: kilometer