Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting monthly climatology using xarray in python

Tags:

python

xarray

I have a netCDF file containing daily data for a variable called var2001-01-01 to 2010-12-31. I want to compute monthly sum for var resulting in a netCDF containing 12 time steps (one for each month of the year). Currently, I am doing this:

import xarray as xr
hndl_fl = xr.open_dataset(path_file)

hndl_fl.resample('1MS', dim='time', how='sum')

However, this results in a netCDF with monthly sums for each month from 2001 to 2010. How do i get the monthly average for 12 months?

like image 604
user308827 Avatar asked Mar 10 '17 03:03

user308827


2 Answers

Comments: I am looking for monthly average for 12 months (for all years from 2001 to 2010). Your solution only computes monthly average for 1 year

My first Output start from 2001-01 up to 2010-12, so all years are coverd.
Do you want to resample these 120 values once more?


How do i get the monthly average for 12 months?

You have to decide what you want:
Average for every Month within a Year, results in 12 Values per Year, up to 120 Values in 10 Years
or
Average for 1 Year, results in 10 Values in 10 Years

Using the following xarray.Dataset, date_range=10 Years

date_range('2001-01-01', '2010-12-31', name='time')
<xarray.Dataset>
Dimensions:  (time: 3652)
Coordinates:
  * time     (time) datetime64[ns] 2001-01-01 2001-01-02 2001-01-03 ...
Data variables:
    data     (time) float64 16.0 18.0 15.0 12.0 23.0 9.0 7.0 18.0 23.0 23.0 ...

Get the monthly_avr for every Month in the date_range('2001-01-01', '2010-12-31', name='time'):

monthly_avr = ds.resample('1MS', dim='time', how='mean')

Output:

monthly_avr=<xarray.Dataset>
Dimensions:  (time: 120)
Coordinates:
  * time     (time) datetime64[ns] 2001-01-01 2001-02-01 2001-03-01 ...
Data variables:
    data     (time) float64 17.42 16.54 19.23 18.37 14.74 17.8 16.45 17.29 ...

Get the year_avr for every Year in the date_range('2001-01-01', '2010-12-31', name='time') :

year_avr = ds.resample('1AS', dim='time', how='mean')  

Output:

year_avr=<xarray.Dataset>
Dimensions:  (time: 10)
Coordinates:
  * time     (time) datetime64[ns] 2001-01-01 2002-01-01 2003-01-01 ...
Data variables:
    data     (time) float64 17.22 17.13 17.05 17.49 17.38 17.07 16.72 16.47 ...  

Tested with Python:3.4.2 - xarray: 0.9.1

like image 159
stovfl Avatar answered Nov 15 '22 07:11

stovfl


Either

hndl_fl.resample('1MS', dim='time', how='mean')

or

hndl_fl.groupby('time.month').mean('time')

should do the trick, depending on what you want exactly.

like image 33
patapouf_ai Avatar answered Nov 15 '22 07:11

patapouf_ai