Project

General

Profile

« Previous | Next » 

Revision 874ff0fa

Added by Benoit Parmentier over 9 years ago

scaling up mosacing, setting up function to produce weights based on distance to tile centroids

View differences:

climate/research/oregon/interpolation/global_run_scalingup_mosaicing.R
209 209

  
210 210
}
211 211

  
212
create_weights_fun <- function(i, list_param){
213
  #This function generates weights from a point location on a raster layer.
214
  #Note that the weights are normatlized on 0-1 scale using max and min values.
215
  #Inputs:
216
  #lf: list of raster files
217
  #df_points: 
218
  #Outputs:
219
  #
220
  ############
221
  
222
  lf <- list_param$lf
223
  df_centroids <- list_param$df_points
224
  out_dir_str <- list_param$out_dir_str
225
    
226
  ####### START SCRIPT #####
227
  
228
  r1 <- raster(lf[i]) #input image
229

  
230
  set1f <- function(x){rep(NA, x)}
231
  r_init <- init(r1, fun=set1f)
232

  
233
  cell_ID <- cellFromXY(r_init,xy=df_centroids[i,])
234
  r_init[cell_ID] <- df_centroids$ID[i]
235

  
236
  r_dist <- distance(r_init)
237
  min_val <- cellStats(r_dist,min) 
238
  max_val <- cellStats(r_dist,max)
239
  r_dist_normalized <- abs(r_dist - max_val)/ (max_val - min_val)
240
  
241
  extension_str <- extension(lf_mosaic_pred_1500x4500[i])
242
  raster_name_tmp <- gsub(extension_str,"",basename(lf_mosaic_pred_1500x4500[i]))
243
  raster_name <- file.path(out_dir_str,paste(raster_name_tmp,"_weights.tif",sep=""))
244
  writeRaster(r_dist_normalized, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)  
245
  
246
  r_var_prod <- r1*r_dist_normalized
247
  raster_name_prod <- file.path(out_dir_str, paste(raster_name_tmp,"_prod_weights.tif"))
248
  writeRaster(r_var_prod, NAflag=NA_flag_val,filename=raster_name_prod,overwrite=TRUE)  
249
    
250
  weights_obj <- list(raster_name,raster_name_prod)
251
  names(weights_obj) <- c("r_weights","r_weights_prod")
252
  return(weights_obj)
253
}
254

  
212 255
############################################
213 256
#### Parameters and constants  
214 257

  
......
292 335
#then distance...
293 336
out_dir_str <- out_dir
294 337
lf_r_weights <- vector("list",length=length(lf_mosaic_pred_1500x4500))
295
  
