Revision e3f7465b
Added by Benoit Parmentier almost 9 years ago
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: 01/02/2016
|
|
9 |
#Version: 4
|
|
8 |
#MODIFIED ON: 01/03/2016
|
|
9 |
#Version: 5
|
|
10 | 10 |
#PROJECT: Environmental Layers project |
11 | 11 |
#TO DO: |
12 | 12 |
# - |
... | ... | |
60 | 60 |
#16) multiple_region <- TRUE #PARAM 12 |
61 | 61 |
#17) countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too |
62 | 62 |
#18) plot_region <- TRUE |
63 |
#19) reg_modified <- TRUE #PARAM 15 |
|
64 | 63 |
#20) threshold_missing_day <- c(367,365,300,200) #PARM18 |
65 | 64 |
##OUTPUTS |
66 | 65 |
#1) |
... | ... | |
110 | 109 |
|
111 | 110 |
|
112 | 111 |
in_dir1 <- list_param_run_assessment_prediction$in_dir1 |
113 |
region_names <- list_param_run_assessment_prediction$region_names #e.g. c("reg23","reg4")
|
|
112 |
region_name <- list_param_run_assessment_prediction$region_name #e.g. c("reg23","reg4") #run only for one region
|
|
114 | 113 |
y_var_name <- list_param_run_assessment_prediction$y_var_name # e.g. dailyTmax" #PARAM3 |
115 | 114 |
interpolation_method <- list_param_run_assessment_prediction$interpolation_method #c("gam_CAI") #PARAM4 |
116 | 115 |
out_prefix <- list_param_run_assessment_prediction$out_prefix #output suffix e.g."run_global_analyses_pred_12282015" #PARAM5 |
... | ... | |
130 | 129 |
multiple_region <- list_param_run_assessment_prediction$multiple_region #PARAM16 |
131 | 130 |
countries_shp <- list_param_run_assessment_prediction$countries_shp #PARAM17 |
132 | 131 |
plot_region <- list_param_run_assessment_prediction$plot_region #PARAM18 |
133 |
reg_modified <- list_param_run_assessment_prediction$reg_modified #PARAM19 |
|
134 | 132 |
threshold_missing_day <- list_param_run_assessment_prediction$threshold_missing_day #PARM20 |
135 | 133 |
|
136 | 134 |
########################## START SCRIPT ######################################### |
... | ... | |
194 | 192 |
|
195 | 193 |
##raster_prediction object : contains testing and training stations with RMSE and model object |
196 | 194 |
in_dir_list_tmp <- file.path(in_dir_list,year_predicted) |
197 |
list_raster_obj_files <- lapply(in_dir_list_tmp,FUN=function(x){list.files(path=x,pattern="^raster_prediction_obj.*.RData",full.names=T)}) |
|
198 |
|
|
195 |
list_raster_obj_files <- try(lapply(in_dir_list_tmp,FUN=function(x){list.files(path=x,pattern="^raster_prediction_obj.*.RData",full.names=T)})) |
|
196 |
#Add stop message here...if no raster object in any tiles then break from the function |
|
197 |
|
|
199 | 198 |
list_names_tile_coord <- lapply(list_raster_obj_files,FUN=function(x){basename(dirname(x))}) |
200 | 199 |
list_names_tile_id <- paste("tile",1:length(list_raster_obj_files),sep="_") |
201 | 200 |
names(list_raster_obj_files)<- list_names_tile_id |
... | ... | |
225 | 224 |
lf_sub_sampling_obj_daily_files_tmp <- lapply(1:length(lf_sub_sampling_obj_daily_files),FUN=function(i,x){val <- x[[i]];if(length(val)==0){val<-0};val},x=lf_sub_sampling_obj_daily_files) |
226 | 225 |
df_tile_processed$sub_sampling_clim <- unlist(lf_sub_sampling_obj_files_tmp) |
227 | 226 |
df_tile_processed$sub_sampling_daily <- unlist(lf_sub_sampling_obj_daily_files_tmp) |
227 |
##review this part!! |
|
228 |
df_tile_processed$reg <- region_name #eg c("reg4) #should only be one char string |
|
229 |
df_tile_processed$year_predicted <- year_predicted #eg c("reg4) #should only be one char string |
|
228 | 230 |
#lf_sub_sampling_obj_files |
229 |
|
|
230 | 231 |
|
231 | 232 |
################ |
232 | 233 |
#### Table 1: Average accuracy metrics per tile and predictions |
... | ... | |
259 | 260 |
long<- as.numeric(lapply(1:length(tx),function(i,x){x[[i]][2]},x=tx)) |
260 | 261 |
summary_metrics_v_NA$lat <- lat |
261 | 262 |
summary_metrics_v_NA$lon <- long |
263 |
summary_metrics_v_NA$reg <- region_name #add region name |
|
264 |
summary_metrics_v_NA$year_predicted <- year_predicted #add year predicted |
|
262 | 265 |
|
263 | 266 |
write.table(as.data.frame(summary_metrics_v_NA), |
264 | 267 |
file=file.path(out_dir,paste("summary_metrics_v2_NA_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
... | ... | |
280 | 283 |
FUN=function(i,x,y){rep(y[i],nrow(x[[i]]))},x=tb_diagnostic_v_tmp,y=names(tb_diagnostic_v_tmp)) |
281 | 284 |
|
282 | 285 |
tb_diagnostic_v_NA$tile_id <- unlist(tile_id_tmp) #adding identifier for tile |
283 |
|
|
284 | 286 |
tb_diagnostic_v_NA <- merge(tb_diagnostic_v_NA,df_tile_processed[,1:2],by="tile_id") |
287 |
tb_diagnostic_v_NA$reg <- region_name #add region name |
|
288 |
tb_diagnostic_v_NA$year_predicted <- year_predicted #add year |
|
285 | 289 |
|
286 | 290 |
write.table((tb_diagnostic_v_NA), |
287 | 291 |
file=file.path(out_dir,paste("tb_diagnostic_v_NA_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
... | ... | |
307 | 311 |
tb_month_diagnostic_s_NA <- merge(tb_month_diagnostic_s_NA,df_tile_processed[,1:2],by="tile_id") |
308 | 312 |
|
309 | 313 |
date_f<-strptime(tb_month_diagnostic_s_NA$date, "%Y%m%d") # interpolation date being processed |
310 |
tb_month_diagnostic_s_NA$month<-strftime(date_f, "%m") # current month of the date being processed |
|
311 |
|
|
314 |
tb_month_diagnostic_s_NA$month <-strftime(date_f, "%m") # current month of the date being processed |
|
315 |
tb_month_diagnostic_s_NA$reg <- region_name #add region name |
|
316 |
tb_month_diagnostic_s_NA$year_predicted <- year_predicted #add year |
|
317 |
|
|
312 | 318 |
write.table((tb_month_diagnostic_s_NA), |
313 | 319 |
file=file.path(out_dir,paste("tb_month_diagnostic_s_NA_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
314 | 320 |
|
... | ... | |
332 | 338 |
tb_diagnostic_s_NA$tile_id <- unlist(tile_id_tmp) #adding identifier for tile |
333 | 339 |
|
334 | 340 |
tb_diagnostic_s_NA <- merge(tb_diagnostic_s_NA,df_tile_processed[,1:2],by="tile_id") |
335 |
|
|
341 |
tb_diagnostic_s_NA$reg <- region_name #add region name |
|
342 |
tb_diagnostic_s_NA$year_predicted <- year_predicted #add year |
|
343 |
|
|
336 | 344 |
write.table((tb_diagnostic_s_NA), |
337 | 345 |
file=file.path(out_dir,paste("tb_diagnostic_s_NA_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
338 | 346 |
list_outfiles[[4]] <- file.path(out_dir,paste("tb_diagnostic_s_NA_",year_predicted,"_",out_prefix,".txt",sep="")) |
... | ... | |
358 | 366 |
FUN=function(i,x){rep(names(x)[i],nrow(x[[i]]))},x=data_month_tmp) |
359 | 367 |
data_month_NAM <- do.call(rbind.fill,data_month_tmp) #combined data_month for "NAM" North America |
360 | 368 |
data_month_NAM$tile_id <- unlist(tile_id) |
361 |
|
|
369 |
data_month_NAM$reg <- region_name #add region name |
|
370 |
data_month_NAM$year_predicted <- year_predicted #add year |
|
371 |
|
|
362 | 372 |
write.table((data_month_NAM), |
363 | 373 |
file=file.path(out_dir,paste("data_month_s_NAM_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
364 | 374 |
list_outfiles[[5]] <- file.path(out_dir,paste("data_month_s_NAM_",year_predicted,"_",out_prefix,".txt",sep="")) |
... | ... | |
380 | 390 |
FUN=function(i,x){rep(names(x)[i],nrow(x[[i]]))},x=data_day_s_tmp) |
381 | 391 |
data_day_s_NAM <- do.call(rbind.fill,data_day_s_tmp) #combined data_month for "NAM" North America |
382 | 392 |
data_day_s_NAM$tile_id <- unlist(tile_id) |
393 |
data_day_s_NAM$reg <- region_name #add region name |
|
394 |
data_day_s_NAM$year_predicted <- year_predicted #add year predicted |
|
383 | 395 |
|
384 | 396 |
tile_id <- lapply(1:length(data_day_v_tmp), |
385 | 397 |
FUN=function(i,x){rep(names(x)[i],nrow(x[[i]]))},x=data_day_v_tmp) |
386 | 398 |
data_day_v_NAM <- do.call(rbind.fill,data_day_v_tmp) #combined data_month for "NAM" North America |
387 | 399 |
data_day_v_NAM$tile_id <- unlist(tile_id) |
400 |
data_day_v_NAM$reg <- region_name #add region name |
|
401 |
data_day_v_NAM$year_predicted <- year_predicted #add year predicted |
|
388 | 402 |
|
389 | 403 |
write.table((data_day_s_NAM), |
390 | 404 |
file=file.path(out_dir,paste("data_day_s_NAM_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
... | ... | |
419 | 433 |
FUN=function(i,x){try(rep(names(x)[i],nrow(x[[i]])))},x=data_month_v_subsampling_tmp) |
420 | 434 |
data_month_v_subsmapling_NAM <- do.call(rbind.fill,ddata_month_v_subsampling_tmp) #combined data_month for "NAM" North America |
421 | 435 |
data_month_v_subsampling_NAM$tile_id <- unlist(tile_id) |
436 |
data_month_v_subsampling_NAM$reg <- reg |
|
437 |
data_month_v_subsampling_NAM$year_predicted <- year_predicted |
|
438 |
|
|
422 | 439 |
write.table((data_month_v_subsampling_NAM), |
423 | 440 |
file=file.path(out_dir,paste("data_month_v_subsampling_NAM_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
424 | 441 |
list_outfiles[[8]] <- file.path(out_dir,paste("data_month_v_subsampling_NAM_",year_predicted,"_",out_prefix,".txt",sep="")) |
... | ... | |
478 | 495 |
pred_data_month_info <- do.call(rbind,lapply(pred_data_info_tmp,function(x){x$pred_data_month_info})) |
479 | 496 |
pred_data_day_info <- do.call(rbind,lapply(pred_data_info_tmp,function(x){x$pred_data_day_info})) |
480 | 497 |
|
498 |
pred_data_month_info$reg <- region_name |
|
499 |
pred_data_day_info$reg <- region_name |
|
500 |
pred_data_month_info$year_predicted <- year_predicted |
|
501 |
pred_data_day_info$year_predicted <- year_predicted |
|
502 |
|
|
481 | 503 |
#putput inforamtion in csv !! |
482 | 504 |
write.table(pred_data_month_info, |
483 | 505 |
file=file.path(out_dir,paste("pred_data_month_info_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",") |
... | ... | |
502 | 524 |
df_tiles_all$tile_coord <- l_shp |
503 | 525 |
#names(df_tiles_all) <- "list_shp_world" |
504 | 526 |
names(df_tiles_all) <- c("shp_files","tile_coord") |
527 |
#add tiles id |
|
528 |
df_tiles_all$reg <- region_name |
|
529 |
df_tiles_all$year_predicted <- year_predicted |
|
530 |
|
|
505 | 531 |
matching_index <- match(basename(in_dir_list),l_shp) |
506 | 532 |
list_shp_reg_files <- list_shp_world[matching_index] |
507 | 533 |
df_tile_processed$shp_files <-list_shp_reg_files |
... | ... | |
511 | 537 |
long<- as.numeric(lapply(1:length(tx),function(i,x){x[[i]][2]},x=tx)) |
512 | 538 |
df_tile_processed$lat <- lat |
513 | 539 |
df_tile_processed$lon <- long |
540 |
|
|
514 | 541 |
|
515 | 542 |
#put that list in the df_processed and also the centroids!! |
516 | 543 |
write.table(df_tile_processed, |
... | ... | |
543 | 570 |
df_assessment_files <- data.frame(filename=outfiles_names,files=unlist(list_outfiles), |
544 | 571 |
reg=region_name,year=year_predicted) |
545 | 572 |
###Prepare files for copying back? |
573 |
df_assessment_files_name <- file.path(out_dir,paste("df_assessment_files_",region_name,"_",year_predicted,"_",out_prefix,".txt",sep="")) |
|
546 | 574 |
write.table(df_assessment_files, |
547 |
file=file.path(out_dir,paste("df_assessment_files_",region_name,"_",year_predicted,"_",out_prefix,".txt",sep="")),sep=",")
|
|
575 |
file=df_assessment_files_name,sep=",")
|
|
548 | 576 |
|
549 | 577 |
###################################################### |
550 | 578 |
####### PART 5: run plotting functions to produce figures |
... | ... | |
554 | 582 |
#interpolation_method <- c("gam_CAI") #PARAM2, already set |
555 | 583 |
out_suffix <- out_prefix #PARAM3 |
556 | 584 |
#out_dir <- #PARAM4, already set |
557 |
#create_out_dir_param <- FALSE #PARAM 5, already created and set
|
|
585 |
create_out_dir_param <- FALSE #PARAM 5, already created and set |
|
558 | 586 |
#mosaic_plot <- FALSE #PARAM6 |
559 | 587 |
#if daily mosaics NULL then mosaicas all days of the year |
560 | 588 |
#day_to_mosaic <- c("19920101","19920102","19920103") #PARAM7 |
561 | 589 |
#CRS_locs_WGS84 already set |
562 |
proj_str<- CRS_WGS84 #PARAM 8 #check this parameter |
|
590 |
proj_str<- CRS_locs_WGS84 #PARAM 8 #check this parameter
|
|
563 | 591 |
#file_format <- ".rst" #PARAM 9, already set |
564 | 592 |
#NA_flag_val <- -9999 #PARAM 11, already set |
565 | 593 |
#multiple_region <- TRUE #PARAM 12 |
566 | 594 |
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too |
567 | 595 |
#plot_region <- TRUE |
568 | 596 |
#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 |
|
597 |
#region_name <- c("reg4") #reference region to merge if necessary, if world all the regions are together #PARAM 16
|
|
570 | 598 |
#use previous files produced in step 1a and stored in a data.frame |
571 |
#df_assessment_files, #PARAM 17, set in the script
|
|
599 |
#df_assessment_files_name <- "df_assessment_files_reg4_1984_run_global_analyses_pred_12282015.txt"# #PARAM 17, set in the script
|
|
572 | 600 |
#threshold_missing_day <- c(367,365,300,200) #PARM18 |
573 | 601 |
|
574 |
list_param_run_assessment_plotting <- list(y_var_name, interpolation_method, out_suffix, |
|
602 |
list_param_run_assessment_plotting <- list(in_dir,y_var_name, interpolation_method, out_suffix,
|
|
575 | 603 |
out_dir, create_out_dir_param, mosaic_plot, proj_str, file_format, NA_value, |
576 | 604 |
multiple_region, countries_shp, plot_region, num_cores, |
577 |
region_name, df_assessment_files, threshold_missing_day) |
|
605 |
region_name, df_assessment_files_name, threshold_missing_day)
|
|
578 | 606 |
|
579 |
names(list_param_run_assessment_plotting) <- c("y_var_name","interpolation_method","out_suffix", |
|
607 |
names(list_param_run_assessment_plotting) <- c("in_dir","y_var_name","interpolation_method","out_suffix",
|
|
580 | 608 |
"out_dir","create_out_dir_param","mosaic_plot","proj_str","file_format","NA_value", |
581 | 609 |
"multiple_region","countries_shp","plot_region","num_cores", |
582 |
"region_name","df_assessment_files","threshold_missing_day") |
|
610 |
"region_name","df_assessment_files_name","threshold_missing_day") |
|
611 |
|
|
612 |
#function_assessment_part2 <- "global_run_scalingup_assessment_part2_01032016.R" |
|
613 |
#source(file.path(script_path,function_assessment_part2)) #source all functions used in this script |
|
583 | 614 |
|
615 |
#debug(run_assessment_plotting_prediction_fun) |
|
584 | 616 |
df_assessment_figures_files <- run_assessment_plotting_prediction_fun(list_param_run_assessment_plotting) |
585 | 617 |
|
586 | 618 |
###################################################### |
Also available in: Unified diff
integrating plotting call into assessement part1