Project

General

Profile

Download (12.6 KB) Statistics
| Branch: | Revision:
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: 04/02/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<-load_obj("raster_prediction_obj_dailyTmin_365d_GAM_fus5_all_lstd_03292013.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_03292013"                #User defined output prefix
43
var<-"TMIN"
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_results_analyses<-list(in_path,out_path,script_path,raster_prediction_obj,infile_covar,covar_names,date_selected,var,out_prefix)
56
names(list_param_results_analyses)<-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_04022013.R"))
78
#j=1
79
#plots_assessment_by_date(1,list_param_results_analyses)
80

    
81

    
82
plots_assessment_by_date<-function(j,list_param){
83
  
84
  date_selected<-list_param$date_selected
85
  var<-list_param$var
86
  #gam_fus_mod<-load_obj("gam_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
87
  #validation_obj<-load_obj("gam_fus_validation_mod_365d_GAM_fus5_all_lstd_02202013.RData")
88
  #clim_obj<-load_obj("gamclim_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
89
  
90
  raster_prediction_obj<-list_param$raster_prediction_obj
91
  method_mod_obj<-raster_prediction_obj$method_mod_obj
92
  method_mod_obj<-raster_prediction_obj$gam_fus_mod #change later for any model type
93
  #validation_obj<-raster_prediction_obj$validation_obj
94
  validation_obj<-raster_prediction_obj$gam_fus_validation_mod #change later for any model type
95
  #clim_obj<-raster_prediction_obj$clim_obj
96
  clim_obj<-raster_prediction_obj$gamclim_fus_mod #change later for any model type
97
  
98
  if (var=="TMAX"){
99
    y_var_name<-"dailyTmax"
100
    y_var_month<-"TMax"
101
  }
102
  if (var=="TMIN"){
103
    y_var_name<-"dailyTmin"
104
    y_var_month <-"TMin"
105
  }
106
  
107
  ## Read covariate stack...
108
  covar_names<-list_param$covar_names
109
  s_raster<-brick(infile_covar)                   #read in the data stack
110
  names(s_raster)<-covar_names               #Assigning names to the raster layers: making sure it is included in the extraction
111
  
112
  ## Prepare study area  mask: based on LC12 (water)
113
  
114
  LC_mask<-subset(s_raster,"LC12")
115
  LC_mask[LC_mask==100]<-NA
116
  LC_mask <- LC_mask < 100
117
  LC_mask_rec<-LC_mask
118
  LC_mask_rec[is.na(LC_mask_rec)]<-0
119
    
120
  #determine index position matching date selected
121
  i_dates<-vector("list",length(date_selected))
122
  for (j in 1:length(date_selected)){
123
    for (i in 1:length(method_mod_obj)){
124
      if(method_mod_obj[[i]]$sampling_dat$date==date_selected[j]){  
125
        i_dates[[j]]<-i
126
      }
127
    }
128
  }
129
  #Examine the first select date add loop or function later
130
  #j=1
131
  date<-strptime(date_selected[j], "%Y%m%d")   # interpolation date being processed
132
  month<-strftime(date, "%m")          # current month of the date being processed
133
  
134
  #Get raster stack of interpolated surfaces
135
  index<-i_dates[[j]]
136
  pred_temp<-as.character(method_mod_obj[[index]][[y_var_name]]) #list of files
137
  rast_pred_temp<-stack(pred_temp) #stack of temperature predictions from models 
138
  
139
  #Get validation metrics, daily spdf training and testing stations, monthly spdf station input
140
  sampling_dat<-method_mod_obj[[index]]$sampling_dat
141
  metrics_v<-validation_obj[[index]]$metrics_v
142
  metrics_s<-validation_obj[[index]]$metrics_s
143
  data_v<-validation_obj[[index]]$data_v
144
  data_s<-validation_obj[[index]]$data_s
145
  data_month<-clim_obj[[index]]$data_month
146
  formulas<-clim_obj[[index]]$formulas
147
  
148
  #Adding layer LST to the raster stack of covariates
149
  #The names of covariates can be changed...
150
  
151
  LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
152
  pos<-match("LST",layerNames(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
153
  s_raster<-dropLayer(s_raster,pos)      # If it exists drop layer
154
  pos<-match(LST_month,layerNames(s_raster)) #Find column with the current month for instance mm12
155
  r1<-raster(s_raster,layer=pos)             #Select layer from stack
156
  layerNames(r1)<-"LST"
157
  #Get mask image!!
158
  
159
  date_proc<-strptime(sampling_dat$date, "%Y%m%d")   # interpolation date being processed
160
  mo<-as.integer(strftime(date_proc, "%m"))          # current month of the date being processed
161
  day<-as.integer(strftime(date_proc, "%d"))
162
  year<-as.integer(strftime(date_proc, "%Y"))
163
  datelabel=format(ISOdate(year,mo,day),"%b %d, %Y")
164
  
165
  ## Figure 1: LST_TMax_scatterplot 
166
  
167
  rmse<-metrics_v$rmse[nrow(metrics_v)]
168
  rmse_f<-metrics_s$rmse[nrow(metrics_s)]  
169
  
170
  png(paste("LST_",y_var_month,"_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
171
            out_prefix,".png", sep=""))
172
  plot(data_month[[y_var_month]],data_month$LST,xlab=paste("Station mo ",y_var_month,sep=""),ylab=paste("LST mo ",y_var_month,sep=""))
173
  title(paste("LST vs ", y_var_month,"for",datelabel,sep=" "))
174
  abline(0,1)
175
  nb_point<-paste("n=",length(data_month[[y_var_month]]),sep="")
176
  mean_bias<-paste("Mean LST bias= ",format(mean(data_month$LSTD_bias,na.rm=TRUE),digits=3),sep="")
177
  #Add the number of data points on the plot
178
  legend("topleft",legend=c(mean_bias,nb_point),bty="n")
179
  dev.off()
180
  
181
  ## Figure 2: Daily_tmax_monthly_TMax_scatterplot, modify for TMin!!
182
  
183
  png(paste("Monhth_day_scatterplot_",y_var_name,"_",y_var_month,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
184
            out_prefix,".png", sep=""))
185
  plot(data_s[[y_var_name]]~data_s[[y_var_month]],xlab=paste("Month") ,ylab=paste("Daily for",datelabel),main="across stations in VE")
186
  nb_point<-paste("ns=",length(data_s[[y_var_month]]),sep="")
187
  nb_point2<-paste("ns_obs=",length(data_s[[y_var_month]])-sum(is.na(data_s[[y_var_name]])),sep="")
188
  nb_point3<-paste("n_month=",length(data_month[[y_var_month]]),sep="")
189
  #Add the number of data points on the plot
190
  legend("topleft",legend=c(nb_point,nb_point2,nb_point3),bty="n",cex=0.8)
191
  dev.off()
192
  
193
  ## Figure 3: Predicted_tmax_versus_observed_scatterplot 
194
  
195
  #This is for mod_kr!! add other models later...
196
  model_name<-"mod_kr" #can be looped through models later on...
197
  
198
  png(paste("Predicted_versus_observed_scatterplot_",y_var_name,"_",model_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",
199
            sampling_dat$run_samp,out_prefix,".png", sep=""))
200
  y_range<-range(c(data_s[[model_name]],data_v[[model_name]]),na.rm=T)
201
  x_range<-range(c(data_s[[y_var_name]],data_v[[y_var_name]]),na.rm=T)
202
  col_t<- c("black","red")
203
  pch_t<- c(1,2)
204
  plot(data_s[[model_name]],data_s[[y_var_name]], 
205
       xlab=paste("Actual daily for",datelabel),ylab="Pred daily", 
206
       ylim=y_range,xlim=x_range,col=col_t[1],pch=pch_t[1])
207
  points(data_v[[model_name]],data_v[[y_var_name]],col=col_t[2],pch=pch_t[2])
208
  grid(lwd=0.5, col="black")
209
  abline(0,1)
210
  legend("topleft",legend=c("training","testing"),pch=pch_t,col=col_t,bty="n",cex=0.8)
211
  title(paste("Predicted_versus_observed_",y_var_name,"_",model_name,"_",datelabel,sep=" "))
212
  nb_point1<-paste("ns_obs=",length(data_s[[y_var_name]])-sum(is.na(data_s[[model_name]])),sep="")
213
  nb_point2<-paste("nv_obs=",length(data_v[[y_var_name]])-sum(is.na(data_v[[model_name]])),sep="")
214

    
215
  rmse_str1<-paste("RMSE= ",format(rmse,digits=3),sep="")
216
  rmse_str2<-paste("RMSE_f= ",format(rmse_f,digits=3),sep="")
217
  
218
  #Add the number of data points on the plot
219
  legend("bottomright",legend=c(nb_point1,nb_point2,rmse_str1,rmse_str2),bty="n",cex=0.8)
220
  dev.off()
221
  
222
  ## Figure 4a: prediction raster images
223
  png(paste("Raster_prediction_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
224
            out_prefix,".png", sep=""))
225
  #paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
226
  names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
227
  #plot(rast_pred_temp)
228
  levelplot(rast_pred_temp)
229
  dev.off()
230
  
231
  ## Figure 4b: prediction raster images
232
  png(paste("Raster_prediction_plot",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
233
            out_prefix,".png", sep=""))
234
  #paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
235
  names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
236
  #plot(rast_pred_temp)
237
  plot(rast_pred_temp)
238
  dev.off()
239
  
240
  ## Figure 5: training and testing stations used
241
  png(paste("Training_testing_stations_map_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
242
            out_prefix,".png", sep=""))
243
  plot(raster(rast_pred_temp,layer=5))
244
  plot(data_s,col="black",cex=1.2,pch=2,add=TRUE)
245
  plot(data_v,col="red",cex=1.2,pch=1,add=TRUE)
246
  legend("topleft",legend=c("training stations", "testing stations"), 
247
         cex=1, col=c("black","red"),
248
         pch=c(2,1),bty="n")
249
  dev.off()
250
  
251
  ## Figure 6: monthly stations used
252
  
253
  png(paste("Monthly_data_study_area_", y_var_name,
254
            out_prefix,".png", sep=""))
255
  plot(raster(rast_pred_temp,layer=5))
256
  plot(data_month,col="black",cex=1.2,pch=4,add=TRUE)
257
  title("Monthly ghcn station in Venezuela for January")
258
  dev.off()
259
  
260
  ## Figure 7: delta surface and bias
261
  
262
  png(paste("Bias_delta_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
263
            "_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))
264
  
265
  bias_rast<-stack(clim_obj[[index]]$bias)
266
  delta_rast<-raster(method_mod_obj[[index]]$delta) #only one delta image!!!
267
  names(delta_rast)<-"delta"
268
  rast_temp_date<-stack(bias_rast,delta_rast)
269
  rast_temp_date<-mask(rast_temp_date,LC_mask,file="test.tif",overwrite=TRUE)
270
  #bias_d_rast<-raster("fusion_bias_LST_20100103_30_1_10d_GAM_fus5_all_lstd_02082013.rst")
271
  plot(rast_temp_date)
272
  
273
  dev.off()
274
  
275
  #Figure 9: histogram for all images...
276
  
277
  #histogram(rast_pred_temp)
278
  list_output_analyses<-list(metrics_s,metrics_v)
279
  return(list_output_analyses)
280
  
281
}
282

    
(40-40/41)