3

There are functions that can group data into hourly i.e. 24 or into day of year i.e. 365. I have a dataset of 3 years 1999-2001 that has hourly values. So total values are 24*365*4+1*24=26304(1*24= leap year day). When I run the function

result=ds.groupby('time.dayofyear').mean('time')

The result it gives:

<xarray.DataArray 'precip' (dayofyear: 366, lat: 21, lon: 33)>
array([[[0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        ...,
        [0.00086806, 0.00065104, 0.00097656, ..., 0.        ,
         0.        , 0.        ],
        [0.00141059, 0.00141059, 0.00130208, ..., 0.        ,
         0.        , 0.        ],
        [0.00195312, 0.00141059, 0.00119358, ..., 0.        ,
         0.        , 0.        ]],

       [[0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        ...,]

Coordinates:
  * lon        (lon) float32 220.0 222.5 225.0 227.5 ... 292.5 295.0 297.5 300.0
  * lat        (lat) float32 20.0 22.0 24.0 26.0 28.0 ... 54.0 56.0 58.0 60.0
  * dayofyear  (dayofyear) int64 1 2 3 4 5 6 7 8 ... 360 361 362 363 364 365 366

If I use the time.hour groupby function:

result=ds.groupby('time.hour').mean('time')
<xarray.DataArray 'precip' (hour: 24, lat: 21, lon: 33)>
array([[[0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        ...,
        [0.00015682, 0.00022097, 0.00047759, ..., 0.        ,
         0.        , 0.        ],
        [0.00033503, 0.00037779, 0.0004562 , ..., 0.        ,
         0.        , 0.        ],
        [0.00044195, 0.00039918, 0.00039205, ..., 0.        ,
         0.        , 0.        ]],, dtype=float32)
Coordinates:
  * lon      (lon) float32 220.0 222.5 225.0 227.5 ... 292.5 295.0 297.5 300.0
  * lat      (lat) float32 20.0 22.0 24.0 26.0 28.0 ... 52.0 54.0 56.0 58.0 60.0
  * hour     (hour) int64 0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23

How to groupy hour of the year where it gives me hourly average of the year rather than a day. Need the function to give result as 366*24 =8784 where average is calculated using day hour index.

1 Answers1

4

I think you are asking for the same thing as in a question I answered earlier. In short, I think the cleanest approach in xarray at the moment is to use strftime to generate a coordinate with the "hourofyear" values for each date and use groupby on that:

ds['hourofyear'] = xr.DataArray(ds.time.dt.strftime('%m-%d %H'), coords=ds.time.coords)
result = ds.groupby('hourofyear').mean('time')
spencerkclark
  • 1,869
  • 1
  • 12
  • 9