I’m working with an Xarray data array (data
) containing precipitation data. I want to analyze droughts as years falling below a specific threshold. Here’s what I’m trying to achieve without relying on explicit loops (ideal for large 3D datasets):
Cumulative Drought Events: Calculate the cumulative number of drought years, but reset the count whenever there’s a transition from drought (True
) to non-drought (False
).
For example, with (data < threshold) = [0,0,0,1,1,1,0,0,1,0]
, the desired output is cumulative = [0,0,0,1,2,3,0,0,1,0]
.
Drought Duration: Obtain an array indicating the duration (in years) of each drought event.
Following the previous example, (data < threshold) = [0,0,0,1,1,1,0,0,1,0]
should return duration = [0,0,0,3,0,0,0,0,1,0]
.
Drought Severity Time Series: Calculate a time series representing the total precipitation deficit during drought years. Defining deficit = threshold - data
, for the example data, the desired severity series would be [0,0,0, (deficit[3:6].sum()), 0,0,0,0, (deficit[8:9].sum()), 0]
.
I’ve tried implementing this using a for loop,but I plan on applying this to a very large 3D spatial dataset (lat,lon,year), therefore I am ideally looking for a solution that does not rely on looping over the time array, if that’s at all possible.