Welcome to climate-library’s documentation!

ClimateIndex Library

ClimateIndex Class

The ClimateIndex class provides a systematic and convenient interface for preprocessing climate data and calculating a wide range of climate indices. It can handle different climate variables, including temperature and precipitation, and allows for the selection, resampling, and filling of missing values according to user-defined parameters.

Testing

The class also includes a testing method to ensure all calculations are performed correctly. The modularity of the class allows for easy expansion to include additional climate indices in the future.

Installation

From PyPI (when available)

You can install the package using pip:

pip install climate_library

From GitHub

Alternatively, you can install the library directly from the GitHub repository:

pip install git+https://github.com/shiv3679/climate-indices.git

Dependencies

The following packages are required dependencies and will be installed automatically:

  • xarray

  • xclim

  • numpy

Compatibility

The library is compatible with Python 3.6 and higher.

Usage

Initialization

Create an instance of the ClimateIndex class by providing the path to your climate data file. This class is the main entry point for working with climate indices in this library.

Example

from climate_library.climate_index import ClimateIndex

# Path to the NetCDF file containing climate data
datafile = 'path/to/your/datafile.nc'

# Create an instance of the ClimateIndex class
climate_index = ClimateIndex(datafile)

The datafile parameter should be the path to a NetCDF file that contains the climate data you want to analyze. The file must include dimensions for time, latitude, and longitude, and it must contain the relevant climate variables (e.g., temperature, precipitation) that you wish to use in calculating climate indices.

Once you have created an instance of the ClimateIndex class, you can use its methods to preprocess the data and calculate various climate indices, as described in the following sections.

Preprocessing Method

climate_index.pre_process(time_range=None, fill_missing=None, resample_freq=None, months=None, season=None, resample_method='mean')
  • time_range (tuple, optional): This parameter allows you to specify a start and end time for the data you want to analyze. By providing a tuple with two date strings, you can subset the dataset to include only the observations within that time frame.

    • Example: time_range=('1961-01-01', '1990-12-31') will include only the data from January 1, 1961, to December 31, 1990.

    • Use Case: This can be useful when analyzing climate patterns over specific periods or when comparing different decades.

  • fill_missing (int, float, or ‘interpolate’, optional): This parameter provides options to handle missing values (NaN) in the dataset.

    • If a numerical value is provided, all missing values will be replaced with that number.

    • If interpolate is specified, linear interpolation will be used to estimate missing values based on neighbouring observations.

    • Example: fill_missing=0 will replace all NaN values with 0.

    • Use Case: Handling missing values is crucial in climate analysis to avoid bias and errors in subsequent calculations.

  • resample_freq (str, optional): This parameter defines the frequency at which the data should be resampled. It allows you to change the time-frequency of the dataset.

    • Example: resample_freq='MS' will resample the data to monthly frequency, starting at the beginning of each month.

    • Use Case: Resampling is useful when analysing data at a different temporal resolution, such as monthly or yearly.

  • months (list, optional): This parameter allows you to select specific months from the dataset.

    • Example: months=[1, 2, 3] will include only the data from January, February, and March.

    • Use Case: Useful for seasonal analysis or when studying climate behaviour during particular months.

  • season (str, optional): This parameter enables the selection of data for a specific meteorological season.

    • Example: season=‘JJA’ will include only the data from the summer months (June, July, August).

    • Use Case: Helpful for analyzing seasonal patterns and variations in climate.

  • resample_method (str, optional, default=‘mean’): This parameter defines the method used for resampling when changing the time-frequency with resample_freq.

    • Default is mean, meaning that the mean value of the observations within each resampling period will be used.

    • Other methods, such as sum, max, etc., can be specified if needed.

    • Use Case: This provides flexibility in how the data is aggregated when resampling, allowing for different types of analysis and interpretation.

  • Units Check: The function also performs a check on the unit of the data variables. It supports ‘K’ for temperature and ‘m’ for precipitation.

  • Special Rule: If season is ‘DJF’, and resample_freq is not specified, it defaults to ‘QS-DEC’.

climate_index.pre_process(time_range=('2000-01-01', '2020-12-31'), resample_freq='M', fill_missing='interpolate')

This example demonstrates how to preprocess the data by selecting a time range, resampling to monthly frequency, and interpolating missing values. Adjust these parameters according to your specific needs and analysis goals.

