API

Indicators

Indices

See: Climate Indices

Health Checks

See: Health Checks

Translation Tools

See: Internationalization

Ensembles Module

Ensemble tools

This submodule defines some useful methods for dealing with ensembles of climate simulations. In xclim, an “ensemble” is a Dataset or a DataArray where multiple climate realizations or models are concatenated along the realization dimension.

xclim.ensembles.create_ensemble(datasets, multifile=False, resample_freq=None, calendar=None, realizations=None, cal_kwargs=None, **xr_kwargs)[source]

Create an xarray dataset of an ensemble of climate simulation from a list of netcdf files.

Input data is concatenated along a newly created data dimension (‘realization’). Returns an xarray dataset object containing input data from the list of netcdf files concatenated along a new dimension (name:’realization’). In the case where input files have unequal time dimensions, the output ensemble Dataset is created for maximum time-step interval of all input files. Before concatenation, datasets not covering the entire time span have their data padded with NaN values. Dataset and variable attributes of the first dataset are copied to the resulting dataset.

Parameters:
  • datasets (list or dict or string) – List of netcdf file paths or xarray Dataset/DataArray objects . If multifile is True, ncfiles should be a list of lists where each sublist contains input .nc files of an xarray multifile Dataset. If DataArray objects are passed, they should have a name in order to be transformed into Datasets. A dictionary can be passed instead of a list, in which case the keys are used as coordinates along the new realization axis. If a string is passed, it is assumed to be a glob pattern for finding datasets.

  • multifile (bool) – If True, climate simulations are treated as xarray multifile Datasets before concatenation. Only applicable when “datasets” is sequence of list of file paths. Default: False.

  • resample_freq (Optional[str]) – If the members of the ensemble have the same frequency but not the same offset, they cannot be properly aligned. If resample_freq is set, the time coordinate of each member will be modified to fit this frequency.

  • calendar (str, optional) – The calendar of the time coordinate of the ensemble. By default, the smallest common calendar is chosen. For example, a mixed input of “noleap” and “360_day” will default to “noleap”. ‘default’ is the standard calendar using np.datetime64 objects (xarray’s “standard” with use_cftime=False).

  • realizations (sequence, optional) – The coordinate values for the new realization axis. If None (default), the new axis has a simple integer coordinate. This argument shouldn’t be used if datasets is a glob pattern as the dataset order is random.

  • cal_kwargs (dict, optional) – Additional arguments to pass to py:func:xclim.core.calendar.convert_calendar. For conversions involving ‘360_day’, the align_on=’date’ option is used by default.

  • **xr_kwargs – Any keyword arguments to be given to xr.open_dataset when opening the files (or to xr.open_mfdataset if multifile is True)

Return type:

Dataset

Returns:

xr.Dataset – Dataset containing concatenated data from all input files.

Notes

Input netcdf files require equal spatial dimension size (e.g. lon, lat dimensions). If input data contains multiple cftime calendar types they must be at monthly or coarser frequency.

Examples

from pathlib import Path
from xclim.ensembles import create_ensemble

ens = create_ensemble(temperature_datasets)

# Using multifile datasets, through glob patterns.
# Simulation 1 is a list of .nc files (e.g. separated by time):
datasets = list(Path("/dir").glob("*.nc"))

# Simulation 2 is also a list of .nc files:
datasets.extend(Path("/dir2").glob("*.nc"))
ens = create_ensemble(datasets, multifile=True)
xclim.ensembles.ensemble_mean_std_max_min(ens, min_members=1, weights=None)[source]

Calculate ensemble statistics between a results from an ensemble of climate simulations.

Returns an xarray Dataset containing ensemble mean, standard-deviation, minimum and maximum for input climate simulations.

Parameters:
  • ens (xr.Dataset) – Ensemble dataset (see xclim.ensembles.create_ensemble).

  • min_members (int, optional) – The minimum number of valid ensemble members for a statistic to be valid. Passing None is equivalent to setting min_members to the size of the realization dimension. The default (1) essentially skips this check.

  • weights (xr.DataArray, optional) – Weights to apply along the ‘realization’ dimension. This array cannot contain missing values.

Return type:

Dataset

Returns:

xr.Dataset – Dataset with data variables of ensemble statistics.

Examples

from xclim.ensembles import create_ensemble, ensemble_mean_std_max_min

# Create the ensemble dataset:
ens = create_ensemble(temperature_datasets)

# Calculate ensemble statistics:
ens_mean_std = ensemble_mean_std_max_min(ens)
xclim.ensembles.ensemble_percentiles(ens, values=None, keep_chunk_size=None, min_members=1, weights=None, split=True)[source]

Calculate ensemble statistics between a results from an ensemble of climate simulations.

Returns a Dataset containing ensemble percentiles for input climate simulations.

Parameters:
  • ens (xr.Dataset or xr.DataArray) – Ensemble Dataset or DataArray (see xclim.ensembles.create_ensemble).

  • values (Sequence[int], optional) – Percentile values to calculate. Default: (10, 50, 90).

  • keep_chunk_size (bool, optional) – For ensembles using dask arrays, all chunks along the ‘realization’ axis are merged. If True, the dataset is rechunked along the dimension with the largest chunks, so that the chunks keep the same size (approximately). If False, no shrinking is performed, resulting in much larger chunks. If not defined, the function decides which is best.

  • min_members (int, optional) – The minimum number of valid ensemble members for a statistic to be valid. Passing None is equivalent to setting min_members to the size of the realization dimension. The default (1) essentially skips this check.

  • weights (xr.DataArray, optional) – Weights to apply along the ‘realization’ dimension. This array cannot contain missing values. When given, the function uses xarray’s quantile method which is slower than xclim’s NaN-optimized algorithm.

  • split (bool) – Whether to split each percentile into a new variable or concatenate the output along a new “percentiles” dimension.

Return type:

DataArray | Dataset

Returns:

xr.Dataset or xr.DataArray – If split is True, same type as ens; dataset otherwise, containing data variable(s) of requested ensemble statistics

Examples

from xclim.ensembles import create_ensemble, ensemble_percentiles

# Create ensemble dataset:
ens = create_ensemble(temperature_datasets)

# Calculate default ensemble percentiles:
ens_percs = ensemble_percentiles(ens)

# Calculate non-default percentiles (25th and 75th)
ens_percs = ensemble_percentiles(ens, values=(25, 50, 75))

# If the original array has many small chunks, it might be more efficient to do:
ens_percs = ensemble_percentiles(ens, keep_chunk_size=False)

Ensemble Reduction

Ensemble reduction is the process of selecting a subset of members from an ensemble in order to reduce the volume of computation needed while still covering a good portion of the simulated climate variability.

xclim.ensembles.kkz_reduce_ensemble(data, num_select, *, dist_method='euclidean', standardize=True, **cdist_kwargs)[source]

Return a sample of ensemble members using KKZ selection.

The algorithm selects num_select ensemble members spanning the overall range of the ensemble. The selection is ordered, smaller groups are always subsets of larger ones for given criteria. The first selected member is the one nearest to the centroid of the ensemble, all subsequent members are selected in a way maximizing the phase-space coverage of the group. Algorithm taken from Cannon [2015].

Parameters:
  • data (xr.DataArray) – Selection criteria data : 2-D xr.DataArray with dimensions ‘realization’ (N) and ‘criteria’ (P). These are the values used for clustering. Realizations represent the individual original ensemble members and criteria the variables/indicators used in the grouping algorithm.

  • num_select (int) – The number of members to select.

  • dist_method (str) – Any distance metric name accepted by scipy.spatial.distance.cdist.

  • standardize (bool) – Whether to standardize the input before running the selection or not. Standardization consists in translation as to have a zero mean and scaling as to have a unit standard deviation.

  • **cdist_kwargs – All extra arguments are passed as-is to scipy.spatial.distance.cdist, see its docs for more information.

Return type:

list

Returns:

list – Selected model indices along the realization dimension.

References

Cannon [2015], Katsavounidis, Jay Kuo, and Zhang [1994]

xclim.ensembles.kmeans_reduce_ensemble(data, *, method=None, make_graph=True, max_clusters=None, variable_weights=None, model_weights=None, sample_weights=None, random_state=None)[source]

Return a sample of ensemble members using k-means clustering.

The algorithm attempts to reduce the total number of ensemble members while maintaining adequate coverage of the ensemble uncertainty in an N-dimensional data space. K-Means clustering is carried out on the input selection criteria data-array in order to group individual ensemble members into a reduced number of similar groups. Subsequently, a single representative simulation is retained from each group.

Parameters:
  • data (xr.DataArray) – Selection criteria data : 2-D xr.DataArray with dimensions ‘realization’ (N) and ‘criteria’ (P). These are the values used for clustering. Realizations represent the individual original ensemble members and criteria the variables/indicators used in the grouping algorithm.

  • method (dict, optional) – Dictionary defining selection method and associated value when required. See Notes.

  • max_clusters (int, optional) – Maximum number of members to include in the output ensemble selection. When using ‘rsq_optimize’ or ‘rsq_cutoff’ methods, limit the final selection to a maximum number even if method results indicate a higher value. Defaults to N.

  • variable_weights (np.ndarray, optional) – An array of size P. This weighting can be used to influence of weight of the climate indices (criteria dimension) on the clustering itself.

  • model_weights (np.ndarray, optional) – An array of size N. This weighting can be used to influence which realization is selected from within each cluster. This parameter has no influence on the clustering itself.

  • sample_weights (np.ndarray, optional) – An array of size N. sklearn.cluster.KMeans() sample_weights parameter. This weighting can be used to influence of weight of simulations on the clustering itself. See: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html

  • random_state (int or np.random.RandomState, optional) – sklearn.cluster.KMeans() random_state parameter. Determines random number generation for centroid initialization. Use to make the randomness deterministic. See: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html

  • make_graph (bool) – output a dictionary of input for displays a plot of R² vs. the number of clusters. Defaults to True if matplotlib is installed in runtime environment.

Return type:

tuple[list, numpy.ndarray, dict]

Notes

Parameters for method in call must follow these conventions:

rsq_optimize

Calculate coefficient of variation (R²) of cluster results for n = 1 to N clusters and determine an optimal number of clusters that balances cost/benefit tradeoffs. This is the default setting. See supporting information S2 text in Casajus et al. [2016].

method={‘rsq_optimize’:None}

rsq_cutoff

Calculate Coefficient of variation (R²) of cluster results for n = 1 to N clusters and determine the minimum numbers of clusters needed for R² > val.

val : float between 0 and 1. R² value that must be exceeded by clustering results.

method={‘rsq_cutoff’: val}

n_clusters

Create a user determined number of clusters.

val : integer between 1 and N

method={‘n_clusters’: val}

Return type:

tuple[list, ndarray, dict]

Returns:

  • list – Selected model indexes (positions)

  • np.ndarray – KMeans clustering results

  • dict – Dictionary of input data for creating R² profile plot. ‘None’ when make_graph=False

References

Casajus, Périé, Logan, Lambert, Blois, and Berteaux [2016]

Examples

import xclim
from xclim.ensembles import create_ensemble, kmeans_reduce_ensemble
from xclim.indices import hot_spell_frequency

# Start with ensemble datasets for temperature:

ensTas = create_ensemble(temperature_datasets)

# Calculate selection criteria -- Use annual climate change Δ fields between 2071-2100 and 1981-2010 normals.
# First, average annual temperature:

tg = xclim.atmos.tg_mean(tas=ensTas.tas)
his_tg = tg.sel(time=slice("1990", "2019")).mean(dim="time")
fut_tg = tg.sel(time=slice("2020", "2050")).mean(dim="time")
dtg = fut_tg - his_tg

# Then, hot spell frequency as second indicator:

hs = hot_spell_frequency(tasmax=ensTas.tas, window=2, thresh_tasmax="10 degC")
his_hs = hs.sel(time=slice("1990", "2019")).mean(dim="time")
fut_hs = hs.sel(time=slice("2020", "2050")).mean(dim="time")
dhs = fut_hs - his_hs

# Create a selection criteria xr.DataArray:

from xarray import concat

crit = concat((dtg, dhs), dim="criteria")

# Finally, create clusters and select realization ids of reduced ensemble:

ids, cluster, fig_data = kmeans_reduce_ensemble(
    data=crit, method={"rsq_cutoff": 0.9}, random_state=42, make_graph=False
)
ids, cluster, fig_data = kmeans_reduce_ensemble(
    data=crit, method={"rsq_optimize": None}, random_state=42, make_graph=True
)
xclim.ensembles.plot_rsqprofile(fig_data)[source]

Create an R² profile plot using kmeans_reduce_ensemble output.

The R² plot allows evaluation of the proportion of total uncertainty in the original ensemble that is provided by the reduced selected.

Examples

>>> from xclim.ensembles import kmeans_reduce_ensemble, plot_rsqprofile
>>> is_matplotlib_installed()
>>> crit = xr.open_dataset(path_to_ensemble_file).data
>>> ids, cluster, fig_data = kmeans_reduce_ensemble(
...     data=crit, method={"rsq_cutoff": 0.9}, random_state=42, make_graph=True
... )
>>> plot_rsqprofile(fig_data)

Ensemble Robustness Metrics

Robustness metrics are used to estimate the confidence of the climate change signal of an ensemble. This submodule is inspired by and tries to follow the guidelines of the IPCC, more specifically [Collins et al., 2013] (AR5) and On Climate Change (IPCC) [2023] (AR6).

xclim.ensembles.robustness_fractions(fut, ref=None, test=None, weights=None, **kwargs)[source]

Robustness statistics qualifying how members of an ensemble agree on the existence of change and on its sign.

Parameters:
  • fut (xr.DataArray) – Future period values along ‘realization’ and ‘time’ (…, nr, nt1) or if ref is None, Delta values along realization (…, nr).

  • ref (xr.DataArray, optional) – Reference period values along realization’ and ‘time’ (…, nr, nt2). The size of the ‘time’ axis does not need to match the one of fut. But their ‘realization’ axes must be identical and the other coordinates should be the same. If None (default), values of fut are assumed to be deltas instead of a distribution across the future period.

  • test ({ttest, welch-ttest, mannwhitney-utest, brownforsythe-test, ipcc-ar6-c, threshold}, optional) – Name of the statistical test used to determine if there was significant change. See notes.

  • weights (xr.DataArray) – Weights to apply along the ‘realization’ dimension. This array cannot contain missing values.

  • **kwargs – Other arguments specific to the statistical test. See notes.

Return type:

Dataset

Returns:

xr.Dataset – Same coordinates as fut and ref, but no time and no realization.

Variables:

changed :

The weighted fraction of valid members showing significant change. Passing test=None yields change_frac = 1 everywhere. Same type as fut.

positive :

The weighted fraction of valid members showing positive change, no matter if it is significant or not.

changed_positive :

The weighted fraction of valid members showing significant and positive change (]0, 1]).

agree :

The weighted fraction of valid members agreeing on the sign of change. It is the maximum between positive and 1 - positive.

valid :

The weighted fraction of valid members. A member is valid is there are no NaNs along the time axes of fut and ref.

pvals :

The p-values estimated by the significance tests. Only returned if the test uses pvals. Has the realization dimension.

Notes

The table below shows the coefficient needed to retrieve the number of members that have the indicated characteristics, by multiplying it by the total number of members (fut.realization.size) and by valid_frac, assuming uniform weights. For compactness, we rename the outputs cf, cpf and pf.

Significant change

Non-significant change

Any change

Any direction

cf

1 - cf

1

Positive change

cpf

pf - cpf

pf

Negative change

(cf - cpf)

1 - pf - (cf -cpf)

1 - pf

Available statistical tests are :

ttest:

Single sample T-test. Same test as used by Tebaldi et al. [2011].

The future values are compared against the reference mean (over ‘time’). Accepts argument p_change (float, default : 0.05) the p-value threshold for rejecting the hypothesis of no significant change.

welch-ttest:

Two-sided T-test, without assuming equal population variance.

Same significance criterion and argument as ‘ttest’.

mannwhitney-utest:

Two-sided Mann-Whiney U-test. Same significance criterion and argument as ‘ttest’.

brownforsythe-test:

Brown-Forsythe test assuming skewed, non-normal distributions.

Same significance criterion and argument as ‘ttest’.

ipcc-ar6-c:

The advanced approach used in the IPCC Atlas chapter (on Climate Change (IPCC) [2023]).

