Revision 19d44fdd
Added by Benoit Parmentier about 8 years ago
climate/research/oregon/interpolation/global_product_assessment_part0.R | ||
---|---|---|
469 | 469 |
rat$legend <- tb_freq$value |
470 | 470 |
levels(r_table) <- rat |
471 | 471 |
|
472 |
|
|
473 | 472 |
res_pix <- 800 |
474 | 473 |
#res_pix <- 480 |
475 | 474 |
col_mfrow <- 1 |
... | ... | |
488 | 487 |
|
489 | 488 |
dev.off() |
490 | 489 |
|
491 |
#r <- raster(matrix(runif(20),5,4)) |
|
492 |
#r[r>.5] <- NA |
|
493 |
#g <- as(r, 'SpatialGridDataFrame') |
|
494 |
#p <- as(r, 'SpatialPixels') |
|
495 |
#p_spdf <- as(r_overlap_m,"SpatialPointsDataFrame") |
|
496 |
#plot(r) |
|
497 |
#points(p) |
|
498 |
|
|
499 | 490 |
### now assign id and match extent for tiles |
500 | 491 |
|
501 | 492 |
lf_files <- unlist(list_predicted) |
... | ... | |
541 | 532 |
#r_tiles_s <- r_tiles_stack |
542 | 533 |
names_tiles <- basename(in_dir_reg) |
543 | 534 |
|
535 |
|
|
536 |
list_param_generate_raster_number_pred <- list(list_tiles_predicted_masked,df_missing_tiles_day,r_overlap_m, |
|
537 |
num_cores,region_name, |
|
538 |
NA_flag_val,out_suffix,out_dir) |
|
539 |
|
|
540 |
names(list_param_generate_raster_number_pred) <- c("list_tiles_predicted_masked","df_missing_tiles_day","r_overlap_m", |
|
541 |
"num_cores","region_name", |
|
542 |
"NA_flag_val","out_suffix","out_dir") |
|
543 |
debug(generate_raster_number_of_prediction_by_day) |
|
544 |
|
|
545 |
obj_number_pix_predictions <- generate_raster_number_of_prediction_by_day(1,list_param_generate_raster_number_pred) |
|
546 |
|
|
547 |
obj_number_pix_predictions <- mcapply(1:nrow(df_missing_tiles_day), |
|
548 |
FUN=generate_raster_number_of_prediction_by_day, |
|
549 |
list_param=list_param_generate_raster_number_pred, |
|
550 |
mc.preschedule=FALSE, |
|
551 |
mc.cores = num_cores) |
|
552 |
|
|
544 | 553 |
generate_raster_number_of_prediction_by_day <- function(i,list_param){ |
554 |
##This function generates raster of missing pixels and number of predictions for days with missing tiles for a given region. |
|
555 |
#INPUTS |
|
556 |
#1) list_tiles_predicted_masked |
|
557 |
#2) df_missing_tiles_day |
|
558 |
#3) r_overlap_m |
|
559 |
#4) num_cores |
|
560 |
#5) region_name |
|
561 |
#6) NA_flag_val |
|
562 |
#7) out_suffix |
|
563 |
#8) out_dir |
|
545 | 564 |
|
546 |
list_names_tile_coord |
|
547 |
df_time_series |
|
548 |
missing_tiles <- df_missing_tiles_day[i,] |
|
549 |
date_str <- missing_tiles$date |
|
550 | 565 |
|
551 |
#r_tiles_s <- list_param$r_tiles_s |
|
552 |
list_param$list_tiles_predicted_masked |
|
566 |
###### Start script ##### |
|
553 | 567 |
|
568 |
#### read in paramters |
|
569 |
list_tiles_predicted_masked <- list_param$list_tiles_predicted_masked |
|
570 |
df_missing_tiles_day <- list_param$df_missing_tiles_day |
|
571 |
r_overlap_m <- list_param$r_overlap_m |
|
572 |
num_cores <- list_param$num_cores # 6 #PARAM 14 |
|
573 |
region_name <- list_param$region_name #<- "world" #PARAM 15 |
|
574 |
NA_flag_val <-list_param$NA_flag_val |
|
575 |
out_suffix <- list_param$out_suffix |
|
576 |
out_dir <- list_param$out_dir |
|
577 |
|
|
578 |
### To add or explore later...could have differences between predictions and rmse |
|
579 |
layers_option <- c("var_pred") #arg 17 ,param 17, options are:#res_training, res_testing,ac_training, ac_testing, var_pred |
|
580 |
NA_value <- NA_flag_val |
|
581 |
#metric_name <- "rmse" #to be added to the code later... |
|
582 |
|
|
583 |
##### Select relevant day and create stack of missing tiles |
|
584 |
|
|
585 |
missing_tiles <- df_missing_tiles_day[i,] |
|
586 |
date_str <- missing_tiles$date |
|
554 | 587 |
selected_col <- names(list_tiles_predicted_masked) |
555 | 588 |
missing_tiles_subset <- subset(missing_tiles,select=selected_col) |
556 | 589 |
selected_missing <- missing_tiles_subset==1 |
557 |
#names(df_missing_tiles_day)[selected_missing] |
|
558 |
|
|
559 |
#names(list_tiles_predicted_masked)[selected_missing] |
|
590 |
|
|
560 | 591 |
list_missing_tiles_raster <- list_tiles_predicted_masked[selected_missing] |
561 | 592 |
r_tiles_s <- stack(list_missing_tiles_raster) |
562 | 593 |
|
563 |
### first sum missing |
|
594 |
##### Sum missing tiles in the stack and generate number of predictions by pixels |
|
595 |
|
|
564 | 596 |
datasum <- stackApply(r_tiles_s, 1:nlayers(r_tiles_s), fun = sum) |
565 | 597 |
|
566 | 598 |
### then substract missing tiles... |
567 | 599 |
r_day_predicted <- r_overlap_m - datasum |
568 |
|
|
600 |
raster_name_number_prediction <- file.path(out_dir_str,paste("r_day_number_of_prediction_sum_day_mosaiced","_",region_name,"_masked_",date_str,file_format,sep="")) |
|
601 |
writeRaster(r_day_predicted, NAflag=NA_flag_val,filename=raster_name_number_prediction,overwrite=TRUE) |
|
602 |
|
|
569 | 603 |
r_table <- ratify(r_day_predicted) # build the Raster Attibute table |
570 | 604 |
rat <- levels(r_table)[[1]]#get the values of the unique cell frot the attribute table |
571 | 605 |
#rat$legend <- paste0("tile_",1:26) |
... | ... | |
612 | 646 |
|
613 | 647 |
dev.off() |
614 | 648 |
|
615 |
### generate retunr object |
|
649 |
### writeout data |
|
650 |
#extension_str <- extension(lf_files) |
|
651 |
#raster_name_tmp <- gsub(extension_str,"",basename(lf_files)) |
|
652 |
#out_suffix_str <- paste0(region_name,"_",out_suffix) |
|
653 |
raster_name_missing <- file.path(out_dir_str,paste("r_missing_day_mosaiced","_",region_name,"_masked_",date_str,file_format,sep="")) |
|
654 |
writeRaster(r_missing_day, NAflag=NA_flag_val,filename=raster_name_missing,overwrite=TRUE) |
|
655 |
|
|
656 |
### generate return object |
|
657 |
obj_number_day_predicted <- list(raster_name_number_prediction,raster_name_missing) |
|
658 |
names(obj_number_day_predicted) <- c("raster_name_number_prediction","raster_name_missing") |
|
659 |
|
|
616 | 660 |
returm(r_day_predicted) |
617 | 661 |
} |
618 | 662 |
|
Also available in: Unified diff
modifying generate_raster_number_of_prediction_by_day function