cf_xarray.accessor.CFAccessor#

class cf_xarray.accessor.CFAccessor(obj)[source]#

Common Dataset and DataArray accessor functionality.

_assert_valid_other_comparison(other)[source]#
_drop_missing_variables(variables)[source]#
_generate_repr(rich=False)[source]#
_get_all_cell_measures()[source]#

Get all cell measures defined in the object, adding CF pre-defined measures.

_maybe_to_dataarray(obj=None)[source]#
_maybe_to_dataset(obj=None)[source]#
_process_signature(func, args, kwargs, key_mappers)[source]#

Processes a function’s signature args, kwargs.

  1. Binds *args so that everything is a Mapping from kwarg name to values

  2. Calls _rewrite_values to rewrite any special CF names to normal xarray names. This uses key_mappers.

  3. Unpacks arguments if necessary before returning them.

_rewrite_values(kwargs, key_mappers, var_kws)[source]#

Rewrites the values in a Mapping from kwarg to value.

Parameters:
kwargsMapping

Mapping from kwarg name to value

key_mappersMapping

Mapping from kwarg name to a Mapper function that will convert a given CF “special” name to an xarray name.

var_kwsList[str]

List of variable kwargs that need special treatment. e.g. **indexers_kwargs in isel

Returns:
dict of kwargs with fully rewritten values.
add_canonical_attributes(override=False, skip=None, verbose=False, source=None)[source]#

Add canonical CF attributes to variables with standard names.

Attributes are parsed from the official CF standard name table [1]. This function adds an entry to the “history” attribute.

Parameters:
overridebool

Override existing attributes.

skipstr, iterable, optional

Attribute(s) to skip: {"units", "grib", "amip", "description"}.

verbosebool

Print added attributes to screen.

sourceoptional

Path of cf-standard-name-table.xml or file object containing XML data. If None, use the latest CF standard name table (requires pooch).

Returns:
DataArray or Dataset

With attributes added.

Notes

The "units" attribute is never added to datetime-like variables.

References

property axes: dict[str, list[Hashable]]#

Mapping valid Axis standard names for .cf[] to variable names.

This is useful for checking whether a key is valid for indexing, i.e. that the attributes necessary to allow indexing by that key exist. However, it will only return the Axis names ("X", "Y", "Z", "T") present in .coords, not in .data_vars.

Returns:
dict

Dictionary with keys that can be used with __getitem__ or as .cf[key]. Keys will be the appropriate subset of (“X”, “Y”, “Z”, “T”). Values are lists of variable names that match that particular key.

property cell_measures: dict[str, list[Hashable]]#

Mapping valid cell measure standard names for .cf[] to variable names.

This is useful for checking whether a key is valid for indexing, i.e. that the attributes necessary to allow indexing by that key exist.

Returns:
dict

Dictionary of valid cell measure names that can be used with __getitem__ or .cf[key].

property cf_roles: dict[str, list[Hashable]]#

Mapping cf_role names to variable names.

Returns:
dict

Dictionary mapping cf_role names to variable names.

References

Please refer to the CF conventions document : http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#coordinates-metadata

Examples

>>> import cf_xarray
>>> from cf_xarray.datasets import dsg
>>> dsg.cf.cf_roles
{'profile_id': ['profile'], 'trajectory_id': ['trajectory']}
property coordinates: dict[str, list[Hashable]]#

Mapping valid Coordinate standard names for .cf[] to variable names.

This is useful for checking whether a key is valid for indexing, i.e. that the attributes necessary to allow indexing by that key exist. However, it will only return the Coordinate names ("latitude", "longitude", "vertical", "time") present in .coords, not in .data_vars.

Returns:
dict

Dictionary of valid Coordinate names that can be used with __getitem__ or .cf[key]. Keys will be the appropriate subset of ("latitude", "longitude", "vertical", "time"). Values are lists of variable names that match that particular key.

curvefit(coords, func, reduce_dims=None, skipna=True, p0=None, bounds=None, param_names=None, kwargs=None)[source]#
describe()[source]#

Print a string repr to screen.

differentiate(coord, *xr_args, positive_upward=False, **xr_kwargs)[source]#

Differentiate an xarray object.

Parameters:
coordHashable

The coordinate used to compute the gradient.

positive_upwardoptional, bool

Change sign of the derivative based on the "positive" attribute of coord so that positive values indicate increasing upward. If positive=="down", then multiplied by -1.

Returns:
DataArray or Dataset

Differentiated object.

See also

DataArray.cf.differentiate
Dataset.cf.differentiate
xarray.DataArray.differentiate

Underlying xarray function.

xarray.Dataset.differentiate

Underlying xarray function.

Notes

xr_args, xr_kwargs are passed directly to the underlying xarray function.

drop(*args, **kwargs)[source]#
get_associated_variable_names(name, skip_bounds=False, error=True)[source]#

Mapping a list of variable names referred to in the appropriate attribute.

Includes:
  1. “ancillary_variables”

  2. “bounds”

  3. “cell_measures”

  4. “coordinates”

  5. “grid_mapping”

  6. “grid”

Parameters:
nameHashable
skip_boundsbool, optional
errorbool, optional

Raise or ignore errors.

Returns:
namesdict

Dictionary with keys “ancillary_variables”, “cell_measures”, “coordinates”, “bounds”, “grid_mapping”, “grid”.

guess_coord_axis(verbose=False)[source]#

Automagically guesses X, Y, Z, T, latitude, longitude, and adds appropriate attributes.

Uses regexes from Metpy and inspired by Iris function of same name.

Existing attributes will not be modified.

Parameters:
verbosebool

Print extra info to screen.

Returns:
DataArray or Dataset

With appropriate attributes added.

isin(test_elements)[source]#

Test each value in the array for whether it is in test_elements.

Parameters:
test_elementsarray_like, 1D

The values against which to test each value of element. These must be in “flag_meanings” attribute, and are mapped to the corresponding value in “flag_values” before passing that on to DataArray.isin.

Returns:
DataArray

Has the same type and shape as this object, but with a bool dtype.

keys()[source]#

Utility function that returns valid keys for .cf[].

This is useful for checking whether a key is valid for indexing, i.e. that the attributes necessary to allow indexing by that key exist.

Returns:
set

Set of valid key names that can be used with __getitem__ or .cf[key].

property plot#

Extended version of regular .plot.

See also

Dataset.plot
rename_like(other, skip=None)[source]#

Rename variables in object to match names of like-variables in other.

“Likeness” is determined by variables sharing similar attributes. If cf_xarray can identify a single “longitude” variable in both this object and other, that variable will be renamed to match the “longitude” variable in other.

For now, this function only matches ("latitude", "longitude", "vertical", "time")

Parameters:
otherDataArray, Dataset

Variables will be renamed to match variable names in this xarray object.

skipstr, Iterable[str], optional

Limit the renaming excluding (“axes”, “bounds”, cell_measures”, “coordinates”, “standard_names”) or a subset thereof.

Returns:
DataArray or Dataset

With renamed variables.

stack(dimensions=None, **dimensions_kwargs)[source]#
property standard_names: dict[str, list[Hashable]]#

Mapping standard names to variable names.

Returns:
dict

Dictionary mapping standard names to variable names.