Project

General

Profile

« Previous | Next » 

Revision 14e1509b

Added by Benoit Parmentier over 8 years ago

moving plot_stations_val_by_date to function script product assessment part1

View differences:

climate/research/oregon/interpolation/global_product_assessment_part1.R
4 4
#Combining tables and figures for individual runs for years and tiles.
5 5
#AUTHOR: Benoit Parmentier 
6 6
#CREATED ON: 05/15/2016  
7
#MODIFIED ON: 09/02/2016            
7
#MODIFIED ON: 09/04/2016            
8 8
#Version: 1
9 9
#PROJECT: Environmental Layers project     
10 10
#COMMENTS: Initial commit, script based on part NASA biodiversity conferenc 
......
85 85
source(file.path(script_path,function_assessment_part3)) #source all functions used in this script 
86 86

  
87 87
#Product assessment
88
function_product_assessment_part1_functions <- "global_product_assessment_part1_functions_08312016.R"
88
function_product_assessment_part1_functions <- "global_product_assessment_part1_functions_09042016.R"
89 89
source(file.path(script_path,function_product_assessment_part1_functions)) #source all functions used in this script 
90 90

  
91 91
###############################
......
304 304
                                                 station_type_name","model_name",
305 305
                                                 "var_selected","y_var_name","out_suffix","out_dir")
306 306
  
