简体   繁体   English

R 中一组 netcdf 文件的平均字段,每个文件具有相同的维度

[英]average fields across a set of netcdf files, each with identical dimensions, in R

Suppose we have 10 of these files .假设我们有 10 个这样的文件 This netcdf dataset has a variable called tmp which is a 3-dim array: [lon][lat][time] .这个 netcdf 数据集有一个名为 tmp 的变量,它是一个 3 维数组: [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.现在我如何组合这 10 个文件以获得一个带有变量 tmp 的单个文件,其值是组合值的平均值。

I thought I could do that ncra ( http://nco.sourceforge.net/nco.html#xmp_ncra ) but have not been successful.我以为我可以做到 ncra ( http://nco.sourceforge.net/nco.html#xmp_ncra ) 但没有成功。

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.您可以使用ncdf包的10个文件读入R,它们组合成一个大nlon x nlat x time x nfiles使用数组abindabind包,然后使用apply于平均出来的file尺寸。 This all assumes that you have enough RAM to load these 10 datasets into memory, ie they cannot be too big.这一切都假设您有足够的 RAM 将这 10 个数据集加载到内存中,即它们不能太大。

Alternatively, I would have a look at CDO , which is a command line tool to manipulate NetCDF files.或者,我会看看CDO ,它是一个用于操作 NetCDF 文件的命令行工具。 There might also be a way to get what you want from that tool.可能还有一种方法可以从该工具中获得您想要的东西。 This might be a more memory friendly option.这可能是一个对内存更友好的选项。

The CDO solution is to use the command ensmean, "ens" refers to ensemble operations across multiple files. CDO 的解决方案是使用命令 ensmean,“ens”指的是跨多个文件的集成操作。 So the base command is for averaging 3 input files would be:因此,用于平均 3 个输入文件的基本命令是:

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因此,如果您有 file01.nc、file02.nc 等文件名,则可以使用

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.请注意,由于这些命令不知道将有多少输入文件,因此您不能将此命令与使用管道过程的其他 CDO 操作符结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM