pcmdi_metrics.utils.check_monthly_time_axis

pcmdi_metrics.utils.check_monthly_time_axis#

pcmdi_metrics.utils.check_monthly_time_axis(ds, time_key='time')[source]#

Check if the time axis of a dataset follows a correct monthly sequence.

This function verifies if the months in the dataset’s time dimension match the expected sequence of months, starting from the first month in the data and repeating as necessary.

Parameters:
  • ds (xarray.Dataset or xarray.DataArray) – The dataset or data array containing the time dimension to check.

  • time_key (str, optional) – The name of the time dimension in the dataset. Default is “time”.

Returns:

None – The function doesn’t return a value if the check passes.

Raises:
  • KeyError – If the specified time_key is not found in the dataset.

  • AttributeError – If the time dimension doesn’t have a ‘dt’ accessor (i.e., not a datetime type).

  • ValueError – If the time axis does not follow the correct monthly sequence.

Example

>>> from pcmdi_metrics.utils import check_monthly_time_axis
>>> # generate a dummy monthly dataset to test
>>> import xarray as xr
>>> ds = xr.Dataset({"time": xr.cftime_range("2000-01-01", periods=20, freq="M", calendar="gregorian")})
>>> # check axis
>>> check_monthly_time_axis(ds)
# No output if check passes