Project

General

Profile

« Previous | Next » 

Revision a19db606

Added by Benoit Parmentier over 9 years ago

scaling up mosacing experimation, creating artifical data to experiment with weighted averaging

View differences:

climate/research/oregon/interpolation/global_run_scalingup_mosaicing.R
312 312
  raster_name_tmp <- gsub(extension_str,"",basename(lf_mosaic_pred_1500x4500[i]))
313 313
  raster_name <- file.path(out_dir_str,paste(raster_name_tmp,"_weights.tif",sep=""))
314 314
  writeRaster(r_dist_normalized, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)  
315

  
315
  
316
  r_var_prod <- r1*r_dist_normalized
317
  raster_name <- file.path(out_dir_str, paste(raster_name_tmp,"_prod_weights.tif"))
318
  writeRaster(r_var_prod, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)  
319
  
316 320
  lf_r_weights[[i]] <- raster_name
317 321
}
318 322

  
323
l_inputs <- lapply(lf_mosaic_pred_1500x4500,raster) #list of raster
324
lf_r_weights
325

  
326
###Mosaic with do.call...
327
#rasters1.mosaicargs <- rasters1
328
#rasters1.mosaicargs$fun <- mean
329
#mos2 <- do.call(mosaic, rasters1.mosaicargs)
330

  
319 331
#Then scale on 1 to zero? or 0 to 1000
320 332
#e.g. for a specific pixel
321 333
#weight_sum=0.2 +0.4 +0.4+0.2=1.2
......
329 341
#r_val_w_sum <-  
330 342
#
331 343

  
344
#################################################
345
#Ok testing on fake data:
346
vect_pred1 <- c(9,4,1,3,5,9,9,9,2)
347
vect_pred2 <- c(10,3,1,2,4,8,7,8,2)
348
vect_pred3 <- c(10,NA,NA,3,5,9,8,9,2)
349
vect_pred4 <- c(9,3,2,NA,5,8,9,9,2)
350
lf_vect_pred <- list(vect_pred1,vect_pred2,vect_pred3,vect_pred4)
351

  
352
vect_w1 <- c(0.2,0.5,0.1,0.3,0.4,0.5,0.5,0.3,0.2)
353
vect_w2 <- c(0.3,0.4,0.1,0.3,0.4,0.5,0.7,0.1,0.2)
354
vect_w3 <- c(0.5,0.3,0.2,0.2,0.3,0.6,0.7,0.3,0.2)
355
vect_w4 <- c(0.2,0.5,0.3,0.3,0.4,0.5,0.5,0.2,0.2)
356
lf_vect_w <- list(vect_w1,vect_w2,vect_w3,vect_w4)
357
test <-do.call(cbind,lf_vect_w)
358

  
359
tr_ref <- raster(nrows=3,ncols=3)
360

  
361
r_pred_l <- lapply(1:length(lf_vect_pred),FUN=make_raster_from_lf,list_lf=lf_vect_pred,r_ref=r_ref)
362
r_w_l <- lapply(1:length(lf_vect_w),FUN=make_raster_from_lf,list_lf=lf_vect_w,r_ref=r_ref)
363

  
364
#r_w1<- make_raster_from_lf(2,list_lf=lf_vect_w,r_ref)
365

  
366
list_args <- r_pred_l
367
list_args$fun <- "sum"
368

  
369
list_args_w <- r_w_l
370
list_args_w$fun <- prod
371

  
372
r_test_val <-do.call(overlay,list_args) #sum
373
r_test_w <-do.call(overlay,list_args_w) #prod
374

  
375
#need to do sumprod
376
r1<- r_w_l[[1]]*r_pred_l[[1]]
377
r2<- r_w_l[[2]]*r_pred_l[[2]]
378
r3<- r_w_l[[3]]*r_pred_l[[3]]
379
r4<- r_w_l[[4]]*r_pred_l[[4]]
380

  
381
list_args_w$fun <- sum
382
r_sum_w <-do.call(overlay,list_args_w) #prod
383

  
384
r_m_w <- ((r1+r2+r3+r4)/(r_sum_w)) #mean weiated
385
#n33e to check the result!! especially the nubmer of valid pix val
386

  
387
#r_test_val <-do.call(overlay,list_args) #sum
388

  
389

  
390
##Quick function to generate test dataset
391
make_raster_from_lf <- function(i,list_lf,r_ref){
392
  vect_val <- list_lf[[i]]
393
  r <-  r_ref
394
  values(r) <-vect_val
395
  #writeRaster...
396
  return(r)
397
}
332 398

  
333
r1 <- raster(c)
334 399
#can do mosaic with sum?? for both weighted sum and val
335 400
#
336 401
#can use gdal calc...
......
341 406

  
342 407
#system("MODULEPATH=$MODULEPATH:/nex/modules/files")
343 408
#system("module load /nex/modules/files/pythonkits/gdal_1.10.0_python_2.7.3_nex")
344
lf1 <- lf_world_pred_1000x3000
345
lf2 <- lf_world_pred_1500x4500
409
#lf1 <- lf_world_pred_1000x3000
410
#lf2 <- lf_world_pred_1500x4500
346 411

  
347 412
#module_path <- ""
348 413
#module_path <- "/nobackupp6/aguzman4/climateLayers/sharedCode/"

Also available in: Unified diff