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 toxcdat.axis.VAR_NAME_MAPfor accepted names.data (
list[int | float] | np.ndarray) – 1-D axis data consisting of integers or floats.bounds (
list[list[int | float]] | np.ndarray | None) – 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 ofdata.generate_bounds (
bool) – Generate bounds for the axis ifboundsis None, by default True.attrs (
dict[str,str] | None) – Custom attributes to be added to the generated xr.DataArray axis, by default None.User provided
attrswill 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,xr.DataArray | None]– A DataArray containing the axis data and optional bounds.- Raises:
ValueError – If
nameis 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"}, >>> )