Project

General

Profile

« Previous | Next » 

Revision 5b9ca6d6

Added by Benoit Parmentier over 11 years ago

results ouput analyses, modifications outputs and clean up

View differences:

climate/research/oregon/interpolation/results_interpolation_date_output_analyses.R
4 4
#Part 1: Script produces plots for every selected date
5 5
#Part 2: Examine 
6 6
#AUTHOR: Benoit Parmentier                                                                       
7
#DATE: 04/16/2013                                                                                 
7
#DATE: 05/10/2013                                                                                 
8 8

  
9 9
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#???--   
10 10

  
11
##Comments and TODO:
12
#Separate inteprolation results analyses from covariates analyses 
13

  
14 11
##################################################################################################
15 12

  
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 13

  
28 14
### Parameters and arguments
29

  
30 15
##Paths to inputs and output
31 16
#Select relevant dates and load R objects created during the interpolation step
32 17

  
......
56 41
#names(list_param_results_analyses)<-c("in_path","out_path","script_path","raster_prediction_obj", "interpolation_method",
57 42
#                     "infile_covar","covar_names","date_selected","var","out_prefix")
58 43

  
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 44
plots_assessment_by_date<-function(j,list_param){
45
  ###Function to assess results from interpolation predictions
46
  #AUTHOR: Benoit Parmentier                                                                       
47
  #DATE: 05/10/2013                                                                                 
48
  #PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
49
  
50
  #1) in_path
51
  #2) out_path
52
  #3) script_path
53
  #4) raster_prediction_obj
54
  #5) interpolation_method
55
  #6) infile_covariates
56
  #7) covar_names
57
  #8) date_selected_results
58
  #9) var
59
  #10) out_prefix
83 60
  
84
  date_selected<-list_param$date_selected
85
  var<-list_param$var
61
  ###Loading R library and packages   
62
  library(RPostgreSQL)
63
  library(sp)                                             # Spatial pacakge with class definition by Bivand et al.
64
  library(spdep)                                          # Spatial pacakge with methods and spatial stat. by Bivand et al.
65
  library(rgdal)                                          # GDAL wrapper for R, spatial utilities
66
  library(raster)
67
  library(gtools)
68
  library(rasterVis)
69
  library(graphics)
70
  library(grid)
71
  library(lattice)
72
  
73
  ## Function(s) used in script
74
  
75
  load_obj <- function(f) 
76
  {
77
    env <- new.env()
78
    nm <- load(f, env)[1]  
79
    env[[nm]]
80
  }
81
  
82
  ### BEGIN SCRIPT
83
  #Parse input parameters
84
  
85
  date_selected<-list_param$date_selected_results #dates for plot creation
86
  var<-list_param$var #variable being interpolated
87
  out_path <- list_param$out_path
86 88
  interpolation_method <- list_param$interpolation_method
89
  infile_covariates <- list_param$infile_covariates
90
  covar_names<-list_param$covar_names
87 91
  
88 92
  raster_prediction_obj<-list_param$raster_prediction_obj
89 93
  method_mod_obj<-raster_prediction_obj$method_mod_obj
......
99 103
    y_var_month <-"TMin"
100 104
  }
101 105
  
102
  ## Read covariate stack...
103
  covar_names<-list_param$covar_names
104
  s_raster<-brick(infile_covar)                   #read in the data stack
106
  ## Read covariate brick...
107
  s_raster<-brick(infile_covariates)
105 108
  names(s_raster)<-covar_names               #Assigning names to the raster layers: making sure it is included in the extraction
106 109
  
107 110
  ## Prepare study area  mask: based on LC12 (water)
......
128 131
  
129 132
  #Get raster stack of interpolated surfaces
130 133
  index<-i_dates[[j]]
131
  pred_temp<-as.character(method_mod_obj[[index]][[y_var_name]]) #list of files
132
  rast_pred_temp<-stack(pred_temp) #stack of temperature predictions from models 
134
  pred_temp<-as.character(method_mod_obj[[index]][[y_var_name]]) #list of files with path included
