Revision 5e456ead
Added by Benoit Parmentier over 8 years ago
climate/research/oregon/interpolation/global_run_scalingup_mosaicing.R | ||
---|---|---|
283 | 283 |
################### PART I: Accuracy layers by tiles ############# |
284 | 284 |
###Depending on value of layers_option, create accuracy surfaces based on day testing metric (e.g. rmse)... |
285 | 285 |
#Generate accuracy layers using tiles definitions and output from the accuracy assessment |
286 |
|
|
287 |
|
|
288 |
|
|
289 |
#### create a function to generate accuracy layers by tiles |
|
290 |
generate_ac_assessment_layers_by_tile <- function(lf,layers_option,df,df_tile_processed,metric_name, |
|
291 |
var_pred,list_models,use_autokrige,pred_mod_name, |
|
292 |
y_var_name,interpolation_method, |
|
293 |
days_to_process,num_cores,NA_flag_val,file_format, |
|
294 |
out_dir,out_suffix){ |
|
295 |
|
|
296 |
#PARAMETERS: |
|
297 |
#lf |
|
298 |
#layers_option |
|
299 |
#df: can be tb,tb_s, data_v or data_s |
|
300 |
#df_tile_processed |
|
301 |
#metric_name <- "rmse" #RMSE, MAE etc. |
|
302 |
#var_pred: e.g. res_mod1, used for kriging of residuals in res_testing or res_training option |
|
303 |
#list_models: NULL then generate the formula for kriging |
|
304 |
#use_autokrige |
|
305 |
#pred_mod_name <- "mod1" |
|
306 |
#y_var_name |
|
307 |
#interpolation_method #c("gam_CAI") #PARAM3 |
|
308 |
|
|
309 |
#days_to_process <- day_to_mosaic |
|
310 |
#num_cores |
|
311 |
#NA_flag_val <- list_param$NA_flag_val |
|
312 |
#file_format <- list_param$file_format |
|
313 |
#out_dir |
|
314 |
#out_suffix |
|
315 |
# |
|
316 |
|
|
317 |
#OUTPUT |
|
318 |
# |
|
319 |
#add options to clean up file after use!! |
|
320 |
|
|
321 |
############################### |
|
322 |
|
|
323 |
### START ##### |
|
324 |
|
|
325 |
out_dir_str <- out_dir |
|
326 |
out_suffix_str <- out_suffix |
|
327 |
#lf <- lf_mosaic |
|
328 |
|
|
329 |
#Improved by adding multicores option |
|
330 |
num_cores_tmp <- num_cores |
|
331 |
|
|
332 |
if(layers_option=="ac_training" | layers_option=="ac_testing"){ |
|
333 |
|
|
334 |
out_suffix_str <- paste(layers_option,"_",out_suffix,sep="") |
|
335 |
|
|
336 |
list_param_accuracy_metric_raster <- list(lf,df,metric_name,pred_mod_name,y_var_name,interpolation_method, |
|
337 |
days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str,out_suffix_str) |
|
338 |
names(list_param_accuracy_metric_raster) <- c("lf","tb","metric_name","pred_mod_name","y_var_name","interpolation_method", |
|
339 |
"days_to_process","num_cores","NA_flag_val","file_format","out_dir_str","out_suffix_str") |
|
340 |
list_raster_created_obj <- lapply(1:length(days_to_process),FUN=create_accuracy_metric_raster, |
|
341 |
list_param=list_param_accuracy_metric_raster) |
|
342 |
|
|
343 |
#debug(create_accuracy_metric_raster) |
|
344 |
#list_raster_created_obj <- lapply(1:1,FUN=create_accuracy_metric_raster, |
|
345 |
# list_param=list_param_accuracy_metric_raster) |
|
346 |
#raster_created_obj <- create_accuracy_metric_raster(1, list_param_accuracy_metric_raster) |
|
347 |
|
|
348 |
#Extract list of files for rmse and date 1 (19920101), there should be 28 raster images |
|
349 |
lf_accuracy_raster <- lapply(1:length(list_raster_created_obj),FUN=function(i){unlist(list_raster_created_obj[[i]]$list_raster_name)}) |
|
350 |
|
|
351 |
lf_ac_assessment_layers <- lf_accuracy_raster |
|
352 |
|
|
353 |
} |
|
354 |
|
|
355 |
if(layers_option=="res_training" | layers_option=="res_testing"){ |
|
356 |
|
|
357 |
## Create accuracy surface by kriging |
|
358 |
#num_cores_tmp <-num_cores |
|
359 |
#lf_day_tiles <- lf_mosaic #list of raster files by dates |
|
360 |
lf_day_tiles <- lf #list of raster files by dates |
|
361 |
#data_df <- data_day_v # data.frame table/spdf containing stations with residuals and variable |
|
362 |
|
|
363 |
#df_tile_processed #tiles processed during assessment usually by region |
|
364 |
#var_pred #variable being modeled |
|
365 |
#if not list of models is provided generate one |
|
366 |
if(is.null(list_models)){ |
|
367 |
list_models <- paste(var_pred,"~","1",sep=" ") #can krige any variable here |
|
368 |
} |
|
369 |
|
|
370 |
#use_autokrige #if TRUE use automap/gstat package |
|
371 |
#y_var_name #"dailyTmax" #PARAM2 |
|
372 |
#interpolation_method #c("gam_CAI") #PARAM3, need to select reg!! |
|
373 |
#date_processed #can be a monthly layer |
|
374 |
#num_cores #number of cores used |
|
375 |
#NA_flag_val |
|
376 |
#file_format |
|
377 |
#out_dir_str <- out_dir #change to specific separate dir?? |
|
378 |
#out_suffix_str <- out_suffix |
|
379 |
#days_to_process <- day_to_mosaic |
|
380 |
|
|
381 |
#out_suffix_str <- paste("data_day_v_",out_suffix,sep="") |
|
382 |
out_suffix_str <- paste(layers_option,"_",var_pred,"_",out_suffix,sep="") |
|
383 |
#browser() |
|
384 |
df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX) |
|
385 |
df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX)) |
|
386 |
|
|
387 |
##By regions, selected earlier |
|
388 |
#for(k in 1:length(region_names)){ |
|
389 |
df_tile_processed_reg <- subset(df_tile_processed,reg==region_selected)#use reg4 |
|
390 |
#i<-1 #loop by days/date to process!! |
|
391 |
#test on the first day |
|
392 |
list_param_create_accuracy_residuals_raster <- list(lf,df,df_tile_processed_reg, |
|
393 |
var_pred,list_models,use_autokrige,y_var_name,interpolation_method, |
|
394 |
days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str, |
|
395 |
out_suffix_str) |
|
396 |
names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed_reg", |
|
397 |
"var_pred","list_models","use_autokrige","y_var_name","interpolation_method", |
|
398 |
"days_to_process","num_cores_tmp","NA_flag_val","file_format","out_dir_str", |
|
399 |
"out_suffix_str") |
|
400 |
#browser() |
|
401 |
list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster, |
|
402 |
list_param=list_param_create_accuracy_residuals_raster) |
|
403 |
|
|
404 |
#undebug(create_accuracy_residuals_raster) |
|
405 |
#list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster, |
|
406 |
# list_param=list_param_create_accuracy_residuals_raster) |
|
407 |
|
|
408 |
#create_accuracy_residuals_raster_obj <- create_accuracy_residuals_raster(1, list_param_create_accuracy_residuals_raster_obj) |
|
409 |
|
|
410 |
#note that three tiles did not produce a residuals surface!!! find out more later, join the output |
|
411 |
#to df_raste_tile to keep track of which one did not work... |
|
412 |
#lf_accuracy_residuals_raster <- as.character(unlist(lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name"))},x=list_create_accuracy_residuals_raster_obj))) |
|
413 |
lf_accuracy_residuals_raster <- lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){as.character(unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name")))},x=list_create_accuracy_residuals_raster_obj) |
|
414 |
lf_ac_assessment_layers <- lf_accuracy_residuals_raster |
|
415 |
} |
|
416 |
|
|
417 |
return(lf_ac_assessment_layers) |
|
418 |
|
|
419 |
} |
|
420 |
|
|
421 |
browser() |
|
422 |
|
|
423 |
#try running the function now: |
|
424 |
|
|
425 |
lf <- lf_mosaic[i] |
|
426 |
|
|
427 |
df <- tb #for ac_testing |
|
428 |
days_to_process <- day_to_mosaic[i] |
|
429 |
|
|
430 |
#browser() |
|
431 |
debug(generate_ac_assessment_layers_by_tile) |
|
432 |
generate_ac_assessment_layers_by_tile(lf,layers_option,df,df_tile_processed,metric_name, |
|
433 |
var_pred,list_models,use_autokrige,pred_mod_name, |
|
434 |
y_var_name,interpolation_method, |
|
435 |
days_to_process,num_cores,NA_flag_val,file_format, |
|
436 |
out_dir,out_suffix) #### create a function to generate accuracy layers by tiles |
|
437 |
|
|
438 |
if(layers_option=="ac_testing"){ |
|
439 |
#this takes about 1 minute and 35 seconds for 3 days and 28 tiles... |
|
440 |
#add options to clean up file after use!! |
|
441 |
#tb <- list_param$tb #fitting or validation table with all days |
|
442 |
#metric_name <- "rmse" #RMSE, MAE etc. |
|
443 |
#pred_mod_name <- "mod1" |
|
444 |
#y_var_name |
|
445 |
#interpolation_method #c("gam_CAI") #PARAM3 |
|
446 |
days_to_process <- day_to_mosaic |
|
447 |
#NA_flag_val <- list_param$NA_flag_val |
|
448 |
#file_format <- list_param$file_format |
|
449 |
out_dir_str <- out_dir |
|
450 |
out_suffix_str <- out_suffix |
|
451 |
lf <- lf_mosaic |
|
452 |
|
|
453 |
#Improved by adding multicores option |
|
454 |
num_cores_tmp <- num_cores |
|
455 |
list_param_accuracy_metric_raster <- list(lf,tb,metric_name,pred_mod_name,y_var_name,interpolation_method, |
|
456 |
days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str,out_suffix_str) |
|
457 |
names(list_param_accuracy_metric_raster) <- c("lf","tb","metric_name","pred_mod_name","y_var_name","interpolation_method", |
|
458 |
"days_to_process","num_cores","NA_flag_val","file_format","out_dir_str","out_suffix_str") |
|
459 |
list_raster_created_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_metric_raster, |
|
460 |
list_param=list_param_accuracy_metric_raster) |
|
461 |
|
|
462 |
#debug(create_accuracy_metric_raster) |
|
463 |
#list_raster_created_obj <- lapply(1:1,FUN=create_accuracy_metric_raster, |
|
464 |
# list_param=list_param_accuracy_metric_raster) |
|
465 |
#raster_created_obj <- create_accuracy_metric_raster(1, list_param_accuracy_metric_raster) |
|
466 |
|
|
467 |
#Extract list of files for rmse and date 1 (19920101), there should be 28 raster images |
|
468 |
lf_accuracy_testing_raster <- lapply(1:length(list_raster_created_obj),FUN=function(i){unlist(list_raster_created_obj[[i]]$list_raster_name)}) |
|
469 |
|
|
470 |
#Plot as quick check |
|
471 |
#r1 <- raster(lf_mosaic[[1]][1]) |
|
472 |
#plot(r1) |
|
473 |
#browser() |
|
474 |
|
|
475 |
} |
|
476 | 286 |
|
477 | 287 |
|
478 |
|
|
479 |
#### Add option for ac training here |
|
480 |
|
|
481 |
if(layers_option=="ac_training"){ |
|
482 |
#this takes about 1 minute and 35 seconds for 3 days and 28 tiles... |
|
483 |
#add options to clean up file after use!! |
|
484 |
#tb <- list_param$tb #fitting or validation table with all days |
|
485 |
#metric_name <- "rmse" #RMSE, MAE etc. |
|
486 |
#pred_mod_name <- "mod1" |
|
487 |
#y_var_name |
|
488 |
#interpolation_method #c("gam_CAI") #PARAM3 |
|
489 |
days_to_process <- day_to_mosaic |
|
490 |
#NA_flag_val <- list_param$NA_flag_val |
|
491 |
#file_format <- list_param$file_format |
|
492 |
out_dir_str <- out_dir |
|
493 |
out_suffix_str <- out_suffix |
|
494 |
lf <- lf_mosaic |
|
495 |
|
|
496 |
#Improved by adding multicores option |
|
497 |
num_cores_tmp <- num_cores |
|
498 |
#use tb_s (training) instead of tb |
|
499 |
list_param_accuracy_metric_raster <- list(lf,tb_s,metric_name,pred_mod_name,y_var_name,interpolation_method, |
|
500 |
days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str,out_suffix_str) |
|
501 |
names(list_param_accuracy_metric_raster) <- c("lf","tb","metric_name","pred_mod_name","y_var_name","interpolation_method", |
|
502 |
"days_to_process","num_cores","NA_flag_val","file_format","out_dir_str","out_suffix_str") |
|
503 |
list_raster_created_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_metric_raster, |
|
504 |
list_param=list_param_accuracy_metric_raster) |
|
505 |
|
|
506 |
#debug(create_accuracy_metric_raster) |
|
507 |
#list_raster_created_obj <- lapply(1:1,FUN=create_accuracy_metric_raster, |
|
508 |
# list_param=list_param_accuracy_metric_raster) |
|
509 |
#raster_created_obj <- create_accuracy_metric_raster(1, list_param_accuracy_metric_raster) |
|
510 |
|
|
511 |
#Extract list of files for rmse and date 1 (19920101), there should be 28 raster images |
|
512 |
lf_accuracy_training_raster <- lapply(1:length(list_raster_created_obj),FUN=function(i){unlist(list_raster_created_obj[[i]]$list_raster_name)}) |
|
513 |
|
|
514 |
#Plot as quick check |
|
515 |
#r1 <- raster(lf_mosaic[[1]][1]) |
|
516 |
#plot(r1) |
|
517 |
#browser() |
|
518 |
|
|
519 |
} |
|
520 |
|
|
521 | 288 |
#################################### |
522 | 289 |
###Depending on value of layers_option, create accuracy surfaces based on testing residuals... |
523 | 290 |
#browser() |
... | ... | |
697 | 464 |
#browser() |
698 | 465 |
|
699 | 466 |
if(layers_option=="ac_testing"){ |
467 |
|
|
468 |
#try running the function now: |
|
469 |
lf <- lf_mosaic[i] |
|
470 |
df <- tb #for ac_testing |
|
471 |
days_to_process <- day_to_mosaic[i] |
|
472 |
|
|
473 |
browser() |
|
474 |
debug(generate_ac_assessment_layers_by_tile) |
|
475 |
lf_accuracy_testing_raster<- generate_ac_assessment_layers_by_tile(lf,layers_option,df,df_tile_processed,metric_name, |
|
476 |
var_pred,list_models,use_autokrige,pred_mod_name, |
|
477 |
y_var_name,interpolation_method, |
|
478 |
days_to_process,num_cores,NA_flag_val,file_format, |
|
479 |
out_dir,out_suffix) #### create a function to generate accuracy layers by tiles |
|
480 |
|
|
700 | 481 |
## Now accuracy based on center of centroids |
701 | 482 |
mosaic_method <- "use_edge_weights" #this is distance from edge |
702 | 483 |
#Adding metric name in the name... |
... | ... | |
733 | 514 |
|
734 | 515 |
if(layers_option=="ac_training"){ |
735 | 516 |
## Now accuracy based on center of centroids |
517 |
|
|
518 |
#try running the function now: |
|
519 |
lf <- lf_mosaic[i] |
|
520 |
df <- tb_s #for ac_testing |
|
521 |
days_to_process <- day_to_mosaic[i] |
|
522 |
|
|
523 |
browser() |
|
524 |
debug(generate_ac_assessment_layers_by_tile) |
|
525 |
lf_accuracy_training_raster<- generate_ac_assessment_layers_by_tile(lf,layers_option,df,df_tile_processed,metric_name, |
|
526 |
var_pred,list_models,use_autokrige,pred_mod_name, |
|
527 |
y_var_name,interpolation_method, |
|
528 |
days_to_process,num_cores,NA_flag_val,file_format, |
|
529 |
out_dir,out_suffix) #### create a function to generate accuracy layers by tiles |
|
530 |
|
|
736 | 531 |
mosaic_method <- "use_edge_weights" #this is distance from edge |
737 | 532 |
#Adding metric name in the name... |
738 | 533 |
out_suffix_tmp <- paste(interpolation_method,metric_name,layers_option,day_to_mosaic[i],out_suffix,sep="_") |
... | ... | |
770 | 565 |
|
771 | 566 |
### produce residuals mosaics |
772 | 567 |
if(layers_option=="res_testing"){ |
568 |
|
|
569 |
lf <- lf_mosaic[i] |
|
570 |
df <- tb_s #for ac_testing |
|
571 |
days_to_process <- day_to_mosaic[i] |
|
572 |
|
|
573 |
browser() |
|
574 |
debug(generate_ac_assessment_layers_by_tile) |
|
575 |
lf_accuracy_training_raster<- generate_ac_assessment_layers_by_tile(lf,layers_option,df,df_tile_processed,metric_name, |
|
576 |
var_pred,list_models,use_autokrige,pred_mod_name, |
|
577 |
y_var_name,interpolation_method, |
|
578 |
days_to_process,num_cores,NA_flag_val,file_format, |
|
579 |
out_dir,out_suffix) #### create a function to generate accuracy layers by tiles |
|
580 |
|
|
773 | 581 |
#for now add data_day_s in the name!! |
774 | 582 |
mosaic_method <- "use_edge_weights" #this is distance from edge |
775 | 583 |
out_suffix_tmp <- paste(interpolation_method,"kriged_residuals","data_day_v",day_to_mosaic[i],out_suffix,sep="_") |
Also available in: Unified diff
moving generate accuray layers functions to script function mosaicing and adding different options