1
votes

I was able to open a netcdf file in R using the package "ncdf4" and extract rainfall (using ncvar_get). However I can't find information on how to calculate monthly values from daily values. Is this possible while staying in the netcdf format or should I convert it to something else? - but what format would keep the lat, long and time information associated with each rainfall value? I am not familiar with netcdfs so any help will be appreciated.

2
For this kind of data analysis, I strongly suggest you to use the Climate Data Operators (CDO). It is fast and simple. CDO is not very good if you want to perform high level analysis, but works very well in this case. The operator you are looking for is monmeanAF7

2 Answers

4
votes

The exact command with cdo is

cdo monmean in.nc out.nc

EDIT:

As in the comment of J W you can also calculate the "average" instead of the "mean"

cdo monavg in.nc out.nc

The difference between the two is in the way they handle missing data. monmean ignores missing values, while monavg includes missing. Quoting the manual: "the mean of 1, 2, miss and 3 is (1+2+3)/3 = 2, whereas the average is (1+2+miss+3)/4 = miss/4 = miss" - see section 2.8 of the manual for more details.

Last, but not least, you can also use CDO commands directly from R using this package which provides R wrappers for CDO

3
votes

I'm not an R programmer but I can recommend the command-line tools nco and/or cdo for these sorts of operations

http://nco.sourceforge.net/nco.html -- see averagers nces, ncra and ncwa. You probably want the record averager ncra

https://code.zmaw.de/projects/cdo -- see https://code.zmaw.de/projects/cdo/embedded/1.6.4/cdo_refcard.pdf for the commands for monthly, daily, seasonal, statistics