Change is considered significant if the delta exceeds a threshold related to the internal variability. If pre-industrial data is given in argument ref_pi, the threshold is defined as \(\sqrt{2}*1.645*\sigma_{20yr}\), where \(\sigma_{20yr}\) is the standard deviation of 20-year means computed from non-overlapping periods after detrending with a quadratic fit. Otherwise, when such pre-industrial control data is not available, the threshold is defined in relation to the historical data (ref) as \(\sqrt{\frac{2}{20}}*1.645*\sigma_{1yr}, where :math:\)sigma_{1yr}` is the inter-annual standard deviation measured after linearly detrending the data. See notebook Ensembles for more details.

threshold :

Change is considered significant when it exceeds an absolute or relative threshold. Accepts one argument, either “abs_thresh” or “rel_thresh”.

None :

Significant change is not tested. Members showing any positive change are included in the pos_frac output.

References

Tebaldi, Arblaster, and Knutti [2011] On Climate Change (IPCC) [2023]

Example

This example computes the mean temperature in an ensemble and compares two time periods, qualifying significant change through a single sample T-test.

>>> from xclim import ensembles
>>> ens = ensembles.create_ensemble(temperature_datasets)
>>> tgmean = xclim.atmos.tg_mean(tas=ens.tas, freq="YS")
>>> fut = tgmean.sel(time=slice("2020", "2050"))
>>> ref = tgmean.sel(time=slice("1990", "2020"))
>>> fractions = ensembles.robustness_fractions(fut, ref, test="ttest")
xclim.ensembles.robustness_categories(changed_or_fractions, agree=None, *, categories=None, ops=None, thresholds=None)[source]

Create a categorical robustness map for mapping hatching patterns.

Each robustness category is defined by a double threshold, one on the fraction of members showing significant change (change_frac) and one on the fraction of member agreeing on the sign of change (agree_frac). When the two thresholds are fulfilled, the point is assigned to the given category. The default values for the comparisons are the ones suggested by the IPCC for its “Advanced approach” described in the Cross-Chapter Box 1 of the Atlas of the AR6 WGI report (on Climate Change (IPCC) [2023]).

Parameters:
  • changed_or_fractions (xr.Dataset or xr.DataArray) – Either the fraction of members showing significant change as an array or directly the output of robustness_fractions().

  • agree (xr.DataArray, optional) – The fraction of members agreeing on the sign of change. Only needed if the first argument is the changed array.

  • categories (list of str, optional) – The label of each robustness categories. They are stored in the semicolon separated flag_descriptions attribute as well as in a compressed form in the flag_meanings attribute. If a point is mapped to two categories, priority is given to the first one in this list.

  • ops (list of tuples of str, optional) – For each category, the comparison operators for change_frac and agree_frac. None or an empty string means the variable is not needed for this category.

  • thresholds (list of tuples of float, optional) – For each category, the threshold to be used with the corresponding operator. All should be between 0 and 1.

Return type:

DataArray

Returns:

xr.DataArray – Categorical (int) array following the flag variables CF conventions. 99 is used as a fill value for points that do not fall in any category.

xclim.ensembles.change_significance(fut, ref, test='ttest', weights=None, p_vals=False, **kwargs)[source]

Backwards-compatible implementation of robustness_fractions().

Return type:

tuple[DataArray | Dataset, DataArray | Dataset] | tuple[DataArray | Dataset, DataArray | Dataset, DataArray | Dataset | None]

xclim.ensembles.robustness_coefficient(fut, ref)[source]

Robustness coefficient quantifying the robustness of a climate change signal in an ensemble.

Taken from Knutti and Sedláček [2013].

The robustness metric is defined as R = 1 − A1 / A2 , where A1 is defined as the integral of the squared area between two cumulative density functions characterizing the individual model projections and the multimodel mean projection and A2 is the integral of the squared area between two cumulative density functions characterizing the multimodel mean projection and the historical climate. Description taken from Knutti and Sedláček [2013].

A value of R equal to one implies perfect model agreement. Higher model spread or smaller signal decreases the value of R.

Parameters:
  • fut (Union[xr.DataArray, xr.Dataset]) – Future ensemble values along ‘realization’ and ‘time’ (nr, nt). Can be a dataset, in which case the coefficient is computed on each variable.

  • ref (Union[xr.DataArray, xr.Dataset]) – Reference period values along ‘time’ (nt). Same type as fut.

Return type:

DataArray | Dataset

Returns:

xr.DataArray or xr.Dataset – The robustness coefficient, ]-inf, 1], float. Same type as fut or ref.

References

Knutti and Sedláček [2013]

Uncertainty Partitioning

This module implements methods and tools meant to partition climate projection uncertainties into different components.

xclim.ensembles.hawkins_sutton(da, sm=None, weights=None, baseline=('1971', '2000'), kind='+')[source]

Return the mean and partitioned variance of an ensemble based on method from Hawkins & Sutton (2009).

Parameters:
  • da (xr.DataArray) – Time series with dimensions ‘time’, ‘scenario’ and ‘model’.

  • sm (xr.DataArray, optional) – Smoothed time series over time, with the same dimensions as da. By default, this is estimated using a 4th-order polynomial. Results are sensitive to the choice of smoothing function, use this to set another polynomial order, or a LOESS curve.

  • weights (xr.DataArray, optional) – Weights to be applied to individual models. Should have model dimension.

  • baseline ((str, str)) – Start and end year of the reference period.

  • kind ({‘+’, ‘’}*) – Whether the mean over the reference period should be subtracted (+) or divided by (*).

Return type:

tuple[DataArray, DataArray]

Returns:

xr.DataArray, xr.DataArray – The mean relative to the baseline, and the components of variance of the ensemble. These components are coordinates along the uncertainty dimension: variability, model, scenario, and total.

Notes

To prepare input data, make sure da has dimensions time, scenario and model, e.g. da.rename({“scen”: “scenario”}).

To reproduce results from Hawkins and Sutton [2009], input data should meet the following requirements:
  • annual time series starting in 1950 and ending in 2100;

  • the same models are available for all scenarios.

To get the fraction of the total variance instead of the variance itself, call fractional_uncertainty on the output.

References

Hawkins and Sutton [2009], Hawkins and Sutton [2011]

xclim.ensembles.lafferty_sriver(da, sm=None, bb13=False)[source]

Return the mean and partitioned variance of an ensemble based on method from Lafferty and Sriver (2023).

Parameters:
  • da (xr.DataArray) – Time series with dimensions ‘time’, ‘scenario’, ‘downscaling’ and ‘model’.

  • sm (xr.DataArray) – Smoothed time series over time, with the same dimensions as da. By default, this is estimated using a 4th-order polynomial. Results are sensitive to the choice of smoothing function, use this to set another polynomial order, or a LOESS curve.

  • bb13 (bool) – Whether to apply the Brekke and Barsugli (2013) method to estimate scenario uncertainty, where the variance over scenarios is computed before taking the mean over models and downscaling methods.

Return type:

tuple[DataArray, DataArray]

Returns:

xr.DataArray, xr.DataArray – The mean relative to the baseline, and the components of variance of the ensemble. These components are coordinates along the uncertainty dimension: variability, model, scenario, downscaling and total.

Notes

To prepare input data, make sure da has dimensions time, scenario, downscaling and model, e.g. da.rename({“experiment”: “scenario”}).

To get the fraction of the total variance instead of the variance itself, call fractional_uncertainty on the output.

References

Lafferty and Sriver [2023]

Units Handling Submodule

Units Handling Submodule

xclim’s pint-based unit registry is an extension of the registry defined in cf-xarray. This module defines most unit handling methods.

xclim.core.units.amount2lwethickness(amount, out_units=None)[source]

Convert a liquid water amount (mass over area) to its equivalent area-averaged thickness (length).

This will simply divide the amount by the density of liquid water, 1000 kg/m³. This is equivalent to using the “hydro” context of xclim.core.units.units.

Parameters:
  • amount (xr.DataArray) – A DataArray storing a liquid water amount quantity.

  • out_units (str, optional) – Specific output units, if needed.

Return type:

Union[DataArray, TypeVar(Quantified, DataArray, str, Quantity)]

Returns:

xr.DataArray or Quantified – The standard_name of amount is modified if a conversion is found (see xclim.core.units.cf_conversion()), it is removed otherwise. Other attributes are left untouched.

xclim.core.units.amount2rate(amount, dim='time', sampling_rate_from_coord=False, out_units=None)[source]

Convert an amount variable to a rate by dividing by the sampling period length.

If the sampling period length cannot be inferred, the amount values are divided by the duration between their time coordinate and the next one. The last period is estimated with the duration of the one just before.

This is the inverse operation of xclim.core.units.rate2amount().

Parameters:
  • amount (xr.DataArray) – “amount” variable. Ex: Precipitation amount in “mm”.

  • dim (str) – The time dimension.

  • sampling_rate_from_coord (boolean) – For data with irregular time coordinates. If True, the diff of the time coordinate will be used as the sampling rate, meaning each data point will be assumed to span the interval ending at the next point. See notes of xclim.core.units.rate2amount(). Defaults to False, which raises an error if the time coordinate is irregular.

  • out_units (str, optional) – Specific output units, if needed.

Raises:

ValueError – If the time coordinate is irregular and sampling_rate_from_coord is False (default).

Return type:

DataArray

Returns:

xr.DataArray

See also

rate2amount

xclim.core.units.check_units(val, dim)[source]

Check that units are compatible with dimensions, otherwise raise a ValidationError.

Parameters:
  • val (str or xr.DataArray, optional) – Value to check.

  • dim (str or xr.DataArray, optional) – Expected dimension, e.g. [temperature]. If a quantity or DataArray is given, the dimensionality is extracted.

Return type:

None

xclim.core.units.convert_units_to(source, target, context=None)[source]

Convert a mathematical expression into a value with the same units as a DataArray.

If the dimensionalities of source and target units differ, automatic CF conversions will be applied when possible. See xclim.core.units.cf_conversion().

Parameters:
  • source (str or xr.DataArray or units.Quantity) – The value to be converted, e.g. ‘4C’ or ‘1 mm/d’.

  • target (str or xr.DataArray or units.Quantity or units.Unit) – Target array of values to which units must conform.

  • context (str, optional) – The unit definition context. Default: None. If “infer”, it will be inferred with xclim.core.units.infer_context() using the standard name from the source or, if none is found, from the target. This means that the ‘hydro’ context could be activated if any one of the standard names allows it.

Return type:

TypeVar(Quantified, DataArray, str, Quantity)

Returns:

str or xr.DataArray or units.Quantity – The source value converted to target’s units. The outputted type is always similar to source initial type. Attributes are preserved unless an automatic CF conversion is performed, in which case only the new standard_name appears in the result.

xclim.core.units.declare_relative_units(**units_by_name)[source]

Function decorator checking the units of arguments.

The decorator checks that input values have units that are compatible with each other. It also stores the input units as a ‘relative_units’ attribute.

Parameters:

**kwargs – Mapping from the input parameter names to dimensions relative to other parameters. The dimensions can be a single parameter name as <other_var> or more complex expressions, like: <other_var> * [time].

Return type:

Callable

Returns:

Callable

Examples

In the following function definition:

@declare_relative_units(thresh="<da>", thresh2="<da> / [time]")
def func(da, thresh, thresh2): ...

The decorator will check that thresh has units compatible with those of da and that thresh2 has units compatible with the time derivative of da.

Usually, the function would be decorated further by declare_units() to create a unit-aware index:

temperature_func = declare_units(da="[temperature]")(func)

This call will replace the “<da>” by “[temperature]” everywhere needed.

See also

declare_units

xclim.core.units.declare_units(**units_by_name)[source]

Create a decorator to check units of function arguments.

The decorator checks that input and output values have units that are compatible with expected dimensions. It also stores the input units as a ‘in_units’ attribute.

Parameters:

**units_by_name – Mapping from the input parameter names to their units or dimensionality (“[…]”). If this decorates a function previously decorated with declare_relative_units(), the relative unit declarations are made absolute with the information passed here.

Return type:

Callable

Returns:

Callable

Examples

In the following function definition:

@declare_units(tas="[temperature]")
def func(tas): ...

The decorator will check that tas has units of temperature (C, K, F).

xclim.core.units.ensure_cf_units(ustr)[source]

Ensure the passed unit string is CF-compliant.

The string will be parsed to pint then recast to a string by xclim’s pint2cfunits.

Return type:

str

xclim.core.units.ensure_delta(unit)[source]

Return delta units for temperature.

For dimensions where delta exist in pint (Temperature), it replaces the temperature unit by delta_degC or delta_degF based on the input unit. For other dimensionality, it just gives back the input units.

Parameters:

unit (str) – unit to transform in delta (or not)

Return type:

str

xclim.core.units.flux2rate(flux, density, out_units=None)[source]

Convert a flux variable to a rate by dividing with a density.

This is the inverse operation of xclim.core.units.rate2flux().

Parameters:
  • flux (xr.DataArray) – “flux” variable. Ex: Snowfall flux in “kg m-2 s-1”.

  • density (Quantified) – Density used to convert from a flux to a rate. Ex: Snowfall density “312 kg m-3”. Density can also be an array with the same shape as flux.

  • out_units (str, optional) – Specific output units, if needed.

Return type:

DataArray

Returns:

rate (xr.DataArray)

Examples

The following converts an array of snowfall flux in kg m-2 s-1 to snowfall flux in mm/s, assuming a density of 100 kg m-3:

>>> time = xr.cftime_range("2001-01-01", freq="D", periods=365)
>>> prsn = xr.DataArray(
...     [0.1] * 365,
...     dims=("time",),
...     coords={"time": time},
...     attrs={"units": "kg m-2 s-1"},
... )
>>> prsnd = flux2rate(prsn, density="100 kg m-3", out_units="mm/s")
>>> prsnd.units
'mm s-1'
>>> float(prsnd[0])
1.0

See also

rate2flux

xclim.core.units.infer_context(standard_name=None, dimension=None)[source]

Return units context based on either the variable’s standard name or the pint dimension.

Valid standard names for the hydro context are those including the terms “rainfall”, “lwe” (liquid water equivalent) and “precipitation”. The latter is technically incorrect, as any phase of precipitation could be referenced. Standard names for evapotranspiration, evaporation and canopy water amounts are also associated with the hydro context.

Parameters:
  • standard_name (str, optional) – CF-Convention standard name.

  • dimension (str, optional) – Pint dimension, e.g. ‘[time]’.

Returns:

str – “hydro” if variable is a liquid water flux, otherwise “none”.

xclim.core.units.infer_sampling_units(da, deffreq='D', dim='time')[source]

Infer a multiplier and the units corresponding to one sampling period.

Parameters:
  • da (xr.DataArray) – A DataArray from which to take coordinate dim.

  • deffreq (str, optional) – If no frequency is inferred from da[dim], take this one.

  • dim (str) – Dimension from which to infer the frequency.

Raises:

ValueError – If the frequency has no exact corresponding units.

Return type:

tuple[int, str]

Returns:

  • int – The magnitude (number of base periods per period)

  • str – Units as a string, understandable by pint.

xclim.core.units.lwethickness2amount(thickness, out_units=None)[source]

Convert a liquid water thickness (length) to its equivalent amount (mass over area).

This will simply multiply the thickness by the density of liquid water, 1000 kg/m³. This is equivalent to using the “hydro” context of xclim.core.units.units.

Parameters:
  • thickness (xr.DataArray) – A DataArray storing a liquid water thickness quantity.

  • out_units (str, optional) – Specific output units, if needed.

Return type:

Union[DataArray, TypeVar(Quantified, DataArray, str, Quantity)]

Returns:

xr.DataArray or Quantified – The standard_name of amount is modified if a conversion is found (see xclim.core.units.cf_conversion()), it is removed otherwise. Other attributes are left untouched.

xclim.core.units.pint2cfunits(value)[source]

Return a CF-compliant unit string from a pint unit.

Parameters:

value (pint.Unit) – Input unit.

Return type:

str

Returns:

str – Units following CF-Convention, using symbols.

xclim.core.units.pint_multiply(da, q, out_units=None)[source]

Multiply xarray.DataArray by pint.Quantity.

Parameters:
  • da (xr.DataArray) – Input array.

  • q (pint.Quantity) – Multiplicative factor.

  • out_units (str, optional) – Units the output array should be converted into.

Return type:

DataArray

Returns:

xr.DataArray

xclim.core.units.rate2amount(rate, dim='time', sampling_rate_from_coord=False, out_units=None)[source]

Convert a rate variable to an amount by multiplying by the sampling period length.

If the sampling period length cannot be inferred, the rate values are multiplied by the duration between their time coordinate and the next one. The last period is estimated with the duration of the one just before.

This is the inverse operation of xclim.core.units.amount2rate().

Parameters:
  • rate (xr.DataArray) – “Rate” variable, with units of “amount” per time. Ex: Precipitation in “mm / d”.

  • dim (str) – The time dimension.

  • sampling_rate_from_coord (boolean) – For data with irregular time coordinates. If True, the diff of the time coordinate will be used as the sampling rate, meaning each data point will be assumed to apply for the interval ending at the next point. See notes. Defaults to False, which raises an error if the time coordinate is irregular.

  • out_units (str, optional) – Specific output units, if needed.

Raises:

ValueError – If the time coordinate is irregular and sampling_rate_from_coord is False (default).

Return type:

DataArray

Returns:

xr.DataArray

Examples

The following converts a daily array of precipitation in mm/h to the daily amounts in mm:

>>> time = xr.cftime_range("2001-01-01", freq="D", periods=365)
>>> pr = xr.DataArray(
...     [1] * 365, dims=("time",), coords={"time": time}, attrs={"units": "mm/h"}
... )
>>> pram = rate2amount(pr)
>>> pram.units
'mm'
>>> float(pram[0])
24.0

Also works if the time axis is irregular : the rates are assumed constant for the whole period starting on the values timestamp to the next timestamp. This option is activated with sampling_rate_from_coord=True.

>>> time = time[[0, 9, 30]]  # The time axis is Jan 1st, Jan 10th, Jan 31st
>>> pr = xr.DataArray(
...     [1] * 3, dims=("time",), coords={"time": time}, attrs={"units": "mm/h"}
... )
>>> pram = rate2amount(pr, sampling_rate_from_coord=True)
>>> pram.values
array([216., 504., 504.])

Finally, we can force output units:

>>> pram = rate2amount(pr, out_units="pc")  # Get rain amount in parsecs. Why not.
>>> pram.values
array([7.00008327e-18, 1.63335276e-17, 1.63335276e-17])

See also

amount2rate

xclim.core.units.rate2flux(rate, density, out_units=None)[source]

Convert a rate variable to a flux by multiplying with a density.

This is the inverse operation of xclim.core.units.flux2rate().

Parameters:
  • rate (xr.DataArray) – “Rate” variable. Ex: Snowfall rate in “mm / d”.

  • density (Quantified) – Density used to convert from a rate to a flux. Ex: Snowfall density “312 kg m-3”. Density can also be an array with the same shape as rate.

  • out_units (str, optional) – Specific output units, if needed.

Return type:

DataArray

Returns:

flux (xr.DataArray)

Examples

The following converts an array of snowfall rate in mm/s to snowfall flux in kg m-2 s-1, assuming a density of 100 kg m-3:

>>> time = xr.cftime_range("2001-01-01", freq="D", periods=365)
>>> prsnd = xr.DataArray(
...     [1] * 365, dims=("time",), coords={"time": time}, attrs={"units": "mm/s"}
... )
>>> prsn = rate2flux(prsnd, density="100 kg m-3", out_units="kg m-2 s-1")
>>> prsn.units
'kg m-2 s-1'
>>> float(prsn[0])
0.1

See also

flux2rate

xclim.core.units.str2pint(val)[source]

Convert a string to a pint.Quantity, splitting the magnitude and the units.

Parameters:

val (str) – A quantity in the form “[{magnitude} ]{units}”, where magnitude can be cast to a float and units is understood by units2pint.

Return type:

Quantity

Returns:

pint.Quantity – Magnitude is 1 if no magnitude was present in the string.

xclim.core.units.to_agg_units(out, orig, op, dim='time')[source]

Set and convert units of an array after an aggregation operation along the sampling dimension (time).

Parameters:
  • out (xr.DataArray) – The output array of the aggregation operation, no units operation done yet.

  • orig (xr.DataArray) – The original array before the aggregation operation, used to infer the sampling units and get the variable units.

  • op ({‘min’, ‘max’, ‘mean’, ‘std’, ‘var’, ‘doymin’, ‘doymax’, ‘count’, ‘integral’, ‘sum’}) – The type of aggregation operation performed. “integral” is mathematically equivalent to “sum”, but the units are multiplied by the timestep of the data (requires an inferrable frequency).

  • dim (str) – The time dimension along which the aggregation was performed.

Return type:

DataArray

Returns:

xr.DataArray

Examples

Take a daily array of temperature and count number of days above a threshold. to_agg_units will infer the units from the sampling rate along “time”, so we ensure the final units are correct:

>>> time = xr.cftime_range("2001-01-01", freq="D", periods=365)
>>> tas = xr.DataArray(
...     np.arange(365),
...     dims=("time",),
...     coords={"time": time},
...     attrs={"units": "degC"},
... )
>>> cond = tas > 100  # Which days are boiling
>>> Ndays = cond.sum("time")  # Number of boiling days
>>> Ndays.attrs.get("units")
None
>>> Ndays = to_agg_units(Ndays, tas, op="count")
>>> Ndays.units
'd'

Similarly, here we compute the total heating degree-days, but we have weekly data:

>>> time = xr.cftime_range("2001-01-01", freq="7D", periods=52)
>>> tas = xr.DataArray(
...     np.arange(52) + 10,
...     dims=("time",),
...     coords={"time": time},
... )
>>> dt = (tas - 16).assign_attrs(units="delta_degC")
>>> degdays = dt.clip(0).sum("time")  # Integral of temperature above a threshold
>>> degdays = to_agg_units(degdays, dt, op="integral")
>>> degdays.units
'week delta_degC'

Which we can always convert to the more common “K days”:

>>> degdays = convert_units_to(degdays, "K days")
>>> degdays.units
'K d'
xclim.core.units.units2pint(value)[source]

Return the pint Unit for the DataArray units.

Parameters:

value (xr.DataArray or str or pint.Quantity) – Input data array or string representing a unit (with no magnitude).

Return type:

Unit

Returns:

pint.Unit – Units of the data array.

SDBA Module

Adjustment Methods

class xclim.sdba.adjustment.DetrendedQuantileMapping(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.TrainAdjust

Detrended Quantile Mapping bias-adjustment.

The algorithm follows these steps, 1-3 being the ‘train’ and 4-6, the ‘adjust’ steps.

  1. A scaling factor that would make the mean of hist match the mean of ref is computed.

  2. ref and hist are normalized by removing the “dayofyear” mean.

  3. Adjustment factors are computed between the quantiles of the normalized ref and hist.

  4. sim is corrected by the scaling factor, and either normalized by “dayofyear” and detrended group-wise or directly detrended per “dayofyear”, using a linear fit (modifiable).

  5. Values of detrended sim are matched to the corresponding quantiles of normalized hist and corrected accordingly.

  6. The trend is put back on the result.

\[F^{-1}_{ref}\left\{F_{hist}\left[\frac{\overline{hist}\cdot sim}{\overline{sim}}\right]\right\}\frac{\overline{sim}}{\overline{hist}}\]

where \(F\) is the cumulative distribution function (CDF) and \(\overline{xyz}\) is the linear trend of the data. This equation is valid for multiplicative adjustment. Based on the DQM method of [Cannon et al., 2015].

Parameters:
  • Train step

  • nquantiles (int or 1d array of floats) – The number of quantiles to use. See equally_spaced_nodes(). An array of quantiles [0, 1] can also be passed. Defaults to 20 quantiles.

  • kind ({‘+’, ‘’}*) – The adjustment kind, either additive or multiplicative. Defaults to “+”.

  • group (Union[str, Grouper]) – The grouping information. See xclim.sdba.base.Grouper for details. Default is “time”, meaning a single adjustment group along dimension “time”.

  • adapt_freq_thresh (str | None) – Threshold for frequency adaptation. See xclim.sdba.processing.adapt_freq for details. Default is None, meaning that frequency adaptation is not performed.

  • Adjust step

  • interp ({‘nearest’, ‘linear’, ‘cubic’}) – The interpolation method to use when interpolating the adjustment factors. Defaults to “nearest”.

  • detrend (int or BaseDetrend instance) – The method to use when detrending. If an int is passed, it is understood as a PolyDetrend (polynomial detrending) degree. Defaults to 1 (linear detrending)

  • extrapolation ({‘constant’, ‘nan’}) – The type of extrapolation to use. See xclim.sdba.utils.extrapolate_qm() for details. Defaults to “constant”.

References

Cannon, Sobie, and Murdock [2015]

class xclim.sdba.adjustment.EmpiricalQuantileMapping(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.TrainAdjust

Empirical Quantile Mapping bias-adjustment.

Adjustment factors are computed between the quantiles of ref and sim. Values of sim are matched to the corresponding quantiles of hist and corrected accordingly.

\[F^{-1}_{ref} (F_{hist}(sim))\]

where \(F\) is the cumulative distribution function (CDF) and mod stands for model data.

Variables:
  • step (Adjust) –

  • nquantiles (int or 1d array of floats) – The number of quantiles to use. Two endpoints at 1e-6 and 1 - 1e-6 will be added. An array of quantiles [0, 1] can also be passed. Defaults to 20 quantiles.

  • kind ({'+', '*'}) – The adjustment kind, either additive or multiplicative. Defaults to “+”.

  • group (Union[str, Grouper]) – The grouping information. See xclim.sdba.base.Grouper for details. Default is “time”, meaning an single adjustment group along dimension “time”.

  • adapt_freq_thresh (str | None) – Threshold for frequency adaptation. See xclim.sdba.processing.adapt_freq for details. Default is None, meaning that frequency adaptation is not performed.

  • step

  • interp ({'nearest', 'linear', 'cubic'}) – The interpolation method to use when interpolating the adjustment factors. Defaults to “nearest”.

  • extrapolation ({'constant', 'nan'}) – The type of extrapolation to use. See xclim.sdba.utils.extrapolate_qm() for details. Defaults to “constant”.

References

Déqué [2007]

class xclim.sdba.adjustment.ExtremeValues(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.TrainAdjust

Adjustment correction for extreme values.

The tail of the distribution of adjusted data is corrected according to the bias between the parametric Generalized Pareto distributions of the simulated and reference data [Roy et al., 2023]. The distributions are composed of the maximal values of clusters of “large” values. With “large” values being those above cluster_thresh. Only extreme values, whose quantile within the pool of large values are above q_thresh, are re-adjusted. See Notes.

This adjustment method should be considered experimental and used with care.

Parameters:
  • Train step

  • cluster_thresh (Quantity (str with units)) – The threshold value for defining clusters.

  • q_thresh (float) – The quantile of “extreme” values, [0, 1[. Defaults to 0.95.

  • ref_params (xr.DataArray, optional) – Distribution parameters to use instead of fitting a GenPareto distribution on ref.

  • Adjust step

  • scen (DataArray) – This is a second-order adjustment, so the adjust method needs the first-order adjusted timeseries in addition to the raw “sim”.

  • interp ({‘nearest’, ‘linear’, ‘cubic’}) – The interpolation method to use when interpolating the adjustment factors. Defaults to “linear”.

  • extrapolation ({‘constant’, ‘nan’}) – The type of extrapolation to use. See extrapolate_qm() for details. Defaults to “constant”.

  • frac (float) – Fraction where the cutoff happens between the original scen and the corrected one. See Notes, ]0, 1]. Defaults to 0.25.

  • power (float) – Shape of the correction strength, see Notes. Defaults to 1.0.

Notes

Extreme values are extracted from ref, hist and sim by finding all “clusters”, i.e. runs of consecutive values above cluster_thresh. The q_thresh`th percentile of these values is taken on `ref and hist and becomes thresh, the extreme value threshold. The maximal value of each cluster, if it exceeds that new threshold, is taken and Generalized Pareto distributions are fitted to them, for both ref and hist. The probabilities associated with each of these extremes in hist is used to find the corresponding value according to ref’s distribution. Adjustment factors are computed as the bias between those new extremes and the original ones.

In the adjust step, a Generalized Pareto distributions is fitted on the cluster-maximums of sim and it is used to associate a probability to each extreme, values over the thresh compute in the training, without the clustering. The adjustment factors are computed by interpolating the trained ones using these probabilities and the probabilities computed from hist.

Finally, the adjusted values (\(C_i\)) are mixed with the pre-adjusted ones (scen, \(D_i\)) using the following transition function:

\[V_i = C_i * \tau + D_i * (1 - \tau)\]

Where \(\tau\) is a function of sim’s extreme values (unadjusted, \(S_i\)) and of arguments frac (\(f\)) and power (\(p\)):

\[\tau = \left(\frac{1}{f}\frac{S - min(S)}{max(S) - min(S)}\right)^p\]

Code based on an internal Matlab source and partly ib the biascorrect_extremes function of the julia package “ClimateTools.jl” [Roy et al., 2021].

Because of limitations imposed by the lazy computing nature of the dask backend, it is not possible to know the number of cluster extremes in ref and hist at the moment the output data structure is created. This is why the code tries to estimate that number and usually overestimates it. In the training dataset, this translated into a quantile dimension that is too large and variables af and px_hist are assigned NaNs on extra elements. This has no incidence on the calculations themselves but requires more memory than is useful.

References

Roy, Smith, Kelman, Nolet-Gravel, Saba, Thomet, TagBot, and Forget [2021] Roy, Rondeau-Genesse, Jalbert, and Fournier [2023]

class xclim.sdba.adjustment.LOCI(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.TrainAdjust

Local Intensity Scaling (LOCI) bias-adjustment.

This bias adjustment method is designed to correct daily precipitation time series by considering wet and dry days separately [Schmidli et al., 2006].

Multiplicative adjustment factors are computed such that the mean of hist matches the mean of ref for values above a threshold.

The threshold on the training target ref is first mapped to hist by finding the quantile in hist having the same exceedance probability as thresh in ref. The adjustment factor is then given by

\[s = \frac{\left \langle ref: ref \geq t_{ref} \right\rangle - t_{ref}}{\left \langle hist : hist \geq t_{hist} \right\rangle - t_{hist}}\]

In the case of precipitations, the adjustment factor is the ratio of wet-days intensity.

For an adjustment factor s, the bias-adjustment of sim is:

\[sim(t) = \max\left(t_{ref} + s \cdot (hist(t) - t_{hist}), 0\right)\]
Variables:
  • step (Adjust) –

  • group (Union[str, Grouper]) – The grouping information. See xclim.sdba.base.Grouper for details. Default is “time”, meaning a single adjustment group along dimension “time”.

  • thresh (str) – The threshold in ref above which the values are scaled.

  • step

  • interp ({'nearest', 'linear', 'cubic'}) – The interpolation method to use then interpolating the adjustment factors. Defaults to “linear”.

References

Schmidli, Frei, and Vidale [2006]

class xclim.sdba.adjustment.NpdfTransform(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.Adjust

N-dimensional probability density function transform.

This adjustment object combines both training and adjust steps in the adjust class method.

A multivariate bias-adjustment algorithm described by Cannon [2018], as part of the MBCn algorithm, based on a color-correction algorithm described by Pitie et al. [2005].

This algorithm in itself, when used with QuantileDeltaMapping, is NOT trend-preserving. The full MBCn algorithm includes a reordering step provided here by xclim.sdba.processing.reordering().

See notes for an explanation of the algorithm.

Parameters:
  • base (BaseAdjustment) – An univariate bias-adjustment class. This is untested for anything else than QuantileDeltaMapping.

  • base_kws (dict, optional) – Arguments passed to the training of the univariate adjustment.

  • n_escore (int) – The number of elements to send to the escore function. The default, 0, means all elements are included. Pass -1 to skip computing the escore completely. Small numbers result in less significant scores, but the execution time goes up quickly with large values.

  • n_iter (int) – The number of iterations to perform. Defaults to 20.

  • pts_dim (str) – The name of the “multivariate” dimension. Defaults to “multivar”, which is the normal case when using xclim.sdba.base.stack_variables().

  • adj_kws (dict, optional) – Dictionary of arguments to pass to the adjust method of the univariate adjustment.

  • rot_matrices (xr.DataArray, optional) – The rotation matrices as a 3D array (‘iterations’, <pts_dim>, <anything>), with shape (n_iter, <N>, <N>). If left empty, random rotation matrices will be automatically generated.

Notes

The historical reference (\(T\), for “target”), simulated historical (\(H\)) and simulated projected (\(S\)) datasets are constructed by stacking the timeseries of N variables together. The algorithm is broken into the following steps:

  1. Rotate the datasets in the N-dimensional variable space with \(\mathbf{R}\), a random rotation NxN matrix.

\[\tilde{\mathbf{T}} = \mathbf{T}\mathbf{R} \ \tilde{\mathbf{H}} = \mathbf{H}\mathbf{R} \ \tilde{\mathbf{S}} = \mathbf{S}\mathbf{R}\]

2. An univariate bias-adjustment \(\mathcal{F}\) is used on the rotated datasets. The adjustments are made in additive mode, for each variable \(i\).

\[\hat{\mathbf{H}}_i, \hat{\mathbf{S}}_i = \mathcal{F}\left(\tilde{\mathbf{T}}_i, \tilde{\mathbf{H}}_i, \tilde{\mathbf{S}}_i\right)\]
  1. The bias-adjusted datasets are rotated back.

\[\begin{split}\mathbf{H}' = \hat{\mathbf{H}}\mathbf{R} \\ \mathbf{S}' = \hat{\mathbf{S}}\mathbf{R}\end{split}\]

These three steps are repeated a certain number of times, prescribed by argument n_iter. At each iteration, a new random rotation matrix is generated.

The original algorithm [Pitie et al., 2005], stops the iteration when some distance score converges. Following cite:t:sdba-cannon_multivariate_2018 and the MBCn implementation in Cannon [2020], we instead fix the number of iterations.

As done by cite:t:sdba-cannon_multivariate_2018, the distance score chosen is the “Energy distance” from Szekely and Rizzo [2004]. (see: xclim.sdba.processing.escore()).

The random matrices are generated following a method laid out by Mezzadri [2007].

This is only part of the full MBCn algorithm, see Statistical Downscaling and Bias-Adjustment for an example on how to replicate the full method with xclim. This includes a standardization of the simulated data beforehand, an initial univariate adjustment and the reordering of those adjusted series according to the rank structure of the output of this algorithm.

References

Cannon [2018], Cannon [2020], Mezzadri [2007], Pitie, Kokaram, and Dahyot [2005], Szekely and Rizzo [2004]

class xclim.sdba.adjustment.PrincipalComponents(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.TrainAdjust

Principal component adjustment.

This bias-correction method maps model simulation values to the observation space through principal components [Hnilica et al., 2017]. Values in the simulation space (multiple variables, or multiple sites) can be thought of as coordinate along axes, such as variable, temperature, etc. Principal components (PC) are a linear combinations of the original variables where the coefficients are the eigenvectors of the covariance matrix. Values can then be expressed as coordinates along the PC axes. The method makes the assumption that bias-corrected values have the same coordinates along the PC axes of the observations. By converting from the observation PC space to the original space, we get bias corrected values. See Notes for a mathematical explanation.

Warning

Be aware that principal components is meant here as the algebraic operation defining a coordinate system based on the eigenvectors, not statistical principal component analysis.

Variables:
  • group (Union[str, Grouper]) – The main dimension and grouping information. See Notes. See xclim.sdba.base.Grouper for details. The adjustment will be performed on each group independently. Default is “time”, meaning a single adjustment group along dimension “time”.

  • best_orientation ({'simple', 'full'}) – Which method to use when searching for the best principal component orientation. See best_pc_orientation_simple() and best_pc_orientation_full(). “full” is more precise, but it is much slower.

  • crd_dim (str) – The data dimension along which the multiple simulation space dimensions are taken. For a multivariate adjustment, this usually is “multivar”, as returned by sdba.stack_variables. For a multisite adjustment, this should be the spatial dimension. The training algorithm currently doesn’t support any chunking along either crd_dim. group.dim and group.add_dims.

Notes

The input data is understood as a set of N points in a \(M\)-dimensional space.

  • \(M\) is taken along crd_dim.

  • \(N\) is taken along the dimensions given through group : (the main dim but also, if requested, the add_dims and window).

The principal components (PC) of hist and ref are used to defined new coordinate systems, centered on their respective means. The training step creates a matrix defining the transformation from hist to ref:

\[scen = e_{R} + \mathrm{\mathbf{T}}(sim - e_{H})\]

Where:

\[\mathrm{\mathbf{T}} = \mathrm{\mathbf{R}}\mathrm{\mathbf{H}}^{-1}\]

\(\mathrm{\mathbf{R}}\) is the matrix transforming from the PC coordinates computed on ref to the data coordinates. Similarly, \(\mathrm{\mathbf{H}}\) is transform from the hist PC to the data coordinates (\(\mathrm{\mathbf{H}}\) is the inverse transformation). \(e_R\) and \(e_H\) are the centroids of the ref and hist distributions respectively. Upon running the adjust step, one may decide to use \(e_S\), the centroid of the sim distribution, instead of \(e_H\).

References

Alavoine and Grenier [2022], Hnilica, Hanel, and Puš [2017]

class xclim.sdba.adjustment.QuantileDeltaMapping(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.EmpiricalQuantileMapping

Quantile Delta Mapping bias-adjustment.

Adjustment factors are computed between the quantiles of ref and hist. Quantiles of sim are matched to the corresponding quantiles of hist and corrected accordingly.

\[sim\frac{F^{-1}_{ref}\left[F_{sim}(sim)\right]}{F^{-1}_{hist}\left[F_{sim}(sim)\right]}\]

where \(F\) is the cumulative distribution function (CDF). This equation is valid for multiplicative adjustment. The algorithm is based on the “QDM” method of [Cannon et al., 2015].

Parameters:
  • Train step

  • nquantiles (int or 1d array of floats) – The number of quantiles to use. See equally_spaced_nodes(). An array of quantiles [0, 1] can also be passed. Defaults to 20 quantiles.

  • kind ({‘+’, ‘’}*) – The adjustment kind, either additive or multiplicative. Defaults to “+”.

  • group (Union[str, Grouper]) – The grouping information. See xclim.sdba.base.Grouper for details. Default is “time”, meaning a single adjustment group along dimension “time”.

  • Adjust step

  • interp ({‘nearest’, ‘linear’, ‘cubic’}) – The interpolation method to use when interpolating the adjustment factors. Defaults to “nearest”.

  • extrapolation ({‘constant’, ‘nan’}) – The type of extrapolation to use. See xclim.sdba.utils.extrapolate_qm() for details. Defaults to “constant”.

  • Extra diagnostics

  • —————–

  • In adjustment

  • quantiles (The quantile of each value of sim. The adjustment factor is interpolated using this as the “quantile” axis on ds.af.)

References

Cannon, Sobie, and Murdock [2015]

class xclim.sdba.adjustment.Scaling(*args, _trained=False, **kwargs)[source]

Bases: xclim.sdba.adjustment.TrainAdjust

Scaling bias-adjustment.

Simple bias-adjustment method scaling variables by an additive or multiplicative factor so that the mean of hist matches the mean of ref.

Parameters:
  • Train step

  • group (Union[str, Grouper]) – The grouping information. See xclim.sdba.base.Grouper for details. Default is “time”, meaning an single adjustment group along dimension “time”.

  • kind ({‘+’, ‘’}*) – The adjustment kind, either additive or multiplicative. Defaults to “+”.

  • Adjust step

  • interp ({‘nearest’, ‘linear’, ‘cubic’}) – The interpolation method to use then interpolating the adjustment factors. Defaults to “nearest”.

Pre- and Post-Processing Submodule

xclim.sdba.processing.adapt_freq(ref, sim, *, group, thresh='0 mm d-1')[source]

Adapt frequency of values under thresh of sim, in order to match ref.

This is useful when the dry-day frequency in the simulations is higher than in the references. This function will create new non-null values for sim/hist, so that adjustment factors are less wet-biased. Based on Themeßl et al. [2012].

Parameters:
  • ref (xr.Dataset) – Target/reference data, usually observed data, with a “time” dimension.

  • sim (xr.Dataset) – Simulated data, with a “time” dimension.

  • group (str or Grouper) – Grouping information, see base.Grouper

  • thresh (str) – Threshold below which values are considered zero, a quantity with units.

Return type:

tuple[DataArray, DataArray, DataArray]

Returns:

  • sim_adj (xr.DataArray) – Simulated data with the same frequency of values under threshold than ref. Adjustment is made group-wise.

  • pth (xr.DataArray) – For each group, the smallest value of sim that was not frequency-adjusted. All values smaller were either left as zero values or given a random value between thresh and pth. NaN where frequency adaptation wasn’t needed.

  • dP0 (xr.DataArray) – For each group, the percentage of values that were corrected in sim.

Notes

With \(P_0^r\) the frequency of values under threshold \(T_0\) in the reference (ref) and \(P_0^s\) the same for the simulated values, \(\\Delta P_0 = \\frac{P_0^s - P_0^r}{P_0^s}\), when positive, represents the proportion of values under \(T_0\) that need to be corrected.

The correction replaces a proportion \(\\Delta P_0\) of the values under \(T_0\) in sim by a uniform random number between \(T_0\) and \(P_{th}\), where \(P_{th} = F_{ref}^{-1}( F_{sim}( T_0 ) )\) and F(x) is the empirical cumulative distribution function (CDF).

References

Themeßl, Gobiet, and Heinrich [2012]

xclim.sdba.processing.construct_moving_yearly_window(da, window=21, step=1, dim='movingwin')[source]

Deprecated function.

Use xclim.core.calendar.stack_periods() instead, renaming step to stride. Beware of the different default value for dim (“period”).

xclim.sdba.processing.escore(tgt, sim, dims=('variables', 'time'), N=0, scale=False)[source]

Energy score, or energy dissimilarity metric, based on Szekely and Rizzo [2004] and Cannon [2018].

Parameters:
  • tgt (xr.DataArray) – Target observations.

  • sim (xr.DataArray) – Candidate observations. Must have the same dimensions as tgt.

  • dims (sequence of 2 strings) – The name of the dimensions along which the variables and observation points are listed. tgt and sim can have different length along the second one, but must be equal along the first one. The result will keep all other dimensions.

  • N (int) – If larger than 0, the number of observations to use in the score computation. The points are taken evenly distributed along obs_dim.

  • scale (bool) – Whether to scale the data before computing the score. If True, both arrays as scaled according to the mean and standard deviation of tgt along obs_dim. (std computed with ddof=1 and both statistics excluding NaN values).

Return type:

DataArray

Returns:

xr.DataArray – e-score with dimensions not in dims.

Notes

Explanation adapted from the “energy” R package documentation. The e-distance between two clusters \(C_i\), \(C_j\) (tgt and sim) of size \(n_i,n_j\) proposed by Szekely and Rizzo [2004] is defined by:

\[e(C_i,C_j) = \frac{1}{2}\frac{n_i n_j}{n_i + n_j} \left[2 M_{ij} − M_{ii} − M_{jj}\right]\]

where

\[M_{ij} = \frac{1}{n_i n_j} \sum_{p = 1}^{n_i} \sum_{q = 1}^{n_j} \left\Vert X_{ip} − X{jq} \right\Vert.\]

\(\Vert\cdot\Vert\) denotes Euclidean norm, \(X_{ip}\) denotes the p-th observation in the i-th cluster.

The input scaling and the factor \(\frac{1}{2}\) in the first equation are additions of Cannon [2018] to the metric. With that factor, the test becomes identical to the one defined by Baringhaus and Franz [2004]. This version is tested against values taken from Alex Cannon’s MBC R package [Cannon, 2020].

References

Baringhaus and Franz [2004], Cannon [2018], Cannon [2020], Szekely and Rizzo [2004]

xclim.sdba.processing.from_additive_space(data, lower_bound=None, upper_bound=None, trans=None, units=None)[source]

Transform back to the physical space a variable that was transformed with to_additive_space.

Based on Alavoine and Grenier [2022]. If parameters are not present on the attributes of the data, they must be all given are arguments.

Parameters:
  • data (xr.DataArray) – A variable that was transformed by to_additive_space().

  • lower_bound (str, optional) – The smallest physical value of the variable, as a Quantity string. The final data will have no value smaller or equal to this bound. If None (default), the sdba_transform_lower attribute is looked up on data.

  • upper_bound (str, optional) – The largest physical value of the variable, as a Quantity string. Only relevant for the logit transformation. The final data will have no value larger or equal to this bound. If None (default), the sdba_transform_upper attribute is looked up on data.

  • trans ({‘log’, ‘logit’}, optional) – The transformation to use. See notes. If None (the default), the sdba_transform attribute is looked up on data.

  • units (str, optional) – The units of the data before transformation to the additive space. If None (the default), the sdba_transform_units attribute is looked up on data.

Returns:

xr.DataArray – The physical variable. Attributes are conserved, even if some might be incorrect. Except units which are taken from sdba_transform_units if available. All sdba_transform* attributes are deleted.

Notes

Given a variable that is not usable in an additive adjustment, to_additive_space() applied a transformation to a space where additive methods are sensible. Given \(Y\) the transformed variable, \(b_-\) the lower physical bound of that variable and \(b_+\) the upper physical bound, two back-transformations are currently implemented to get \(X\), the physical variable.

  • log

    \[X = e^{Y} + b_-\]
  • logit

    \[X' = \frac{1}{1 + e^{-Y}} X = X * (b_+ - b_-) + b_-\]

See also

to_additive_space

for the original transformation.

References

Alavoine and Grenier [2022]

xclim.sdba.processing.jitter(x, lower=None, upper=None, minimum=None, maximum=None)[source]

Replace values under a threshold and values above another by a uniform random noise.

Warning

Not to be confused with R’s jitter, which adds uniform noise instead of replacing values.

Parameters:
  • x (xr.DataArray) – Values.

  • lower (str, optional) – Threshold under which to add uniform random noise to values, a quantity with units. If None, no jittering is performed on the lower end.

  • upper (str, optional) – Threshold over which to add uniform random noise to values, a quantity with units. If None, no jittering is performed on the upper end.

  • minimum (str, optional) – Lower limit (excluded) for the lower end random noise, a quantity with units. If None but lower is not None, 0 is used.

  • maximum (str, optional) – Upper limit (excluded) for the upper end random noise, a quantity with units. If upper is not None, it must be given.

Return type:

DataArray

Returns:

xr.DataArray – Same as x but values < lower are replaced by a uniform noise in range (minimum, lower) and values >= upper are replaced by a uniform noise in range [upper, maximum). The two noise distributions are independent.

xclim.sdba.processing.jitter_over_thresh(x, thresh, upper_bnd)[source]

Replace values greater than threshold by a uniform random noise.

Warning

Not to be confused with R’s jitter, which adds uniform noise instead of replacing values.

Parameters:
  • x (xr.DataArray) – Values.

  • thresh (str) – Threshold over which to add uniform random noise to values, a quantity with units.

  • upper_bnd (str) – Maximum possible value for the random noise, a quantity with units.

Return type:

DataArray

Returns:

xr.DataArray

Notes

If thresh is low, this will change the mean value of x.

xclim.sdba.processing.jitter_under_thresh(x, thresh)[source]

Replace values smaller than threshold by a uniform random noise.

Warning

Not to be confused with R’s jitter, which adds uniform noise instead of replacing values.

Parameters:
  • x (xr.DataArray) – Values.

  • thresh (str) – Threshold under which to add uniform random noise to values, a quantity with units.

Return type:

DataArray

Returns:

xr.DataArray

Notes

If thresh is high, this will change the mean value of x.

xclim.sdba.processing.normalize(data, norm=None, *, group, kind='+')[source]

Normalize an array by removing its mean.

Normalization if performed group-wise and according to kind.

Parameters:
  • data (xr.DataArray) – The variable to normalize.

  • norm (xr.DataArray, optional) – If present, it is used instead of computing the norm again.

  • group (str or Grouper) – Grouping information. See xclim.sdba.base.Grouper for details..

  • kind ({‘+’, ‘’}*) – If kind is “+”, the mean is subtracted from the mean and if it is ‘*’, it is divided from the data.

Return type:

tuple[DataArray, DataArray]

Returns:

  • xr.DataArray – Groupwise anomaly.

  • norm (xr.DataArray) – Mean over each group.

xclim.sdba.processing.reordering(ref, sim, group='time')[source]

Reorders data in sim following the order of ref.

The rank structure of ref is used to reorder the elements of sim along dimension “time”, optionally doing the operation group-wise.

Parameters:
  • sim (xr.DataArray) – Array to reorder.

  • ref (xr.DataArray) – Array whose rank order sim should replicate.

  • group (str) – Grouping information. See xclim.sdba.base.Grouper for details.

Return type:

Dataset

Returns:

xr.Dataset – sim reordered according to ref’s rank order.

References

Cannon [2018]

xclim.sdba.processing.stack_variables(ds, rechunk=True, dim='multivar')[source]

Stack different variables of a dataset into a single DataArray with a new “variables” dimension.

Variable attributes are all added as lists of attributes to the new coordinate, prefixed with “_”. Variables are concatenated in the new dimension in alphabetical order, to ensure coherent behaviour with different datasets.

Parameters:
  • ds (xr.Dataset) – Input dataset.

  • rechunk (bool) – If True (default), dask arrays are rechunked with variables : -1.

  • dim (str) – Name of dimension along which variables are indexed.

Returns:

xr.DataArray – The transformed variable. Attributes are conserved, even if some might be incorrect, except for units, which are replaced with “”. Old units are stored in sdba_transformation_units. A sdba_transform attribute is added, set to the transformation method. sdba_transform_lower and sdba_transform_upper are also set if the requested bounds are different from the defaults.

Array with variables stacked along dim dimension. Units are set to “”.

xclim.sdba.processing.standardize(da, mean=None, std=None, dim='time')[source]

Standardize a DataArray by centering its mean and scaling it by its standard deviation.

Either of both of mean and std can be provided if need be.

Return type:

tuple[DataArray | Dataset, DataArray, DataArray]

Returns:

  • out (xr.DataArray or xr.Dataset) – Standardized data.

  • mean (xr.DataArray) – Mean.

  • std (xr.DataArray) – Standard Deviation.

xclim.sdba.processing.to_additive_space(data, lower_bound, upper_bound=None, trans='log')[source]

Transform a non-additive variable into an additive space by the means of a log or logit transformation.

Based on Alavoine and Grenier [2022].

Parameters:
  • data (xr.DataArray) – A variable that can’t usually be bias-adjusted by additive methods.

  • lower_bound (str) – The smallest physical value of the variable, excluded, as a Quantity string. The data should only have values strictly larger than this bound.

  • upper_bound (str, optional) – The largest physical value of the variable, excluded, as a Quantity string. Only relevant for the logit transformation. The data should only have values strictly smaller than this bound.

  • trans ({‘log’, ‘logit’}) – The transformation to use. See notes.

Notes

Given a variable that is not usable in an additive adjustment, this applies a transformation to a space where additive methods are sensible. Given \(X\) the variable, \(b_-\) the lower physical bound of that variable and \(b_+\) the upper physical bound, two transformations are currently implemented to get \(Y\), the additive-ready variable. \(\ln\) is the natural logarithm.

  • log

    \[Y = \ln\left( X - b_- \right)\]

    Usually used for variables with only a lower bound, like precipitation (pr, prsn, etc) and daily temperature range (dtr). Both have a lower bound of 0.

  • logit

    \[X' = (X - b_-) / (b_+ - b_-) Y = \ln\left(\frac{X'}{1 - X'} \right)\]

    Usually used for variables with both a lower and a upper bound, like relative and specific humidity, cloud cover fraction, etc.

This will thus produce Infinity and NaN values where \(X == b_-\) or \(X == b_+\). We recommend using jitter_under_thresh() and jitter_over_thresh() to remove those issues.

See also

from_additive_space

for the inverse transformation.

jitter_under_thresh

Remove values exactly equal to the lower bound.

jitter_over_thresh

Remove values exactly equal to the upper bound.

References

Alavoine and Grenier [2022]

xclim.sdba.processing.uniform_noise_like(da, low=1e-06, high=0.001)[source]

Return a uniform noise array of the same shape as da.

Noise is uniformly distributed between low and high. Alternative method to jitter_under_thresh for avoiding zeroes.

Return type:

DataArray

xclim.sdba.processing.unpack_moving_yearly_window(da, dim='movingwin', append_ends=True)[source]

Deprecated function.

Use xclim.core.calendar.unstack_periods() instead. Beware of the different default value for dim (“period”). The new function always behaves like appends_ends=True.

xclim.sdba.processing.unstack_variables(da, dim=None)[source]

Unstack a DataArray created by stack_variables to a dataset.

Parameters:
  • da (xr.DataArray) – Array holding different variables along dim dimension.

  • dim (str, optional) – Name of dimension along which the variables are stacked. If not specified (default), dim is inferred from attributes of the coordinate.

Returns:

xr.Dataset – Dataset holding each variable in an individual DataArray.

xclim.sdba.processing.unstandardize(da, mean, std)[source]

Rescale a standardized array by performing the inverse operation of standardize.

Detrending Objects Utilities

class xclim.sdba.detrending.LoessDetrend(group='time', kind='+', f=0.2, niter=1, d=0, weights='tricube', equal_spacing=None, skipna=True)[source]

Bases: xclim.sdba.detrending.BaseDetrend

Detrend time series using a LOESS regression.

The fit is a piecewise linear regression. For each point, the contribution of all neighbors is weighted by a bell-shaped curve (gaussian) with parameters sigma (std). The x-coordinate of the DataArray is scaled to [0,1] before the regression is computed.

Variables:
  • group (str or Grouper) – The grouping information. See xclim.sdba.base.Grouper for details. The fit is performed along the group’s main dim.

  • kind ({'*', '+'}) – The way the trend is removed or added, either additive or multiplicative.

  • d ({0, 1}) – Order of the local regression. Only 0 and 1 currently implemented.

  • f (float) – Parameter controlling the span of the weights, between 0 and 1.

  • niter (int) – Number of robustness iterations to execute.

  • weights (["tricube", "gaussian"]) – Shape of the weighting function: “tricube” : a smooth top-hat like curve, f gives the span of non-zero values. “gaussian” : a gaussian curve, f gives the span for 95% of the values.

  • skipna (bool) – If True (default), missing values are not included in the loess trend computation and thus are not propagated. The output will have the same missing values as the input.

Notes

LOESS smoothing is computationally expensive. As it relies on a loop on gridpoints, it can be useful to use smaller than usual chunks. Moreover, it suffers from heavy boundary effects. As a rule of thumb, the outermost N * f/2 points should be considered dubious. (N is the number of points along each group)

class xclim.sdba.detrending.MeanDetrend(*, group='time', kind='+', **kwargs)[source]

Bases: xclim.sdba.detrending.BaseDetrend

Simple detrending removing only the mean from the data, quite similar to normalizing.

class xclim.sdba.detrending.NoDetrend(*, group='time', kind='+', **kwargs)[source]

Bases: xclim.sdba.detrending.BaseDetrend

Convenience class for polymorphism. Does nothing.

class xclim.sdba.detrending.PolyDetrend(group='time', kind='+', degree=4, preserve_mean=False)[source]

Bases: xclim.sdba.detrending.BaseDetrend

Detrend time series using a polynomial regression.

Variables:
  • group (Union[str, Grouper]) – The grouping information. See xclim.sdba.base.Grouper for details. The fit is performed along the group’s main dim.

  • kind ({'*', '+'}) – The way the trend is removed or added, either additive or multiplicative.

  • degree (int) – The order of the polynomial to fit.

  • preserve_mean (bool) – Whether to preserve the mean when de/re-trending. If True, the trend has its mean removed before it is used.

class xclim.sdba.detrending.RollingMeanDetrend(group='time', kind='+', win=30, weights=None, min_periods=None)[source]

Bases: xclim.sdba.detrending.BaseDetrend

Detrend time series using a rolling mean.

Variables:
  • group (str or Grouper) – The grouping information. See xclim.sdba.base.Grouper for details. The fit is performed along the group’s main dim.

  • kind ({'*', '+'}) – The way the trend is removed or added, either additive or multiplicative.

  • win (int) – The size of the rolling window. Units are the steps of the grouped data, which means this detrending is best use with either group=’time’ or group=’time.dayofyear’. Other grouping will have large jumps included within the windows and :py`:class:LoessDetrend might offer a better solution.

  • weights (sequence of floats, optional) – Sequence of length win. Defaults to None, which means a flat window.

  • min_periods (int, optional) – Minimum number of observations in window required to have a value, otherwise the result is NaN. See xarray.DataArray.rolling(). Defaults to None, which sets it equal to win. Setting both weights and this is not implemented yet.

