Revision ac1538ae
Added by Benoit Parmentier over 8 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: 01/05/2016
|
|
8 |
#MODIFIED ON: 04/05/2016
|
|
9 | 9 |
#Version: 5 |
10 | 10 |
#PROJECT: Environmental Layers project |
11 | 11 |
#COMMENTS: analyses run for reg4 1992 for test of mosaicing using 1500x4500km and other tiles |
... | ... | |
172 | 172 |
#28) match_extent : if "FALSE" try without matching geographic extent #PARAM 28 |
173 | 173 |
#29) list_models : if NULL use y~1 formula #PARAM 29 |
174 | 174 |
|
175 |
###OUTPUT |
|
176 |
# |
|
177 |
# |
|
178 |
|
|
175 | 179 |
###Loading R library and packages |
176 | 180 |
#library used in the workflow production: |
177 | 181 |
library(gtools) # loading some useful tools |
... | ... | |
226 | 230 |
NA_value <- list_param_run_mosaicing_prediction$NA_value # -9999 #PARAM 15 |
227 | 231 |
|
228 | 232 |
num_cores <- list_param_run_mosaicing_prediction$num_cores #6 #PARAM 17 |
229 |
region_names <- list_param_run_mosaicing_prediction$region_names # c("reg23","reg4") #selected region names, ##PARAM 18 |
|
233 |
#region_names <- list_param_run_mosaicing_prediction$region_names # c("reg23","reg4") #selected region names, ##PARAM 18
|
|
230 | 234 |
use_autokrige <- list_param_run_mosaicing_prediction$use_autokrige # F #PARAM 19 |
231 | 235 |
|
232 | 236 |
###Separate folder for masks by regions, should be listed as just the dir!!... #PARAM 20 |
... | ... | |
234 | 238 |
infile_mask <- list_param_run_mosaicing_prediction$infile_mask # input mask used in defining the region |
235 | 239 |
|
236 | 240 |
#in_dir can be on NEX or Atlas |
237 |
df_assessment_files_name <- list_param_run_mosaicing_prediction$df_assessment_files_name # data.frame with all files used in assessmnet, PARAM 21 |
|
241 |
|
|
242 |
##skip this for now |
|
243 |
#df_assessment_files_name <- list_param_run_mosaicing_prediction$df_assessment_files_name # data.frame with all files used in assessmnet, PARAM 21 |
|
238 | 244 |
|
239 | 245 |
#python script and gdal on NEX NASA: |
240 | 246 |
#mosaic_python <- "/nobackupp6/aguzman4/climateLayers/sharedCode/" |
... | ... | |
255 | 261 |
####### PART 1: Read in data and process data ######## |
256 | 262 |
|
257 | 263 |
|
258 |
out_dir <- in_dir #PARAM 11 |
|
259 |
in_dir_tiles <- file.path(in_dir,"tiles") #this is valid both for Atlas and NEX |
|
264 |
#out_dir <- in_dir #PARAM 11
|
|
265 |
#in_dir_tiles <- file.path(in_dir,"tiles") #this is valid both for Atlas and NEX
|
|
260 | 266 |
NA_flag_val <- NA_value #PARAM 16 |
261 | 267 |
|
262 | 268 |
#in_dir <- file.path(in_dir,region_name) |
263 |
out_dir <- in_dir |
|
269 |
#out_dir <- in_dir
|
|
264 | 270 |
if(create_out_dir_param==TRUE){ |
265 |
out_dir <- create_dir_fun(out_dir,out_suffix) |
|
271 |
create_dir_fun() |
|
272 |
out_dir_tmp <- file.path(out_dir,"mosaic") |
|
273 |
out_dir <- create_dir_fun(out_dir_tmp,out_suffix) |
|
266 | 274 |
setwd(out_dir) |
267 | 275 |
}else{ |
268 | 276 |
setwd(out_dir) #use previoulsy defined directory |
... | ... | |
270 | 278 |
|
271 | 279 |
setwd(out_dir) |
272 | 280 |
|
281 |
###on 04/05 skipping this for now |
|
273 | 282 |
### Read in assessment and accuracy files |
274 | 283 |
df_assessment_files <- read.table(df_assessment_files_name,stringsAsFactors=F,sep=",") |
275 | 284 |
|
276 |
tb_v_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[2])) |
|
277 |
tb_s_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[4])) |
|
278 |
tb_s_month_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[3])) |
|
279 |
data_month_s_name <- file.path(in_dir,basename(df_assessment_files$files[5])) |
|
280 |
data_day_v_name <- file.path(in_dir, basename(df_assessment_files$files[6])) |
|
281 |
data_day_s_name <- file.path(in_dir, basename(df_assessment_files$files[7])) |
|
282 |
#data_month_v_name <- file.path(in_dir,basename(df_assessment_files$files[8])) |
|
283 |
pred_data_month_info_name <- file.path(in_dir, basename(df_assessment_files$files[9])) |
|
284 |
pred_data_day_info_name <- file.path(in_dir, basename(df_assessment_files$files[10])) |
|
285 |
df_tile_processed_name <- file.path(in_dir, basename(df_assessment_files$files[11])) |
|
286 |
|
|
285 |
#tb_v_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[2])) |
|
286 |
#tb_s_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[4])) |
|
287 |
#tb_s_month_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[3])) |
|
288 |
#data_month_s_name <- file.path(in_dir,basename(df_assessment_files$files[5])) |
|
289 |
#data_day_v_name <- file.path(in_dir, basename(df_assessment_files$files[6])) |
|
290 |
#data_day_s_name <- file.path(in_dir, basename(df_assessment_files$files[7])) |
|
291 |
##data_month_v_name <- file.path(in_dir,basename(df_assessment_files$files[8])) |
|
292 |
#pred_data_month_info_name <- file.path(in_dir, basename(df_assessment_files$files[9])) |
|
293 |
#pred_data_day_info_name <- file.path(in_dir, basename(df_assessment_files$files[10])) |
|
294 |
#df_tile_processed_name <- file.path(in_dir, basename(df_assessment_files$files[11])) |
|
295 |
|
|
296 |
tb_v_accuracy_name <- df_assessment_files$files[2] |
|
297 |
tb_s_accuracy_name <- df_assessment_files$files[4] |
|
298 |
tb_s_month_accuracy_name <- df_assessment_files$files[3] |
|
299 |
data_month_s_name <- df_assessment_files$files[5] |
|
300 |
data_day_v_name <- df_assessment_files$files[6] |
|
301 |
data_day_s_name <- df_assessment_files$files[7] |
|
302 |
##data_month_v_name <- file.path(in_dir,basename(df_assessment_files$files[8])) |
|
303 |
pred_data_month_info_name <- df_assessment_files$files[9] |
|
304 |
pred_data_day_info_name <- df_assessment_files$files[10] |
|
305 |
df_tile_processed_name <- df_assessment_files$files[11] |
|
287 | 306 |
# accuracy table by tiles |
288 | 307 |
tb <- read.table(tb_v_accuracy_name,sep=",") |
289 | 308 |
tb_s <- read.table(tb_s_accuracy_name,sep=",") |
... | ... | |
294 | 313 |
|
295 | 314 |
#list all files to mosaic for a list of day |
296 | 315 |
#Take into account multiple region in some cases!!! |
297 |
reg_lf_mosaic <- vector("list",length=length(region_names)) |
|
298 |
for(k in 1:length(region_names)){ |
|
299 |
in_dir_tiles_tmp <- file.path(in_dir_tiles, region_names[k]) |
|
316 |
reg_lf_mosaic <- vector("list",length=length(region_name)) |
|
317 |
|
|
318 |
#for(k in 1:length(region_names)){ |
|
319 |
#this part needs to be improve make this a function and use multicore to loop through files... |
|
320 |
#give a range of dates to run... |
|
321 |
if(is.null(day_to_mosaic)){ |
|
322 |
start_date <- #first date |
|
323 |
end_date <- |
|
324 |
} |
|
325 |
|
|
326 |
for(k in 1:length(region_name)){ |
|
327 |
in_dir_tiles_tmp <- file.path(in_dir, region_name[k]) |
|
328 |
#fix this later and add the year.. |
|
329 |
#gam_CAI_dailyTmax_predicted_mod1 |
|
300 | 330 |
reg_lf_mosaic[[k]] <- lapply(1:length(day_to_mosaic),FUN=function(i){list.files(path=file.path(in_dir_tiles_tmp), |
301 |
pattern=paste(".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=F)})
|
|
331 |
pattern=paste("gam_CAI_dailyTmax_predicted_",pred_mod_name,".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=T)})
|
|
302 | 332 |
} |
303 | 333 |
|
334 |
#reg_lf_mosaic[[k]] <- list.files(path=file.path(in_dir_tiles_tmp),pattern=paste(".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=T) |
|
304 | 335 |
##################### PART 2: produce the mosaic ################## |
305 | 336 |
|
306 | 337 |
#This is is assuming a list of file for a region!! |
307 | 338 |
#this is where the main function for mosaicing region starts!! |
308 | 339 |
#use reg4 to test the code for now, redo later for any regions!!! |
309 |
k<-2
|
|
310 |
for(k in 1:length(region_names)){
|
|
311 |
region_selected <- region_names[k]
|
|
312 |
##########################
|
|
313 |
#### First generate rmse images for each date and tile for the region
|
|
314 |
|
|
315 |
|
|
316 |
lf_mosaic <- reg_lf_mosaic[[k]] #list of files to mosaic by regions
|
|
317 |
#There a 28 files for reg4, South America
|
|
318 |
|
|
319 |
#######################################
|
|
320 |
################### PART I: Accuracy layers by tiles #############
|
|
321 |
#first generate accuracy layers using tiles definitions and output from the accuracy assessment
|
|
322 |
|
|
323 |
#tb <- list_param$tb #fitting or validation table with all days
|
|
324 |
#metric_name <- "rmse" #RMSE, MAE etc.
|
|
325 |
#pred_mod_name <- "mod1"
|
|
326 |
#y_var_name
|
|
327 |
#interpolation_method #c("gam_CAI") #PARAM3
|
|
328 |
days_to_process <- day_to_mosaic
|
|
329 |
#NA_flag_val <- list_param$NA_flag_val
|
|
330 |
#file_format <- list_param$file_format
|
|
331 |
out_dir_str <- out_dir
|
|
332 |
out_suffix_str <- out_suffix
|
|
333 |
lf <- lf_mosaic
|
|
334 |
|
|
335 |
#Improved by adding multicores option
|
|
336 |
num_cores_tmp <- num_cores
|
|
337 |
list_param_accuracy_metric_raster <- list(lf,tb,metric_name,pred_mod_name,y_var_name,interpolation_method,
|
|
340 |
k<-1
|
|
341 |
#for(k in 1:length(region_name)){
|
|
342 |
region_selected <- region_name[k]
|
|
343 |
########################## |
|
344 |
#### First generate rmse images for each date and tile for the region |
|
345 |
|
|
346 |
|
|
347 |
lf_mosaic <- reg_lf_mosaic[[k]] #list of files to mosaic by regions |
|
348 |
#There a 28 files for reg4, South America |
|
349 |
|
|
350 |
####################################### |
|
351 |
################### PART I: Accuracy layers by tiles ############# |
|
352 |
#first generate accuracy layers using tiles definitions and output from the accuracy assessment |
|
353 |
|
|
354 |
#tb <- list_param$tb #fitting or validation table with all days |
|
355 |
#metric_name <- "rmse" #RMSE, MAE etc. |
|
356 |
#pred_mod_name <- "mod1" |
|
357 |
#y_var_name |
|
358 |
#interpolation_method #c("gam_CAI") #PARAM3 |
|
359 |
days_to_process <- day_to_mosaic |
|
360 |
#NA_flag_val <- list_param$NA_flag_val |
|
361 |
#file_format <- list_param$file_format |
|
362 |
out_dir_str <- out_dir |
|
363 |
out_suffix_str <- out_suffix |
|
364 |
lf <- lf_mosaic |
|
365 |
|
|
366 |
#Improved by adding multicores option |
|
367 |
num_cores_tmp <- num_cores |
|
368 |
list_param_accuracy_metric_raster <- list(lf,tb,metric_name,pred_mod_name,y_var_name,interpolation_method, |
|
338 | 369 |
days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str,out_suffix_str) |
339 |
names(list_param_accuracy_metric_raster) <- c("lf","tb","metric_name","pred_mod_name","y_var_name","interpolation_method",
|
|
370 |
names(list_param_accuracy_metric_raster) <- c("lf","tb","metric_name","pred_mod_name","y_var_name","interpolation_method", |
|
340 | 371 |
"days_to_process","num_cores","NA_flag_val","file_format","out_dir_str","out_suffix_str") |
341 |
list_raster_created_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_metric_raster,
|
|
372 |
list_raster_created_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_metric_raster, |
|
342 | 373 |
list_param=list_param_accuracy_metric_raster) |
343 | 374 |
|
344 |
#debug(create_accuracy_metric_raster)
|
|
345 |
#list_raster_created_obj <- lapply(1:1,FUN=create_accuracy_metric_raster,
|
|
346 |
# list_param=list_param_accuracy_metric_raster)
|
|
347 |
#raster_created_obj <- create_accuracy_metric_raster(1, list_param_accuracy_metric_raster)
|
|
348 |
|
|
349 |
#Extract list of files for rmse and date 1 (19920101), there should be 28 raster images
|
|
350 |
lf_accuracy_raster <- lapply(1:length(list_raster_created_obj),FUN=function(i){unlist(list_raster_created_obj[[i]]$list_raster_name)})
|
|
351 |
|
|
352 |
#Plot as quick check
|
|
353 |
#r1 <- raster(lf_mosaic[[1]][1])
|
|
354 |
#plot(r1)
|
|
355 |
|
|
356 |
####################################
|
|
357 |
### Now create accuracy surfaces from residuals...
|
|
358 |
|
|
359 |
## Create accuracy surface by kriging
|
|
360 |
|
|
361 |
num_cores_tmp <-num_cores
|
|
362 |
lf_day_tiles <- lf_mosaic #list of raster files by dates
|
|
363 |
data_df <- data_day_v # data.frame table/spdf containing stations with residuals and variable
|
|
364 |
#df_tile_processed #tiles processed during assessment usually by region
|
|
365 |
#var_pred #variable being modeled
|
|
366 |
#if not list of models is provided generate one
|
|
367 |
if(is.null(list_models)){
|
|
368 |
list_models <- paste(var_pred,"~","1",sep=" ")
|
|
369 |
}
|
|
370 |
|
|
371 |
#use_autokrige #if TRUE use automap/gstat package
|
|
372 |
#y_var_name #"dailyTmax" #PARAM2
|
|
373 |
#interpolation_method #c("gam_CAI") #PARAM3, need to select reg!!
|
|
374 |
#date_processed #can be a monthly layer
|
|
375 |
#num_cores #number of cores used
|
|
376 |
#NA_flag_val
|
|
377 |
#file_format
|
|
378 |
out_dir_str <- out_dir
|
|
379 |
out_suffix_str <- out_suffix
|
|
380 |
days_to_process <- day_to_mosaic
|
|
381 |
df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX)
|
|
382 |
df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX))
|
|
383 |
|
|
384 |
##By regions, selected earlier
|
|
385 |
#for(k in 1:length(region_names)){
|
|
386 |
df_tile_processed_reg <- subset(df_tile_processed,reg==region_selected)#use reg4
|
|
387 |
#i<-1 #loop by days/date to process!!
|
|
388 |
#test on the first day
|
|
389 |
list_param_create_accuracy_residuals_raster <- list(lf_day_tiles,data_df,df_tile_processed_reg,
|
|
375 |
#debug(create_accuracy_metric_raster) |
|
376 |
#list_raster_created_obj <- lapply(1:1,FUN=create_accuracy_metric_raster, |
|
377 |
# list_param=list_param_accuracy_metric_raster) |
|
378 |
#raster_created_obj <- create_accuracy_metric_raster(1, list_param_accuracy_metric_raster) |
|
379 |
|
|
380 |
#Extract list of files for rmse and date 1 (19920101), there should be 28 raster images |
|
381 |
lf_accuracy_raster <- lapply(1:length(list_raster_created_obj),FUN=function(i){unlist(list_raster_created_obj[[i]]$list_raster_name)}) |
|
382 |
|
|
383 |
#Plot as quick check |
|
384 |
#r1 <- raster(lf_mosaic[[1]][1]) |
|
385 |
#plot(r1) |
|
386 |
|
|
387 |
#################################### |
|
388 |
### Now create accuracy surfaces from residuals... |
|
389 |
|
|
390 |
## Create accuracy surface by kriging |
|
391 |
|
|
392 |
num_cores_tmp <-num_cores |
|
393 |
lf_day_tiles <- lf_mosaic #list of raster files by dates |
|
394 |
data_df <- data_day_v # data.frame table/spdf containing stations with residuals and variable |
|
395 |
#df_tile_processed #tiles processed during assessment usually by region |
|
396 |
#var_pred #variable being modeled |
|
397 |
#if not list of models is provided generate one |
|
398 |
if(is.null(list_models)){ |
|
399 |
list_models <- paste(var_pred,"~","1",sep=" ") |
|
400 |
} |
|
401 |
|
|
402 |
#use_autokrige #if TRUE use automap/gstat package |
|
403 |
#y_var_name #"dailyTmax" #PARAM2 |
|
404 |
#interpolation_method #c("gam_CAI") #PARAM3, need to select reg!! |
|
405 |
#date_processed #can be a monthly layer |
|
406 |
#num_cores #number of cores used |
|
407 |
#NA_flag_val |
|
408 |
#file_format |
|
409 |
out_dir_str <- out_dir |
|
410 |
out_suffix_str <- out_suffix |
|
411 |
days_to_process <- day_to_mosaic |
|
412 |
df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX) |
|
413 |
df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX)) |
|
414 |
|
|
415 |
##By regions, selected earlier |
|
416 |
#for(k in 1:length(region_names)){ |
|
417 |
df_tile_processed_reg <- subset(df_tile_processed,reg==region_selected)#use reg4 |
|
418 |
#i<-1 #loop by days/date to process!! |
|
419 |
#test on the first day |
|
420 |
list_param_create_accuracy_residuals_raster <- list(lf_day_tiles,data_df,df_tile_processed_reg, |
|
390 | 421 |
var_pred,list_models,use_autokrige,y_var_name,interpolation_method, |
391 | 422 |
days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str, |
392 | 423 |
out_suffix_str) |
393 |
names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed_reg",
|
|
424 |
names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed_reg", |
|
394 | 425 |
"var_pred","list_models","use_autokrige","y_var_name","interpolation_method", |
395 | 426 |
"days_to_process","num_cores_tmp","NA_flag_val","file_format","out_dir_str", |
396 | 427 |
"out_suffix_str") |
397 | 428 |
|
398 |
list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster,
|
|
429 |
list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster, |
|
399 | 430 |
list_param=list_param_create_accuracy_residuals_raster) |
400 | 431 |
|
401 |
#undebug(create_accuracy_residuals_raster)
|
|
402 |
#list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster,
|
|
403 |
# list_param=list_param_create_accuracy_residuals_raster)
|
|
404 |
|
|
405 |
#create_accuracy_residuals_raster_obj <- create_accuracy_metric_raster(1, list_param_create_accuracy_residuals_raster_obj)
|
|
406 |
|
|
407 |
#note that three tiles did not produce a residuals surface!!! find out more later, join the output
|
|
408 |
#to df_raste_tile to keep track of which one did not work...
|
|
409 |
#lf_accuracy_residuals_raster <- as.character(unlist(lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name"))},x=list_create_accuracy_residuals_raster_obj)))
|
|
410 |
lf_accuracy_residuals_raster <- lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){as.character(unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name")))},x=list_create_accuracy_residuals_raster_obj)
|
|
411 |
|
|
412 |
#Plot as quick check
|
|
413 |
#r1 <- raster(lf_mosaic[[1]][1])
|
|
414 |
#list_create_accuracy_residuals_raster_obj
|
|
415 |
|
|
416 |
##Run for data_day_s
|
|
417 |
##
|
|
418 |
data_df <- data_day_s # data.frame table/spdf containing stations with residuals and variable
|
|
419 |
|
|
420 |
num_cores_tmp <-num_cores
|
|
421 |
lf_day_tiles <- lf_mosaic #list of raster files by dates
|
|
422 |
#data_df <- data_day_v # data.frame table/spdf containing stations with residuals and variable
|
|
423 |
#df_tile_processed #tiles processed during assessment usually by region
|
|
424 |
#var_pred #variable being modeled
|
|
425 |
#if not list of models is provided generate one
|
|
426 |
if(is.null(list_models)){
|
|
427 |
list_models <- paste(var_pred,"~","1",sep=" ")
|
|
428 |
}
|
|
429 |
|
|
430 |
#use_autokrige #if TRUE use automap/gstat package
|
|
431 |
#y_var_name #"dailyTmax" #PARAM2
|
|
432 |
#interpolation_method #c("gam_CAI") #PARAM3, need to select reg!!
|
|
433 |
#date_processed #can be a monthly layer
|
|
434 |
#num_cores #number of cores used
|
|
435 |
#NA_flag_val
|
|
436 |
#file_format
|
|
437 |
out_dir_str <- out_dir
|
|
438 |
out_suffix_str <- paste("data_day_s_",out_suffix,sep="")
|
|
439 |
days_to_process <- day_to_mosaic
|
|
440 |
df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX)
|
|
441 |
df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX))
|
|
442 |
|
|
443 |
##By regions, selected earlier
|
|
444 |
#for(k in 1:length(region_names)){
|
|
445 |
df_tile_processed_reg <- subset(df_tile_processed,reg==region_selected)#use reg4
|
|
446 |
#i<-1 #loop by days/date to process!!
|
|
447 |
#test on the first day
|
|
448 |
list_param_create_accuracy_residuals_raster <- list(lf_day_tiles,data_df,df_tile_processed_reg,
|
|
432 |
#undebug(create_accuracy_residuals_raster) |
|
433 |
#list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster, |
|
434 |
# list_param=list_param_create_accuracy_residuals_raster) |
|
435 |
|
|
436 |
#create_accuracy_residuals_raster_obj <- create_accuracy_metric_raster(1, list_param_create_accuracy_residuals_raster_obj) |
|
437 |
|
|
438 |
#note that three tiles did not produce a residuals surface!!! find out more later, join the output |
|
439 |
#to df_raste_tile to keep track of which one did not work... |
|
440 |
#lf_accuracy_residuals_raster <- as.character(unlist(lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name"))},x=list_create_accuracy_residuals_raster_obj))) |
|
441 |
lf_accuracy_residuals_raster <- lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){as.character(unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name")))},x=list_create_accuracy_residuals_raster_obj) |
|
442 |
|
|
443 |
#Plot as quick check |
|
444 |
#r1 <- raster(lf_mosaic[[1]][1]) |
|
445 |
#list_create_accuracy_residuals_raster_obj |
|
446 |
|
|
447 |
##Run for data_day_s |
|
448 |
## |
|
449 |
data_df <- data_day_s # data.frame table/spdf containing stations with residuals and variable |
|
450 |
|
|
451 |
num_cores_tmp <-num_cores |
|
452 |
lf_day_tiles <- lf_mosaic #list of raster files by dates |
|
453 |
#data_df <- data_day_v # data.frame table/spdf containing stations with residuals and variable |
|
454 |
#df_tile_processed #tiles processed during assessment usually by region |
|
455 |
#var_pred #variable being modeled |
|
456 |
#if not list of models is provided generate one |
|
457 |
if(is.null(list_models)){ |
|
458 |
list_models <- paste(var_pred,"~","1",sep=" ") |
|
459 |
} |
|
460 |
|
|
461 |
#use_autokrige #if TRUE use automap/gstat package |
|
462 |
#y_var_name #"dailyTmax" #PARAM2 |
|
463 |
#interpolation_method #c("gam_CAI") #PARAM3, need to select reg!! |
|
464 |
#date_processed #can be a monthly layer |
|
465 |
#num_cores #number of cores used |
|
466 |
#NA_flag_val |
|
467 |
#file_format |
|
468 |
out_dir_str <- out_dir |
|
469 |
out_suffix_str <- paste("data_day_s_",out_suffix,sep="") |
|
470 |
days_to_process <- day_to_mosaic |
|
471 |
df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX) |
|
472 |
df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX)) |
|
473 |
|
|
474 |
##By regions, selected earlier |
|
475 |
#for(k in 1:length(region_names)){ |
|
476 |
df_tile_processed_reg <- subset(df_tile_processed,reg==region_selected)#use reg4 |
|
477 |
#i<-1 #loop by days/date to process!! |
|
478 |
#test on the first day |
|
479 |
list_param_create_accuracy_residuals_raster <- list(lf_day_tiles,data_df,df_tile_processed_reg, |
|
449 | 480 |
var_pred,list_models,use_autokrige,y_var_name,interpolation_method, |
450 | 481 |
days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str, |
451 | 482 |
out_suffix_str) |
452 |
names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed_reg",
|
|
483 |
names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed_reg", |
|
453 | 484 |
"var_pred","list_models","use_autokrige","y_var_name","interpolation_method", |
454 | 485 |
"days_to_process","num_cores_tmp","NA_flag_val","file_format","out_dir_str", |
455 | 486 |
"out_suffix_str") |
456 | 487 |
|
457 |
list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster,
|
|
488 |
list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster, |
|
458 | 489 |
list_param=list_param_create_accuracy_residuals_raster) |
459 | 490 |
|
460 |
#undebug(create_accuracy_residuals_raster)
|
|
461 |
#list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster,
|
|
462 |
# list_param=list_param_create_accuracy_residuals_raster)
|
|
463 |
|
|
464 |
#create_accuracy_residuals_raster_obj <- create_accuracy_metric_raster(1, list_param_create_accuracy_residuals_raster_obj)
|
|
465 |
|
|
466 |
#note that three tiles did not produce a residuals surface!!! find out more later, join the output
|
|
467 |
#to df_raste_tile to keep track of which one did not work...
|
|
468 |
#lf_accuracy_residuals_raster <- as.character(unlist(lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name"))},x=list_create_accuracy_residuals_raster_obj)))
|
|
469 |
lf_accuracy_residuals_data_s_raster <- lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){as.character(unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name")))},x=list_create_accuracy_residuals_raster_obj)
|
|
470 |
|
|
471 |
##took 31 minutes to generate the residuals maps for each tiles (28) for region 4
|
|
472 |
|
|
473 |
######################################################
|
|
474 |
#### PART 2: GENETATE MOSAIC FOR LIST OF FILES #####
|
|
475 |
#################################
|
|
476 |
#### Mosaic tiles for the variable predicted and accuracy metric
|
|
477 |
|
|
478 |
#methods availbable:use_sine_weights,use_edge,use_linear_weights
|
|
479 |
#only use edge method for now
|
|
480 |
#loop to dates..., make this a function...
|
|
481 |
list_mosaic_obj <- vector("list",length=length(day_to_mosaic))
|
|
482 |
for(i in 1:length(day_to_mosaic)){
|
|
483 |
#
|
|
484 |
mosaic_method <- "use_edge_weights" #this is distance from edge
|
|
485 |
out_suffix_tmp <- paste(interpolation_method,y_var_name,day_to_mosaic[i],out_suffix,sep="_")
|
|
486 |
#debug(mosaicFiles)
|
|
487 |
#can also loop through methods!!!
|
|
488 |
#python_bin <- "/usr/bin/" #python gdal bin, on Atlas NCEAS
|
|
489 |
#python_bin <- "/nobackupp6/aguzman4/climateLayers/sharedModules/bin" #on NEX
|
|
490 |
#gdal_merge_sum_noDataTest.py
|
|
491 |
#undebug(create_accuracy_residuals_raster) |
|
492 |
#list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster, |
|
493 |
# list_param=list_param_create_accuracy_residuals_raster) |
|
494 |
|
|
495 |
#create_accuracy_residuals_raster_obj <- create_accuracy_metric_raster(1, list_param_create_accuracy_residuals_raster_obj) |
|
496 |
|
|
497 |
#note that three tiles did not produce a residuals surface!!! find out more later, join the output |
|
498 |
#to df_raste_tile to keep track of which one did not work... |
|
499 |
#lf_accuracy_residuals_raster <- as.character(unlist(lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name"))},x=list_create_accuracy_residuals_raster_obj))) |
|
500 |
lf_accuracy_residuals_data_s_raster <- lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){as.character(unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name")))},x=list_create_accuracy_residuals_raster_obj) |
|
501 |
|
|
502 |
##took 31 minutes to generate the residuals maps for each tiles (28) for region 4 |
|
503 |
|
|
504 |
###################################################### |
|
505 |
#### PART 2: GENETATE MOSAIC FOR LIST OF FILES ##### |
|
506 |
################################# |
|
507 |
#### Mosaic tiles for the variable predicted and accuracy metric |
|
508 |
|
|
509 |
#methods availbable:use_sine_weights,use_edge,use_linear_weights |
|
510 |
#only use edge method for now |
|
511 |
#loop to dates..., make this a function... |
|
512 |
list_mosaic_obj <- vector("list",length=length(day_to_mosaic)) |
|
513 |
for(i in 1:length(day_to_mosaic)){ |
|
514 |
# |
|
515 |
mosaic_method <- "use_edge_weights" #this is distance from edge |
|
516 |
out_suffix_tmp <- paste(interpolation_method,y_var_name,day_to_mosaic[i],out_suffix,sep="_") |
|
517 |
#debug(mosaicFiles) |
|
518 |
#can also loop through methods!!! |
|
519 |
#python_bin <- "/usr/bin/" #python gdal bin, on Atlas NCEAS |
|
520 |
#python_bin <- "/nobackupp6/aguzman4/climateLayers/sharedModules/bin" #on NEX |
|
521 |
#gdal_merge_sum_noDataTest.py |
|
491 | 522 |
|
492 |
mosaic_edge_obj_prediction <- mosaicFiles(lf_mosaic[[i]],
|
|
523 |
mosaic_edge_obj_prediction <- mosaicFiles(lf_mosaic[[i]], |
|
493 | 524 |
mosaic_method="use_edge_weights", |
494 | 525 |
num_cores=num_cores, |
495 | 526 |
r_mask_raster_name=infile_mask, |
... | ... | |
503 | 534 |
out_suffix=out_suffix_tmp, |
504 | 535 |
out_dir=out_dir) |
505 | 536 |
|
506 |
mosaic_method <- "use_edge_weights" #this is distance from edge
|
|
507 |
out_suffix_tmp <- paste(interpolation_method,metric_name,day_to_mosaic[i],out_suffix,sep="_")
|
|
537 |
mosaic_method <- "use_edge_weights" #this is distance from edge |
|
538 |
out_suffix_tmp <- paste(interpolation_method,metric_name,day_to_mosaic[i],out_suffix,sep="_") |
|
508 | 539 |
|
509 |
#debug(mosaicFiles)
|
|
510 |
#can also loop through methods!!!
|
|
511 |
mosaic_edge_obj_accuracy <- mosaicFiles(lf_accuracy_raster[[i]],
|
|
540 |
#debug(mosaicFiles) |
|
541 |
#can also loop through methods!!! |
|
542 |
mosaic_edge_obj_accuracy <- mosaicFiles(lf_accuracy_raster[[i]], |
|
512 | 543 |
mosaic_method="use_edge_weights", |
513 | 544 |
num_cores=num_cores, |
514 | 545 |
r_mask_raster_name=infile_mask, |
... | ... | |
521 | 552 |
out_suffix=out_suffix_tmp, |
522 | 553 |
out_dir=out_dir) |
523 | 554 |
|
524 |
list_mosaic_obj[[i]] <- list(prediction=mosaic_edge_obj_prediction,accuracy=mosaic_edge_obj_accuracy)
|
|
555 |
list_mosaic_obj[[i]] <- list(prediction=mosaic_edge_obj_prediction,accuracy=mosaic_edge_obj_accuracy) |
|
525 | 556 |
|
526 |
### produce residuals mosaics
|
|
527 |
#for now add data_day_s in the name!!
|
|
528 |
mosaic_method <- "use_edge_weights" #this is distance from edge
|
|
529 |
out_suffix_tmp <- paste(interpolation_method,"kriged_residuals","data_day_s",day_to_mosaic[i],out_suffix,sep="_")
|
|
530 |
#lf_tmp<-list.files(pattern="*kriged_residuals.*.tif",full.names=T)
|
|
531 |
lf_tmp <- lf_accuracy_residuals_raster[[i]]
|
|
532 |
#lf_accuracy_residuals_raster[[i]]
|
|
533 |
#debug(mosaicFiles)
|
|
534 |
mosaic_edge_obj_residuals <- mosaicFiles(lf_tmp,
|
|
557 |
### produce residuals mosaics |
|
558 |
#for now add data_day_s in the name!! |
|
559 |
mosaic_method <- "use_edge_weights" #this is distance from edge |
|
560 |
out_suffix_tmp <- paste(interpolation_method,"kriged_residuals","data_day_s",day_to_mosaic[i],out_suffix,sep="_") |
|
561 |
#lf_tmp<-list.files(pattern="*kriged_residuals.*.tif",full.names=T) |
|
562 |
lf_tmp <- lf_accuracy_residuals_raster[[i]] |
|
563 |
#lf_accuracy_residuals_raster[[i]] |
|
564 |
#debug(mosaicFiles) |
|
565 |
mosaic_edge_obj_residuals <- mosaicFiles(lf_tmp, |
|
535 | 566 |
mosaic_method="use_edge_weights", |
536 | 567 |
num_cores=num_cores, |
537 | 568 |
r_mask_raster_name=infile_mask, |
... | ... | |
545 | 576 |
out_suffix=out_suffix_tmp, |
546 | 577 |
out_dir=out_dir) |
547 | 578 |
|
548 |
list_mosaic_obj[[i]] <- list(prediction=mosaic_edge_obj_prediction,accuracy=mosaic_edge_obj_accuracy,mosaic_edge_obj_residuals)
|
|
549 |
} |
|
579 |
list_mosaic_obj[[i]] <- list(prediction=mosaic_edge_obj_prediction,accuracy=mosaic_edge_obj_accuracy,mosaic_edge_obj_residuals) |
|
580 |
#}
|
|
550 | 581 |
|
551 | 582 |
##End of mosaicing function for region predictions |
552 | 583 |
} |
Also available in: Unified diff
mosaicing script, major changes to further integrate code in the workflow