Project

General

Profile

« Previous | Next » 

Revision a05b1d76

Added by Benoit Parmentier almost 9 years ago

assessment part2 plotting of figures into function callable from stage 6

View differences:

climate/research/oregon/interpolation/global_run_scalingup_assessment_part2.R
5 5
#Analyses, figures, tables and data are also produced in the script.
6 6
#AUTHOR: Benoit Parmentier 
7 7
#CREATED ON: 03/23/2014  
8
#MODIFIED ON: 10/05/2015            
8
#MODIFIED ON: 01/01/2016            
9 9
#Version: 4
10 10
#PROJECT: Environmental Layers project     
11 11
#COMMENTS: analyses for run 10 global analyses,all regions 1500x4500km with additional tiles to increase overlap 
......
89 89
#parent output dir for the current script analyes
90 90
#out_dir <- "/nobackup/bparmen1/" #on NEX
91 91
#in_dir_shp <- "/nobackupp4/aguzman4/climateLayers/output4/subset/shapefiles/"
92

  
92
in_dir <- "" #PARAM 0
93 93
y_var_name <- "dailyTmax" #PARAM1
94 94
interpolation_method <- c("gam_CAI") #PARAM2
95 95
#out_suffix<-"run10_global_analyses_01282015"
......
97 97
out_suffix <- "run10_1500x4500_global_analyses_pred_1992_10052015" #PARAM3
98 98
out_dir <- "/data/project/layers/commons/NEX_data/output_run10_1500x4500_global_analyses_pred_1992_10052015" #PARAM4
99 99
create_out_dir_param <- FALSE #PARAM 5
100

  
101 100
mosaic_plot <- FALSE #PARAM6
102

  
103 101
#if daily mosaics NULL then mosaicas all days of the year
104

  
105
day_to_mosaic <- c("19920101","19920102","19920103")
106

  
107

  
108
CRS_WGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 #CONSTANT1
102
#day_to_mosaic <- c("19920101","19920102","19920103") #PARAM7
103
#CRS_WGS84 <-    CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 #CONSTANT1
109 104
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
110

  
111 105
proj_str<- CRS_WGS84 #PARAM 8 #check this parameter
112 106
file_format <- ".rst" #PARAM 9
113 107
NA_value <- -9999 #PARAM10
114 108
NA_flag_val <- NA_value
115
                                   
116
tile_size <- "1500x4500" #PARAM 11
109
#tile_size <- "1500x4500" #PARAM 11
117 110
multiple_region <- TRUE #PARAM 12
118

  
119
region_name <- "world" #PARAM 13
111
#region_name <- "world" #PARAM 13
112
countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too
120 113
plot_region <- TRUE
121 114
num_cores <- 6 #PARAM 14
122
reg_modified <- TRUE
123
region <- c("reg4") #reference region to merge if necessary #PARAM 16
115
reg_modified <- TRUE #PARAM 15
116
region_name <- c("reg4") #reference region to merge if necessary, if world all the regions are together #PARAM 16
117
#use previous files produced in step 1a and stored in a data.frame
118
df_assessment_files <- "df_assessment_files_reg4_1984_run_global_analyses_pred_12282015.txt" #PARAM 17
119
threshold_missing_day <- c(367,365,300,200) #PARM18
124 120

  
125 121
########################## START SCRIPT ##############################
126 122

  
......
135 131
}
136 132

  
137 133
setwd(out_dir)
134
#i <- year_predicted
135
run_assessment_plotting_prediction_fun <-function(list_param_run_assessment_plotting){
136
  
137
  in_dir <- list_param_run_assessment_plotting$in_dir #PARAM 0
138
  y_var_name <- list_param_run_assessment_plotting$y_var_name #PARAM1
139
  interpolation_method <- list_param_run_assessment_plotting$interpolation_method #c("gam_CAI") #PARAM2
140
  out_suffix <- list_param_run_assessment_plotting$out_suffix #PARAM3
141
  out_dir <- list_param_run_assessment_plotting$out_dir #
142
  create_out_dir_param <- list_param_run_assessment_plotting$create_out_dir_param # FALSE #PARAM 5
143
  mosaic_plot <- list_param_run_assessment_plotting$mosaic_plot #FALSE #PARAM6
144
  proj_str<- list_param_run_assessment_plotting$proj_str #CRS_WGS84 #PARAM 8 #check this parameter
145
  file_format <- list_param_run_assessment_plotting$file_format #".rst" #PARAM 9
146
  NA_value <- list_param_run_assessment_plotting$NA_value #-9999 #PARAM10
147
  multiple_region <- list_param_run_assessment_plotting$multiple_region # <- TRUE #PARAM 12
148
  countries_shp <- list_param_run_assessment_plotting$countries_shp #<- "world" #PARAM 13
149
  plot_region <- list_param_run_assessment_plotting$plot_region #<- TRUE
150
  num_cores <- list_param_run_assessment_plotting$num_cores # 6 #PARAM 14
151
  reg_modified <- list_param_run_assessment_plotting$reg_modified #<- TRUE #PARAM 15
152
  region <- list_param_run_assessment_plotting$region #<- c("reg4") #reference region to merge if necessary #PARAM 16
153
  region_name <- list_param_run_assessment_plotting$region_name #<- "world" #PARAM 13
154
  df_assessment_files <- list_param_run_assessment_plotting$df_assessment_files #PARAM 16
155
  threshold_missing_day <- list_param_run_assessment_plotting$threshold_missing_day #PARM18
156
 
157
  NA_flag_val <- NA_value
158
  
159
  #tile_size <- "1500x4500" #PARAM 11
160
  
161
  ##################### START SCRIPT #################
162
  
163
  ###Table 1: Average accuracy metrics
164
  ###Table 2: daily accuracy metrics for all tiles
138 165

  
139
###Table 1: Average accuracy metrics
140
###Table 2: daily accuracy metrics for all tiles
141

  
142
summary_metrics_v <- read.table(file=file.path(out_dir,paste("summary_metrics_v2_NA_",out_suffix,".txt",sep="")),sep=",")
143
#fname <- file.path(out_dir,paste("summary_metrics_v2_NA_",out_suffix,".txt",sep=""))
144
tb <- read.table(file=file.path(out_dir,paste("tb_diagnostic_v_NA","_",out_suffix,".txt",sep="")),sep=",")
145
#tb_diagnostic_s_NA_run10_global_analyses_11302014.txt
146
tb_s <- read.table(file=file.path(out_dir,paste("tb_diagnostic_s_NA","_",out_suffix,".txt",sep="")),sep=",")
147

  
148
tb_month_s <- read.table(file=file.path(out_dir,paste("tb_month_diagnostic_s_NA","_",out_suffix,".txt",sep="")),sep=",")
149
pred_data_month_info <- read.table(file=file.path(out_dir,paste("pred_data_month_info_",out_suffix,".txt",sep="")),sep=",")
150
pred_data_day_info <- read.table(file=file.path(out_dir,paste("pred_data_day_info_",out_suffix,".txt",sep="")),sep=",")
151
df_tile_processed <- read.table(file=file.path(out_dir,paste("df_tile_processed_",out_suffix,".txt",sep="")),sep=",")
152

  
153
#add column for tile size later on!!!
154

  
155
tb$pred_mod <- as.character(tb$pred_mod)
156
summary_metrics_v$pred_mod <- as.character(summary_metrics_v$pred_mod)
157
summary_metrics_v$tile_id <- as.character(summary_metrics_v$tile_id)
158
df_tile_processed$tile_id <- as.character(df_tile_processed$tile_id)
159

  
160
tb_month_s$pred_mod <- as.character(tb_month_s$pred_mod)
161
tb_month_s$tile_id<- as.character(tb_month_s$tile_id)
162
tb_s$pred_mod <- as.character(tb_s$pred_mod)
163
tb_s$tile_id <- as.character(tb_s$tile_id)
164

  
165

  
166
#multiple regions?
167
if(multiple_region==TRUE){
168
  df_tile_processed$reg <- basename(dirname(as.character(df_tile_processed$path_NEX)))
169
  
170
  tb <- merge(tb,df_tile_processed,by="tile_id")
171
  tb_s <- merge(tb_s,df_tile_processed,by="tile_id")
172
  tb_month_s<- merge(tb_month_s,df_tile_processed,by="tile_id")
173
  summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id")
174

  
175
}
176

  
177
tb_all <- tb
178

  
179
summary_metrics_v_all <- summary_metrics_v 
180
#deal with additional tiles...
181
# if(reg_modified==T){
182
#   
183
#   summary_metrics_v_tmp <- summary_metrics_v
184
#   #summary_metrics_v_tmp$reg[summary_metrics_v_tmp$reg=="reg_1b"] <- "reg1"
185
#   #summary_metrics_v_tmp$reg[summary_metrics_v_tmp$reg=="reg_1c"] <- "reg1"
186
#   #summary_metrics_v_tmp$reg[summary_metrics_v_tmp$reg=="reg_3b"] <- "reg3"
187
#   summary_metrics_v_tmp$reg[summary_metrics_v_tmp$reg=="reg5b"] <- "reg5"
188
# 
189
#   summary_metrics_v_tmp$reg_all <- summary_metrics_v$reg
190
#   ###
191
#   summary_metrics_v<- summary_metrics_v_tmp
192
#   
193
#   ###
194
#   tb_tmp <- tb
195
#   #tb_tmp$reg[tb_tmp$reg=="reg_1b"] <- "reg1"
196
#   #tb_tmp$reg[tb_tmp$reg=="reg_1c"] <- "reg1"
197
#   #tb_tmp$reg[tb_tmp$reg=="reg_3b"] <- "reg3"
198
#   tb_tmp$reg[tb_tmp$reg=="reg5b"] <- "reg5"
199
# 
200
#   ###
201
#   tb <- tb_tmp
202
# }
203

  
204
table(summary_metrics_v_all$reg)
205
table(summary_metrics_v$reg)
206
table(tb_all$reg)
207
table(tb$reg)
208

  
209
############ PART 2: PRODUCE FIGURES ################
210

  
211
###########################
212
### Figure 1: plot location of the study area with tiles processed
213

  
214
#df_tiled_processed <- na.omit(df_tile_processed) #remove other list of folders irrelevant
215
#list_shp_reg_files <- df_tiled_processed$shp_files
216
list_shp_reg_files<- as.character(df_tile_processed$shp_files)
217
#list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir,
218
#          as.character(df_tile_processed$tile_coord),"shapefiles",basename(list_shp_reg_files))
219
list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir,
220
          "shapefiles",basename(list_shp_reg_files))
