Question

Suppose we have 10 of these files. This netcdf dataset has a variable called tmp which is a 3-dim array: [lon][lat][time]. Now how do I combine these 10 files to get a single file with the variable tmp whose values are the average of the combined values.

I thought I could do that ncra (http://nco.sourceforge.net/nco.html#xmp_ncra) but have not been successful.

Was it helpful?

Solution

You can use the ncdf package to read the 10 files into R, combine them into one big nlon x nlat x time x nfiles array using abind from the abind package, and then using apply to average out the file dimension. This all assumes that you have enough RAM to load these 10 datasets into memory, i.e. they cannot be too big.

Alternatively, I would have a look at CDO, which is a command line tool to manipulate NetCDF files. There might also be a way to get what you want from that tool. This might be a more memory friendly option.

OTHER TIPS

The CDO solution is to use the command ensmean, "ens" refers to ensemble operations across multiple files. So the base command is for averaging 3 input files would be:

cdo ensmean file1.nc file2.nc file3.nc   ensemble_mean.nc

Obviously it is boring to type out a long list of filenames, so better to use wildcards. So if you have filenames like file01.nc, file02.nc etc, you can use

cdo ensmean file*.nc ensemble_mean.nc 

Note that, as these commands do not know how many input files there will be, you can not combine this command with other CDO operators using the piping process.

Another option is to use ncea which is the full nco ensemble averaged package

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top