-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkigin_Brazil.R
62 lines (48 loc) · 2.73 KB
/
Skigin_Brazil.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
rm(list=ls(all=TRUE)) # clear memory
library(rgdal)
library(raster)
library(dplyr)
library(maptools)
setwd("E:\\GISWork_2\\Skigin_Brazil")
brazil.admin2 <- readOGR(dsn="Admin", layer = "BRA_adm2", encoding = "ISO-8859") #This opens the boundary file for the departments of Brazil, a shapefile
all <- 1979:2013 #years of interest
all.means <- stack() #Creates a blank raster stack to hold the climate data
save.clips <- TRUE #Just a binary about what to save
# tpye <- "Precip" #either "Precip" or "Temp
tpye <- "Temp" #either "Precip" or "Temp
for (each.year in all){
print(each.year)
pat <- paste("^.*",each.year,".*.tif$", sep = "")
climate.files2 <- list.files(paste("CHELSA\\",tpye,"\\World",sep = ""), pattern=pat, full.names=TRUE) #Loading all of the Precipitation Files
if (length(climate.files2) == 12){ #Just a check to make sure all of the files were downloaded properly
print(" Loading")
all.climate2 <- stack(climate.files2) #Creating a stack of all of them, makes it much easier to calculate means and clipe
print(" Clipping")
brazil.climate.year <- crop(all.climate2, brazil.admin2) #Cut down the data for the whole world to just that for Brazil
if (save.clips){
print(" Saving clips")
writeRaster(brazil.climate.year, filename=file.path(paste("CHELSA\\",tpye,"\\Brazil",sep = ""),names(brazil.climate.year)), bylayer=TRUE,format="GTiff")
#Saving it this way saves each layer as a seprate file
}
print(" Calculating mean")
each.mean <- mean(brazil.climate.year) #Calcualtes the mean for each pixel of the year
names(each.mean) <- paste(tpye,"_",each.year,sep = "") #Renames that mean so that it contains the variable and the year
all.means <- addLayer(all.means,each.mean) #Adds it into the stack of all means. This will make it easier to sample all years later
print(" All Good")
}else{
print(length(climate.files2))
} #end else
}# end for loop
writeRaster(all.means, filename=file.path(paste("CHELSA\\",tpye,"\\Means",sep = ""),names(all.means)), bylayer=TRUE,format="GTiff") #writing out all of the mean years
only.points <- rasterToPoints(all.means,spatial = T) #converting the raster stack to a series of points
#it seems counterintiative, but this runs a lot faster than as a raster stack
ord <- over(only.points,brazil.admin2) #Figuring out which deparment every point is in
union.table <- cbind(only.points@data, as.data.frame(ord)) # rejoining the data back on
union.table <- union.table[,c(1:35,41,42)] #Dropping unnecessary fields
#Creating final summary table
summary <- union.table %>%
filter(!is.na(NAME_2)) %>%
group_by(ID_2,NAME_2)%>%
add_tally() %>%
summarise_all(funs(mean))
write.csv(summary, paste(tpye,"_summary.csv",sep=""), row.names = F)