307
debug(plot_stations_val_by_date)
308
test<- plot_stations_val_by_date(1,list_param = list_param_plot_stations_val_by_date)
309

  
310
plot_stations_val_by_date <- function(i,list_param){
311
  #
312
  #
313
  #function to plot residuals by date
314
  #
315
  
316
  #####
317
  
318
  l_dates <- list_param$l_dates
319
  model_name <- list_param$model_name
320
  station_type_name <- list_param$station_type_name
321
  var_selected <- list_param$var_selected
322
  #list_df_points_dates <- 
323
  df_combined_data_points <- list_param$df_combined_data_points
324
  countries_shp <- list_param$countries_shp
325
  proj_str <- list_param$proj_str
326
  out_suffix <- list_param$out_suffix
327
  out_dir <- list_param$out_dir
328
  
329
  ### STARTFUNCTION ####
330
    
331
  date_processed <- l_dates[i]
332
  
333
  df_points <- subset(df_combined_data_points,date==date_processed)
334
  #df_points <- list_df_points_dates[[i]]
335
  #plot(df_points)
336
  freq_tile_id <- as.data.frame(table(df_points$tile_id))
337
  freq_tile_id$date <- date_processed
338
  names(freq_tile_id) <- c("tile_id","freq","date")
339
    
340
  #plot(df_points,add=T)
341
  coordinates(df_points) <- cbind(df_points$x,df_points$y)
342
  #proj4string(df_points) <- CRS_locs_WGS84
343
  proj4string(df_points) <- proj_str
344
  # # No spatial duplicates
345
  df_points_day <- remove.duplicates(df_points) #remove duplicates...
346
  #plot(df_points_day)
347
  #dim(df_points_day)
348
  #dim(df_points)
349
  #plot(df_points)
350
  
351
  ##layer to be clipped
352
  if(class(countries_shp)!="SpatialPolygonsDataFrame"){
353
    reg_layer <- readOGR(dsn=dirname(countries_shp),sub(".shp","",basename(countries_shp)))
354
  }else{
355
    reg_layer <- countries_shp
356
  }
357

  
358
  #extent_df_points_day <- extent(df_points_day)
359
  
360
  reg_layer_clipped <- gClip(shp=reg_layer, bb=df_points_day , keep.attribs=TRUE,outDir=NULL,outSuffix=NULL)
361
  
362
  #data_stations_var_pred <- aggregate(id ~ date, data = data_stations, min)
363
  #data_stations_var_pred <- aggregate(id ~ x + y + date + dailyTmax + mod1 + res_mod1 , data = data_stations, min)
364
  
365
  #change the formula later on to use different y_var_name (tmin and precip)
366
  data_stations_var_pred <- aggregate(id ~ x + y + date + dailyTmax + res_mod1 + tile_id + reg ,data = df_points, mean ) #+ mod1 + res_mod1 , data = data_stations, min)
367
  dim(data_stations_var_pred)
368
  #> dim(data_stations_var_pred)
369
  #[1] 11171     5
370

  
371
  data_stations_var_pred$date_str <- data_stations_var_pred$date
372
  data_stations_var_pred$date <- as.Date(strptime(data_stations_var_pred$date_str,"%Y%m%d"))
373
  coordinates(data_stations_var_pred) <- cbind(data_stations_var_pred$x,data_stations_var_pred$y)
374
  #data_stations_var_pred$constant <- c(1000,2000)
375
  #plot(data_stations_var_pred)
376
  #plot(reg_layer)
377
  #res_pix <- 1200
378
  res_pix <- 800
379
  col_mfrow <- 1
380
  row_mfrow <- 1
381
  
382
  png_filename <- paste("Figure_ac_metrics_map_stations_l_",station_type_name,"_",model_name,"_",y_var_name,"_",date_processed,
383
                       "_",out_suffix,".png",sep="")
384
  png(filename=png_filename,
385
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
386
  #plot(data_stations_var_pred
387
  #p_shp <- spplot(reg_layer_clipped,"ISO3" ,col.regions=NA, col="black") #ok problem solved!!
388
  #title("(a) Mean for 1 January")
389
  #p <- bubble(data_stations_var_pred,"constant",main=paste0("Average Residuals by validation stations ",
390
  #                                                      date_processed,
391
  #                                                      "for ",var_selected))
392
  #p <- spplot(data_stations_var_pred,"constant",col.regions=NA, col="black",
393
  #            main=paste0("Average Residuals by validation stations ",pch=3,cex=10,
394
  #            date_processed, "for ",var_selected))
395

  
396
  #p1 <- p+p_shp
397
  #try(print(p1)) #error raised if number of missing values below a threshold does not exist
398
  
399
  title_str <- paste0("Stations ",station_type_name," on ",date_processed, " for ",y_var_name," ", var_selected)
400
  plot(reg_layer_clipped,main=title_str)
401
  plot(data_stations_var_pred,add=T,cex=0.5,col="blue")
402
  legend("topleft",legend=paste("n= ", nrow(data_stations_var_pred)),bty = "n")
403
  
404
  dev.off()
405
  
406
  res_pix <- 800
407
  col_mfrow <- 1
408
  row_mfrow <- 1
409
  png_filename <- paste("Figure_ac_metrics_map_stations",station_type_name,"averaged","_fixed_intervals_",model_name,y_var_name,date_processed,
410
                       out_suffix,".png",sep="_")
411
  png(filename=png_filename,
412
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
413
    
414
  #model_name[j]
415
  title_str <- paste0("Average Residuals by ",station_type_name," stations ",date_processed, " for ",y_var_name," ", var_selected)
416
  #p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
417
  p_shp <- spplot(reg_layer,"ISO3" ,col.regions=NA, col="black") #ok problem solved!!
418
  #title("(a) Mean for 1 January")
419
  class_intervals <- c(-20,-10,-5,-4,-3,-2,-1,0,1,2,3,4,5,10,20)
420
  p <- bubble(data_stations_var_pred,zcol="res_mod1",key.entries = class_intervals , 
421
              fill=F, #plot circle with filling
422
              #col= matlab.like(length(class_intervals)),
423
              main=title_str)
424
  p1 <- p + p_shp
425
  try(print(p1)) #error raised if number of missing values below a threshold does not exist
426
  
427
  dev.off()
428 307

  
429
  #### Add additional plot with quantiles and min max?...
430
  
431
  
432
  #library(classInt)
433
  #breaks.qt <- classIntervals(palo_alto$PrCpInc, n = 6, style = "quantile", intervalClosure = "right")
434
  #spplot(palo_alto, "PrCpInc", col = "transparent", col.regions = my.palette, 
435
  #  at = breaks.qt$brks)
436

  
437
  #### histogram of values
438
  res_pix <- 800
439
  col_mfrow <- 1
440
  row_mfrow <- 1
441
  png_filename <- paste("Figure_ac_metrics_histograms_stations",station_type_name,"averaged",model_name,y_var_name,date_processed,
442
                       out_suffix,".png",sep="_")
443
  png(filename=png_filename,
444
        width=col_mfrow*res_pix,height=row_mfrow*res_pix)
445

  
446
  title_str <- paste0("Stations ",station_type_name," on ",date_processed, " for ",y_var_name," ", var_selected)
447

  
448
  h<- histogram(data_stations_var_pred$res_mod1,breaks=seq(-50,50,5),
449
                main=title_str)
450
  
451
  print(h)
452
  dev.off()
453
  
454
  ##Make data.frame with dates for later use!!
455
  #from libary mosaic
456
  df_basic_stat <- fav_stats(data_stations_var_pred$res_mod1)
457
  df_basic_stat$date <- date_processed
458
  #df_basic_stat$reg <- reg
459
  #quantile(data_stations_var_pred$res_mod1,c(1,5,10,90,95,99))
460
  df_quantile_val <- quantile(data_stations_var_pred$res_mod1,c(0.01,0.05,0.10,0.45,0.50,0.55,0.90,0.95,0.99))
461
  #names(df_quantile_val)
462
  #as.list(df_quantile_val)
463
  #df_test <- data.frame(names(df_quantile_val))[numeric(0), ]
464

  
465

  
466
  #quantile(c(1,5,10,90,95,99),data_stations_var_pred$res_mod1,)
467
  #rmse(data_stations_var_pred$res_mod1)
468
  
469
  plot_obj <- list(data_stations_var_pred,df_basic_stat,df_quantile_val,freq_tile_id)
470
  names(plot_obj) <- c("data_stations_var_pred","df_basic_stat","df_quantile_val","freq_tile_id")
471
   
472
  return(plot_obj)
473
}
308
list_plot_obj <- mclapply(1:length(l_dates),FUN=plot_stations_val_by_date,list_param=list_param_plot_stations_val_by_date,
309
                           mc.preschedule=FALSE,
310
                           mc.cores = num_cores)       
311

  
312
#### run for training now
313
station_type_name <- "training"
314
list_param_plot_stations_val_by_date <- list(l_dates,df_combined_data_s_dates,countries_shp_tmp,CRS_locs_WGS84,
315
                                             station_type_name,model_name,
316
                                             var_selected,y_var_name,out_suffix,out_dir)
317
names(list_param_plot_stations_val_by_date) <- c("l_dates","df_combined_data_points","countries_shp","proj_str","
318
                                                 station_type_name","model_name",
319
                                                 "var_selected","y_var_name","out_suffix","out_dir")
320
#debug(plot_stations_val_by_date)
321
#test <- plot_stations_val_by_date(1,list_param = list_param_plot_stations_val_by_date)
474 322

  
323
list_plot_obj_data_s <- mclapply(1:length(l_dates),FUN=plot_stations_val_by_date,list_param=list_param_plot_stations_val_by_date,
324
                           mc.preschedule=FALSE,
325
                           mc.cores = num_cores)   
326
## do training
475 327

  
328
##plot summary of staistics
476 329

  
477 330
############### PART2: Select stations by ID to build a time series #############
478 331
#### Extract corresponding stationsfor given dates and plot stations used

Also available in: Unified diff