将坐标更改为R中具有多个图层的一系列栅格

时间:2014-07-22 10:19:01

标签: r raster

我在光栅文件中组织的气候预测数据库存在问题。

每个文件包含一年的信息(例如关于降水),组织如下:从0到360 lon和从65到-65 lat的0.5度网格的365层数据。每个图层都是一个包含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)
}`

非常感谢。

1 个答案:

答案 0 :(得分:2)

这应该有效:

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)
}
相关问题