umbersar
umbersar

Reputation: 1931

calculate mean using 'year + month' combination in xarray

I have a netcdf file with daily data for 5 years (2011 to 2015). I want to calculate monthly averages for the data using XArray in Python.

netcdf file:////test/Combined.nc {
  dimensions:
    latitude = 681;
    longitude = 841;
    time = 1826;
  variables:
    double latitude(latitude=681);
      :_FillValue = NaN; // double
      :name = "latitude";
      :long_name = "latitude";
      :units = "degrees_north";
      :standard_name = "latitude";

    double longitude(longitude=841);
      :_FillValue = NaN; // double
      :name = "longitude";
      :long_name = "longitude";
      :units = "degrees_east";
      :standard_name = "longitude";

    long time(time=1826);
      :name = "time";
      :long_name = "time";
      :standard_name = "time";
      :units = "days since 2011-01-01 00:00:00";
      :calendar = "proleptic_gregorian";

    float PET(time=1826, latitude=681, longitude=841);
      :_FillValue = -999.0f; // float
      :name = "PET";
      :long_name = "Potential evapotranspiration";
      :units = "mm";
      :standard_name = "PET";      

  :var_name = "PET";
}

What I tried to do was was use groupby to calculate monthly averages:

import numpy as np
import xarray as xr

ds = xr.open_dataset("c:\\test\\Combined.nc")
ds_avg = ds.PET.groupby('time.month').mean(dim='time')
ds_avg.to_netcdf("C:\\test\\Combined_avg.nc") 

But the problem with above code is spits out a file with combined monthly average (from 2011 to 2015). That means i have 12 months in the result file. That is not what i want to do. I want to calculate the monthly average for January 2011, Feb 2011, March 2011 to Dec 2015 so that i get 12 * 5 months in the result file. So that means that the groupby should happen not on 'time.month' but 'time.year:time.month'. How do I do that?

Thanks

Upvotes: 3

Views: 1806

Answers (1)

Matteo De Felice
Matteo De Felice

Reputation: 1518

You should use resampledoc with the frequency with one month. Then:

ds_avg = ds.resample('1M').mean()

If you are interested on any other similar (simple) manipulations have a look at this notebook we have set up for the ERA-NUTS dataset.

Another example using another dataset:

<xarray.Dataset>
Dimensions:    (bnds: 2, latitude: 61, longitude: 91, time: 218)
Coordinates:
  * longitude  (longitude) float32 -22.5 -21.75 -21.0 -20.25 ... 43.5 44.25 45.0
  * latitude   (latitude) float32 72.0 71.25 70.5 69.75 ... 28.5 27.75 27.0
  * time       (time) datetime64[ns] 2000-01-16T15:00:00 ... 2018-01-01T03:00:00
Dimensions without coordinates: bnds
Data variables:
    time_bnds  (time, bnds) datetime64[ns] ...
    ssrdc      (time, latitude, longitude) float64 ...
    ssrd       (time, latitude, longitude) float64 ...

And then applying the resample:

In [13]: d.resample(time = '1Y').mean()                                                                                           
Out[13]: 
<xarray.Dataset>
Dimensions:    (latitude: 61, longitude: 91, time: 19)
Coordinates:
  * time       (time) datetime64[ns] 2000-12-31 2001-12-31 ... 2018-12-31
  * longitude  (longitude) float32 -22.5 -21.75 -21.0 -20.25 ... 43.5 44.25 45.0
  * latitude   (latitude) float32 72.0 71.25 70.5 69.75 ... 28.5 27.75 27.0
Data variables:
    ssrdc      (time, latitude, longitude) float64 5.033e+05 ... 1.908e+05
    ssrd       (time, latitude, longitude) float64 4.229e+05 ... 1.909e+05

Upvotes: 2

Related Questions