Project

General

Profile

« Previous | Next » 

Revision 0c4658af

Added by Benoit Parmentier over 8 years ago

mosaicing functions, fixing outputs names and directories

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: 04/06/2016            
7
#MODIFIED ON: 04/08/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
......
238 238
  #This function generates weights from a point location on a raster layer.
239 239
  #Note that the weights are normatlized on 0-1 scale using max and min values.
240 240
  #Inputs:
241
  #lf: list of raster files
242
  #df_points: reference points from which to compute distance
243
  #r_feature: reference features as raster image from which to compute distance from
244
  #methods: options available: use_sine_weights,use_edge,use_linear_weights
241
  #1)lf: list of raster files
242
  #2)df_points: reference points from which to compute distance
243
  #3)r_feature: reference features as raster image from which to compute distance from
244
  #4)methods: options available: use_sine_weights,use_edge,use_linear_weights
245
  #5)NA_flag : raster flag values, e.g. -9999
246
  #6)file_format: raster format used, default is ".tif"
247
  #7)out_suffix_str: output suffix, default is NULL, it is recommended to add the variable name etc.
248
  #             here e.g. dailyTmax and date!!
249
  #8)out_dir_str: output directory, default is NULL
250

  
245 251
  #Outputs:
246 252
  #raster list of weights and product of wegihts and inuts
247 253
  #TODO: 
......
252 258
  #
253 259
  ############
254 260
  
261
  ##### START SCRIPT #####
262
  
263
  ##### Parse out the input parameters
264
  
255 265
  lf <- list_param$lf
256 266
  df_points <- list_param$df_points
257 267
  r_feature <- list_param$r_feature #this should be change to a list
258 268
  padding <- TRUE #if padding true then make buffer around edges??
259 269
  method <- list_param$method #differnt methods available to create weights
260 270
  #NAflag,file_format,out_suffix etc...
271
  NA_flag_val <- list_param$NA_flag
272
  file_format <- list_param$file_format
273
  out_suffix_str <- list_param$out_suffix_str
261 274
  out_dir_str <- list_param$out_dir_str
262 275
    
263
  ####### START SCRIPT #####
276
  ##### Prepare weight layers  
264 277
  
265 278
  r_in <- raster(lf[i]) #input image
266
  tile_no <- i
279
  tile_no <- i #file being processed, assuming tiles by tiles
267 280
  
268 281
  set1f <- function(x){rep(NA, x)}
269 282
  r_init <- init(r_in, fun=set1f)
......
323 336
      r_init[1:n_row,1] <- 1
324 337
      r_init[1:n_row,n_col] <- 1
325 338
      #r_dist <- distance(r_init)
326
      srcfile <- file.path(out_dir,paste("feature_target_",tile_no,"_",Sys.getpid(),".tif",sep=""))
339
      #out_suffix_str
340
      srcfile <- file.path(out_dir_str,paste("feature_target_",tile_no,"_",Sys.getpid(),out_suffix_str,file_format,sep=""))
327 341

  
328 342
      writeRaster(r_init,filename=srcfile,overwrite=T)
329 343
      #Sys.getpid
330
      dstfile <- file.path(out_dir,paste("feature_target_edge_distance",tile_no,"_",Sys.getpid(),".tif",sep=""))
344
      dstfile <- file.path(out_dir_str,paste("feature_target_edge_distance",tile_no,"_",Sys.getpid(),out_suffix_str,file_format,sep=""))
331 345
      n_values <- "1"
332 346
        
333 347
      cmd_str <- paste("gdal_proximity.py", srcfile, dstfile,"-values",n_values,sep=" ")
......
348 362
  
349 363
  extension_str <- extension(lf[i])
350 364
  raster_name_tmp <- gsub(extension_str,"",basename(lf[i]))
351
  raster_name <- file.path(out_dir_str,paste(raster_name_tmp,"_",method,"_weights.tif",sep=""))
365
  raster_name <- file.path(out_dir_str,paste(raster_name_tmp,"_",method,"_weights_",out_suffix_str,file_format,sep=""))
352 366
  writeRaster(r, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)  
353 367
  
354 368
  r_var_prod <- r_in*r
355
  raster_name_prod <- file.path(out_dir_str, paste(raster_name_tmp,"_",method,"_prod_weights.tif",sep=""))
369
  raster_name_prod <- file.path(out_dir_str, paste(raster_name_tmp,"_",method,"_prod_weights_",out_suffix_str,file_format,sep=""))
