xcdat.bounds.BoundsAccessor
xcdat.bounds.BoundsAccessor#
- class xcdat.bounds.BoundsAccessor(dataset)[source]#
An accessor class that provides bounds attributes and methods on xarray Datasets through the
.boundsattribute.Examples
Import BoundsAccessor class:
>>> import xcdat # or from xcdat import bounds
Use BoundsAccessor class:
>>> ds = xcdat.open_dataset("/path/to/file") >>> >>> ds.bounds.<attribute> >>> ds.bounds.<method> >>> ds.bounds.<property>
- Parameters
dataset (xr.Dataset) – A Dataset object.
Examples
Import:
>>> from xcdat import bounds
Return dictionary of axis and coordinate keys mapped to bounds:
>>> ds.bounds.map
Return list of keys for bounds data variables:
>>> ds.bounds.keys
Add missing coordinate bounds for supported axes in the Dataset:
>>> ds = ds.bounds.add_missing_bounds()
Get coordinate bounds if they exist:
>>> lat_bounds = ds.bounds.get_bounds("Y") >>> lon_bounds = ds.bounds.get_bounds("X") >>> time_bounds = ds.bounds.get_bounds("T")
Add coordinate bounds for a specific axis if they don’t exist:
>>> ds = ds.bounds.add_bounds("Y")
Methods
__init__(dataset)add_bounds(axis[, width])Add bounds for an axis using its coordinate points.
add_missing_bounds([width])Adds missing coordinate bounds for supported axes in the Dataset.
get_bounds(axis)Get bounds for axis coordinates if both exist.
Attributes
Returns a list of keys for the bounds data variables in the Dataset.
Returns a map of axis and coordinates keys to their bounds.
- _dataset#
- property map#
Returns a map of axis and coordinates keys to their bounds.
The dictionary provides all valid CF compliant keys for axis and coordinates. For example, latitude will includes keys for “lat”, “latitude”, and “Y”.
- Returns
Dict[str, Optional[xr.DataArray]] – Dictionary mapping axis and coordinate keys to their bounds.
- property keys#
Returns a list of keys for the bounds data variables in the Dataset.
- Returns
List[str] – A list of sorted bounds data variable keys.
- add_missing_bounds(width=0.5)[source]#
Adds missing coordinate bounds for supported axes in the Dataset.
This function loops through the Dataset’s axes and attempts to adds bounds to its coordinates if they don’t exist. The coordinates must meet the following criteria in order to add bounds:
The axis for the coordinates are “X”, “Y”, “T”, or “Z”
Coordinates are a single dimension, not multidimensional
Coordinates are a length > 1 (not singleton)
Bounds must not already exist. * Determined by attempting to map the coordinate variable’s “bounds” attr (if set) to the bounds data variable of the same key.
- Parameters
width (float, optional) – Width of the bounds relative to the position of the nearest points, by default 0.5.
- Returns
xr.Dataset
- get_bounds(axis)[source]#
Get bounds for axis coordinates if both exist.
- Parameters
axis (CFAxisName) – The CF-compliant axis name (“X”, “Y”, “T”, “Z”).
- Returns
xr.DataArray – The coordinate bounds.
- Raises
ValueError – If an incorrect
axisargument is passed.KeyError – If the coordinate variable was not found for the
axis.KeyError – If the coordinate bounds were not found for the
axis.
- add_bounds(axis, width=0.5)[source]#
Add bounds for an axis using its coordinate points.
The coordinates must meet the following criteria in order to add bounds:
The axis for the coordinates are “X”, “Y”, “T”, or “Z”
Coordinates are a single dimension, not multidimensional
Coordinates are a length > 1 (not singleton)
Bounds must not already exist. * Determined by attempting to map the coordinate variable’s “bounds” attr (if set) to the bounds data variable of the same key.
- Parameters
axis (CFAxisName) – The CF-compliant axis name (“X”, “Y”, “T”, “Z”).
width (float, optional) – Width of the bounds relative to the position of the nearest points, by default 0.5.
- Returns
xr.Dataset – The dataset with bounds added.
- Raises
ValueError – If bounds already exist. They must be dropped first.
- _add_bounds(axis, width=0.5)[source]#
Add bounds for an axis using its coordinate points.
- Parameters
axis (CFAxisName) – The CF-compliant axis name (“X”, “Y”, “T”, “Z”).
width (float, optional) – Width of the bounds relative to the position of the nearest points, by default 0.5.
- Returns
xr.Dataset – The dataset with new coordinate bounds for an axis.
- Raises
ValueError – If coords dimensions does not equal 1.
Notes
Based on 1
iris.coords._guess_boundsand 2cf_xarray.accessor.add_boundsReferences