Project

General

Profile

« Previous | Next » 

Revision 1cd3531a

Added by Benoit Parmentier about 9 years ago

function mosaicing adding mask and path to python/gdal binaries

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/10/2015            
7
#MODIFIED ON: 10/21/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
......
385 385
  lf_files <- list_param$lf_files
386 386
  rast_ref <- list_param$rast_ref #name of reference file
387 387
  file_format <- list_param$file_format #".tif",".rst" or others
388
  python_bin <- list_param$python_bin
388 389
  out_suffix <- list_param$out_suffix
389 390
  out_dir_str <- list_param$out_dir_str
390 391
    
......
407 408

  
408 409
  #cmd_str <- paste("/usr/bin/gdalwarp",inFilename,raster_name,sep=" ") #this may be a problem
409 410
  cmd_str <- paste(python_cmd,inFilename,raster_name,sep=" ") #this may be a problem
410
      
411
      
412
      
413
      
414
      
415 411
  #gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' raw_spot.tif utm11.tif
416

  
417 412
  system(cmd_str)
418 413
  
419 414
  ##return name of file created
......
433 428
  #2)mosaic_method: mosaic methods availbable:use_sine_weights,use_edge,use_linear_weights
434 429
  #3)num_cores: number of cores used in parallilization in mclapply
435 430
  #4)r_mask_raster_name: mask rference raster image
436
  #4)python_bin: location of python executables, defaut is NULL
437
  #5)df_points: point location used in weighting, defaul is NULL
438
  #6)NA_flag_val: raster flag values, e.g. -9999
439
  #7)file_format: raster format used, default is ".tif"
440
  #8)out_suffix: output suffix, default is NULL, it is recommended to add the variable name
431
  #5)python_bin: location of python executables, defaut is NULL
432
  #6)df_points: point location used in weighting, defaul is NULL
433
  #7)NA_flag_val: raster flag values, e.g. -9999
434
  #8)file_format: raster format used, default is ".tif"
435
  #9)out_suffix: output suffix, default is NULL, it is recommended to add the variable name
441 436
  #             here e.g. dailyTmax and date!!
442
  #9)out_dir: output directory, default is NULL
437
  #10)out_dir: output directory, default is NULL
443 438
  #
444 439
  #OUTPUT:
445 440
  # Object is produced with 3 components:
......
545 540
  #mosaic function using gdal_merge to compute a reference image to mach.
546 541

  
547 542
  rast_ref <- file.path(out_dir,paste("avg_",out_suffix,file_format,sep="")) #this is a the ref
548
  cmd_str <- paste("python","/usr/bin/gdal_merge.py","-o ",rast_ref,paste(lf_mosaic,collapse=" ")) 
543
  if(is.null(python_bin)){
544
      python_bin=""
545
  }
546
    
547
  python_cmd <- file.path(python_bin,"gdal_merge.py")  
548
  cmd_str <- paste("python",python_cmd,"-o ",rast_ref,paste(lf_mosaic,collapse=" ")) 
549 549
  system(cmd_str)
550 550

  
551 551
  ## Create raster image for original predicted images with matching resolution and extent to the mosaic (reference image)
......
558 558
    
559 559
    lf_files <- unlist(list_weights)
560 560

  
561
    list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir)
562
    names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str")
561
    list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
562
    names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
563 563

  
564 564
    #debug(raster_match)
565 565
    #r_test <- raster(raster_match(1,list_param_raster_match))
......
567 567
    list_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores)                           
568 568

  
569 569
    lf_files <- unlist(list_weights_prod)
570
    list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir)
571
    names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str")
570
    list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
571
    names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
572 572

  
573 573
    #num_cores <-11
574 574
    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)                           
......
597 597
    r_weights_sum_raster_name <- file.path(out_dir,paste("r_weights_sum_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep=""))
598 598
    list_args_weights$filename <- r_weights_sum_raster_name
599 599
    list_args_weights$overwrite<- TRUE
600
    
600
    list_args_weights_prod$overwrite<- TRUE  #add to overwrite existing image  
601 601
    
602 602
    list_args_weights$fun <- "sum" #we want the sum to compute the weighted mean
603 603
    list_args_weights$na.rm <- TRUE
......
635 635
    
636 636
    #writeRaster(r_m_weighted_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)  
637 637
    #now use the mask
638
    r_m_weighted_mean_mask_raster_name <- file.path(out_dir,paste("r_m_",method_str,"_weighted_mean_mask_",out_suffix,".tif",sep=""))
639

  
640
    #mask(raster(r_m_weighted_mean_raster_name),mask=r_mask_raster_name,filename=r_m_weighted_mean_mask_raster_name)
641
    
638
    if(!is.null(r_mask_raster_name)){
639
      r_m_weighted_mean_mask_raster_name <- file.path(out_dir,paste("r_m_",method_str,"_weighted_mean_mask_",out_suffix,".tif",sep=""))
640
      mask(raster(r_m_weighted_mean_raster_name),mask=r_mask_raster_name,filename=r_m_weighted_mean_mask_raster_name)
641
      raster_name <- r_m_weighted_mean_mask_raster_name
642
    }else{
643
      raster_name <- r_m_weighted_mean_raster_name
644
    }
642 645
  }
643 646

  
644 647
  if(mosaic_method=="unweighted"){
......
663 666

  
664 667
    list_args_pred_m$fun <- "mean"
665 668
    list_args_pred_m$na.rm <- TRUE
669
    list_args_pred_m$overwrite<- TRUE  #add to overwrite existing image 
666 670
    #list_args_pred_m$filename <- 
667 671

  
668
    #Mosaic files
672
    #Mosaic files using R raster mosaic 
669 673
    r_m_mean <- do.call(mosaic,list_args_pred_m) #this is unweighted mean from the predicted raster
670 674

  
671
    raster_name <- file.path(out_dir,paste("r_m_mean_",out_suffix,".tif",sep=""))
672
    writeRaster(r_m_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)  #unweighted mean
675
    r_m_mean_raster_name <- file.path(out_dir,paste("r_m_mean_",out_suffix,".tif",sep=""))
676
    writeRaster(r_m_mean, NAflag=NA_flag_val,filename=r_m_mean_raster_name,overwrite=TRUE)  #unweighted mean
673 677

  
674 678
    #r_m_mean_unweighted <- paste("r_m_mean_",out_suffix,".tif",sep="")
675
    list_weights <- NULL
676
    list_weights_prod <- NULL
679
    #list_weights <- NULL
680
    #list_weights_prod <- NULL
681

  
682
    
683
    if(!is.null(r_mask_raster_name)){
684
      r_m_mean_mask_raster_name <- file.path(out_dir,paste("r_m_",method_str,"_unweighted_mean_mask_",out_suffix,".tif",sep=""))
685
      mask(raster(r_m_mean_raster_name),mask=r_mask_raster_name,filename=r_m_mean_mask_raster_name)
686
      raster_name <- r_m_mean_mask_raster_name
687
    }else{
688
      raster_name <- r_m_mean_raster_name
689
    }
677 690

  
678 691
  }
679 692
  

Also available in: Unified diff