我对以栅格文件形式组织的气候预测数据库有疑问。
每个文件包含一年的信息(例如,有关降水的信息),其组织方式如下:0.5度网格的365层数据从0到360 lon,从65到-65 lat。每层是一个具有720列和260行的网格。
class : RasterStack dimensions : 260, 720, 187200, 365 (nrow, ncol, ncell, nlayers) resolution : 0.5, 0.5 (x, y) extent : -0.25, 359.75, -65.25, 64.75 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
我需要重新组织这些信息,将网格的右侧移动到格林威治数据库的中间(而不是将系统从0°转到360°,我需要从-180和+ 180°来组织信息)。重组不能局限于整个图层的正常移动:我希望将图层的一部分移至180°/ 360°并将其移至-180°/ 0°。必须对数据库的每个文件的每个层(对应于年份)执行此操作。
我为此动作草拟了一个循环,但是我遇到了一些问题,需要花费大量时间进行处理。你有更好的主意吗?
`setwd ("E:/directory") #inside the directory there are a list of layerrasterfile.nc
filelist<-list.files(pattern=".nc")
n<-length(filelist)
clim_data<-vector("list",n)
for (j in 1:65){ # or (j in filelist), for this action only 65 files
clim_data<-stack(filelist[j])
e<-extent(0,360,-65,65)
extent(clim_data)<- c(0,360,-65,65)
yr_name <-substr(filelist,30,34) #with this I want to capture the year contained in the file name (2206 2007 and so on)
rdataname <- paste(paste(yr_name, sep='_'), ".Rdata", sep="")
rdataname <- rdataname[j]
form<-function(clim_data)
{
for (i in 1:260)
{
AA<-clim_data[i,]
HH<-AA[c(361:720,1:360),]
clim_data[i,]<-HH
}
save(cmlim_data, file = rdataname[j]) #with this I want to save a .Rdata file for each of the years in order to proceed to the extraction of the information with a second loop.
}
form(clim_data)
}`
非常感谢。
这应该工作:
setwd ("E:/directory")
filelist <- list.files(pattern=".nc")
for (j in filelist){
clim_data <- stack(j)
clim_data <- rotate(clim_data)
yr_name <- substr(j, 30, 34)
rdataname <- paste(paste(yr_name, sep='_'), ".Rdata", sep="")
save(cmlim_data, file = rdataname)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句