将坐标更改为 R 中具有多个图层的一系列栅格 [英] Change coordinates to a series of rasters with multiple layers in R
问题描述
我在使用栅格文件组织的气候预测数据库时遇到问题.
I have a problem with a climate projections database organised in raster files.
每个文件都包含一年的信息(例如关于降水),组织如下:365 层数据,用于从 0 到 360 lon 和从 65 到 -65 lat 的 0.5 度网格.每层是一个有 720 列和 260 行的网格.
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尺寸:260、720、187200、365(nrow、ncol、ncell、nlayers)分辨率 : 0.5, 0.5 (x, y)范围:-0.25、359.75、-65.25、64.75(xmin、xmax、ymin、ymax)坐标.参考: +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
我需要重新组织这些信息,移动网格的右侧部分并将格林威治放在数据库的中间(而不是让系统从 0° 到 360°,我需要重新组织 -180 和 +180 的信息°).重组不能局限于整个图层的正常移动:我需要将图层的一部分180°/360°移动到-180°/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)
}`
非常感谢.
推荐答案
这应该有效:
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)
}
这篇关于将坐标更改为 R 中具有多个图层的一系列栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!