简体   繁体   English

如何在Windows 7上使用NCO或R将每月TRMM netCDF文件连接成一个netCDF文件?

[英]How to concatenate monthly TRMM netCDF files into a single netCDF file using NCO or R on windows 7?

I have downloaded TRMM monthly precipitation rate in netCDF format from 1998 -2016, so approximately more than 200 files.The names of these files are 3B43.19980101.7.HDF.nc 3B43.19980201.7.HDF.nc 3B43.19980301.7.HDF.nc , and so on. 我已经从1998年到2016年以netCDF格式下载了TRMM月降水率,因此大约有200多个文件。这些文件的名称是3B43.19980101.7.HDF.nc 3B43.19980201.7.HDF.nc 3B43.19980301.7.HDF.nc ,等等。 I would like to concatenate all of these files into a single netCDF. 我想将所有这些文件连接成一个netCDF。 I've tried using the NCO operator "ncrcat" which should be able to concatenate a very long series of files along the record dimension, in this case time, but so far no luck. 我已经尝试过使用NCO运算符“ncrcat”,它应该能够在记录维度上连接很长的一系列文件,在这种情况下是时间,但到目前为止还没有运气。 I tried at first simple with only 2 files 我刚开始尝试只有2个文件

ncrcat -O -h 3B43.19980101.7.HDF.nc 3B43.19980201.7.HDF.nc out.nc

got 拿到

ERROR: no variable fit criteria for processing 错误:没有可变的拟合处理标准

so then I tried 所以我试过

ncks --mk_rec_dmn time 3B43.19980101.7.HDF.nc TD.3B43.19980101.7.HDF.nc
ncks --mk_rec_dmn time 3B43.19980201.7.HDF.nc TD.3B43.19980201.7.HDF.nc

I tried again with 我又试过了

ncrcat -O -h TD.3B43.19980101.7.HDF.nc TD.3B43.19980201.7.HDF.nc out.nc

still got same error 仍然有同样的错误

ERROR: no variable fit criteria for processing 错误:没有可变的拟合处理标准

Is there an easier way to doing this with 200+ files? 使用200多个文件有更简单的方法吗? A script that I can follow? 我可以遵循的脚本? I am new to all this so please be gentle. 我对这一切都是新手,所以请保持温柔。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I am using Windows 7 x86. 我使用的是Windows 7 x86。

It is completely possible to do this with NCO. 使用NCO完全可以做到这一点。 I looked at your input files and they simply lack a time dimension, so ncrcat fails. 我看了你的输入文件,他们只是缺少时间维度,所以ncrcat失败了。 Add a time dimension with 添加时间维度

ncecat -u time in.nc out.nc

Then use ncrcat as you say above. 然后如上所述使用ncrcat。 ps I have changed the ncrcat and ncra error messages to be more explicit about how to do this. ps我已经更改了ncrcat和ncra错误消息,以更明确地说明如何执行此操作。 Previously the HINTs only applied to cases where the file already had the dimension, but it was fixed. 以前,HINT仅适用于文件已经具有维度的情况,但它已修复。 Your files did not have a time dimension, so the ncks command you issued had no effect. 您的文件没有时间维度,因此您发出的ncks命令无效。

Edit to show loops: 编辑以显示循环:

To do this or anything like it in a loop use a construct like 要在循环中执行此操作或类似操作,请使用类似的结构

for fl in `ls trmm*.nc`; do
    ncecat -u time ${fl} ${fl/trmm/trmm_new} # Base output name in input name
    ... # more processing
done

The NCO manual has many examples of using file loops. NCO手册有许多使用文件循环的例子。

In R, you can do this by reading in all the data, combining into one large 3d array (latxlonxtime). 在R中,您可以通过读入所有数据,组合成一个大型3d数组(latxlonxtime)来完成此操作。 For example, array[,,1] would be the latxlon grid for Jan 1998. This can then be saved as a .rds format for further use in R, or saved as a netCDF file, which I won't cover but there are tutorials for saving R arrays as .nc files online. 例如,数组[,, 1]将是1998年1月的latxlon网格。然后可以将其保存为.rds格式以便在R中进一步使用,或者保存为netCDF文件,我将不会介绍但是有将R阵列保存为.nc文件在线的教程。

First, make a .csv file that contains a single column of all the filenames you downloaded. 首先,创建一个.csv文件,其中包含您下载的所有文件名的单个列。 One easy way is to ctrl-C the output from typing 'ls' in terminal into an excel sheet. 一种简单的方法是ctrl-C输出,在终端输入'ls'到excel表。 The code below reads in those files one by one, adding each to the array. 下面的代码逐个读入这些文件,将每个文件添加到数组中。

library(ncdf4)
library(abind)
filenames=read.csv('TRMM.filenames.csv',head=F) #read in filenames
filenames=as.character(filenames[,1]) #convert to 'character' format

n.lon=192 #input the correct #'s here, must be the same for all files
n.lat=94

NA.matrix=matrix(rep(NA,n.lon*n.lat),nrow=n.lon) #used to initialize
prcp=array(NA.matrix,c(n.lon,n.lat,1)) #n.lonxn.latx1 array of NA's to initialize
for (i in 1:length(filenames)){
  ncdata=nc_open(filenames[i]) #read in file i, assuming files are in same location as filenames.csv/your current working directory
  #ncdata=nc_open(paste(data.dir,filenames[i],sep="")) #if your data is in another directory than the filenames.csv file, you could read it in with this line instead
  nc=ncvar_get(ncdata,"precip") #check the .nc files to see what the variable name actually is; this reads in the variable "precip"
  prcp=abind(prcp,nc)
}
prcp=prcp[,,-1] #remove the NA.matrix used to initialize

dim(prcp) #check that the lonxlatxtime dimensions make sense
saveRDS(prcp,'TRMM.all.rds') #save as .rds file, or proceed to save it as .nc file, which takes a bit more work

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

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