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

问题描述 投票:1回答:1

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

我用过:

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

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

如何进行第二项任务?

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

r list variables netcdf
1个回答
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。

© www.soinside.com 2019 - 2024. All rights reserved.