296
for(i in 1:length(lf)){
297
  #
298
  r1 <- raster(lf_mosaic_pred_1500x4500[i]) 
299 338

  
300
  set1f <- function(x){rep(NA, x)}
301
  r_init <- init(r1, fun=set1f, filename='test.grd', overwrite=TRUE)
339
list_param_create_weights <- list(lf_mosaic_pred_1500x4500,df_centroids,out_dir_str) 
340
names(list_param_create_weights) <- c("lf","df_points","out_dir_str") 
302 341

  
303
  cell_ID <- cellFromXY(r_init,xy=df_centroids[i,])
304
  r_init[cell_ID] <- df_centroids$ID[i]
342
create_weights_fun
343
num_cores <- 6
305 344

  
306
  r_dist <- distance(r_init)
307
  min_val <- cellStats(r_dist,min) 
308
  max_val <- cellStats(r_dist,max)
309
  r_dist_normalized <- abs(r_dist - max_val)/ (max_val - min_val)
310
  
311
  extension_str <- extension(lf_mosaic_pred_1500x4500[i])
312
  raster_name_tmp <- gsub(extension_str,"",basename(lf_mosaic_pred_1500x4500[i]))
313
  raster_name <- file.path(out_dir_str,paste(raster_name_tmp,"_weights.tif",sep=""))
314
  writeRaster(r_dist_normalized, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)  
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
  
320
  lf_r_weights[[i]] <- raster_name
321
}
345
#debug(create_weights_fun)
346
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights)
347

  
348
weights_obj_list <- mclapply(1:length(lf_mosaic_pred_1500x4500),FUN=create_weights_fun,list_param=list_param_create_weights,mc.preschedule=FALSE,mc.cores = num_cores)                           
349

  
350
list_args_weights_prod <- lapply(1:length(weights_obj_list), FUN=function(x){raster(x[[i]]$r_weights_prod})}
351
list_args_weights_prod$fun <- "sum"
352

  
353

  
354
#"r_weights","r_weights_prod"
355

  
356
list_args_weights <- lapply(1:length(weights_obj_list), FUN=function(i,x){raster(x[[i]]$r_weights)},x=weights_obj_list)
357
list_args_weights_prod <- lapply(1:length(weights_obj_list), FUN=function(i,x){raster(x[[i]]$r_weights_prod)},x=weights_obj_list)
358

  
359
list_args_weights$fun <- "sum"
360
#list_args_weights$fun <- "mean"
361

  
362
list_args_weights$na.rm <- TRUE
363
list_args_weights$tolerance <- 1
364

  
365
list_args_weights_prod$fun <- "sum"
366
list_args_weights_prod$na.rm <- TRUE
367
list_args_weights_prod$na.rm <- TRUE
368

  
369
r_weights_sum <- do.call(mosaic,list_args_weights) #sum
370
r_prod_sum <- do.call(mosaic,list_args_weights_prod) #sum
371

  
372
r_weighted_mean <- r_weights_sum/r_prod_sum
373

  
374

  
375
#r_weights_sum <- do.call(overlay,list_args_weights) #sum
376
#r_weights_sum <- do.call(overlay,list_args_weights) #sum
377

  
378
#r_test_w <-do.call(overlay,list_args_w) #prod
322 379

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

  
326 381
###Mosaic with do.call...
327 382
#rasters1.mosaicargs <- rasters1
......
343 398

  
344 399
#################################################
345 400
#Ok testing on fake data:
401

  
402
##Quick function to generate test dataset
403
make_raster_from_lf <- function(i,list_lf,r_ref){
404
  vect_val <- list_lf[[i]]
405
  r <-  r_ref
406
  values(r) <-vect_val
407
  #writeRaster...
408
  return(r)
409
}
410

  
346 411
vect_pred1 <- c(9,4,1,3,5,9,9,9,2)
347 412
vect_pred2 <- c(10,3,1,2,4,8,7,8,2)
348 413
vect_pred3 <- c(10,NA,NA,3,5,9,8,9,2)
......
354 419
vect_w3 <- c(0.5,0.3,0.2,0.2,0.3,0.6,0.7,0.3,0.2)
355 420
vect_w4 <- c(0.2,0.5,0.3,0.3,0.4,0.5,0.5,0.2,0.2)
356 421
lf_vect_w <- list(vect_w1,vect_w2,vect_w3,vect_w4)
357
test <-do.call(cbind,lf_vect_w)
422
df_vect_w <-do.call(cbind,lf_vect_w)
423
df_vect_pred <-do.call(cbind,lf_vect_pred)
358 424

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

  
......
363 429

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

  
366
list_args <- r_pred_l
367
list_args$fun <- "sum"
432
list_args_pred <- r_pred_l
433
list_args_pred$fun <- "sum"
368 434

  
369 435
list_args_w <- r_w_l
370 436
list_args_w$fun <- prod
......
378 444
r3<- r_w_l[[3]]*r_pred_l[[3]]
379 445
r4<- r_w_l[[4]]*r_pred_l[[4]]
380 446

  
447
r_pred <- stack(r_pred_l)
448
r_w <- stack(r_w_l)
449

  
450
list_args_pred <- r_pred_l
451
list_args_pred$fun <- mean
452
list_args_pred$na.rm <- TRUE
453
#r_sum_pred <-do.call(overlay,list_args_pred) #prod
454

  
455
#r_sum_pred <-do.call(mean,list_args_pred) #prod
456
r_sum_pred <-do.call(mosaic,list_args_pred) #prod
457

  
458
list_args_pred$na.rm <- FALSE
459
r_sum_pred <-do.call(overlay,list_args_pred) #prod
460

  
461
r_sum_pred <-do.call(overlay,list_args_w) #prod
462

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

  
......
386 468

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

  
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
}
398

  
399 471
#can do mosaic with sum?? for both weighted sum and val
400 472
#
401 473
#can use gdal calc...

Also available in: Unified diff