Learn R Programming

tidyextreme (version 1.0.0)

calculate_CWD: Calculate consecutive wet days (CWD)

Description

Calculates statistics for wet spells (consecutive days with precipitation \(\geq\) 1 mm), following ETCCDI definition CWD.

Usage

calculate_CWD(
  df,
  frequency = "daily",
  time_col = NULL,
  prcp_col = NULL,
  precip_col = NULL,
  wet_threshold = 1
)

Value

A data.frame with columns: year, CWD_max, CWD_mean, CWD_median, n_wet_spells

Arguments

df

Data frame with precipitation data

frequency

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

time_col

Name of the time column (string). Must be in a format recognizable by lubridate (e.g., Date for daily data, POSIXct for hourly data). Recommended formats: - Daily: YYYY-MM-DD (e.g., "2023-01-15") - Hourly: YYYY-MM-DD HH:MM:SS (e.g., "2023-01-15 14:30:00")

prcp_col

Name of precipitation column (daily data) (string)

precip_col

Name of precipitation column (hourly data) (string)

wet_threshold

Threshold for wet day in mm (default: 1)

Examples

Run this code
# Daily precipitation data
daily_prcp <- data.frame(
  date = seq(as.Date("2000-01-01"), as.Date("2002-12-31"), by = "day"),
  rainfall = pmax(0, rgamma(1096, shape = 0.5, scale = 10))
)

# Calculate consecutive wet days statistics
calculate_CWD(
  df = daily_prcp,
  frequency = "daily",
  time_col = "date",
  prcp_col = "rainfall"
)

# With custom wet threshold (5mm instead of 1mm)
calculate_CWD(
  df = daily_prcp,
  frequency = "daily",
  time_col = "date",
  prcp_col = "rainfall",
  wet_threshold = 5
)

Run the code above in your browser using DataLab