xarray.register_dataset_accessor

xarray.register_dataset_accessor(name)

Register a custom property on xarray.Dataset objects.

Parameters
namestr

Name under which the accessor should be registered. A warning is issued if this name conflicts with a preexisting attribute.

Examples

In your library code:

import xarray as xr

@xr.register_dataset_accessor('geo')
class GeoAccessor(object):
    def __init__(self, xarray_obj):
        self._obj = xarray_obj

    @property
    def center(self):
        # return the geographic center point of this dataset
        lon = self._obj.latitude
        lat = self._obj.longitude
        return (float(lon.mean()), float(lat.mean()))

    def plot(self):
        # plot this array's data on a map, e.g., using Cartopy
        pass

Back in an interactive IPython session:

>>> ds = xarray.Dataset({'longitude': np.linspace(0, 10),
...                      'latitude': np.linspace(0, 20)})
>>> ds.geo.center
(5.0, 10.0)
>>> ds.geo.plot()
# plots data on a map