pcmdi_metrics.utils.regenerate_time_axis

pcmdi_metrics.utils.regenerate_time_axis(ds, start_str=None, periods=None, frequency='monthly')[source]

Regenerate the time axis and bounds for an xarray Dataset.

Parameters:
  • ds (xr.Dataset) – The input dataset with a time dimension.

  • start_str (str, optional) – The start date in ‘YYYY-MM-DD’ format. If None, it’s extracted from the dataset.

  • periods (int, optional) – The number of time periods. If None, it’s inferred from the dataset.

  • frequency ({'monthly', 'daily'}, optional) – The frequency of the time axis. Default is ‘monthly’.

Returns:

xr.Dataset – The dataset with regenerated time axis and bounds.

Raises:

ValueError – If an unsupported frequency is provided.

Notes

This function uses get_time_key, get_time_bounds_key, and extract_date_components functions.

Examples

>>> from pcmdi_metrics.utils import regenerate_time_axis
>>> import xarray as xr
>>> dates = xr.cftime_range('2000-01-01', periods=12, freq='MS')
>>> ds = xr.Dataset({'time': dates, 'data': ('time', range(12))})
>>> regenerated_ds = regenerate_time_axis(ds, frequency='monthly')
>>> print(regenerated_ds.time)