221

  
222
#table(summary_metrics_v$reg)
223
#table(summary_metrics_v$reg)
224

  
225
### Potential function starts here:
226
#function(in_dir,out_dir,list_shp_reg_files,title_str,region_name,num_cores,out_suffix,out_suffix)
227

  
228
### First get background map to display where study area is located
229
#can make this more general later on..should have this already in a local directory on Atlas or NEX!!!!
230

  
231
if (region_name=="USA"){
232
  usa_map <- getData('GADM', country='USA', level=1) #Get US map
233
  #usa_map <- getData('GADM', country=region_name,level=1) #Get US map, this is not working right now
234
  usa_map <- usa_map[usa_map$NAME_1!="Alaska",] #remove Alaska
235
  reg_layer <- usa_map[usa_map$NAME_1!="Hawaii",] #remove Hawai 
236
}
237

  
238
if (region_name=="world"){
166
  #df_assessment_files, note that in_dir indicate the path of the textfiles
167
  summary_metrics_v <- file.path(in_dir,basename(df_assessment_files$files[1]))
168
  tb <- file.path(in_dir, basename(df_assessment_files$files[2]))
169
  tb_s <-file.path(in_dir, basename(df_assessment_files$files[4]))
170
  
171
  tb_month_s <- file.path(in_dir,basename(df_assessment_files$files[3]))
172
  pred_data_month_info <- file.path(in_dir, basename(df_assessment_files$files[10]))
173
  pred_data_day_info <- file.path(in_dir, basename(df_assessment_files$files[11]))
174
  df_tile_processed <- file.path(in_dir, basename(df_assessment_files$files[12]))
175
  
176
  #add column for tile size later on!!!
177
  
178
  tb$pred_mod <- as.character(tb$pred_mod)
179
  summary_metrics_v$pred_mod <- as.character(summary_metrics_v$pred_mod)
180
  summary_metrics_v$tile_id <- as.character(summary_metrics_v$tile_id)
181
  df_tile_processed$tile_id <- as.character(df_tile_processed$tile_id)
182
  
183
  tb_month_s$pred_mod <- as.character(tb_month_s$pred_mod)
184
  tb_month_s$tile_id<- as.character(tb_month_s$tile_id)
185
  tb_s$pred_mod <- as.character(tb_s$pred_mod)
186
  tb_s$tile_id <- as.character(tb_s$tile_id)
187
  
188
  #multiple regions?
189
  if(multiple_region==TRUE){
190
    df_tile_processed$reg <- basename(dirname(as.character(df_tile_processed$path_NEX)))
191
    tb <- merge(tb,df_tile_processed,by="tile_id")
192
    tb_s <- merge(tb_s,df_tile_processed,by="tile_id")
193
    tb_month_s<- merge(tb_month_s,df_tile_processed,by="tile_id")
194
    summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id")
195
  }
196
  
197
  tb_all <- tb
198
  
199
  summary_metrics_v_all <- summary_metrics_v 
200
  
201
  #table(summary_metrics_v_all$reg)
202
  #table(summary_metrics_v$reg)
203
  #table(tb_all$reg)
204
  #table(tb$reg)
205
  
206
  ############ PART 2: PRODUCE FIGURES ################
207
  
208
  ###########################
209
  ### Figure 1: plot location of the study area with tiles processed
210
  
211
  #df_tiled_processed <- na.omit(df_tile_processed) #remove other list of folders irrelevant
212
  #list_shp_reg_files <- df_tiled_processed$shp_files
213
  list_shp_reg_files<- as.character(df_tile_processed$shp_files)
214
  #list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir,
215
  #          as.character(df_tile_processed$tile_coord),"shapefiles",basename(list_shp_reg_files))
216
  #list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir,
217
                                  #"shapefiles",basename(list_shp_reg_files))
218
  
219
  #table(summary_metrics_v$reg)
220
  #table(summary_metrics_v$reg)
221
  
222
  ### Potential function starts here:
223
  #function(in_dir,out_dir,list_shp_reg_files,title_str,region_name,num_cores,out_suffix,out_suffix)
224
  
225
  ### First get background map to display where study area is located
226
  #can make this more general later on..should have this already in a local directory on Atlas or NEX!!!!
227
  
239 228
  #http://www.diva-gis.org/Data
240 229
  countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp"
241 230
  path_to_shp <- dirname(countries_shp)
......
243 232
  reg_layer <- readOGR(path_to_shp, layer_name)
244 233
  #proj4string(reg_layer) <- CRS_locs_WGS84
245 234
  #reg_shp<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]])))
246
}
247

  
248
centroids_pts <- vector("list",length(list_shp_reg_files))
249
shps_tiles <- vector("list",length(list_shp_reg_files))
250
#collect info: read in all shapfiles
251
#This is slow...make a function and use mclapply??
252
#/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles
253
  