Climate Indices Calculation Methods

Temperature Indices

TX10P (Temperature Exceeding 10th Percentile)
tx10p_index = climate_index.calculate_tx10p()
  • Description: Calculates the number of days when the maximum temperature exceeds the 10th percentile of a reference period.

  • Calculation: Computed by first calculating the 10th percentile of daily maximum temperature over a reference period and then counting how many days in the target period exceed this threshold.

TX90P (Temperature Exceeding 90th Percentile)
tx90p_index = climate_index.calculate_tx90p()
  • Description: Calculates the number of days when the maximum temperature exceeds the 90th percentile of a reference period.

  • Calculation: Similar to TX10P, but using the 90th percentile as the threshold.

TN10P (Temperature Below 10th Percentile)
tn10p_index = climate_index.calculate_tn10p()
  • Description: Calculates the number of days when the minimum temperature is below the 10th percentile of a reference period.

  • Calculation: Similar to TX10P, but for daily minimum temperature.

TN90P (Temperature Exceeding 90th Percentile)
tn90p_index = climate_index.calculate_tn90p()
  • Description: Calculates the number of days when the minimum temperature exceeds the 90th percentile of a reference period.

  • Calculation: Similar to TN10P, but using the 90th percentile as the threshold.

Frost Days
frost_days_index = climate_index.calculate_frost_days()
  • Description: Counts the number of days when the minimum temperature falls below 0°C.

  • Calculation: Number of days when daily minimum temperature falls below freezing.

Warm Spell Duration Index
warm_spell_index = climate_index.calculate_warm_spell()
  • Description: Measures the duration of warm spells when daily maximum temperature exceeds the 90th percentile for consecutive days.

  • Calculation: Length of periods exceeding the 90th percentile of maximum temperature.

Cold Spell Duration Index
cold_spell_index = climate_index.calculate_cold_spell()
  • Description: Measures the duration of cold spells when daily minimum temperature falls below the 10th percentile for consecutive days.

  • Calculation: Length of periods falling below the 10th percentile of minimum temperature.

Icing Days
id_index = climate_index.calculate_id()
  • Description: Counts the number of days when the maximum temperature falls below 0°C.

  • Calculation: Number of days when daily maximum temperature is below freezing (273.15 K).

Summer Days
su_index = climate_index.calculate_su()
  • Description: Counts the number of days when the maximum temperature exceeds 25°C.

  • Calculation: Number of days when daily maximum temperature is above 25°C (298.15 K).

Tropical Nights
tr_index = climate_index.calculate_tr()
  • Description: Counts the number of days when the minimum temperature exceeds 20°C.

  • Calculation: Number of days when daily minimum temperature is above 20°C (293.15 K).

Growing Season Length (GSL)
gsl_index = climate_index.calculate_gsl()
  • Description: Calculates the length of the growing season in days.

  • Calculation: The growing season starts with the first run of at least 6 days where the daily mean temperature exceeds 5°C and ends with the first run of at least 6 days where the daily mean temperature falls below 5°C after July 1st.

Diurnal Temperature Range (DTR)
dtr_index = climate_index.calculate_dtr()
  • Description: Calculates the mean monthly diurnal temperature range.

  • Calculation: The difference between the daily maximum and minimum temperature is averaged over each month.

Precipitation Indices

Percentile Precipitation
rXXp_index_time_series = climate_index.calculate_percentile_p(precip_var='tp', percentile=95, wet_day_thresh_mm=1.0, reference_period=('1961-01-01', '1990-12-31'), resample_freq='Y')
  • Description: Calculates the sum of precipitation on days exceeding a specified percentile.

  • Calculation: Sum of precipitation on days exceeding the threshold defined by the percentile.

  • Example: To calculate the sum of precipitation on days exceeding the 95th percentile, with a wet day threshold of 1 mm, and reference period from 1961 to 1990, use percentile=95, wet_day_thresh_mm=1.0, and reference_period=(‘1961-01-01’, ‘1990-12-31’).

Precipitation Days
precip_days_index = climate_index.calculate_precip_days(precip_var='tp', threshold_mm=10.0, resample_freq='Y')
  • Description: Counts the number of days with precipitation above a specified threshold.

  • Calculation: Number of days with precipitation exceeding the threshold.

  • Example: To calculate the number of days with precipitation above 10 mm, use threshold_mm=10.0.

