xcdat.create_axis#

xcdat.create_axis(name, data, bounds=None, generate_bounds=True, attrs=None)[source]#

Creates an axis and optional bounds.

Parameters:
  • name (str) – The CF standard name for the axis (e.g., “longitude”, “latitude”, “height”). xCDAT also accepts additional names such as “lon”, “lat”, and “lev”. Refer to xcdat.axis.VAR_NAME_MAP for accepted names.

  • data (Union[List[Union[int, float]], np.ndarray]) – 1-D axis data consisting of integers or floats.

  • bounds (Optional[Union[List[List[Union[int, float]]], np.ndarray]]) – 2-D axis bounds data consisting of integers or floats, defaults to None. Must have a shape of n x 2, where n is the length of data.

  • generate_bounds (Optiona[bool]) – Generate bounds for the axis if bounds is None, by default True.

  • attrs (Optional[Dict[str, str]]) – Custom attributes to be added to the generated xr.DataArray axis, by default None.

    User provided attrs will be merged with a set of default attributes. Default attributes (“axis”, “coordinate”, “bnds”) cannot be overwritten. The default “units” attribute is the only default that can be overwritten.

Returns:

Tuple[xr.DataArray, Optional[xr.DataArray]] – A DataArray containing the axis data and optional bounds.

Raises:

ValueError – If name is not valid CF axis name.

Examples

Create axis and generate bounds (by default):

>>> lat, bnds = create_axis("lat", np.array([-45, 0, 45]))

Create axis and bounds from list of floats:

>>> lat, bnds = create_axis("lat", [-45, 0, 45], bounds=[[-67.5, -22.5], [-22.5, 22.5], [22.5, 67.5]])

Create axis and disable generating bounds:

>>> lat, _ = create_axis("lat", np.array([-45, 0, 45]), generate_bounds=False)

Provide additional attributes and overwrite units:

>>> lat, _ = create_axis(
>>>     "lat",
>>>     np.array([-45, 0, 45]),
>>>     attrs={"generated": str(datetime.date.today()), "units": "degrees_south"},
>>> )