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
-
assign_coordinates
(coordinates)[source]¶ Assign the given coordinates to the given CF axis types.
- Parameters
coordinates (dict or None) – Mapping from axis types (‘T’, ‘Z’, ‘Y’, ‘X’) 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.
-
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).
-
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’, and ‘x’.
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.
-
property
loc
¶ Wrap DataArray.loc with an indexer to handle units and coordinate types.
-
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
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 or longitude coordinate.
-
property
y
¶ Return the y or latitude coordinate.
-
-
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.testing 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
orxarray.Dataset
– Parsed DataArray (if varname is a string) or Dataset
-
property