Revision 91fc5062
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/22/2016
|
|
7 |
#MODIFIED ON: 04/24/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 |
... | ... | |
519 | 519 |
} |
520 | 520 |
valid_range <- values_range #if NULL don't screen values!! |
521 | 521 |
#valid_range <- c(-100,100) #pass this as parameter!! (in the next update) |
522 |
|
|
522 |
if(data_type=="Int16"){ |
|
523 |
data_type_str <- "INT2S" |
|
524 |
} |
|
523 | 525 |
lf_r_weights <- vector("list",length=length(lf_mosaic)) |
524 | 526 |
|
525 | 527 |
############### |
... | ... | |
845 | 847 |
"--overwrite",sep=" ") #division by zero is problematic... |
846 | 848 |
system(cmd_str3) |
847 | 849 |
|
850 |
if(!is.null(r_mask_raster_name)){ |
|
851 |
#different extent between mask and output if match extent is false!! |
|
852 |
#match resolution and extent first |
|
853 |
|
|
854 |
#lf_files <- c(r_m_weighted_mean_raster_name) #file(s) to be matched |
|
855 |
lf_files <- c(r_m_weighted_mean_raster_name) #match to mask |
|
856 |
rast_ref <- r_mask_raster_name |
|
857 |
##Maching resolution is probably only necessary for the r mosaic function |
|
858 |
#Modify later to take into account option R or python... |
|
859 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir) |
|
860 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
|
861 |
|
|
862 |
#undebug(raster_match) |
|
863 |
r_m_weighted_mean_raster_name_matched <- raster_match(1,list_param_raster_match) |
|
864 |
} |
|
865 |
|
|
848 | 866 |
#writeRaster(r_m_weighted_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE) |
849 | 867 |
|
850 | 868 |
###Starting rescaling, switched by masking first then screening to avoid potential problems |
851 | 869 |
##Can merge one and 2 with parentheses operations!!, make this a function? |
852 | 870 |
##Reclassify with valid range: -100,100 |
853 |
raster_name <- r_m_weighted_mean_raster_name |
|
871 |
|
|
872 |
#raster_name <- r_m_weighted_mean_raster_name |
|
873 |
raster_name <- r_m_weighted_mean_raster_name_matched |
|
854 | 874 |
max_val <- valid_range[2]*scaling #set min_valid |
855 | 875 |
raster_name_rec1 <- file.path(out_dir_str,paste("r_m_",mosaic_method,"_weighted_mean_rec1_",out_suffix,"_tmp",".tif",sep="")) |
856 | 876 |
#rec_tmp1 <- file.path(out_dir_str,paste("r_m_",mosaic_method,"_weighted_mean_rec_",out_suffix,".tif",sep="")) |
... | ... | |
897 | 917 |
|
898 | 918 |
#browser() #22minutes for one mosaic |
899 | 919 |
|
900 |
#cmd_mosaic_logfile <- file.path(out_dir,paste("cmd_mosaic_",out_suffix,".txt",sep=""))
|
|
920 |
cmd_mosaic_logfile <- file.path(out_dir,paste("cmd_mosaic_",out_suffix,".txt",sep="")) |
|
901 | 921 |
|
902 | 922 |
writeLines(cmd_str1,con=cmd_mosaic_logfile) #weights files to mosaic |
903 | 923 |
#writeLines(cmd_str2,con=file.path(out_dir,paste("cmd_mosaic_",out_suffix,".txt",sep=""))) #weights files to mosaic |
... | ... | |
915 | 935 |
#match resolution and extent first |
916 | 936 |
|
917 | 937 |
#lf_files <- c(r_m_weighted_mean_raster_name) #file(s) to be matched |
918 |
lf_files <- c(r_m_weighted_mean_raster_name_rec) #match to mask |
|
938 |
#lf_files <- c(r_m_weighted_mean_raster_name_rec) #match to mask
|
|
919 | 939 |
rast_ref <- r_mask_raster_name |
920 | 940 |
##Maching resolution is probably only necessary for the r mosaic function |
921 | 941 |
#Modify later to take into account option R or python... |
922 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir) |
|
923 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
|
942 |
#list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
|
|
943 |
#names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
|
|
924 | 944 |
|
925 | 945 |
#undebug(raster_match) |
926 |
r_m_weighted_mean_raster_name_matched <- raster_match(1,list_param_raster_match) |
|
946 |
#r_m_weighted_mean_raster_name_matched <- raster_match(1,list_param_raster_match)
|
|
927 | 947 |
#output |
928 | 948 |
r_m_weighted_mean_mask_raster_name <- file.path(out_dir_str,paste("r_m_",mosaic_method,"_weighted_mean_mask_",out_suffix,".tif",sep="")) |
929 | 949 |
|
930 |
#mask(raster(r_m_weighted_mean_raster_name_matched),mask=raster(r_mask_raster_name), |
|
931 |
# filename=r_m_weighted_mean_mask_raster_name,overwrite=TRUE) |
|
950 |
in_raster_name <- r_m_weighted_mean_raster_name_rec |
|
951 |
mask(raster(in_raster_name), |
|
952 |
mask=raster(r_mask_raster_name), |
|
953 |
filename=r_m_weighted_mean_mask_raster_name, |
|
954 |
datatype=data_type_str, |
|
955 |
options=c("COMPRESS=LZW"), |
|
956 |
overwrite=TRUE, |
|
957 |
NAflag=NA_flag_val) |
|
932 | 958 |
|
933 | 959 |
##Now combine A and B and multiply by mask?? This must be faster than the mask option in R |
934 | 960 |
#The mask must be in 1,NA format with 1 being valid values being considered in roi. |
935 | 961 |
#r_m_weighted_mean_raster_name_rec <- file.path(out_dir_str,paste("r_m_",mosaic_method,"_weighted_mean_rec_",out_suffix,"_tmp",".tif",sep="")) |
936 | 962 |
#browser() |
937 |
cmd_mosaic_logfile <- file.path(out_dir,paste("cmd_mosaic_",out_suffix,".txt",sep="")) |
|
963 |
#cmd_mosaic_logfile <- file.path(out_dir,paste("cmd_mosaic_",out_suffix,".txt",sep="")) |
|
964 |
#in_raster_name <- r_m_weighted_mean_raster_name_matched |
|
938 | 965 |
|
939 |
cmd_str7 <- paste(python_cmd, |
|
940 |
paste("-A ", r_m_weighted_mean_raster_name_matched,sep=""), |
|
941 |
paste("-B ", r_mask_raster_name,sep=""), |
|
942 |
paste("--outfile=",r_m_weighted_mean_mask_raster_name,sep=""), |
|
943 |
paste("--type=",data_type,sep=""), |
|
944 |
"--co='COMPRESS=LZW'", |
|
945 |
paste("--NoDataValue=",NA_flag_val,sep=""), |
|
946 |
paste("--calc='(A*B)'",sep=""), |
|
947 |
"--overwrite",sep=" ") #division by zero is problematic... |
|
948 |
system(cmd_str7) |
|
949 |
cat(cmd_str7, file=cmd_mosaic_logfile, append=TRUE, sep = "\n") |
|
966 |
|
|
967 |
#cmd_str7 <- paste(python_cmd, |
|
968 |
# paste("-A ", in_raster_name,sep=""), |
|
969 |
# paste("-B ", r_mask_raster_name,sep=""), |
|
970 |
# paste("--outfile=",r_m_weighted_mean_mask_raster_name,sep=""), |
|
971 |
# paste("--type=",data_type,sep=""), |
|
972 |
# "--co='COMPRESS=LZW'", |
|
973 |
# paste("--NoDataValue=",NA_flag_val,sep=""), |
|
974 |
# paste("--calc='(A*B)'",sep=""), |
|
975 |
# "--overwrite",sep=" ") #division by zero is problematic... |
|
976 |
#system(cmd_str7) |
|
977 |
#cat(cmd_str7, file=cmd_mosaic_logfile, append=TRUE, sep = "\n") |
|
950 | 978 |
|
951 | 979 |
##Set min max and NA value |
952 | 980 |
r_mosaiced <- raster(r_m_weighted_mean_mask_raster_name) |
953 | 981 |
r_mosaiced <- setMinMax(r_mosaiced) |
954 | 982 |
NAvalue(r_mosaiced) <- NA_flag_val |
955 | 983 |
raster_name <- r_m_weighted_mean_mask_raster_name |
956 |
browser() |
|
984 |
#browser()
|
|
957 | 985 |
#-32,768 is NA |
958 | 986 |
}else{ |
959 | 987 |
raster_name <- r_m_weighted_mean_raster_name |
Also available in: Unified diff
fixing problem with masking and no data values