Notes

As for the LoessDetrend detrending, important boundary effects are to be expected.

Statistical Downscaling and Bias Adjustment Utilities

xclim.sdba.utils.add_cyclic_bounds(da, att, cyclic_coords=True)[source]

Reindex an array to include the last slice at the beginning and the first at the end.

This is done to allow interpolation near the end-points.

Parameters:
  • da (xr.DataArray or xr.Dataset) – An array

  • att (str) – The name of the coordinate to make cyclic

  • cyclic_coords (bool) – If True, the coordinates are made cyclic as well, if False, the new values are guessed using the same step as their neighbour.

Return type:

DataArray | Dataset

Returns:

xr.DataArray or xr.Dataset – da but with the last element along att prepended and the last one appended.

xclim.sdba.utils.apply_correction(x, factor, kind=None)[source]

Apply the additive or multiplicative correction/adjustment factors.

If kind is not given, default to the one stored in the “kind” attribute of factor.

Return type:

DataArray

xclim.sdba.utils.best_pc_orientation_full(R, Hinv, Rmean, Hmean, hist)[source]

Return best orientation vector for A according to the method of Alavoine and Grenier [2022].

Eigenvectors returned by pc_matrix do not have a defined orientation. Given an inverse transform Hinv, a transform R, the actual and target origins Hmean and Rmean and the matrix of training observations hist, this computes a scenario for all possible orientations and return the orientation that maximizes the Spearman correlation coefficient of all variables. The correlation is computed for each variable individually, then averaged.

This trick is explained in Alavoine and Grenier [2022]. See docstring of sdba.adjustment.PrincipalComponentAdjustment().

Parameters:
  • R (np.ndarray) – MxM Matrix defining the final transformation.

  • Hinv (np.ndarray) – MxM Matrix defining the (inverse) first transformation.

  • Rmean (np.ndarray) – M vector defining the target distribution center point.

  • Hmean (np.ndarray) – M vector defining the original distribution center point.

  • hist (np.ndarray) – MxN matrix of all training observations of the M variables/sites.

Return type:

ndarray

Returns:

np.ndarray – M vector of orientation correction (1 or -1).

References

Alavoine and Grenier [2022]

See also

sdba.adjustment.PrincipalComponentAdjustment

xclim.sdba.utils.best_pc_orientation_simple(R, Hinv, val=1000)[source]

Return best orientation vector according to a simple test.

Eigenvectors returned by pc_matrix do not have a defined orientation. Given an inverse transform Hinv and a transform R, this returns the orientation minimizing the projected distance for a test point far from the origin.

This trick is inspired by the one exposed in Hnilica et al. [2017]. For each possible orientation vector, the test point is reprojected and the distance from the original point is computed. The orientation minimizing that distance is chosen.

Parameters:
  • R (np.ndarray) – MxM Matrix defining the final transformation.

  • Hinv (np.ndarray) – MxM Matrix defining the (inverse) first transformation.

  • val (float) – The coordinate of the test point (same for all axes). It should be much greater than the largest furthest point in the array used to define B.

Return type:

ndarray

Returns:

np.ndarray – Mx1 vector of orientation correction (1 or -1).

See also

sdba.adjustment.PrincipalComponentAdjustment

References

Hnilica, Hanel, and Puš [2017]

xclim.sdba.utils.broadcast(grouped, x, *, group='time', interp='nearest', sel=None)[source]

Broadcast a grouped array back to the same shape as a given array.

Parameters:
  • grouped (xr.DataArray) – The grouped array to broadcast like x.

  • x (xr.DataArray) – The array to broadcast grouped to.

  • group (str or Grouper) – Grouping information. See xclim.sdba.base.Grouper for details.

  • interp ({‘nearest’, ‘linear’, ‘cubic’}) – The interpolation method to use,

  • sel (dict[str, xr.DataArray]) – Mapping of grouped coordinates to x coordinates (other than the grouping one).

Return type:

DataArray

Returns:

xr.DataArray

xclim.sdba.utils.copy_all_attrs(ds, ref)[source]

Copy all attributes of ds to ref, including attributes of shared coordinates, and variables in the case of Datasets.

xclim.sdba.utils.ecdf(x, value, dim='time')[source]

Return the empirical CDF of a sample at a given value.

Parameters:
  • x (array) – Sample.

  • value (float) – The value within the support of x for which to compute the CDF value.

  • dim (str) – Dimension name.

Return type:

DataArray

Returns:

xr.DataArray – Empirical CDF.

xclim.sdba.utils.ensure_longest_doy(func)[source]

Ensure that selected day is the longest day of year for x and y dims.

Return type:

Callable

xclim.sdba.utils.equally_spaced_nodes(n, eps=None)[source]

Return nodes with n equally spaced points within [0, 1], optionally adding two end-points.

Parameters:
  • n (int) – Number of equally spaced nodes.

  • eps (float, optional) – Distance from 0 and 1 of added end nodes. If None (default), do not add endpoints.

Return type:

ndarray

Returns:

np.array – Nodes between 0 and 1. Nodes can be seen as the middle points of n equal bins.

Warning

Passing a small eps will effectively clip the scenario to the bounds of the reference on the historical period in most cases. With normal quantile mapping algorithms, this can give strange result when the reference does not show as many extremes as the simulation does.

Notes

For n=4, eps=0 : 0—x——x——x——x—1

xclim.sdba.utils.get_clusters(data, u1, u2, dim='time')[source]

Get cluster count, maximum and position along a given dim.

See get_clusters_1d. Used by adjustment.ExtremeValues.

Parameters:
  • data (1D ndarray) – Values to get clusters from.

  • u1 (float) – Extreme value threshold, at least one value in the cluster must exceed this.

  • u2 (float) – Cluster threshold, values above this can be part of a cluster.

  • dim (str) – Dimension name.

Return type:

Dataset

Returns:

xr.Dataset

With variables,
  • nclusters : Number of clusters for each point (with dim reduced), int

  • start : First index in the cluster (dim reduced, new cluster), int

  • end : Last index in the cluster, inclusive (dim reduced, new cluster), int

  • maxpos : Index of the maximal value within the cluster (dim reduced, new cluster), int

  • maximum : Maximal value within the cluster (dim reduced, new cluster), same dtype as data.

For start, end and maxpos, -1 means NaN and should always correspond to a NaN in maximum. The length along cluster is half the size of “dim”, the maximal theoretical number of clusters.

xclim.sdba.utils.get_clusters_1d(data, u1, u2)[source]

Get clusters of a 1D array.

A cluster is defined as a sequence of values larger than u2 with at least one value larger than u1.

Parameters:
  • data (1D ndarray) – Values to get clusters from.

  • u1 (float) – Extreme value threshold, at least one value in the cluster must exceed this.

  • u2 (float) – Cluster threshold, values above this can be part of a cluster.

Return type:

tuple[ndarray, ndarray, ndarray, ndarray]

Returns:

(np.array, np.array, np.array, np.array)

References

getcluster of Extremes.jl (Jalbert [2022]).

xclim.sdba.utils.get_correction(x, y, kind)[source]

Return the additive or multiplicative correction/adjustment factors.

Return type:

DataArray

xclim.sdba.utils.interp_on_quantiles(newx, xq, yq, *, group='time', method='linear', extrapolation='constant')[source]

Interpolate values of yq on new values of x.

Interpolate in 2D with scipy.interpolate.griddata() if grouping is used, in 1D otherwise, with scipy.interpolate.interp1d. Any NaNs in xq or yq are removed from the input map. Similarly, NaNs in newx are left NaNs.

