Project

General

Profile

« Previous | Next » 

Revision 81318fd0

Added by Benoit Parmentier almost 9 years ago

mosaicing function adding python and R options to mosaicFile

View differences:

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: 10/27/2015            
7
#MODIFIED ON: 11/09/2015            
8 8
#Version: 1
9 9
#PROJECT: Environmental Layers project     
10 10
#COMMENTS: first commit of function script to test mosaicing using 1500x4500km and other tiles
......
192 192

  
193 193
#### end of function
194 194

  
195
mosaic_python_merge <- function(module_path,module_name,input_file,out_mosaic_name){
196
  #out_mosaic_name <- r_weights_sum_raster_name <- file.path(out_dir,paste("r_weights_sum_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep=""))
197
  cmd_str <- paste("python", file.path(module_path,module_name),
198
                   "--config GDAL_CACHEMAX=1500",
199
                   "--overwrite=TRUE",
200
                   paste("-o",out_mosaic_name,sep=" "),
201
                  paste("--optfile", input_file,sep=" "))
202
  system(cmd_str)
203
  #list(out_mosaic_name,cmd_str)
204
  return(out_mosaic_name)
205
}
206
      
207
      
195 208
create_weights_fun <- function(i, list_param){
196 209
  #This function generates weights from a point location on a raster layer.
197 210
  #Note that the weights are normatlized on 0-1 scale using max and min values.
......
566 579
  
567 580
  if(mosaic_method%in%c("use_linear_weights","use_sine_weights","use_edge_weights")){
568 581
    
569
    lf_files <- unlist(list_weights)
570

  
571
    ##Maching resolution is probably only necessary for the r mosaic function
572
    #MOdify later to take into account option R or python...
573
    list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
574
    names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
575

  
576
    #debug(raster_match)
577
    #r_test <- raster(raster_match(1,list_param_raster_match))
578

  
579
    list_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores)                           
580

  
581
    lf_files <- unlist(list_weights_prod)
582
    list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
583
    names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
584

  
585
    #num_cores <-11
586
    list_weights_prod_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores)                           
587 582

  
588 583
    #####################
589 584
    ###### PART 4: compute the weighted mean with the mosaic function #####
590 585

  
591 586
    if(algorithm=="python"){
592
      
587

  
588

  
593 589
      #The file to do the merge is /nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum.py. Sample call below.
594 590
      #python /nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum.py --config GDAL_CACHEMAX=1500 --overwrite=TRUE -o  outputname.tif --optfile input.txt
595 591
      #lf_day_to_mosaic <- list_weights_m
......
599 595
      #lf_day_to_mosaic <- unlist(lf_day_to_mosaic)
600 596
      #write.table(lf_day_to_mosaic,file=file.path(out_dir,paste("list_to_mosaics_",day_to_mosaic[i],".txt",sep="")))
601 597
      #filename_list_mosaics <- file.path(out_dir,paste("list_to_mosaics_",day_to_mosaic[i],".txt",sep=""))
602
      filename_list_mosaics_weights_m <- file.path(out_dir,paste("list_to_mosaics_","weights_m_",mosaic_method,"_",out_suffix,".txt",sep=""))
603
      filename_list_mosaics_prod_weights_m <- file.path(out_dir,paste("list_to_mosaics_","prod_weights_m_",mosaic_method,"_",out_suffix,".txt",sep=""))
598

  
599
      filename_list_mosaics_weights_m <- file.path(out_dir,paste("list_to_mosaics_","weights_",mosaic_method,"_",out_suffix,".txt",sep=""))
600
      filename_list_mosaics_prod_weights_m <- file.path(out_dir,paste("list_to_mosaics_","prod_weights_",mosaic_method,"_",out_suffix,".txt",sep=""))
601
      
602
      #writeLines(unlist(list_weights_m),con=filename_list_mosaics_weights_m) #weights files to mosaic 
603
      #writeLines(unlist(list_weights_prod_m),con=filename_list_mosaics_prod_weights_m) #prod weights files to mosaic
604 604
      
605
      writeLines(unlist(list_weights_m),con=filename_list_mosaics_weights_m) #weights files to mosaic 
606
      writeLines(unlist(list_weights_prod_m),con=filename_list_mosaics_prod_weights_m) #prod weights files to mosaic
605
      writeLines(unlist(list_weights),con=filename_list_mosaics_weights_m) #weights files to mosaic 
606
      writeLines(unlist(list_weights_prod),con=filename_list_mosaics_prod_weights_m) #prod weights files to mosaic
607 607

  
608 608
      #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=""))
609 609
      #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=""))
......
627 627
      input_file <- filename_list_mosaics_weights_m
628 628
      module_path <- mosaic_python #this should be a parameter for the function...
629 629

  
630
      mosaic_python_merge <- function(module_path,module_name,input_file,out_mosaic_name){
631
        #d
632
        #out_mosaic_name <- r_weights_sum_raster_name <- file.path(out_dir,paste("r_weights_sum_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep=""))
633
        cmd_str <- paste("python", file.path(module_path,module_name),
634
                         "--config GDAL_CACHEMAX=1500",
635
                         "--overwrite=TRUE",
636
                         paste("-o",out_mosaic_name,sep=" "),
637
                         paste("--optfile", input_file,sep=" "))
638
        system(cmd_str)
639
        #list(out_mosaic_name,cmd_str)
640
        return(out_mosaic_name)
641
      }
630

  
642 631
      #python /nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum.py 
643 632
      #--config GDAL_CACHEMAX=1500 --overwrite=TRUE -o  outputname.tif --optfile input.txt
644 633
      r_weights_sum_raster_name <- mosaic_python_merge(module_path=mosaic_python,
......
655 644
    
656 645
    if(algorithm=="R"){
657 646
      
647
      #If using R, it is necessary to match extent firt
648
      
649
      lf_files <- unlist(list_weights)
650

  
651
      ##Maching resolution is probably only necessary for the r mosaic function
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)
654
      names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
655

  
656
      #undebug(raster_match)
657
      #r_test <- raster_match(1,list_param_raster_match)
658
      #r_test <- raster(raster_match(1,list_param_raster_match))
659

  
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

  
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)
664
      names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
665

  
666
      #num_cores <-11
667
      list_weights_prod_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores)                           
668

  
669
      
658 670
      #The file to do the merge is /nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum.py. Sample call below.
659 671
      #python /nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum.py --config GDAL_CACHEMAX=1500 --overwrite=TRUE -o  outputname.tif --optfile input.txt
660 672
          ##Make this a function later

Also available in: Unified diff