Learn R Programming

tidyextreme (version 1.0.0)

calculate_WSDI: Calculate Warm Spell Duration Index (WSDI)

Description

Calculates the number of days with at least 6 consecutive days where temperature exceeds the 90th percentile, following ETCCDI definition WSDI.

Usage

calculate_WSDI(
  df,
  frequency = "daily",
  time_col = NULL,
  tmax_col = NULL,
  temp_col = NULL,
  window_days = 30,
  min_consecutive = 6
)

Value

A tibble with columns: year, WSDI, n_spells, mean_spell_length

Arguments

df

Data frame with climate data

frequency

Temporal frequency: "daily" or "hourly" (string)

time_col

Name of the time column (string). For daily frequency, the column should be of class Date or a string in the format YYYY-MM-DD. For hourly frequency, the column should be of class POSIXct or a string in the format YYYY-MM-DD HH:MM:SS.

tmax_col

Name of maximum temperature column (daily data) (string)

temp_col

Name of temperature column (for single temp or hourly) (string)

window_days

Window size for percentile calculation (default: 30)

min_consecutive

Minimum consecutive days for warm spell (default: 6)

Examples

Run this code
# Daily data with maximum temperature
set.seed(123)
daily_data <- data.frame(
  date = seq(as.Date("2000-01-01"), as.Date("2002-12-31"), by = "day"),
  tmax = 25 + 10 * sin(seq(0, 4*pi, length.out = 1096)) + rnorm(1096, 0, 5)
)

calculate_WSDI(
  df = daily_data,
  frequency = "daily",
  time_col = "date",
  tmax_col = "tmax"
)

# With custom window and consecutive days
calculate_WSDI(
  df = daily_data,
  frequency = "daily",
  time_col = "date",
  tmax_col = "tmax",
  window_days = 15,
  min_consecutive = 5
)

Run the code above in your browser using DataLab