Parameters:
  • newx (xr.DataArray) – The values at which to evaluate yq. If group has group information, new should have a coordinate with the same name as the group name In that case, 2D interpolation is used.

  • xq, yq (xr.DataArray) – Coordinates and values on which to interpolate. The interpolation is done along the “quantiles” dimension if group has no group information. If it does, interpolation is done in 2D on “quantiles” and on the group dimension.

  • group (str or Grouper) – The dimension and grouping information. (ex: “time” or “time.month”). Defaults to “time”.

  • method ({‘nearest’, ‘linear’, ‘cubic’}) – The interpolation method.

  • extrapolation ({‘constant’, ‘nan’}) – The extrapolation method used for values of newx outside the range of xq. See notes.

Notes

Extrapolation methods:

  • ‘nan’ : Any value of newx outside the range of xq is set to NaN.

  • ‘constant’ : Values of newx smaller than the minimum of xq are set to the first value of yq and those larger than the maximum, set to the last one (first and last non-nan values along the “quantiles” dimension). When the grouping is “time.month”, these limits are linearly interpolated along the month dimension.

xclim.sdba.utils.invert(x, kind=None)[source]

Invert a DataArray either by addition (-x) or by multiplication (1/x).

If kind is not given, default to the one stored in the “kind” attribute of x.

Return type:

DataArray

xclim.sdba.utils.map_cdf(ds, *, y_value, dim)[source]

Return the value in x with the same CDF as y_value in y.

This function is meant to be wrapped in a Grouper.apply.

Parameters:
  • ds (xr.Dataset) – Variables: x, Values from which to pick, y, Reference values giving the ranking

  • y_value (float, array) – Value within the support of y.

  • dim (str) – Dimension along which to compute quantile.

Returns:

array – Quantile of x with the same CDF as y_value in y.

xclim.sdba.utils.map_cdf_1d(x, y, y_value)[source]

Return the value in x with the same CDF as y_value in y.

xclim.sdba.utils.pc_matrix(arr)[source]

Construct a Principal Component matrix.

This matrix can be used to transform points in arr to principal components coordinates. Note that this function does not manage NaNs; if a single observation is null, all elements of the transformation matrix involving that variable will be NaN.

Parameters:

arr (numpy.ndarray or dask.array.Array) – 2D array (M, N) of the M coordinates of N points.

Return type:

ndarray | Array

Returns:

numpy.ndarray or dask.array.Array – MxM Array of the same type as arr.

xclim.sdba.utils.rand_rot_matrix(crd, num=1, new_dim=None)[source]

Generate random rotation matrices.

Rotation matrices are members of the SO(n) group, where n is the matrix size (crd.size). They can be characterized as orthogonal matrices with determinant 1. A square matrix \(R\) is a rotation matrix if and only if \(R^t = R^{−1}\) and \(\mathrm{det} R = 1\).

Parameters:
  • crd (xr.DataArray) – 1D coordinate DataArray along which the rotation occurs. The output will be square with the same coordinate replicated, the second renamed to new_dim.

  • num (int) – If larger than 1 (default), the number of matrices to generate, stacked along a “matrices” dimension.

  • new_dim (str) – Name of the new “prime” dimension, defaults to the same name as crd + “_prime”.

Return type:

DataArray

Returns:

xr.DataArray – float, NxN if num = 1, numxNxN otherwise, where N is the length of crd.

References

Mezzadri [2007]

xclim.sdba.utils.rank(da, dim='time', pct=False)[source]

Ranks data along a dimension.

Replicates xr.DataArray.rank but as a function usable in a Grouper.apply(). Xarray’s docstring is below:

Equal values are assigned a rank that is the average of the ranks that would have been otherwise assigned to all the values within that set. Ranks begin at 1, not 0. If pct, computes percentage ranks, ranging from 0 to 1.

A list of dimensions can be provided and the ranks are then computed separately for each dimension.

Parameters:
  • da (xr.DataArray) – Source array.

  • dim (str | list[str], hashable) – Dimension(s) over which to compute rank.

  • pct (bool, optional) – If True, compute percentage ranks, otherwise compute integer ranks. Percentage ranks range from 0 to 1, in opposition to xarray’s implementation, where they range from 1/N to 1.

Return type:

DataArray

Returns:

DataArray – DataArray with the same coordinates and dtype ‘float64’.

Notes

The bottleneck library is required. NaNs in the input array are returned as NaNs.

See also

xarray.DataArray.rank

class xclim.sdba.base.Grouper(group, window=1, add_dims=None)[source]

Create the Grouper object.

Parameters:
  • group (str) – The usual grouping name as xarray understands it. Ex: “time.month” or “time”. The dimension name before the dot is the “main dimension” stored in Grouper.dim and the property name after is stored in Grouper.prop.

  • window (int) – If larger than 1, a centered rolling window along the main dimension is created when grouping data. Units are the sampling frequency of the data along the main dimension.

  • add_dims (Optional[Union[Sequence[str], str]]) – Additional dimensions that should be reduced in grouping operations. This behaviour is also controlled by the main_only parameter of the apply method. If any of these dimensions are absent from the DataArrays, they will be omitted.

apply(func, da, main_only=False, **kwargs)[source]

Apply a function group-wise on DataArrays.

Parameters:
  • func (Callable or str) – The function to apply to the groups, either a callable or a xr.core.groupby.GroupBy method name as a string. The function will be called as func(group, dim=dims, **kwargs). See main_only for the behaviour of dims.

  • da (xr.DataArray or dict[str, xr.DataArray] or xr.Dataset) – The DataArray on which to apply the function. Multiple arrays can be passed through a dictionary. A dataset will be created before grouping.

  • main_only (bool) – Whether to call the function with the main dimension only (if True) or with all grouping dims (if False, default) (including the window and dimensions given through add_dims). The dimensions used are also written in the “group_compute_dims” attribute. If all the input arrays are missing one of the ‘add_dims’, it is silently omitted.

  • **kwargs – Other keyword arguments to pass to the function.

Return type:

DataArray | Dataset

Returns:

xr.DataArray or xr.Dataset – Attributes “group”, “group_window” and “group_compute_dims” are added.

If the function did not reduce the array:

  • The output is sorted along the main dimension.

  • The output is rechunked to match the chunks on the input If multiple inputs with differing chunking were given as inputs, the chunking with the smallest number of chunks is used.

If the function reduces the array:

  • If there is only one group, the singleton dimension is squeezed out of the output

  • The output is rechunked as to have only 1 chunk along the new dimension.

Notes

For the special case where a Dataset is returned, but only some of its variable where reduced by the grouping, xarray’s GroupBy.map will broadcast everything back to the ungrouped dimensions. To overcome this issue, function may add a “_group_apply_reshape” attribute set to True on the variables that should be reduced and these will be re-grouped by calling da.groupby(self.name).first().

property freq

Format a frequency string corresponding to the group.

For use with xarray’s resampling functions.

classmethod from_kwargs(**kwargs)[source]

Parameterize groups using kwargs.

Return type:

dict[str, Grouper]

get_coordinate(ds=None)[source]

Return the coordinate as in the output of group.apply.

Currently, only implemented for groupings with prop == month or dayofyear. For prop == dayfofyear, a ds (Dataset or DataArray) can be passed to infer the max day of year from the available years and calendar.

Return type:

DataArray

get_index(da, interp=None)[source]

Return the group index of each element along the main dimension.

Parameters:
  • da (xr.DataArray or xr.Dataset) – The input array/dataset for which the group index is returned. It must have Grouper.dim as a coordinate.

  • interp (bool, optional) – If True, the returned index can be used for interpolation. Only value for month grouping, where integer values represent the middle of the month, all other days are linearly interpolated in between.

Return type:

DataArray

Returns:

xr.DataArray – The index of each element along Grouper.dim. If Grouper.dim is time and Grouper.prop is None, a uniform array of True is returned. If Grouper.prop is a time accessor (month, dayofyear, etc.), a numerical array is returned, with a special case of month and interp=True. If Grouper.dim is not time, the dim is simply returned.

group(da=None, main_only=False, **das)[source]

Return a xr.core.groupby.GroupBy object.

More than one array can be combined to a dataset before grouping using the das kwargs. A new window dimension is added if self.window is larger than 1. If Grouper.dim is ‘time’, but ‘prop’ is None, the whole array is grouped together.

When multiple arrays are passed, some of them can be grouped along the same group as self. They are broadcast, merged to the grouping dataset and regrouped in the output.

Return type:

GroupBy

property prop_name

Create a significant name for the grouping.

Numba-accelerated Utilities

xclim.sdba.nbutils.quantile(da, q, dim)[source]

Compute the quantiles from a fixed list q.

Return type:

DataArray

xclim.sdba.nbutils.remove_NaNs(x)[source]

Remove NaN values from series.

xclim.sdba.nbutils.vecquantiles(da, rnk, dim)[source]

For when the quantile (rnk) is different for each point.

da and rnk must share all dimensions but dim.

Return type:

DataArray

LOESS Smoothing Submodule

xclim.sdba.loess.loess_smoothing(da, dim='time', d=1, f=0.5, niter=2, weights='tricube', equal_spacing=None, skipna=True)[source]

Locally weighted regression in 1D: fits a nonparametric regression curve to a scatter plot.

Returns a smoothed curve along given dimension. The regression is computed for each point using a subset of neighbouring points as given from evaluating the weighting function locally. Follows the procedure of Cleveland [1979].

Parameters:
  • da (xr.DataArray) – The data to smooth using the loess approach.

  • dim (str) – Name of the dimension along which to perform the loess.

  • d ([0, 1]) – Degree of the local regression.

  • f (float) – Parameter controlling the shape of the weight curve. Behavior depends on the weighting function, but it usually represents the span of the weighting function in reference to x-coordinates normalized from 0 to 1.

  • niter (int) – Number of robustness iterations to execute.

  • weights ([“tricube”, “gaussian”] or callable) – Shape of the weighting function, see notes. The user can provide a function or a string: “tricube” : a smooth top-hat like curve. “gaussian” : a gaussian curve, f gives the span for 95% of the values.

  • equal_spacing (bool, optional) – Whether to use the equal spacing optimization. If None (the default), it is activated only if the x-axis is equally-spaced. When activated, dx = x[1] - x[0].

  • skipna (bool) – If True (default), skip missing values (as marked by NaN). The output will have the same missing values as the input.

Notes

As stated in Cleveland [1979], the weighting function \(W(x)\) should respect the following conditions:

  • \(W(x) > 0\) for \(|x| < 1\)

  • \(W(-x) = W(x)\)

  • \(W(x)\) is non-increasing for \(x \ge 0\)

  • \(W(x) = 0\) for \(|x| \ge 0\)

If a Callable is provided, it should only accept the 1D np.ndarray \(x\) which is an absolute value function going from 1 to 0 to 1 around \(x_i\), for all values where \(x - x_i < h_i\) with \(h_i\) the distance of the rth nearest neighbor of \(x_i\), \(r = f * size(x)\).

References

Cleveland [1979]

Code adapted from: Gramfort [2015]

Properties Submodule

SDBA diagnostic tests are made up of statistical properties and measures. Properties are calculated on both simulation and reference datasets. They collapse the time dimension to one value.

This framework for the diagnostic tests was inspired by the VALUE project. Statistical Properties is the xclim term for ‘indices’ in the VALUE project.

xclim.sdba.properties.acf(da='da', *, lag=1, group='time.season', ds=None)

Autocorrelation. (realm: generic)

Autocorrelation with a lag over a time resolution and averaged over all years.

Based on indice _acf().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • lag (number) – Lag. Default : 1.

  • group ({‘time.season’, ‘time.month’}) – Grouping of the output. E.g. If ‘time.month’, the autocorrelation is calculated over each month separately for all years. Then, the autocorrelation for all Jan/Feb/… is averaged over all years, giving 12 outputs for each grid point. Default : time.season.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

acf (DataArray) – Lag-{lag} autocorrelation of the variable over a {group.prop} and averaged over all years.

Return type:

xarray.DataArray

References

Alavoine and Grenier [2022]

xclim.sdba.properties.annual_cycle_amplitude(da='da', *, window=31, group='time', ds=None)

Annual cycle statistics. (realm: generic)

A daily climatology is calculated and optionally smoothed with a (circular) moving average. The requested statistic is returned.

Based on indice _annual_cycle(). With injected parameters: stat=absamp.

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

annual_cycle_amplitude (DataArray) – {stat} of the annual cycle., with additional attributes: cell_methods: time: mean time: range

Return type:

xarray.DataArray

xclim.sdba.properties.annual_cycle_asymmetry(da='da', *, window=31, group='time', ds=None)

Annual cycle statistics. (realm: generic)

A daily climatology is calculated and optionally smoothed with a (circular) moving average. The requested statistic is returned.

Based on indice _annual_cycle(). With injected parameters: stat=asymmetry.

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

annual_cycle_asymmetry (DataArray) – {stat} of the annual cycle. [yr]

Return type:

xarray.DataArray

xclim.sdba.properties.annual_cycle_maximum(da='da', *, window=31, group='time', ds=None)

Annual cycle statistics. (realm: generic)

A daily climatology is calculated and optionally smoothed with a (circular) moving average. The requested statistic is returned.

Based on indice _annual_cycle(). With injected parameters: stat=max.

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

annual_cycle_maximum (DataArray) – {stat} of the annual cycle., with additional attributes: cell_methods: time: mean time: max

Return type:

xarray.DataArray

xclim.sdba.properties.annual_cycle_minimum(da='da', *, window=31, group='time', ds=None)

Annual cycle statistics. (realm: generic)

A daily climatology is calculated and optionally smoothed with a (circular) moving average. The requested statistic is returned.

Based on indice _annual_cycle(). With injected parameters: stat=min.

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

annual_cycle_minimum (DataArray) – {stat} of the annual cycle., with additional attributes: cell_methods: time: mean time: min

Return type:

xarray.DataArray

xclim.sdba.properties.annual_cycle_phase(da='da', *, window=31, group='time', ds=None)

Annual cycle statistics. (realm: generic)

A daily climatology is calculated and optionally smoothed with a (circular) moving average. The requested statistic is returned.

Based on indice _annual_cycle(). With injected parameters: stat=phase.

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

annual_cycle_phase (DataArray) – {stat} of the annual cycle., with additional attributes: cell_methods: time: range

Return type:

xarray.DataArray

xclim.sdba.properties.corr_btw_var(da1='da1', da2='da2', *, corr_type='Spearman', output='correlation', group='time', ds=None)

Correlation between two variables. (realm: generic)

Spearman or Pearson correlation coefficient between two variables at the time resolution.

Based on indice _corr_btw_var().

Parameters:
  • da1 (str or DataArray) – First variable on which to calculate the diagnostic. Default : ds.da1.

  • da2 (str or DataArray) – Second variable on which to calculate the diagnostic. Default : ds.da2.

  • corr_type ({‘Pearson’, ‘Spearman’}) – Type of correlation to calculate. Default : Spearman.

  • output ({‘pvalue’, ‘correlation’}) – Whether to return the correlation coefficient or the p-value. Default : correlation.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. e.g. For ‘time.month’, the correlation would be calculated on each month separately, but with all the years together. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

corr_btw_var (DataArray) – {corr_type} correlation coefficient

Return type:

xarray.DataArray

xclim.sdba.properties.decorrelation_length(da='da', *, radius=300, thresh=0.5, dims=None, bins=100, group='time', ds=None)

Decorrelation length. (realm: generic)

Distance from a grid cell where the correlation with its neighbours goes below the threshold. A correlogram is calculated for each grid cell following the method from xclim.sdba.properties.spatial_correlogram. Then, we find the first bin closest to the correlation threshold.

Based on indice _decorrelation_length().

Parameters:
  • da (str or DataArray) – Data. Default : ds.da.

  • radius (number) – Radius (in km) defining the region where correlations will be calculated between a point and its neighbours. Default : 300.

  • thresh (number) – Threshold correlation defining decorrelation. The decorrelation length is defined as the center of the distance bin that has a correlation closest to this threshold. Default : 0.5.

  • dims (Any) – Name of the spatial dimensions. Once these are stacked, the longitude and latitude coordinates must be 1D. Default : None.

  • bins (number) – Same as argument bins from scipy.stats.binned_statistic(). If given as a scalar, the equal-width bin limits from 0 to radius are generated here (instead of letting scipy do it) to improve performance. Default : 100.

  • group (str) – Useless for now. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

decorrelation_length (DataArray) – Decorrelation length.

Return type:

xarray.DataArray

Notes

Calculating this property requires a lot of memory. It will not work with large datasets.

xclim.sdba.properties.first_eof()[source]

EOF Statistical Property (function removed).

Warning

Due to a licensing issue, eofs-based functionality has been permanently removed. Please excuse the inconvenience. For more information, see: https://github.com/Ouranosinc/xclim/issues/1620

xclim.sdba.properties.mean(da='da', *, group='time', ds=None)

Mean. (realm: generic)

Mean over all years at the time resolution.

Based on indice _mean().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. E.g. If ‘time.month’, the temporal average is performed separately for each month. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

mean (DataArray) – Mean of the variable., with additional attributes: cell_methods: time: mean

Return type:

xarray.DataArray

xclim.sdba.properties.mean_annual_phase(da='da', *, window=31, group='time', ds=None)

Annual range statistics. (realm: generic)

Compute a statistic on each year of data and return the interannual average. This is similar to the annual cycle, but with the statistic and average operations inverted.

Based on indice _annual_statistic(). With injected parameters: stat=phase.

Parameters:
  • da (str or DataArray) – Data. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

mean_annual_phase (DataArray) – Average annual {stat}.

Return type:

xarray.DataArray

xclim.sdba.properties.mean_annual_range(da='da', *, window=31, group='time', ds=None)

Annual range statistics. (realm: generic)

Compute a statistic on each year of data and return the interannual average. This is similar to the annual cycle, but with the statistic and average operations inverted.

Based on indice _annual_statistic(). With injected parameters: stat=absamp.

Parameters:
  • da (str or DataArray) – Data. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

mean_annual_range (DataArray) – Average annual {stat}.

Return type:

xarray.DataArray

xclim.sdba.properties.mean_annual_relative_range(da='da', *, window=31, group='time', ds=None)

Annual range statistics. (realm: generic)

Compute a statistic on each year of data and return the interannual average. This is similar to the annual cycle, but with the statistic and average operations inverted.

Based on indice _annual_statistic(). With injected parameters: stat=relamp.

Parameters:
  • da (str or DataArray) – Data. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

mean_annual_relative_range (DataArray) – Average annual {stat}. [%]

Return type:

xarray.DataArray

xclim.sdba.properties.quantile(da='da', *, q=0.98, group='time', ds=None)

Quantile. (realm: generic)

Returns the quantile q of the distribution of the variable over all years at the time resolution.

Based on indice _quantile().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • q (number) – Quantile to be calculated. Should be between 0 and 1. Default : 0.98.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. E.g. If ‘time.month’, the quantile is computed separately for each month. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

quantile (DataArray) – Quantile {q} of the variable.

Return type:

xarray.DataArray

xclim.sdba.properties.relative_annual_cycle_amplitude(da='da', *, window=31, group='time', ds=None)

Annual cycle statistics. (realm: generic)

A daily climatology is calculated and optionally smoothed with a (circular) moving average. The requested statistic is returned.

Based on indice _annual_cycle(). With injected parameters: stat=relamp.

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • window (number) – Size of the window for the moving average filtering. Deactivate this feature by passing window = 1. Default : 31.

  • group (str) – Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

relative_annual_cycle_amplitude (DataArray) – {stat} of the annual cycle. [%], with additional attributes: cell_methods: time: mean time: range

Return type:

xarray.DataArray

xclim.sdba.properties.relative_frequency(da='da', *, op='>=', thresh='1 mm d-1', group='time', ds=None)

Relative Frequency. (realm: generic)

Relative Frequency of days with variable respecting a condition (defined by an operation and a threshold) at the time resolution. The relative frequency is the number of days that satisfy the condition divided by the total number of days.

Based on indice _relative_frequency().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • op ({‘>=’, ‘>’, ‘<’, ‘<=’}) – Operation to verify the condition. The condition is variable {op} threshold. Default : >=.

  • thresh (str) – Threshold on which to evaluate the condition. Default : 1 mm d-1.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping on the output. Eg. For ‘time.month’, the relative frequency would be calculated on each month, with all years included. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

relative_frequency (DataArray) – Relative frequency of values {op} {thresh}.

Return type:

xarray.DataArray

xclim.sdba.properties.return_value(da='da', *, period=20, op='max', method='ML', group='time', ds=None)

Return value. (realm: generic)

Return the value corresponding to a return period. On average, the return value will be exceeded (or not exceed for op=’min’) every return period (e.g. 20 years). The return value is computed by first extracting the variable annual maxima/minima, fitting a statistical distribution to the maxima/minima, then estimating the percentile associated with the return period (eg. 95th percentile (1/20) for 20 years)

Based on indice _return_value().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • period (number) – Return period. Number of years over which to check if the value is exceeded (or not for op=’min’). Default : 20.

  • op ({‘min’, ‘max’}) – Whether we are looking for a probability of exceedance (‘max’, right side of the distribution) or a probability of non-exceedance (min, left side of the distribution). Default : max.

  • method ({‘ML’, ‘PWM’}) – Fitting method, either maximum likelihood (ML) or probability weighted moments (PWM), also called L-Moments. The PWM method is usually more robust to outliers. Default : ML.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. A distribution of the extremes is done for each group. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

return_value (DataArray) – {period}-{group.prop_name} {op} return level of the variable.

Return type:

xarray.DataArray

xclim.sdba.properties.skewness(da='da', *, group='time', ds=None)

Skewness. (realm: generic)

Skewness of the distribution of the variable over all years at the time resolution.

Based on indice _skewness().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. E.g. If ‘time.month’, the skewness is performed separately for each month. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

skewness (DataArray) – Skewness of the variable.

Return type:

xarray.DataArray

xclim.sdba.properties.spatial_correlogram(da='da', *, dims=None, bins=100, group='time', method=1, ds=None)

Spatial correlogram. (realm: generic)

Compute the pairwise spatial correlations (Spearman) and averages them based on the pairwise distances. This collapses the spatial and temporal dimensions and returns a distance bins dimension. Needs coordinates for longitude and latitude. This property is heavy to compute, and it will need to create a NxN array in memory (outside of dask), where N is the number of spatial points. There are shortcuts for all-nan time-slices or spatial points, but scipy’s nan-omitting algorithm is extremely slow, so the presence of any lone NaN will increase the computation time. Based on an idea from [François et al., 2020].

Based on indice _spatial_correlogram().

Parameters:
  • da (str or DataArray) – Data. Default : ds.da.

  • dims (Any) – Name of the spatial dimensions. Once these are stacked, the longitude and latitude coordinates must be 1D. Default : None.

  • bins (number) – Same as argument bins from xarray.DataArray.groupby_bins(). If given as a scalar, the equal-width bin limits are generated here (instead of letting xarray do it) to improve performance. Default : 100.

  • group (str) – Useless for now. Default : time.

  • method (number) – Default : 1.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

spatial_correlogram (DataArray) – Inter-site correlogram as a function of distance.

Return type:

xarray.DataArray

xclim.sdba.properties.spell_length_distribution(da='da', *, method='amount', op='>=', thresh='1 mm d-1', stat='mean', group='time', resample_before_rl=True, ds=None)

Spell length distribution. (realm: generic)

Statistic of spell length distribution when the variable respects a condition (defined by an operation, a method and a threshold).

Based on indice _spell_length_distribution().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • method ({‘quantile’, ‘amount’}) – Method to choose the threshold. ‘amount’: The threshold is directly the quantity in {thresh}. It needs to have the same units as {da}. ‘quantile’: The threshold is calculated as the quantile {thresh} of the distribution. Default : amount.

  • op ({‘>=’, ‘>’, ‘<’, ‘<=’}) – Operation to verify the condition for a spell. The condition for a spell is variable {op} threshold. Default : >=.

  • thresh (str) – Threshold on which to evaluate the condition to have a spell. Str with units if the method is “amount”. Float of the quantile if the method is “quantile”. Default : 1 mm d-1.

  • stat ({‘mean’, ‘min’, ‘max’}) – Statistics to apply to the resampled input at the {group} (e.g. 1-31 Jan 1980) and then over all years (e.g. Jan 1980-2010) Default : mean.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. E.g. If ‘time.month’, the spell lengths are computed separately for each month. Default : time.

  • resample_before_rl (boolean) – Determines if the resampling should take place before or after the run length encoding (or a similar algorithm) is applied to runs. Default : True.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

spell_length_distribution (DataArray) – {stat} of spell length distribution when the variable is {op} the {method} {thresh}.

Return type:

xarray.DataArray

xclim.sdba.properties.std(da='da', *, group='time', ds=None)

Standard Deviation. (realm: generic)

Standard deviation of the variable over all years at the time resolution.

Based on indice _std().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. E.g. If ‘time.month’, the standard deviation is performed separately for each month. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

std (DataArray) – Standard deviation of the variable., with additional attributes: cell_methods: time: std

Return type:

xarray.DataArray

xclim.sdba.properties.transition_probability(da='da', *, initial_op='>=', final_op='>=', thresh='1 mm d-1', group='time', ds=None)

Transition probability. (realm: generic)

Probability of transition from the initial state to the final state. The states are booleans comparing the value of the day to the threshold with the operator.

Based on indice _transition_probability().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • initial_op ({‘le’, ‘==’, ‘ge’, ‘ne’, ‘lt’, ‘!=’, ‘<’, ‘<=’, ‘eq’, ‘>=’, ‘gt’, ‘>’}) – Operation to verify the condition for the initial state. The condition is variable {op} threshold. Default : >=.

  • final_op ({‘le’, ‘==’, ‘ge’, ‘ne’, ‘lt’, ‘!=’, ‘<’, ‘<=’, ‘eq’, ‘>=’, ‘gt’, ‘>’}) – Operation to verify the condition for the final state. The condition is variable {op} threshold. Default : >=.

  • thresh (str) – Threshold on which to evaluate the condition. Default : 1 mm d-1.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping on the output. e.g. For “time.month”, the transition probability would be calculated on each month, with all years included. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

transition_probability (DataArray) – Transition probability of values {initial_op} {thresh} to values {final_op} {thresh}.

Return type:

xarray.DataArray

xclim.sdba.properties.trend(da='da', *, output='slope', group='time', ds=None)

Linear Trend. (realm: generic)

The data is averaged over each time resolution and the inter-annual trend is returned. This function will rechunk along the grouping dimension.

Based on indice _trend().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • output ({‘slope’, ‘intercept_stderr’, ‘stderr’, ‘rvalue’, ‘intercept’, ‘pvalue’}) – The attributes of the linear regression to return, as defined in scipy.stats.linregress: ‘slope’ is the slope of the regression line. ‘intercept’ is the intercept of the regression line. ‘rvalue’ is The Pearson correlation coefficient. The square of rvalue is equal to the coefficient of determination. ‘pvalue’ is the p-value for a hypothesis test whose null hypothesis is that the slope is zero, using Wald Test with t-distribution of the test statistic. ‘stderr’ is the standard error of the estimated slope (gradient), under the assumption of residual normality. ‘intercept_stderr’ is the standard error of the estimated intercept, under the assumption of residual normality. Default : slope.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping on the output. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

