Computing 'seasonal means' spanning 4 months (with resample or groupy) #6180
-
Climatologists often use 'seasonal means', i.e. means over 3 months. Useful periods are DJF for December-January-February, MAM, JJA and SON. groupby or resample are nice functions to compute these seasonal means. sea for example : But some studies need means over 4 months : DJFM, MAMJ, JJAS and SOND. Would it be feasible that these 4-month periods are recognized by groupby and resample ? For resample, we define 3-month means with a syntax like : resample(time='QS-DEC') A resampling over 4 months is more tricky : it is not a real resampling, as some months are repeated. We still need 4 seasonal values ... Thanks, Olivier |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
You can use import xarray as xr
# load example dataset and create monthly means
air = xr.tutorial.open_dataset("air_temperature")
air = air.resample(time="M").mean()
# find months belonging to season
DJFM = air.time.dt.month.isin([12, 1, 2, 3])
MAMJ = air.time.dt.month.isin([3, 4, 5, 6])
concat_dim = xr.DataArray(["DJFM", "MAMJ"], dims="season")
seasons = xr.concat([DJFM, MAMJ], dim=concat_dim)
# calc mean over seasons
xr.dot(air.air, seasons) |
Beta Was this translation helpful? Give feedback.
-
This will compute wrong results for DJFM : il will average the JFM values at the beginning of the year with December of the same year. It should use Dec of previous year. Is there a way to have a correct computation ? it would really help ! |
Beta Was this translation helpful? Give feedback.
-
Iris correctly jump over year change :-)
But it don’t take into account the correct length of each month :-(
dataset.Var.values = array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.,
14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26.,
27., 28., 29., 30., 31., 32., 33., 34., 35., 36.])
dataset_seasons.Var.values = array([ 2. , 4.5, 7.5, 10.5, 13.5, 16.5, 19.5, 22.5, 25.5, 28.5, 31.5,
34.5, 36. ])
Olivier
Le 17 avr. 2024 à 13:36, Martin Yeo ***@***.***> a écrit :
This should work:
—
Olivier Marti
LSCE Bât 714 p. 1049
MERMAID Team
Office : +33 1 69 08 77 27
Mobile : +33 6 45 36 43 74
***@***.***
|
Beta Was this translation helpful? Give feedback.
You can use
xr.dot
to 'emulate'groupby
(xr.dot
only works for DataArrays but it should get you there).xr.dot
does the same as(air.air * seasons).sum("time")
but more efficient.