如何列出文件夹中带有.nc(netcdf)的所有文件,并从10个变量中提取1个变量?

时间:2019-12-19 15:04:25

标签: r list variables netcdf

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

我用过:

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

这使我得到所有扩展名为.nc的文件。

如何执行第二项任务?

我希望从文件夹中这些数量的文件中使用此变量a并将其堆叠。

1 个答案:

答案 0 :(得分:0)

如果只希望将输出输出到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。