Revision 2644e643
Added by Benoit Parmentier over 8 years ago
climate/research/oregon/interpolation/global_run_scalingup_mosaicing_function.R | ||
---|---|---|
4 | 4 |
#Different options to explore mosaicing are tested. This script only contains functions. |
5 | 5 |
#AUTHOR: Benoit Parmentier |
6 | 6 |
#CREATED ON: 04/14/2015 |
7 |
#MODIFIED ON: 04/08/2016
|
|
7 |
#MODIFIED ON: 04/09/2016
|
|
8 | 8 |
#Version: 2 |
9 | 9 |
#PROJECT: Environmental Layers project |
10 | 10 |
#COMMENTS: first commit of function script to test mosaicing using 1500x4500km and other tiles |
... | ... | |
502 | 502 |
|
503 | 503 |
out_dir_str <- out_dir |
504 | 504 |
|
505 |
if(tmp_file==T){
|
|
506 |
out_suffix_str_tmp <- paste0(out_suffix,"_tmp")
|
|
507 |
} |
|
505 |
#if(tmp_files==T){
|
|
506 |
out_suffix_str_tmp <- paste0(out_suffix,"_tmp") |
|
507 |
#}
|
|
508 | 508 |
|
509 | 509 |
lf_r_weights <- vector("list",length=length(lf_mosaic)) |
510 | 510 |
|
... | ... | |
562 | 562 |
#num_cores <- 11 |
563 | 563 |
|
564 | 564 |
#debug(create_weights_fun) |
565 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
565 |
#weights_obj <- create_weights_fun(1,list_param=list_param_create_weights)
|
|
566 | 566 |
|
567 | 567 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
568 | 568 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
... | ... | |
588 | 588 |
#list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
589 | 589 |
#names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
590 | 590 |
#num_cores <- 11 |
591 |
#debug(create_weights_fun) |
|
591 |
#undebug(create_weights_fun)
|
|
592 | 592 |
#weights_obj <- create_weights_fun(3,list_param=list_param_create_weights) |
593 | 593 |
|
594 | 594 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
... | ... | |
650 | 650 |
|
651 | 651 |
##Maching resolution is probably only necessary for the r mosaic function |
652 | 652 |
#Modify later to take into account option R or python... |
653 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
|
|
653 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix_str_tmp,out_dir_str)
|
|
654 | 654 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
655 | 655 |
|
656 | 656 |
#undebug(raster_match) |
... | ... | |
660 | 660 |
list_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
661 | 661 |
|
662 | 662 |
lf_files <- unlist(list_weights_prod) |
663 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
|
|
663 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix_str_tmp,out_dir_str)
|
|
664 | 664 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
665 | 665 |
|
666 | 666 |
#num_cores <-11 |
... | ... | |
681 | 681 |
#write.table(lf_day_to_mosaic,file=file.path(out_dir,paste("list_to_mosaics_",day_to_mosaic[i],".txt",sep=""))) |
682 | 682 |
#filename_list_mosaics <- file.path(out_dir,paste("list_to_mosaics_",day_to_mosaic[i],".txt",sep="")) |
683 | 683 |
|
684 |
filename_list_mosaics_weights_m <- file.path(out_dir,paste("list_to_mosaics_","weights_",mosaic_method,"_",out_suffix,".txt",sep=""))
|
|
685 |
filename_list_mosaics_prod_weights_m <- file.path(out_dir,paste("list_to_mosaics_","prod_weights_",mosaic_method,"_",out_suffix,".txt",sep=""))
|
|
684 |
filename_list_mosaics_weights_m <- file.path(out_dir_str,paste("list_to_mosaics_","weights_",mosaic_method,"_",out_suffix_str_tmp,".txt",sep=""))
|
|
685 |
filename_list_mosaics_prod_weights_m <- file.path(out_dir_str,paste("list_to_mosaics_","prod_weights_",mosaic_method,"_",out_suffix_str_tmp,".txt",sep=""))
|
|
686 | 686 |
|
687 | 687 |
#writeLines(unlist(list_weights_m),con=filename_list_mosaics_weights_m) #weights files to mosaic |
688 | 688 |
#writeLines(unlist(list_weights_prod_m),con=filename_list_mosaics_prod_weights_m) #prod weights files to mosaic |
... | ... | |
692 | 692 |
|
693 | 693 |
#out_mosaic_name_weights_m <- r_weights_sum_raster_name <- file.path(out_dir,paste("r_weights_sum_m_",mosaic_method,"_weighted_mean_",out_suffix,".tif",sep="")) |
694 | 694 |
#out_mosaic_name_prod_weights_m <- r_weights_sum_raster_name <- file.path(out_dir,paste("r_prod_weights_sum_m_",mosaic_method,"_weighted_mean_",out_suffix,".tif",sep="")) |
695 |
out_mosaic_name_weights_m <- file.path(out_dir,paste("r_weights_sum_m_",mosaic_method,"_weighted_mean_",out_suffix,".tif",sep=""))
|
|
696 |
out_mosaic_name_prod_weights_m <- file.path(out_dir,paste("r_prod_weights_sum_m_",mosaic_method,"_weighted_mean_",out_suffix,".tif",sep=""))
|
|
695 |
out_mosaic_name_weights_m <- file.path(out_dir_str,paste("r_weights_sum_m_",mosaic_method,"_weighted_mean_",out_suffix_str_tmp,".tif",sep=""))
|
|
696 |
out_mosaic_name_prod_weights_m <- file.path(out_dir_str,paste("r_prod_weights_sum_m_",mosaic_method,"_weighted_mean_",out_suffix_str_tmp,".tif",sep=""))
|
|
697 | 697 |
|
698 | 698 |
#in_file_to_mosaics <- filename_list_mosaics |
699 | 699 |
#in_dir_mosaics <- file.path(in_dir1,region_names[i]) |
... | ... | |
754 | 754 |
|
755 | 755 |
##Maching resolution is probably only necessary for the r mosaic function |
756 | 756 |
#MOdify later to take into account option R or python... |
757 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
|
|
757 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix_str_tmp,out_dir_str)
|
|
758 | 758 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
759 | 759 |
|
760 | 760 |
#undebug(raster_match) |
... | ... | |
764 | 764 |
list_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
765 | 765 |
|
766 | 766 |
lf_files <- unlist(list_weights_prod) |
767 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
|
|
767 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix_str_tmp,out_dir_str)
|
|
768 | 768 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
769 | 769 |
|
770 | 770 |
#num_cores <-11 |
... | ... | |
791 | 791 |
list_args_weights_prod <- lapply(1:length(list_args_weights_prod), FUN=function(i,x){raster(x[[i]])},x=list_args_weights_prod) |
792 | 792 |
list_args_weights_prod$fun <- "sum" #use sum while mosaicing |
793 | 793 |
list_args_weights_prod$na.rm <- TRUE #deal with NA by removal |
794 |
r_weights_sum_raster_name <- file.path(out_dir,paste("r_weights_sum_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep=""))
|
|
794 |
r_weights_sum_raster_name <- file.path(out_dir_str,paste("r_weights_sum_m_",method_str,"_weighted_mean_",out_suffix_str_tmp,".tif",sep=""))
|
|
795 | 795 |
list_args_weights$filename <- r_weights_sum_raster_name |
796 | 796 |
list_args_weights$overwrite<- TRUE |
797 | 797 |
list_args_weights_prod$overwrite<- TRUE #add to overwrite existing image |
798 | 798 |
|
799 | 799 |
list_args_weights$fun <- "sum" #we want the sum to compute the weighted mean |
800 | 800 |
list_args_weights$na.rm <- TRUE |
801 |
r_prod_sum_raster_name <- file.path(out_dir,paste("r_prod_sum_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep=""))
|
|
801 |
r_prod_sum_raster_name <- file.path(out_dir_str,paste("r_prod_sum_m_",method_str,"_weighted_mean_",out_suffix_str_tmp,".tif",sep=""))
|
|
802 | 802 |
list_args_weights_prod$filename <- r_prod_sum_raster_name |
803 | 803 |
|
804 | 804 |
#Mosaic files: this is where we can use Alberto Python function but modified with option for |
... | ... | |
813 | 813 |
|
814 | 814 |
#r_m_weighted_mean <- r_prod_sum/r_weights_sum #this is the mosaic using weighted mean... |
815 | 815 |
|
816 |
r_m_weighted_mean_raster_name <- file.path(out_dir,paste("r_m_",mosaic_method,"_weighted_mean_",out_suffix,".tif",sep="")) |
|
816 |
r_m_weighted_mean_raster_name <- file.path(out_dir_str,paste("r_m_",mosaic_method,"_weighted_mean_",out_suffix,".tif",sep=""))
|
|
817 | 817 |
|
818 | 818 |
if(is.null(python_bin)){ |
819 | 819 |
python_bin="" |
... | ... | |
850 | 850 |
#undebug(raster_match) |
851 | 851 |
r_m_weighted_mean_raster_name_matched <- raster_match(1,list_param_raster_match) |
852 | 852 |
|
853 |
r_m_weighted_mean_mask_raster_name <- file.path(out_dir,paste("r_m_",mosaic_method,"_weighted_mean_mask_",out_suffix,".tif",sep="")) |
|
853 |
r_m_weighted_mean_mask_raster_name <- file.path(out_dir_str,paste("r_m_",mosaic_method,"_weighted_mean_mask_",out_suffix,".tif",sep=""))
|
|
854 | 854 |
mask(raster(r_m_weighted_mean_raster_name_matched),mask=raster(r_mask_raster_name), |
855 | 855 |
filename=r_m_weighted_mean_mask_raster_name,overwrite=TRUE) |
856 | 856 |
raster_name <- r_m_weighted_mean_mask_raster_name |
... | ... | |
862 | 862 |
if(mosaic_method=="unweighted"){ |
863 | 863 |
#### Fourth use original images |
864 | 864 |
#macth file to mosaic extent using the original predictions |
865 |
lf_files <- lf_mosaic |
|
866 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
867 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
868 |
|
|
869 |
list_pred_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
870 |
|
|
865 |
|
|
866 |
if(match_extent==TRUE){ |
|
867 |
lf_files <- lf_mosaic |
|
868 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
869 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
870 |
list_pred_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
871 |
}else{ |
|
872 |
list_pred_m <- lf_mosaic |
|
873 |
} |
|
871 | 874 |
#list_mosaiced_files <- list.files(pattern="r_m.*._weighted_mean_.*.tif") |
872 | 875 |
|
873 | 876 |
#names(list_mosaiced_files) <- c("edge","linear","sine") |
... | ... | |
919 | 922 |
|
920 | 923 |
} |
921 | 924 |
|
925 |
########## clean up the disk/directories before ending the function #### |
|
926 |
|
|
927 |
if(tmp_files==F){ #if false...delete all files with "_tmp" |
|
928 |
lf_tmp <- list.files(pattern="*.*tmp*.*",path=out_dir_str,full.names=T) |
|
929 |
##now delete temporary files... |
|
930 |
file.remove(lf_tmp) |
|
931 |
} |
|
932 |
|
|
922 | 933 |
#Create return object |
923 | 934 |
mosaic_obj <- list(raster_name,list_weights,list_weights_prod,mosaic_method) |
924 | 935 |
names(mosaic_obj) <- c("mean_mosaic","r_weights","r_weigths_prod","method") |
925 |
save(mosaic_obj,file=file.path(out_dir,paste(mosaic_method,"_","mosaic_obj_",out_suffix,".RData",sep=""))) |
|
936 |
save(mosaic_obj,file=file.path(out_dir_str,paste(mosaic_method,"_","mosaic_obj_",out_suffix,".RData",sep="")))
|
|
926 | 937 |
return(mosaic_obj) |
927 | 938 |
} |
928 | 939 |
|
Also available in: Unified diff
mosaicing functions, option to remove temporary files