在 R 中,您可以通过读入所有数据并组合成一个大型 3d 数组(latxlonxtime)来做到这一点。例如,array[,,1] 将是 1998 年 1 月的 latxlon 网格。然后可以将其保存为 .rds 格式以在 R 中进一步使用,或者保存为 netCDF 文件,我不会介绍,但有在线将 R 数组保存为 .nc 文件的教程。
首先,制作一个 .csv 文件,其中包含您下载的所有文件名的单列。一种简单的方法是将在终端中键入“ls”的输出按 ctrl-C 键输入到 Excel 工作表中。下面的代码一个一个地读取这些文件,将每个文件添加到数组中。
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