Skip to content Skip to sidebar Skip to footer

Averaging 2 Decades Of Data On 6 Hourly Timestep Using Netcdf Data And Python

I have 2 decades of spatially variable wind data recorded at six-hourly intervals. I need to average the 2 decades of data across each six-hourly time interval, so I end up with 36

Solution 1:

Indeed there is not a very well documented way of doing this. Note also that dayofyear may not be exactly what you expect it to be.

In lieu of being able to use groupby with multiple levels (e.g. see this answer regarding how to do something similar to what you are asking in pandas), which is not available yet in xarray, a reasonably clean way of solving this kind of problem is to define a new coordinate for grouping that represents the "time of year" for each time in your Dataset.

In your case you are looking to group by the "hour of the year" (i.e. matching month, day, and hour). For this you can create an array of strings, which are basically just the string representations of the dates in the time coordinate with the years dropped:

ds['hourofyear'] = xr.DataArray(ds.indexes['time'].strftime('%m-%d %H'), coords=ds.time.coords)
result = ds.groupby('hourofyear').mean('time')

Post a Comment for "Averaging 2 Decades Of Data On 6 Hourly Timestep Using Netcdf Data And Python"