如何从数千个栅格中提取数据并保存到新的数据帧中

时间:2016-12-09 00:21:19

标签: r raster r-raster

我正在处理R中的气候数据,我有3个栅格地图文件夹:温度(tmax,tmin)和沉降(RF) - 每个文件夹中有9131个栅格,总共27393个。

我需要打开每个栅格并为每个空间点(284479点)提取数据,并将数据保存在新的data.frame中。

目标是拥有284479个文件,每个空间点一个,每个栅格的数据都在列中(nrows = 9131,ncol = 3)。

#Set spatial coordinates to create a Spatial object:
coordinates(GridPoint) = ~x + y
Coord <- coordinates(GridPoint)

#Create a loop to generate a input file for each station
for (i in 1:9131) {

#Open each raster per day of record
Tmax <- raster(Tmax_asc_files[i])
Tmin <- raster(Tmin_asc_files[i])
RF   <- raster(RF_asc_files[i])

#extract Values from each raster for that day
Tmax_E <- extract(Tmax, Coord)
tmax.df<- data.frame(Tmax_E)

Tmin_E <- extract(Tmin, Coord)
tmin.df<- data.frame(Tmin_E)

Prcp_E   <- extract(RF, Coord)
prcp.df  <- data.frame(Prcp_E)

for (j in 1:length(tmax.df))  {

Cell.matrix <- matrix(nrow = 9131,ncol = 4)
Cell.matrix <- as.data.frame(Cell.matrix)
colnames(Cell.matrix) <- c("yearday","tmax","tmin","prcp")

tmax.row <- tmax.df[j,]
tmin.row <- tmin.df[j,]
prcp.row <- prcp.df[j,]

#Add date to first column
Cell.matrix[1] <- Date
Cell.matrix[i,2:4] <- c(tmax.row, tmin.row, prcp.row)


filename <- paste("GridPt_", j, sep = "")
write.table(Cell.matrix, file = paste(path, filename, ".mtcin", sep = ""),
            row.names = F, quote = F) 
    }
  }

0 个答案:

没有答案