I am working with climate data in R, I have 3 folders of raster maps: temperature (tmax,tmin) and precip (RF) - 9131 rasters in each folder, 27393 total.
I need to open each raster and extract data for every Spatial Point (284479 pts) and save the data in a new data.frame.
The goal is to have 284479 files, one for each Spatial Point with the data from each raster in columns (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)
}
}