xarray

Provide accessors to enhance interoperability between xarray and MetPy.

MetPy relies upon the CF Conventions. to provide helpful attributes and methods on xarray DataArrays and Dataset for working with coordinate-related metadata. Also included are several attributes and methods for unit operations.

These accessors will be activated with any import of MetPy. Do not use the MetPyDataArrayAccessor or MetPyDatasetAccessor classes directly, instead, utilize the applicable properties and methods via the .metpy attribute on an xarray DataArray or Dataset.

See Also: xarray with MetPy Tutorial.

Accessors

class metpy.xarray.MetPyDataArrayAccessor[source]

Provide custom attributes and methods on xarray DataArrays for MetPy functionality.

This accessor provides several convenient attributes and methods through the metpy attribute on a DataArray. For example, MetPy can identify the coordinate corresponding to a particular axis (given sufficent metadata):

>>> import xarray as xr
>>> temperature = xr.DataArray([[0, 1], [2, 3]], dims=('lat', 'lon'),
...                            coords={'lat': [40, 41], 'lon': [-105, -104]},
...                            attrs={'units': 'degC'})
>>> temperature.metpy.x
<xarray.DataArray 'lon' (lon: 2)>
array([-105, -104])
Coordinates:
  * lon      (lon) int64 -105 -104
Attributes:
    _metpy_axis:  x,longitude
as_timestamp()[source]

Return the data as unix timestamp (for easier time derivatives).

Notes

This function has been deprecated and will be removed in v1.0. For the purposes of time derivatives, use time_deltas instead, which allows sub-second precision.

assign_coordinates(coordinates)[source]

Assign the given coordinates to the given MetPy axis types.

Parameters

coordinates (dict or None) – Mapping from axis types (‘time’, ‘vertical’, ‘y’, ‘latitude’, ‘x’, ‘longitude’) to coordinates of this DataArray. Coordinates can either be specified directly or by their name. If None, clears the _metpy_axis attribute on all coordinates, which will trigger reparsing of all coordinates on next access.

Notes

Prior to v0.12, coordinates was taken as a mapping from CF axis types (‘T’, ‘Z’, ‘Y’, ‘X’) to coordinates. This behavior has been deprecated and will be be removed in v1.0.

property cartopy_crs

Return the coordinate reference system (CRS) as a cartopy object.

property cartopy_globe

Return the globe belonging to the coordinate reference system (CRS).

convert_coordinate_units(coord, units)[source]

Return new DataArray with coordinate converted to different units.

Notes

Backported from MetPy 1.0.

convert_units(units)[source]

Convert the data values to different units in-place.

coordinates(*args)[source]

Return the coordinate variables corresponding to the given axes types.

Parameters

args (str) – Strings describing the axes type(s) to obtain. Currently understood types are ‘time’, ‘vertical’, ‘y’, ‘latitude’, ‘x’, and ‘longitude’.

Notes

This method is designed for use with mutliple coordinates; it returns a generator. To access a single coordinate, use the appropriate attribute on the accessor, or use tuple unpacking.

coordinates_identical(other)[source]

Return whether or not the coordinates of other match this DataArray’s.

property crs

Return the coordinate reference system (CRS) as a CFProjection object.

find_axis_name(axis)[source]

Return the name of the axis corresponding to the given identifier.

Parameters

axis (str or int) – Identifier for an axis. Can be the an axis number (integer), dimension coordinate name (string) or a standard axis type (string).

property latitude

Return the latitude coordinate (if it exists).

property loc

Wrap DataArray.loc with an indexer to handle units and coordinate types.

property longitude

Return the longitude coordinate (if it exists).

sel(indexers=None, method=None, tolerance=None, drop=False, **indexers_kwargs)[source]

Wrap DataArray.sel to handle units and coordinate types.

property time

Return the time coordinate.

property time_deltas

Return the time difference of the data in seconds (to microsecond precision).

property unit_array

Return the data values of this DataArray as a pint.Quantity.

property units

Return the units of this DataArray as a pint.Quantity.

property vertical

Return the vertical coordinate.

property x

Return the x coordinate.

Notes

Prior to version v0.12, this attribute allowed multidimensional coordinates (including longitude when another x coordinate was not available). This behavior has been deprecated and will be removed in v1.0.

property y

Return the y coordinate.

Notes

Prior to version v0.12, this attribute allowed multidimensional coordinates (including latitude when another y coordinate was not available). This behavior has been deprecated and will be removed in v1.0.

class metpy.xarray.MetPyDatasetAccessor[source]

Provide custom attributes and methods on XArray Datasets for MetPy functionality.

This accessor provides parsing of CF metadata and unit-/coordinate-type-aware selection.

>>> import xarray as xr
>>> from metpy.cbook import get_test_data
>>> ds = xr.open_dataset(get_test_data('narr_example.nc', False)).metpy.parse_cf()
>>> print(ds['crs'].item())
Projection: lambert_conformal_conic
property loc

Wrap Dataset.loc with an indexer to handle units and coordinate types.

parse_cf(varname=None, coordinates=None)[source]

Parse Climate and Forecasting (CF) convention metadata.

Parameters
  • varname (str or iterable of str, optional) – Name of the variable(s) to extract from the dataset while parsing for CF metadata. Defaults to all variables.

  • coordinates (dict, optional) – Dictionary mapping CF axis types to coordinates of the variable(s). Only specify if you wish to override MetPy’s automatic parsing of some axis type(s).

Returns

xarray.DataArray or xarray.Dataset – Parsed DataArray (if varname is a string) or Dataset

sel(indexers=None, method=None, tolerance=None, drop=False, **indexers_kwargs)[source]

Wrap Dataset.sel to handle units.