trend (DataArray) – {output} of the interannual linear trend.

Return type:

xarray.DataArray

xclim.sdba.properties.var(da='da', *, group='time', ds=None)

Variance. (realm: generic)

Variance of the variable over all years at the time resolution.

Based on indice _var().

Parameters:
  • da (str or DataArray) – Variable on which to calculate the diagnostic. Default : ds.da.

  • group ({‘time.season’, ‘time’, ‘time.month’}) – Grouping of the output. E.g. If ‘time.month’, the variance is performed separately for each month. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

var (DataArray) – Variance of the variable., with additional attributes: cell_methods: time: var

Return type:

xarray.DataArray

Measures Submodule

SDBA diagnostic tests are made up of properties and measures. Measures compare adjusted simulations to a reference, through statistical properties or directly. This framework for the diagnostic tests was inspired by the VALUE project.

class xclim.sdba.measures.StatisticalPropertyMeasure(**kwds)[source]

Base indicator class for statistical properties that include the comparison measure, used when validating bias-adjusted outputs.

StatisticalPropertyMeasure objects combine the functionalities of xclim.sdba.properties.StatisticalProperty and xclim.sdba.properties.StatisticalMeasure.

Statistical properties usually reduce the time dimension and sometimes more dimensions (for example in spatial properties), sometimes adding a grouping dimension according to the passed value of group (e.g.: group=’time.month’ means the loss of the time dimension and the addition of a month one).

Statistical measures usually take two arrays as input: “sim” and “ref”, “sim” being measured against “ref”.

Statistical property-measures are generally unit-generic. If the inputs have different units, “sim” is converted to match “ref”.

allowed_groups = None

A list of allowed groupings. A subset of dayofyear, week, month, season or group. The latter stands for no temporal grouping.

aspect = None

marginal, temporal, multivariate or spatial.

Type:

The aspect the statistical property studies

xclim.sdba.measures.annual_cycle_correlation(sim='sim', ref='ref', *, window=15, group='time', ds=None)

Annual cycle correlation. (realm: generic)

Pearson correlation coefficient between the smooth day-of-year averaged annual cycles of the simulation and the reference. In the smooth day-of-year averaged annual cycles, each day-of-year is averaged over all years and over a window of days around that day.

Based on indice _annual_cycle_correlation().