254
for(i in 1:length(list_shp_reg_files)){
255
  #path_to_shp <- dirname(list_shp_reg_files[[i]])
256
  path_to_shp <- file.path(out_dir,"/shapefiles")
257
  layer_name <- sub(".shp","",basename(list_shp_reg_files[[i]]))
258
  shp1 <- try(readOGR(path_to_shp, layer_name)) #use try to resolve error below
259
  #shp_61.0_-160.0
260
  #Geographical CRS given to non-conformant data: -186.331747678
261
 
262
  #shp1<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]])))
263
  if (!inherits(shp1,"try-error")) {
235
  
236
  centroids_pts <- vector("list",length(list_shp_reg_files))
237
  shps_tiles <- vector("list",length(list_shp_reg_files))
238
  #collect info: read in all shapfiles
239
  #This is slow...make a function and use mclapply??
240
  #/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles
241
  
242
  for(i in 1:length(list_shp_reg_files)){
243
    #path_to_shp <- dirname(list_shp_reg_files[[i]])
244
    path_to_shp <- file.path(out_dir,"/shapefiles")
245
    layer_name <- sub(".shp","",basename(list_shp_reg_files[[i]]))
246
    shp1 <- try(readOGR(path_to_shp, layer_name)) #use try to resolve error below
247
    #shp_61.0_-160.0
248
    #Geographical CRS given to non-conformant data: -186.331747678
249
    
250
    #shp1<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]])))
251
    if (!inherits(shp1,"try-error")) {
264 252
      pt <- gCentroid(shp1)
265 253
      centroids_pts[[i]] <- pt
266
  }else{
267
    pt <- shp1
268
    centroids_pts[[i]] <- pt
269
  }
270
  shps_tiles[[i]] <- shp1
271
  #centroids_pts[[i]] <- centroids
272
}
273

  
274
#fun <- function(i,list_shp_files)
275
#coord_names <- c("lon","lat")
276
#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)
277

  
278
#remove try-error polygons...we loose three tiles because they extend beyond -180 deg
279
tmp <- shps_tiles
280
shps_tiles <- remove_errors_list(shps_tiles) #[[!inherits(shps_tiles,"try-error")]]
281
#shps_tiles <- tmp
282
length(tmp)-length(shps_tiles) #number of tiles with error message
283

  
284
tmp_pts <- centroids_pts 
285
centroids_pts <- remove_errors_list(centroids_pts) #[[!inherits(shps_tiles,"try-error")]]
286
#centroids_pts <- tmp_pts 
287
  
288
#plot info: with labels
289
res_pix <-1200
290
col_mfrow <- 1 
291
row_mfrow <- 1
292

  
293
png(filename=paste("Figure1_tile_processed_region_",region_name,"_",out_suffix,".png",sep=""),
294
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
295

  
296
plot(reg_layer)
297
#Add polygon tiles...
298
for(i in 1:length(shps_tiles)){
299
  shp1 <- shps_tiles[[i]]
300
  pt <- centroids_pts[[i]]
301
  if(!inherits(shp1,"try-error")){
302
    plot(shp1,add=T,border="blue")
303
    #plot(pt,add=T,cex=2,pch=5)
304
    label_id <- df_tile_processed$tile_id[i]
305
    text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1.3,font=2,col=c("red"))
254
    }else{
255
      pt <- shp1
256
      centroids_pts[[i]] <- pt
257
    }
258
    shps_tiles[[i]] <- shp1
259
    #centroids_pts[[i]] <- centroids
306 260
  }
307
}
308
#title(paste("Tiles ", tile_size,region_name,sep=""))
309

  
310
dev.off()
311
      
312
#unique(summaty_metrics$tile_id)
313
#text(lat-shp,)
314
#union(list_shp_reg_files[[1]],list_shp_reg_files[[2]])
315

  
316
###############
317
### Figure 2: boxplot of average accuracy by model and by tiles
318

  
319

  
320
tb$tile_id <- factor(tb$tile_id, levels=unique(tb$tile_id))
321

  
322
model_name <- as.character(unique(tb$pred_mod))
323

  
324

  
325
## Figure 2a
326

  
327
for(i in  1:length(model_name)){
328 261
  
329
  res_pix <- 480
330
  col_mfrow <- 1
262
  #fun <- function(i,list_shp_files)
263
  #coord_names <- c("lon","lat")
264
  #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)
265
  
266
  #remove try-error polygons...we loose three tiles because they extend beyond -180 deg
267
  tmp <- shps_tiles
268
  shps_tiles <- remove_errors_list(shps_tiles) #[[!inherits(shps_tiles,"try-error")]]
269
  #shps_tiles <- tmp
270
  length(tmp)-length(shps_tiles) #number of tiles with error message
271
  
272
  tmp_pts <- centroids_pts 
273
  centroids_pts <- remove_errors_list(centroids_pts) #[[!inherits(shps_tiles,"try-error")]]
274
  #centroids_pts <- tmp_pts 
275
  
276
  #plot info: with labels
277
  res_pix <-1200
278
  col_mfrow <- 1 
331 279
  row_mfrow <- 1
332

  
333
  png(filename=paste("Figure2a_boxplot_with_oultiers_by_tiles_",model_name[i],"_",out_suffix,".png",sep=""),
334
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
335 280
  
336
  boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod==model_name[i]))
337
  title(paste("RMSE per ",model_name[i]))
281
  png(filename=paste("Figure1_tile_processed_region_",region_name,"_",out_suffix,".png",sep=""),
282
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
283
  
284
  plot(reg_layer)
285
  #Add polygon tiles...
286
  for(i in 1:length(shps_tiles)){
287
    shp1 <- shps_tiles[[i]]
288
    pt <- centroids_pts[[i]]
289
    if(!inherits(shp1,"try-error")){
290
      plot(shp1,add=T,border="blue")
291
      #plot(pt,add=T,cex=2,pch=5)
292
      label_id <- df_tile_processed$tile_id[i]
293
      text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1.3,font=2,col=c("red"))
294
    }
295
  }
296
  #title(paste("Tiles ", tile_size,region_name,sep=""))
338 297
  
339 298
  dev.off()
340
}
341

  
342
## Figure 2b
343
#wtih ylim and removing trailing...
344
for(i in  1:length(model_name)){
345

  
299
  
300
  #unique(summaty_metrics$tile_id)
301
  #text(lat-shp,)
302
  #union(list_shp_reg_files[[1]],list_shp_reg_files[[2]])
303
  
304
  ###############
305
  ### Figure 2: boxplot of average accuracy by model and by tiles
306
  
307
  
308
  tb$tile_id <- factor(tb$tile_id, levels=unique(tb$tile_id))
309
  
310
  model_name <- as.character(unique(tb$pred_mod))
311
  
312
  
313
  ## Figure 2a
314
  
315
  for(i in  1:length(model_name)){
316
    
317
    res_pix <- 480
318
    col_mfrow <- 1
319
    row_mfrow <- 1
320
    
321
    png(filename=paste("Figure2a_boxplot_with_oultiers_by_tiles_",model_name[i],"_",out_suffix,".png",sep=""),
322
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
323
    
324
    boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod==model_name[i]))
325
    title(paste("RMSE per ",model_name[i]))
326
    
327
    dev.off()
328
  }
329
  
330
  ## Figure 2b
331
  #wtih ylim and removing trailing...
332
  for(i in  1:length(model_name)){
333
    
334
    res_pix <- 480
335
    col_mfrow <- 1
336
    row_mfrow <- 1
337
    png(filename=paste("Figure2b_boxplot_scaling_by_tiles","_",model_name[i],"_",out_suffix,".png",sep=""),
338
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
339
    
340
    model_name <- unique(tb$pred_mod)
341
    boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod==model_name[i])
342
            ,ylim=c(0,4),outline=FALSE)
343
    title(paste("RMSE per ",model_name[i]))
344
    dev.off()
345
  }
346
  #bwplot(rmse~tile_id, data=subset(tb,tb$pred_mod=="mod1"))
347
  
348
  ###############
349
  ### Figure 3: boxplot of average RMSE by model acrosss all tiles
350
  
351
  ## Figure 3a
346 352
  res_pix <- 480
347 353
  col_mfrow <- 1
348 354
  row_mfrow <- 1
349
  png(filename=paste("Figure2b_boxplot_scaling_by_tiles","_",model_name[i],"_",out_suffix,".png",sep=""),
350
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
351

  
352
  model_name <- unique(tb$pred_mod)
353
  boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod==model_name[i])
354
          ,ylim=c(0,4),outline=FALSE)
355
  title(paste("RMSE per ",model_name[i]))
355
  