135
  rast_pred_temp_s <-stack(pred_temp) #stack of temperature predictions from models (daily)
136
  rast_pred_temp <-mask(rast_pred_temp_s,LC_mask,file=file.path(out_path,"test.tif"),overwrite=TRUE)
133 137
  
134 138
  #Get validation metrics, daily spdf training and testing stations, monthly spdf station input
135 139
  sampling_dat<-method_mod_obj[[index]]$sampling_dat
......
144 148
  #The names of covariates can be changed...
145 149
  
146 150
  LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
147
  pos<-match("LST",layerNames(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
151
  pos<-match("LST",names(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
148 152
  s_raster<-dropLayer(s_raster,pos)      # If it exists drop layer
149
  pos<-match(LST_month,layerNames(s_raster)) #Find column with the current month for instance mm12
153
  pos<-match(LST_month,names(s_raster)) #Find column with the current month for instance mm12
150 154
  r1<-raster(s_raster,layer=pos)             #Select layer from stack
151
  layerNames(r1)<-"LST"
155
  names(r1)<-"LST"
152 156
  #Get mask image!!
153 157
  
154 158
  date_proc<-strptime(sampling_dat$date, "%Y%m%d")   # interpolation date being processed
......
162 166
  rmse<-metrics_v$rmse[nrow(metrics_v)]
163 167
  rmse_f<-metrics_s$rmse[nrow(metrics_s)]  
164 168
  
165
  png(paste("LST_",y_var_month,"_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
166
            out_prefix,".png", sep=""))
169
  png(file.path(out_path,paste("LST_",y_var_month,"_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
170
            out_prefix,".png", sep="")))
167 171
  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=""))
168 172
  title(paste("LST vs ", y_var_month,"for",datelabel,sep=" "))
169 173
  abline(0,1)
......
175 179
  
176 180
  ## Figure 2: Daily_tmax_monthly_TMax_scatterplot, modify for TMin!!
177 181
  
178
  png(paste("Monhth_day_scatterplot_",y_var_name,"_",y_var_month,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
179
            out_prefix,".png", sep=""))
182
  png(file.path(out_path,paste("Month_day_scatterplot_",y_var_name,"_",y_var_month,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
183
            out_prefix,".png", sep="")))
180 184
  plot(data_s[[y_var_name]]~data_s[[y_var_month]],xlab=paste("Month") ,ylab=paste("Daily for",datelabel),main="across stations in VE")
181 185
  nb_point<-paste("ns=",length(data_s[[y_var_month]]),sep="")
182 186
  nb_point2<-paste("ns_obs=",length(data_s[[y_var_month]])-sum(is.na(data_s[[y_var_name]])),sep="")
......
190 194
  #This is for mod_kr!! add other models later...
191 195
  model_name<-"mod_kr" #can be looped through models later on...
192 196
  
193
  png(paste("Predicted_versus_observed_scatterplot_",y_var_name,"_",model_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",
194
            sampling_dat$run_samp,out_prefix,".png", sep=""))
197
  png(file.path(out_path,paste("Predicted_versus_observed_scatterplot_",y_var_name,"_",model_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",
198
            sampling_dat$run_samp,out_prefix,".png", sep="")))
195 199
  y_range<-range(c(data_s[[model_name]],data_v[[model_name]]),na.rm=T)
196 200
  x_range<-range(c(data_s[[y_var_name]],data_v[[y_var_name]]),na.rm=T)
197 201
  col_t<- c("black","red")
......
215 219
  dev.off()
216 220
  
217 221
  ## Figure 4a: prediction raster images
218
  png(paste("Raster_prediction_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
219
            out_prefix,".png", sep=""))
222
  png(file.path(out_path,paste("Raster_prediction_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
223
            out_prefix,".png", sep="")))
220 224
  #paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
221 225
  names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
222 226
  #plot(rast_pred_temp)
......
224 228
  dev.off()
225 229
  
226 230
  ## Figure 4b: prediction raster images
227
  png(paste("Raster_prediction_plot",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
228
            out_prefix,".png", sep=""))
231
  png(file.path(out_path,paste("Raster_prediction_plot",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
232
            out_prefix,".png", sep="")))
229 233
  #paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
230 234
  names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
231
  #plot(rast_pred_temp)
232 235
  plot(rast_pred_temp)
233 236
  dev.off()
234 237
  
235 238
  ## Figure 5: training and testing stations used
236
  png(paste("Training_testing_stations_map_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
237
            out_prefix,".png", sep=""))
239
  png(file.path(out_path,paste("Training_testing_stations_map_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
240
            out_prefix,".png", sep="")))
238 241
  plot(raster(rast_pred_temp,layer=5))
239 242
  plot(data_s,col="black",cex=1.2,pch=2,add=TRUE)
240 243
  plot(data_v,col="red",cex=1.2,pch=1,add=TRUE)
......
245 248
  
246 249
  ## Figure 6: monthly stations used
247 250
  
248
  png(paste("Monthly_data_study_area_", y_var_name,
249
            out_prefix,".png", sep=""))
251
  png(file.path(out_path,paste("Monthly_data_study_area_", y_var_name,
252
            out_prefix,".png", sep="")))
250 253
  plot(raster(rast_pred_temp,layer=5))
251 254
  plot(data_month,col="black",cex=1.2,pch=4,add=TRUE)
252 255
  title("Monthly ghcn station in Venezuela for January")
......
255 258
  ## Figure 7: delta surface and bias
256 259
  
257 260
  if (interpolation_method=="gam_fus"){
258
    png(paste("Bias_delta_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
259
              "_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))
261
    png(file.path(out_path,paste("Bias_delta_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
262
              "_",sampling_dat$run_samp[i],out_prefix,".png", sep="")))
260 263
    
261 264
    bias_rast<-stack(clim_method_mod_obj[[index]]$bias)
262 265
    delta_rast<-raster(method_mod_obj[[index]]$delta) #only one delta image!!!
263 266
    names(delta_rast)<-"delta"
264 267
    rast_temp_date<-stack(bias_rast,delta_rast)
265
    rast_temp_date<-mask(rast_temp_date,LC_mask,file="test.tif",overwrite=TRUE)
268
    rast_temp_date<-mask(rast_temp_date,LC_mask,file=file.path(out_path,"test.tif"),overwrite=TRUE)
266 269
    #bias_d_rast<-raster("fusion_bias_LST_20100103_30_1_10d_GAM_fus5_all_lstd_02082013.rst")
267 270
    plot(rast_temp_date)
268 271
    dev.off()
269 272
  }
270 273

  
271 274
  if (interpolation_method=="gam_CAI"){
272
    png(paste("clim_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
273
              "_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))
275
    png(file.path(out_path,paste("clim_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
276
              "_",sampling_dat$run_samp[i],out_prefix,".png", sep="")))
274 277
    
275 278
    clim_rast<-stack(clim_method_mod_obj[[index]]$clim)
279
    delta_rast<-raster(method_mod_obj[[index]]$delta) #only one delta image!!!
280
    names(delta_rast)<-"delta"
276 281
    rast_temp_date<-stack(clim_rast,delta_rast)
277
    rast_temp_date<-mask(rast_temp_date,LC_mask,file="test.tif",overwrite=TRUE)
282
    rast_temp_date<-mask(rast_temp_date,LC_mask,file=file.path(out_path,"test.tif"),overwrite=TRUE)
278 283
    #bias_d_rast<-raster("fusion_bias_LST_20100103_30_1_10d_GAM_fus5_all_lstd_02082013.rst")
279 284
    plot(rast_temp_date)
280 285
    
......
285 290
  
286 291
  #histogram(rast_pred_temp)
287 292
  list_output_analyses<-list(metrics_s,metrics_v)
293
  names(list_output_analyses) <- c("metrics_s", "metrics_v")
288 294
  return(list_output_analyses)
289 295
  
290 296
}

Also available in: Unified diff