Parameters:
  • sim (str or DataArray) – data from the simulation (a time-series for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (a time-series for each grid-point) Default : ds.ref.

  • window (number) – Size of window around each day of year around which to take the mean. E.g. If window=31, Jan 1st is averaged over from December 17th to January 16th. Default : 15.

  • group (str) – Compute the property and measure for each temporal groups individually. Currently not implemented. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

annual_cycle_correlation (DataArray) – Annual cycle correlation

Return type:

xarray.DataArray

xclim.sdba.measures.bias(sim='sim', ref='ref', *, ds=None)

Bias. (realm: generic)

The bias is the simulation minus the reference.

Based on indice _bias().

Parameters:
  • sim (str or DataArray) – data from the simulation (one value for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (one value for each grid-point) Default : ds.ref.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

bias (DataArray) – Absolute bias

Return type:

xarray.DataArray

xclim.sdba.measures.circular_bias(sim='sim', ref='ref', *, ds=None)

Circular bias. (realm: generic)

Bias considering circular time series. E.g. The bias between doy 365 and doy 1 is 364, but the circular bias is -1.

Based on indice _circular_bias().

Parameters:
  • sim (str or DataArray) – data from the simulation (one value for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (one value for each grid-point) Default : ds.ref.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

circular_bias (DataArray) – Circular bias [days]

Return type:

xarray.DataArray

xclim.sdba.measures.mae(sim='sim', ref='ref', *, group='time', ds=None)

Mean absolute error. (realm: generic)

The mean absolute error on the time dimension between the simulation and the reference.

Based on indice _mae().

Parameters:
  • sim (str or DataArray) – data from the simulation (a time-series for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (a time-series for each grid-point) Default : ds.ref.

  • group (str) – Compute the property and measure for each temporal groups individually. Currently not implemented. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

mae (DataArray) – Mean absolute error, with additional attributes: cell_methods: time: mean

Return type:

xarray.DataArray

xclim.sdba.measures.ratio(sim='sim', ref='ref', *, ds=None)

Ratio. (realm: generic)

The ratio is the quotient of the simulation over the reference.

Based on indice _ratio().

Parameters:
  • sim (str or DataArray) – data from the simulation (one value for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (one value for each grid-point) Default : ds.ref.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

ratio (DataArray) – Ratio

Return type:

xarray.DataArray

xclim.sdba.measures.relative_bias(sim='sim', ref='ref', *, ds=None)

Relative Bias. (realm: generic)

The relative bias is the simulation minus reference, divided by the reference.

Based on indice _relative_bias().

Parameters:
  • sim (str or DataArray) – data from the simulation (one value for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (one value for each grid-point) Default : ds.ref.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

relative_bias (DataArray) – Relative bias

Return type:

xarray.DataArray

xclim.sdba.measures.rmse(sim='sim', ref='ref', *, group='time', ds=None)

Root mean square error. (realm: generic)

The root mean square error on the time dimension between the simulation and the reference.

Based on indice _rmse().

Parameters:
  • sim (str or DataArray) – Data from the simulation (a time-series for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – Data from the reference (observations) (a time-series for each grid-point) Default : ds.ref.

  • group (str) – Compute the property and measure for each temporal groups individually. Currently not implemented. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

rmse (DataArray) – Root mean square error, with additional attributes: cell_methods: time: mean

Return type:

xarray.DataArray

xclim.sdba.measures.scorr(sim='sim', ref='ref', *, dims=None, group='time', ds=None)

Spatial correllogram. (realm: generic)

Compute the inter-site correlations of each array, compute the difference in correlations and sum. Taken from Vrac (2018). The spatial and temporal dimensions are reduced.

Based on indice _scorr().

Parameters:
  • sim (str or DataArray) – data from the simulation (a time-series for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (a time-series for each grid-point) Default : ds.ref.

  • dims (Any) – Name of the spatial dimensions. If None (default), all dimensions except ‘time’ are used. Default : None.

  • group (str) – Compute the property and measure for each temporal groups individually. Currently not implemented. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

Scorr (DataArray) – Sum of the inter-site correlation differences.

Return type:

xarray.DataArray

xclim.sdba.measures.taylordiagram(sim='sim', ref='ref', *, dim='time', group='time', ds=None)

Taylor diagram. (realm: generic)

Compute the respective standard deviations of a simulation and a reference array, as well as the Pearson correlation coefficient between both, all necessary parameters to plot points on a Taylor diagram.

Based on indice _taylordiagram().

Parameters:
  • sim (str or DataArray) – data from the simulation (a time-series for each grid-point) Default : ds.sim.

  • ref (str or DataArray) – data from the reference (observations) (a time-series for each grid-point) Default : ds.ref.

  • dim (str) – Dimension across which the correlation and standard deviation should be computed. Default : time.

  • group (str) – Compute the property and measure for each temporal groups individually. Currently not implemented. Default : time.

  • ds (Dataset, optional) – A dataset with the variables given by name. Default : None.

Returns:

taylordiagram (DataArray) – Standard deviations of sim, ref and correlation coefficient between both.

Return type:

xarray.DataArray

Spatial Analogues Module

class xclim.analog.spatial_analogs(target, candidates, dist_dim='time', method='kldiv', **kwargs)[source]

Compute dissimilarity statistics between target points and candidate points.

Spatial analogues based on the comparison of climate indices. The algorithm compares the distribution of the reference indices with the distribution of spatially distributed candidate indices and returns a value measuring the dissimilarity between both distributions over the candidate grid.

Parameters:
  • target (xr.Dataset) – Dataset of the target indices. Only indice variables should be included in the dataset’s data_vars. They should have only the dimension(s) dist_dim `in common with `candidates.

  • candidates (xr.Dataset) – Dataset of the candidate indices. Only indice variables should be included in the dataset’s data_vars.

  • dist_dim (str) – The dimension over which the distributions are constructed. This can be a multi-index dimension.

  • method ({‘seuclidean’, ‘nearest_neighbor’, ‘zech_aslan’, ‘kolmogorov_smirnov’, ‘friedman_rafsky’, ‘kldiv’}) – Which method to use when computing the dissimilarity statistic.

  • **kwargs – Any other parameter passed directly to the dissimilarity method.

Returns:

xr.DataArray – The dissimilarity statistic over the union of candidates’ and target’s dimensions. The range depends on the method.

xclim.analog.friedman_rafsky(x, y)[source]

Compute a dissimilarity metric based on the Friedman-Rafsky runs statistics.

The algorithm builds a minimal spanning tree (the subset of edges connecting all points that minimizes the total edge length) then counts the edges linking points from the same distribution. This method is scale-dependent.

Parameters:
  • x (np.ndarray (n,d)) – Reference sample.

  • y (np.ndarray (m,d)) – Candidate sample.

Return type:

float

Returns:

float – Friedman-Rafsky dissimilarity metric ranging from 0 to (m+n-1)/(m+n).

References

Friedman and Rafsky [1979]

xclim.analog.kldiv(x, y, *, k=1)[source]

Compute the Kullback-Leibler divergence between two multivariate samples.

The formula to compute the K-L divergence from samples is given by:

\[D(P||Q) = \frac{d}{n} \sum_i^n \log\left\{\frac{r_k(x_i)}{s_k(x_i)}\right\} + \log\left\{\frac{m}{n-1}\right\}\]

where \(r_k(x_i)\) and \(s_k(x_i)\) are, respectively, the Euclidean distance to the kth neighbour of \(x_i\) in the x array (excepting \(x_i\)) and in the y array. This method is scale-dependent.

Parameters:
  • x (np.ndarray (n,d)) – Samples from distribution P, which typically represents the true distribution (reference).

  • y (np.ndarray (m,d)) – Samples from distribution Q, which typically represents the approximate distribution (candidate)

  • k (int or sequence) – The kth neighbours to look for when estimating the density of the distributions. Defaults to 1, which can be noisy.

Return type:

float | Sequence[float]

Returns:

float or sequence – The estimated Kullback-Leibler divergence D(P||Q) computed from the distances to the kth neighbour.

Notes

In information theory, the Kullback–Leibler divergence [Perez-Cruz, 2008] is a non-symmetric measure of the difference between two probability distributions P and Q, where P is the “true” distribution and Q an approximation. This nuance is important because \(D(P||Q)\) is not equal to \(D(Q||P)\).

For probability distributions P and Q of a continuous random variable, the K–L divergence is defined as:

\[D_{KL}(P||Q) = \int p(x) \log\left(\frac{p(x)}{q(x)}\right) dx\]

This formula assumes we have a representation of the probability densities \(p(x)\) and \(q(x)\). In many cases, we only have samples from the distribution, and most methods first estimate the densities from the samples and then proceed to compute the K-L divergence. In Perez-Cruz [2008], the author proposes an algorithm to estimate the K-L divergence directly from the sample using an empirical CDF. Even though the CDFs do not converge to their true values, the paper proves that the K-L divergence almost surely does converge to its true value.

References

Perez-Cruz [2008]

xclim.analog.kolmogorov_smirnov(x, y)[source]

Compute the Kolmogorov-Smirnov statistic applied to two multivariate samples as described by Fasano and Franceschini.

This method is scale-dependent.

Parameters:
  • x (np.ndarray (n,d)) – Reference sample.

  • y (np.ndarray (m,d)) – Candidate sample.

Return type:

float

Returns:

float – Kolmogorov-Smirnov dissimilarity metric ranging from 0 to 1.

References

Fasano and Franceschini [1987]

xclim.analog.nearest_neighbor(x, y)[source]

Compute a dissimilarity metric based on the number of points in the pooled sample whose nearest neighbor belongs to the same distribution.

This method is scale-invariant.

Parameters:
  • x (np.ndarray (n,d)) – Reference sample.

  • y (np.ndarray (m,d)) – Candidate sample.

Return type:

ndarray

Returns:

float – Nearest-Neighbor dissimilarity metric ranging from 0 to 1.

References

Henze [1988]

xclim.analog.seuclidean(x, y)[source]

Compute the Euclidean distance between the mean of a multivariate candidate sample with respect to the mean of a reference sample.

This method is scale-invariant.

Parameters:
  • x (np.ndarray (n,d)) – Reference sample.

  • y (np.ndarray (m,d)) – Candidate sample.

Return type:

float

Returns:

float – Standardized Euclidean Distance between the mean of the samples ranging from 0 to infinity.

Notes

This metric considers neither the information from individual points nor the standard deviation of the candidate distribution.

References

Veloz, Williams, Lorenz, Notaro, Vavrus, and Vimont [2012]

xclim.analog.szekely_rizzo(x, y, *, standardize=True)[source]

Compute the Székely-Rizzo energy distance dissimilarity metric based on an analogy with Newton’s gravitational potential energy.

This method is scale-invariant when standardize=True (default), scale-dependent otherwise.

Parameters:
  • x (ndarray (n,d)) – Reference sample.

  • y (ndarray (m,d)) – Candidate sample.

  • standardize (bool) – If True (default), the standardized euclidean norm is used, instead of the conventional one.

Return type:

float

Returns:

float – Székely-Rizzo’s energy distance dissimilarity metric ranging from 0 to infinity.

Notes

The e-distance between two variables \(X\), \(Y\) (target and candidates) of sizes \(n,d\) and \(m,d\) proposed by Szekely and Rizzo [2004] is defined by:

\[e(X, Y) = \frac{n m}{n + m} \left[2\phi_{xy} − \phi_{xx} − \phi_{yy} \right]\]

where

\[\begin{split}\phi_{xy} &= \frac{1}{n m} \sum_{i = 1}^n \sum_{j = 1}^m \left\Vert X_i − Y_j \right\Vert \\ \phi_{xx} &= \frac{1}{n^2} \sum_{i = 1}^n \sum_{j = 1}^n \left\Vert X_i − X_j \right\Vert \\ \phi_{yy} &= \frac{1}{m^2} \sum_{i = 1}^m \sum_{j = 1}^m \left\Vert X_i − Y_j \right\Vert \\\end{split}\]

and where \(\Vert\cdot\Vert\) denotes the Euclidean norm, \(X_i\) denotes the i-th observation of \(X\). When standardized=False, this corresponds to the \(T\) test of Rizzo and Székely [2016] (p. 28) and to the eqdist.e function of the energy R package (with two samples) and gives results twice as big as xclim.sdba.processing.escore(). The standardization was added following the logic of [Grenier et al., 2013] to make the metric scale-invariant.

References

Grenier, Parent, Huard, Anctil, and Chaumont [2013], Rizzo and Székely [2016], Szekely and Rizzo [2004]

xclim.analog.zech_aslan(x, y, *, dmin=1e-12)[source]

Compute a modified Zech-Aslan energy distance dissimilarity metric based on an analogy with the energy of a cloud of electrical charges.

This method is scale-invariant.

Parameters:
  • x (np.ndarray (n,d)) – Reference sample.

  • y (np.ndarray (m,d)) – Candidate sample.

  • dmin (float) – The cut-off for low distances to avoid singularities on identical points.

Return type:

float

Returns:

float – Zech-Aslan dissimilarity metric ranging from -infinity to infinity.

Notes

The energy measure between two variables \(X\), \(Y\) (target and candidates) of sizes \(n,d\) and \(m,d\) proposed by Aslan and Zech [2003] is defined by:

\[\begin{split}e(X, Y) &= \left[\phi_{xx} + \phi_{yy} - \phi_{xy}\right] \\ \phi_{xy} &= \frac{1}{n m} \sum_{i = 1}^n \sum_{j = 1}^m R\left[SED(X_i, Y_j)\right] \\ \phi_{xx} &= \frac{1}{n^2} \sum_{i = 1}^n \sum_{j = i + 1}^n R\left[SED(X_i, X_j)\right] \\ \phi_{yy} &= \frac{1}{m^2} \sum_{i = 1}^m \sum_{j = i + 1}^m R\left[SED(X_i, Y_j)\right] \\\end{split}\]

where \(X_i\) denotes the i-th observation of \(X\). \(R\) is a weight function and \(SED(A, B)\) denotes the standardized Euclidean distance.

\[\begin{split}R(r) &= \left\{\begin{array}{r l} -\ln r & \text{for } r > d_{min} \\ -\ln d_{min} & \text{for } r \leq d_{min} \end{array}\right. \\ SED(X_i, Y_j) &= \sqrt{\sum_{k=1}^d \frac{\left(X_i(k) - Y_i(k)\right)^2}{\sigma_x(k)\sigma_y(k)}}\end{split}\]

where \(k\) is a counter over dimensions (indices in the case of spatial analogs) and \(\sigma_x(k)\) is the standard deviation of \(X\) in dimension \(k\). Finally, \(d_{min}\) is a cut-off to avoid poles when \(r \to 0\), it is controllable through the dmin parameter.

This version corresponds the \(D_{ZAE}\) test of Grenier et al. [2013] (eq. 7), which is a version of \(\phi_{NM}\) from Aslan and Zech [2003], modified by using the standardized Euclidean distance, the log weight function and choosing \(d_{min} = 10^{-12}\).

References

Aslan and Zech [2003], Grenier, Parent, Huard, Anctil, and Chaumont [2013], Zech and Aslan [2003]

Subset Module

Warning

The xclim.subset module was removed in xclim==0.40. Subsetting is now offered via clisops.core.subset. The subsetting functions offered by clisops are available at the following link: CLISOPS core subsetting API

Note

For more information about clisops refer to their documentation here: CLISOPS documentation

Other Utilities

Calendar Handling Utilities

Helper function to handle dates, times and different calendars with xarray.

xclim.core.calendar.adjust_doy_calendar(source, target)[source]

Interpolate from one set of dayofyear range to another calendar.

Interpolate an array defined over a dayofyear range (say 1 to 360) to another dayofyear range (say 1 to 365).

Parameters:
  • source (xr.DataArray) – Array with dayofyear coordinate.

  • target (xr.DataArray or xr.Dataset) – Array with time coordinate.

Return type:

DataArray

Returns:

xr.DataArray – Interpolated source array over coordinates spanning the target dayofyear range.

xclim.core.calendar.build_climatology_bounds(da)[source]

Build the climatology_bounds property with the start and end dates of input data.

Parameters:

da (xr.DataArray) – The input data. Must have a time dimension.

Return type:

list[str]

xclim.core.calendar.climatological_mean_doy(arr, window=5)[source]

Calculate the climatological mean and standard deviation for each day of the year.

Parameters:
  • arr (xarray.DataArray) – Input array.

  • window (int) – Window size in days.

Return type:

tuple[DataArray, DataArray]

Returns:

xarray.DataArray, xarray.DataArray – Mean and standard deviation.

xclim.core.calendar.common_calendar(calendars, join='outer')[source]

Return a calendar common to all calendars from a list.

Uses the hierarchy: 360_day < noleap < standard < all_leap. Returns “default” only if all calendars are “default.”

Parameters:
  • calendars (Sequence of string) – List of calendar names.

  • join ({‘inner’, ‘outer’}) – The criterion for the common calendar.

    • ‘outer’: the common calendar is the smallest calendar (in number of days by year) that will include all the

      dates of the other calendars. When converting the data to this calendar, no timeseries will lose elements, but some might be missing (gaps or NaNs in the series).

    • ‘inner’: the common calendar is the smallest calendar of the list.

      When converting the data to this calendar, no timeseries will have missing elements (no gaps or NaNs), but some might be dropped.

Return type:

str

Examples

>>> common_calendar(["360_day", "noleap", "default"], join="outer")
'standard'
>>> common_calendar(["360_day", "noleap", "default"], join="inner")
'360_day'
xclim.core.calendar.compare_offsets(freqA, op, freqB)[source]

Compare offsets string based on their approximate length, according to a given operator.

Offset are compared based on their length approximated for a period starting after 1970-01-01 00:00:00. If the offsets are from the same category (same first letter), only the multiplier prefix is compared (QS-DEC == QS-JAN, MS < 2MS). “Business” offsets are not implemented.

Parameters:
  • freqA (str) – RHS Date offset string (‘YS’, ‘1D’, ‘QS-DEC’, …)

  • op ({‘<’, ‘<=’, ‘==’, ‘>’, ‘>=’, ‘!=’}) – Operator to use.

  • freqB (str) – LHS Date offset string (‘YS’, ‘1D’, ‘QS-DEC’, …)

Return type:

bool

Returns:

bool – freqA op freqB

xclim.core.calendar.construct_offset(mult, base, start_anchored, anchor)[source]

Reconstruct an offset string from its parts.

Parameters:
  • mult (int) – The period multiplier (>= 1).

  • base (str) – The base period string (one char).

  • start_anchored (bool) – If True and base in [Y, Q, M], adds the “S” flag, False add “E”.

  • anchor (str, optional) – The month anchor of the offset. Defaults to JAN for bases YS and QS and to DEC for bases YE and QE.

Returns:

str – An offset string, conformant to pandas-like naming conventions.

Notes

This provides the mirror opposite functionality of parse_offset().

xclim.core.calendar.convert_calendar(source, target, align_on=None, missing=None, doy=False, dim='time')[source]

Convert a DataArray/Dataset to another calendar using the specified method.

By default, only converts the individual timestamps, does not modify any data except in dropping invalid/surplus dates or inserting missing dates.

If the source and target calendars are either no_leap, all_leap or a standard type, only the type of the time array is modified. When converting to a leap year from a non-leap year, the 29th of February is removed from the array. In the other direction and if target is a string, the 29th of February will be missing in the output, unless missing is specified, in which case that value is inserted.

For conversions involving 360_day calendars, see Notes.

This method is safe to use with sub-daily data as it doesn’t touch the time part of the timestamps.

Parameters:
  • source (xr.DataArray or xr.Dataset) – Input array/dataset with a time coordinate of a valid dtype (datetime64 or a cftime.datetime).

  • target (xr.DataArray or str) – Either a calendar name or the 1D time coordinate to convert to. If an array is provided, the output will be reindexed using it and in that case, days in target that are missing in the converted source are filled by missing (which defaults to NaN).

  • align_on ({None, ‘date’, ‘year’, ‘random’}) – Must be specified when either source or target is a 360_day calendar, ignored otherwise. See Notes.

  • missing (Any, optional) – A value to use for filling in dates in the target that were missing in the source. If target is a string, default (None) is not to fill values. If it is an array, default is to fill with NaN.

  • doy (bool or {‘year’, ‘date’}) – If not False, variables flagged as “dayofyear” (with a is_dayofyear==1 attribute) are converted to the new calendar too. Can be a string, which will be passed as the align_on argument of convert_doy(). If True, year is passed.

  • dim (str) – Name of the time coordinate.

Return type:

DataArray | Dataset

Returns:

xr.DataArray or xr.Dataset – Copy of source with the time coordinate converted to the target calendar. If target is given as an array, the output is reindexed to it, with fill value missing. If target was a string and missing was None (default), invalid dates in the new calendar are dropped, but missing dates are not inserted. If target was a string and missing was given, then start, end and frequency of the new time axis are inferred and the output is reindexed to that a new array.

Notes

If one of the source or target calendars is 360_day, align_on must be specified and two options are offered.

“year”

The dates are translated according to their rank in the year (dayofyear), ignoring their original month and day information, meaning that the missing/surplus days are added/removed at regular intervals.

From a 360_day to a standard calendar, the output will be missing the following dates (day of year in parentheses):
To a leap year:

January 31st (31), March 31st (91), June 1st (153), July 31st (213), September 31st (275) and November 30th (335).

To a non-leap year:

February 6th (36), April 19th (109), July 2nd (183), September 12th (255), November 25th (329).

From standard calendar to a ‘360_day’, the following dates in the source array will be dropped:
From a leap year:

January 31st (31), April 1st (92), June 1st (153), August 1st (214), September 31st (275), December 1st (336)

From a non-leap year:

February 6th (37), April 20th (110), July 2nd (183), September 13th (256), November 25th (329)

This option is best used on daily and subdaily data.

“date”

The month/day information is conserved and invalid dates are dropped from the output. This means that when converting from a 360_day to a standard calendar, all 31st (Jan, March, May, July, August, October and December) will be missing as there is no equivalent dates in the 360_day and the 29th (on non-leap years) and 30th of February will be dropped as there are no equivalent dates in a standard calendar.

This option is best used with data on a frequency coarser than daily.

“random”

Similar to “year”, each day of year of the source is mapped to another day of year of the target. However, instead of having always the same missing days according the source and target years, here 5 days are chosen randomly, one for each fifth of the year. However, February 29th is always missing when converting to a leap year, or its value is dropped when converting from a leap year. This is similar to method used in the Pierce et al. [2014] dataset.

This option is best used on daily data.

References

Pierce, Cayan, and Thrasher [2014]

Examples

This method does not try to fill the missing dates other than with a constant value, passed with missing. In order to fill the missing dates with interpolation, one can simply use xarray’s method:

>>> tas_nl = convert_calendar(tas, "noleap")  # For the example
>>> with_missing = convert_calendar(tas_nl, "standard", missing=np.NaN)
>>> out = with_missing.interpolate_na("time", method="linear")

Here, if Nans existed in the source data, they will be interpolated too. If that is, for some reason, not wanted, the workaround is to do:

>>> mask = convert_calendar(tas_nl, "standard").notnull()
>>> out2 = out.where(mask)
xclim.core.calendar.convert_doy(source, target_cal, source_cal=None, align_on='year', missing=nan, dim='time')[source]

Convert the calendar of day of year (doy) data.

Parameters:
  • source (xr.DataArray) – Day of year data (range [1, 366], max depending on the calendar).

  • target_cal (str) – Name of the calendar to convert to.

  • source_cal (str, optional) – Calendar the doys are in. If not given, uses the “calendar” attribute of source or, if absent, the calendar of its dim axis.

  • align_on ({‘date’, ‘year’}) – If ‘year’ (default), the doy is seen as a “percentage” of the year and is simply rescaled unto the new doy range. This always result in floating point data, changing the decimal part of the value. if ‘date’, the doy is seen as a specific date. See notes. This never changes the decimal part of the value.

  • missing (Any) – If align_on is “date” and the new doy doesn’t exist in the new calendar, this value is used.

  • dim (str) – Name of the temporal dimension.

Return type:

DataArray

xclim.core.calendar.date_range(*args, calendar='default', **kwargs)[source]

Wrap a Pandas date_range object.

Uses pd.date_range (if calendar == ‘default’) or xr.cftime_range (otherwise).

Return type:

DatetimeIndex | CFTimeIndex

xclim.core.calendar.date_range_like(source, calendar)[source]

Generate a datetime array with the same frequency, start and end as another one, but in a different calendar.

Parameters:
  • source (xr.DataArray) – 1D datetime coordinate DataArray

  • calendar (str) – New calendar name.

Raises:

ValueError – If the source’s frequency was not found.

Return type:

DataArray

Returns:

xr.DataArray

1D datetime coordinate with the same start, end and frequency as the source, but in the new calendar.

The start date is assumed to exist in the target calendar. If the end date doesn’t exist, the code tries 1 and 2 calendar days before. Exception when the source is in 360_day and the end of the range is the 30th of a 31-days month, then the 31st is appended to the range.

xclim.core.calendar.datetime_to_decimal_year(times, calendar='')[source]

Convert a datetime xr.DataArray to decimal years according to its calendar or the given one.

Decimal years are the number of years since 0001-01-01 00:00:00 AD. Ex: ‘2000-03-01 12:00’ is 2000.1653 in a standard calendar, 2000.16301 in a “noleap” or 2000.16806 in a “360_day”.

Parameters:
  • times (xr.DataArray)

  • calendar (str)

Return type:

DataArray

Returns:

xr.DataArray

xclim.core.calendar.days_in_year(year, calendar='default')[source]

Return the number of days in the input year according to the input calendar.

Return type:

int

xclim.core.calendar.days_since_to_doy(da, start=None, calendar=None)[source]

Reverse the conversion made by doy_to_days_since().

Converts data given in days since a specific date to day-of-year.

Parameters:
  • da (xr.DataArray) – The result of doy_to_days_since().

  • start (DateOfYearStr, optional) – da is considered as days since that start date (in the year of the time index). If None (default), it is read from the attributes.

  • calendar (str, optional) – Calendar the “days since” were computed in. If None (default), it is read from the attributes.

Return type:

DataArray

Returns:

xr.DataArray – Same shape as da, values as day of year.

Examples

>>> from xarray import DataArray
>>> time = date_range("2020-07-01", "2021-07-01", freq="AS-JUL")
>>> da = DataArray(
...     [-86, 92],
...     dims=("time",),
...     coords={"time": time},
...     attrs={"units": "days since 10-02"},
... )
>>> days_since_to_doy(da).values
array([190, 2])
xclim.core.calendar.doy_from_string(doy, year, calendar)[source]

Return the day-of-year corresponding to a “MM-DD” string for a given year and calendar.

Return type:

int

xclim.core.calendar.doy_to_days_since(da, start=None, calendar=None)[source]

Convert day-of-year data to days since a given date.

This is useful for computing meaningful statistics on doy data.

Parameters:
  • da (xr.DataArray) – Array of “day-of-year”, usually int dtype, must have a time dimension. Sampling frequency should be finer or similar to yearly and coarser than daily.

  • start (date of year str, optional) – A date in “MM-DD” format, the base day of the new array. If None (default), the time axis is used. Passing start only makes sense if da has a yearly sampling frequency.

  • calendar (str, optional) – The calendar to use when computing the new interval. If None (default), the calendar attribute of the data or of its time axis is used. All time coordinates of da must exist in this calendar. No check is done to ensure doy values exist in this calendar.

Return type:

DataArray

Returns:

xr.DataArray – Same shape as da, int dtype, day-of-year data translated to a number of days since a given date. If start is not None, there might be negative values.

Notes

The time coordinates of da are considered as the START of the period. For example, a doy value of 350 with a timestamp of ‘2020-12-31’ is understood as ‘2021-12-16’ (the 350th day of 2021). Passing start=None, will use the time coordinate as the base, so in this case the converted value will be 350 “days since time coordinate”.

Examples

>>> from xarray import DataArray
>>> time = date_range("2020-07-01", "2021-07-01", freq="AS-JUL")
>>> # July 8th 2020 and Jan 2nd 2022
>>> da = DataArray([190, 2], dims=("time",), coords={"time": time})
>>> # Convert to days since Oct. 2nd, of the data's year.
>>> doy_to_days_since(da, start="10-02").values
array([-86, 92])
xclim.core.calendar.ensure_cftime_array(time)[source]

Convert an input 1D array to a numpy array of cftime objects.

Python’s datetime are converted to cftime.DatetimeGregorian (“standard” calendar).

Parameters:

time (sequence) – A 1D array of datetime-like objects.

Return type:

ndarray

Returns:

np.ndarray

Raises:

ValueError – When unable to cast the input.:

xclim.core.calendar.get_calendar(obj, dim='time')[source]

Return the calendar of an object.

Parameters:
  • obj (Any) – An object defining some date. If obj is an array/dataset with a datetime coordinate, use dim to specify its name. Values must have either a datetime64 dtype or a cftime dtype. obj can also be a python datetime.datetime, a cftime object or a pandas Timestamp or an iterable of those, in which case the calendar is inferred from the first value.

  • dim (str) – Name of the coordinate to check (if obj is a DataArray or Dataset).

Raises:

ValueError – If no calendar could be inferred.

Return type:

str

Returns:

str – The cftime calendar name or “default” when the data is using numpy’s or python’s datetime types. Will always return “standard” instead of “gregorian”, following CF conventions 1.9.

xclim.core.calendar.interp_calendar(source, target, dim='time')[source]

Interpolates a DataArray/Dataset to another calendar based on decimal year measure.

Each timestamp in source and target are first converted to their decimal year equivalent then source is interpolated on the target coordinate. The decimal year is the number of years since 0001-01-01 AD. Ex: ‘2000-03-01 12:00’ is 2000.1653 in a standard calendar or 2000.16301 in a ‘noleap’ calendar.

This method should be used with daily data or coarser. Sub-daily result will have a modified day cycle.

Parameters:
  • source (xr.DataArray or xr.Dataset) – The source data to interpolate, must have a time coordinate of a valid dtype (np.datetime64 or cftime objects)

  • target (xr.DataArray) – The target time coordinate of a valid dtype (np.datetime64 or cftime objects)

  • dim (str) – The time coordinate name.

Return type:

DataArray | Dataset

Returns:

xr.DataArray or xr.Dataset – The source interpolated on the decimal years of target,

xclim.core.calendar.is_offset_divisor(divisor, offset)[source]

Check that divisor is a divisor of offset.

A frequency is a “divisor” of another if a whole number of periods of the former fit within a single period of the latter.

Parameters:
  • divisor (str) – The divisor frequency.

  • offset (str) – The large frequency.

Returns:

bool

Examples

>>> is_offset_divisor("QS-Jan", "YS")
True
>>> is_offset_divisor("QS-DEC", "YS-JUL")
False
>>> is_offset_divisor("D", "M")
True
xclim.core.calendar.parse_offset(freq)[source]

Parse an offset string.

Parse a frequency offset and, if needed, convert to cftime-compatible components.

Parameters:

freq (str) – Frequency offset.

Return type:

tuple[int, str, bool, str | None]

Returns:

  • multiplier (int) – Multiplier of the base frequency. “[n]W” is always replaced with “[7n]D”, as xarray doesn’t support “W” for cftime indexes.

  • offset_base (str) – Base frequency.

  • is_start_anchored (bool) – Whether coordinates of this frequency should correspond to the beginning of the period (True) or its end (False). Can only be False when base is Y, Q or M; in other words, xclim assumes frequencies finer than monthly are all start-anchored.

  • anchor (str, optional) – Anchor date for bases Y or Q. As xarray doesn’t support “W”, neither does xclim (anchor information is lost when given).

xclim.core.calendar.percentile_doy(arr, window=5, per=10.0, alpha=0.3333333333333333, beta=0.3333333333333333, copy=True)[source]

Percentile value for each day of the year.

Return the climatological percentile over a moving window around each day of the year. Different quantile estimators can be used by specifying alpha and beta according to specifications given by Hyndman and Fan [1996]. The default definition corresponds to method 8, which meets multiple desirable statistical properties for sample quantiles. Note that numpy.percentile corresponds to method 7, with alpha and beta set to 1.

Parameters:
  • arr (xr.DataArray) – Input data, a daily frequency (or coarser) is required.

  • window (int) – Number of time-steps around each day of the year to include in the calculation.

  • per (float or sequence of floats) – Percentile(s) between [0, 100]

  • alpha (float) – Plotting position parameter.

  • beta (float) – Plotting position parameter.

  • copy (bool) – If True (default) the input array will be deep-copied. It’s a necessary step to keep the data integrity, but it can be costly. If False, no copy is made of the input array. It will be mutated and rendered unusable but performances may significantly improve. Put this flag to False only if you understand the consequences.

Return type:

DataArray

Returns:

xr.DataArray – The percentiles indexed by the day of the year. For calendars with 366 days, percentiles of doys 1-365 are interpolated to the 1-366 range.

References

Hyndman and Fan [1996]

xclim.core.calendar.resample_doy(doy, arr)[source]

Create a temporal DataArray where each day takes the value defined by the day-of-year.

Parameters:
  • doy (xr.DataArray) – Array with dayofyear coordinate.

  • arr (xr.DataArray or xr.Dataset) – Array with time coordinate.

Return type:

DataArray

Returns:

xr.DataArray – An array with the same dimensions as doy, except for dayofyear, which is replaced by the time dimension of arr. Values are filled according to the day of year value in doy.

xclim.core.calendar.select_time(da, drop=False, season=None, month=None, doy_bounds=None, date_bounds=None, include_bounds=True)[source]

Select entries according to a time period.

This conveniently improves xarray’s xarray.DataArray.where() and xarray.DataArray.sel() with fancier ways of indexing over time elements. In addition to the data da and argument drop, only one of season, month, doy_bounds or date_bounds may be passed.

Parameters:
  • da (xr.DataArray or xr.Dataset) – Input data.

  • drop (bool) – Whether to drop elements outside the period of interest or to simply mask them (default).

  • season (string or sequence of strings, optional) – One or more of ‘DJF’, ‘MAM’, ‘JJA’ and ‘SON’.

  • month (integer or sequence of integers, optional) – Sequence of month numbers (January = 1 … December = 12)

  • doy_bounds (2-tuple of integers, optional) – The bounds as (start, end) of the period of interest expressed in day-of-year, integers going from 1 (January 1st) to 365 or 366 (December 31st). If calendar awareness is needed, consider using date_bounds instead.

  • date_bounds (2-tuple of strings, optional) – The bounds as (start, end) of the period of interest expressed as dates in the month-day (%m-%d) format.

  • include_bounds (bool or 2-tuple of booleans) – Whether the bounds of doy_bounds or date_bounds should be inclusive or not. Either one value for both or a tuple. Default is True, meaning bounds are inclusive.

Return type:

DataArray | Dataset

Returns:

xr.DataArray or xr.Dataset – Selected input values. If drop=False, this has the same length as da (along dimension ‘time’), but with masked (NaN) values outside the period of interest.

Examples

Keep only the values of fall and spring.

>>> ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
>>> ds.time.size
1461
>>> out = select_time(ds, drop=True, season=["MAM", "SON"])
>>> out.time.size
732

Or all values between two dates (included).

>>> out = select_time(ds, drop=True, date_bounds=("02-29", "03-02"))
>>> out.time.values
array(['1990-03-01T00:00:00.000000000', '1990-03-02T00:00:00.000000000',
       '1991-03-01T00:00:00.000000000', '1991-03-02T00:00:00.000000000',
       '1992-02-29T00:00:00.000000000', '1992-03-01T00:00:00.000000000',
       '1992-03-02T00:00:00.000000000', '1993-03-01T00:00:00.000000000',
       '1993-03-02T00:00:00.000000000'], dtype='datetime64[ns]')
xclim.core.calendar.stack_periods(da, window=30, stride=None, min_length=None, freq='YS', dim='period', start='1970-01-01', align_days=True, pad_value=<NA>)[source]

Construct a multi-period array.

Stack different equal-length periods of da into a new ‘period’ dimension.

This is similar to da.rolling(time=window).construct(dim, stride=stride), but adapted for arguments in terms of a base temporal frequency that might be non-uniform (years, months, etc.). It is reversible for some cases (see stride). A rolling-construct method will be much more performant for uniform periods (days, weeks).

Parameters:
  • da (xr.Dataset or xr.DataArray) – An xarray object with a time dimension. Must have a uniform timestep length. Output might be strange if this does not use a uniform calendar (noleap, 360_day, all_leap).

  • window (int) – The length of the moving window as a multiple of freq.

  • stride (int, optional) – At which interval to take the windows, as a multiple of freq. For the operation to be reversible with unstack_periods(), it must divide window into an odd number of parts. Default is window (no overlap between periods).

  • min_length (int, optional) – Windows shorter than this are not included in the output. Given as a multiple of freq. Default is window (every window must be complete). Similar to the min_periods argument of da.rolling. If freq is annual or quarterly and min_length == ``window, the first period is considered complete if the first timestep is in the first month of the period.

  • freq (str) – Units of window, stride and min_length, as a frequency string. Must be larger or equal to the data’s sampling frequency. Note that this function offers an easier interface for non-uniform period (like years or months) but is much slower than a rolling-construct method.

  • dim (str) – The new dimension name.

  • start (str) – The start argument passed to xarray.date_range() to generate the new placeholder time coordinate.

  • align_days (bool) – When True (default), an error is raised if the output would have unaligned days across periods. If freq = ‘YS’, day-of-year alignment is checked and if freq is “MS” or “QS”, we check day-in-month. Only uniform-calendar will pass the test for freq=’YS’. For other frequencies, only the 360_day calendar will work. This check is ignored if the sampling rate of the data is coarser than “D”.

  • pad_value (Any) – When some periods are shorter than others, this value is used to pad them at the end. Passed directly as argument fill_value to xarray.concat(), the default is the same as on that function.

Returns:

xr.DataArray – A DataArray with a new period dimension and a time dimension with the length of the longest window. The new time coordinate has the same frequency as the input data but is generated using xarray.date_range() with the given start value. That coordinate is the same for all periods, depending on the choice of window and freq, it might make sense. But for unequal periods or non-uniform calendars, it will certainly not. If stride is a divisor of window, the correct timeseries can be reconstructed with unstack_periods(). The coordinate of period is the first timestep of each window.

xclim.core.calendar.time_bnds(time, freq=None, precision=None)[source]

Find the time bounds for a datetime index.

As we are using datetime indices to stand in for period indices, assumptions regarding the period are made based on the given freq.

Parameters:
  • time (DataArray, Dataset, CFTimeIndex, DatetimeIndex, DataArrayResample or DatasetResample) – Object which contains a time index as a proxy representation for a period index.

  • freq (str, optional) – String specifying the frequency/offset such as ‘MS’, ‘2D’, or ‘3min’ If not given, it is inferred from the time index, which means that index must have at least three elements.

  • precision (str, optional) – A timedelta representation that pandas.Timedelta understands. The time bounds will be correct up to that precision. If not given, 1 ms (“1U”) is used for CFtime indexes and 1 ns (“1N”) for numpy datetime64 indexes.

Returns:

DataArray – The time bounds: start and end times of the periods inferred from the time index and a frequency. It has the original time index along it’s time coordinate and a new bnds coordinate. The dtype and calendar of the array are the same as the index.

Notes

xclim assumes that indexes for greater-than-day frequencies are “floored” down to a daily resolution. For example, the coordinate “2000-01-31 00:00:00” with a “ME” frequency is assumed to mean a period going from “2000-01-01 00:00:00” to “2000-01-31 23:59:59.999999”.

Similarly, it assumes that daily and finer frequencies yield indexes pointing to the period’s start. So “2000-01-31 00:00:00” with a “3h” frequency, means a period going from “2000-01-31 00:00:00” to “2000-01-31 02:59:59.999999”.

xclim.core.calendar.unstack_periods(da, dim='period')[source]

Unstack an array constructed with stack_periods().

Can only work with periods stacked with a stride that divides window in an odd number of sections. When stride is smaller than window, only the center-most stride of each window is kept, except for the beginning and end which are taken from the first and last windows.

Parameters:
  • da (xr.DataArray) – As constructed by stack_periods(), attributes of the period coordinates must have been preserved.

  • dim (str) – The period dimension name.

Notes

The following table shows which strides are included (o) in the unstacked output.

In this example, stride was a fifth of window and min_length was four (4) times stride. The row index i the period index in the stacked dataset, columns are the stride-long section of the original timeseries.

Unstacking example with stride < window.

i

0

1

2

3

4

5

6

3

x

x

o

o

2

x

x

o

x

x

1

x

x

o

x

x

0

o

o

o

x

x

xclim.core.calendar.within_bnds_doy(arr, *, low, high)[source]

Return whether array values are within bounds for each day of the year.

Parameters:
  • arr (xarray.DataArray) – Input array.

  • low (xarray.DataArray) – Low bound with dayofyear coordinate.

  • high (xarray.DataArray) – High bound with dayofyear coordinate.

Return type:

DataArray

Returns:

xarray.DataArray

xclim.core.calendar.yearly_interpolated_doy(time, source_calendar, target_calendar)[source]

Return the nearest day in the target calendar of the corresponding “decimal year” in the source calendar.

xclim.core.calendar.yearly_random_doy(time, rng, source_calendar, target_calendar)[source]

Return a day of year in the new calendar.

Removes Feb 29th and five other days chosen randomly within five sections of 72 days.

Formatting Utilities for Indicators

class xclim.core.formatting.AttrFormatter(mapping, modifiers)[source]

Bases: string.Formatter

A formatter for frequently used attribute values.

See the doc of format_field() for more details.

format(format_string, /, *args, **kwargs)[source]

Format a string.

Parameters:
  • format_string (str)

  • *args (Any)

  • **kwargs (Any)

Return type:

str

Returns:

str

format_field(value, format_spec)[source]

Format a value given a formatting spec.

If format_spec is in this Formatter’s modifiers, the corresponding variation of value is given. If format_spec is ‘r’ (raw), the value is returned unmodified. If format_spec is not specified but value is in the mapping, the first variation is returned.

Examples

Let’s say the string “The dog is {adj1}, the goose is {adj2}” is to be translated to French and that we know that possible values of adj are nice and evil. In French, the genre of the noun changes the adjective (cat = chat is masculine, and goose = oie is feminine) so we initialize the formatter as:

>>> fmt = AttrFormatter(
...     {
...         "nice": ["beau", "belle"],
...         "evil": ["méchant", "méchante"],
...         "smart": ["intelligent", "intelligente"],
...     },
...     ["m", "f"],
... )
>>> fmt.format(
...     "Le chien est {adj1:m}, l'oie est {adj2:f}, le gecko est {adj3:r}",
...     adj1="nice",
...     adj2="evil",
...     adj3="smart",
... )
"Le chien est beau, l'oie est méchante, le gecko est smart"

The base values may be given using unix shell-like patterns:

>>> fmt = AttrFormatter(
...     {"YS-*": ["annuel", "annuelle"], "MS": ["mensuel", "mensuelle"]},
...     ["m", "f"],
... )
>>> fmt.format(
...     "La moyenne {freq:f} est faite sur un échantillon {src_timestep:m}",
...     freq="YS-JUL",
...     src_timestep="MS",
... )
'La moyenne annuelle est faite sur un échantillon mensuel'
xclim.core.formatting.gen_call_string(funcname, *args, **kwargs)[source]

Generate a signature string for use in the history attribute.

DataArrays and Dataset are replaced with their name, while Nones, floats, ints and strings are printed directly. All other objects have their type printed between < >.

Arguments given through positional arguments are printed positionnally and those given through keywords are printed prefixed by their name.

Parameters:
  • funcname (str) – Name of the function

  • args, kwargs – Arguments given to the function.

Example

>>> A = xr.DataArray([1], dims=("x",), name="A")
>>> gen_call_string("func", A, b=2.0, c="3", d=[10] * 100)
"func(A, b=2.0, c='3', d=<list>)"
xclim.core.formatting.generate_indicator_docstring(ind)[source]

Generate an indicator’s docstring from keywords.

Parameters:

ind – Indicator instance

Return type:

str

Returns:

str

xclim.core.formatting.get_percentile_metadata(data, prefix)[source]

Get the metadata related to percentiles from the given DataArray as a dictionary.

Parameters:
  • data (xr.DataArray) – Must be a percentile DataArray, this means the necessary metadata must be available in its attributes and coordinates.

  • prefix (str) – The prefix to be used in the metadata key. Usually this takes the form of “tasmin_per” or equivalent.

Return type:

dict[str, str]

Returns:

dict – A mapping of the configuration used to compute these percentiles.

xclim.core.formatting.merge_attributes(attribute, *inputs_list, new_line='\\n', missing_str=None, **inputs_kws)[source]

Merge attributes from several DataArrays or Datasets.

If more than one input is given, its name (if available) is prepended as: “<input name> : <input attribute>”.

Parameters:
  • attribute (str) – The attribute to merge.

  • inputs_list (xr.DataArray or xr.Dataset) – The datasets or variables that were used to produce the new object. Inputs given that way will be prefixed by their name attribute if available.

  • new_line (str) – The character to put between each instance of the attributes. Usually, in CF-conventions, the history attributes uses ‘\n’ while cell_methods uses ‘ ‘.

  • missing_str (str) – A string that is printed if an input doesn’t have the attribute. Defaults to None, in which case the input is simply skipped.

  • **inputs_kws (xr.DataArray or xr.Dataset) – Mapping from names to the datasets or variables that were used to produce the new object. Inputs given that way will be prefixes by the passed name.

Returns:

str – The new attribute made from the combination of the ones from all the inputs.

xclim.core.formatting.parse_doc(doc)[source]

Crude regex parsing reading an indice docstring and extracting information needed in indicator construction.

The appropriate docstring syntax is detailed in Defining new indices.

Parameters:

doc (str) – The docstring of an indice function.

Return type:

dict[str, str]

Returns:

dict – A dictionary with all parsed sections.

xclim.core.formatting.prefix_attrs(source, keys, prefix)[source]

Rename some keys of a dictionary by adding a prefix.

Parameters:
  • source (dict) – Source dictionary, for example data attributes.

  • keys (sequence) – Names of keys to prefix.

  • prefix (str) – Prefix to prepend to keys.

Returns:

dict – Dictionary of attributes with some keys prefixed.

xclim.core.formatting.unprefix_attrs(source, keys, prefix)[source]

Remove prefix from keys in a dictionary.

Parameters:
  • source (dict) – Source dictionary, for example data attributes.

  • keys (sequence) – Names of original keys for which prefix should be removed.

  • prefix (str) – Prefix to remove from keys.

Returns:

dict – Dictionary of attributes whose keys were prefixed, with prefix removed.

xclim.core.formatting.update_history(hist_str, *inputs_list, new_name=None, **inputs_kws)[source]

Return a history string with the timestamped message and the combination of the history of all inputs.

The new history entry is formatted as “[<timestamp>] <new_name>: <hist_str> - xclim version: <xclim.__version__>.”

Parameters:
  • hist_str (str) – The string describing what has been done on the data.

  • new_name (Optional[str]) – The name of the newly created variable or dataset to prefix hist_msg.

  • *inputs_list (Sequence[Union[xr.DataArray, xr.Dataset]]) – The datasets or variables that were used to produce the new object. Inputs given that way will be prefixed by their “name” attribute if available.

  • **inputs_kws (Union[xr.DataArray, xr.Dataset]) – Mapping from names to the datasets or variables that were used to produce the new object. Inputs given that way will be prefixes by the passed name.

Returns:

str – The combine history of all inputs starting with hist_str.

See also

merge_attributes

xclim.core.formatting.update_xclim_history(func)[source]

Decorator that auto-generates and fills the history attribute.

The history is generated from the signature of the function and added to the first output. Because of a limitation of the boltons wrapper, all arguments passed to the wrapped function will be printed as keyword arguments.

Options Submodule

Global or contextual options for xclim, similar to xarray.set_options.

class xclim.core.options.set_options(**kwargs)[source]

Set options for xclim in a controlled context.

Variables:
  • metadata_locales (list[Any]) – List of IETF language tags or tuples of language tags and a translation dict, or tuples of language tags and a path to a json file defining translation of attributes. Default: [].

  • data_validation ({"log", "raise", "error"}) – Whether to “log”, “raise” an error or ‘warn’ the user on inputs that fail the data checks in xclim.core.datachecks(). Default: "raise".

  • cf_compliance ({"log", "raise", "error"}) – Whether to “log”, “raise” an error or “warn” the user on inputs that fail the CF compliance checks in xclim.core.cfchecks(). Default: "warn".

  • check_missing ({"any", "wmo", "pct", "at_least_n", "skip"}) – How to check for missing data and flag computed indicators. Available methods are “any”, “wmo”, “pct”, “at_least_n” and “skip”. Missing method can be registered through the xclim.core.options.register_missing_method decorator. Default: "any"

  • missing_options (dict) – Dictionary of options to pass to the missing method. Keys must the name of missing method and values must be mappings from option names to values.

  • run_length_ufunc (str) – Whether to use the 1D ufunc version of run length algorithms or the dask-ready broadcasting version. Default is "auto", which means the latter is used for dask-backed and large arrays.

  • sdba_extra_output (bool) – Whether to add diagnostic variables to outputs of sdba’s train, adjust and processing operations. Details about these additional variables are given in the object’s docstring. When activated, adjust will return a Dataset with scen and those extra diagnostics For processing functions, see the doc, the output type might change, or not depending on the algorithm. Default: False.

  • sdba_encode_cf (bool) – Whether to encode cf coordinates in the map_blocks optimization that most adjustment methods are based on. This should have no impact on the results, but should run much faster in the graph creation phase.

  • keep_attrs (bool or str) – Controls attributes handling in indicators. If True, attributes from all inputs are merged using the drop_conflicts strategy and then updated with xclim-provided attributes. If as_dataset is also True and a dataset was passed to the ds argument of the Indicator, the dataset’s attributes are copied to the indicator’s output. If False, attributes from the inputs are ignored. If “xarray”, xclim will use xarray’s keep_attrs option. Note that xarray’s “default” is equivalent to False. Default: "xarray".

  • as_dataset (bool) – If True, indicators output datasets. If False, they output DataArrays. Default :False.

Examples

You can use set_options either as a context manager:

>>> import xclim
>>> ds = xr.open_dataset(path_to_tas_file).tas
>>> with xclim.set_options(metadata_locales=["fr"]):
...     out = xclim.atmos.tg_mean(ds)
...

Or to set global options:

import xclim

xclim.set_options(missing_options={"pct": {"tolerance": 0.04}})

Miscellaneous Indices Utilities

Helper functions for the indices computations, indicator construction and other things.

class xclim.core.utils.DateStr

Type annotation for strings representing full dates (YYYY-MM-DD), may include time.

alias of str

class xclim.core.utils.DayOfYearStr

Type annotation for strings representing dates without a year (MM-DD).

alias of str

class xclim.core.utils.Quantified

Type annotation for thresholds and other not-exactly-a-variable quantities

alias of TypeVar(‘Quantified’, xarray.DataArray, str, pint.registry.Quantity)

xclim.core.utils.VARIABLES = {'air_density': {'canonical_units': 'kg m-3', 'cell_methods': 'time: mean', 'description': 'Air density.', 'dimensions': '[density]', 'standard_name': 'air_density'}, 'areacello': {'canonical_units': 'm2', 'cell_methods': 'area: sum', 'description': 'Cell area (over the ocean).', 'dimensions': '[area]', 'standard_name': 'cell_area'}, 'discharge': {'canonical_units': 'm3 s-1', 'cell_methods': 'time: mean', 'description': 'The amount of water, in all phases, flowing in the river channel and flood plain.', 'standard_name': 'water_volume_transport_in_river_channel'}, 'evspsblpot': {'canonical_units': 'kg m-2 s-1', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}], 'description': 'Potential evapotranspiration flux.', 'dimensions': '[discharge]', 'standard_name': 'water_potential_evapotranspiration_flux'}, 'hurs': {'canonical_units': '%', 'cell_methods': 'time: mean', 'data_flags': [{'percentage_values_outside_of_bounds': None}], 'description': 'Relative humidity.', 'dimensions': '[]', 'standard_name': 'relative_humidity'}, 'huss': {'canonical_units': '1', 'cell_methods': 'time: mean', 'description': 'Specific humidity.', 'dimensions': '[]', 'standard_name': 'specific_humidity'}, 'lat': {'canonical_units': 'degrees_north', 'description': 'Latitude.', 'dimensions': '[]', 'standard_name': 'latitude'}, 'pr': {'canonical_units': 'kg m-2 s-1', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}, {'very_large_precipitation_events': {'thresh': '300 mm d-1'}}, {'values_op_thresh_repeating_for_n_or_more_days': {'n': 5, 'op': 'eq', 'thresh': '5 mm d-1'}}, {'values_op_thresh_repeating_for_n_or_more_days': {'n': 10, 'op': 'eq', 'thresh': '1 mm d-1'}}], 'description': 'Surface precipitation flux (all phases).', 'dimensions': '[precipitation]', 'standard_name': 'precipitation_flux'}, 'prc': {'canonical_units': 'kg m-2 s-1', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}], 'description': 'Precipitation flux due to the convection schemes of the model (all phases).', 'dimensions': '[precipitation]', 'standard_name': 'convective_precipitation_flux'}, 'prsn': {'canonical_units': 'kg m-2 s-1', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}], 'description': 'Surface snowfall flux.', 'dimensions': '[mass]/([area][time])', 'standard_name': 'snowfall_flux'}, 'prsnd': {'canonical_units': 'm s-1', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}], 'description': 'Surface snowfall rate.', 'dimensions': '[length]/[time]'}, 'ps': {'canonical_units': 'Pa', 'cell_methods': 'time: mean', 'data_flags': [{'values_repeating_for_n_or_more_days': {'n': 5}}], 'description': 'Air pressure at surface', 'standard_name': 'surface_air_pressure'}, 'psl': {'canonical_units': 'Pa', 'cell_methods': 'time: mean', 'data_flags': [{'values_repeating_for_n_or_more_days': {'n': 5}}], 'description': 'Air pressure at sea level.', 'dimensions': '[pressure]', 'standard_name': 'air_pressure_at_sea_level'}, 'rlds': {'canonical_units': 'W m-2', 'cell_methods': 'time: mean', 'description': 'Incoming longwave radiation.', 'dimensions': '[radiation]', 'standard_name': 'surface_downwelling_longwave_flux'}, 'rls': {'canonical_units': 'W m-2', 'cell_methods': 'time: mean', 'description': 'Net longwave radiation.', 'dimensions': '[radiation]', 'standard_name': 'surface_net_downward_longwave_flux'}, 'rlus': {'canonical_units': 'W m-2', 'cell_methods': 'time: mean', 'description': 'Outgoing longwave radiation.', 'dimensions': '[radiation]', 'standard_name': 'surface_upwelling_longwave_flux'}, 'rsds': {'canonical_units': 'W m-2', 'cell_methods': 'time: mean', 'description': 'Incoming shortwave radiation.', 'dimensions': '[radiation]', 'standard_name': 'surface_downwelling_shortwave_flux'}, 'rss': {'canonical_units': 'W m-2', 'cell_methods': 'time: mean', 'description': 'Net shortwave radiation.', 'dimensions': '[radiation]', 'standard_name': 'surface_net_downward_shortwave_flux'}, 'rsus': {'canonical_units': 'W m-2', 'cell_methods': 'time: mean', 'description': 'Outgoing shortwave radiation.', 'dimensions': '[radiation]', 'standard_name': 'surface_upwelling_shortwave_flux'}, 'sfcWind': {'canonical_units': 'm s-1', 'cell_methods': 'time: mean', 'data_flags': [{'wind_values_outside_of_bounds': {'lower': '0 m s-1', 'upper': '46.0 m s-1'}}, {'values_op_thresh_repeating_for_n_or_more_days': {'n': 6, 'op': 'gt', 'thresh': '2.0 m s-1'}}], 'description': 'Surface wind speed.', 'dimensions': '[speed]', 'standard_name': 'wind_speed'}, 'sfcWindfromdir': {'canonical_units': 'degree', 'cell_methods': 'time: mean', 'cmip6': False, 'description': 'Surface wind direction of provenance.', 'dimensions': '[]', 'standard_name': 'wind_from_direction'}, 'sfcWindmax': {'canonical_units': 'm s-1', 'cell_methods': 'time: max', 'data_flags': [{'wind_values_outside_of_bounds': {'lower': '0 m s-1', 'upper': '46.0 m s-1'}}, {'values_op_thresh_repeating_for_n_or_more_days': {'n': 6, 'op': 'gt', 'thresh': '2.0 m s-1'}}], 'description': 'Surface maximum wind speed.', 'dimensions': '[speed]', 'standard_name': 'wind_speed'}, 'siconc': {'canonical_units': '%', 'cell_methods': 'time: mean', 'data_flags': [{'percentage_values_outside_of_bounds': None}], 'description': 'Sea ice concentration (area fraction).', 'dimensions': '[]', 'standard_name': 'sea_ice_area_fraction'}, 'smd': {'canonical_units': 'mm d-1', 'cell_methods': 'time: mean', 'description': 'Soil moisture deficit.', 'dimensions': '[precipitation]', 'standard_name': 'soil_moisture_deficit'}, 'snc': {'canonical_units': '%', 'cell_methods': 'time: mean', 'data_flags': [{'percentage_values_outside_of_bounds': None}], 'description': 'Surface area fraction covered by snow.', 'dimensions': '[]', 'standard_name': 'surface_snow_area_fraction'}, 'snd': {'canonical_units': 'm', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}], 'description': 'Surface snow thickness.', 'dimensions': '[length]', 'standard_name': 'surface_snow_thickness'}, 'snr': {'canonical_units': 'kg m-3', 'cell_methods': 'time: mean', 'description': 'Surface snow density.', 'dimensions': '[density]', 'standard_name': 'surface_snow_density'}, 'snw': {'canonical_units': 'kg m-2', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}], 'description': 'Surface snow amount.', 'dimensions': '[mass]/[area]', 'standard_name': 'surface_snow_amount'}, 'streamflow': {'canonical_units': 'm3 s-1', 'cell_methods': 'time: mean', 'description': 'The amount of water, in all phases, flowing in the river channel and flood plain.', 'standard_name': 'water_volume_transport_in_river_channel'}, 'sund': {'canonical_units': 's', 'cell_methods': 'time: mean', 'cmip6': False, 'description': 'Duration of sunshine.', 'dimensions': '[time]', 'standard_name': 'duration_of_sunshine'}, 'swe': {'canonical_units': 'm', 'cell_methods': 'time: mean', 'data_flags': [{'negative_accumulation_values': None}], 'description': 'Surface snow water equivalent amount', 'dimensions': '[length]', 'standard_name': 'lwe_thickness_of_snow_amount'}, 'tas': {'canonical_units': 'K', 'cell_methods': 'time: mean', 'data_flags': [{'temperature_extremely_high': {'thresh': '60 degC'}}, {'temperature_extremely_low': {'thresh': '-90 degC'}}, {'tas_exceeds_tasmax': None}, {'tas_below_tasmin': None}, {'values_repeating_for_n_or_more_days': {'n': 5}}, {'outside_n_standard_deviations_of_climatology': {'n': 5, 'window': 5}}], 'description': 'Mean surface temperature.', 'dimensions': '[temperature]', 'standard_name': 'air_temperature'}, 'tasmax': {'canonical_units': 'K', 'cell_methods': 'time: maximum', 'data_flags': [{'temperature_extremely_high': {'thresh': '60 degC'}}, {'temperature_extremely_low': {'thresh': '-90 degC'}}, {'tas_exceeds_tasmax': None}, {'tasmax_below_tasmin': None}, {'values_repeating_for_n_or_more_days': {'n': 5}}, {'outside_n_standard_deviations_of_climatology': {'n': 5, 'window': 5}}], 'description': 'Maximum surface temperature.', 'dimensions': '[temperature]', 'standard_name': 'air_temperature'}, 'tasmin': {'canonical_units': 'K', 'cell_methods': 'time: minimum', 'data_flags': [{'temperature_extremely_high': {'thresh': '60 degC'}}, {'temperature_extremely_low': {'thresh': '-90 degC'}}, {'tasmax_below_tasmin': None}, {'tas_below_tasmin': None}, {'values_repeating_for_n_or_more_days': {'n': 5}}, {'outside_n_standard_deviations_of_climatology': {'n': 5, 'window': 5}}], 'description': 'Minimum surface temperature.', 'dimensions': '[temperature]', 'standard_name': 'air_temperature'}, 'tdps': {'canonical_units': 'K', 'cell_methods': 'time: mean', 'description': 'Mean surface dew point temperature.', 'dimensions': '[temperature]', 'standard_name': 'dew_point_temperature'}, 'thickness_of_rainfall_amount': {'canonical_units': 'm', 'cell_methods': 'time: sum', 'description': 'Accumulated depth of rainfall, i.e. the thickness of a layer of liquid water having the same mass per unit area as the rainfall amount.\n', 'dimensions': '[length]', 'standard_name': 'thickness_of_rainfall_amount'}, 'ua': {'canonical_units': 'm s-1', 'cell_methods': 'time: mean', 'description': 'Eastward component of the wind velocity (in the atmosphere).', 'dimensions': '[speed]', 'standard_name': 'eastward_wind'}, 'uas': {'canonical_units': 'm s-1', 'cell_methods': 'time: mean', 'description': 'Eastward component of the wind velocity (at the surface).', 'dimensions': '[speed]', 'standard_name': 'eastward_wind'}, 'vas': {'canonical_units': 'm s-1', 'cell_methods': 'time: mean', 'description': 'Northward component of the wind velocity (at the surface).', 'dimensions': '[speed]', 'standard_name': 'northward_wind'}, 'wind_speed': {'canonical_units': 'm s-1', 'cell_methods': 'time: mean', 'description': 'Wind speed.', 'dimensions': '[speed]', 'standard_name': 'wind_speed'}, 'wsgsmax': {'canonical_units': 'm s-1', 'cell_methods': 'time: maximum', 'cmip6': False, 'data_flags': [{'wind_values_outside_of_bounds': {'lower': '0 m s-1', 'upper': '76.0 m s-1'}}, {'values_op_thresh_repeating_for_n_or_more_days': {'n': 5, 'op': 'gt', 'thresh': '4.0 m s-1'}}], 'description': 'Maximum surface wind speed.', 'dimensions': '[speed]', 'standard_name': 'wind_speed_of_gust'}}