RXnDay
rxnday_index = climate_index.calculate_rxnday(precip_var='tp', n_days=5, resample_freq='M')
  • Description: Finds the maximum precipitation sum for a specified number of consecutive days.

  • Calculation: Maximum sum of precipitation for the defined number of consecutive days.

  • Example: To calculate the maximum 5-day consecutive precipitation sum, use n_days=5.

Consecutive Wet Days (CWD)
cwd_index = climate_index.calculate_cwd(precip_var='tp')
  • Description: Calculates the maximum length of consecutive wet days in a year.

  • Calculation:Length of the longest sequence of wet days (precipitation equal to or greater than 1 mm).

Consecutive Dry Days (CDD)
cdd_index = climate_index.calculate_cdd(precip_var='tp')
  • Description: Calculates the maximum length of consecutive dry days in a year.

  • Calculation:Length of the longest sequence of dry days (precipitation less than 1 mm).

Simple Precipitation Intensity Index (SDII)
sdii_index = climate_index.calculate_sdii(precip_var='tp')
  • Description: Calculates the average precipitation intensity on wet days.

  • Calculation:Sum of precipitation on wet days (≥ 1 mm) divided by the total number of wet days in each year.

These indices provide various ways to quantify temperature and precipitation extremes, trends, and variability, aiding in climate analysis, pattern recognition, and decision-making.

Testing Indices

The test_indices method provides a way to test the implemented indices for a specific data type (either ‘temperature’ or ‘precipitation’).

climate_index.test_indices(data_type='temperature')

Importance

  • Validation: Tests the implemented indices to ensure that they are functioning correctly.

  • Debugging: Helps in identifying and resolving any errors or inconsistencies.

  • Flexibility: Allows testing for different data types (temperature or precipitation) based on the user’s needs.

Functionality

  • Temperature Testing: If data_type='temperature', the following methods will be tested:

    • calculate_tx10p

    • calculate_tx90p

    • calculate_tn10p

    • calculate_tn90p

    • calculate_frost_days

    • calculate_warm_spell

    • calculate_cold_spell

    • calculate_dtr

    • calculate_su

    • calculate_id

    • calculate_tr

    • calculate_gsl

  • Precipitation Testing: If data_type='precipitation', the following methods will be tested:

    • calculate_percentile_p

    • calculate_precip_days

    • calculate_rxnday

    • calculate_cdd

    • calculate_cwd

    • calculate_sdii

Example Usage

  • To test all temperature-related indices:

    climate_index.test_indices(data_type='temperature')
    
  • To test all precipitation-related indices:

    climate_index.test_indices(data_type='precipitation')
    

Authors

Shiv Shankar Singh

Akshit Nanda

The authors would like to acknowledge the support of Dr. Raju Attada and Sree Hari from the WEATHER AND CLIMATE MODELLING RESEARCH GROUP (IISER MOHALI) in the development of this library.

Contact

For any inquiries or collaboration, please feel free to reach out to the authors via the provided email addresses.

License

This project is licensed under the MIT License.

class climate_index.ClimateIndex(datafile)

A class to handle climate index calculations.

Parameters:

datafile (str) – The path to the climate data file in netCDF format.

Note

The class is designed to load climate data and provide various index calculations. After initializing, use the pre_process method to preprocess the data before calculating any indices.

Example:

>>> climate_index = ClimateIndex("path/to/datafile.nc")
Variables:
  • datafile (str) – The path to the climate data file.

  • data (xarray.Dataset or None) – The loaded climate data, initialized as None and populated using the pre_process method.

calculate_cdd(precip_var='tp')

Calculate Consecutive Dry Days (CDD) based on daily precipitation data and a threshold.

This method computes the CDD index, which represents the maximum length of dry spells, defined as periods with daily precipitation less than 1 mm, in a year.

The input netCDF file should contain daily precipitation data with a variable that can be accessed using the precip_var parameter.

Parameters:

precip_var (str, optional) – The variable name in the netCDF file that contains daily precipitation data. (default is ‘tp’)

Returns:

An xarray Dataset containing the CDD values for each year.

Return type:

xarray.Dataset

Example:

>>> cdd_index = climate_index.calculate_cdd(precip_var='tp')
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_cold_spell(temp_var='t2m')

Calculate the Cold Spell Duration Index (CSDI).

