icclim._core.climate_variable#

Contain the ClimateVariable class and its related functions.

A climate variable is a structure that contains all the pre-processed input varaible to compute a climate index. A climate index may require one or more climate variables to be computed.

Module Contents#

class icclim._core.climate_variable.ClimateVariable[source]#

ClimateVariable is a dataclass that represents a climate variable used to compute a climate index.

It groups together the input variable (studied_data), its associated metadata (standard_var) if any, the threshold it must be compared to.

name[source]#

Name of the variable.

Type:

str

standard_var[source]#

CF metadata bounded to the standard variable used for this ClimateVariable.

Type:

StandardVariable

studied_data[source]#

The variable studied.

Type:

DataArray

threshold[source]#

thresholds for this variable

Type:

Threshold | None

build_indicator_metadata(src_freq: icclim._core.frequency.Frequency, must_run_bootstrap: bool, jinja_scope: dict[str, Any], jinja_env: jinja2.Environment) dict[str, str | dict][source]#

Build the metadata for the indicator that will be computed with this variable.

Parameters:
  • src_freq (Frequency) – The frequency of the source data.

  • must_run_bootstrap (bool) – Whether the bootstrap method must be run.

  • jinja_scope (dict) – The scope to use for jinja templating.

  • jinja_env (jinja2.Environment) – The environment to use for jinja templating.

Returns:

The metadata for the indicator.

Return type:

dict of str, str | dict

icclim._core.climate_variable.build_climate_vars(climate_vars_dict: dict[str, icclim._core.model.in_file_dictionary.InFileDictionary], ignore_Feb29th: bool, time_range: collections.abc.Sequence[datetime.datetime | str] | None, base_period: collections.abc.Sequence[str] | None, standard_index: icclim._core.model.standard_index.StandardIndex | None, is_compared_to_reference: bool) list[ClimateVariable][source]#

Build a list of ClimateVariable from a dictionary of input files.

Parameters:
  • climate_vars_dict (dict of str, InFileDictionary) – The dictionary of input files.

  • ignore_Feb29th (bool) – Whether to ignore February 29th.

  • time_range (Sequence of datetime | str | None) – The time range to consider.

  • base_period (Sequence of str | None) –

    The base period to consider, used to build a reference variable for indices such

    as anomaly.

  • standard_index (StandardIndex | None) – The standard index to compute.

Return type:

list of ClimateVariable that will be used to compute the climate index.

icclim._core.climate_variable.build_climate_var(climate_var_name: str, climate_var_data: icclim._core.model.in_file_dictionary.InFileDictionary | icclim._core.model.icclim_types.InFileBaseType, ignore_Feb29th: bool, time_range: collections.abc.Sequence[datetime.datetime | str] | None, standard_var: icclim._core.model.standard_variable.StandardVariable | None) ClimateVariable[source]#

Build a ClimateVariable object.

Parameters:
  • climate_var_name (str) – The name of the climate variable.

  • climate_var_data (InFileDictionary | InFileBaseType) – The input data for the climate variable. It can be either a dictionary or a file.

  • ignore_Feb29th (bool) – Flag indicating whether to ignore February 29th in the time range.

  • time_range (Sequence[datetime | str] | None) – The time range to consider for the climate variable. It can be a sequence of datetime objects or strings, or None to consider the entire time range.

  • standard_var (StandardVariable | None) – The standard variable to use for the climate variable. If None, the input data will be used to guess the standard variable.

Returns:

The built ClimateVariable object.

Return type:

ClimateVariable

Notes

This function builds a ClimateVariable object based on the provided inputs. It reads the input data, determines the standard variable, builds the studied data, and sets the threshold and global metadata.

If the input data is a dictionary, it is assumed to have a ‘study’ key containing the study data and an optional ‘thresholds’ key containing the threshold data.

If the input data is a file, it is assumed to contain the study data.

The standard variable is used to determine the conversion unit for the threshold data.

The studied data is built based on the study data, time range, ignore_Feb29th flag, and standard variable.

If a threshold is provided in the dictionary, it is added to the ClimateVariable.

Examples

>>> climate_var_name = "tas"
>>> climate_var_data = {"study": "/path/to/data.nc", "thresholds": ">= 27 degC"}
>>> ignore_Feb29th = False
>>> time_range = ["2000-01-01", "2010-12-31"]
>>> standard_var = StandardVariableRegistry.TAS
>>> climate_var = build_climate_var(
...     climate_var_name, climate_var_data, ignore_Feb29th, time_range, standard_var
... )
icclim._core.climate_variable.must_run_bootstrap(da: xarray.core.dataarray.DataArray, threshold: icclim._core.model.threshold.Threshold | None) bool[source]#

Determine whether to run the bootstrap method.

Parameters:
  • da (DataArray) – The studied data.

  • threshold (Threshold | None) – The threshold that contains the reference period.

Returns:

Whether to run the bootstrap method.

Return type:

bool

Notes

This function is used to avoid bootstrapping if there is one single year overlapping or no year overlapping or all year overlapping between the studied data da and the reference period defined by the threshold.

icclim._core.climate_variable._build_reference_variable(reference_period: collections.abc.Sequence[str] | None, in_files: dict[str, icclim._core.model.in_file_dictionary.InFileDictionary], standard_var: icclim._core.model.standard_variable.StandardVariable) ClimateVariable[source]#

Add a secondary variable for indices such as anomaly.

This kind of indices require exactly two variables, but the second variable can just be a subset of the first one.