I tried to implement a moving percentile on climatic data using xarray
.
I've a dataset with daily min and max temperature, something like this:
<xarray.Dataset>
Dimensions: (latitude: 60, longitude: 20, time: 14456)
Coordinates:
* latitude (latitude) float64 39.05 39.05 39.15 39.15 ... 41.85 41.95 41.95
* longitude (longitude) float64 8.05 8.15 8.25 8.35 ... 9.65 9.75 9.85 9.95
* time (time) datetime64[ns] 1980-01-01 1980-01-02 ... 2019-07-30
Data variables:
tn (time, latitude, longitude) float32 nan nan nan ... nan nan nan
tx (time, latitude, longitude) float32 nan nan nan ... nan nan nan
Attributes:
E-OBS_version: 20.0e
Conventions: CF-1.4
References: http://surfobs.climate.copernicus.eu/dataaccess/access_eo...
history: Wed May 13 17:26:55 2020: ncrcat tn_ens_mean_0.1deg_reg_1...
NCO: netCDF Operators version 4.8.1 (Homepage = http://nco.sf....
For each day of the year I want to compute a moving quantile taking a moving window of 5 days. For better understanding see this pic, the window take 5 days of each year and compute the quantile (the green, red and blue boxes represent the moving quantile filter).
I didn't found the correct combination of rolling
and groupby
operators to achieve my purpose.
So I solved it using a (slow) for loop. I created a list of dates and I use sel
operator to select and compute the quantile, something like:
# all_days_lists is a list of lists
for days_list in all_days_lists:
# select days of interest
tmp_ds = xdataset.sel(time=days_list)
# compute 90th percentile
p90 = tmp_ds.quantile(0.9,dim='time')
Any ideas to solve this problem? Thanks a lot.