This method measures the duration of cold spells when the daily minimum temperature falls below the 10th percentile for consecutive days.

The input netCDF file should contain daily temperature data with a variable that can be accessed using the temp_var parameter. The 10th percentile is calculated over the period from 1961 to 1990.

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. (default is ‘t2m’)

Returns:

An xarray Dataset containing the Cold Spell Duration Index.

Return type:

xarray.Dataset

Example:

>>> cold_spell_index = climate_index.calculate_cold_spell()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_cwd(precip_var='tp')

Calculate Consecutive Wet Days (CWD) based on daily precipitation data and a threshold.

This method computes the CWD index, which represents the maximum length of wet spells, defined as periods with daily precipitation equal to or greater than 1 mm, in a year.

The input netCDF file should contain daily precipitation data with a variable that can be accessed using the precip_var parameter.

Parameters:

precip_var (str, optional) – The variable name in the netCDF file that contains daily precipitation data. (default is ‘tp’)

Returns:

An xarray Dataset containing the CWD values for each year.

Return type:

xarray.Dataset

Example:

>>> cwd_index = climate_index.calculate_cwd(precip_var='tp')
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_dtr(temp_var='t2m')

Calculate the Mean Monthly Diurnal Temperature Range (DTR).

This method calculates the mean monthly diurnal temperature range, which is the difference between the daily maximum and minimum temperature averaged over each month.

The input netCDF file should contain daily temperature data with a variable that can be accessed using the temp_var parameter.

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. (default is ‘t2m’)

Returns:

An xarray Dataset containing the mean monthly diurnal temperature range.

Return type:

xarray.Dataset

Example:

>>> dtr_index = climate_index.calculate_dtr()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_frost_days(temp_var='t2m')

Count the number of days when the minimum temperature falls below 0°C (Frost Days).

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. Default is ‘t2m’.

Returns:

An xarray Dataset containing the count of Frost Days.

Return type:

xarray.Dataset

Example:

>>> frost_days_index = climate_index.calculate_frost_days()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_gsl(temp_var='t2m')

Calculate the Length of the Growing Season (GSL) in days.

This method calculates the length of the growing season based on daily mean temperature data.

The input netCDF file should contain daily temperature data with a variable that can be accessed using the temp_var parameter.

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. (default is ‘t2m’)

Returns:

An xarray Dataset containing the length of the growing season in days.

Return type:

xarray.Dataset

Example:

>>> gsl_index = climate_index.calculate_gsl()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed. Additional warnings will be issued if no growing season or end of growing season is found in the dataset.

calculate_id(temp_var='t2m')

Calculate the Number of Icing Days (ID) in a year.

This method calculates the number of days in a year when the daily maximum temperature falls below 0°C (273.15 K).

The input netCDF file should contain daily temperature data with a variable that can be accessed using the temp_var parameter.

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. (default is ‘t2m’)

Returns:

An xarray Dataset containing the number of icing days for each year.

Return type:

xarray.Dataset

Example:

>>> id_index = climate_index.calculate_id()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_percentile_p(precip_var='tp', percentile=95, wet_day_thresh_mm=1.0, reference_period=('1961-01-01', '1990-12-31'), resample_freq='Y')

Calculate the Sum of Precipitation on Days Exceeding a Specified Percentile (Percentile Precipitation).

This method calculates the sum of precipitation on days that exceed a given percentile for a specified period.

The input netCDF file should contain daily precipitation data with a variable that can be accessed using the precip_var parameter.

Parameters:
  • precip_var (str, optional) – The variable name in the netCDF file that contains daily precipitation data. (default is ‘tp’)

  • percentile (int, optional) – The percentile to use for identifying days with significant precipitation. (default is 95)

  • wet_day_thresh_mm (float, optional) – The precipitation threshold in millimeters to consider a day as ‘wet’. (default is 1.0)

  • reference_period (tuple of str, optional) – The time period to use for calculating the percentile. (default is (‘1961-01-01’, ‘1990-12-31’))

  • resample_freq (str, optional) – The frequency for resampling the data. Common options include ‘M’ for monthly and ‘Y’ for yearly. (default is ‘Y’)

Returns:

An xarray Dataset containing the sum of precipitation on days exceeding the percentile for each resampling period.

Return type:

xarray.Dataset

Example:

