pcmdi_metrics.utils.extract_date_components

pcmdi_metrics.utils.extract_date_components(ds, index=0)[source]

Extract year, month, and day from a dataset’s time dimension.

Parameters:
  • ds (xarray.Dataset) – The dataset containing a time dimension.

  • index (int, optional) – The index of the time value to extract. Default is 0 (first time value).

Returns:

tuple of int – A tuple containing (year, month, day).

Raises:
  • KeyError – If no time dimension is found in the dataset.

  • IndexError – If the specified index is out of bounds for the time dimension.

Notes

This function assumes that the dataset has a time dimension and that the time values are datetime-like objects with year, month, and day attributes.

Examples

>>> from pcmdi_metrics.utils import extract_date_components
>>> import xarray as xr
>>> dates = xr.cftime_range('2000-01-01', periods=365)
>>> ds = xr.Dataset({'time': dates, 'data': ('time', range(365))})
>>> extract_date_components(ds)
(2000, 1, 1)
>>> extract_date_components(ds, index=100)
(2000, 4, 10)

See also

get_time_key

Function to find the time dimension key in the dataset.