1
|
##################################### METHODS COMPARISON part 5 ##########################################
|
2
|
#################################### Spatial Analysis ############################################
|
3
|
#This script utilizes the R ojbects created during the interpolation phase. #
|
4
|
#This scripts focuses on a detailed study of differences in the predictions of CAI_kr and FUsion_Kr
|
5
|
#Differences are examined through:
|
6
|
#1) per land cover classes
|
7
|
#2) per elevation classes
|
8
|
#3) through spiatial transects
|
9
|
#
|
10
|
#Note this code is for exploratory analyses so some sections are not succinct and
|
11
|
#can be improve for repeatability and clarity.
|
12
|
#AUTHOR: Benoit Parmentier #
|
13
|
#DATE: 12/04/2012 #
|
14
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#491 -- #
|
15
|
###################################################################################################
|
16
|
|
17
|
###Loading R library and packages
|
18
|
library(gtools) # loading some useful tools such as mixedsort
|
19
|
library(mgcv) # GAM package by Wood 2006 (version 2012)
|
20
|
library(sp) # Spatial pacakge with class definition by Bivand et al. 2008
|
21
|
library(spdep) # Spatial package with methods and spatial stat. by Bivand et al. 2012
|
22
|
library(rgdal) # GDAL wrapper for R, spatial utilities (Keitt et al. 2012)
|
23
|
library(gstat) # Kriging and co-kriging by Pebesma et al. 2004
|
24
|
library(automap) # Automated Kriging based on gstat module by Hiemstra et al. 2008
|
25
|
library(spgwr)
|
26
|
library(gpclib)
|
27
|
library(maptools)
|
28
|
library(graphics)
|
29
|
library(parallel) # Urbanek S. and Ripley B., package for multi cores & parralel processing
|
30
|
library(raster)
|
31
|
library(rasterVis)
|
32
|
library(plotrix) #Draw circle on graph
|
33
|
library(reshape)
|
34
|
######### Functions used in the script
|
35
|
#loading R objects that might have similar names
|
36
|
load_obj <- function(f)
|
37
|
{
|
38
|
env <- new.env()
|
39
|
nm <- load(f, env)[1]
|
40
|
env[[nm]]
|
41
|
}
|
42
|
|
43
|
plot_transect<-function(list_trans,r_stack,title_plot,disp=TRUE){
|
44
|
#This function creates plot of transects for stack of raster images.
|
45
|
#The parameters are:
|
46
|
#list_trans: list of files containing the transects lines in shapefile format
|
47
|
#r_stack: raster stack of files
|
48
|
#title_plot: plot title
|
49
|
#disp: dispaly and save from X11 if TRUE
|
50
|
nb<-length(list_trans)
|
51
|
t_col<-rainbow(nb)
|
52
|
list_trans_data<-vector("list",nb)
|
53
|
for (i in 1:nb){
|
54
|
trans_file<-list_trans[[i]][1]
|
55
|
filename<-sub(".shp","",trans_file) #Removing the extension from file.
|
56
|
transect<-readOGR(".", filename) #reading shapefile
|
57
|
trans_data<-extract(r_stack, transect)
|
58
|
if (disp==FALSE){
|
59
|
png(file=paste(list_trans[[i]]),".png",sep="")
|
60
|
}
|
61
|
for (k in 1:ncol(trans_data[[1]])){
|
62
|
y<-trans_data[[1]][,k]
|
63
|
x<-1:length(y)
|
64
|
if (k!=1){
|
65
|
lines(x,y,col=t_col[k])
|
66
|
}
|
67
|
if (k==1){
|
68
|
plot(x,y,type="l",xlab="Position index", ylab="temperature",col=rainbow(k))
|
69
|
}
|
70
|
}
|
71
|
title(title_plot[i])
|
72
|
legend("topright",legend=layerNames(r_stack),
|
73
|
cex=1.2, col=t_col,
|
74
|
lty=1)
|
75
|
|
76
|
if (disp==TRUE){
|
77
|
savePlot(file=paste(list_trans[[i]][2],".png",sep=""),type="png")
|
78
|
}
|
79
|
if (disp==FALSE){
|
80
|
dev.off()
|
81
|
}
|
82
|
list_trans_data[[i]]<-trans_data
|
83
|
}
|
84
|
names(list_trans_data)<-names(list_trans)
|
85
|
return(list_trans_data)
|
86
|
}
|
87
|
|
88
|
plot_transect_m<-function(list_trans,r_stack,title_plot,disp=TRUE,m_layers){
|
89
|
#This function creates plot of transects for stack of raster images.
|
90
|
#Arguments:
|
91
|
#list_trans: list of files containing the transects lines in shapefile format
|
92
|
#r_stack: raster stack containing the information to extect
|
93
|
#title_plot: plot title
|
94
|
#disp: display and save from X11 if TRUE or plot to png file if FALSE
|
95
|
#m_layers: index for layerers containing alternate units to be drawned on a differnt scale
|
96
|
#RETURN:
|
97
|
#list containing transect information
|
98
|
|
99
|
nb<-length(list_trans)
|
100
|
t_col<-rainbow(nb)
|
101
|
list_trans_data<-vector("list",nb)
|
102
|
|
103
|
#For scale 1
|
104
|
for (i in 1:nb){
|
105
|
trans_file<-list_trans[[i]][1]
|
106
|
filename<-sub(".shp","",trans_file) #Removing the extension from file.
|
107
|
transect<-readOGR(".", filename) #reading shapefile
|
108
|
trans_data<-extract(r_stack, transect)
|
109
|
if (disp==FALSE){
|
110
|
png(file=paste(list_trans[[i]]),".png",sep="")
|
111
|
}
|
112
|
#Plot layer values for specific transect
|
113
|
for (k in 1:ncol(trans_data[[1]])){
|
114
|
y<-trans_data[[1]][,k]
|
115
|
x<-1:length(y)
|
116
|
m<-match(k,m_layers)
|
117
|
|
118
|
if (k==1 & is.na(m)){
|
119
|
plot(x,y,type="l",xlab="Position index", ylab="temperature",col=t_col[k])
|
120
|
axis(2,xlab="",ylab="tmax (in degree C)")
|
121
|
}
|
122
|
if (k==1 & !is.na(m)){
|
123
|
plot(x,y,type="l",col=t_col[k],axes=F) #plotting fusion profile
|
124
|
axis(4,xlab="",ylab="tmax (in degree C)")
|
125
|
|
126
|
}
|
127
|
if (k!=1 & is.na(m)){
|
128
|
#par(new=TRUE) # new plot without erasing old
|
129
|
lines(x,y,type="l",col=t_col[k],axes=F) #plotting fusion profile
|
130
|
#axis(2,xlab="",ylab="tmax (in degree C)")
|
131
|
}
|
132
|
if (k!=1 & !is.na(m)){
|
133
|
par(new=TRUE) # key: ask for new plot without erasing old
|
134
|
plot(x,y,type="l",col=t_col[k],axes=F) #plotting fusion profile
|
135
|
#axis(4,xlab="",ylab="tmax (in degree C)")
|
136
|
}
|
137
|
|
138
|
}
|
139
|
title(title_plot[i])
|
140
|
legend("topright",legend=layerNames(r_stack),
|
141
|
cex=1.2, col=t_col,
|
142
|
lty=1)
|
143
|
|
144
|
if (disp==TRUE){
|
145
|
savePlot(file=paste(list_trans[[i]][2],".png",sep=""),type="png")
|
146
|
}
|
147
|
if (disp==FALSE){
|
148
|
dev.off()
|
149
|
}
|
150
|
list_trans_data[[i]]<-trans_data
|
151
|
}
|
152
|
names(list_trans_data)<-names(list_trans)
|
153
|
return(list_trans_data)
|
154
|
}
|
155
|
|
156
|
transect_from_spdf<-function (spdf,selected_features){
|
157
|
#This function produces a transect from a set of selected points in a point layer
|
158
|
# Arguments:
|
159
|
# spdf: SpatialPointDataFrame
|
160
|
# selected_features: index of ssubset points used in the transect line
|
161
|
# Return: SpatialLinesDataframe object corresponding to the transect
|
162
|
# Author: Benoit Parmentier
|
163
|
# Date: 11-29-2012
|
164
|
|
165
|
dat_id<-spdf[selected_features,] #creating new subset from spdf
|
166
|
spdf_proj<-proj4string(dat_id)
|
167
|
matrix_point_coords<-coordinates(dat_id)
|
168
|
#Add possibility of keeping attributes?
|
169
|
#Transform a sequence of points with coords into Spatial Lines
|
170
|
#Note that X is the ID, modify for dataframe?
|
171
|
trans4<-SpatialLines(list(Lines(list(Line(coordinates(matrix_point_coords))),"X")))
|
172
|
tmp<-as.data.frame(dat_id[1,])
|
173
|
row.names(tmp)<-rep("X",1)
|
174
|
trans4<-SpatialLinesDataFrame(trans4,data=tmp)
|
175
|
proj4string(trans4)<-spdf_proj
|
176
|
return(trans4)
|
177
|
}
|
178
|
|
179
|
###Parameters and arguments
|
180
|
|
181
|
infile1<- "ghcn_or_tmax_covariates_06262012_OR83M.shp" #GHCN shapefile containing variables for modeling 2010
|
182
|
#infile2<-"list_10_dates_04212012.txt" #List of 10 dates for the regression
|
183
|
infile2<-"list_365_dates_04212012.txt" #list of dates
|
184
|
infile3<-"LST_dates_var_names.txt" #LST dates name
|
185
|
infile4<-"models_interpolation_05142012.txt" #Interpolation model names
|
186
|
infile5<-"mean_day244_rescaled.rst" #mean LST for day 244
|
187
|
inlistf<-"list_files_05032012.txt" #list of raster images containing the Covariates
|
188
|
infile6<-"OR83M_state_outline.shp"
|
189
|
#stat_loc<-read.table(paste(path,"/","location_study_area_OR_0602012.txt",sep=""),sep=",", header=TRUE)
|
190
|
|
191
|
out_prefix<-"methods_comp5_12142012_"
|
192
|
nb_transect<-4
|
193
|
##### LOAD USEFUL DATA
|
194
|
|
195
|
#obj_list<-"list_obj_08262012.txt" #Results of fusion from the run on ATLAS
|
196
|
path_wd<-"/home/parmentier/Data/IPLANT_project/methods_interpolation_comparison_10242012" #Jupiter LOCATION on Atlas for kriging #Jupiter Location on XANDERS
|
197
|
#path<-"/Users/benoitparmentier/Dropbox/Data/NCEAS/Oregon_covariates/" #Local dropbox folder on Benoit's laptop
|
198
|
setwd(path_wd)
|
199
|
path_data_cai<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_10242012_CAI" #Change to constant
|
200
|
path_data_fus<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_10242012_GAM"
|
201
|
#list files that contain model objects and ratingin-testing information for CAI and Fusion
|
202
|
obj_mod_fus_name<-"results_mod_obj__365d_GAM_fusion_const_all_lstd_11022012.RData"
|
203
|
#obj_mod_cai_name<-"results_mod_obj__365d_GAM_CAI2_const_all_10312012.RData"
|
204
|
obj_mod_cai_name<-"results_mod_obj__365d_GAM_CAI3_const_all_12072012.RData"
|
205
|
gam_cai_mod<-load_obj(file.path(path_data_cai,obj_mod_cai_name))
|
206
|
lf_raster_cai<-"*_365d_GAM_CAI3_const_all_12072012.rst"
|
207
|
lf_raster_fus<-"*_365d_GAM_fusion_const_all_lstd_11022012.rst"
|
208
|
|
209
|
gam_fus<-load_obj(file.path(path_data_fus,obj_mod_fus_name))
|
210
|
gam_cai<-load_obj(file.path(path_data_cai,obj_mod_cai_name)) #This contains all the info
|
211
|
sampling_date_list<-gam_fus$sampling_obj$sampling_dat$date
|
212
|
|
213
|
### Projection for the current region
|
214
|
proj_str="+proj=lcc +lat_1=43 +lat_2=45.5 +lat_0=41.75 +lon_0=-120.5 +x_0=400000 +y_0=0 +ellps=GRS80 +units=m +no_defs";
|
215
|
#User defined output prefix
|
216
|
|
217
|
### MAKE THIS A FUNCTION TO LOAD STACK AND DEFINE VALID RANGE...
|
218
|
#CRS<-proj4string(ghcn) #Storing projection information (ellipsoid, datum,etc.)
|
219
|
lines<-read.table(paste(path,"/",inlistf,sep=""), sep="") #Column 1 contains the names of raster files
|
220
|
inlistvar<-lines[,1]
|
221
|
inlistvar<-paste(path,"/",as.character(inlistvar),sep="")
|
222
|
covar_names<-as.character(lines[,2]) #Column two contains short names for covaraites
|
223
|
|
224
|
s_raster<- stack(inlistvar) #Creating a stack of raster images from the list of variables.
|
225
|
layerNames(s_raster)<-covar_names #Assigning names to the raster layers
|
226
|
projection(s_raster)<-proj_str
|
227
|
|
228
|
#Create mask using land cover data
|
229
|
pos<-match("LC10",layerNames(s_raster)) #Find the layer which contains water bodies
|
230
|
LC10<-subset(s_raster,pos)
|
231
|
LC10[is.na(LC10)]<-0 #Since NA values are 0, we assign all zero to NA
|
232
|
mask_land<-LC10<100 #All values below 100% water are assigned the value 1, value 0 is "water"
|
233
|
mask_land_NA<-mask_land
|
234
|
mask_land_NA[mask_land_NA==0]<-NA #Water bodies are assigned value 1
|
235
|
|
236
|
data_name<-"mask_land_OR"
|
237
|
raster_name<-paste(data_name,".rst", sep="")
|
238
|
writeRaster(mask_land, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
239
|
#writeRaster(r2, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
240
|
|
241
|
pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM"
|
242
|
ELEV_SRTM<-raster(s_raster,layer=pos) #Select layer from stack on 10/30
|
243
|
s_raster<-dropLayer(s_raster,pos)
|
244
|
ELEV_SRTM[ELEV_SRTM <0]<-NA
|
245
|
mask_ELEV_SRTM<-ELEV_SRTM>0
|
246
|
|
247
|
#Change this a in loop...
|
248
|
pos<-match("LC1",layerNames(s_raster)) #Find column with name "value"
|
249
|
LC1<-raster(s_raster,layer=pos) #Select layer from stack
|
250
|
s_raster<-dropLayer(s_raster,pos)
|
251
|
LC1[is.na(LC1)]<-0
|
252
|
pos<-match("LC2",layerNames(s_raster)) #Find column with name "value"
|
253
|
LC2<-raster(s_raster,layer=pos) #Select layer from stack
|
254
|
s_raster<-dropLayer(s_raster,pos)
|
255
|
LC2[is.na(LC2)]<-0
|
256
|
pos<-match("LC3",layerNames(s_raster)) #Find column with name "value"
|
257
|
LC3<-raster(s_raster,layer=pos) #Select layer from stack
|
258
|
s_raster<-dropLayer(s_raster,pos)
|
259
|
LC3[is.na(LC3)]<-0
|
260
|
pos<-match("LC4",layerNames(s_raster)) #Find column with name "value"
|
261
|
LC4<-raster(s_raster,layer=pos) #Select layer from stack
|
262
|
s_raster<-dropLayer(s_raster,pos)
|
263
|
LC4[is.na(LC4)]<-0
|
264
|
pos<-match("LC6",layerNames(s_raster)) #Find column with name "value"
|
265
|
LC6<-raster(s_raster,layer=pos) #Select layer from stack
|
266
|
s_raster<-dropLayer(s_raster,pos)
|
267
|
LC6[is.na(LC6)]<-0
|
268
|
pos<-match("LC7",layerNames(s_raster)) #Find column with name "value"
|
269
|
LC7<-raster(s_raster,layer=pos) #Select layer from stack
|
270
|
s_raster<-dropLayer(s_raster,pos)
|
271
|
LC7[is.na(LC7)]<-0
|
272
|
pos<-match("LC9",layerNames(s_raster)) #Find column with name "LC9", this is wetland...
|
273
|
LC9<-raster(s_raster,layer=pos) #Select layer from stack
|
274
|
s_raster<-dropLayer(s_raster,pos)
|
275
|
LC9[is.na(LC9)]<-0
|
276
|
|
277
|
LC_s<-stack(LC1,LC2,LC3,LC4,LC6,LC7)
|
278
|
layerNames(LC_s)<-c("LC1_forest","LC2_shrub","LC3_grass","LC4_crop","LC6_urban","LC7_barren")
|
279
|
LC_s <-mask(LC_s,mask_ELEV_SRTM)
|
280
|
plot(LC_s)
|
281
|
|
282
|
s_raster<-addLayer(s_raster, LC_s)
|
283
|
|
284
|
################ VISUALIZATION !!!!!!!! ############
|
285
|
#updated the analysis
|
286
|
|
287
|
dates<-c("20100901")
|
288
|
i=2
|
289
|
|
290
|
for(i in 1:length(dates)){
|
291
|
|
292
|
date_selected<-dates[i]
|
293
|
|
294
|
#Selecting model from stack
|
295
|
mf_selected<-"FSS_kr" #model to be used in the assessment...fusion+kr
|
296
|
mc_selected<-"CAI_mod6" #model to be used in the assessment...CAI+kr...
|
297
|
###Note that the order of the layer is not the same as in the dataframe..this part can be modified later
|
298
|
|
299
|
m_fus<-3 #model image to use in fus: mod7 is 1 in raster stack:
|
300
|
m_CAI<-2 #model image to use in CAI: mod+1
|
301
|
rast_fus_pred<-raster(predictions,m_fus) # Select the first model from the stack i.e fusion with kriging for both steps
|
302
|
rast_cai_pred<-raster(predictions,m_CAI)
|
303
|
layerNames(rast_cai_pred)<-paste("cai",date_selected,sep="_")
|
304
|
layerNames(rast_fus_pred)<-paste("fus",date_selected,sep="_")
|
305
|
|
306
|
## Start difference analysis
|
307
|
#Calculate difference image for the date selected
|
308
|
rast_diff<-rast_fus_pred-rast_cai_pred
|
309
|
layerNames(rast_diff)<-paste("diff",date_selected,sep="_")
|
310
|
mean_val<-cellStats(rast_diff,mean)
|
311
|
sd_val<-cellStats(rast_diff,sd)
|
312
|
|
313
|
#Summarize results using plot
|
314
|
#LC1 and LC3 and LC4
|
315
|
avl<-c(0,10,1,10,20,2,20,30,3,30,40,4,40,50,5,50,60,6,60,70,7,70,80,8,80,90,9,90,100,10)#Note that category 1 does not include 0!!
|
316
|
rclmat<-matrix(avl,ncol=3,byrow=TRUE)
|
317
|
LC1_rec<-reclass(LC1,rclmat) #Loss of layer names when using reclass
|
318
|
LC2_rec<-reclass(LC2,rclmat) #Loss of layer names when using reclass
|
319
|
LC3_rec<-reclass(LC3,rclmat) #Loss of layer names when using reclass
|
320
|
LC4_rec<-reclass(LC4,rclmat) #Loss of layer names when using reclass
|
321
|
LC6_rec<-reclass(LC6,rclmat) #Loss of layer names when using reclass
|
322
|
|
323
|
#LC_s<-stack(LC1,LC3,LC4,LC6)
|
324
|
LC_s<-stack(LC1,LC2,LC3,LC4,LC6)
|
325
|
layerNames(LC_s)<-c("LC1_forest", "LC2_shrub", "LC3_grass", "LC4_crop", "LC6_urban")
|
326
|
LC_s<-mask(LC_s,mask_ELEV_SRTM)
|
327
|
LC_rec_s<-reclass(LC_s,rclmat)
|
328
|
|
329
|
#plot average difference per class of forest and LC2
|
330
|
rast_stack_zones<-LC_rec_s
|
331
|
|
332
|
avg_LC1_rec<-zonal(rast_diff,zones=LC1_rec,stat="mean",na.rm=TRUE)
|
333
|
avg_LC2_rec<-zonal(rast_diff,zones=LC2_rec,stat="mean",na.rm=TRUE)
|
334
|
avg_LC3_rec<-zonal(rast_diff,zones=LC3_rec,stat="mean",na.rm=TRUE)
|
335
|
avg_LC4_rec<-zonal(rast_diff,zones=LC4_rec,stat="mean",na.rm=TRUE)
|
336
|
avg_LC6_rec<-zonal(rast_diff,zones=LC6_rec,stat="mean",na.rm=TRUE)
|
337
|
|
338
|
std_LC1_rec<-zonal(rast_diff,zones=LC1_rec,stat="sd",na.rm=TRUE)
|
339
|
std_LC2_rec<-zonal(rast_diff,zones=LC2_rec,stat="sd",na.rm=TRUE)
|
340
|
std_LC3_rec<-zonal(rast_diff,zones=LC3_rec,stat="sd",na.rm=TRUE)
|
341
|
std_LC4_rec<-zonal(rast_diff,zones=LC4_rec,stat="sd",na.rm=TRUE)
|
342
|
std_LC6_rec<-zonal(rast_diff,zones=LC6_rec,stat="sd",na.rm=TRUE)
|
343
|
|
344
|
avg_LC_rec<-zonal(rast_diff,zones=LC_rec_s,stat="mean",na.rm=TRUE)
|
345
|
std_LC_rec<-zonal(rast_diff,zones=LC_rec_s,stat="sd",na.rm=TRUE)
|
346
|
|
347
|
zones_stat_std<-as.data.frame(cbind(std_LC1_rec,std_LC2_rec[,2],std_LC3_rec[,2],std_LC4_rec[,2],std_LC6_rec[,2]))
|
348
|
zones_stat<-as.data.frame(cbind(avg_LC1_rec,avg_LC2_rec[,2],avg_LC3_rec[,2],avg_LC4_rec[,2],avg_LC6_rec[,2]))
|
349
|
names(zones_stat)<-c("zones","LC1_forest", "LC2_shrub", "LC3_grass", "LC4_crop", "LC6_urban")
|
350
|
names(zones_stat_std)<-c("zones","LC1_forest", "LC2_shrub", "LC3_grass", "LC4_crop", "LC6_urban")
|
351
|
|
352
|
X11()
|
353
|
#Selecting model from stack
|
354
|
#mf_selected<-"FSS_kr" #model to be used in the assessment...fusion+kr
|
355
|
#mc_selected<-"CAI_mod7" #model to be used in the assessment...CAI+kr...
|
356
|
plot(zones_stat$zones,zones_stat$LC1_forest,type="b",ylim=c(-4.5,4.5),
|
357
|
ylab="",xlab="",axes=FALSE)
|
358
|
mtext("difference between FSS and CAI (degree C)",line=3,side=2,cex=1.2,font=2) #Add ylab with distance 3 from box
|
359
|
mtext("land cover percent classes",side=1,cex=1.2,line=3,font=2)
|
360
|
lines(zones_stat$zones,zones_stat[,3],col="red",type="b")
|
361
|
lines(zones_stat$zones,zones_stat[,4],col="blue",type="b")
|
362
|
lines(zones_stat$zones,zones_stat[,5],col="darkgreen",type="b")
|
363
|
lines(zones_stat$zones,zones_stat[,6],col="purple",type="b")
|
364
|
breaks_lab<-zones_stat$zones
|
365
|
tick_lab<-c("0","1-10","","20-30","","40-50","","60-70","","80-90","90-100") #Not enough space for
|
366
|
#tick_lab<-c("0","10-20","30-40","60-70","80-90","90-100")
|
367
|
axis(side=1,las=1,tick=TRUE,
|
368
|
at=breaks_lab,labels=tick_lab, cex.axis=1.2,font=2) #reduce number of labels to Jan and June
|
369
|
#text(tick_lab, par(“usr”)[3], labels = tick_lab, srt = 45, adj = c(1.1,1.1), xpd = TRUE, cex=.9)
|
370
|
axis(2,cex.axis=1.2,font=2)
|
371
|
box()
|
372
|
legend("topleft",legend=c("LC1_forest", "LC2_shrub", "LC3_grass", "LC4_crop", "LC6_urban"),
|
373
|
cex=1, col=c("black","red","blue","darkgreen","purple"),bty="n",
|
374
|
lty=1)
|
375
|
title(paste("Prediction tmax difference (",mf_selected,"-",mc_selected,") and land cover ",sep=""),cex=1.4,font=2)
|
376
|
savePlot(paste("fig6_diff_prediction_tmax_difference_land cover",mf_selected,mc_selected,date_selected,out_prefix,".png", sep="_"), type="png")
|
377
|
dev.off()
|
378
|
|
379
|
LC1<-mask(LC1,mask_ELEV_SRTM)
|
380
|
cellStats(LC1,"countNA") #Check that NA have been assigned to water and areas below 0 m
|
381
|
|
382
|
LC1_50_m<- LC1>50
|
383
|
LC1_100_m<- LC1>=100
|
384
|
LC1_50_m[LC1_50_m==0]<-NA
|
385
|
LC1_100_m[LC1_100_m==0]<-NA
|
386
|
LC1_50<-LC1_50_m*LC1
|
387
|
LC1_100<-LC1_100_m*LC1
|
388
|
avl<-c(0,500,1,500,1000,2,1000,1500,3,1500,2000,4,2000,4000,5)
|
389
|
rclmat<-matrix(avl,ncol=3,byrow=TRUE)
|
390
|
elev_rec<-reclass(ELEV_SRTM,rclmat) #Loss of layer names when using reclass
|
391
|
|
392
|
elev_rec_forest<-elev_rec*LC1_100_m
|
393
|
avg_elev_rec<-zonal(rast_diff,zones=elev_rec,stat="mean",na.rm=TRUE)
|
394
|
std_elev_rec<-zonal(rast_diff,zones=elev_rec,stat="sd",na.rm=TRUE)
|
395
|
avg_elev_rec_forest<-zonal(rast_diff,zones=elev_rec_forest,stat="mean",na.rm=TRUE)
|
396
|
std_elev_rec_forest<-zonal(rast_diff,zones=elev_rec_forest,stat="sd",na.rm=TRUE)
|
397
|
|
398
|
## CREATE plots
|
399
|
X11()
|
400
|
plot(avg_elev_rec[,1],avg_elev_rec[,2],type="b",ylim=c(-10,1),
|
401
|
ylab="",xlab="",axes=FALSE)
|
402
|
mtext("tmax difference between FSS and CAI (degree C)",side=2,cex=1.2,line=3,font=2)
|
403
|
mtext("elevation classes (m)",side=1,cex=1.2,line=3,font=2)
|
404
|
lines(avg_elev_rec_forest[,1],avg_elev_rec_forest[,2],col="green",type="b") #Elevation and 100% forest...
|
405
|
breaks_lab<-avg_elev_rec[,1]
|
406
|
elev_lab<-c("0-500","500-1000","1000-1500","1500-2000","2000-4000")
|
407
|
axis(side=1,las=1,
|
408
|
at=breaks_lab,labels=elev_lab, cex=1.5,font=2) #reduce number of labels to Jan and June
|
409
|
axis(2,cex.axis=1.2,font=2)
|
410
|
legend("bottomleft",legend=c("Elevation", "elev_forest"),
|
411
|
cex=1, lwd=1.3,col=c("black","green"),bty="n",
|
412
|
lty=1)
|
413
|
box()
|
414
|
title(paste("Prediction tmax difference (",mf_selected,"-",mc_selected,") and elevation ",sep=""),cex=1.4,font=2)
|
415
|
savePlot(paste("fig7_diff_prediction_tmax_difference_elevation",mf_selected,mc_selected,date_selected,out_prefix,".png", sep="_"), type="png")
|
416
|
dev.off()
|
417
|
#Add plots with std as CI
|
418
|
|
419
|
}
|