Project

General

Profile

« Previous | Next » 

Revision 9d1eff29

Added by Benoit Parmentier about 9 years ago

debugging of function assessment and adding part2 call to make figures

View differences:

climate/research/oregon/interpolation/global_run_scalingup_assessment_part1a.R
5 5
#Part 1 create summary tables and inputs files for figure in part 2 and part 3.
6 6
#AUTHOR: Benoit Parmentier 
7 7
#CREATED ON: 03/23/2014  
8
#MODIFIED ON: 12/31/2015            
8
#MODIFIED ON: 01/02/2016            
9 9
#Version: 4
10 10
#PROJECT: Environmental Layers project  
11 11
#TO DO:
......
33 33

  
34 34

  
35 35
run_assessment_prediction_fun <-function(i,list_param_run_assessment_prediction){
36

  
37
  ##Function to predict temperature interpolation with 12 input parameters
38
  #12 parameters used in the data preparation stage and input in the current script
36
  #This function assesses results from prediction of climate variables. 
37
  #Predictions are run on the NEX-NASA computer by tiles for a given year.
38
  #The function collects information by region and tiles and generate tables of accuracy by tiles/regions.
39
  #There are currently five processing:
40
  #reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia)
41
  ##There are 20 inputs to this function.
39 42
  #
40
  #1) in_dir1 : llcoactation of rroot directory containging tiles
43
  ##INPUTS:
44
  #1) in_dir1 : location of root directory containging tiles
41 45
  #2) region_names : region_names #e.g. c("reg23","reg4") 
42 46
  #3) y_var_name : list_param_run_assessment_prediction$y_var_name # e.g. dailyTmax" #PARAM3
43 47
  #4) interpolation_method :  e.g. #c("gam_CAI") #PARAM4
44 48
  #5) out_prefix : e.g. "run_global_analyses_pred_12282015" #PARAM5
45 49
  #6) out_dir <- list_param_run_assessment_prediction$out_dir #<- "/nobackupp8/bparmen1/" #PARAM6
46 50
  #7) create_out_dir_param: if true a new dirrectory  is craeted for the outputs 
47
  #8) CRS_locs_WGS84: coordinates  CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84, #PARAM8
51
  #8) proj_str: coordinates  CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84, #PARAM8
48 52
  #9) list_year_predicted : list of years predicted e.g. 1984:2004
49 53
  #10) file_format : format for mosaiced files #PARAM10
50 54
  #11) NA_flag_val : No data value, #PARAM11
51 55
  #12) num_cores : number of cores used #PARAM13
56
  #13) plotting_figures: if TRUE, figures are generated from tables using assessment part2 script 
57
  ##Parameters related to assessment part 2: plot functions
58
  #14) mosaic_plot <- FALSE #PARAM6
59
  #15) day_to_mosaic <- c("19920101","19920102","19920103") #PARAM7,#if daily mosaics NULL then mosaic all days of the year
60
  #16) multiple_region <- TRUE #PARAM 12
61
  #17) countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too
62
  #18) plot_region <- TRUE
63
  #19) reg_modified <- TRUE #PARAM 15
64
  #20) threshold_missing_day <- c(367,365,300,200) #PARM18
65
  ##OUTPUTS
66
  #1)
67
  #2)
68
  #3)
69
  #
70
  #
71
  
72
  ###Loading R library and packages   
52 73
  
53
  ###Loading R library and packages     
54 74
  ### Loading R library and packages        
55 75
  #library used in the workflow production:
56 76
  library(gtools)                              # loading some useful tools 
......
88 108
  ##############################
89 109
  #### Parameters and constants  
90 110
  
91
  #Make this a function
92
  #reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia)
93
  #master directory containing the definition of tile size and tiles predicted
111

  
94 112
  in_dir1 <- list_param_run_assessment_prediction$in_dir1 
95 113
  region_names <- list_param_run_assessment_prediction$region_names #e.g. c("reg23","reg4") 
96 114
  y_var_name <- list_param_run_assessment_prediction$y_var_name # e.g. dailyTmax" #PARAM3
97 115
  interpolation_method <- list_param_run_assessment_prediction$interpolation_method #c("gam_CAI") #PARAM4
98
  out_prefix <- list_param_run_assessment_prediction$out_prefix #"run_global_analyses_pred_12282015" #PARAM5
116
  out_prefix <- list_param_run_assessment_prediction$out_prefix #output suffix e.g."run_global_analyses_pred_12282015" #PARAM5
99 117
  out_dir <- list_param_run_assessment_prediction$out_dir #<- "/nobackupp8/bparmen1/" #PARAM6
100
  create_out_dir_param <-list_param_run_assessment_prediction$create_out_dir_param #<- TRUE #PARAM7
101
  CRS_locs_WGS84 <- list_param_run_assessment_prediction$CRS_locs_WGS84 #<- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84, #PARAM8
102
  list_year_predicted <- list_param_run_assessment_prediction$list_year_predicted #<- 1984:2004
103
  #list_param_run_assessment_prediction$year_predicted <- list_year_predicted[1]
118
  create_out_dir_param <-list_param_run_assessment_prediction$create_out_dir_param #if TRUE output dir created #PARAM7
119
  proj_str <- list_param_run_assessment_prediction$proj_str # CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84, #PARAM8
120
  list_year_predicted <- list_param_run_assessment_prediction$list_year_predicted # 1984:2004
104 121
  file_format <- list_param_run_assessment_prediction$file_format #<- ".tif" #format for mosaiced files #PARAM10
105 122
  NA_flag_val <- list_param_run_assessment_prediction$NA_flag_val #<- -9999  #No data value, #PARAM11
106 123
  num_cores <- list_param_run_assessment_prediction$num_cores #<- 6 #number of cores used #PARAM13
124
  plotting_figures <- list_param_run_assessment_prediction$plotting_figures #if true run part2 of assessment
107 125
  
126
  ##for plotting assessment function
127
  
128
  mosaic_plot <- list_param_run_assessment_prediction$mosaic_plot  #PARAM14
129
  day_to_mosaic <- list_param_run_assessment_prediction$day_to_mosaic #PARAM15
130
  multiple_region <- list_param_run_assessment_prediction$multiple_region #PARAM16
131
  countries_shp <- list_param_run_assessment_prediction$countries_shp #PARAM17
132
  plot_region <- list_param_run_assessment_prediction$plot_region #PARAM18
133
  reg_modified <- list_param_run_assessment_prediction$reg_modified #PARAM19
134
  threshold_missing_day <- list_param_run_assessment_prediction$threshold_missing_day #PARM20
135

  
108 136
  ########################## START SCRIPT #########################################
109 137
  
110 138
  #Need to make this a function to run as a job...
......
309 337
              file=file.path(out_dir,paste("tb_diagnostic_s_NA_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",")
310 338
  list_outfiles[[4]] <- file.path(out_dir,paste("tb_diagnostic_s_NA_",year_predicted,"_",out_prefix,".txt",sep=""))
311 339
  
312
  ##### Table 5: Add later on: daily info
313
  ### with also data_s and data_v saved!!!
314
  
315
  #Insert here...compute input and predicted ranges to spot potential errors?
316
  
340
  ##### Table 5: monthly station information used in training
341

  
317 342
  ### Make this part a function...this is repetitive
318
  ##### SPDF of Monhtly Station info
319 343
  #load data_month for specific tiles
320 344
  #10.45pm
321 345
  #data_month <- extract_from_list_obj(robj1$clim_method_mod_obj,"data_month")
......
432 456
  
433 457
  ### Stations and model fitting ###
434 458
  #summarize location and number of training and testing used by tiles
435
  
436 459
  #names(data_month) #this contains LST means (mm_1, mm_2 etc.) as well as TMax and other info
437 460
  
438 461
  use_day=TRUE
......
469 492
  ##### Table 12, Table 13, Table 14: collect location of predictions from shapefiles
470 493

  
471 494
  #get shape files for the region being assessed:
472
  
473 495
  list_shp_world <- list.files(path=in_dir_shp,pattern=".*.shp",full.names=T)
474 496
  l_shp <- gsub(".shp","",basename(list_shp_world))
475 497
  l_shp <- sub("shp_","",l_shp)
......
500 522
  list_outfiles[[13]] <- file.path(out_dir,paste("df_tiles_all_",year_predicted,"_",out_prefix,".txt",sep=""))
501 523
  
502 524
  #Copy to local home directory on NAS-NEX
503
  #
504 525
  dir.create(file.path(out_dir,"shapefiles"))
505 526
  file.copy(list_shp_world,file.path(out_dir,"shapefiles"))
506 527
  
......
509 530
              file=file.path(out_dir,"shapefiles",paste("df_tiles_all_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",")
510 531
  list_outfiles[[14]] <- file.path(out_dir,"shapefiles",paste("df_tiles_all_",year_predicted,"_",out_prefix,".txt",sep=""))
511 532

  
512
  ######################################################
513
  ##### Prepare objet to return ####
514

  
533
  ##############################################################
534
  ############## Collect information from assessment ##########
535
  
515 536
  outfiles_names <- c("summary_metrics_v_names","tb_v_accuracy_name","tb_month_s_name","tb_s_accuracy_name", 
516 537
  "data_month_s_name","data_day_v_name","data_day_s_name","data_month_v_name", "tb_month_v_name",
517 538
  "pred_data_month_info_name","pred_data_day_info_name","df_tile_processed_name","df_tiles_all_name", 
......
525 546
  write.table(df_assessment_files,
526 547
              file=file.path(out_dir,paste("df_assessment_files_",region_name,"_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",")
527 548

  
549
  ######################################################
550
  ####### PART 5: run plotting functions to produce figures
551

  
552
  in_dir <- out_dir #PARAM 0
553
  #y_var_name <- "dailyTmax" #PARAM1 , already set
554
  #interpolation_method <- c("gam_CAI") #PARAM2, already set
555
  out_suffix <- out_prefix #PARAM3
556
  #out_dir <-  #PARAM4, already set
557
  #create_out_dir_param <- FALSE #PARAM 5, already created and set
558
  #mosaic_plot <- FALSE #PARAM6
559
  #if daily mosaics NULL then mosaicas all days of the year
560
  #day_to_mosaic <- c("19920101","19920102","19920103") #PARAM7
561
  #CRS_locs_WGS84 already set
562
  proj_str<- CRS_WGS84 #PARAM 8 #check this parameter
563
  #file_format <- ".rst" #PARAM 9, already set
564
  #NA_flag_val <- -9999 #PARAM 11, already set
565
  #multiple_region <- TRUE #PARAM 12
566
  #countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too
567
  #plot_region <- TRUE
568
  #num_cores <- 6 #PARAM 14, already set
569
  region_name <- c("reg4") #reference region to merge if necessary, if world all the regions are together #PARAM 16
570
  #use previous files produced in step 1a and stored in a data.frame
571
  #df_assessment_files, #PARAM 17, set in the script
572
  #threshold_missing_day <- c(367,365,300,200) #PARM18
573

  
574
  list_param_run_assessment_plotting <- list(y_var_name, interpolation_method, out_suffix, 
575
                      out_dir, create_out_dir_param, mosaic_plot, proj_str, file_format, NA_value,
576
                      multiple_region, countries_shp, plot_region, num_cores, 
577
                      region_name, df_assessment_files, threshold_missing_day) 
578

  
579
  names(list_param_run_assessment_plotting) <- c("y_var_name","interpolation_method","out_suffix", 
580
                      "out_dir","create_out_dir_param","mosaic_plot","proj_str","file_format","NA_value",
581
                      "multiple_region","countries_shp","plot_region","num_cores", 
582
                      "region_name","df_assessment_files","threshold_missing_day") 
583

  
584
  df_assessment_figures_files <- run_assessment_plotting_prediction_fun(list_param_run_assessment_plotting) 
585
  
586
  ######################################################
587
  ##### Prepare objet to return ####
588

  
589
  assessment_obj <- list(df_assessment_files, df_assessment_figures_files)
590
  names(assessment_obj) <- c("df_assessment_files", "df_assessment_figures_files")
528 591
  ## Prepare list of files to return...
529
  return(df_assessment_files)
592
  return(assessment_obj)
530 593
}
531 594

  
532 595
##################### END OF SCRIPT ######################

Also available in: Unified diff