Project

General

Profile

« Previous | Next » 

Revision 9fc590d0

Added by Benoit Parmentier over 8 years ago

modifying mosaic function script to use reference raster as output

View differences:

climate/research/oregon/interpolation/global_run_scalingup_mosaicing.R
5 5
#Analyses, figures, tables and data are also produced in the script.
6 6
#AUTHOR: Benoit Parmentier 
7 7
#CREATED ON: 04/14/2015  
8
#MODIFIED ON: 06/19/2016            
8
#MODIFIED ON: 08/22/2016            
9 9
#Version: 6
10 10
#PROJECT: Environmental Layers project     
11 11
#COMMENTS: analyses run for reg4 1991 for test of mosaicing using 1500x4500km and other tiles
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: 07/05/2016            
7
#MODIFIED ON: 08/16/2016            
8 8
#Version: 2
9 9
#PROJECT: Environmental Layers project     
10 10
#COMMENTS: Bug solved for reg5, problem in matching rmse val and number of files in predictions by tiles
......
221 221

  
222 222
#### end of function
223 223

  
224
mosaic_python_merge <- function(NA_flag_val,module_path,module_name,input_file,out_mosaic_name){
225
  #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=""))
226
  cmd_str <- paste("python", file.path(module_path,module_name),
224
mosaic_python_merge <- function(NA_flag_val,module_path,module_name,input_file,out_mosaic_name,raster_ref_name=NULL){
225
  #Inputs:
226
  #NA_falg_val: NA value to use in the output
227
  #module_path: python module to be used with path
228
  #module_name: name of the module
229
  #input_file: input file as text file containing a list of files to mosaic
230
  #out_mosaic_name: output file for the mosaic
231
  #rast_ref_name: reference file with extent and resolution to be used in the mosaicing
232
  #
233
  
234
  #### Start script ###
235
  
236
        
237
  if(is.null(raster_ref_name)){
238

  
239
    #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=""))
240
    cmd_str <- paste("python", file.path(module_path,module_name),
227 241
                   "--config GDAL_CACHEMAX=1500",
228 242
                   "--overwrite=TRUE",
229 243
                   paste("-o",out_mosaic_name,sep=" "),
230 244
                   paste("--optfile", input_file,sep=" "),
231 245
                   paste("-n",NA_flag_val,sep=" "))
232
  system(cmd_str)
246
    system(cmd_str)
247
  }
248
  if(!is.null(raster_ref_name)){
249
    
250
    #lf_files <- c(r_m_weighted_mean_raster_name) #match to mask
251
    #rast_ref_name <- r_mask_raster_name
252
    r_ref <- raster(raster_ref_name)
253
    extent_r_ref <- as.numeric(as.matrix(extent(r_ref)))
254
    res_pix <- res(r_mask)
255
    #c(xmin,ymax,xmax,ymin)
256
    #c(ulx,uly,lrx,lry)
257
    extent_str <- c(extent_r_ref[1],extent_r_ref[4],extent_r_ref[3],extent_r_ref[2])
258
    #-ps 0.00833349 0.008333229 -ul_lr -187.143383752 81.744912045 -5.073165638 14.554089652
259
    #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=""))
260
    cmd_str <- paste("python", file.path(module_path,module_name),
261
                   "--config GDAL_CACHEMAX=1500",
262
                   "--overwrite=TRUE",
263
                   paste("-o",out_mosaic_name,sep=" "),
264
                   paste("-ps", res_pix[1],res_pix[2],sep=" "), #pixel size
265
                   paste("-ul_lr", extent_str[1],extent_str[2],extent_str[3],extent_str[4],sep=" "), #extent
266
                   paste("--optfile", input_file,sep=" "),
267
                   paste("-n",NA_flag_val,sep=" "))
268
    system(cmd_str)
269
    
270
  }
233 271
  #list(out_mosaic_name,cmd_str)
234 272
  mosaic_python_merge_obj <- list(out_mosaic_name,cmd_str)
235 273
  names(mosaic_python_merge_obj) <- c("out_mosaic_name","cmd_str")
......
721 759
      #browser()
722 760
      #python /nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum.py 
723 761
      #--config GDAL_CACHEMAX=1500 --overwrite=TRUE -o  outputname.tif --optfile input.txt
762
      if(!is.null(r_mask_raster_name)){
763
        #d
764
        #different extent between mask and output if match extent is false!!
765
        #match resolution and extent first
766
      
767
        #lf_files <- c(r_m_weighted_mean_raster_name) #file(s) to be matched
768
        #lf_files <- c(r_m_weighted_mean_raster_name) #match to mask
769
        rast_ref_name <- r_mask_raster_name
770
        #r_mask <- raster(rast_ref)
771
        #extent_r_mask <- as.numeric(as.matrix(extent(r_mask)))
772
        #res_pix <- res(r_mask)
773
        #c(xmin,ymax,xmax,ymin)
774
        #c(ulx,uly,lrx,lry)
775
        #extent_str <- c(extent_r_mask[1],extent_r_mask[4],extent_r_mask[3],extent_r_mask[2])
776
      }else{
777
        rast_ref_name <- NULL
778
      }
779
      #cmd_mosaic_gam_CAI_dailyTmax_19840101_reg1_1984.txt
780
      #python /nobackupp6/aguzman4/climateLayers/sharedCode//gdal_merge_sum.py --config GDAL_CACHEMAX=1500 --overwrite=TRUE -o /nobackupp8/bparmen1/climateLayers/out
724 781
      mosaic_weights_obj <- mosaic_python_merge(NA_flag_val=NA_flag_val,
725 782
                                                module_path=mosaic_python,
726 783
                                                module_name="gdal_merge_sum.py",
727 784
                                                input_file=filename_list_mosaics_weights_m,
728
                                                out_mosaic_name=out_mosaic_name_weights_m)
785
                                                out_mosaic_name=out_mosaic_name_weights_m,
786
                                                raster_ref_name = rast_ref_name) ##if NA, not take into account
729 787
      r_weights_sum_raster_name <- mosaic_weights_obj$out_mosaic_name
730 788
      cmd_str1 <- mosaic_weights_obj$cmd_str
731 789
      #r_prod_sum_raster_name <- mosaic_python_merge(module_path=mosaic_python,
......
737 795
                                                     module_path=mosaic_python,
738 796
                                                     module_name="gdal_merge_sum.py",
739 797
                                                     input_file=filename_list_mosaics_prod_weights_m,
740
                                                     out_mosaic_name=out_mosaic_name_prod_weights_m)
798
                                                     out_mosaic_name=out_mosaic_name_prod_weights_m,
799
                                                     raster_ref_name = rast_ref_name)
741 800
      r_prod_sum_raster_name <- mosaic_prod_weights_obj$out_mosaic_name
742 801
      cmd_str2 <- mosaic_prod_weights_obj$cmd_str
743 802
      #write out python command used for mosaicing
......
842 901
      #lf_files <- c(r_m_weighted_mean_raster_name) #file(s) to be matched
843 902
      lf_files <- c(r_m_weighted_mean_raster_name) #match to mask
844 903
      rast_ref <- r_mask_raster_name
904
      raster(rast_ref)
905
      extent_r_mask <- extent(r_mask)
845 906
      ##Maching resolution is probably only necessary for the r mosaic function
846 907
      #Modify later to take into account option R or python...
847 908
      list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
climate/research/oregon/interpolation/master_script_stage_7.R
14 14

  
15 15
#AUTHOR: Benoit Parmentier                                                                        
16 16
#CREATED ON: 01/01/2016  
17
#MODIFIED ON: 07/05/2016  
17
#MODIFIED ON: 08/05/2016  
18 18
#PROJECT: NCEAS INPLANT: Environment and Organisms                                                                           
19 19

  
20 20
#First source these files:

Also available in: Unified diff