Official variables definitions.

A mapping from variable name to a dict with the following keys:

  • canonical_units [required] : The conventional units used by this variable.

  • cell_methods [optional] : The conventional cell_methods CF attribute

  • description [optional] : A description of the variable, to populate dynamically generated docstrings.

  • dimensions [optional] : The dimensionality of the variable, an abstract version of the units. See xclim.units.units._dimensions.keys() for available terms. This is especially useful for making xclim aware of “[precipitation]” variables.

  • standard_name [optional] : If it exists, the CF standard name.

  • data_flags [optional] : Data flags methods (xclim.core.dataflags) applicable to this variable. The method names are keys and values are dicts of keyword arguments to pass (an empty dict if there’s nothing to configure).

xclim.core.utils.wrapped_partial(func, suggested=None, **fixed)[source]

Wrap a function, updating its signature but keeping its docstring.

Parameters:
  • func (Callable) – The function to be wrapped

  • suggested (dict, optional) – Keyword arguments that should have new default values but still appear in the signature.

  • **fixed – Keyword arguments that should be fixed by the wrapped and removed from the signature.

Return type:

Callable

Returns:

Callable

Examples

>>> from inspect import signature
>>> def func(a, b=1, c=1):
...     print(a, b, c)
...
>>> newf = wrapped_partial(func, b=2)
>>> signature(newf)
<Signature (a, *, c=1)>
>>> newf(1)
1 2 1
>>> newf = wrapped_partial(func, suggested=dict(c=2), b=2)
>>> signature(newf)
<Signature (a, *, c=2)>
>>> newf(1)
1 2 2
xclim.core.utils.deprecated(from_version, suggested=None)[source]

Mark an index as deprecated and optionally suggest a replacement.

Parameters:
  • from_version (str, optional) – The version of xclim from which the function is deprecated.

  • suggested (str, optional) – The name of the function to use instead.

Return type:

Callable

Returns:

Callable

xclim.core.utils.walk_map(d, func)[source]

Apply a function recursively to values of dictionary.

Parameters:
  • d (dict) – Input dictionary, possibly nested.

  • func (Callable) – Function to apply to dictionary values.

Return type:

dict

Returns:

dict – Dictionary whose values are the output of the given function.

xclim.core.utils.load_module(path, name=None)[source]

Load a python module from a python file, optionally changing its name.

Examples

Given a path to a module file (.py):

from pathlib import Path
import os

path = Path("path/to/example.py")

The two following imports are equivalent, the second uses this method.

os.chdir(path.parent)
import example as mod1  # noqa

os.chdir(previous_working_dir)
mod2 = load_module(path)
mod1 == mod2
exception xclim.core.utils.ValidationError[source]

Bases: ValueError

Error raised when input data to an indicator fails the validation tests.

property msg
exception xclim.core.utils.MissingVariableError[source]

Bases: ValueError

Error raised when a dataset is passed to an indicator but one of the needed variable is missing.

xclim.core.utils.ensure_chunk_size(da, **minchunks)[source]

Ensure that the input DataArray has chunks of at least the given size.

If only one chunk is too small, it is merged with an adjacent chunk. If many chunks are too small, they are grouped together by merging adjacent chunks.

Parameters:
  • da (xr.DataArray) – The input DataArray, with or without the dask backend. Does nothing when passed a non-dask array.

  • **minchunks (dict[str, int]) – A kwarg mapping from dimension name to minimum chunk size. Pass -1 to force a single chunk along that dimension.

Return type:

DataArray

Returns:

xr.DataArray

xclim.core.utils.uses_dask(*das)[source]

Evaluate whether dask is installed and array is loaded as a dask array.

Parameters:

das (xr.DataArray or xr.Dataset) – DataArrays or Datasets to check.

Return type:

bool

Returns:

bool – True if any of the passed objects is using dask.

xclim.core.utils.calc_perc(arr, percentiles=None, alpha=1.0, beta=1.0, copy=True)[source]

Compute percentiles using nan_calc_percentiles and move the percentiles’ axis to the end.

Return type:

ndarray

xclim.core.utils.nan_calc_percentiles(arr, percentiles=None, axis=-1, alpha=1.0, beta=1.0, copy=True)[source]

Convert the percentiles to quantiles and compute them using _nan_quantile.

Return type:

ndarray

xclim.core.utils.raise_warn_or_log(err, mode, msg=None, err_type=<class 'ValueError'>, stacklevel=1)[source]

Raise, warn or log an error according.

Parameters:
  • err (Exception) – An error.

  • mode ({‘ignore’, ‘log’, ‘warn’, ‘raise’}) – What to do with the error.

  • msg (str, optional) – The string used when logging or warning. Defaults to the msg attr of the error (if present) or to “Failed with <err>”.

  • err_type (type) – The type of error/exception to raise.

  • stacklevel (int) – Stacklevel when warning. Relative to the call of this function (1 is added).