>>> rXXp_index_time_series = climate_index.calculate_percentile_p(precip_var='tp', percentile=95, wet_day_thresh_mm=1.0, reference_period=('1961-01-01', '1990-12-31'), resample_freq='Y')
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_precip_days(precip_var='tp', threshold_mm=10.0, resample_freq='Y')

Count the Number of Days with Precipitation Above a Specified Threshold (Precipitation Days).

This method counts the number of days in each resampling period where the precipitation amount exceeds a given threshold.

The input netCDF file should contain daily precipitation data with a variable that can be accessed using the precip_var parameter.

Parameters:
  • precip_var (str, optional) – The variable name in the netCDF file that contains daily precipitation data. (default is ‘tp’)

  • threshold_mm (float, optional) – The precipitation threshold in millimeters to consider a day as having significant precipitation. (default is 10.0)

  • resample_freq (str, optional) – The frequency for resampling the data. Common options include ‘M’ for monthly and ‘Y’ for yearly. (default is ‘Y’)

Returns:

An xarray Dataset containing the count of days with precipitation above the threshold for each resampling period.

Return type:

xarray.Dataset

Example:

>>> precip_days_index = climate_index.calculate_precip_days(precip_var='tp', threshold_mm=10.0, resample_freq='Y')
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_rxnday(precip_var='tp', n_days=5, resample_freq='M')

Calculate Maximum Precipitation Sum for a Specified Number of Consecutive Days (RXnDay).

This method finds the maximum sum of precipitation for a specified number of consecutive days, typically resampled over each month.

The input netCDF file should contain daily precipitation data with a variable that can be accessed using the precip_var parameter.

Parameters:
  • precip_var (str, optional) – The variable name in the netCDF file that contains daily precipitation data. (default is ‘tp’)

  • n_days (int, optional) – The number of consecutive days to consider for finding the maximum precipitation sum. (default is 5)

  • resample_freq (str, optional) – The frequency for resampling the data. Common options include ‘M’ for monthly and ‘Y’ for yearly. (default is ‘M’)

Returns:

An xarray Dataset containing the RXnDay values resampled according to the frequency.

Return type:

xarray.Dataset

Example:

>>> rxnday_index = climate_index.calculate_rxnday(precip_var='tp', n_days=5, resample_freq='M')
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_sdii(precip_var='tp')

Calculate Simple Precipitation Intensity Index (SDII) based on daily precipitation data.

This method computes the SDII index, which represents the mean precipitation amount on wet days (days with precipitation equal to or greater than 1 mm) in a year.

The input netCDF file should contain daily precipitation data with a variable that can be accessed using the precip_var parameter.

Parameters:

precip_var (str, optional) – The variable name in the netCDF file that contains daily precipitation data. (default is ‘tp’)

Returns:

An xarray Dataset containing the SDII values for each year.

Return type:

xarray.Dataset

Example:

>>> sdii_index = climate_index.calculate_sdii(precip_var='tp')
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_su(temp_var='t2m')

Calculate the Number of Summer Days (SU) in a year.

This method calculates the number of days in a year when the daily maximum temperature exceeds 25°C (298.15 K).

The input netCDF file should contain daily temperature data with a variable that can be accessed using the temp_var parameter.

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. (default is ‘t2m’)

Returns:

An xarray Dataset containing the number of summer days for each year.

Return type:

xarray.Dataset

Example:

>>> su_index = climate_index.calculate_su()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_tn10p(temp_var='t2m')

Calculate the number of days when minimum temperature is below the 10th percentile (TN10P).

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. Default is ‘t2m’.

Returns:

An xarray Dataset containing the TN10P index.

Return type:

xarray.Dataset

The 10th percentile is calculated over the period from 1961 to 1990.

Example:

>>> tn10p_index = climate_index.calculate_tn10p()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_tn90p(temp_var='t2m')

Calculate the number of days when minimum temperature exceeds the 90th percentile (TN90P).

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. Default is ‘t2m’.

Returns:

An xarray Dataset containing the TN90P index.

Return type:

xarray.Dataset

The 90th percentile is calculated over the period from 1961 to 1990.

Example:

>>> tn90p_index = climate_index.calculate_tn90p()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_tr(temp_var='t2m')

Calculate the Number of Tropical Nights (TR) in a year.

This method calculates the number of days in a year when the daily minimum temperature exceeds 20°C (293.15 K).