356
  png(filename=paste("Figure3a_boxplot_overall_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep=""),
357
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
358
  
359
  boxplot(rmse~pred_mod,data=tb)#,names=tb$pred_mod)
360
  title("RMSE per model over all tiles")
361
  
356 362
  dev.off()
357
}
358
#bwplot(rmse~tile_id, data=subset(tb,tb$pred_mod=="mod1"))
359

  
360
###############
361
### Figure 3: boxplot of average RMSE by model acrosss all tiles
362

  
363
## Figure 3a
364
res_pix <- 480
365
col_mfrow <- 1
366
row_mfrow <- 1
367

  
368
png(filename=paste("Figure3a_boxplot_overall_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep=""),
369
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
370

  
371
boxplot(rmse~pred_mod,data=tb)#,names=tb$pred_mod)
372
title("RMSE per model over all tiles")
373

  
374
dev.off()
375

  
376
## Figure 3b
377
png(filename=paste("Figure3b_boxplot_overall_region_scaling_",model_name[i],"_",out_suffix,".png",sep=""),
378
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
379

  
380
boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
381
title("RMSE per model over all tiles")
382

  
383
dev.off()
384

  
385

  
386
################ 
387
### Figure 4: plot predicted tiff for specific date per model
388

  
389
#y_var_name <-"dailyTmax"
390
#index <-244 #index corresponding to Sept 1
391

  
392
# if (mosaic_plot==TRUE){
393
#   index  <- 1 #index corresponding to Jan 1
394
#   date_selected <- "20100901"
395
#   name_method_var <- paste(interpolation_method,"_",y_var_name,"_",sep="")
396
# 
397
#   pattern_str <- paste("mosaiced","_",name_method_var,"predicted",".*.",date_selected,".*.tif",sep="")
398
#   lf_pred_list <- list.files(pattern=pattern_str)
399
# 
400
#   for(i in 1:length(lf_pred_list)){
401
#     
402
#   
403
#     r_pred <- raster(lf_pred_list[i])
404
#   
405
#     res_pix <- 480
406
#     col_mfrow <- 1
407
#     row_mfrow <- 1
408
#   
409
#     png(filename=paste("Figure4_models_predicted_surfaces_",model_name[i],"_",name_method_var,"_",data_selected,"_",out_suffix,".png",sep=""),
410
#        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
411
#   
412
#     plot(r_pred)
413
#     title(paste("Mosaiced",model_name[i],name_method_var,date_selected,sep=" "))
414
#     dev.off()
415
#   }
416
#   #Plot Delta and clim...
417
# 
418
#    ## plotting of delta and clim for later scripts...
419
# 
420
# }
421

  
422

  
423
######################
424
### Figure 5: plot accuracy ranked 
425

  
426
#Turn summary table to a point shp
427

  
428
list_df_ac_mod <- vector("list",length=length(model_name))
429
for (i in 1:length(model_name)){
430 363
  
431
  ac_mod <- summary_metrics_v[summary_metrics_v$pred_mod==model_name[i],]
432
  ### Ranking by tile...
433
  df_ac_mod <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("pred_mod","rmse","mae","tile_id")]
434
  list_df_ac_mod[[i]] <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("rmse","mae","tile_id")]
364
  ## Figure 3b
365
  png(filename=paste("Figure3b_boxplot_overall_region_scaling_",model_name[i],"_",out_suffix,".png",sep=""),
366
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
367
  
368
  boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
369
  title("RMSE per model over all tiles")
435 370
  
436
  res_pix <- 480
437
  col_mfrow <- 1
438
  row_mfrow <- 1
439

  
440
  png(filename=paste("Figure5_ac_metrics_ranked_",model_name[i],"_",out_suffix,".png",sep=""),
441
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
442
  x<- as.character(df_ac_mod$tile_id)
443
  barplot(df_ac_mod$rmse, names.arg=x)
444
  #plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T)
445
  #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
446
  title(paste("RMSE ranked by tile for ",model_name[i],sep=""))
447

  
448 371
  dev.off()
449 372
  
450
}
451

  
452
######################
453
### Figure 6: plot map of average RMSE per tile at centroids
454

  
455
#Turn summary table to a point shp
456

  
457
# coordinates(summary_metrics_v) <- cbind(summary_metrics_v$lon,summary_metrics_v$lat)
458
# proj4string(summary_metrics_v) <- CRS_WGS84
459
# #lf_list <- lf_pred_list
460
# list_df_ac_mod <- vector("list",length=length(lf_pred_list))
461
# for (i in 1:length(lf_list)){
462
#   
463
#   ac_mod <- summary_metrics_v[summary_metrics_v$pred_mod==model_name[i],]
464
#   r_pred <- raster(lf_list[i])
465
#   
466
#   res_pix <- 480
467
#   col_mfrow <- 1
468
#   row_mfrow <- 1
469
# 
470
#   png(filename=paste("Figure6_ac_metrics_map_centroids_tile_",model_name[i],"_",out_suffix,".png",sep=""),
471
#     width=col_mfrow*res_pix,height=row_mfrow*res_pix)
472
# 
473
#   plot(r_pred)  
474
#   
475
#   #plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T)
476
#   plot(ac_mod,cex=(ac_mod$rmse^2)/10,pch=1,add=T)
477
#   #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
478
#   title(paste("Averrage RMSE per tile and by ",model_name[i]))
479
# 
480
#   dev.off()
481
#   
482
#   ### Ranking by tile...
483
#   #df_ac_mod <- 
484
#   list_df_ac_mod[[i]] <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("rmse","mae","tile_id")]
485
# }
486
  
487
#quick kriging...
488
#autokrige(rmse~1,r2,)
489

  
490

  
491
### Without 
492

  
493
#list_df_ac_mod <- vector("list",length=length(lf_pred_list))
494
list_df_ac_mod <- vector("list",length=2)
495

  
496
for (i in 1:length(model_name)){
497 373
  
498
  ac_mod <- summary_metrics_v[summary_metrics_v$pred_mod==model_name[i],]
499
  #r_pred <- raster(lf_list[i])
374
  ################ 
375
  ### Figure 4: plot predicted tiff for specific date per model
500 376
  
501
  res_pix <- 1200
502
  #res_pix <- 480
503

  
504
  col_mfrow <- 1
505
  row_mfrow <- 1
506

  
507
  png(filename=paste("Figure6_ac_metrics_map_centroids_tile_",model_name[i],"_",out_suffix,".png",sep=""),
508
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
509

  
510
  #plot(r_pred)  
511
  #plot(reg_layer)
512
  #plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T)
513
  #plot(ac_mod,cex=(ac_mod$rmse^2)/10,pch=1,col="red",add=T)
514

  
515
  #coordinates(ac_mod) <- ac_mod[,c("lon","lat")] 
516
  coordinates(ac_mod) <- ac_mod[,c("lon.x","lat.x")] #solve this later
517
  p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
518
  #title("(a) Mean for 1 January")
519
  p <- bubble(ac_mod,"rmse",main=paste("Averrage RMSE per tile and by ",model_name[i]))
520
  p1 <- p+p_shp
521
  print(p1)
522
  #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
523
  #title(paste("Averrage RMSE per tile and by ",model_name[i]))
524

  
377
  #y_var_name <-"dailyTmax"
378
  #index <-244 #index corresponding to Sept 1
379
  
380
  # if (mosaic_plot==TRUE){
381
  #   index  <- 1 #index corresponding to Jan 1
382
  #   date_selected <- "20100901"
383
  #   name_method_var <- paste(interpolation_method,"_",y_var_name,"_",sep="")
384
  # 
385
  #   pattern_str <- paste("mosaiced","_",name_method_var,"predicted",".*.",date_selected,".*.tif",sep="")
386
  #   lf_pred_list <- list.files(pattern=pattern_str)
387
  # 
388
  #   for(i in 1:length(lf_pred_list)){
389
  #     
390
  #   
391
  #     r_pred <- raster(lf_pred_list[i])
392
  #   
393
  #     res_pix <- 480
394
  #     col_mfrow <- 1
395
  #     row_mfrow <- 1
396
  #   
397
  #     png(filename=paste("Figure4_models_predicted_surfaces_",model_name[i],"_",name_method_var,"_",data_selected,"_",out_suffix,".png",sep=""),
398
  #        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
399
  #   
400
  #     plot(r_pred)
401
  #     title(paste("Mosaiced",model_name[i],name_method_var,date_selected,sep=" "))
402
  #     dev.off()
403
  #   }
404
  #   #Plot Delta and clim...
405
  # 
406
  #    ## plotting of delta and clim for later scripts...
407
  # 
408
  # }
409
  
410
  
411
  ######################
412
  ### Figure 5: plot accuracy ranked 
413
  
414
  #Turn summary table to a point shp
415
  
416
  list_df_ac_mod <- vector("list",length=length(model_name))
417
  for (i in 1:length(model_name)){
418
    
419
    ac_mod <- summary_metrics_v[summary_metrics_v$pred_mod==model_name[i],]
420
    ### Ranking by tile...
421
    df_ac_mod <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("pred_mod","rmse","mae","tile_id")]
422
    list_df_ac_mod[[i]] <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("rmse","mae","tile_id")]
423
    
424
    res_pix <- 480
425
    col_mfrow <- 1
426
    row_mfrow <- 1
427
    
428
    png(filename=paste("Figure5_ac_metrics_ranked_",model_name[i],"_",out_suffix,".png",sep=""),
429
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
430
    x<- as.character(df_ac_mod$tile_id)
431
    barplot(df_ac_mod$rmse, names.arg=x)
432
    #plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T)
433
    #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
434
    title(paste("RMSE ranked by tile for ",model_name[i],sep=""))
435
    
436
    dev.off()
437
    
438
  }
439
  
440
  ######################
441
  ### Figure 6: plot map of average RMSE per tile at centroids
442
  
443
  ### Without 
444
  
445
  #list_df_ac_mod <- vector("list",length=length(lf_pred_list))
446
  list_df_ac_mod <- vector("list",length=2)
447
  
448
  for (i in 1:length(model_name)){
449
    
450
    ac_mod <- summary_metrics_v[summary_metrics_v$pred_mod==model_name[i],]
451
    #r_pred <- raster(lf_list[i])
452
    
453
    res_pix <- 1200
454
    #res_pix <- 480
455
    
456
    col_mfrow <- 1
457
    row_mfrow <- 1
458
    
459
    png(filename=paste("Figure6_ac_metrics_map_centroids_tile_",model_name[i],"_",out_suffix,".png",sep=""),
460
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
461
    
462
    #plot(r_pred)  
463
    #plot(reg_layer)
464
    #plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T)
465
    #plot(ac_mod,cex=(ac_mod$rmse^2)/10,pch=1,col="red",add=T)
466
    
467
    #coordinates(ac_mod) <- ac_mod[,c("lon","lat")] 
468
    coordinates(ac_mod) <- ac_mod[,c("lon.x","lat.x")] #solve this later
469
    p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
470
    #title("(a) Mean for 1 January")
471
    p <- bubble(ac_mod,"rmse",main=paste("Averrage RMSE per tile and by ",model_name[i]))
472
    p1 <- p+p_shp
473
    print(p1)
474
    #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
475
    #title(paste("Averrage RMSE per tile and by ",model_name[i]))
476
    
477
    dev.off()
478
    
479
    ### Ranking by tile...
480
    #df_ac_mod <- 
481
    list_df_ac_mod[[i]] <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("rmse","mae","tile_id")]
482
  }
483
  
484
  ## Number of tiles with information:
485
  sum(df_tile_processed$metrics_v) #26,number of tiles with raster object
486
  length(df_tile_processed$metrics_v) #26,number of tiles in the region
487
  sum(df_tile_processed$metrics_v)/length(df_tile_processed$metrics_v) #80 of tiles with info
488
  
489
  #coordinates
490
  #coordinates(summary_metrics_v) <- c("lon","lat")
491
  coordinates(summary_metrics_v) <- c("lon.y","lat.y")
492
  
493
  #threshold_missing_day <- c(367,365,300,200)
494
  
495
  nb<-nrow(subset(summary_metrics_v,model_name=="mod1"))  
496
  sum(subset(summary_metrics_v,model_name=="mod1")$n_missing)/nb #33/35
497
  
498
  ## Make this a figure...
499
  
500
  #plot(summary_metrics_v)
501
  #Make this a function later so that we can explore many thresholds...
502
  
503
  j<-1 #for model name 1
504
  for(i in 1:length(threshold_missing_day)){
505
    
506
    #summary_metrics_v$n_missing <- summary_metrics_v$n == 365
507
    #summary_metrics_v$n_missing <- summary_metrics_v$n < 365
508
    summary_metrics_v$n_missing <- summary_metrics_v$n < threshold_missing_day[i]
509
    summary_metrics_v_subset <- subset(summary_metrics_v,model_name=="mod1")
510
    
511
    #res_pix <- 1200
512
    res_pix <- 960
513
    
514
    col_mfrow <- 1
515
    row_mfrow <- 1
516
    
517
    png(filename=paste("Figure7a_ac_metrics_map_centroids_tile_",model_name[j],"_","missing_day_",threshold_missing_day[i],
518
                       "_",out_suffix,".png",sep=""),
519
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
520
    
521
    model_name[j]
522
    
523
    p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
524
    #title("(a) Mean for 1 January")
525
    p <- bubble(summary_metrics_v_subset,"n_missing",main=paste("Missing per tile and by ",model_name[j]," for ",
526
                                                                threshold_missing_day[i]))
527
    p1 <- p+p_shp
528
    try(print(p1)) #error raised if number of missing values below a threshold does not exist
529
    #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
530
    #title(paste("Averrage RMSE per tile and by ",model_name[i]))
531
    
532
    dev.off()
533
  }
534
  
535
  ######################
536
  ### Figure 7: Number of predictions: daily and monthly
537
  
538
  #xyplot(rmse~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
539
  #                                           pred_mod!="mod_kr"),type="b")
540
  
541
  #xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
542
  #                                           pred_mod!="mod_kr"),type="h")
543
  
544
  #cor
545
  
546
  # 
547
  ## Figure 7a
548
  png(filename=paste("Figure7a_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
549
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
550
  
551
  xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
552
                                          pred_mod!="mod_kr"),type="h")
525 553
  dev.off()
526 554
  
527
  ### Ranking by tile...
528
  #df_ac_mod <- 
529
  list_df_ac_mod[[i]] <- arrange(as.data.frame(ac_mod),desc(rmse))[,c("rmse","mae","tile_id")]
530
}
531

  
532
## Number of tiles with information:
533
sum(df_tile_processed$metrics_v) #26,number of tiles with raster object
534
length(df_tile_processed$metrics_v) #26,number of tiles in the region
535
sum(df_tile_processed$metrics_v)/length(df_tile_processed$metrics_v) #80 of tiles with info
536

  
537
#coordinates
538
#coordinates(summary_metrics_v) <- c("lon","lat")
539
coordinates(summary_metrics_v) <- c("lon.y","lat.y")
540

  
541
threshold_missing_day <- c(367,365,300,200)
542

  
543
nb<-nrow(subset(summary_metrics_v,model_name=="mod1"))  
544
sum(subset(summary_metrics_v,model_name=="mod1")$n_missing)/nb #33/35
545

  
546
## Make this a figure...
547

  
548
#plot(summary_metrics_v)
549
#Make this a function later so that we can explore many thresholds...
550

  
551
j<-1 #for model name 1
552
for(i in 1:length(threshold_missing_day)){
555
  table(tb$pred_mod)
556
  table(tb$index_d)
557
  table(subset(tb,pred_mod!="mod_kr"))
558
  table(subset(tb,pred_mod=="mod1")$index_d)
559
  #aggregate()
560
  tb$predicted <- 1
561
  test <- aggregate(predicted~pred_mod+tile_id,data=tb,sum)
562
  xyplot(predicted~pred_mod | tile_id,data=subset(as.data.frame(test),
563
                                                  pred_mod!="mod_kr"),type="h")
553 564
  
554
  #summary_metrics_v$n_missing <- summary_metrics_v$n == 365
555
  #summary_metrics_v$n_missing <- summary_metrics_v$n < 365
556
  summary_metrics_v$n_missing <- summary_metrics_v$n < threshold_missing_day[i]
557
  summary_metrics_v_subset <- subset(summary_metrics_v,model_name=="mod1")
558

  
559
  #res_pix <- 1200
560
  res_pix <- 960
561

  
565
  as.character(unique(test$tile_id)) #141 tiles
566
  
567
  dim(subset(test,test$predicted==365 & test$pred_mod=="mod1"))
568
  histogram(subset(test, test$pred_mod=="mod1")$predicted)
569
  unique(subset(test, test$pred_mod=="mod1")$predicted)
570
  table((subset(test, test$pred_mod=="mod1")$predicted))
571
  
572
  #LST_avgm_min <- aggregate(LST~month,data=data_month_all,min)
573
  histogram(test$predicted~test$tile_id)
574
  #table(tb)
575
  ## Figure 7b
576
  #png(filename=paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
577
  #    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
578
  
579
  #xyplot(n~month | tile_id + pred_mod,data=subset(as.data.frame(tb_month_s),
580
  #                                           pred_mod!="mod_kr"),type="h")
581
  #xyplot(n~month | tile_id,data=subset(as.data.frame(tb_month_s),
582
  #                                           pred_mod="mod_1"),type="h")
583
  #test=subset(as.data.frame(tb_month_s),pred_mod="mod_1")
584
  #table(tb_month_s$month)
585
  #dev.off()
586
  #
587
  
588
  
589
  ##########################################################
590
  ##### Figure 8: Breaking down accuracy by regions!! #####
591
  
592
  #summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id")
593
  
594
  ## Figure 8a
595
  res_pix <- 480
562 596
  col_mfrow <- 1
563 597
  row_mfrow <- 1
564

  
565
  png(filename=paste("Figure7a_ac_metrics_map_centroids_tile_",model_name[j],"_","missing_day_",threshold_missing_day[i],
566
                     "_",out_suffix,".png",sep=""),
567
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
568
  
569
  model_name[j]
570
  
571
  p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
572
  #title("(a) Mean for 1 January")
573
  p <- bubble(summary_metrics_v_subset,"n_missing",main=paste("Missing per tile and by ",model_name[j]," for ",
574
                                                              threshold_missing_day[i]))
575
  p1 <- p+p_shp
576
  try(print(p1)) #error raised if number of missing values below a threshold does not exist
577
  #plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T)
578
  #title(paste("Averrage RMSE per tile and by ",model_name[i]))
579

  
598
  
599
  png(filename=paste("Figure8a_boxplot_overall_separated_by_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep=""),
600
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
601
  
602
  p<- bwplot(rmse~pred_mod | reg, data=tb,
603
             main="RMSE per model and region over all tiles")
604
  print(p)
580 605
  dev.off()
581
}
582

  
583
######################
584
### Figure 7: Number of predictions: daily and monthly
585

  
586
#xyplot(rmse~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
587
#                                           pred_mod!="mod_kr"),type="b")
588

  
589
#xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
590
#                                           pred_mod!="mod_kr"),type="h")
591

  
592
#cor
593

  
594
# 
595
## Figure 7a
596
png(filename=paste("Figure7a_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
597
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
598

  
599
xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
600
                                           pred_mod!="mod_kr"),type="h")
601
dev.off()
602

  
603
table(tb$pred_mod)
604
table(tb$index_d)
605
table(subset(tb,pred_mod!="mod_kr"))
606
table(subset(tb,pred_mod=="mod1")$index_d)
607
#aggregate()
608
tb$predicted <- 1
609
test <- aggregate(predicted~pred_mod+tile_id,data=tb,sum)
610
xyplot(predicted~pred_mod | tile_id,data=subset(as.data.frame(test),
611
                                           pred_mod!="mod_kr"),type="h")
612

  
613
test
614

  
615
as.character(unique(test$tile_id)) #141 tiles
616

  
617
dim(subset(test,test$predicted==365 & test$pred_mod=="mod1"))
618
histogram(subset(test, test$pred_mod=="mod1")$predicted)
619
unique(subset(test, test$pred_mod=="mod1")$predicted)
620
table((subset(test, test$pred_mod=="mod1")$predicted))
621

  
622
#LST_avgm_min <- aggregate(LST~month,data=data_month_all,min)
623
histogram(test$predicted~test$tile_id)
624
#table(tb)
625
## Figure 7b
626
#png(filename=paste("Figure7b_number_daily_predictions_per_models","_",out_suffix,".png",sep=""),
627
#    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
628

  
629
#xyplot(n~month | tile_id + pred_mod,data=subset(as.data.frame(tb_month_s),
630
#                                           pred_mod!="mod_kr"),type="h")
631
#xyplot(n~month | tile_id,data=subset(as.data.frame(tb_month_s),
632
#                                           pred_mod="mod_1"),type="h")
633
#test=subset(as.data.frame(tb_month_s),pred_mod="mod_1")
634
#table(tb_month_s$month)
635
#dev.off()
636
#
637

  
638

  
639
##########################################################
640
##### Figure 8: Breaking down accuracy by regions!! #####
641

  
642
#summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id")
643

  
644
## Figure 8a
645
res_pix <- 480
646
col_mfrow <- 1
647
row_mfrow <- 1
648

  
649
png(filename=paste("Figure8a_boxplot_overall_separated_by_region_with_oultiers_",model_name[i],"_",out_suffix,".png",sep=""),
650
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
651

  
652
p<- bwplot(rmse~pred_mod | reg, data=tb,
653
           main="RMSE per model and region over all tiles")
654
print(p)
655
dev.off()
656

  
657
## Figure 8b
658
png(filename=paste("Figure8b_boxplot_overall_separated_by_region_scaling_",model_name[i],"_",out_suffix,".png",sep=""),
659
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
660

  
661
boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
662
title("RMSE per model over all tiles")
663
p<- bwplot(rmse~pred_mod | reg, data=tb,ylim=c(0,5),
664
           main="RMSE per model and region over all tiles")
665
print(p)
666
dev.off()
667

  
668
#####################################################
669
#### Figure 9: plotting mosaics of regions ###########
670
## plot mosaics...
671

  
672
#First collect file names
673

  
674

  
675
#names(lf_mosaics_reg) <- l_reg_name
676

  
677
#This part should be automated...
678
#plot Australia
679
#lf_m <- lf_mosaics_reg[[2]]
680
#out_dir_str <- out_dir
681
#reg_name <- "reg6_1000x3000"
682
#lapply()
683
#list_param_plot_daily_mosaics <- list(lf_m=lf_m,reg_name=reg_name,out_dir_str=out_dir_str,out_suffix=out_suffix)
684
#list_param_plot_daily_mosaics <- list(lf_m=lf_m,reg_name=reg_name,out_dir_str=out_dir_str,out_suffix=out_suffix,l_dates=day_to_mosaic)
685

  
686
#lf_m_mask_reg4_1500x4500 <- mclapply(1:2,FUN=plot_daily_mosaics,list_param=list_param_plot_daily_mosaics,mc.preschedule=FALSE,mc.cores = 6)
687
#debug(plot_daily_mosaics)
688
#lf_m_mask_reg6_1000x3000 <- plot_daily_mosaics(1,list_param=list_param_plot_daily_mosaics)
689

  
690
#lf_m_mask_reg6_1000x3000 <- mclapply(1:length(lf_m),FUN=plot_daily_mosaics,list_param=list_param_plot_daily_mosaics,mc.preschedule=FALSE,mc.cores = 10)
691

  
692

  
693
################## WORLD MOSAICS NEEDS MAJOR CLEAN UP OF CODE HERE
694
##make functions!!
695
###Combine mosaics with modified code from Alberto
696

  
697
#use list from above!!
698

  
699
# test_list <-list.files(path=file.path(out_dir,"mosaics"),    
700
#            pattern=paste("^world_mosaics.*.tif$",sep=""), 
701
# )
702
# #world_mosaics_mod1_output1500x4500_km_20101105_run10_1500x4500_global_analyses_03112015.tif
703
# 
704
# #test_list<-lapply(1:30,FUN=function(i){lapply(1:x[[i]]},x=lf_mosaics_mask_reg)
705
# #test_list<-unlist(test_list)
706
# #mosaic_list_mean <- vector("list",length=1)
707
# mosaic_list_mean <- test_list 
708
# out_rastnames <- "world_test_mosaic_20100101"
709
# out_path <- out_dir
710
# 
711
# list_param_mosaic<-list(mosaic_list_mean,out_path,out_rastnames,file_format,NA_flag_val,out_suffix)
712
# names(list_param_mosaic)<-c("mosaic_list","out_path","out_rastnames","file_format","NA_flag_val","out_suffix")
713
# #mean_m_list <-mclapply(1:12, list_param=list_param_mosaic, mosaic_m_raster_list,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
714
# 
715
# lf <- mosaic_m_raster_list(1,list_param_mosaic)
716
# 
717
# debug(mosaic_m_raster_list)
718
# mosaic_list<-list_param$mosaic_list
719
# out_path<-list_param$out_path
720
# out_names<-list_param$out_rastnames
721
# file_format <- list_param$file_format
722
# NA_flag_val <- list_param$NA_flag_val
723
# out_suffix <- list_param$out_suffix
724

  
725
##Now mosaic for mean: should reorder files!!
726
#out_rastnames_mean<-paste("_",lst_pat,"_","mean",out_suffix,sep="")
727
#list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
728
#mosaic_list<-split(tmp_str1,list_date_names)
729
#new_list<-vector("list",length(mosaic_list))
730
# for (i in 1:length(list_date_names)){
731
#    j<-grep(list_date_names[i],mosaic_list,value=FALSE)
732
#    names(mosaic_list)[j]<-list_date_names[i]
733
#    new_list[i]<-mosaic_list[j]
734
#  }
735
#  mosaic_list_mean <-new_list #list ready for mosaicing
736
#  out_rastnames_mean<-paste(list_date_names,out_rastnames_mean,sep="")
737 606
  
738
  ### Now mosaic tiles...Note that function could be improved to use less memory
739

  
740

  
741
################## PLOTTING WORLD MOSAICS ################
742

  
743
#lf_world_pred <-list.files(path=file.path(out_dir,"mosaics"),    
744
#           pattern=paste("^world_mosaics.*.tif$",sep=""),full.names=T) 
745

  
746
lf_world_pred <-list.files(path=file.path(out_dir,"mosaics"),    
747
           pattern=paste("^reg5.*.",".tif$",sep=""),full.names=T) 
748
l_reg_name <- unique(df_tile_processed$reg)
749
lf_world_pred <-list.files(path=file.path(out_dir,l_reg_name[[i]]),    
750
           pattern=paste(".tif$",sep=""),full.names=T) 
751

  
752
#mosaic_list_mean <- test_list 
753
#out_rastnames <- "world_test_mosaic_20100101"
754
#out_path <- out_dir
755

  
756
#lf_world_pred <- list.files(pattern="world.*2010090.*.tif$")
757
#lf_raster_fname <- list.files(pattern="world.*2010*.*02162015.tif$",full.names=T)
758
lf_raster_fname <- lf_world_pred
759
prefix_str <- "Figure10_clim_world_mosaics_day_"
760
l_dates <-day_to_mosaic
761
screenRast=FALSE
762
list_param_plot_screen_raster <- list(lf_raster_fname,screenRast,l_dates,out_dir,prefix_str,out_suffix)
763
names(list_param_plot_screen_raster) <- c("lf_raster_fname","screenRast","l_dates","out_dir_str","prefix_str","out_suffix_str")
764

  
765
debug(plot_screen_raster_val)
766

  
767
world_m_list1<- plot_screen_raster_val(1,list_param_plot_screen_raster)
768
#world_m_list <- mclapply(11:30, list_param=list_param_plot_screen_raster, plot_screen_raster_val,mc.preschedule=FALSE,mc.cores = num_cores) #This is the end bracket from mclapply(...) statement
769
world_m_list <- mclapply(1:length(l_dates), list_param=list_param_plot_screen_raster, plot_screen_raster_val,mc.preschedule=FALSE,mc.cores = num_cores) #This is the end bracket from mclapply(...) statement
770

  
771
s_pred <- stack(lf_raster_fname)
772

  
773
res_pix <- 1500
774
col_mfrow <- 3 
775
row_mfrow <- 2
776

  
777
png(filename=paste("Figure10_levelplot_combined_",region_name,"_",out_suffix,".png",sep=""),
778
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
779

  
780
levelplot(s_pred,layers=1:6,col.regions=rev(terrain.colors(255)),cex=4)
781

  
782
dev.off()
783

  
784
#   blues<- designer.colors(6, c( "blue", "white") )
785
# reds <- designer.colors(6, c( "white","red")  )
786
# colorTable<- c( blues[-6], reds)
787
# breaks with a gap of 10 to 17 assigned the white color
788
# brks<- c(seq( 1, 10,,6), seq( 17, 25,,6)) 
789
# image.plot( x,y,z,breaks=brks, col=colorTable)
790
#
791

  
792
#lf_world_mask_reg <- vector("list",length=length(lf_world_pred))
793
#for(i in 1:length(lf_world_pred)){
794
  #
795
#  lf_m <- lf_mosaics_reg[i]
796
#  out_dir_str <- out_dir
797
  #reg_name <- paste(l_reg_name[i],"_",tile_size,sep="") #make this automatic
607
  ## Figure 8b
608
  png(filename=paste("Figure8b_boxplot_overall_separated_by_region_scaling_",model_name[i],"_",out_suffix,".png",sep=""),
609
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
610
  
611
  boxplot(rmse~pred_mod,data=tb,ylim=c(0,5),outline=FALSE)#,names=tb$pred_mod)
612
  title("RMSE per model over all tiles")
613
  p<- bwplot(rmse~pred_mod | reg, data=tb,ylim=c(0,5),
614
             main="RMSE per model and region over all tiles")
615
  print(p)
616
  dev.off()
617
  
618
  #####################################################
619
  #### Figure 9: plotting mosaics of regions ###########
620
  ## plot mosaics...
621
  
622
  #First collect file names
623
  
624
  
625
  #names(lf_mosaics_reg) <- l_reg_name
626
  
627
  #This part should be automated...
628
  #plot Australia
629
  #lf_m <- lf_mosaics_reg[[2]]
630
  #out_dir_str <- out_dir
631
  #reg_name <- "reg6_1000x3000"
798 632
  #lapply()
799
#  list_param_plot_daily_mosaics <- list(lf_m=lf_m,reg_name=tile_size,out_dir_str=out_dir_str,out_suffix=out_suffix,l_dates=day_to_mosaic)
633
  #list_param_plot_daily_mosaics <- list(lf_m=lf_m,reg_name=reg_name,out_dir_str=out_dir_str,out_suffix=out_suffix)
634
  #list_param_plot_daily_mosaics <- list(lf_m=lf_m,reg_name=reg_name,out_dir_str=out_dir_str,out_suffix=out_suffix,l_dates=day_to_mosaic)
635
  
800 636
  #lf_m_mask_reg4_1500x4500 <- mclapply(1:2,FUN=plot_daily_mosaics,list_param=list_param_plot_daily_mosaics,mc.preschedule=FALSE,mc.cores = 6)
801

  
802
  #lf_world_mask_reg[[i]] <- mclapply(1:length(lf_m),FUN=plot_daily_mosaics,list_param=list_param_plot_daily_mosaics,mc.preschedule=FALSE,mc.cores = 10)
803
}
804

  
805
############# NEW MASK AND DATA
806
## Plot areas and day predicted as first check
637
  #debug(plot_daily_mosaics)
638
  #lf_m_mask_reg6_1000x3000 <- plot_daily_mosaics(1,list_param=list_param_plot_daily_mosaics)
639
  
640
  #lf_m_mask_reg6_1000x3000 <- mclapply(1:length(lf_m),FUN=plot_daily_mosaics,list_param=list_param_plot_daily_mosaics,mc.preschedule=FALSE,mc.cores = 10)
641
  
642
  
643
  ################## WORLD MOSAICS NEEDS MAJOR CLEAN UP OF CODE HERE
644
  ##make functions!!
645
  ###Combine mosaics with modified code from Alberto
646
  
647
  #use list from above!!
648
  
649
  # test_list <-list.files(path=file.path(out_dir,"mosaics"),    
650
  #            pattern=paste("^world_mosaics.*.tif$",sep=""), 
651
  # )
652
  # #world_mosaics_mod1_output1500x4500_km_20101105_run10_1500x4500_global_analyses_03112015.tif
653
  # 
654
  # #test_list<-lapply(1:30,FUN=function(i){lapply(1:x[[i]]},x=lf_mosaics_mask_reg)
655
  # #test_list<-unlist(test_list)
656
  # #mosaic_list_mean <- vector("list",length=1)
657
  # mosaic_list_mean <- test_list 
658
  # out_rastnames <- "world_test_mosaic_20100101"
659
  # out_path <- out_dir
660
  # 
661
  # list_param_mosaic<-list(mosaic_list_mean,out_path,out_rastnames,file_format,NA_flag_val,out_suffix)
662
  # names(list_param_mosaic)<-c("mosaic_list","out_path","out_rastnames","file_format","NA_flag_val","out_suffix")
663
  # #mean_m_list <-mclapply(1:12, list_param=list_param_mosaic, mosaic_m_raster_list,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
664
  # 
665
  # lf <- mosaic_m_raster_list(1,list_param_mosaic)
666
  # 
667
  # debug(mosaic_m_raster_list)
668
  # mosaic_list<-list_param$mosaic_list
669
  # out_path<-list_param$out_path
670
  # out_names<-list_param$out_rastnames
671
  # file_format <- list_param$file_format
672
  # NA_flag_val <- list_param$NA_flag_val
673
  # out_suffix <- list_param$out_suffix
674
  
675
  ##Now mosaic for mean: should reorder files!!
676
  #out_rastnames_mean<-paste("_",lst_pat,"_","mean",out_suffix,sep="")
677
  #list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
678
  #mosaic_list<-split(tmp_str1,list_date_names)
679
  #new_list<-vector("list",length(mosaic_list))
680
  # for (i in 1:length(list_date_names)){
681
  #    j<-grep(list_date_names[i],mosaic_list,value=FALSE)
682
  #    names(mosaic_list)[j]<-list_date_names[i]
683
  #    new_list[i]<-mosaic_list[j]
684
  #  }
685
  #  mosaic_list_mean <-new_list #list ready for mosaicing
686
  #  out_rastnames_mean<-paste(list_date_names,out_rastnames_mean,sep="")
687
  
688
  ### Now mosaic tiles...Note that function could be improved to use less memory
689
  
690
  
691
  ################## PLOTTING WORLD MOSAICS ################
692
  
693
  #lf_world_pred <-list.files(path=file.path(out_dir,"mosaics"),    
694
  #           pattern=paste("^world_mosaics.*.tif$",sep=""),full.names=T) 
695
  
696
  lf_world_pred <-list.files(path=file.path(out_dir,"mosaics"),    
697
                             pattern=paste("^reg5.*.",".tif$",sep=""),full.names=T) 
698
  l_reg_name <- unique(df_tile_processed$reg)
699
  lf_world_pred <-list.files(path=file.path(out_dir,l_reg_name[[i]]),    
700
                             pattern=paste(".tif$",sep=""),full.names=T) 
807 701
  
808
l_reg_name <- unique(df_tile_processed$reg)
809
#(subset(df_tile_processed$reg == l_reg_name[i],date)
810

  
811
for(i in 1:length(l_reg_name)){
812
  lf_world_pred<-list.files(path=file.path(out_dir,l_reg_name[[i]]),    
813
           pattern=paste(".tif$",sep=""),full.names=T) 
814

  
815 702
  #mosaic_list_mean <- test_list 
816 703
  #out_rastnames <- "world_test_mosaic_20100101"
817 704
  #out_path <- out_dir
818

  
705
  
819 706
  #lf_world_pred <- list.files(pattern="world.*2010090.*.tif$")
820 707
  #lf_raster_fname <- list.files(pattern="world.*2010*.*02162015.tif$",full.names=T)
821 708
  lf_raster_fname <- lf_world_pred
822
  prefix_str <- paste("Figure10_",l_reg_name[i],sep="")
823

  
824
  l_dates <- basename(lf_raster_fname)
825
  tmp_name <- gsub(".tif","",l_dates)
826
  tmp_name <- gsub("gam_CAI_dailyTmax_predicted_mod1_0_1_","",tmp_name)
827
  #l_dates <- tmp_name
828
  l_dates <- paste(l_reg_name[i],"_",tmp_name,sep="")
829

  
830
  screenRast=TRUE
709
  prefix_str <- "Figure10_clim_world_mosaics_day_"
710
  l_dates <-day_to_mosaic
711
  screenRast=FALSE
831 712
  list_param_plot_screen_raster <- list(lf_raster_fname,screenRast,l_dates,out_dir,prefix_str,out_suffix)
832 713
  names(list_param_plot_screen_raster) <- c("lf_raster_fname","screenRast","l_dates","out_dir_str","prefix_str","out_suffix_str")
833

  
834
  #undebug(plot_screen_raster_val)
835

  
836
  #world_m_list1<- plot_screen_raster_val(1,list_param_plot_screen_raster)
714
  
715
  debug(plot_screen_raster_val)
716
  
717
  world_m_list1<- plot_screen_raster_val(1,list_param_plot_screen_raster)
837 718
  #world_m_list <- mclapply(11:30, list_param=list_param_plot_screen_raster, plot_screen_raster_val,mc.preschedule=FALSE,mc.cores = num_cores) #This is the end bracket from mclapply(...) statement
838 719
  world_m_list <- mclapply(1:length(l_dates), list_param=list_param_plot_screen_raster, plot_screen_raster_val,mc.preschedule=FALSE,mc.cores = num_cores) #This is the end bracket from mclapply(...) statement
720
  
721
  s_pred <- stack(lf_raster_fname)
722
  
723
  res_pix <- 1500
724
  col_mfrow <- 3 
725
  row_mfrow <- 2
726
  
727
  png(filename=paste("Figure10_levelplot_combined_",region_name,"_",out_suffix,".png",sep=""),
728
      width=col_mfrow*res_pix,height=row_mfrow*res_pix)
729
  
730
  levelplot(s_pred,layers=1:6,col.regions=rev(terrain.colors(255)),cex=4)
731
  
732
  dev.off()
733
  
734
  #   blues<- designer.colors(6, c( "blue", "white") )
735
  # reds <- designer.colors(6, c( "white","red")  )
736
  # colorTable<- c( blues[-6], reds)
737
  # breaks with a gap of 10 to 17 assigned the white color
738
  # brks<- c(seq( 1, 10,,6), seq( 17, 25,,6)) 
739
  # image.plot( x,y,z,breaks=brks, col=colorTable)
740
  #
741
  
742
  #lf_world_mask_reg <- vector("list",length=length(lf_world_pred))
743
  #for(i in 1:length(lf_world_pred)){
744
  #
745
  #  lf_m <- lf_mosaics_reg[i]
746
  #  out_dir_str <- out_dir
747
  #reg_name <- paste(l_reg_name[i],"_",tile_size,sep="") #make this automatic
748
  #lapply()
749
  #  list_param_plot_daily_mosaics <- list(lf_m=lf_m,reg_name=tile_size,out_dir_str=out_dir_str,out_suffix=out_suffix,l_dates=day_to_mosaic)
750
  #lf_m_mask_reg4_1500x4500 <- mclapply(1:2,FUN=plot_daily_mosaics,list_param=list_param_plot_daily_mosaics,mc.preschedule=FALSE,mc.cores = 6)
751
  
752
  #lf_world_mask_reg[[i]] <- mclapply(1:length(lf_m),FUN=plot_daily_mosaics,list_param=list_param_plot_daily_mosaics,mc.preschedule=FALSE,mc.cores = 10)
753
  }
839 754

  
840
  #s_pred <- stack(lf_raster_fname)
841

  
842
  #res_pix <- 1500
843
  #col_mfrow <- 3 
844
  #row_mfrow <- 2
845

  
846
  #png(filename=paste("Figure10_levelplot_combined_",region_name,"_",out_suffix,".png",sep=""),
847
  #  width=col_mfrow*res_pix,height=row_mfrow*res_pix)
755
  ############# NEW MASK AND DATA
756
  ## Plot areas and day predicted as first check
848 757

  
849
  #levelplot(s_pred,layers=1:6,col.regions=rev(terrain.colors(255)),cex=4)
758
  l_reg_name <- unique(df_tile_processed$reg)
759
  #(subset(df_tile_processed$reg == l_reg_name[i],date)
850 760

  
851
  #dev.off()
761
  for(i in 1:length(l_reg_name)){
762
    lf_world_pred<-list.files(path=file.path(out_dir,l_reg_name[[i]]),    
763
                            pattern=paste(".tif$",sep=""),full.names=T) 
764
  
765
    #mosaic_list_mean <- test_list 
766
    #out_rastnames <- "world_test_mosaic_20100101"
767
    #out_path <- out_dir
768
    
769
    #lf_world_pred <- list.files(pattern="world.*2010090.*.tif$")
770
    #lf_raster_fname <- list.files(pattern="world.*2010*.*02162015.tif$",full.names=T)
771
    lf_raster_fname <- lf_world_pred
772
    prefix_str <- paste("Figure10_",l_reg_name[i],sep="")
773
    
774
    l_dates <- basename(lf_raster_fname)
775
    tmp_name <- gsub(".tif","",l_dates)
776
    tmp_name <- gsub("gam_CAI_dailyTmax_predicted_mod1_0_1_","",tmp_name)
777
    #l_dates <- tmp_name
778
    l_dates <- paste(l_reg_name[i],"_",tmp_name,sep="")
779
    
780
    screenRast=TRUE
781
    list_param_plot_screen_raster <- list(lf_raster_fname,screenRast,l_dates,out_dir,prefix_str,out_suffix)
782
    names(list_param_plot_screen_raster) <- c("lf_raster_fname","screenRast","l_dates","out_dir_str","prefix_str","out_suffix_str")
783
    
784
    #undebug(plot_screen_raster_val)
785
    
786
    #world_m_list1<- plot_screen_raster_val(1,list_param_plot_screen_raster)
787
    #world_m_list <- mclapply(11:30, list_param=list_param_plot_screen_raster, plot_screen_raster_val,mc.preschedule=FALSE,mc.cores = num_cores) #This is the end bracket from mclapply(...) statement
788
    world_m_list <- mclapply(1:length(l_dates), list_param=list_param_plot_screen_raster, plot_screen_raster_val,mc.preschedule=FALSE,mc.cores = num_cores) #This is the end bracket from mclapply(...) statement
789
    
790
    #s_pred <- stack(lf_raster_fname)
791
    
792
    #res_pix <- 1500
793
    #col_mfrow <- 3 
794
    #row_mfrow <- 2
795
    
796
    #png(filename=paste("Figure10_levelplot_combined_",region_name,"_",out_suffix,".png",sep=""),
797
    #  width=col_mfrow*res_pix,height=row_mfrow*res_pix)
798
    
799
    #levelplot(s_pred,layers=1:6,col.regions=rev(terrain.colors(255)),cex=4)
800
    
801
    #dev.off()
852 802
}
853 803

  
854 804

  

Also available in: Unified diff