Revision 49ab95cd
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: 05/07/2015
|
|
8 |
#MODIFIED ON: 05/08/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 |
... | ... | |
252 | 252 |
return(weights_obj) |
253 | 253 |
} |
254 | 254 |
|
255 |
mosaic_m_raster_list<-function(j,list_param){ |
|
256 |
#This functions returns a subset of tiles from the modis grid. |
|
257 |
#Arguments: modies grid tile,list of tiles |
|
258 |
#Output: spatial grid data frame of the subset of tiles |
|
259 |
#Note that rasters are assumed to be in the same projection system!! |
|
260 |
#modified for global mosaic...still not working right now... |
|
261 |
|
|
262 |
#rast_list<-vector("list",length(mosaic_list)) |
|
263 |
#for (i in 1:length(mosaic_list)){ |
|
264 |
# read the individual rasters into a list of RasterLayer objects |
|
265 |
# this may be changed so that it is not read in the memory!!! |
|
266 |
|
|
267 |
#parse output... |
|
268 |
|
|
269 |
#j<-list_param$j |
|
270 |
mosaic_list<-list_param$mosaic_list |
|
271 |
out_path<-list_param$out_path |
|
272 |
out_names<-list_param$out_rastnames |
|
273 |
file_format <- list_param$file_format |
|
274 |
NA_flag_val <- list_param$NA_flag_val |
|
275 |
out_suffix <- list_param$out_suffix |
|
276 |
## Start |
|
277 |
|
|
278 |
if(class(mosaic_list[[j]])=="list"){ |
|
279 |
m_list <- unlist(mosaic_list[[j]]) |
|
280 |
}else{ |
|
281 |
m_list <- mosaic_list[[j]] |
|
282 |
} |
|
283 |
input.rasters <- lapply(m_list, raster) #create raster image for each element of the list |
|
284 |
#inMemory(input.rasters[[1]]) |
|
285 |
#note that input.rasters are not stored in memory!! |
|
286 |
mosaiced_rast<-input.rasters[[1]] |
|
287 |
|
|
288 |
for (k in 2:length(input.rasters)){ |
|
289 |
mosaiced_rast<-mosaic(mosaiced_rast,input.rasters[[k]], tolerance=1,fun=mean) |
|
290 |
#mosaiced_rast<-mosaic(mosaiced_rast,raster(input.rasters[[k]]), fun=mean) |
|
291 |
} |
|
292 |
|
|
293 |
data_name<-paste("mosaiced_",sep="") #can add more later... |
|
294 |
#raster_name<-paste(data_name,out_names[j],".tif", sep="") |
|
295 |
raster_name<-paste(data_name,out_names[j],file_format, sep="") |
|
296 |
|
|
297 |
writeRaster(mosaiced_rast, NAflag=NA_flag_val,filename=file.path(out_path,raster_name),overwrite=TRUE) |
|
298 |
#Writing the data in a raster file format... |
|
299 |
rast_list<-file.path(out_path,raster_name) |
|
300 |
|
|
301 |
## The Raster and rgdal packages write temporary files on the disk when memory is an issue. This can potential build up |
|
302 |
## in long loops and can fill up hard drives resulting in errors. The following sections removes these files |
|
303 |
## as they are created in the loop. This code section can be transformed into a "clean-up function later on |
|
304 |
## Start remove |
|
305 |
#tempfiles<-list.files(tempdir(),full.names=T) #GDAL transient files are not removed |
|
306 |
#files_to_remove<-grep(out_suffix,tempfiles,value=T) #list files to remove |
|
307 |
#if(length(files_to_remove)>0){ |
|
308 |
# file.remove(files_to_remove) |
|
309 |
#} |
|
310 |
#now remove temp files from raster package located in rasterTmpDir |
|
311 |
removeTmpFiles(h=0) #did not work if h is not set to 0 |
|
312 |
## end of remove section |
|
313 |
|
|
314 |
return(rast_list) |
|
315 |
} |
|
316 |
|
|
255 | 317 |
############################################ |
256 | 318 |
#### Parameters and constants |
257 | 319 |
|
... | ... | |
338 | 400 |
|
339 | 401 |
list_param_create_weights <- list(lf_mosaic_pred_1500x4500,df_centroids,out_dir_str) |
340 | 402 |
names(list_param_create_weights) <- c("lf","df_points","out_dir_str") |
341 |
|
|
342 |
create_weights_fun |
|
343 | 403 |
num_cores <- 6 |
344 | 404 |
|
345 | 405 |
#debug(create_weights_fun) |
... | ... | |
353 | 413 |
|
354 | 414 |
#"r_weights","r_weights_prod" |
355 | 415 |
|
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) |
|
416 |
#list_r_weights <- lapply(1:length(weights_obj_list), FUN=function(i,x){raster(x[[i]]$r_weights)},x=weights_obj_list) |
|
417 |
#list_r_weights_prod <- lapply(1:length(weights_obj_list), FUN=function(i,x){raster(x[[i]]$r_weights_prod)},x=weights_obj_list) |
|
418 |
|
|
419 |
list_r_weights <- lapply(1:length(weights_obj_list), FUN=function(i,x){x[[i]]$r_weights},x=weights_obj_list) |
|
420 |
list_r_weights_prod <- lapply(1:length(weights_obj_list), FUN=function(i,x){x[[i]]$r_weights_prod},x=weights_obj_list) |
|
421 |
|
|
422 |
list_args_weights <- list_r_weights |
|
423 |
list_args_weights_prod <- list_r_weights_prod |
|
358 | 424 |
|
359 | 425 |
list_args_weights$fun <- "sum" |
360 | 426 |
#list_args_weights$fun <- "mean" |
... | ... | |
395 | 461 |
#r_weights_sum <- ... |
396 | 462 |
#r_val_w_sum <- |
397 | 463 |
# |
464 |
mosaic_list_var <- list(list_r_weights) |
|
465 |
#out_rastnames_var <- l_out_rastnames_var[[i]] |
|
466 |
out_rastnames_var <- c("reg2_mosaic_weights.tif") |
|
467 |
|
|
468 |
#list_param_mosaic <- list(list_r_weights,out_dir,outrastnames,file_format,NA_flag_val,out_suffix) |
|
469 |
|
|
470 |
file_format <- ".tif" |
|
471 |
NA_flag_val <- -9999 |
|
472 |
|
|
473 |
j<-1 #date index for loop |
|
474 |
list_param_mosaic<-list(j,mosaic_list_var,out_rastnames_var,out_dir,file_format,NA_flag_val) |
|
475 |
names(list_param_mosaic)<-c("j","mosaic_list","out_rastnames","out_path","file_format","NA_flag_val") |
|
476 |
debug(mosaic_m_raster_list) |
|
477 |
mosaic_m_raster_list(1,list_param_mosaic) |
|
478 |
|
|
479 |
|
|
480 |
#list_var_mosaiced <- mclapply(1:2,FUN=mosaic_m_raster_list,list_param=list_param_mosaic,mc.preschedule=FALSE,mc.cores = 2) |
|
481 |
list_var_mosaiced <- mclapply(1,FUN=mosaic_m_raster_list,list_param=list_param_mosaic,mc.preschedule=FALSE,mc.cores = 1) |
|
482 |
#list_var_mosaiced <- mclapply(1:1,FUN=mosaic_m_raster_list,list_param=list_param_mosaic,mc.preschedule=FALSE,mc.cores = 1) |
|
483 |
#list_var_mosaiced <- mclapply(1:365,FUN=mosaic_m_raster_list,list_param=list_param_mosaic,mc.preschedule=FALSE,mc.cores = 2) |
|
484 |
|
|
485 |
outrastnames <- "reg2_mosaic_weights.tif" |
|
486 |
|
|
487 |
list_param_mosaic <- list(list_r_weights,out_dir,outrastnames,file_format,NA_flag_val,out_suffix) |
|
488 |
|
|
489 |
#mosaic_list<-list_param$mosaic_list |
|
490 |
#out_path<-list_param$out_path |
|
491 |
# out_names<-list_param$out_rastnames |
|
492 |
# file_format <- list_param$file_format |
|
493 |
# NA_flag_val <- list_param$NA_flag_val |
|
494 |
# out_suffix <- list_param$out_suffix |
|
398 | 495 |
|
399 | 496 |
################################################# |
400 | 497 |
#Ok testing on fake data: |
Also available in: Unified diff
scaling up mosaicing experiment, testing mosacing function, problems in R related to differences in resolution