Climate indices computation package based on Xarray with extensive climate analysis capabilities
Land surface climate indicators focusing on snow, streamflow, and terrestrial processes for hydrological and agricultural applications. These indicators are essential for water resource management, flood forecasting, and agricultural planning.
Snow depth and snow water equivalent analysis for hydrological modeling and water resource assessment.
def snow_depth(snd, freq="YS"):
"""
Mean snow depth over specified period.
Parameters:
- snd: xr.DataArray, daily snow depth data
- freq: str, resampling frequency (default "YS" for yearly)
Returns:
xr.DataArray: Mean snow depth
"""
def snd_max(snd, freq="YS"):
"""
Maximum snow depth over specified period.
Parameters:
- snd: xr.DataArray, daily snow depth data
- freq: str, resampling frequency
Returns:
xr.DataArray: Maximum snow depth
"""
def snd_season_length(snd, thresh="2 cm", freq="YS"):
"""
Length of snow season based on snow depth threshold.
Parameters:
- snd: xr.DataArray, daily snow depth data
- thresh: str or float, snow depth threshold (default "2 cm")
- freq: str, resampling frequency
Returns:
xr.DataArray: Snow season length in days
"""
def continuous_snow_season_start(snd, thresh="2 cm", window=5, freq="YS"):
"""
Start date of continuous snow season.
Parameters:
- snd: xr.DataArray, daily snow depth data
- thresh: str or float, snow depth threshold (default "2 cm")
- window: int, minimum number of consecutive days (default 5)
- freq: str, resampling frequency
Returns:
xr.DataArray: Snow season start day of year
"""
def continuous_snow_season_end(snd, thresh="2 cm", window=5, freq="YS"):
"""
End date of continuous snow season.
Parameters:
- snd: xr.DataArray, daily snow depth data
- thresh: str or float, snow depth threshold (default "2 cm")
- window: int, minimum number of consecutive days (default 5)
- freq: str, resampling frequency
Returns:
xr.DataArray: Snow season end day of year
"""
def snw_max(snw, freq="YS"):
"""
Maximum snow water equivalent.
Parameters:
- snw: xr.DataArray, daily snow water equivalent data
- freq: str, resampling frequency
Returns:
xr.DataArray: Maximum snow water equivalent
"""
def melt_and_precip_max(snw, pr, window=3, freq="YS"):
"""
Maximum snow melt and precipitation sum over window.
Parameters:
- snw: xr.DataArray, daily snow water equivalent data
- pr: xr.DataArray, daily precipitation data
- window: int, window size in days (default 3)
- freq: str, resampling frequency
Returns:
xr.DataArray: Maximum combined melt and precipitation
"""Streamflow characteristics and hydrological indices for river flow analysis and flood risk assessment.
def base_flow_index(q, freq="YS"):
"""
Base flow index (ratio of base flow to total flow).
Parameters:
- q: xr.DataArray, daily streamflow data
- freq: str, resampling frequency
Returns:
xr.DataArray: Base flow index (0-1)
"""
def rb_flashiness_index(q, freq="YS"):
"""
Richards-Baker flashiness index for flow variability.
Parameters:
- q: xr.DataArray, daily streamflow data
- freq: str, resampling frequency
Returns:
xr.DataArray: Flashiness index
"""
def snd_storm_days(snd, thresh="10 cm", freq="YS"):
"""
Number of snow storm days (large daily snow depth increases).
Parameters:
- snd: xr.DataArray, daily snow depth data
- thresh: str or float, storm threshold (default "10 cm")
- freq: str, resampling frequency
Returns:
xr.DataArray: Number of snow storm days
"""
def high_flow_frequency(q, thresh="90%", freq="YS"):
"""
Frequency of high flow events above threshold.
Parameters:
- q: xr.DataArray, daily streamflow data
- thresh: str or float, flow threshold (default "90%" for 90th percentile)
- freq: str, resampling frequency
Returns:
xr.DataArray: Number of high flow events
"""
def low_flow_frequency(q, thresh="10%", freq="YS"):
"""
Frequency of low flow events below threshold.
Parameters:
- q: xr.DataArray, daily streamflow data
- thresh: str or float, flow threshold (default "10%" for 10th percentile)
- freq: str, resampling frequency
Returns:
xr.DataArray: Number of low flow events
"""import xarray as xr
import xclim.land as xcl
# Load snow depth data
snd = xr.open_dataset("snow_data.nc").snd
# Calculate snow indicators
max_snow = xcl.snd_max(snd, freq="YS")
season_length = xcl.snd_season_length(snd, thresh="5 cm", freq="YS")
season_start = xcl.continuous_snow_season_start(
snd, thresh="5 cm", window=10, freq="YS"
)# Load streamflow data
q = xr.open_dataset("streamflow.nc").discharge
# Calculate streamflow indicators
base_flow = xcl.base_flow_index(q, freq="YS")
flashiness = xcl.rb_flashiness_index(q, freq="YS")
high_flows = xcl.high_flow_frequency(q, thresh="95%", freq="YS")