class xclim.core.utils.InputKind(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: enum.IntEnum

Constants for input parameter kinds.

For use by external parses to determine what kind of data the indicator expects. On the creation of an indicator, the appropriate constant is stored in xclim.core.indicator.Indicator.parameters. The integer value is what gets stored in the output of xclim.core.indicator.Indicator.json().

For developers : for each constant, the docstring specifies the annotation a parameter of an indice function should use in order to be picked up by the indicator constructor. Notice that we are using the annotation format as described in PEP 604, i.e. with ‘|’ indicating a union and without import objects from typing.

VARIABLE = 0

A data variable (DataArray or variable name).

Annotation : xr.DataArray.

OPTIONAL_VARIABLE = 1

An optional data variable (DataArray or variable name).

Annotation : xr.DataArray | None. The default should be None.

QUANTIFIED = 2

A quantity with units, either as a string (scalar), a pint.Quantity (scalar) or a DataArray (with units set).

Annotation : xclim.core.utils.Quantified and an entry in the xclim.core.units.declare_units() decorator. “Quantified” translates to str | xr.DataArray | pint.util.Quantity.

FREQ_STR = 3

A string representing an “offset alias”, as defined by pandas.

See the Pandas documentation on Offset aliases for a list of valid aliases.

Annotation : str + freq as the parameter name.

NUMBER = 4

A number.

Annotation : int, float and unions thereof, potentially optional.

STRING = 5

A simple string.

Annotation : str or str | None. In most cases, this kind of parameter makes sense with choices indicated in the docstring’s version of the annotation with curly braces. See Defining new indices.

DAY_OF_YEAR = 6

A date, but without a year, in the MM-DD format.

Annotation : xclim.core.utils.DayOfYearStr (may be optional).

DATE = 7

A date in the YYYY-MM-DD format, may include a time.

Annotation : xclim.core.utils.DateStr (may be optional).

NUMBER_SEQUENCE = 8

A sequence of numbers

Annotation : Sequence[int], Sequence[float] and unions thereof, may include single int and float, may be optional.

BOOL = 9

A boolean flag.

Annotation : bool, may be optional.

KWARGS = 50

A mapping from argument name to value.

Developers : maps the **kwargs. Please use as little as possible.

DATASET = 70

An xarray dataset.

Developers : as indices only accept DataArrays, this should only be added on the indicator’s constructor.

OTHER_PARAMETER = 99

An object that fits None of the previous kinds.

Developers : This is the fallback kind, it will raise an error in xclim’s unit tests if used.

xclim.core.utils.infer_kind_from_parameter(param)[source]

Return the appropriate InputKind constant from an inspect.Parameter object.

Parameters:

param (Parameter)

Return type:

InputKind

Notes

The correspondence between parameters and kinds is documented in xclim.core.utils.InputKind.

xclim.core.utils.adapt_clix_meta_yaml(raw, adapted)[source]

Read in a clix-meta yaml representation and refactor it to fit xclim’s yaml specifications.

xclim.core.utils.is_percentile_dataarray(source)[source]

Evaluate whether a DataArray is a Percentile.

A percentile dataarray must have climatology_bounds attributes and either a quantile or percentiles coordinate, the window is not mandatory.

Return type:

bool

Modules for xclim Developers

Indicator Tools

Indicator Utilities

The Indicator class wraps indices computations with pre- and post-processing functionality. Prior to computations, the class runs data and metadata health checks. After computations, the class masks values that should be considered missing and adds metadata attributes to the object.

There are many ways to construct indicators. A good place to start is this notebook.

Dictionary and YAML parser

To construct indicators dynamically, xclim can also use dictionaries and parse them from YAML files. This is especially useful for generating whole indicator “submodules” from files. This functionality is inspired by the work of clix-meta.

YAML file structure

Indicator-defining yaml files are structured in the following way. Most entries of the indicators section are mirroring attributes of the Indicator, please refer to its documentation for more details on each.

module: <module name>  # Defaults to the file name
realm: <realm>  # If given here, applies to all indicators that do not already provide it.
keywords: <keywords> # Merged with indicator-specific keywords (joined with a space)
references: <references> # Merged with indicator-specific references (joined with a new line)
base: <base indicator class>  # Defaults to "Daily" and applies to all indicators that do not give it.
doc: <module docstring>  # Defaults to a minimal header, only valid if the module doesn't already exist.
variables:  # Optional section if indicators declared below rely on variables unknown to xclim
            # (not in `xclim.core.utils.VARIABLES`)
            # The variables are not module-dependent and will overwrite any already existing with the same name.
  <varname>:
    canonical_units: <units> # required
    description: <description> # required
    standard_name: <expected standard_name> # optional
    cell_methods: <expected cell_methods> # optional
indicators:
  <identifier>:
    # From which Indicator to inherit
    base: <base indicator class>  # Defaults to module-wide base class
                                  # If the name startswith a '.', the base class is taken from the current module
                                  # (thus an indicator declared _above_).
                                  # Available classes are listed in `xclim.core.indicator.registry` and
                                  # `xclim.core.indicator.base_registry`.

    # General metadata, usually parsed from the `compute`'s docstring when possible.
    realm: <realm>  # defaults to module-wide realm. One of "atmos", "land", "seaIce", "ocean".
    title: <title>
    abstract: <abstract>
    keywords: <keywords>  # Space-separated, merged to module-wide keywords.
    references: <references>  # newline-seperated, merged to module-wide references.
    notes: <notes>

    # Other options
    missing: <missing method name>
    missing_options:
        # missing options mapping
    allowed_periods: [<list>, <of>, <allowed>, <periods>]

    # Compute function
    compute: <function name>  # Referring to a function in `Indices` module (xclim.indices.generic or xclim.indices)
    input:  # When "compute" is a generic function, this is a mapping from argument name to the expected variable.
            # This will allow the input units and CF metadata checks to run on the inputs.
            # Can also be used to modify the expected variable, as long as it has the same dimensionality
            # Ex: tas instead of tasmin.
            # Can refer to a variable declared in the `variables` section above.
      <var name in compute> : <variable official name>
      ...
    parameters:
     <param name>: <param data>  # Simplest case, to inject parameters in the compute function.
     <param name>:  # To change parameters metadata or to declare units when "compute" is a generic function.
        units: <param units>  # Only valid if "compute" points to a generic function
        default : <param default>
        description: <param description>
        kind: <param kind> # Override the parameter kind.
                         # This is mostly useful for transforming an optional variable into a required one by passing ``kind: 0``.
    ...
  ...  # and so on.

All fields are optional. Other fields found in the yaml file will trigger errors in xclim. In the following, the section under <identifier> is referred to as data. When creating indicators from a dictionary, with Indicator.from_dict(), the input dict must follow the same structure of data.

When a module is built from a yaml file, the yaml is first validated against the schema (see xclim/data/schema.yml) using the YAMALE library ([Lopker, 2022]). See the “Extending xclim” notebook for more info.

Inputs

As xclim has strict definitions of possible input variables (see xclim.core.utils.variables), the mapping of data.input simply links an argument name from the function given in “compute” to one of those official variables.

class xclim.core.indicator.Parameter(kind, default, description='', units=<class 'xclim.core.indicator._empty'>, choices=<class 'xclim.core.indicator._empty'>, value=<class 'xclim.core.indicator._empty'>)[source]

Bases: object

Class for storing an indicator’s controllable parameter.

For retrocompatibility, this class implements a “getitem” and a special “contains”.

Example

>>> p = Parameter(InputKind.NUMBER, default=2, description="A simple number")
>>> p.units is Parameter._empty  # has not been set
True
>>> "units" in p  # Easier/retrocompatible way to test if units are set
False
>>> p.description
'A simple number'
>>> p["description"]  # Same as above, for convenience.
'A simple number'
default[source]

alias of inspect._empty

update(other)[source]

Update a parameter’s values from a dict.

Return type:

None

classmethod is_parameter_dict(other)[source]

Return whether indicator has a parameter dictionary.

Return type:

bool

asdict()[source]

Format indicators as a dictionary.

Return type:

dict

property injected: bool

Indicate whether values are injected.

class xclim.core.indicator.IndicatorRegistrar[source]

Bases: object

Climate Indicator registering object.

classmethod get_instance()[source]

Return first found instance.

Raises ValueError if no instance exists.

class xclim.core.indicator.Indicator(**kwds)[source]

Bases: xclim.core.indicator.IndicatorRegistrar

Climate indicator base class.

Climate indicator object that, when called, computes an indicator and assigns its output a number of CF-compliant attributes. Some of these attributes can be templated, allowing metadata to reflect the value of call arguments.

Instantiating a new indicator returns an instance but also creates and registers a custom subclass in xclim.core.indicator.registry.

Attributes in Indicator.cf_attrs will be formatted and added to the output variable(s). This attribute is a list of dictionaries. For convenience and retro-compatibility, standard CF attributes (names listed in xclim.core.indicator.Indicator._cf_names) can be passed as strings or list of strings directly to the indicator constructor.

A lot of the Indicator’s metadata is parsed from the underlying compute function’s docstring and signature. Input variables and parameters are listed in xclim.core.indicator.Indicator.parameters, while parameters that will be injected in the compute function are in xclim.core.indicator.Indicator.injected_parameters. Both are simply views of xclim.core.indicator.Indicator._all_parameters.

Compared to their base compute function, indicators add the possibility of using dataset as input, with the injected argument ds in the call signature. All arguments that were indicated by the compute function to be variables (DataArrays) through annotations will be promoted to also accept strings that correspond to variable names in the ds dataset.

Parameters:
  • identifier (str) – Unique ID for class registry, should be a valid slug.

  • realm ({‘atmos’, ‘seaIce’, ‘land’, ‘ocean’}) – General domain of validity of the indicator. Indicators created outside xclim.indicators must set this attribute.

  • compute (func) – The function computing the indicators. It should return one or more DataArray.

  • cf_attrs (list of dicts) – Attributes to be formatted and added to the computation’s output. See xclim.core.indicator.Indicator.cf_attrs.

  • title (str) – A succinct description of what is in the computed outputs. Parsed from compute docstring if None (first paragraph).

  • abstract (str) – A long description of what is in the computed outputs. Parsed from compute docstring if None (second paragraph).

  • keywords (str) – Comma separated list of keywords. Parsed from compute docstring if None (from a “Keywords” section).

  • references (str) – Published or web-based references that describe the data or methods used to produce it. Parsed from compute docstring if None (from the “References” section).

  • notes (str) – Notes regarding computing function, for example the mathematical formulation. Parsed from compute docstring if None (form the “Notes” section).

  • src_freq (str, sequence of strings, optional) – The expected frequency of the input data. Can be a list for multiple frequencies, or None if irrelevant.

  • context (str) – The pint unit context, for example use ‘hydro’ to allow conversion from kg m-2 s-1 to mm/day.

Notes

All subclasses created are available in the registry attribute and can be used to define custom subclasses or parse all available instances.

cf_attrs: list[dict[str, str]] = None

A list of metadata information for each output of the indicator.

It minimally contains a “var_name” entry, and may contain : “standard_name”, “long_name”, “units”, “cell_methods”, “description” and “comment” on official xclim indicators. Other fields could also be present if the indicator was created from outside xclim.

var_name:

Output variable(s) name(s). For derived single-output indicators, this field is not inherited from the parent indicator and defaults to the identifier.

standard_name:

Variable name, must be in the CF standard names table (this is not checked).

long_name:

Descriptive variable name. Parsed from compute docstring if not given. (first line after the output dtype, only works on single output function).

units:

Representative units of the physical quantity.

cell_methods:

List of blank-separated words of the form “name: method”. Must respect the CF-conventions and vocabulary (not checked).

description:

Sentence(s) meant to clarify the qualifiers of the fundamental quantities, such as which surface a quantity is defined on or what the flux sign conventions are.

comment:

Miscellaneous information about the data or methods used to produce it.

classmethod from_dict(data, identifier, module=None)[source]

Create an indicator subclass and instance from a dictionary of parameters.

Most parameters are passed directly as keyword arguments to the class constructor, except:

  • “base” : A subclass of Indicator or a name of one listed in xclim.core.indicator.registry or xclim.core.indicator.base_registry. When passed, it acts as if from_dict was called on that class instead.

  • “compute” : A string function name translates to a xclim.indices.generic or xclim.indices function.

Parameters:
  • data (dict) – The exact structure of this dictionary is detailed in the submodule documentation.

  • identifier (str) – The name of the subclass and internal indicator name.

  • module (str) – The module name of the indicator. This is meant to be used only if the indicator is part of a dynamically generated submodule, to override the module of the base class.

classmethod translate_attrs(locale, fill_missing=True)[source]

Return a dictionary of unformatted translated translatable attributes.

Translatable attributes are defined in xclim.core.locales.TRANSLATABLE_ATTRS.

Parameters:
  • locale (str or sequence of str) – The POSIX name of the locale or a tuple of a locale name and a path to a json file defining translations. See xclim.locale for details.

  • fill_missing (bool) – If True (default) fill the missing attributes by their english values.

classmethod json(args=None)[source]

Return a serializable dictionary representation of the class.

Parameters:

args (mapping, optional) – Arguments as passed to the call method of the indicator. If not given, the default arguments will be used when formatting the attributes.

Notes

This is meant to be used by a third-party library wanting to wrap this class into another interface.

static compute(*args, **kwds)[source]

Compute the indicator.

This would typically be a function from xclim.indices.

cfcheck(**das)[source]

Compare metadata attributes to CF-Convention standards.

Default cfchecks use the specifications in xclim.core.utils.VARIABLES, assuming the indicator’s inputs are using the CMIP6/xclim variable names correctly. Variables absent from these default specs are silently ignored.

When subclassing this method, use functions decorated using xclim.core.options.cfcheck.

datacheck(**das)[source]

Verify that input data is valid.

When subclassing this method, use functions decorated using xclim.core.options.datacheck.

For example, checks could include:

  • assert no precipitation is negative

  • assert no temperature has the same value 5 days in a row

This base datacheck checks that the input data has a valid sampling frequency, as given in self.src_freq. If there are multiple inputs, it also checks if they all have the same frequency and the same anchor.

property n_outs

Return the length of all cf_attrs.

property parameters

Create a dictionary of controllable parameters.

Similar to Indicator._all_parameters, but doesn’t include injected parameters.

property injected_parameters

Return a dictionary of all injected parameters.

Opposite of Indicator.parameters().

property is_generic

Return True if the indicator is “generic”, meaning that it can accept variables with any units.

class xclim.core.indicator.CheckMissingIndicator(**kwds)[source]

Bases: xclim.core.indicator.Indicator

Class adding missing value checks to indicators.

This should not be used as-is, but subclassed by implementing the _get_missing_freq method. This method will be called in _postprocess using the compute parameters as only argument. It should return a freq string, the same as the output freq of the computed data. It can also be “None” to indicator the full time axis has been reduced, or “False” to skip the missing checks.

Parameters:
  • missing ({any, wmo, pct, at_least_n, skip, from_context}) – The name of the missing value method. See xclim.core.missing.MissingBase to create new custom methods. If None, this will be determined by the global configuration (see xclim.set_options). Defaults to “from_context”.

  • missing_options (dict, optional) – Arguments to pass to the missing function. If None, this will be determined by the global configuration.

class xclim.core.indicator.ReducingIndicator(**kwds)[source]

Bases: xclim.core.indicator.CheckMissingIndicator

Indicator that performs a time-reducing computation.

Compared to the base Indicator, this adds the handling of missing data.

Parameters:
  • missing ({any, wmo, pct, at_least_n, skip, from_context}) – The name of the missing value method. See xclim.core.missing.MissingBase to create new custom methods. If None, this will be determined by the global configuration (see xclim.set_options). Defaults to “from_context”.

  • missing_options (dict, optional) – Arguments to pass to the missing function. If None, this will be determined by the global configuration.

class xclim.core.indicator.ResamplingIndicator(**kwds)[source]

Bases: xclim.core.indicator.CheckMissingIndicator

Indicator that performs a resampling computation.

Compared to the base Indicator, this adds the handling of missing data, and the check of allowed periods.

Parameters:
  • missing ({any, wmo, pct, at_least_n, skip, from_context}) – The name of the missing value method. See xclim.core.missing.MissingBase to create new custom methods. If None, this will be determined by the global configuration (see xclim.set_options). Defaults to “from_context”.

  • missing_options (dict, optional) – Arguments to pass to the missing function. If None, this will be determined by the global configuration.

  • allowed_periods (Sequence[str], optional) – A list of allowed periods, i.e. base parts of the freq parameter. For example, indicators meant to be computed annually only will have allowed_periods=[“A”]. None means “any period” or that the indicator doesn’t take a freq argument.

class xclim.core.indicator.IndexingIndicator(**kwds)[source]

Bases: xclim.core.indicator.Indicator

Indicator that also injects “indexer” kwargs to subset the inputs before computation.

class xclim.core.indicator.ResamplingIndicatorWithIndexing(**kwds)[source]

Bases: xclim.core.indicator.ResamplingIndicator, xclim.core.indicator.IndexingIndicator

Resampling indicator that also injects “indexer” kwargs to subset the inputs before computation.

class xclim.core.indicator.Daily(**kwds)[source]

Bases: xclim.core.indicator.ResamplingIndicator

Class for daily inputs and resampling computes.

class xclim.core.indicator.Hourly(**kwds)[source]

Bases: xclim.core.indicator.ResamplingIndicator

Class for hourly inputs and resampling computes.

xclim.core.indicator.add_iter_indicators(module)[source]

Create an iterable of loaded indicators.

xclim.core.indicator.build_indicator_module(name, objs, doc=None, reload=False)[source]

Create or update a module from imported objects.

The module is inserted as a submodule of xclim.indicators.

Parameters:
  • name (str) – New module name. If it already exists, the module is extended with the passed objects, overwriting those with same names.

  • objs (dict[str, Indicator]) – Mapping of the indicators to put in the new module. Keyed by the name they will take in that module.

  • doc (str) – Docstring of the new module. Defaults to a simple header. Invalid if the module already exists.

  • reload (bool) – If reload is True and the module already exists, it is first removed before being rebuilt. If False (default), indicators are added or updated, but not removed.

Return type:

ModuleType

Returns:

ModuleType – A indicator module built from a mapping of Indicators.

xclim.core.indicator.build_indicator_module_from_yaml(filename, name=None, indices=None, translations=None, mode='raise', encoding='UTF8', reload=False, validate=True)[source]

Build or extend an indicator module from a YAML file.

The module is inserted as a submodule of xclim.indicators. When given only a base filename (no ‘yml’ extension), this tries to find custom indices in a module of the same name (.py) and translations in json files (.<lang>.json), see Notes.

Parameters:
  • filename (PathLike) – Path to a YAML file or to the stem of all module files. See Notes for behaviour when passing a basename only.

  • name (str, optional) – The name of the new or existing module, defaults to the basename of the file. (e.g: atmos.yml -> atmos)

  • indices (Mapping of callables or module or path, optional) – A mapping or module of indice functions or a python file declaring such a file. When creating the indicator, the name in the index_function field is first sought here, then the indicator class will search in xclim.indices.generic and finally in xclim.indices.

  • translations (Mapping of dicts or path, optional) – Translated metadata for the new indicators. Keys of the mapping must be 2-char language tags. Values can be translations dictionaries as defined in Internationalization. They can also be a path to a json file defining the translations.

  • mode ({‘raise’, ‘warn’, ‘ignore’}) – How to deal with broken indice definitions.

  • encoding (str) – The encoding used to open the .yaml and .json files. It defaults to UTF-8, overriding python’s mechanism which is machine dependent.

  • reload (bool) – If reload is True and the module already exists, it is first removed before being rebuilt. If False (default), indicators are added or updated, but not removed.

  • validate (bool or path) – If True (default), the yaml module is validated against xclim’s schema. Can also be the path to a yml schema against which to validate. Or False, in which case validation is simply skipped.

Return type:

ModuleType

Returns:

ModuleType – A submodule of pym:mod:`xclim.indicators.

Notes

When the given filename has no suffix (usually ‘.yaml’ or ‘.yml’), the function will try to load custom indice definitions from a file with the same name but with a .py extension. Similarly, it will try to load translations in *.<lang>.json files, where <lang> is the IETF language tag.

For example. a set of custom indicators could be fully described by the following files:

  • example.yml : defining the indicator’s metadata.

  • example.py : defining a few indice functions.

  • example.fr.json : French translations

  • example.tlh.json : Klingon translations.

See also

xclim.core.indicator, build_module

Bootstrapping Algorithms for Indicators Submodule

Module comprising the bootstrapping algorithm for indicators.

xclim.core.bootstrapping.bootstrap_func(compute_index_func, **kwargs)[source]

Bootstrap the computation of percentile-based indices.

Indices measuring exceedance over percentile-based thresholds (such as tx90p) may contain artificial discontinuities at the beginning and end of the reference period used to calculate percentiles. The bootstrap procedure can reduce those discontinuities by iteratively computing the percentile estimate and the index on altered reference periods.

These altered reference periods are themselves built iteratively: When computing the index for year x, the bootstrapping creates as many altered reference periods as the number of years in the reference period. To build one altered reference period, the values of year x are replaced by the values of another year in the reference period, then the index is computed on this altered period. This is repeated for each year of the reference period, excluding year x. The final result of the index for year x is then the average of all the index results on altered years.

Parameters:
  • compute_index_func (Callable) – Index function.

  • **kwargs – Arguments to func.

Return type:

DataArray

Returns:

xr.DataArray – The result of func with bootstrapping.

References

Zhang, Hegerl, Zwiers, and Kenyon [2005]

Notes

This function is meant to be used by the percentile_bootstrap decorator. The parameters of the percentile calculation (percentile, window, reference_period) are stored in the attributes of the percentile DataArray. The bootstrap algorithm implemented here does the following:

For each temporal grouping in the calculation of the index
    If the group `g_t` is in the reference period
        For every other group `g_s` in the reference period
            Replace group `g_t` by `g_s`
            Compute percentile on resampled time series
            Compute index function using percentile
        Average output from index function over all resampled time series
    Else compute index function using original percentile
xclim.core.bootstrapping.build_bootstrap_year_da(da, groups, label, dim='time')[source]

Return an array where a group in the original is replaced by every other groups along a new dimension.

Parameters:
  • da (DataArray) – Original input array over reference period.

  • groups (dict) – Output of grouping functions, such as DataArrayResample.groups.

  • label (Any) – Key identifying the group item to replace.

  • dim (str) – Dimension recognized as time. Default: time.

Return type:

DataArray

Returns:

DataArray – Array where one group is replaced by values from every other group along the bootstrap dimension.

xclim.core.bootstrapping.percentile_bootstrap(func)[source]

Decorator applying a bootstrap step to the calculation of exceedance over a percentile threshold.

This feature is experimental.

Bootstrapping avoids discontinuities in the exceedance between the reference period over which percentiles are computed, and “out of reference” periods. See bootstrap_func for details.

Declaration example:

@declare_units(tas="[temperature]", t90="[temperature]")
@percentile_bootstrap
def tg90p(
    tas: xarray.DataArray,
    t90: xarray.DataArray,
    freq: str = "YS",
    bootstrap: bool = False,
) -> xarray.DataArray:
    pass

Examples

>>> from xclim.core.calendar import percentile_doy
>>> from xclim.indices import tg90p
>>> tas = xr.open_dataset(path_to_tas_file).tas
>>> # To start bootstrap reference period must not fully overlap the studied period.
>>> tas_ref = tas.sel(time=slice("1990-01-01", "1992-12-31"))
>>> t90 = percentile_doy(tas_ref, window=5, per=90)
>>> tg90p(tas=tas, tas_per=t90.sel(percentiles=90), freq="YS", bootstrap=True)

SDBA Utilities

Base Classes and Developer Tools

class xclim.sdba.base.Parametrizable[source]

Bases: dict

Helper base class resembling a dictionary.

This object is _completely_ defined by the content of its internal dictionary, accessible through item access (self[‘attr’]) or in self.parameters. When serializing and restoring this object, only members of that internal dict are preserved. All other attributes set directly with self.attr = value will not be preserved upon serialization and restoration of the object with [json]pickle dictionary. Other variables set with self.var = data will be lost in the serialization process. This class is best serialized and restored with jsonpickle.

property parameters: dict

All parameters as a dictionary. Read-only.

class xclim.sdba.base.ParametrizableWithDataset[source]

Bases: xclim.sdba.base.Parametrizable

Parametrizable class that also has a ds attribute storing a dataset.

classmethod from_dataset(ds)[source]

Create an instance from a dataset.

The dataset must have a global attribute with a name corresponding to cls._attribute, and that attribute must be the result of jsonpickle.encode(object) where object is of the same type as this object.

set_dataset(ds)[source]

Store an xarray dataset in the ds attribute.

Useful with custom object initialization or if some external processing was performed.

Return type:

None

xclim.sdba.base.duck_empty(dims, sizes, dtype='float64', chunks=None)[source]

Return an empty DataArray based on a numpy or dask backend, depending on the “chunks” argument.

Return type:

DataArray

xclim.sdba.base.map_blocks(reduces=None, **out_vars)[source]

Decorator for declaring functions and wrapping them into a map_blocks.

Takes care of constructing the template dataset. Dimension order is not preserved. The decorated function must always have the signature: func(ds, **kwargs), where ds is a DataArray or a Dataset. It must always output a dataset matching the mapping passed to the decorator.

Parameters:
  • reduces (sequence of strings) – Name of the dimensions that are removed by the function.

  • **out_vars – Mapping from variable names in the output to their new dimensions. The placeholders Grouper.PROP, Grouper.DIM and Grouper.ADD_DIMS can be used to signify group.prop,``group.dim`` and group.add_dims respectively. If an output keeps a dimension that another loses, that dimension name must be given in reduces and in the list of new dimensions of the first output.

Return type:

Callable

xclim.sdba.base.map_groups(reduces=None, main_only=False, **out_vars)[source]

Decorator for declaring functions acting only on groups and wrapping them into a map_blocks.

This is the same as map_blocks but adds a call to group.apply() in the mapped func and the default value of reduces is changed.

The decorated function must have the signature: func(ds, dim, **kwargs). Where ds is a DataAray or Dataset, dim is the group.dim (and add_dims). The group argument is stripped from the kwargs, but must evidently be provided in the call.

Parameters:
  • reduces (sequence of str, optional) – Dimensions that are removed from the inputs by the function. Defaults to [Grouper.DIM, Grouper.ADD_DIMS] if main_only is False, and [Grouper.DIM] if main_only is True. See map_blocks().

  • main_only (bool) – Same as for Grouper.apply().

  • **out_vars – Mapping from variable names in the output to their new dimensions. The placeholders Grouper.PROP, Grouper.DIM and Grouper.ADD_DIMS can be used to signify group.prop,``group.dim`` and group.add_dims, respectively. If an output keeps a dimension that another loses, that dimension name must be given in reduces and in the list of new dimensions of the first output.

Return type:

Callable

See also

map_blocks

xclim.sdba.base.parse_group(func, kwargs=None, allow_only=None)[source]

Parse the kwargs given to a function to set the group arg with a Grouper object.

This function can be used as a decorator, in which case the parsing and updating of the kwargs is done at call time. It can also be called with a function from which extract the default group and kwargs to update, in which case it returns the updated kwargs.

If allow_only is given, an exception is raised when the parsed group is not within that list.

Return type:

Callable

class xclim.sdba.detrending.BaseDetrend(*, group='time', kind='+', **kwargs)[source]

Base class for detrending objects.

Defines three methods:

fit(da) : Compute trend from da and return a new _fitted_ Detrend object. detrend(da) : Return detrended array. retrend(da) : Puts trend back on da.

A fitted Detrend object is unique to the trend coordinate of the object used in fit, (usually ‘time’). The computed trend is stored in Detrend.ds.trend.

Subclasses should implement _get_trend_group() or _get_trend(). The first will be called in a group.apply(..., main_only=True), and should return a single DataArray. The second allows the use of functions wrapped in map_groups() and should also return a single DataArray.

The subclasses may reimplement _detrend and _retrend.

detrend(da)[source]

Remove the previously fitted trend from a DataArray.

fit(da)[source]

Extract the trend of a DataArray along a specific dimension.

Returns a new object that can be used for detrending and retrending. Fitted objects are unique to the fitted coordinate used.

property fitted

Return whether instance is fitted.

retrend(da)[source]

Put the previously fitted trend back on a DataArray.

class xclim.sdba.adjustment.TrainAdjust(*args, _trained=False, **kwargs)[source]

Base class for adjustment objects obeying the train-adjust scheme.

Children classes should implement these methods:

  • _train(ref, hist, **kwargs), classmethod receiving the training target and data, returning a training dataset and parameters to store in the object.

  • _adjust(sim, **kwargs), receiving the projected data and some arguments, returning the scen DataArray.

adjust(sim, *args, **kwargs)[source]

Return bias-adjusted data. Refer to the class documentation for the algorithm details.

Parameters:
  • sim (DataArray) – Time series to be bias-adjusted, usually a model output.

  • args (xr.DataArray) – Other DataArrays needed for the adjustment (usually none).

  • kwargs – Algorithm-specific keyword arguments, see class doc.

set_dataset(ds)[source]

Store an xarray dataset in the ds attribute.

Useful with custom object initialization or if some external processing was performed.

classmethod train(ref, hist, **kwargs)[source]

Train the adjustment object. Refer to the class documentation for the algorithm details.

Parameters:
  • ref (DataArray) – Training target, usually a reference time series drawn from observations.

  • hist (DataArray) – Training data, usually a model output whose biases are to be adjusted.

class xclim.sdba.adjustment.Adjust(*args, _trained=False, **kwargs)[source]

Adjustment with no intermediate trained object.

Children classes should implement a _adjust classmethod taking as input the three DataArrays and returning the scen dataset/array.

classmethod adjust(ref, hist, sim, **kwargs)[source]

Return bias-adjusted data. Refer to the class documentation for the algorithm details.

Parameters:
  • ref (DataArray) – Training target, usually a reference time series drawn from observations.

  • hist (DataArray) – Training data, usually a model output whose biases are to be adjusted.

  • sim (DataArray) – Time series to be bias-adjusted, usually a model output.

  • **kwargs – Algorithm-specific keyword arguments, see class doc.

xclim.sdba.properties.StatisticalProperty(**kwds)[source]

Base indicator class for statistical properties used for validating bias-adjusted outputs.

Statistical properties reduce the time dimension, sometimes adding a grouping dimension according to the passed value of group (e.g.: group=’time.month’ means the loss of the time dimension and the addition of a month one).

Statistical properties are generally unit-generic. To use those indicator in a workflow, it is recommended to wrap them with a virtual submodule, creating one specific indicator for each variable input (or at least for each possible dimensionality).

Statistical properties may restrict the sampling frequency of the input, they usually take in a single variable (named “da” in unit-generic instances).

xclim.sdba.measures.StatisticalMeasure(**kwds)[source]

Base indicator class for statistical measures used when validating bias-adjusted outputs.

Statistical measures use input data where the time dimension was reduced, usually by the computation of a xclim.sdba.properties.StatisticalProperty instance. They usually take two arrays as input: “sim” and “ref”, “sim” being measured against “ref”. The two arrays must have identical coordinates on their common dimensions.

Statistical measures are generally unit-generic. If the inputs have different units, “sim” is converted to match “ref”.

Spatial Analogues Helpers

xclim.analog.metric(func)[source]

Register a metric function in the metrics mapping and add some preparation/checking code.

All metric functions accept 2D inputs. This reshapes 1D inputs to (n, 1) and (m, 1). All metric functions are invalid when any non-finite values are present in the inputs.

xclim.analog.standardize(x, y)[source]

Standardize x and y by the square root of the product of their standard deviation.

Parameters:
  • x (np.ndarray) – Array to be compared.

  • y (np.ndarray) – Array to be compared.

Return type:

tuple[ndarray, ndarray]

Returns:

(ndarray, ndarray) – Standardized arrays.

Testing Module

Testing and Tutorial Utilities’ Module

xclim.testing.utils.get_file(name, github_url='https://github.com/Ouranosinc/xclim-testdata', branch='main', cache_dir=PosixPath('/home/docs/.cache/xclim-testdata'))[source]

Return a file from an online GitHub-like repository.

If a local copy is found then always use that to avoid network traffic.

Parameters:
  • name (str | os.PathLike | Sequence[str | os.PathLike]) – Name of the file or list/tuple of names of files containing the dataset(s) including suffixes.

  • github_url (str) – URL to GitHub repository where the data is stored.

  • branch (str, optional) – For GitHub-hosted files, the branch to download from.

  • cache_dir (Path) – The directory in which to search for and write cached data.

Return type:

Path | list[Path]

Returns:

Path | list[Path]

xclim.testing.utils.get_local_testdata(patterns, temp_folder, branch='master', _local_cache=PosixPath('/home/docs/.cache/xclim-testdata'))[source]

Copy specific testdata from a default cache to a temporary folder.

Return files matching pattern in the default cache dir and move to a local temp folder.

Parameters:
  • patterns (str | Sequence[str]) – Glob patterns, which must include the folder.

  • temp_folder (str | os.PathLike) – Target folder to copy files and filetree to.

  • branch (str) – For GitHub-hosted files, the branch to download from.

  • _local_cache (str | os.PathLike) – Local cache of testing data.

Return type:

Path | list[Path]

Returns:

Path | list[Path]

xclim.testing.utils.list_datasets(github_repo='Ouranosinc/xclim-testdata', branch='main')[source]

Return a DataFrame listing all xclim test datasets available on the GitHub repo for the given branch.

The result includes the filepath, as passed to open_dataset, the file size (in KB) and the html url to the file. This uses an unauthenticated call to GitHub’s REST API, so it is limited to 60 requests per hour (per IP). A single call of this function triggers one request per subdirectory, so use with parsimony.

xclim.testing.utils.list_input_variables(submodules=None, realms=None)[source]

List all possible variables names used in xclim’s indicators.

Made for development purposes. Parses all indicator parameters with the xclim.core.utils.InputKind.VARIABLE or OPTIONAL_VARIABLE kinds.

Parameters:
  • realms (Sequence of str, optional) – Restrict the output to indicators of a list of realms only. Default None, which parses all indicators.

  • submodules (str, optional) – Restrict the output to indicators of a list of submodules only. Default None, which parses all indicators.

Return type:

dict

Returns:

dict – A mapping from variable name to indicator class.

xclim.testing.utils.open_dataset(name, suffix=None, dap_url=None, github_url='https://github.com/Ouranosinc/xclim-testdata', branch='main', cache=True, cache_dir=PosixPath('/home/docs/.cache/xclim-testdata'), **kwargs)[source]

Open a dataset from the online GitHub-like repository.

If a local copy is found then always use that to avoid network traffic.

Parameters:
  • name (str or os.PathLike) – Name of the file containing the dataset.

  • suffix (str, optional) – If no suffix is given, assumed to be netCDF (‘.nc’ is appended). For no suffix, set “”.

  • dap_url (str, optional) – URL to OPeNDAP folder where the data is stored. If supplied, supersedes github_url.

  • github_url (str) – URL to GitHub repository where the data is stored.

  • branch (str, optional) – For GitHub-hosted files, the branch to download from.

  • cache_dir (Path) – The directory in which to search for and write cached data.

  • cache (bool) – If True, then cache data locally for use on subsequent calls.

  • **kwargs – For NetCDF files, keywords passed to xarray.open_dataset().

Return type:

Dataset

Returns:

Union[Dataset, Path]

See also

xarray.open_dataset

xclim.testing.utils.publish_release_notes(style='md', file=None, changes=None)[source]

Format release notes in Markdown or ReStructuredText.

Parameters:
  • style ({“rst”, “md”}) – Use ReStructuredText formatting or Markdown. Default: Markdown.

  • file ({os.PathLike, StringIO, TextIO}, optional) – If provided, prints to the given file-like object. Otherwise, returns a string.

  • changes ({str, os.PathLike}, optional) – If provided, manually points to the file where the changelog can be found. Assumes a relative path otherwise.

Return type:

str | None

Returns:

str, optional

Notes

This function is used solely for development and packaging purposes.

xclim.testing.utils.show_versions(file=None, deps=None)[source]

Print the versions of xclim and its dependencies.

Parameters:
  • file ({os.PathLike, StringIO, TextIO}, optional) – If provided, prints to the given file-like object. Otherwise, returns a string.

  • deps (list, optional) – A list of dependencies to gather and print version information from. Otherwise, prints xclim dependencies.

Return type:

str | None

Returns:

str or None

Module for loading testing data.

xclim.testing.helpers.PREFETCH_TESTING_DATA = False

Indicates whether the testing data should be downloaded when running tests.

Notes

When running tests multiple times, this flag allows developers to significantly speed up the pytest suite by preventing sha256sum checks for all downloaded files. Proceed with caution.

This can be set for both pytest and tox by exporting the variable:

$ export XCLIM_PREFETCH_TESTING_DATA=1

or setting the variable at runtime:

$ env XCLIM_PREFETCH_TESTING_DATA=1 pytest
xclim.testing.helpers.TESTDATA_BRANCH = 'main'

Sets the branch of Ouranosinc/xclim-testdata to use when fetching testing datasets.

Notes

When running tests locally, this can be set for both pytest and tox by exporting the variable:

$ export XCLIM_TESTDATA_BRANCH="my_testing_branch"

or setting the variable at runtime:

$ env XCLIM_TESTDATA_BRANCH="my_testing_branch" pytest
xclim.testing.helpers.add_example_file_paths(cache_dir)[source]

Create a dictionary of relevant datasets to be patched into the xdoctest namespace.

Return type:

dict[str]

xclim.testing.helpers.assert_lazy = <dask.callbacks.Callback object>

Context manager that raises an AssertionError if any dask computation is triggered.

xclim.testing.helpers.generate_atmos(cache_dir)[source]

Create the atmosds synthetic testing dataset.

xclim.testing.helpers.populate_testing_data(temp_folder=None, branch='main', _local_cache=PosixPath('/home/docs/.cache/xclim-testdata'))[source]

Perform _get_file or get_local_dataset calls to GitHub to download or copy relevant testing data.

xclim.testing.helpers.test_timeseries(values, variable, start='2000-07-01', units=None, freq='D', as_dataset=False, cftime=False)[source]

Create a generic timeseries object based on pre-defined dictionaries of existing variables.