1
|
################## Validation and analyses of results #######################################
|
2
|
############################ Covariate production for a given tile/region ##########################################
|
3
|
#This script examines inputs and outputs from the interpolation step.
|
4
|
#Part 1: Script produces plots for every selected date
|
5
|
#Part 2: Examine
|
6
|
#AUTHOR: Benoit Parmentier
|
7
|
#DATE: 03/18/2013
|
8
|
|
9
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#???--
|
10
|
|
11
|
##Comments and TODO:
|
12
|
#Separate inteprolation results analyses from covariates analyses
|
13
|
|
14
|
##################################################################################################
|
15
|
|
16
|
###Loading R library and packages
|
17
|
library(RPostgreSQL)
|
18
|
library(sp) # Spatial pacakge with class definition by Bivand et al.
|
19
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al.
|
20
|
library(rgdal) # GDAL wrapper for R, spatial utilities
|
21
|
library(raster)
|
22
|
library(gtools)
|
23
|
library(rasterVis)
|
24
|
library(graphics)
|
25
|
library(grid)
|
26
|
library(lattice)
|
27
|
|
28
|
### Parameters and arguments
|
29
|
|
30
|
##Paths to inputs and output
|
31
|
#Select relevant dates and load R objects created during the interpolation step
|
32
|
|
33
|
##Paths to inputs and output
|
34
|
script_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/"
|
35
|
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
|
36
|
out_path<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data/"
|
37
|
infile_covar<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script
|
38
|
date_selected<-c("20000101") ##This is for year 2000!!!
|
39
|
raster_prediction_obj<-"raster_prediction_obj__365d_GAM_fus5_all_lstd_03132013.RData"
|
40
|
#out_prefix<-"_365d_GAM_fus5_all_lstd_03132013"
|
41
|
#out_prefix<-"_365d_GAM_fus5_all_lstd_03142013" #User defined output prefix
|
42
|
out_prefix<-"_365d_GAM_fus5_all_lstd_03272013" #User defined output prefix
|
43
|
var<-"TMAX"
|
44
|
#gam_fus_mod<-load_obj("gam_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
|
45
|
#validation_obj<-load_obj("gam_fus_validation_mod_365d_GAM_fus5_all_lstd_02202013.RData")
|
46
|
#clim_obj<-load_obj("gamclim_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
|
47
|
|
48
|
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
|
49
|
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
|
50
|
lst_names<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12",
|
51
|
"nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
|
52
|
"nobs_09","nobs_10","nobs_11","nobs_12")
|
53
|
covar_names<-c(rnames,lc_names,lst_names)
|
54
|
|
55
|
list_param<-list(in_path,out_path,script_path,raster_prediction_obj,infile_covar,covar_names,date_selected,var,out_prefix)
|
56
|
names(list_param)<-c("in_path","out_path","script_path","raster_prediction_obj",
|
57
|
"infile_covar","covar_names","date_selected","var","out_prefix")
|
58
|
|
59
|
setwd(in_path)
|
60
|
|
61
|
## make this a script that calls several function:
|
62
|
#1) covariate script
|
63
|
#2) plots by dates
|
64
|
#3) number of data points monthly and daily
|
65
|
|
66
|
### Functions used in the script
|
67
|
|
68
|
load_obj <- function(f)
|
69
|
{
|
70
|
env <- new.env()
|
71
|
nm <- load(f, env)[1]
|
72
|
env[[nm]]
|
73
|
}
|
74
|
|
75
|
|
76
|
### PLOTTING RESULTS FROM VENEZUELA INTERPOLATION FOR ANALYSIS
|
77
|
#source(file.path(script_path,"results_interpolation_date_output_analyses_03182013.R"))
|
78
|
#j=1
|
79
|
#plots_assessment_by_date(1,list_param)
|
80
|
plots_assessment_by_date<-function(j,list_param){
|
81
|
|
82
|
date_selected<-list_param$date_selected
|
83
|
var<-list_param$var
|
84
|
#gam_fus_mod<-load_obj("gam_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
|
85
|
#validation_obj<-load_obj("gam_fus_validation_mod_365d_GAM_fus5_all_lstd_02202013.RData")
|
86
|
#clim_obj<-load_obj("gamclim_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
|
87
|
|
88
|
raster_prediction_obj<-load_obj(list_param$raster_prediction_obj)
|
89
|
#method_mod_obj<-raster_prediction_obj$method_mod_obj
|
90
|
method_mod_obj<-raster_prediction_obj$gam_fus_mod #change later for any model type
|
91
|
#validation_obj<-raster_prediction_obj$validation_obj
|
92
|
validation_obj<-raster_prediction_obj$gam_fus_validation_mod #change later for any model type
|
93
|
#clim_obj<-raster_prediction_obj$clim_obj
|
94
|
clim_obj<-raster_prediction_obj$gamclim_fus_mod #change later for any model type
|
95
|
|
96
|
if (var=="TMAX"){
|
97
|
y_var_name<-"dailyTmax"
|
98
|
}
|
99
|
if (var=="TMIN"){
|
100
|
y_var_name<-"dailyTmin"
|
101
|
}
|
102
|
|
103
|
## Read covariate stack...
|
104
|
covar_names<-list_param$covar_names
|
105
|
s_raster<-brick(infile_covar) #read in the data stack
|
106
|
names(s_raster)<-covar_names #Assigning names to the raster layers: making sure it is included in the extraction
|
107
|
|
108
|
## Prepare study area mask: based on LC12 (water)
|
109
|
|
110
|
LC_mask<-subset(s_raster,"LC12")
|
111
|
LC_mask[LC_mask==100]<-NA
|
112
|
LC_mask <- LC_mask < 100
|
113
|
LC_mask_rec<-LC_mask
|
114
|
LC_mask_rec[is.na(LC_mask_rec)]<-0
|
115
|
|
116
|
#determine index position matching date selected
|
117
|
|
118
|
for (j in 1:length(date_selected)){
|
119
|
for (i in 1:length(method_mod_obj)){
|
120
|
if(method_mod_obj[[i]]$sampling_dat$date==date_selected[j]){
|
121
|
i_dates[[j]]<-i
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
#Examine the first select date add loop or function later
|
126
|
#j=1
|
127
|
date<-strptime(date_selected[j], "%Y%m%d") # interpolation date being processed
|
128
|
month<-strftime(date, "%m") # current month of the date being processed
|
129
|
|
130
|
#Get raster stack of interpolated surfaces
|
131
|
index<-i_dates[[j]]
|
132
|
pred_temp<-as.character(method_mod_obj[[index]]$dailyTmax) #list of files
|
133
|
rast_pred_temp<-stack(pred_temp) #stack of temperature predictions from models
|
134
|
|
135
|
#Get validation metrics, daily spdf training and testing stations, monthly spdf station input
|
136
|
sampling_dat<-method_mod_obj[[index]]$sampling_dat
|
137
|
metrics_v<-validation_obj[[index]]$metrics_v
|
138
|
metrics_s<-validation_obj[[index]]$metrics_s
|
139
|
data_v<-validation_obj[[index]]$data_v
|
140
|
data_s<-validation_obj[[index]]$data_s
|
141
|
data_month<-clim_obj[[index]]$data_month
|
142
|
formulas<-clim_obj[[index]]$formulas
|
143
|
|
144
|
#Adding layer LST to the raster stack of covariates
|
145
|
#The names of covariates can be changed...
|
146
|
|
147
|
LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
|
148
|
pos<-match("LST",layerNames(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
|
149
|
s_raster<-dropLayer(s_raster,pos) # If it exists drop layer
|
150
|
pos<-match(LST_month,layerNames(s_raster)) #Find column with the current month for instance mm12
|
151
|
r1<-raster(s_raster,layer=pos) #Select layer from stack
|
152
|
layerNames(r1)<-"LST"
|
153
|
#Get mask image!!
|
154
|
|
155
|
date_proc<-strptime(sampling_dat$date, "%Y%m%d") # interpolation date being processed
|
156
|
mo<-as.integer(strftime(date_proc, "%m")) # current month of the date being processed
|
157
|
day<-as.integer(strftime(date_proc, "%d"))
|
158
|
year<-as.integer(strftime(date_proc, "%Y"))
|
159
|
datelabel=format(ISOdate(year,mo,day),"%b %d, %Y")
|
160
|
|
161
|
## Figure 1: LST_TMax_scatterplot
|
162
|
|
163
|
rmse<-metrics_v$rmse[nrow(metrics_v)]
|
164
|
rmse_f<-metrics_s$rmse[nrow(metrics_s)]
|
165
|
|
166
|
png(paste("LST_TMax_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
167
|
out_prefix,".png", sep=""))
|
168
|
plot(data_month$TMax,data_month$LST,xlab="Station mo Tmax",ylab="LST mo Tmax")
|
169
|
title(paste("LST vs TMax for",datelabel,sep=" "))
|
170
|
abline(0,1)
|
171
|
nb_point<-paste("n=",length(data_month$TMax),sep="")
|
172
|
mean_bias<-paste("Mean LST bias= ",format(mean(data_month$LSTD_bias,na.rm=TRUE),digits=3),sep="")
|
173
|
#Add the number of data points on the plot
|
174
|
legend("topleft",legend=c(mean_bias,nb_point),bty="n")
|
175
|
dev.off()
|
176
|
|
177
|
## Figure 2: Daily_tmax_monthly_TMax_scatterplot, modify for TMin!!
|
178
|
|
179
|
png(paste("Daily_tmax_monthly_TMax_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
180
|
out_prefix,".png", sep=""))
|
181
|
plot(dailyTmax~TMax,data=data_s,xlab="Mo Tmax",ylab=paste("Daily for",datelabel),main="across stations in VE")
|
182
|
nb_point<-paste("ns=",length(data_s$TMax),sep="")
|
183
|
nb_point2<-paste("ns_obs=",length(data_s$TMax)-sum(is.na(data_s[[y_var_name]])),sep="")
|
184
|
nb_point3<-paste("n_month=",length(data_month$TMax),sep="")
|
185
|
#Add the number of data points on the plot
|
186
|
legend("topleft",legend=c(nb_point,nb_point2,nb_point3),bty="n",cex=0.8)
|
187
|
dev.off()
|
188
|
|
189
|
## Figure 3: Predicted_tmax_versus_observed_scatterplot
|
190
|
|
191
|
#This is for mod_kr!! add other models later...
|
192
|
png(paste("Predicted_tmax_versus_observed_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
193
|
out_prefix,".png", sep=""))
|
194
|
#plot(data_s$mod_kr~data_s[[y_var_name]],xlab=paste("Actual daily for",datelabel),ylab="Pred daily")
|
195
|
|
196
|
y_range<-range(c(data_s$mod_kr,data_v$mod_kr),na.rm=T)
|
197
|
x_range<-range(c(data_s[[y_var_name]],data_v[[y_var_name]]),na.rm=T)
|
198
|
col_t<- c("black","red")
|
199
|
pch_t<- c(1,2)
|
200
|
plot(data_s$mod_kr,data_s[[y_var_name]],
|
201
|
xlab=paste("Actual daily for",datelabel),ylab="Pred daily",
|
202
|
ylim=y_range,xlim=x_range,col=col_t[1],pch=pch_t[1])
|
203
|
points(data_v$mod_kr,data_v[[y_var_name]],col=col_t[2],pch=pch_t[2])
|
204
|
grid(lwd=0.5, col="black")
|
205
|
#plot(data_v$mod_kr~data_v[[y_var_name]],xlab=paste("Actual daily for",datelabel),ylab="Pred daily")
|
206
|
abline(0,1)
|
207
|
legend("topleft",legend=c("training","testing"),pch=pch_t,col=col_t,bty="n",cex=0.8)
|
208
|
title(paste("Predicted_tmax_versus_observed_scatterplot for",datelabel,sep=" "))
|
209
|
nb_point1<-paste("ns_obs=",length(data_s$TMax)-sum(is.na(data_s[[y_var_name]])),sep="")
|
210
|
rmse_str1<-paste("RMSE= ",format(rmse,digits=3),sep="")
|
211
|
rmse_str2<-paste("RMSE_f= ",format(rmse_f,digits=3),sep="")
|
212
|
|
213
|
#Add the number of data points on the plot
|
214
|
legend("bottomright",legend=c(nb_point1,rmse_str1,rmse_str2),bty="n",cex=0.8)
|
215
|
dev.off()
|
216
|
|
217
|
## Figure 5: prediction raster images
|
218
|
png(paste("Raster_prediction_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
219
|
out_prefix,".png", sep=""))
|
220
|
#paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
|
221
|
names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
|
222
|
#plot(rast_pred_temp)
|
223
|
levelplot(rast_pred_temp)
|
224
|
dev.off()
|
225
|
|
226
|
## Figure 5b: prediction raster images
|
227
|
png(paste("Raster_prediction_plot",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
228
|
out_prefix,".png", sep=""))
|
229
|
#paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
|
230
|
names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
|
231
|
#plot(rast_pred_temp)
|
232
|
plot(rast_pred_temp)
|
233
|
dev.off()
|
234
|
|
235
|
## Figure 6: training and testing stations used
|
236
|
png(paste("Training_testing_stations_map_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
237
|
out_prefix,".png", sep=""))
|
238
|
plot(raster(rast_pred_temp,layer=5))
|
239
|
plot(data_s,col="black",cex=1.2,pch=2,add=TRUE)
|
240
|
plot(data_v,col="red",cex=1.2,pch=1,add=TRUE)
|
241
|
legend("topleft",legend=c("training stations", "testing stations"),
|
242
|
cex=1, col=c("black","red"),
|
243
|
pch=c(2,1),bty="n")
|
244
|
dev.off()
|
245
|
|
246
|
## Figure 7: monthly stations used
|
247
|
|
248
|
png(paste("Monthly_data_study_area",
|
249
|
out_prefix,".png", sep=""))
|
250
|
plot(raster(rast_pred_temp,layer=5))
|
251
|
plot(data_month,col="black",cex=1.2,pch=4,add=TRUE)
|
252
|
title("Monthly ghcn station in Venezuela for January")
|
253
|
dev.off()
|
254
|
|
255
|
## Figure 8: delta surface and bias
|
256
|
|
257
|
png(paste("Bias_delta_surface_",sampling_dat$date[i],"_",sampling_dat$prop[i],
|
258
|
"_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))
|
259
|
|
260
|
bias_rast<-stack(clim_obj[[index]]$bias)
|
261
|
delta_rast<-raster(method_mod_obj[[index]]$delta) #only one delta image!!!
|
262
|
names(delta_rast)<-"delta"
|
263
|
rast_temp_date<-stack(bias_rast,delta_rast)
|
264
|
rast_temp_date<-mask(rast_temp_date,LC_mask,file="test.tif",overwrite=TRUE)
|
265
|
#bias_d_rast<-raster("fusion_bias_LST_20100103_30_1_10d_GAM_fus5_all_lstd_02082013.rst")
|
266
|
plot(rast_temp_date)
|
267
|
|
268
|
dev.off()
|
269
|
|
270
|
#Figure 9: histogram for all images...
|
271
|
|
272
|
#histogram(rast_pred_temp)
|
273
|
list_output_analyses<-list(metrics_s,metrics_v)
|
274
|
return(list_output_analyses)
|
275
|
|
276
|
}
|
277
|
|