356 370
  writeRaster(r_var_prod, NAflag=NA_flag_val,filename=raster_name_prod,overwrite=TRUE)  
357 371
    
358 372
  weights_obj <- list(raster_name,raster_name_prod)
......
449 463
  return(raster_name)
450 464
}
451 465

  
452
mosaicFiles <- function(lf_mosaic,mosaic_method="unweighted",num_cores=1,r_mask_raster_name=NULL,python_bin=NULL,mosaic_python="/nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum_noDataTest.py",algorithm="R",match_extent=TRUE,df_points=NULL,NA_flag_val=-9999,file_format=".tif",out_suffix=NULL,out_dir=NULL){
466
mosaicFiles <- function(lf_mosaic,mosaic_method="unweighted",num_cores=1,r_mask_raster_name=NULL,python_bin=NULL,mosaic_python="/nobackupp6/aguzman4/climateLayers/sharedCode/gdal_merge_sum_noDataTest.py",algorithm="R",match_extent=TRUE,df_points=NULL,NA_flag_val=-9999,file_format=".tif",out_suffix=NULL,out_dir=NULL,tmp_files=FALSE){
453 467
  #This functions mosaics tiles/files give a list of files. 
454 468
  #There are four options to mosaic:   use_sine_weights,use_edge,use_linear_weights, unweighted
455 469
  #Sine weights fits sine fuctions across rows and column producing elliptical/spherical patterns from center
......
473 487
  #11)out_dir: output directory, default is NULL
474 488
  #12)algorithm: use R or python function
475 489
  #13)match extent: if TRUE match extent before mosaicing
490
  #14)tmp_files: if TRUE then keep temporary files
476 491
  #
477 492
  #OUTPUT:
478 493
  # Object is produced with 3 components:
......
487 502
  
488 503
  out_dir_str <- out_dir
489 504

  
505
  if(tmp_file==T){
506
    out_suffix_str_tmp <- paste0(out_suffix,"_tmp")
507
  }
508
  
490 509
  lf_r_weights <- vector("list",length=length(lf_mosaic))
491 510
  
492 511
  ###############
......
499 518
    #df_points <- NULL
500 519
    r_feature <- NULL
501 520

  
502
    list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) 
503
    names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") 
521
    #lf <- list_param$lf
522
    #df_points <- list_param$df_points
523
    #r_feature <- list_param$r_feature #this should be change to a list
524
    #padding <- TRUE #if padding true then make buffer around edges??
525
    #method <- list_param$method #differnt methods available to create weights
526
    #NAflag,file_format,out_suffix etc...
527
    #NA_flag_val <- list_param$NA_flag
528
    #file_format <- list_param$file_format
529
    #out_suffix_str <- list_param$out_suffix_str
530
    #out_dir_str <- list_param$out_dir_str
531
    list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,NA_flag_val,file_format,out_suffix_str_tmp,out_dir_str) 
532
    names(list_param_create_weights) <- c("lf","df_points","r_feature","method","NA_flag","file_format","out_suffix_str","out_dir_str") 
504 533
    #num_cores <- 11
505 534

  
506 535
    #debug(create_weights_fun)
......
525 554
    df_points <- NULL
526 555
    r_feature <- NULL
527 556

  
528
    list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) 
529
    names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") 
557
    #list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) 
558
    #names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") 
559
    list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,NA_flag_val,file_format,out_suffix_str_tmp,out_dir_str) 
560
    names(list_param_create_weights) <- c("lf","df_points","r_feature","method","NA_flag","file_format","out_suffix_str","out_dir_str") 
561

  
530 562
    #num_cores <- 11
531 563

  
532 564
    #debug(create_weights_fun)
......
550 582
    method <- "use_edge"
551 583
    df_points <- NULL
552 584
    r_feature <- NULL
553
    list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) 
554
    names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") 
585
    
586
    list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,NA_flag_val,file_format,out_suffix_str_tmp,out_dir_str) 
587
    names(list_param_create_weights) <- c("lf","df_points","r_feature","method","NA_flag","file_format","out_suffix_str","out_dir_str") 
588
    #list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) 
589
    #names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") 
555 590
    #num_cores <- 11
556 591
    #debug(create_weights_fun)
557 592
    #weights_obj <- create_weights_fun(3,list_param=list_param_create_weights)

Also available in: Unified diff