问题
I have a problem with a climate projections database organised in raster files.
Each file contains information (about precipitation for instance) for one year, organised as follow: 365 layers of data for a grid of 0.5 degrees going from 0 to 360 lon and from 65 to -65 lat. Each layer is a grid with 720 columns and 260 rows.
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
I need to reorganise these information shifting the right part of the grid and putting Greenwich in the middle of the database (instead of having a system going from 0°to 360° I need to reorganise the infomation from -180 and +180°). The reorganization cannot be limited to a normal shift of the entire layer: I neet to take the part of the layer 180°/360° and moving it to -180°/0°. This action has to be done for each of the layers of each of the files (corresponding to the years) of the database.
I drafted a loop for this action, but I have some problems and takes a huge amount of time to be processed. Do you have any better idea?
`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)
}`
Thanks a lot.
回答1:
This should work:
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)
}
来源:https://stackoverflow.com/questions/24884694/change-coordinates-to-a-series-of-rasters-with-multiple-layers-in-r