Project

General

Profile

« Previous | Next » 

Revision b66e6dcc

Added by Benoit Parmentier almost 9 years ago

more modifications for mosaicing of functions for residuals, test for several days

View differences:

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: 12/16/2015            
8
#MODIFIED ON: 12/17/2015            
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
......
58 58

  
59 59
#### FUNCTION USED IN SCRIPT
60 60

  
61
function_mosaicing <-"global_run_scalingup_mosaicing_function_12162015b.R"
61
function_mosaicing <-"global_run_scalingup_mosaicing_function_12172015.R"
62 62

  
63 63
in_dir_script <-"/home/parmentier/Data/IPLANT_project/env_layers_scripts" #NCEAS UCSB
64 64
#in_dir_script <- "/nobackupp8/bparmen1/env_layers_scripts" #NASA NEX
......
177 177
df_tile_processed <- read.table( df_tile_processed_name,sep=",")
178 178

  
179 179
#list all files to mosaic for a list of day
180
#Take into account multiple region in some cases!!!      
181
lf_mosaic <- lapply(1:length(day_to_mosaic),FUN=function(i){list.files(path=file.path(in_dir_tiles),    
182
           pattern=paste(".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=T)})
180
#Take into account multiple region in some cases!!!  
181
reg_lf_mosaic <- vector("list",length=length(region_names))
182
for(k in 1:length(region_names)){
183
  in_dir_tiles_tmp <- file.path(in_dir_tiles, region_names[k])
184
  reg_lf_mosaic[[k]] <- lapply(1:length(day_to_mosaic),FUN=function(i){list.files(path=file.path(in_dir_tiles_tmp),    
185
           pattern=paste(".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=F)})
186
}
183 187

  
184 188
##################### PART 2: produce the mosaic ##################
185 189

  
......
187 191
#### First generate rmse images for each date and tile for the region
188 192

  
189 193
#lf <- lf_mosaic1 #list of files to mosaic for date 1, there a 28 files for reg4, South America
190
lf <- lf_mosaic #list of files to mosaic for date 1, there a 28 files for reg4, South America
194

  
195
#use reg4 to test the code for now, redo later for any regions!!!
196
lf_mosaic <- reg_lf_mosaic[[2]] #list of files to mosaic for date 1, there a 28 files for reg4, South America
191 197

  
192 198
#tb <- list_param$tb #fitting or validation table with all days
193 199
#metric_name <- "rmse" #RMSE, MAE etc.
......
232 238

  
233 239
## Create accuracy surface by kriging
234 240

  
235
num_cores_tmp <- 6
241
num_cores_tmp <- 7
236 242
lf_day_tiles  <- lf_mosaic #list of raster files by dates
237 243
data_df <- data_day_v # data.frame table/spdf containing stations with residuals and variable
238 244
#df_tile_processed  #tiles processed during assessment usually by region
......
240 246
list_models <- paste(var_pred,"~","1",sep=" ")
241 247
#use_autokrige #if TRUE use automap/gstat package
242 248
#y_var_name  #"dailyTmax" #PARAM2
243
#interpolation_method #c("gam_CAI") #PARAM3
249
#interpolation_method #c("gam_CAI") #PARAM3, need to select reg!!
244 250
#date_processed #can be a monthly layer
245 251
#num_cores #number of cores used
246 252
#NA_flag_val 
......
248 254
out_dir_str <- out_dir
249 255
out_suffix_str <- out_suffix 
250 256
days_to_process <- day_to_mosaic
251

  
252
#data_tmp <- subset(data_month_s,month==1)
253
#var_pred <- "res_mod1"
254
#list_models<- c("res_mod1 ~ 1")
255

  
256

  
257
##By regions
258
i<-1 #loop by days/date to process!!
259
 
260
list_param_create_accuracy_residuals_raster <- list(lf_day_tiles,data_df,df_tile_processed,
257
df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX)
258
df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX))
259

  
260
##By regions, selected earlier
261
for(k in 1:length(region_names)){
262
  df_tile_processed_reg <- subset(df_tile_processed,reg==region_names[k])#use reg4
263
  #i<-1 #loop by days/date to process!!
264
  #test on the first day 
265
  list_param_create_accuracy_residuals_raster <- list(lf_day_tiles,data_df,df_tile_processed_reg,
261 266
                    var_pred,list_models,use_autokrige,y_var_name,interpolation_method,
262 267
                    days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str,
263 268
                    out_suffix_str) 
264
names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed",
269
  names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed_reg",
265 270
                    "var_pred","list_models","use_autokrige","y_var_name","interpolation_method",
266 271
                    "days_to_process","num_cores_tmp","NA_flag_val","file_format","out_dir_str",
267 272
                    "out_suffix_str") 
268 273

  
269
#list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster_obj,
270
#                                  list_param=list_param_create_accuracy_residuals_raster_obj)
274
  #list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster_obj,
275
  #                                list_param=list_param_create_accuracy_residuals_raster_obj)
271 276

  
272
debug(create_accuracy_residuals_raster)
273
list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster,
277
  #undebug(create_accuracy_residuals_raster)
278
  list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster,
274 279
                                  list_param=list_param_create_accuracy_residuals_raster)
275 280

  
276
#create_accuracy_residuals_raster_obj <- create_accuracy_metric_raster(1, list_param_create_accuracy_residuals_raster_obj)
281
  #create_accuracy_residuals_raster_obj <- create_accuracy_metric_raster(1, list_param_create_accuracy_residuals_raster_obj)
282

  
283
}
284

  
285
lf_accuracy_residuals_raster <- lapply(1:length(list_raster_created_obj),FUN=function(i){unlist(list_raster_created_obj[[i]]$list_raster_name)}) 
286

  
287
#Plot as quick check
288
r1 <- raster(lf_mosaic[[1]][1]) 
289

  
290
list_create_accuracy_residuals_raster_obj
277 291

  
278 292
#################################
279 293
#### Second mosaic tiles for the variable and accuracy metrid
......
325 339
                                        out_dir=out_dir)
326 340
  
327 341
  list_mosaic_obj[[i]] <- list(prediction=mosaic_edge_obj_prediction,accuracy=mosaic_edge_obj_accuracy)
342

  
343
  mosaic_method <- "use_edge_weights" #this is distance from edge
344
  out_suffix_tmp <- paste(interpolation_method,metric_name,day_to_mosaic[i],out_suffix,sep="_")
345

  
346
  mosaic_edge_obj_residuals <- mosaicFiles(lf_accuracy_residuals_raster[[i]],
347
                                        mosaic_method="use_edge_weights",
348
                                        num_cores=num_cores,
349
                                        r_mask_raster_name=infile_mask,
350
                                        python_bin=python_bin,
351
                                        mosaic_python=mosaic_python,
352
                                        algorithm=algorithm,
353
                                        df_points=NULL,
354
                                        NA_flag=NA_flag_val,
355
                                        file_format=file_format,
356
                                        out_suffix=out_suffix_tmp,
357
                                        out_dir=out_dir)
358
  
359
  list_mosaic_obj[[i]] <- list(prediction=mosaic_edge_obj_prediction,accuracy=mosaic_edge_obj_accuracy)
360
  
328 361
}
329 362

  
330 363
#####################

Also available in: Unified diff