首页 > 解决方案 > 我如何在文件夹中列出所有带有 .nc (netcdf) 的文件并从 10 个变量中提取 1 个变量?

问题描述

我的任务是从一个文件夹中获取多个相似的NetCDF ( .nc) 文件,并从 10 个变量中堆叠一个a变量。

我用了:

a <- list.files(path=ncpath, pattern = "nc$", full.names = TRUE)

这让我得到了所有带有.nc扩展名的文件。

如何进行第二项任务?

a我想要一个文件夹中这些文件数量的这个变量并将它们堆叠起来。

标签: rlistvariablesnetcdfcdo-climate

解决方案


如果您只想要 netcdf 文件中的输出,您可以考虑从 linux 的命令行执行此任务并使用 cdo?

files=$(ls *.nc)   # you might want to be more selective with your wildcard
# this loop selects the variable from each file and puts into file with the var name at the end
for file in $files ; do 
   cdo selvar,varname $file ${file%???}_varname.nc 
done 
# now merge into one file:
cdo merge *_varname.nc merged_file.nc  

您显然需要将 varname 替换为您选择的变量的名称。


推荐阅读