Project

General

Profile

« Previous | Next » 

Revision 5ea4761b

Added by Benoit Parmentier almost 9 years ago

initial commit, assessment part3 to combine all outputs

View differences:

climate/research/oregon/interpolation/global_run_scalingup_assessment_part2_test.R
1
##############################  INTERPOLATION OF TEMPERATURES  #######################################
2
#######################  Script for assessment of scaling up on NEX : part2 ##############################
3
#This script uses the worklfow code applied to the globe. Results currently reside on NEX/PLEIADES NASA.
4
#Accuracy methods are added in the the function scripts to evaluate results.
5
#Analyses, figures, tables and data are also produced in the script.
6
#AUTHOR: Benoit Parmentier 
7
#CREATED ON: 03/23/2014  
8
#MODIFIED ON: 02/03/2016            
9
#Version: 5
10
#PROJECT: Environmental Layers project     
11
#COMMENTS: analyses for run 10 global analyses,all regions 1500x4500km with additional tiles to increase overlap 
12
#TODO:
13
#1) Add plot broken down by year and region 
14
#2) Modify code for overall assessment accross all regions and year
15
#3) Clean up
16

  
17
#First source these files:
18
#Resolved call issues from R.
19
#source /nobackupp6/aguzman4/climateLayers/sharedModules2/etc/environ.sh 
20
#
21
#setfacl -Rmd user:aguzman4:rwx /nobackupp8/bparmen1/output_run10_1500x4500_global_analyses_pred_1992_10052015
22
#setfacl -Rm user:aguzman4:rwx /nobackupp8/bparmen1/output_run_global_analyses_pred_12282015
23

  
24
#################################################################################################
25

  
26
#### FUNCTION USED IN SCRIPT
27

  
28
#function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_07182014.R" #first interp paper
29
#function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_08132014.R"
30
#function_global_run_assessment_part2 <- "global_run_scalingup_assessment_part2_functions_0923015.R"
31

  
32
############################################
33
#### Parameters and constants  
34

  
35
#on ATLAS
36
#in_dir1 <- "/data/project/layers/commons/NEX_data/test_run1_03232014/output" #On Atlas
37
#parent output dir : contains subset of the data produced on NEX
38
#in_dir1 <- "/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/output20Deg2"
39
# parent output dir for the curent script analyes
40
#out_dir <-"/data/project/layers/commons/NEX_data/output_run3_global_analyses_06192014/" #On NCEAS Atlas
41
# input dir containing shapefiles defining tiles
42
#in_dir_shp <- "/data/project/layers/commons/NEX_data/output_run5_global_analyses_08252014/output/subset/shapefiles"
43

  
44
#On NEX
45
#contains all data from the run by Alberto
46
#in_dir1 <- " /nobackupp6/aguzman4/climateLayers/out_15x45/" #On NEX
47
#parent output dir for the current script analyes
48
#out_dir <- "/nobackup/bparmen1/" #on NEX
49
#in_dir_shp <- "/nobackupp4/aguzman4/climateLayers/output4/subset/shapefiles/"
50
#in_dir <- "" #PARAM 0
51
#y_var_name <- "dailyTmax" #PARAM1
52
#interpolation_method <- c("gam_CAI") #PARAM2
53
#out_suffix<-"run10_global_analyses_01282015"
54
#out_suffix <- "output_run10_1000x3000_global_analyses_02102015"
55
#out_suffix <- "run10_1500x4500_global_analyses_pred_1992_10052015" #PARAM3
56
#out_dir <- "/data/project/layers/commons/NEX_data/output_run10_1500x4500_global_analyses_pred_1992_10052015" #PARAM4
57
#create_out_dir_param <- FALSE #PARAM 5
58
#mosaic_plot <- FALSE #PARAM6
59
#if daily mosaics NULL then mosaicas all days of the year
60
#day_to_mosaic <- c("19920101","19920102","19920103") #PARAM7
61
#CRS_WGS84 <-    CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 #CONSTANT1
62
#CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
63
#proj_str<- CRS_WGS84 #PARAM 8 #check this parameter
64
#file_format <- ".rst" #PARAM 9
65
#NA_value <- -9999 #PARAM10
66
#NA_flag_val <- NA_value
67
#multiple_region <- TRUE #PARAM 12
68
#region_name <- "world" #PARAM 13
69
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too
70
#plot_region <- TRUE
71
#num_cores <- 6 #PARAM 14
72
#region_name <- c("reg4") #reference region to merge if necessary, if world all the regions are together #PARAM 16
73
#use previous files produced in step 1a and stored in a data.frame
74
#df_assessment_files <- "df_assessment_files_reg4_1984_run_global_analyses_pred_12282015.txt" #PARAM 17
75
#threshold_missing_day <- c(367,365,300,200) #PARM18
76

  
77
#list_param_run_assessment_plottingin_dir <- list(in_dir,y_var_name, interpolation_method, out_suffix, 
78
#                      out_dir, create_out_dir_param, mosaic_plot, proj_str, file_format, NA_value,
79
#                      multiple_region, countries_shp, plot_region, num_cores, 
80
#                      region_name, df_assessment_files, threshold_missing_day) 
81

  
82
#names(list_param_run_assessment_plottingin_dir) <- c("in_dir","y_var_name","interpolation_method","out_suffix", 
83
#                      "out_dir","create_out_dir_param","mosaic_plot","proj_str","file_format","NA_value",
84
#                      "multiple_region","countries_shp","plot_region","num_cores", 
85
#                      "region_name","df_assessment_files","threshold_missing_day") 
86

  
87
#run_assessment_plotting_prediction_fun(list_param_run_assessment_plottingin_dir) 
88

  
89
run_assessment_plotting_prediction_fun <-function(list_param_run_assessment_plotting){
90
  
91
  ####
92
  #1) in_dir: input directory containing data tables and shapefiles for plotting #PARAM 0
93
  #2) y_var_name : variables being predicted e.g. dailyTmax #PARAM1
94
  #3) interpolation_method: method used #c("gam_CAI") #PARAM2
95
  #4) out_suffix: output suffix #PARAM3
96
  #5) out_dir  #
97
  #6) create_out_dir_param # FALSE #PARAM 5
98
  #7) mosaic_plot  #FALSE #PARAM6
99
  #8) proj_str # projection/coordinates system e.g. CRS_WGS84 #PARAM 8 #check this parameter
100
  #9) file_format #".rst" #PARAM 9
101
  #10) NA_value #-9999 #PARAM10
102
  #11) multiple_region  # <- TRUE #PARAM 12
103
  #12) countries_shp  #<- "world" #PARAM 13
104
  #13) plot_region  #<- TRUE
105
  #14) num_cores <- number of cores used # 6 #PARAM 14
106
  #15) region_name  #<- c("reg4"), world if full assessment #reference region to merge if necessary #PARAM 16
107
  #16) df_assessment_files  #PARAM 16
108
  #17) threshold_missing_day  #PARM18
109
  #18) year_predicted
110
  
111
  ### Loading R library and packages        
112
  #library used in the workflow production:
113
  library(gtools)                              # loading some useful tools 
114
  library(mgcv)                                # GAM package by Simon Wood
115
  library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
116
  library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
117
  library(rgdal)                               # GDAL wrapper for R, spatial utilities
118
  library(gstat)                               # Kriging and co-kriging by Pebesma et al.
119
  library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
120
  library(raster)                              # Hijmans et al. package for raster processing
121
  library(gdata)                               # various tools with xls reading, cbindX
122
  library(rasterVis)                           # Raster plotting functions
123
  library(parallel)                            # Parallelization of processes with multiple cores
124
  library(maptools)                            # Tools and functions for sp and other spatial objects e.g. spCbind
125
  library(maps)                                # Tools and data for spatial/geographic objects
126
  library(reshape)                             # Change shape of object, summarize results 
127
  library(plotrix)                             # Additional plotting functions
128
  library(plyr)                                # Various tools including rbind.fill
129
  library(spgwr)                               # GWR method
130
  library(automap)                             # Kriging automatic fitting of variogram using gstat
131
  library(rgeos)                               # Geometric, topologic library of functions
132
  #RPostgreSQL                                 # Interface R and Postgres, not used in this script
133
  library(gridExtra)
134
  #Additional libraries not used in workflow
135
  library(pgirmess)                            # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess}  
136
  library(colorRamps)
137
  library(zoo)
138
  library(xts)
139
  
140
  ####### Function used in the script #######
141
  
142
  #function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_0923015.R"
143
  #source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script 
144

  
145
  ####### PARSE INPUT ARGUMENTS/PARAMETERS #####
146
  
147
  in_dir <- list_param_run_assessment_plotting$in_dir #PARAM 1
148
  y_var_name <- list_param_run_assessment_plotting$y_var_name #PARAM2
149
  interpolation_method <- list_param_run_assessment_plotting$interpolation_method #c("gam_CAI") #PARAM3
150
  out_suffix <- list_param_run_assessment_plotting$out_suffix #PARAM4
151
  out_dir <- list_param_run_assessment_plotting$out_dir # PARAM5
152
  create_out_dir_param <- list_param_run_assessment_plotting$create_out_dir_param # FALSE #PARAM 6
153
  mosaic_plot <- list_param_run_assessment_plotting$mosaic_plot #FALSE #PARAM7
154
  proj_str<- list_param_run_assessment_plotting$proj_str #CRS_WGS84 #PARAM 8 #check this parameter
155
  file_format <- list_param_run_assessment_plotting$file_format #".rst" #PARAM 9
156
  NA_flag_val <- list_param_run_assessment_plotting$NA_flag_val #-9999 #PARAM10
157
  multiple_region <- list_param_run_assessment_plotting$multiple_region # <- TRUE #PARAM 11
158
  countries_shp <- list_param_run_assessment_plotting$countries_shp #<- "world" #PARAM 12
159
  plot_region <- list_param_run_assessment_plotting$plot_region # PARAM13 
160
  num_cores <- list_param_run_assessment_plotting$num_cores # 6 #PARAM 14
161
  region_name <- list_param_run_assessment_plotting$region_name #<- "world" #PARAM 15
162
  df_assessment_files_name <- list_param_run_assessment_plotting$df_assessment_files_name #PARAM 16
163
  threshold_missing_day <- list_param_run_assessment_plotting$threshold_missing_day #PARM17
164
  year_predicted <- list_param_run_assessment_plotting$year_predicted
165
 
166
  NA_value <- NA_flag_val 
167
  metric_name <- "rmse" #to be added to the code later...
168
  
169
  ##################### START SCRIPT #################
170
  
171
  ####### PART 1: Read in data ########
172

  
173
  if(create_out_dir_param==TRUE){
174
    out_dir <- create_dir_fun(out_dir,out_suffix)
175
    setwd(out_dir)
176
  }else{
177
    setwd(out_dir) #use previoulsy defined directory
178
  }
179

  
180
  setwd(out_dir)
181
  
182
  list_outfiles <- vector("list", length=25) #collect names of output files
183
  list_outfiles_names <- vector("list", length=25) #collect names of output files
184
  counter_fig <- 0 #index of figure to collect outputs
185
  
186
  #i <- year_predicted
187
  ###Table 1: Average accuracy metrics
188
  ###Table 2: daily accuracy metrics for all tiles
189

  
190
  df_assessment_files <- read.table(df_assessment_files_name,stringsAsFactors=F,sep=",")
191
  #df_assessment_files, note that in_dir indicate the path of the textfiles
192
  summary_metrics_v <- read.table(file.path(in_dir,basename(df_assessment_files$files[1])),sep=",")
193
  tb <- read.table(file.path(in_dir, basename(df_assessment_files$files[2])),sep=",")
194
  tb_s <- read.table(file.path(in_dir, basename(df_assessment_files$files[4])),sep=",")
195
  
196
  tb_month_s <- read.table(file.path(in_dir,basename(df_assessment_files$files[3])),sep=",")
197
  pred_data_month_info <- read.table(file.path(in_dir, basename(df_assessment_files$files[10])),sep=",")
198
  pred_data_day_info <- read.table(file.path(in_dir, basename(df_assessment_files$files[11])),sep=",")
199
  df_tile_processed <- read.table(file.path(in_dir, basename(df_assessment_files$files[12])),sep=",")
200
  
201
  #add column for tile size later on!!!
202
  
203
  tb$pred_mod <- as.character(tb$pred_mod)
204
  summary_metrics_v$pred_mod <- as.character(summary_metrics_v$pred_mod)
205
  summary_metrics_v$tile_id <- as.character(summary_metrics_v$tile_id)
206
  df_tile_processed$tile_id <- as.character(df_tile_processed$tile_id)
207
  
208
  tb_month_s$pred_mod <- as.character(tb_month_s$pred_mod)
209
  tb_month_s$tile_id<- as.character(tb_month_s$tile_id)
210
  tb_s$pred_mod <- as.character(tb_s$pred_mod)
211
  tb_s$tile_id <- as.character(tb_s$tile_id)
212
  
213
  #multiple regions? #this needs to be included in the previous script!!!
214
  #if(multiple_region==TRUE){
215
  df_tile_processed$reg <- as.character(df_tile_processed$reg)
216
  tb <- merge(tb,df_tile_processed,by="tile_id")
217
  tb_s <- merge(tb_s,df_tile_processed,by="tile_id")
218
  tb_month_s<- merge(tb_month_s,df_tile_processed,by="tile_id")
219
  summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id")
220
  #test <- merge(summary_metrics_v,df_tile_processed,by="tile_id",all=F)
221
  #duplicate columns...need to be cleaned up later
222
  try(tb$year_predicted <- tb$year_predicted.x)
223
  try(tb$reg <- tb$reg.x)
224
  try(summary_metrics_v$year_predicted <- summary_metrics_v$year_predicted.x)
225
  try(summary_metrics_v$reg <- summary_metrics_v$reg.x)  
226
  try(summary_metrics_v$lat <- summary_metrics_v$lat.x)
227
  try(summary_metrics_v$lon <- summary_metrics_v$lon.x)
228
  #tb_all <- tb
229
  #summary_metrics_v_all <- summary_metrics_v 
230
  
231
  #table(summary_metrics_v_all$reg)
232
  #table(summary_metrics_v$reg)
233
  #table(tb_all$reg)
234
  #table(tb$reg)
235
  
236
  ############ PART 2: PRODUCE FIGURES ################
237
  
238
  ###########################
239
  ### Figure 1: plot location of the study area with tiles processed
240
  
241
  #df_tiled_processed <- na.omit(df_tile_processed) #remove other list of folders irrelevant
242
  #list_shp_reg_files <- df_tiled_processed$shp_files
243
  list_shp_reg_files<- as.character(df_tile_processed$shp_files)
244
  #list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir,
245
  #          as.character(df_tile_processed$tile_coord),"shapefiles",basename(list_shp_reg_files))
246
  #list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir,
247
                                  #"shapefiles",basename(list_shp_reg_files))
248
  
249
  ### Potential function starts here:
250
  #function(in_dir,out_dir,list_shp_reg_files,title_str,region_name,num_cores,out_suffix,out_suffix)
251
  
252
  ### First get background map to display where study area is located
253
  #can make this more general later on..should have this already in a local directory on Atlas or NEX!!!!
254
  
255
  #http://www.diva-gis.org/Data
256
  #countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp"
257
  path_to_shp <- dirname(countries_shp)
258
  layer_name <- sub(".shp","",basename(countries_shp))
259
  reg_layer <- readOGR(path_to_shp, layer_name)
260
  #proj4string(reg_layer) <- CRS_locs_WGS84
261
  #reg_shp<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]])))
262
  
263
  centroids_pts <- vector("list",length(list_shp_reg_files))
264
  shps_tiles <- vector("list",length(list_shp_reg_files))
265
  #collect info: read in all shapfiles
266
  #This is slow...make a function and use mclapply??
267
  #/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles
268
  
269
  for(i in 1:length(list_shp_reg_files)){
270
    #path_to_shp <- dirname(list_shp_reg_files[[i]])
271
    path_to_shp <- file.path(out_dir,"/shapefiles")
272
    layer_name <- sub(".shp","",basename(list_shp_reg_files[[i]]))
273
    shp1 <- try(readOGR(path_to_shp, layer_name)) #use try to resolve error below
274
    #shp_61.0_-160.0
275
    #Geographical CRS given to non-conformant data: -186.331747678
276
    
277
    #shp1<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]])))
278
    if (!inherits(shp1,"try-error")) {
279
      pt <- gCentroid(shp1)
280
      centroids_pts[[i]] <- pt
281
    }else{
282
      pt <- shp1
283
      centroids_pts[[i]] <- pt
284
    }
285
    shps_tiles[[i]] <- shp1
286
    #centroids_pts[[i]] <- centroids
287
  }
288
  
289
  #fun <- function(i,list_shp_files)
290
  #coord_names <- c("lon","lat")
291
  #l_ras#t <- rasterize_df_fun(test,coord_names,proj_str,out_suffix=out_suffix,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005)
292
  
293
  #remove try-error polygons...we loose three tiles because they extend beyond -180 deg
294
  tmp <- shps_tiles
295
  shps_tiles <- remove_errors_list(shps_tiles) #[[!inherits(shps_tiles,"try-error")]]
296
  #shps_tiles <- tmp
297
  length(tmp)-length(shps_tiles) #number of tiles with error message
298
  
299
  tmp_pts <- centroids_pts 
300
  centroids_pts <- remove_errors_list(centroids_pts) #[[!inherits(shps_tiles,"try-error")]]
301
  #centroids_pts <- tmp_pts 
302
  
303
  #plot info: with labels
304
  res_pix <-1200
305
  col_mfrow <- 1 
306
  row_mfrow <- 1
307
  
308
  png(filename=paste("Figure1_tile_processed_region_",region_name,"_",out_suffix,".png",sep=""),
309
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
310
  
311
  plot(reg_layer)
312
  #Add polygon tiles...
313
  for(i in 1:length(shps_tiles)){
314
    shp1 <- shps_tiles[[i]]
315
    pt <- centroids_pts[[i]]
316
    if(!inherits(shp1,"try-error")){
317
      plot(shp1,add=T,border="blue",usePolypath = FALSE) #added usePolypath following error on brige and NEX
318
      #plot(pt,add=T,cex=2,pch=5)
319
      label_id <- df_tile_processed$tile_id[i]
320
      text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1.3,font=2,col=c("red"),family="HersheySerif")
321
    }
322
  }
323
  #title(paste("Tiles ", tile_size,region_name,sep=""))
324
  #plot(shp1,add=T,border="blue",usePolypath = FALSE) #,add=T,
325
  #plot(pt,add=T,cex=2,pch=5)
326
  #label_id <- df_tile_processed$tile_id[i]
327
  #text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1.3,font=2,col=c("red"),family="HersheySerif")
328
  dev.off()
329
  
330
  #unique(summaty_metrics$tile_id)
331
  #text(lat-shp,)
332
  #union(list_shp_reg_files[[1]],list_shp_reg_files[[2]])
333
  #Row used in constructing output table...
334

  
335
  list_outfiles[[counter_fig+1]] <- paste("Figure1_tile_processed_region_",region_name,"_",out_suffix,".png",sep="")
336
  counter_fig <- counter_fig+1
337
  #this will be changed to be added to data.frame on the fly
338
  r1 <-c("figure_1","Tiles processed for the region",NA,NA,region_name,year_predicted,list_outfiles[[1]]) 
339
  
340
  ###############
341
  ### Figure 2: boxplot of average accuracy by model and by tiles
342
  
343
  tb$tile_id <- factor(tb$tile_id, levels=unique(tb$tile_id))
344
  model_name <- as.character(unique(tb$pred_mod))
345
  
346
  ## Figure 2a
347
  for(i in  1:length(model_name)){
348
    
349
    res_pix <- 480
350
    col_mfrow <- 1
351
    row_mfrow <- 1
352
    fig_filename <-  paste("Figure2a_boxplot_with_oultiers_by_tiles_",model_name[i],"_",out_suffix,".png",sep="")
353
    png(filename=paste("Figure2a_boxplot_with_oultiers_by_tiles_",model_name[i],"_",out_suffix,".png",sep=""),
354
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
355
    
356
    boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod==model_name[i]))
357
    title(paste("RMSE per ",model_name[i]))
358
    
359
    dev.off()
360
    list_outfiles[[counter_fig+i]] <- fig_filename
361
  }
