Wrong time dimension after doing a groupby with library xarray (python)

Viewed 420

my problem is that I would like to use the easy functionality of the xarray-library in python, but I run into problems with the time dimension in case of aggregating data.

I have opened a dataset, which contains daily data over the year 2013: datset=xr.open_dataset(filein).

The contents of the file are:

<xarray.Dataset>
Dimensions:       (bnds: 2, rlat: 228, rlon: 234, time: 365)
Coordinates:
  * rlon          (rlon) float64 -28.24 -28.02 -27.8 -27.58 -27.36 -27.14 ...
  * rlat          (rlat) float64 -23.52 -23.3 -23.08 -22.86 -22.64 -22.42 ...
  * time          (time) datetime64[ns] 2013-01-01T11:30:00 ...
Dimensions without coordinates: bnds
Data variables:
    rotated_pole  |S1 ''
    time_bnds     (time, bnds) float64 1.073e+09 1.073e+09 1.073e+09 ...
    ASWGLOB_S     (time, rlat, rlon) float64 nan nan nan nan nan nan nan nan ...
Attributes:
    CDI:                       Climate Data Interface version 1.7.0 (http://m...
    Conventions:               CF-1.4
    references:                http://www.clm-community.eu/
    NCO:                       4.6.7
    CDO:                       Climate Data Operators version 1.7.0

When I use now the groupby method to compute the monthly means, the time dimension is destroyed:

datset.groupby('time.month')
<xarray.core.groupby.DatasetGroupBy object at 0x246a250>
>>> datset.groupby('time.month').mean('time')
<xarray.Dataset>
Dimensions:    (bnds: 2, month: 12, rlat: 228, rlon: 234)
Coordinates:
  * rlon       (rlon) float64 -28.24 -28.02 -27.8 -27.58 -27.36 -27.14 ...
  * rlat       (rlat) float64 -23.52 -23.3 -23.08 -22.86 -22.64 -22.42 -22.2 ...
  * month      (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Dimensions without coordinates: bnds
Data variables:
    time_bnds  (month, bnds) float64 1.074e+09 1.074e+09 1.077e+09 1.077e+09 ...
    ASWGLOB_S  (month, rlat, rlon) float64 nan nan nan nan nan nan nan nan ...

Now I have instead of a time dimension a month dimension with values from 1 to 12. Is this a side effect of the 'mean' - function? As long as i do not use this mean function, the time variable is retained.

What I am doing wrong? The examples given in the documentation and this forum seems to have a different behaviour. There, timestamps are retained except that the first date of each month is used.

Can I reinvent my old time dimension? What if I want to have time stamps indicating the middle of the month and 'time_bounds' indicating the interval for each mean-value, i.e. beginning of the month, end of the month.

Thanks for your help, Ronny

1 Answers
Related