Revision b6948b65
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/05/2015
|
|
8 |
#MODIFIED ON: 06/09/2015
|
|
9 | 9 |
#Version: 4 |
10 | 10 |
#PROJECT: Environmental Layers project |
11 | 11 |
#COMMENTS: analyses for run 10 global analyses,all regions 1500x4500km and other tiles |
... | ... | |
116 | 116 |
####### START SCRIPT ##### |
117 | 117 |
|
118 | 118 |
r_in <- raster(lf[i]) #input image |
119 |
|
|
119 |
tile_no <- i |
|
120 |
|
|
120 | 121 |
set1f <- function(x){rep(NA, x)} |
121 | 122 |
r_init <- init(r_in, fun=set1f) |
122 | 123 |
|
... | ... | |
154 | 155 |
phase <- 0 |
155 | 156 |
use_cos <- FALSE |
156 | 157 |
vx <- sine_structure_fun(v,T,phase,a,b,use_cos) |
157 |
vx_rep <- unlist((lapply(1:n_row,FUN=function(i){rep(vx[i],time=n_col)})))
|
|
158 |
vx_rep <- unlist((lapply(1:n_row,FUN=function(j){rep(vx[j],time=n_col)})))
|
|
158 | 159 |
|
159 | 160 |
r2 <-setValues(r_init,vx_rep) |
160 | 161 |
#plot(r2) |
... | ... | |
174 | 175 |
r_init[n_row,1:n_col] <- 1 |
175 | 176 |
r_init[1:n_row,1] <- 1 |
176 | 177 |
r_init[1:n_row,n_col] <- 1 |
177 |
r_dist <- distance(r_init) |
|
178 |
#r_dist <- distance(r_init) |
|
179 |
srcfile <- file.path(out_dir,paste("feature_target_",tile_no,".tif",sep="")) |
|
180 |
|
|
181 |
writeRaster(r_init,filename=srcfile,overwrite=T) |
|
182 |
dstfile <- file.path(out_dir,paste("feature_target_edge_distance",tile_no,".tif",sep="")) |
|
183 |
n_values <- "1" |
|
184 |
|
|
185 |
cmd_str <- paste("gdal_proximity.py", srcfile, dstfile,"-values",n_values,sep=" ") |
|
186 |
system(cmd_str) |
|
187 |
r_dist<- raster(dstfile) |
|
178 | 188 |
min_val <- cellStats(r_dist,min) |
179 | 189 |
max_val <- cellStats(r_dist,max) |
180 |
r <- abs(r_dist - max_val)/ (max_val - min_val) |
|
181 |
} #too slow with R use http://www.gdal.org/gdal_proximity.html |
|
182 |
|
|
183 |
#plot(r_init) |
|
190 |
r <- abs(r_dist - min_val)/ (max_val - min_val) #no need to inverse... |
|
191 |
} #too slow with R so used http://www.gdal.org/gdal_proximity.html |
|
184 | 192 |
|
185 | 193 |
if(method=="use_linear_weights"){ |
186 | 194 |
# |
... | ... | |
314 | 322 |
interpolation_method <- c("gam_CAI") #PARAM2 |
315 | 323 |
region_name <- "reg2" #PARAM 13 #reg1 is North America, Africa Region 5 |
316 | 324 |
|
317 |
out_suffix <- paste(region_name,"_","mosaic_run10_1500x4500_global_analyses_06052015",sep="")
|
|
325 |
out_suffix <- paste(region_name,"_","mosaic_run10_1500x4500_global_analyses_06072015",sep="")
|
|
318 | 326 |
#PARAM3 |
319 | 327 |
out_dir <- in_dir #PARAM4 |
320 | 328 |
create_out_dir_param <- TRUE #PARAM 5 |
... | ... | |
387 | 395 |
#methods availbable:use_sine_weights,use_edge,use_linear_weights |
388 | 396 |
|
389 | 397 |
### First use linear weights |
398 |
|
|
390 | 399 |
method <- "use_linear_weights" |
391 | 400 |
df_points <- df_centroids |
392 | 401 |
#df_points <- NULL |
... | ... | |
406 | 415 |
list_linear_r_weights <- lapply(1:length(linear_weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=linear_weights_obj_list) |
407 | 416 |
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) |
408 | 417 |
|
409 |
### Second use sine weights |
|
418 |
### Second use distance from edge weights |
|
419 |
|
|
420 |
method <- "use_edge" |
|
421 |
df_points <- NULL |
|
422 |
r_feature <- NULL |
|
423 |
list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) |
|
424 |
names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") |
|
425 |
num_cores <- 11 |
|
426 |
|
|
427 |
weights_obj <- create_weights_fun(1,list_param=list_param_create_weights) |
|
428 |
|
|
429 |
#This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to |
|
430 |
#the edges...can use rows and columsn to set edges to 1 and 0 for the others. |
|
431 |
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) |
|
432 |
|
|
433 |
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) |
|
434 |
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) |
|
435 |
|
|
436 |
### Third use sine weights |
|
410 | 437 |
method <- "use_sine_weights" |
411 | 438 |
#df_points <- df_centroids |
412 | 439 |
df_points <- NULL |
... | ... | |
472 | 499 |
num_cores <-11 |
473 | 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) |
474 | 501 |
|
475 |
### Second match wegihts from sine option |
|
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") |
|
508 |
|
|
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 |
|
476 | 523 |
|
477 | 524 |
lf_files <- unlist(list_sine_r_weights) |
478 | 525 |
|
... | ... | |
491 | 538 |
num_cores <-11 |
492 | 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) |
493 | 540 |
|
494 |
#### Third use original images
|
|
541 |
#### Fourth use original images
|
|
495 | 542 |
#macth file to mosaic extent using the original predictions |
496 | 543 |
lf_files <- lf_mosaic |
497 | 544 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,out_suffix,out_dir) |
... | ... | |
502 | 549 |
##################### |
503 | 550 |
###### PART 4: compute the weighted mean with the mosaic function ##### |
504 | 551 |
|
552 |
##Make this a function later |
|
553 |
|
|
554 |
list_weights_m <- list(list_linear_weights_m,list_edge_weights_m,list_sine_weights_m) |
|
555 |
list_weights_prod_m <- list(list_linear_weights_prod_m,list_edge_weights_prod_m,list_sine_weights_prod_m) |
|
556 |
list_methods <- c("linear","edge","sine") |
|
557 |
list_mosaiced_files <- vector("list",length=length(list_methods)) |
|
558 |
|
|
559 |
for(k in 1:length(list_methods)){ |
|
560 |
|
|
561 |
list_args_weights <- list_weights_m[[k]] |
|
562 |
list_args_weights_prod <- list_weights_prod_m[[k]] |
|
563 |
method_str <- list_methods[[k]] |
|
564 |
|
|
565 |
#list_args_weights <- (mixedsort(list.files(pattern="r_weights_m_.*.tif"))) |
|
566 |
list_args_weights <- lapply(1:length(list_args_weights), FUN=function(i,x){raster(x[[i]])},x=list_args_weights) |
|
567 |
|
|
568 |
#get the list of weights product into raster objects |
|
569 |
#list_args_weights_prod <- list_weights_prod_m |
|
570 |
#list_args_weights_prod <- (mixedsort(list.files(pattern="r_weights_prod_m_.*.tif"))) |
|
571 |
list_args_weights_prod <- lapply(1:length(list_args_weights_prod), FUN=function(i,x){raster(x[[i]])},x=list_args_weights_prod) |
|
572 |
|
|
573 |
list_args_weights_prod$fun <- "sum" |
|
574 |
list_args_weights_prod$na.rm <- TRUE |
|
575 |
|
|
576 |
list_args_weights$fun <- "sum" #we want the sum to compute the weighted mean |
|
577 |
list_args_weights$na.rm <- TRUE |
|
578 |
|
|
579 |
#Mosaic files |
|
580 |
r_weights_sum <- do.call(mosaic,list_args_weights) #weights sum image mosaiced |
|
581 |
r_prod_sum <- do.call(mosaic,list_args_weights_prod) #weights sum product image mosacied |
|
582 |
|
|
583 |
r_m_weighted_mean <- r_prod_sum/r_weights_sum #this is the mosaic using weighted mean... |
|
584 |
raster_name <- file.path(out_dir,paste("r_m_",method_str,"_weighted_mean_",out_suffix,".tif",sep="")) |
|
585 |
writeRaster(r_m_weighted_mean, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE) |
|
586 |
|
|
587 |
} |
|
588 |
|
|
589 |
list_mosaiced_files <- list.files(pattern="r_m.*._weighted_mean_.*.tif") |
|
590 |
|
|
505 | 591 |
#get the list of weights into raster objects |
506 | 592 |
list_args_linear_weights <- list_linear_weights_m |
507 | 593 |
#list_args_weights <- (mixedsort(list.files(pattern="r_weights_m_.*.tif"))) |
... | ... | |
512 | 598 |
#list_args_weights_prod <- (mixedsort(list.files(pattern="r_weights_prod_m_.*.tif"))) |
513 | 599 |
list_args_linear_weights_prod <- lapply(1:length(list_args_linear_weights_prod), FUN=function(i,x){raster(x[[i]])},x=list_args_linear_weights_prod) |
514 | 600 |
|
601 |
### |
|
602 |
#get the list of edge weights into raster objects |
|
603 |
list_args_edge_weights <- list_linear_weights_m |
|
604 |
#list_args_weights <- (mixedsort(list.files(pattern="r_weights_m_.*.tif"))) |
|
605 |
list_args_edge_weights <- lapply(1:length(list_args_edge_weights), FUN=function(i,x){raster(x[[i]])},x=list_args_linear_weights) |
|
606 |
|
|
607 |
#get the list of weights product into raster objects |
|
608 |
list_args_linear_weights_prod <- list_linear_weights_prod_m |
|
609 |
#list_args_weights_prod <- (mixedsort(list.files(pattern="r_weights_prod_m_.*.tif"))) |
|
610 |
list_args_linear_weights_prod <- lapply(1:length(list_args_linear_weights_prod), FUN=function(i,x){raster(x[[i]])},x=list_args_linear_weights_prod) |
|
611 |
|
|
612 |
|
|
613 |
|
|
515 | 614 |
#get the list of sine weights into raster objects |
516 | 615 |
list_args_sine_weights <- list_sine_weights_m |
517 | 616 |
#list_args_weights <- (mixedsort(list.files(pattern="r_weights_m_.*.tif"))) |
Also available in: Unified diff
cleaning up code to compare different mosaicing methods