-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_mapStatesSolved.r
142 lines (98 loc) · 3.88 KB
/
4_mapStatesSolved.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# clean
rm(list=ls())
# load libs
library(doParallel)
library(raster)
# open cluster
cl <- makeCluster(35)
registerDoParallel(cl)
# load raster ref
grid_ref <- readRDS("./data/futClimGrid/rasterClim/res1000/rcp85-MIROC_ESM_CHEM-1985-2000.rda")
######### WINDOW 1985-2000
# list files
probs2000_files <- list.files("./data/futStatesGrid/probs",full.names=TRUE,pattern="1985-2000")
# prep stack res
TempGrids2000 <- stack()
foreach(i=1:length(probs2000_files),.packages=c('raster','rgdal'))%do%{
# read file and compute temperate prob
probsGrid <- read.csv(probs2000_files[i])
probsGrid$ProbTemp <- probsGrid$T + probsGrid$M
# set coordinates from grid_ref
probsGrid <- cbind(probsGrid,coordinates(grid_ref))
# Turn df into raster
df_probsTemp <- probsGrid[,c(8,7,6)]
coordinates(df_probsTemp) <- ~ x + y
gridded(df_probsTemp) <- TRUE
rs_probsTemp <- raster(df_probsTemp)
TempGrids2000 <- addLayer(TempGrids2000,rs_probsTemp)
}
metadata <- unlist(lapply(strsplit(probs2000_files,"[./-]"),function(x) x[7]))
projection(TempGrids2000) <- projection(grid_ref)
names(TempGrids2000) <- metadata
saveRDS(TempGrids2000,"./res/2000_tempProbSolved.rda")
######### WINDOW 2000-2015
# list files
probs2015_files <- list.files("./data/futStatesGrid/probs",full.names=TRUE,pattern="2000-2015")
# prep stack res
TempGrids2015 <- stack()
foreach(i=1:length(probs2015_files),.packages=c('raster','rgdal'))%do%{
# read file and compute temperate prob
probsGrid <- read.csv(probs2015_files[i])
probsGrid$ProbTemp <- probsGrid$T + probsGrid$M
# set coordinates from grid_ref
probsGrid <- cbind(probsGrid,coordinates(grid_ref))
# Turn df into raster
df_probsTemp <- probsGrid[,c(8,7,6)]
coordinates(df_probsTemp) <- ~ x + y
gridded(df_probsTemp) <- TRUE
rs_probsTemp <- raster(df_probsTemp)
TempGrids2015 <- addLayer(TempGrids2015,rs_probsTemp)
}
metadata <- unlist(lapply(strsplit(probs2015_files,"[./-]"),function(x) x[7]))
projection(TempGrids2015) <- projection(grid_ref)
names(TempGrids2015) <- metadata
saveRDS(TempGrids2015,"./res/2015_tempProbSolved.rda")
######### WINDOW 2045-2030
# list files
probs2045_files <- list.files("./data/futStatesGrid/probs",full.names=TRUE,pattern="2030-2045")
# prep stack res
TempGrids2045 <- stack()
foreach(i=1:length(probs2045_files),.packages=c('raster','rgdal'))%do%{
# read file and compute temperate prob
probsGrid <- read.csv(probs2045_files[i])
probsGrid$ProbTemp <- probsGrid$T + probsGrid$M
# set coordinates from grid_ref
probsGrid <- cbind(probsGrid,coordinates(grid_ref))
# Turn df into raster
df_probsTemp <- probsGrid[,c(8,7,6)]
coordinates(df_probsTemp) <- ~ x + y
gridded(df_probsTemp) <- TRUE
rs_probsTemp <- raster(df_probsTemp)
TempGrids2045 <- addLayer(TempGrids2045,rs_probsTemp)
}
metadata <- unlist(lapply(strsplit(probs2045_files,"[./-]"),function(x) x[7]))
projection(TempGrids2045) <- projection(grid_ref)
names(TempGrids2045) <- metadata
saveRDS(TempGrids2045,"./res/2045_tempProbSolved.rda")
######### WINDOW 2080-2095
# list files
probs2095_files <- list.files("./data/futStatesGrid/probs",full.names=TRUE,pattern="2080-2095")
# prep stack res
TempGrids2095 <- stack()
foreach(i=1:length(probs2095_files),.packages=c('raster','rgdal'))%do%{
# read file and compute temperate prob
probsGrid <- read.csv(probs2095_files[i])
probsGrid$ProbTemp <- probsGrid$T + probsGrid$M
# set coordinates from grid_ref
probsGrid <- cbind(probsGrid,coordinates(grid_ref))
# Turn df into raster
df_probsTemp <- probsGrid[,c(8,7,6)]
coordinates(df_probsTemp) <- ~ x + y
gridded(df_probsTemp) <- TRUE
rs_probsTemp <- raster(df_probsTemp)
TempGrids2095 <- addLayer(TempGrids2095,rs_probsTemp)
}
metadata <- unlist(lapply(strsplit(probs2095_files,"[./-]"),function(x) x[7]))
projection(TempGrids2095) <- projection(grid_ref)
names(TempGrids2095) <- metadata
saveRDS(TempGrids2095,"./res/2095_tempProbSolved.rda")