Revision 3d3c6f37
Added by Benoit Parmentier over 9 years ago
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/10/2015
|
|
8 |
#MODIFIED ON: 06/15/2015
|
|
9 | 9 |
#Version: 4 |
10 | 10 |
#PROJECT: Environmental Layers project |
11 | 11 |
#COMMENTS: analyses run for reg5 for test of mosaicing using 1500x4500km and other tiles |
... | ... | |
308 | 308 |
return(raster_name) |
309 | 309 |
} |
310 | 310 |
|
311 |
mosaicFiles <- function(lf_mosaic,mosaic_method,num_cores,python_bin=NULL,df_points=NULL,NA_flag_val=-9999,file_format=".tif",out_suffix=NULL,out_dir=NULL){ |
|
312 |
|
|
313 |
out_dir_str <- out_dir |
|
314 |
|
|
315 |
lf_r_weights <- vector("list",length=length(lf_mosaic)) |
|
316 |
|
|
317 |
############### |
|
318 |
### PART 2: prepare weights using tile rasters ############ |
|
319 |
#methods availbable:use_sine_weights,use_edge,use_linear_weights |
|
320 |
|
|
321 |
if(mosaic_method=="use_linear_weights"){ |
|
322 |
method <- "use_linear_weights" |
|
323 |
df_points <- df_centroids |
|
324 |
#df_points <- NULL |
|
325 |
r_feature <- NULL |
|
326 |
|
|
327 |
list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
|
328 |
names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
|
329 |
#num_cores <- 11 |
|
330 |
|
|
331 |
#debug(create_weights_fun) |
|
332 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
333 |
|
|
334 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
|
335 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
|
336 |
linear_weights_obj_list <- mclapply(1:length(lf_mosaic),FUN=create_weights_fun,list_param=list_param_create_weights,mc.preschedule=FALSE,mc.cores = num_cores) |
|
337 |
|
|
338 |
list_linear_r_weights <- lapply(1:length(linear_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=linear_weights_obj_list) |
|
339 |
list_linear_r_weights_prod <- lapply(1:length(linear_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights_prod},x=linear_weights_obj_list) |
|
340 |
|
|
341 |
list_weights <- list_linear_r_weights |
|
342 |
list_weights_prod <- list_linear_r_weights_prod |
|
343 |
|
|
344 |
} |
|
345 |
if(mosaic_method=="use_sine_weights"){ |
|
346 |
|
|
347 |
### Third use sine weights |
|
348 |
method <- "use_sine_weights" |
|
349 |
#df_points <- df_centroids |
|
350 |
df_points <- NULL |
|
351 |
r_feature <- NULL |
|
352 |
|
|
353 |
list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
|
354 |
names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
|
355 |
#num_cores <- 11 |
|
356 |
|
|
357 |
#debug(create_weights_fun) |
|
358 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
359 |
|
|
360 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
|
361 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
|
362 |
sine_weights_obj_list <- mclapply(1:length(lf_mosaic),FUN=create_weights_fun,list_param=list_param_create_weights,mc.preschedule=FALSE,mc.cores = num_cores) |
|
363 |
|
|
364 |
list_sine_r_weights <- lapply(1:length(sine_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=sine_weights_obj_list) |
|
365 |
list_sine_r_weights_prod <- lapply(1:length(sine_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights_prod},x=sine_weights_obj_list) |
|
366 |
|
|
367 |
list_weights <- list_sine_r_weights |
|
368 |
list_weights_prod <- list_sine_r_weights_prod |
|
369 |
|
|
370 |
} |
|
371 |
|
|
372 |
if(mosaic_method=="use_edge_weights"){ |
|
373 |
|
|
374 |
method <- "use_edge" |
|
375 |
df_points <- NULL |
|
376 |
r_feature <- NULL |
|
377 |
list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
|
378 |
names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
|
379 |
#num_cores <- 11 |
|
380 |
|
|
381 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
382 |
|
|
383 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
|
384 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
|
385 |
use_edge_weights_obj_list <- mclapply(1:length(lf_mosaic),FUN=create_weights_fun,list_param=list_param_create_weights,mc.preschedule=FALSE,mc.cores = num_cores) |
|
386 |
|
|
387 |
list_edge_r_weights <- lapply(1:length(use_edge_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=use_edge_weights_obj_list) |
|
388 |
list_edge_r_weights_prod <- lapply(1:length(use_edge_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights_prod},x=use_edge_weights_obj_list) |
|
389 |
|
|
390 |
#simplifly later... |
|
391 |
list_weights <- list_edge_r_weights |
|
392 |
list_weights_prod <- list_edge_r_weights_prod |
|
393 |
#r_test <- raster(list_edge_r_weights[[1]]) |
|
394 |
|
|
395 |
} |
|
396 |
|
|
397 |
############### |
|
398 |
### PART 3: prepare weightsfor mosaicing by matching extent ############ |
|
399 |
|
|
400 |
## Rasters tiles vary slightly in resolution, they need to be matched for the mosaic. Resolve issue in the |
|
401 |
#mosaic funciton using gdal_merge to compute a reference image to mach. |
|
402 |
|
|
403 |
cmd_str <- paste("python","/usr/bin/gdal_merge.py","-o avg.tif",paste(lf_mosaic,collapse=" ")) |
|
404 |
system(cmd_str) |
|
405 |
|
|
406 |
## Create raster image for original predicted images with matching resolution and extent to the mosaic (reference image) |
|
407 |
|
|
408 |
rast_ref <- file.path(out_dir,"avg",out_suffix,file_format) #this is a the ref |
|
409 |
r_ref <- raster(rast_ref) |
|
410 |
#plot(r_ref) |
|
411 |
|
|
412 |
if(method_str%in%c("use_linear_weights","use_sine_weights","use_edge_weights")){ |
|
413 |
|
|
414 |
lf_files <- unlist(list_r_weights) |
|
415 |
|
|
416 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
417 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
418 |
|
|
419 |
#debug(raster_match) |
|
420 |
#r_test <- raster(raster_match(1,list_param_raster_match)) |
|
421 |
|
|
422 |
list_linear_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
423 |
|
|
424 |
lf_files <- unlist(list_r_weights_prod) |
|
425 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
426 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
427 |
|
|
428 |
#num_cores <-11 |
|
429 |
list_linear_weights_prod_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
430 |
lf_files <- unlist(list_linear_r_weights) |
|
431 |
|
|
432 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
433 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
434 |
|
|
435 |
#debug(raster_match) |
|
436 |
#r_test <- raster(raster_match(1,list_param_raster_match)) |
|
437 |
|
|
438 |
list_linear_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
439 |
|
|
440 |
lf_files <- unlist(list_linear_r_weights_prod) |
|
441 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
442 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
443 |
|
|
444 |
num_cores <-11 |
|
445 |
list_linear_weights_prod_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
446 |
|
|
447 |
|
|
448 |
##################### |
|
449 |
###### PART 4: compute the weighted mean with the mosaic function ##### |
|
450 |
|
|
451 |
##Make this a function later |
|
452 |
#list_weights_m <- list(list_linear_weights_m,list_edge_weights_m,list_sine_weights_m) |
|
453 |
#list_weights_prod_m <- list(list_linear_weights_prod_m,list_edge_weights_prod_m,list_sine_weights_prod_m) |
|
454 |
#list_methods <- c("linear","edge","sine") |
|
455 |
list_mosaiced_files <- vector("list",length=1) |
|
456 |
|
|
457 |
list_args_weights <- list_weights_m |
|
458 |
list_args_weights_prod <- list_weights_prod_m |
|
459 |
method_str <- list_methods |
|
460 |
|
|
461 |
#list_args_weights <- (mixedsort(list.files(pattern="r_weights_m_.*.tif"))) |
|
462 |
list_args_weights <- lapply(1:length(list_args_weights), FUN=function(i,x){raster(x[[i]])},x=list_args_weights) |
|
463 |
|
|
464 |
#get the list of weights product into raster objects |
|
465 |
#list_args_weights_prod <- list_weights_prod_m |
|
466 |
#list_args_weights_prod <- (mixedsort(list.files(pattern="r_weights_prod_m_.*.tif"))) |
|
467 |
list_args_weights_prod <- lapply(1:length(list_args_weights_prod), FUN=function(i,x){raster(x[[i]])},x=list_args_weights_prod) |
|
468 |
|
|
469 |
list_args_weights_prod$fun <- "sum" |
|
470 |
list_args_weights_prod$na.rm <- TRUE |
|
471 |
|
|
472 |
list_args_weights$fun <- "sum" #we want the sum to compute the weighted mean |
|
473 |
list_args_weights$na.rm <- TRUE |
|
474 |
|
|
475 |
#Mosaic files |
|
476 |
r_weights_sum <- do.call(mosaic,list_args_weights) #weights sum image mosaiced |
|
477 |
r_prod_sum <- do.call(mosaic,list_args_weights_prod) #weights sum product image mosacied |
|
478 |
|
|
479 |
r_m_weighted_mean <- r_prod_sum/r_weights_sum #this is the mosaic using weighted mean... |
|
480 |
raster_name <- file.path(out_dir,paste("r_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep="")) |
|
481 |
writeRaster(r_m_weighted_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE) |
|
482 |
|
|
483 |
} |
|
484 |
|
|
485 |
if(method_mosaic=="unweighted"){ |
|
486 |
#### Fourth use original images |
|
487 |
#macth file to mosaic extent using the original predictions |
|
488 |
lf_files <- lf_mosaic |
|
489 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
490 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
491 |
|
|
492 |
list_pred_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
493 |
|
|
494 |
#list_mosaiced_files <- list.files(pattern="r_m.*._weighted_mean_.*.tif") |
|
495 |
|
|
496 |
#names(list_mosaiced_files) <- c("edge","linear","sine") |
|
497 |
|
|
498 |
#### NOW unweighted mean mosaic |
|
499 |
|
|
500 |
#get the original predicted image to raster (matched previously to the mosaic extent) |
|
501 |
list_args_pred_m <- list_pred_m |
|
502 |
#list_args_pred_m <- (mixedsort(list.files(pattern="^gam_CAI.*.m_mosaic_run10_1500x4500_global_analyses_03252015.tif"))) |
|
503 |
list_args_pred_m <- lapply(1:length(list_args_pred_m), FUN=function(i,x){raster(x[[i]])},x=list_args_pred_m) |
|
504 |
|
|
505 |
list_args_pred_m$fun <- "mean" |
|
506 |
list_args_pred_m$na.rm <- TRUE |
|
507 |
|
|
508 |
#Mosaic files |
|
509 |
r_m_mean <- do.call(mosaic,list_args_pred_m) #this is unweighted mean from the predicted raster |
|
510 |
|
|
511 |
raster_name <- file.path(out_dir,paste("r_m_mean_",out_suffix,".tif",sep="")) |
|
512 |
writeRaster(r_m_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE) #unweighted mean |
|
513 |
|
|
514 |
r_m_mean_unweighted <- paste("r_m_mean_",out_suffix,".tif",sep="") |
|
515 |
|
|
516 |
} |
|
517 |
|
|
518 |
#Create return object |
|
519 |
mosaic_obj <- list(raster_name,list_r_weights,list_r_weights_prod,method) |
|
520 |
names(mosaic_obj) <- c("mean_mosaic","r_weights","r_weigths_prod","method") |
|
521 |
return(mosaic_obj) |
|
522 |
} |
|
523 |
|
|
524 |
|
|
311 | 525 |
|
312 | 526 |
############################################ |
313 | 527 |
#### Parameters and constants |
314 | 528 |
|
315 |
#Data is on ATLAS |
|
529 |
#Data is on ATLAS: reg4 (South America)
|
|
316 | 530 |
|
317 | 531 |
in_dir <- "/data/project/layers/commons/NEX_data/mosaicing_data_test" #reg1 is North America and reg5 is Africa |
318 | 532 |
#in_dir <- "/data/project/layers/commons/NEX_data/mosaicing_data_test/reg1" |
... | ... | |
321 | 535 |
|
322 | 536 |
y_var_name <- "dailyTmax" #PARAM1 |
323 | 537 |
interpolation_method <- c("gam_CAI") #PARAM2 |
324 |
region_name <- "reg5" #PARAM 13 #reg1 is North America, Africa Region 5
|
|
538 |
region_name <- "reg4" #PARAM 13 #reg4 is South America, Africa Region 5
|
|
325 | 539 |
|
326 |
out_suffix <- paste(region_name,"_","mosaic_run10_1500x4500_global_analyses_06102015",sep="")
|
|
540 |
out_suffix <- paste(region_name,"_","mosaic_run10_1500x4500_global_analyses_06152015",sep="")
|
|
327 | 541 |
#PARAM3 |
328 | 542 |
out_dir <- in_dir #PARAM4 |
329 | 543 |
create_out_dir_param <- TRUE #PARAM 5 |
... | ... | |
386 | 600 |
df_centroids$ID <- as.numeric(1:nrow(df_centroids)) |
387 | 601 |
coordinates(df_centroids) <- cbind(df_centroids$long,df_centroids$lat) |
388 | 602 |
proj4string(df_centroids) <- projection(r1) |
389 |
|
|
390 |
############### |
|
391 |
### PART 2: prepare weights using tile rasters ############ |
|
392 |
|
|
393 |
out_dir_str <- out_dir |
|
394 |
lf_r_weights <- vector("list",length=length(lf_mosaic)) |
|
395 |
|
|
396 |
#methods availbable:use_sine_weights,use_edge,use_linear_weights |
|
397 |
|
|
398 |
### First use linear weights |
|
399 |
|
|
400 |
method <- "use_linear_weights" |
|
401 | 603 |
df_points <- df_centroids |
402 |
#df_points <- NULL |
|
403 |
r_feature <- NULL |
|
404 |
|
|
405 |
list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
|
406 |
names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
|
407 |
num_cores <- 11 |
|
408 |
|
|
409 |
#debug(create_weights_fun) |
|
410 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
411 |
|
|
412 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
|
413 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
|
414 |
linear_weights_obj_list <- mclapply(1:length(lf_mosaic),FUN=create_weights_fun,list_param=list_param_create_weights,mc.preschedule=FALSE,mc.cores = num_cores) |
|
415 |
|
|
416 |
list_linear_r_weights <- lapply(1:length(linear_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=linear_weights_obj_list) |
|
417 |
list_linear_r_weights_prod <- lapply(1:length(linear_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights_prod},x=linear_weights_obj_list) |
|
418 |
|
|
419 |
### Second use distance from edge weights |
|
420 |
|
|
421 |
method <- "use_edge" |
|
422 |
df_points <- NULL |
|
423 |
r_feature <- NULL |
|
424 |
list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
|
425 |
names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
|
426 |
num_cores <- 11 |
|
427 |
|
|
428 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
429 |
|
|
430 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
|
431 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
|
432 |
use_edge_weights_obj_list <- mclapply(1:length(lf_mosaic),FUN=create_weights_fun,list_param=list_param_create_weights,mc.preschedule=FALSE,mc.cores = num_cores) |
|
433 |
|
|
434 |
list_edge_r_weights <- lapply(1:length(use_edge_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=use_edge_weights_obj_list) |
|
435 |
list_edge_r_weights_prod <- lapply(1:length(use_edge_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights_prod},x=use_edge_weights_obj_list) |
|
436 |
|
|
437 |
#r_test <- raster(list_edge_r_weights[[1]]) |
|
438 |
|
|
439 |
### Third use sine weights |
|
440 |
method <- "use_sine_weights" |
|
441 |
#df_points <- df_centroids |
|
442 |
df_points <- NULL |
|
443 |
r_feature <- NULL |
|
444 |
|
|
445 |
list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
|
446 |
names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
|
447 |
num_cores <- 11 |
|
448 |
|
|
449 |
#debug(create_weights_fun) |
|
450 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
451 |
|
|
452 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
|
453 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
|
454 |
sine_weights_obj_list <- mclapply(1:length(lf_mosaic),FUN=create_weights_fun,list_param=list_param_create_weights,mc.preschedule=FALSE,mc.cores = num_cores) |
|
604 |
#methods availbable:use_sine_weights,use_edge,use_linear_weights |
|
455 | 605 |
|
456 |
list_sine_r_weights <- lapply(1:length(sine_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=sine_weights_obj_list)
|
|
457 |
list_sine_r_weights_prod <- lapply(1:length(sine_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights_prod},x=sine_weights_obj_list)
|
|
606 |
debug(mosaicFiles)
|
|
607 |
mosaic_edge_20100901_obj <- mosaicFiles(lf_mosaic,mosaic_method="use_edge_weights",python_bin=NULL,NA_flag,file_format,num_cores,out_suffix=out_suffix)
|
|
458 | 608 |
|
459 | 609 |
## |
460 | 610 |
|
... | ... | |
466 | 616 |
#m_val= sum_val/weight_sum #mean value |
467 | 617 |
# |
468 | 618 |
|
469 |
############### |
|
470 |
### PART 3: prepare weightsfor mosaicing by matching extent ############ |
|
471 |
|
|
472 |
## Rasters tiles vary slightly in resolution, they need to be matched for the mosaic. Resolve issue in the |
|
473 |
#mosaic funciton using gdal_merge to compute a reference image to mach. |
|
474 |
|
|
475 |
cmd_str <- paste("python","/usr/bin/gdal_merge.py","-o avg.tif",paste(lf_mosaic,collapse=" ")) |
|
476 |
system(cmd_str) |
|
477 |
|
|
478 |
## Create raster image for original predicted images with matching resolution and extent to the mosaic (reference image) |
|
479 |
|
|
480 |
rast_ref <- file.path(out_dir,"avg.tif") #this is a the ref |
|
481 |
r_ref <- raster(rast_ref) |
|
482 |
plot(r_ref) |
|
483 |
|
|
484 |
### First match weights from linear option |
|
485 |
lf_files <- unlist(list_linear_r_weights) |
|
486 |
|
|
487 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
488 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
489 |
|
|
490 |
#debug(raster_match) |
|
491 |
#r_test <- raster(raster_match(1,list_param_raster_match)) |
|
492 |
|
|
493 |
list_linear_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
494 |
|
|
495 |
lf_files <- unlist(list_linear_r_weights_prod) |
|
496 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
497 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
498 |
|
|
499 |
num_cores <-11 |
|
500 |
list_linear_weights_prod_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
501 |
|
|
502 |
#### Second use use edge (dist) images |
|
503 |
|
|
504 |
lf_files <- unlist(list_edge_r_weights) |
|
505 |
|
|
506 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
507 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
619 |
#### Now run unweighted |
|
508 | 620 |
|
509 |
#debug(raster_match) |
|
510 |
#r_test <- raster(raster_match(1,list_param_raster_match)) |
|
511 |
|
|
512 |
list_edge_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
513 |
|
|
514 |
lf_files <- unlist(list_edge_r_weights_prod) |
|
515 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
516 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
517 |
|
|
518 |
num_cores <-11 |
|
519 |
list_edge_weights_prod_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
520 |
|
|
521 |
|
|
522 |
### third match wegihts from sine option |
|
523 |
|
|
524 |
lf_files <- unlist(list_sine_r_weights) |
|
525 |
|
|
526 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
527 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
528 |
|
|
529 |
#debug(raster_match) |
|
530 |
#r_test <- raster(raster_match(1,list_param_raster_match)) |
|
531 |
|
|
532 |
list_sine_weights_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
533 |
|
|
534 |
lf_files <- unlist(list_sine_r_weights_prod) |
|
535 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
536 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
537 |
|
|
538 |
num_cores <-11 |
|
539 |
list_sine_weights_prod_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
540 |
|
|
541 |
#### Fourth use original images |
|
542 |
#macth file to mosaic extent using the original predictions |
|
543 |
lf_files <- lf_mosaic |
|
544 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
|
545 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","out_suffix","out_dir_str") |
|
546 |
|
|
547 |
list_pred_m <- mclapply(1:length(lf_files),FUN=raster_match,list_param=list_param_raster_match,mc.preschedule=FALSE,mc.cores = num_cores) |
|
548 |
|
|
549 |
##################### |
|
550 |
###### PART 4: compute the weighted mean with the mosaic function ##### |
|
551 |
|
|
552 |
##Make this a function later |
|
553 |
list_weights_m <- list(list_linear_weights_m,list_edge_weights_m,list_sine_weights_m) |
|
554 |
list_weights_prod_m <- list(list_linear_weights_prod_m,list_edge_weights_prod_m,list_sine_weights_prod_m) |
|
555 |
list_methods <- c("linear","edge","sine") |
|
556 |
list_mosaiced_files <- vector("list",length=length(list_methods)) |
|
557 |
|
|
558 |
for(k in 1:length(list_methods)){ |
|
559 |
|
|
560 |
list_args_weights <- list_weights_m[[k]] |
|
561 |
list_args_weights_prod <- list_weights_prod_m[[k]] |
|
562 |
method_str <- list_methods[[k]] |
|
563 |
|
|
564 |
#list_args_weights <- (mixedsort(list.files(pattern="r_weights_m_.*.tif"))) |
|
565 |
list_args_weights <- lapply(1:length(list_args_weights), FUN=function(i,x){raster(x[[i]])},x=list_args_weights) |
|
566 |
|
|
567 |
#get the list of weights product into raster objects |
|
568 |
#list_args_weights_prod <- list_weights_prod_m |
|
569 |
#list_args_weights_prod <- (mixedsort(list.files(pattern="r_weights_prod_m_.*.tif"))) |
|
570 |
list_args_weights_prod <- lapply(1:length(list_args_weights_prod), FUN=function(i,x){raster(x[[i]])},x=list_args_weights_prod) |
|
571 |
|
|
572 |
list_args_weights_prod$fun <- "sum" |
|
573 |
list_args_weights_prod$na.rm <- TRUE |
|
574 |
|
|
575 |
list_args_weights$fun <- "sum" #we want the sum to compute the weighted mean |
|
576 |
list_args_weights$na.rm <- TRUE |
|
577 |
|
|
578 |
#Mosaic files |
|
579 |
r_weights_sum <- do.call(mosaic,list_args_weights) #weights sum image mosaiced |
|
580 |
r_prod_sum <- do.call(mosaic,list_args_weights_prod) #weights sum product image mosacied |
|
581 |
|
|
582 |
r_m_weighted_mean <- r_prod_sum/r_weights_sum #this is the mosaic using weighted mean... |
|
583 |
raster_name <- file.path(out_dir,paste("r_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep="")) |
|
584 |
writeRaster(r_m_weighted_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE) |
|
585 |
|
|
586 |
} |
|
587 |
|
|
588 |
list_mosaiced_files <- list.files(pattern="r_m.*._weighted_mean_.*.tif") |
|
589 |
|
|
590 |
names(list_mosaiced_files) <- c("edge","linear","sine") |
|
591 |
|
|
592 |
#### NOW unweighted mean mosaic |
|
593 |
|
|
594 |
#get the original predicted image to raster (matched previously to the mosaic extent) |
|
595 |
list_args_pred_m <- list_pred_m |
|
596 |
#list_args_pred_m <- (mixedsort(list.files(pattern="^gam_CAI.*.m_mosaic_run10_1500x4500_global_analyses_03252015.tif"))) |
|
597 |
list_args_pred_m <- lapply(1:length(list_args_pred_m), FUN=function(i,x){raster(x[[i]])},x=list_args_pred_m) |
|
598 |
|
|
599 |
list_args_pred_m$fun <- "mean" |
|
600 |
list_args_pred_m$na.rm <- TRUE |
|
601 |
|
|
602 |
#Mosaic files |
|
603 |
r_m_mean <- do.call(mosaic,list_args_pred_m) #this is unweighted mean from the predicted raster |
|
604 |
|
|
605 |
raster_name <- file.path(out_dir,paste("r_m_mean_",out_suffix,".tif",sep="")) |
|
606 |
writeRaster(r_m_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE) #unweighted mean |
|
607 |
|
|
608 |
r_m_mean_unweighted <- paste("r_m_mean_",out_suffix,".tif",sep="") |
|
621 |
#.... |
|
609 | 622 |
|
610 | 623 |
##################### |
611 | 624 |
###### PART 5: Now plot of the weighted mean and unweighted mean with the mosaic function ##### |
... | ... | |
721 | 734 |
|
722 | 735 |
dev.off() |
723 | 736 |
|
724 |
|
|
725 |
|
|
726 | 737 |
##################### END OF SCRIPT ###################### |
727 | 738 |
|
Also available in: Unified diff
creating mosaicFiles function to ease automation of comparison