Skip to content

input4mips_validation.validation.frequency#

input4mips_validation.validation.frequency #

Validation of the frequency attribute

validate_frequency(frequency, ds, time_dimension='time', frequency_metadata_keys=FrequencyMetadataKeys(), bounds_info=BoundsInfo()) #

Validate the frequency value

Parameters:

Name Type Description Default
frequency str

Frequency value to validate.

required
ds Dataset

Dataset to which the frequency metadata applies.

required
time_dimension str

The name of the time dimension, if it appears in ds.

'time'
frequency_metadata_keys FrequencyMetadataKeys

Metadata definitions for frequency information

FrequencyMetadataKeys()
bounds_info BoundsInfo

Metadata definitions for bounds handling

BoundsInfo()

Raises:

Type Description
ValueError

frequency's value is incorrect

Source code in src/input4mips_validation/validation/frequency.py
def validate_frequency(
    frequency: str,
    ds: xr.Dataset,
    time_dimension: str = "time",
    frequency_metadata_keys: FrequencyMetadataKeys = FrequencyMetadataKeys(),
    bounds_info: BoundsInfo = BoundsInfo(),
) -> None:
    """
    Validate the frequency value

    Parameters
    ----------
    frequency
        Frequency value to validate.

    ds
        Dataset to which the frequency metadata applies.

    time_dimension
        The name of the time dimension, if it appears in `ds`.

    frequency_metadata_keys
        Metadata definitions for frequency information

    bounds_info
        Metadata definitions for bounds handling

    Raises
    ------
    ValueError
        `frequency`'s value is incorrect
    """
    try:
        expected_frequency = infer_frequency(
            ds=ds,
            no_time_axis_frequency=frequency_metadata_keys.no_time_axis_frequency,
            time_dimension=time_dimension,
            time_bounds=bounds_info.time_bounds,
            bounds_dim=bounds_info.bounds_dim,
            bounds_dim_lower_val=bounds_info.bounds_dim_lower_val,
            bounds_dim_upper_val=bounds_info.bounds_dim_upper_val,
        )
    except NotImplementedError as exc:
        msg = f"The expected `frequency` attribute value could not be obtained. {exc}"
        raise NotImplementedError(msg) from exc

    if frequency != expected_frequency:
        msg = (
            "Given the time axis in the data, "
            f"the frequency attribute must be {expected_frequency!r}. "
            f"Received {frequency=!r}."
            f"{bounds_info=}. {ds=}"
        )
        raise ValueError(msg)