362
  counter_fig <- counter_fig + length(model_name)
363
  
364
  r2 <-c("figure_2a","Boxplot of accuracy with outliers by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[2]]) 
365
  r3 <-c("figure_2a","boxplot of accuracy with outliers by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[3]])
366
  
367
  ## Figure 2b
368
  #with ylim and removing trailing...
369
  for(i in  1:length(model_name)){ #there are two models!!
370
    
371
    res_pix <- 480
372
    col_mfrow <- 1
373
    row_mfrow <- 1
374
    fig_filename <- paste("Figure2b_boxplot_scaling_by_tiles","_",model_name[i],"_",out_suffix,".png",sep="")
375
    png(filename=paste("Figure2b_boxplot_scaling_by_tiles","_",model_name[i],"_",out_suffix,".png",sep=""),
376
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
377
    
378
    model_name <- unique(tb$pred_mod)
379
    boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod==model_name[i])
380
            ,ylim=c(0,4),outline=FALSE)
381
    title(paste("RMSE per ",model_name[i]))
382
    dev.off()
383
    #we already stored one figure
384
    list_outfiles[[counter_fig+i]] <- fig_filename
385
  }
386
  counter_fig <- counter_fig + length(model_name)
387
  #bwplot(rmse~tile_id, data=subset(tb,tb$pred_mod=="mod1"))
388
  r4 <-c("figure_2b","Boxplot of accuracy with scaling by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[4]])  
389
  r5 <-c("figure_2b","Boxplot of accuracy with scaling by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[5]])  
390

  
391
  ###############
392
  ### Figure 3: boxplot of average RMSE by model acrosss all tiles
393
  
394
  #Ok fixed..now selection of model but should also offer an option for using both models!! so make this a function!!
395
  for(i in  1:length(model_name)){ #there are two models!!
396
    ## Figure 3a
397
    res_pix <- 480
398
    col_mfrow <- 1
399
    row_mfrow <- 1
400
    
401
    png(filename=paste("Figure3a_boxplot_overall_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep=""),
402
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
403
    
404
    #boxplot(rmse~pred_mod,data=tb)#,names=tb$pred_mod)
405
    boxplot(rmse~pred_mod,data=subset(tb,tb$pred_mod==model_name[i]))#,names=tb$pred_mod)
406
    title(paste("RMSE with outliers for all tiles: ",model_name[i],sep=""))
407
    dev.off()
408
    list_outfiles[[counter_fig+1]] <- paste("Figure3a_boxplot_overall_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep="")
409
    
410
    ## Figure 3b
411
    png(filename=paste("Figure3b_boxplot_overall_region_scaling_",model_name[i],"_",out_suffix,".png",sep=""),
412
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
413
    #boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
414
    boxplot(rmse~pred_mod,data=subset(tb,tb$pred_mod==model_name[i]),ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
415
    #title("RMSE per model over all tiles")
416
    title(paste("RMSE with scaling for all tiles: ",model_name[i],sep=""))
417
    dev.off()
418
    list_outfiles[[counter_fig+2]] <- paste("Figure3b_boxplot_overall_region_scaling_",model_name[i],"_",out_suffix,".png",sep="")
419
  }
420
  counter_fig <- counter_fig + length(model_name)
421
  r6 <-c("figure_3a","Boxplot overall accuracy with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[6]])  
422
  r7 <-c("figure_3b","Boxplot overall accuracy with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[7]])  
423
  r8 <-c("figure_3a","Boxplot overall accuracy with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[8]])
424
  r9 <-c("figure_3b","Boxplot overall accuracy with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
425

  
426
  ################ 
427
  ### Figure 4: plot predicted tiff for specific date per model
428
  
429
  #y_var_name <-"dailyTmax"
430
  #index <-244 #index corresponding to Sept 1
431
  
432
  # if (mosaic_plot==TRUE){
433
  #   index  <- 1 #index corresponding to Jan 1
434
  #   date_selected <- "20100901"
435
  #   name_method_var <- paste(interpolation_method,"_",y_var_name,"_",sep="")
436
  # 
437
  #   pattern_str <- paste("mosaiced","_",name_method_var,"predicted",".*.",date_selected,".*.tif",sep="")
438
  #   lf_pred_list <- list.files(pattern=pattern_str)
439
  # 
440
  #   for(i in 1:length(lf_pred_list)){
441
  #     
442
  #   
443
  #     r_pred <- raster(lf_pred_list[i])
444
  #   
445
  #     res_pix <- 480
446
  #     col_mfrow <- 1
447
  #     row_mfrow <- 1
448
  #   
449
  #     png(filename=paste("Figure4_models_predicted_surfaces_",model_name[i],"_",name_method_var,"_",data_selected,"_",out_suffix,".png",sep=""),
450
  #        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
451
  #   
452
  #     plot(r_pred)
453
  #     title(paste("Mosaiced",model_name[i],name_method_var,date_selected,sep=" "))
454
  #     dev.off()
455
  #   }
456
  #   #Plot Delta and clim...
457
  # 
458
  #    ## plotting of delta and clim for later scripts...
459
  # 
460
  # }
461
  
462
  
463
  ######################
464
  ### Figure 5: plot accuracy ranked 
465
  
466
  #Turn summary table to a point shp
467
  
468
  list_df_ac_mod <- vector("list",length=length(model_name))
469
  for (i in 1:length(model_name)){
470
    
471
    ac_mod <- summary_metrics_v[summary_metrics_v$pred_mod==model_name[i],]
472
    ### Ranking by tile...
473
    df_ac_mod <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("pred_mod","rmse","mae","tile_id")]
474
    list_df_ac_mod[[i]] <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("rmse","mae","tile_id")]
475
    
476
    res_pix <- 480
477
    col_mfrow <- 1
478
    row_mfrow <- 1
479
    fig_filename <- paste("Figure5_ac_metrics_ranked_",model_name[i],"_",out_suffix,".png",sep="")
480

  
481
    png(filename=paste("Figure5_ac_metrics_ranked_",model_name[i],"_",out_suffix,".png",sep=""),
482
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
483
    x<- as.character(df_ac_mod$tile_id)
484
    barplot(df_ac_mod$rmse, names.arg=x)
485
    #plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T)
486
    #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
487
    title(paste("RMSE ranked by tile for ",model_name[i],sep=""))
488
    
489
    dev.off()
490
    list_outfiles[[counter_fig+i]] <- fig_filename
491
  }
492
  
493
  counter_fig <- counter_fig + length(model_name)
494
  
495
  r10 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
496
  r11 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
497

  
498
  ######################
499
  ### Figure 6: plot map of average RMSE per tile at centroids
500
  
501
  ### Without 
502
  
503
  #list_df_ac_mod <- vector("list",length=length(lf_pred_list))
504
  list_df_ac_mod <- vector("list",length=length(model_name))
505
  
506
  for (i in 1:length(model_name)){
507
    
508
    ac_mod <- summary_metrics_v[summary_metrics_v$pred_mod==model_name[i],]
509
    #r_pred <- raster(lf_list[i])
510
    
511
    res_pix <- 1200
512
    #res_pix <- 480
513
    
514
    col_mfrow <- 1
515
    row_mfrow <- 1
516
    fig_filename <- paste("Figure6_ac_metrics_map_centroids_tile_",model_name[i],"_",out_suffix,".png",sep="")
517
    png(filename=paste("Figure6_ac_metrics_map_centroids_tile_",model_name[i],"_",out_suffix,".png",sep=""),
518
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
519
    
520
    coordinates(ac_mod) <- ac_mod[,c("lon","lat")] 
521
    #coordinates(ac_mod) <- ac_mod[,c("lon.x","lat.x")] #solve this later
522
    p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
523
    #title("(a) Mean for 1 January")
524
    p <- bubble(ac_mod,"rmse",main=paste("Average RMSE per tile and by ",model_name[i]))
525
    p1 <- p+p_shp
526
    print(p1)
527
    #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
528
    #title(paste("Averrage RMSE per tile and by ",model_name[i]))
529
    
530
    dev.off()
531
    
532
    ### Ranking by tile...
533
    #df_ac_mod <- 
534
    list_df_ac_mod[[i]] <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("rmse","mae","tile_id")]
535
    list_outfiles[[counter_fig+i]] <- fig_filename
536
  }
537
  counter_fig <- counter_fig+length(model_name)
538

  
539
  r12 <-c("figure_6","Average accuracy metrics map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
540
  r13 <-c("figure_6","Average accuracy metrics map at centroids","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
541

  
542
  ######################
543
  ### Figure 7: Number of predictions: daily and monthly
544
  
545
  ## Figure 7a
546
 
547
  ## Number of tiles with information:
548
  sum(df_tile_processed$metrics_v) #26,number of tiles with raster object
549
  length(df_tile_processed$metrics_v) #26,number of tiles in the region
550
  sum(df_tile_processed$metrics_v)/length(df_tile_processed$metrics_v) #80 of tiles with info
551
  
552
  #coordinates
553
  try(coordinates(summary_metrics_v) <- c("lon","lat"))
554
  #try(coordinates(summary_metrics_v) <- c("lon.y","lat.y"))
555
  
556
  #threshold_missing_day <- c(367,365,300,200)
557
  
558
  nb<-nrow(subset(summary_metrics_v,model_name=="mod1"))  
559
  sum(subset(summary_metrics_v,model_name=="mod1")$n_missing)/nb #33/35
560
  
561
  ## Make this a figure...
562
  
563
  #plot(summary_metrics_v)
564
  #Make this a function later so that we can explore many thresholds...
565
  #Problem here
566
  #Browse[3]> c
567
   #Error in grid.Call.graphics(L_setviewport, pvp, TRUE) : 
568
  #non-finite location and/or size for viewport
569

  
570
  j<-1 #for model name 1,mod1
571
  for(i in 1:length(threshold_missing_day)){
572
    
573
    #summary_metrics_v$n_missing <- summary_metrics_v$n == 365
574
    #summary_metrics_v$n_missing <- summary_metrics_v$n < 365
575
    summary_metrics_v$n_missing <- as.numeric(summary_metrics_v$n < threshold_missing_day[i])
576
    summary_metrics_v_subset <- subset(summary_metrics_v,model_name=="mod1")
577
    
578
    fig_filename <- paste("Figure7a_ac_metrics_map_centroids_tile_",model_name[j],"_","missing_day_",
579
                          threshold_missing_day[i],
580
                       "_",out_suffix,".png",sep="")
581

  
582
    if(sum(summary_metrics_v_subset$n_missing) > 0){#then there are centroids to plot!!!
583
      
584
      #res_pix <- 1200
585
      res_pix <- 960
586
      col_mfrow <- 1
587
      row_mfrow <- 1
588
      #only mod1 right now
589
      png(filename=paste("Figure7a_ac_metrics_map_centroids_tile_",model_name[j],"_","missing_day_",
590
                         threshold_missing_day[i],
591
                       "_",out_suffix,".png",sep=""),
592
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
593
    
594
      model_name[j]
595
    
596
      p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
597
      #title("(a) Mean for 1 January")
598
      p <- bubble(summary_metrics_v_subset,"n_missing",main=paste("Missing per tile and by ",model_name[j]," for ",
599
                                                                threshold_missing_day[i]))
600
      p1 <- p+p_shp
601
      try(print(p1)) #error raised if number of missing values below a threshold does not exist
602
      dev.off()
603

  
604
    } 
605
     
606
    list_outfiles[[counter_fig+i]] <- fig_filename
607
  }
608
  counter_fig <- counter_fig+length(threshold_missing_day) #currently 4 days...
609

  
610
  r14 <-c("figure_7","Number of missing days threshold1 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
611
  r15 <-c("figure_7","Number of missing days threshold2 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
612
  r16 <-c("figure_7","Number of missing days threshold3 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[8]])
613
  r17 <-c("figure_7","Number of missing days threshold4 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
614

  
615
  ### Potential
616
  png(filename=paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
617
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
618
  
619
  xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
620
                                          pred_mod!="mod_kr"),type="h")
621
  dev.off()
622
  
623
  list_outfiles[[counter_fig+1]] <- paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep="")
624
  counter_fig <- counter_fig + 1
625
  r18 <-c("figure_7b","Number of daily predictions per_models","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
626
  
627
  table(tb$pred_mod)
628
  table(tb$index_d)
629
  #table(subset(tb,pred_mod!="mod_kr"))
630
  table(subset(tb,pred_mod=="mod1")$index_d)
631
  #aggregate()
632
  tb$predicted <- 1
633
  test <- aggregate(predicted~pred_mod+tile_id,data=tb,sum)
634
  #xyplot(predicted~pred_mod | tile_id,data=subset(as.data.frame(test),
635
  #                                                pred_mod!="mod_kr"),type="h")
636
  
637
  as.character(unique(test$tile_id)) #141 tiles
638
  
639
  dim(subset(test,test$predicted==365 & test$pred_mod=="mod1"))
640
  #histogram(subset(test, test$pred_mod=="mod1")$predicted)
641
  unique(subset(test, test$pred_mod=="mod1")$predicted)
642
  table((subset(test, test$pred_mod=="mod1")$predicted))
643
  
644
  #LST_avgm_min <- aggregate(LST~month,data=data_month_all,min)
645
  png(filename=paste("Figure7c_histogram_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
646
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
647

  
648
  histogram(test$predicted~test$tile_id)
649
  dev.off()
650
  
651
  list_outfiles[[counter_fig+1]] <- paste("Figure7c_histogram_number_daily_predictions_per_models","_",out_suffix,".png",sep="")
652
  counter_fig <- counter_fig + 1
653
  r19 <-c("figure_7c","Histogram number daily predictions per models","mod1",metric_name,region_name,year_predicted,list_outfiles[[9]])  
654

  
655
  
656
  #table(tb)
657
  ## Figure 7b
658
  #png(filename=paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
659
  #    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
660
  
661
  #xyplot(n~month | tile_id + pred_mod,data=subset(as.data.frame(tb_month_s),
662
  #                                           pred_mod!="mod_kr"),type="h")
663
  #xyplot(n~month | tile_id,data=subset(as.data.frame(tb_month_s),
664
  #                                           pred_mod="mod_1"),type="h")
665
  #test=subset(as.data.frame(tb_month_s),pred_mod="mod_1")
666
  #table(tb_month_s$month)
667
  #dev.off()
668
  #
669
  
670
  ##########################################################
671
  ##### Figure 8: Breaking down accuracy by regions!! #####
672
  
673
  #summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id")
674
  
675
  ##################
676
  ##First plot with all models together
677
  
678
  ## Figure 8a
679
  res_pix <- 480
680
  col_mfrow <- 1
681
  row_mfrow <- 1
682
  
683
  png(filename=paste("Figure8a_boxplot_overall_separated_by_region_with_oultiers_",out_suffix,".png",sep=""),
684
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
685
  
686
  p<- bwplot(rmse~pred_mod | reg, data=tb,
687
             main="RMSE per model and region over all tiles with outliers")
688
  print(p)
689
  dev.off()
690
  
691
  list_outfiles[[counter_fig+1]] <- paste("Figure8a_boxplot_overall_accuracy_by_model_separated_by_region_with_oultiers_",out_suffix,".png",sep="")
692
  counter_fig <- counter_fig + 1
693
  
694
  ## Figure 8b
695
  png(filename=paste("Figure8b_boxplot_overall_separated_by_region_scaling_",out_suffix,".png",sep=""),
696
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
697
  
698
  #boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
699
  #title("RMSE per model over all tiles")
700
  p<- bwplot(rmse~pred_mod | reg, data=tb,ylim=c(0,5),
701
             main="RMSE per model and region over all tiles with scaling")
702
  print(p)
703
  dev.off()
704
  
705
  list_outfiles[[counter_fig+1]] <- paste("Figure8b_boxplot_overall_accuracy_by_model_separated_by_region_scaling_",out_suffix,".png",sep="")
706
  counter_fig <- counter_fig + 1
707
  
708
  
709
  r20 <-c("figure 8a","Boxplot overall accuracy by model separated by region with outliers",NA,metric_name,region_name,year_predicted,list_outfiles[[20]])  
710
  r21 <-c("figure 8b","Boxplot overall accuracy by model separated by region with scaling",NA,metric_name,region_name,year_predicted,list_outfiles[[21]])  
711

  
712
  #######
713
  ##Second, plot for each model separately
714
  
715
  for(i in 1:length(model_name)){
716
    
717
    tb_subset <- subset(tb,pred_mod==model_name[i])#mod1 is i=1, mod_kr is last
718
    ## Figure 8c
719
  
720
    res_pix <- 480
721
    col_mfrow <- 1
722
    row_mfrow <- 1
723
  
724
    fig_filename <- paste("Figure8c_boxplot_overall_accuracy_separated_by_region_with_outliers_",model_name[i],"_",out_suffix,".png",sep="")
725
    png(filename=fig_filename,
726
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
727
  
728
    p<- bwplot(rmse~pred_mod | reg, data=tb_subset,
729
             main="RMSE per model and region over all tiles with outliers")
730
    print(p)
731
    dev.off()
732
  
733
    list_outfiles[[counter_fig+1]] <- fig_filename
734
    counter_fig <- counter_fig + 1
735
  
736
    ## Figure 8d
737
    fig_filename <- paste("Figure8d_boxplot_overall_accuracy_separated_by_region_scaling_",model_name[i],"_",out_suffix,".png",sep="")
738
    png(filename=fig_filename,
739
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
740
  
741
    #boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
742
    #title("RMSE per model over all tiles")
743
    p<- bwplot(rmse~pred_mod | reg, data=tb_subset,ylim=c(0,5),
744
             main="RMSE per model and region over all tiles with scaling")
745
    print(p)
746
    dev.off()
747
  
748
    list_outfiles[[counter_fig+1]] <- fig_filename
749
    counter_fig <- counter_fig + 1
750

  
751
  }
752
  
753
  r22 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[20]])  
754
  r23 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[21]])  
755
  r24 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[20]])  
756
  r25 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[21]])  
757

  
758
  #####################################################
759
  #### Figure 9: plotting boxplot by year and regions ###########
760
  
761
#   ## Figure 9a
762
#   res_pix <- 480
763
#   col_mfrow <- 1
764
#   row_mfrow <- 1
765
#   
766
#   png(filename=paste("Figure9a_boxplot_overall_separated_by_region_year_with_oultiers_",model_name[i],"_",out_suffix,".png",sep=""),
767
#       width=col_mfrow*res_pix,height=row_mfrow*res_pix)
768
#   
769
#   p<- bwplot(rmse~pred_mod | reg + year_predicted, data=tb,
770
#              main="RMSE per model and region over all tiles")
771
#   print(p)
772
#   dev.off()
773
#   
774
#   ## Figure 9b
775
#   png(filename=paste("Figure8b_boxplot_overall_separated_by_region_year_scaling_",model_name[i],"_",out_suffix,".png",sep=""),
776
#       width=col_mfrow*res_pix,height=row_mfrow*res_pix)
777
#   
778
#   boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
779
#   title("RMSE per model over all tiles")
780
#   p<- bwplot(rmse~pred_mod | reg + year_predicted, data=tb,ylim=c(0,5),
781
#              main="RMSE per model and region over all tiles")
782
#   print(p)
783
#   dev.off()
784
# 
785
#   list_outfiles[[counter_fig+1]] <- paste("Figure9a_boxplot_overall_separated_by_region_year_with_oultiers_",model_name[i],"_",out_suffix,".png",sep="")
786
#   counter_fig <- counter_fig + 1
787

  
788
  ##############################################################
789
  ############## Prepare object to return
790
  ############## Collect information from assessment ##########
791
  
792
  # This is hard coded and can be improved later on for flexibility. It works for now...                                                                 
793
  #This data.frame contains all the files from the assessment
794

  
795
  #Should have this at the location of the figures!!! will be done later?    
796
  #r1 <-c("figure_1","Tiles processed for the region",NA,NA,region_name,year_predicted,list_outfiles[[1]])
797
  #r2 <-c("figure_2a","Boxplot of accuracy with outliers by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[2]]) 
798
  #r3 <-c("figure_2a","boxplot of accuracy with outliers by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[3]])
799
  #r4 <-c("figure_2b","Boxplot of accuracy with scaling by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[4]])  
800
  #r5 <-c("figure_2b","Boxplot of accuracy with scaling by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[5]])  
801
  #r6 <-c("figure_3a","Boxplot overall accuracy with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[6]])  
802
  #r7 <-c("figure_3b","Boxplot overall accuracy with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[7]])  
803
  #r8 <-c("figure_3a","Boxplot overall accuracy with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[8]])
804
  #r9 <-c("figure_3b","Boxplot overall accuracy with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[9]])  
805
  #r10 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod1",metric_name,region_name,year_predicted,list_outfiles[[10]])
806
  #r11 <-c("figure_5","Barplot of accuracy metrics ranked by tiles","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[11]])  
807
  #r12 <-c("figure_6","Average accuracy metrics map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[12]])
808
  #r13 <-c("figure_6","Average accuracy metrics map at centroids","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[13]])  
809
  #r14 <-c("figure_7","Number of missing days threshold1 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[14]])
810
  #r15 <-c("figure_7","Number of missing days threshold2 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[15]])  
811
  #r16 <-c("figure_7","Number of missing days threshold3 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[16]])
812
  #r17 <-c("figure_7","Number of missing days threshold4 map at centroids","mod1",metric_name,region_name,year_predicted,list_outfiles[[17]])  
813
  #r18 <-c("figure_7b","Number of daily predictions per_models","mod1",metric_name,region_name,year_predicted,list_outfiles[[18]])  
814
  #r19 <-c("figure_7c","Histogram number daily predictions per models","mod1",metric_name,region_name,year_predicted,list_outfiles[[19]])  
815
  #r20 <-c("figure 8a","Boxplot overall accuracy by model separated by region with outliers",NA,metric_name,region_name,year_predicted,list_outfiles[[20]])  
816
  #r21 <-c("figure 8b","Boxplot overall accuracy by model separated by region with scaling",NA,metric_name,region_name,year_predicted,list_outfiles[[21]])  
817
  #r22 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod1",metric_name,region_name,year_predicted,list_outfiles[[22]])  
818
  #r23 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod1",metric_name,region_name,year_predicted,list_outfiles[[23]])  
819
  #r24 <-c("figure 8c","Boxplot overall accuracy separated by region with outliers","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[24]])  
820
  #r25 <-c("figure 8d","Boxplot overall accuracy separated by region with scaling","mod_kr",metric_name,region_name,year_predicted,list_outfiles[[25]])  
821

  
822
  #Assemble all the figures description and information in a data.frame for later use
823
  list_rows <-list(r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21,r22,r23,r24,r25)
824
  df_assessment_figures_files <- as.data.frame(do.call(rbind,list_rows))
825
  names(df_assessment_figures_files) <- c("figure_no","comment","model_name","reg","metric_name","year_predicted","filename")
826
  
827
  ###Prepare files for copying back?
828
  df_assessment_figures_files_names <- file.path(out_dir,paste("df_assessment_figures_files_",region_name,"_",year_predicted,"_",out_suffix,".txt",sep=""))
829
  write.table(df_assessment_figures_files,
830
              file=df_assessment_figures_files_names ,sep=",")
831

  
832
  #df_assessment_figures_files_names
833
  
834
  ######################################################
835
  ##### Prepare objet to return ####
836

  
837
  assessment_obj <- list(df_assessment_files, df_assessment_figures_files)
838
  names(assessment_obj) <- c("df_assessment_files", "df_assessment_figures_files")
839
  ## Prepare list of files to return...
840
  return(assessment_obj)
841
 
842
}
843
  
844
##################### END OF SCRIPT ######################
845

  
846
#### Run on the bridge:
847

  
848
#args<-commandArgs(TRUE)
849
#script_path<-"/nobackupp6/aguzman4/climateLayers/finalCode/environmental-layers/climate/research/oregon/interpolation"
850
#dataHome<-"/nobackupp6/aguzman4/climateLayers/interp/testdata/"
851
#script_path2<-"/nobackupp6/aguzman4/climateLayers/finalCode/environmental-layers/climate/research/world/interpolation"
852

  
853
#CALLED FROM MASTER SCRIPT:
854

  
855
script_path <- "/nobackupp8/bparmen1/env_layers_scripts" #path to script
856
function_assessment_part1_functions <- "global_run_scalingup_assessment_part1_functions_02112015.R" #PARAM12
857
function_assessment_part1a <-"global_run_scalingup_assessment_part1a_01042016.R"
858
function_assessment_part2 <- "global_run_scalingup_assessment_part2_02032016.R"
859
function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R"
860
source(file.path(script_path,function_assessment_part1_functions)) #source all functions used in this script 
861
source(file.path(script_path,function_assessment_part1a)) #source all functions used in this script 
862
source(file.path(script_path,function_assessment_part2)) #source all functions used in this script 
863
source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script 
864

  
865
### Parameters and arguments ###
866
  
867
var<-"TMAX" # variable being interpolated
868
if (var == "TMAX") {
869
  y_var_name <- "dailyTmax"
870
  y_var_month <- "TMax"
871
}
872
if (var == "TMIN") {
873
  y_var_name <- "dailyTmin"
874
  y_var_month <- "TMin"
875
}
876

  
877
#interpolation_method<-c("gam_fusion") #other otpions to be added later
878
interpolation_method<-c("gam_CAI")
879
CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
880
#CRS_interp <-"+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";
881
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
882

  
883
out_region_name<-""
884
list_models<-c("y_var ~ s(lat,lon,k=5) + s(elev_s,k=3) + s(LST,k=3)")
885

  
886
#reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia)
887
#master directory containing the definition of tile size and tiles predicted
888
in_dir1 <- "/nobackupp6/aguzman4/climateLayers/out/"
889
#/nobackupp6/aguzman4/climateLayers/out_15x45/1982
890

  
891
#region_names <- c("reg23","reg4") #selected region names, #PARAM2
892
region_name <- c("reg4") #run assessment by region, this is a unique region only
893
#region_names <- c("reg1","reg2","reg3","reg4","reg5","reg6") #selected region names, #PARAM2
894
interpolation_method <- c("gam_CAI") #PARAM4
895
out_prefix <- "run_global_analyses_pred_12282015" #PARAM5
896
#out_dir <- "/nobackupp8/bparmen1/" #PARAM6
897
out_dir <- "/nobackupp8/bparmen1/output_run_global_analyses_pred_12282015"
898
#out_dir <-paste(out_dir,"_",out_prefix,sep="")
899
create_out_dir_param <- FALSE #PARAM7
900

  
901
#CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
902
#CRS_interp <-"+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";
903
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
904

  
905
#list_year_predicted <- 1984:2004
906
list_year_predicted <- c("2014")
907
#year_predicted <- list_year_predicted[1]
908

  
909
file_format <- ".tif" #format for mosaiced files #PARAM10
910
NA_flag_val <- -9999  #No data value, #PARAM11
911
num_cores <- 6 #number of cores used #PARAM13
912
plotting_figures <- TRUE #running part2 of assessment to generate figures...
913
  
914
##Additional parameters used in part 2, some these may be removed as code is simplified
915
mosaic_plot <- FALSE #PARAM14
916
day_to_mosaic <- c("19920102","19920103","19920103") #PARAM15
917
multiple_region <- TRUE #PARAM16
918
countries_shp <- "/nobackupp8/bparmen1/NEX_data/countries.shp" #PARAM17
919
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #Atlas
920
plot_region <- TRUE  #PARAM18
921
threshold_missing_day <- c(367,365,300,200)#PARAM19
922

  
923
year_predicted <- list_year_predicted[1]
924
in_dir <- out_dir #PARAM 0
925
#y_var_name <- "dailyTmax" #PARAM1 , already set
926
#interpolation_method <- c("gam_CAI") #PARAM2, already set
927
out_suffix <- out_prefix #PARAM3
928
#out_dir <-  #PARAM4, already set
929
create_out_dir_param <- FALSE #PARAM 5, already created and set
930
#mosaic_plot <- FALSE #PARAM6
931
#if daily mosaics NULL then mosaicas all days of the year
932
#day_to_mosaic <- c("19920101","19920102","19920103") #PARAM7
933
#CRS_locs_WGS84 already set
934
proj_str <- CRS_locs_WGS84 #PARAM 8 #check this parameter
935
#file_format <- ".rst" #PARAM 9, already set
936
#NA_flag_val <- -9999 #PARAM 11, already set
937
#multiple_region <- TRUE #PARAM 12
938
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too
939
#plot_region <- TRUE
940
#num_cores <- 6 #PARAM 14, already set
941
#region_name <- c("reg4") #reference region to merge if necessary, if world all the regions are together #PARAM 16
942
#use previous files produced in step 1a and stored in a data.frame
943
df_assessment_files_name <- file.path("/nobackupp8/bparmen1/output_run_global_analyses_pred_12282015","df_assessment_files_reg4_2014_run_global_analyses_pred_12282015.txt")# #PARAM 17, set in the script
944
df_assessment_files <- read.table(df_assessment_files_name,stringsAsFactors=F,sep=",")
945
#threshold_missing_day <- c(367,365,300,200) #PARM18
946

  
947
list_param_run_assessment_plotting <-list(
948
    in_dir,y_var_name, interpolation_method, out_suffix,
949
    out_dir, create_out_dir_param, mosaic_plot, proj_str, file_format, NA_flag_val,
950
    multiple_region, countries_shp, plot_region, num_cores,
951
    region_name, df_assessment_files_name, threshold_missing_day,year_predicted
952
  )
953

  
954
names(list_param_run_assessment_plotting) <- c(
955
    "in_dir","y_var_name","interpolation_method","out_suffix",
956
    "out_dir","create_out_dir_param","mosaic_plot","proj_str","file_format","NA_flag_val",
957
    "multiple_region","countries_shp","plot_region","num_cores",
958
    "region_name","df_assessment_files_name","threshold_missing_day","year_predicted"
959
  )
960

  
961
#function_assessment_part2 <- "global_run_scalingup_assessment_part2_01032016.R"
962
#source(file.path(script_path,function_assessment_part2)) #source all functions used in this script
963

  
964
debug(run_assessment_plotting_prediction_fun)
965
df_assessment_figures_files <-
966
  run_assessment_plotting_prediction_fun(list_param_run_assessment_plotting)
967

  
968

  
969

  

Also available in: Unified diff