The input netCDF file should contain daily temperature data with a variable that can be accessed using the temp_var parameter.

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. (default is ‘t2m’)

Returns:

An xarray Dataset containing the number of tropical nights for each year.

Return type:

xarray.Dataset

Example:

>>> tr_index = climate_index.calculate_tr()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_tx10p(temp_var='t2m')

Calculate the number of days when maximum temperature exceeds the 10th percentile (TX10P).

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. Default is ‘t2m’.

Returns:

An xarray Dataset containing the TX10P index.

Return type:

xarray.Dataset

The 10th percentile is calculated over the period from 1961 to 1990.

Example:

>>> tx10p_index = climate_index.calculate_tx10p()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_tx90p(temp_var='t2m')

Calculate the number of days when maximum temperature exceeds the 90th percentile (TX90P).

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. Default is ‘t2m’.

Returns:

An xarray Dataset containing the TX90P index.

Return type:

xarray.Dataset

The 90th percentile is calculated over the period from 1961 to 1990.

Example:

>>> tx90p_index = climate_index.calculate_tx90p()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

calculate_warm_spell(temp_var='t2m')

Calculate the Warm Spell Duration Index (WSDI).

This method measures the duration of warm spells when the daily maximum temperature exceeds the 90th percentile for consecutive days.

The input netCDF file should contain daily temperature data with a variable that can be accessed using the temp_var parameter. The 90th percentile is calculated over the period from 1961 to 1990.

Parameters:

temp_var (str, optional) – The variable name in the netCDF file that contains daily temperature data. (default is ‘t2m’)

Returns:

An xarray Dataset containing the Warm Spell Duration Index.

Return type:

xarray.Dataset

Example:

>>> warm_spell_index = climate_index.calculate_warm_spell()
Warnings:

An exception will be caught and a warning will be issued if the calculation cannot be performed.

pre_process(time_range=None, fill_missing=None, resample_freq=None, months=None, season=None, resample_method='mean')

Pre-process the climate data for further analysis.

Parameters:
  • time_range (tuple, optional) – A tuple specifying the start and end time for data selection. Example: (‘1961-01-01’, ‘1990-12-31’). Default is None.

  • fill_missing (int, float, or str, optional) – The method for filling missing values, either a numerical value or ‘interpolate’. Example: 0. Default is None.

  • resample_freq (str, optional) – The frequency for resampling the data. Example: ‘MS’. Default is None.

  • months (list of int, optional) – A list of integers specifying the months for data selection. Example: [1, 2, 3]. Default is None.

  • season (str, optional) – A string specifying the season for data selection (‘DJF’, ‘MAM’, ‘JJA’, ‘SON’). Example: ‘JJA’. Default is None.

  • resample_method (str, optional) – The method for resampling, should be a method of xarray’s DataArray resampler. Default is ‘mean’.

Returns:

None

Return type:

None

The function updates the instance’s data attribute with the pre-processed data. - Units Check: The function supports ‘K’ for temperature and ‘m’ for precipitation. - Special Rule: If season is ‘DJF’, and resample_freq is not specified, it defaults to ‘QS-DEC’.

Example:

>>> climate_index.pre_process(time_range=('2000-01-01', '2020-12-31'), resample_freq='M', fill_missing='interpolate')
Warnings:

An exception will be caught and a warning will be issued if the pre-processing cannot be performed.

climate_index.first_run(data, window, dim='time')

Identify and count consecutive sequences (runs) of days that satisfy a condition.

Parameters:
  • data (xarray.DataArray) – The xarray data containing the variable to check.

  • window (int) – The minimum length of a run to be counted.

  • dim (str, optional) – The dimension along which to find runs (default is ‘time’).

Returns:

An xarray DataArray containing the length of runs that satisfy the condition.

Return type:

xarray.DataArray

Note

The function assumes that the condition to check is already applied to the data passed.

Example:

>>> data = xr.DataArray([True, True, False, True, True, True], dims=['time'])
>>> first_run(data, window=2)
climate_index.longest_run(arr, condition_func)

Calculate the length of the longest run in an array that satisfies a condition.

Parameters:
  • arr (iterable) – The array containing the elements to check.

  • condition_func (callable) – The function that defines the condition to check.

Returns:

The length of the longest run that satisfies the condition.

Return type:

int

Example:

>>> longest_run([True, True, False, True, True, True], lambda x: x)

Indices and tables