Project

General

Profile

« Previous | Next » 

Revision 504953ca

Added by Benoit Parmentier almost 9 years ago

mosaicing script adding functions relevant to accuracy and residuals surface kriging

View differences:

climate/research/oregon/interpolation/global_run_scalingup_mosaicing_function.R
4 4
#Different options to explore mosaicing are tested. This script only contains functions.
5 5
#AUTHOR: Benoit Parmentier 
6 6
#CREATED ON: 04/14/2015  
7
#MODIFIED ON: 12/12/2015            
8
#Version: 1
7
#MODIFIED ON: 12/17/2015            
8
#Version: 2
9 9
#PROJECT: Environmental Layers project     
10 10
#COMMENTS: first commit of function script to test mosaicing using 1500x4500km and other tiles
11 11
#TODO:
......
13 13
#2) Improve performance: there will be a need to improve efficiency for the workflow.
14 14

  
15 15
#Functions: available:
16
#
16
#See below
17 17

  
18 18
#################################################################################################
19 19

  
......
24 24
library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
25 25
library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
26 26
library(rgdal)                               # GDAL wrapper for R, spatial utilities
27
#library(gstat)                               # Kriging and co-kriging by Pebesma et al., not on NEX
27
library(gstat)                               # Kriging and co-kriging by Pebesma et al., not on NEX
28 28
library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
29 29
library(raster)                              # Hijmans et al. package for raster processing
30 30
library(gdata)                               # various tools with xls reading, cbindX
......
36 36
library(plotrix)                             # Additional plotting functions
37 37
library(plyr)                                # Various tools including rbind.fill
38 38
#library(spgwr)                               # GWR method, not on NEX
39
#library(automap)                             # Kriging automatic fitting of variogram using gstat, not on NEX
40
#library(rgeos)                               # Geometric, topologic library of functions
39
library(automap)                             # Kriging automatic fitting of variogram using gstat, not on NEX
40
library(rgeos)                               # Geometric, topologic library of functions
41 41
#RPostgreSQL                                 # Interface R and Postgres, not used in this script
42 42
library(gridExtra)
43 43
#Additional libraries not used in workflow
......
48 48

  
49 49
#### FUNCTION USED IN SCRIPT
50 50

  
51
function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_07182014.R" #first interp paper
52
function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_08132014.R"
51
##List all the functions in this script:
53 52

  
54
load_obj <- function(f)
55
{
56
  env <- new.env()
57
  nm <- load(f, env)[1]
58
  env[[nm]]
59
}
53
#[1] "autoKrige_fun"                      
54
#[2] "create_accuracy_metric_raster"     
55
#[3] "create_accuracy_residuals_raster"   
56
#[4] "create_weights_fun"                
57
# [5] "fit_models"                         "function_mosaicing"                
58
# [7] "in_dir_script"                      "mosaicFiles"                       
59
# [9] "mosaic_m_raster_list"               "mosaic_python_merge"               
60
#[11] "plot_daily_mosaics"                 "plot_diff_raster"                  
61
#[13] "plot_mosaic"                        "plot_screen_raster_val"            
62
#[15] "predict_accuracy_raster_by_station" "predict_auto_krige_raster_model"   
63
#[17] "raster_match"                       "remove_na_spdf"                    
64
#[19] "select_var_stack"                   "sine_structure_fun"    
60 65

  
61
create_dir_fun <- function(out_dir,out_suffix){
62
  if(!is.null(out_suffix)){
63
    out_name <- paste("output_",out_suffix,sep="")
64
    out_dir <- file.path(out_dir,out_name)
65
  }
66
  #create if does not exists
67
  if(!file.exists(out_dir)){
68
    dir.create(out_dir)
69
  }
70
  return(out_dir)
71
}
66
############## START FUNCTIONS DEFINITIONS ####
72 67

  
73 68
sine_structure_fun <-function(x,T,phase,a,b,use_cos=FALSE){
74 69
  
......
88 83
  return(y)
89 84
}
90 85

  
91
## Add numcores
86
## Add numcores: done
92 87
## use mclapply
93 88
create_accuracy_metric_raster <- function(i, list_param){
94 89
  #This function generates weights from a point location on a raster layer.
......
503 498
    #num_cores <- 11
504 499

  
505 500
    #debug(create_weights_fun)
506
    weights_obj <- create_weights_fun(1,list_param=list_param_create_weights)
501
    #weights_obj <- create_weights_fun(1,list_param=list_param_create_weights)
507 502

  
508 503
    #This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to
509 504
    #the edges...can use rows and columsn to set edges to 1 and 0 for the others.
......
552 547
    list_param_create_weights <- list(lf_mosaic,df_points,r_feature,method,out_dir_str) 
553 548
    names(list_param_create_weights) <- c("lf","df_points","r_feature","method","out_dir_str") 
554 549
    #num_cores <- 11
555

  
550
    #debug(create_weights_fun)
556 551
    #weights_obj <- create_weights_fun(1,list_param=list_param_create_weights)
557 552

  
558 553
    #This is the function creating the weights by tile. Distance from the centroids needs to be change from distance to
......
1247 1242
    class(result) = c("autoKrige", "list")
1248 1243
    return(result)
1249 1244
}
1245

  
1246
predict_accuracy_raster_by_station <- function(var_pred,ref_rast,data_training,out_filename,out_suffix_str,out_dir,mask_file=NULL){
1247
  #
1248
  coord_xy<-coordinates(data_training)
1249
  var_vals <- data_training[[var_pred]]
1250
  fitKrig <- Krig(coord_xy,var_vals)#,theta=1e5) #use TPS or krige 
1251
  #fitKrig <- Krig(coord_xy,var_vals,theta=1e5) #use TPS or krige 
1252
  #mod_krtmp1<-fitKrig
1253
  #model_name<-"mod_kr"
1254
  #options(scipen=999)
1255
  krig_rast <- try(interpolate(ref_rast,fitKrig)) #interpolation using function from raster package
1256

  
1257
  #Write out modeled layers
1258
    
1259
  if(is.null(mask_file)){
1260
    writeRaster(krig_rast, NAflag=NA_flag_val,filename=out_filename,overwrite=TRUE)  
1261
  }
1262
  if(!is.null(mask_file)){
1263
    #modify here later
1264
    writeRaster(krig_rast, NAflag=NA_flag_val,filename=out_filename,overwrite=TRUE)  
1265
  }
1266

  
1267
  ### Preparing return object
1268
  kriging_obj <- list(out_filename,fitKrig)
1269
  names(kriging_obj)<-c("raster_name","mod_obj")
1270
  #save(kriging_obj,file= file.path(out_dir,paste("kriging_obj","_",out_suffix_str,".RData",sep="")))
1271
  return(kriging_obj)
1272
}
1273

  
1274
create_accuracy_residuals_raster <- function(i,list_param){
1275

  
1276
  #create_accuracy_residuals_raster <- function(i,lf_day_tiles,data_df,df_tile_processed,var_pred,list_models,date_processed,num_cores,NA_flag_val,file_format,out_dir_str,out_suffix_str){
1277
  #This function generates surface for residuals values for a giving set of formula and data input.
1278
  #The method used is currently kriging with two options: automap/gstat and Fields packages.
1279
  #Inputs:
1280
  #lf_day_tiles: list of raster files
1281
  #df_tile_processed: processed tiles
1282
  #data_df: data.frame table/spdf containing stations with residuals and variable
1283
  #var_pred: variable selected to be mapped using modeling (kriging)
1284
  #list_models: formula for the modeling (kriging) e.g. useg by autokrige
1285
  #y_var_name: variable being modeled e.g."dailyTmax", dailyTmin, precip  
1286
  #interpolation_method: names of the interpolation/modeling method
1287
  #date_processed: day being processed , e.g. 19920101, can be month too!!!
1288
  #num_cores : number of cores used in the parallelization
1289
  #NA_flag_val: value used as flag in the raster 
1290
  #file_format: e.g. tif., .rst
1291
  #out_dir_str: output directory
1292
  #out_suffix_str: output suffix
1293
  #Outputs:
1294
  #raster list of resdiuals surfaces and associated modeles by tiles and for one date.
1295
  #
1296
  #TODO: 
1297

  
1298
  # - automap/gstat for data with projection
1299
  # - clean up
1300
  #
1301

  
1302
  
1303
  #### FUNCTIONS USED #####
1304
  generate_residuals_raster <- function(j,list_param){
1305
    ##Add documentation here later...
1306
    
1307
    ### PARSE ARGUMENTS ##
1308
    
1309
    lf <- list_param$lf
1310
    var_pred <- list_param$var_pred
1311
    data_df <- list_param$data_df
1312
    df_raster_pred_tiles <- list_param$df_raster_pred_tiles
1313
    list_formulas <- list_param$list_formulas
1314
    NA_flag_val <- list_param$NA_flag_val
1315
    file_format <- list_param$file_format
1316
    out_dir_str <- list_param$out_dir_str
1317
    out_suffix_str <- list_param$out_suffix_str
1318

  
1319
    ###### START SCRIPT
1320
    
1321
    #list_pred_res_obj <-vector("list",length=length(lf))
1322
    #for(j in 1:length(lf)){
1323

  
1324
    inFilename <- lf[j]
1325

  
1326
    ref_rast <- raster(inFilename)
1327
    #out_filename <- "test.tif"
1328
    #create output name for predicted raster
1329
    extension_str <- extension(inFilename)
1330
    raster_name_tmp <- gsub(extension_str,"",basename(inFilename))
1331
    out_filename <- file.path(out_dir,paste(raster_name_tmp,"_","kriged_residuals_",var_pred,"_",out_suffix,file_format,sep="")) #for use in function later...
1332

  
1333
    #tile_selected <- as.character(df_raster_pred_tiles$tile_id[j])
1334
    data_df$tile_id <- as.character(data_df$tile_id)
1335
    tile_selected <- df_raster_pred_tiles$tile_id[j]
1336
    data_training <- subset(data_df,tile_id==tile_selected) #df_raster_pred_tiles$files
1337
    data_training <- data_training[!is.na(data_training[[var_pred]]),] #screen NA in the independent var
1338
    
1339
    if(use_autokrige==TRUE){
1340
      #this is still under development since there was an error message!!
1341
      r_stack <- stack(inFilename)
1342
      #debug(predict_auto_krige_raster_model) #this calls other function to clean up the inputs
1343
      #data_training_tmp <- idw(zinc ~ 1, meuse2[!is.na(meuse2$zinc),],newdata= meuse.grid)
1344
      
1345
      pred_res_obj <- predict_auto_krige_raster_model(list_formulas,r_stack,data_training,out_filename)
1346
      #mod <- try(autoKrige(formula_mod, input_data=data_fit,new_data=s_spdf,data_variogram=data_fit))
1347
      #Error in autoKrige(formula_mod, input_data = data_fit, new_data = s_spdf,  : 
1348
      #Either input_data or new_data is in LongLat, please reproject.
1349
      #input_data:  +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
1350
      # new_data:    +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
1351
      #Problems using autoKrige on non projected data!!! it looks like the fields package is needed...
1352
      #Modified autokrige function...but new error:
1353
      #Error : dimensions do not match: locations 264 and data 125
1354
    }
1355
  
1356
    if(use_autokrige==FALSE){
1357
      #New function with the Fields package...
1358
      #debug(predict_accuracy_raster_by_station)
1359
      #pred_res_obj <- predict_accuracy_raster_by_station(var_pred,ref_rast,data_training,out_filename,out_suffix_str,out_dir,mask_file=NULL)
1360
      pred_res_obj <- predict_accuracy_raster_by_station(var_pred,ref_rast,data_training,out_filename,out_suffix_str,out_dir_str,mask_file=NULL)
1361
                                
1362
    }  
1363
    return(pred_res_obj)
1364
  }
1365

  
1366
  ####### PARSE ARGUMENTS
1367
  
1368

  
1369
  #lf <- list_param$lf[[i]] #list of files to mosaic
1370
  lf_day_tiles <- list_param$lf_day_tiles[[i]] #list of raster files
1371
  data_df <- list_param$data_df # data.frame table/spdf containing stations with residuals and variable
1372
  df_tile_processed_reg <- list_param$df_tile_processed_reg #tiles processed during assessment usually by region
1373
  var_pred <- list_param$var_pred #variable being modeled
1374
  list_models <- list_param$list_models #formula for the modeling 
1375
  use_autokrige <- list_param$use_autokrige #if TRUE use automap/gstat package
1376
  y_var_name <- list_param$y_var_name #"dailyTmax" #PARAM2
1377
  interpolation_method <- list_param$interpolation_method #c("gam_CAI") #PARAM3
1378
  date_processed <- list_param$days_to_process[i] #can be a monthly layer
1379
  num_cores <- list_param$num_cores #number of cores used
1380
  NA_flag_val <- list_param$NA_flag_val
1381
  #NAflag,file_format,out_suffix etc...
1382
  file_format <- list_param$file_format
1383
  out_dir_str <- list_param$out_dir
1384
  out_suffix_str <- list_param$out_suffix
1385
  
1386
  ######## START SCRIPT ###############
1387
  
1388
  list_formulas <- lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
1389

  
1390
  #data_training <- data_df 
1391
  coordinates(data_df)<-cbind(data_df$x,data_df$y)
1392
  data_df <- subset(data_df,date==date_processed) #select the date...
1393
  #lf_day_tiles <- lf_mosaic[[i]] #i index for time...can be monthly or daily time steps??, ok
1394
  
1395
  #Now match the correct tiles with data used in kriging...
1396
  #match the correct tile!!! df_tile_processed
1397
  #pattern_str <- as.character(unique(df_tile_processed$tile_coord))
1398
  list_tile_coord <- as.character(df_tile_processed_reg$tile_coord)
1399
  pattern_str <- glob2rx(paste("*",list_tile_coord,"*","*.tif",sep=""))
1400
  keywords_str <- pattern_str
1401
  tmp_str2 <-unlist(lapply(keywords_str,grep,lf_day_tiles,value=TRUE))
1402
  df_raster_pred_tiles_tmp <- data.frame(files =tmp_str2, tile_coord=list_tile_coord)
1403
  df_raster_pred_tiles <- merge(df_raster_pred_tiles_tmp,df_tile_processed_reg,by="tile_coord")
1404
  df_raster_pred_tiles$path_NEX <- as.character(df_raster_pred_tiles$path_NEX)
1405
  df_raster_pred_tiles$reg <- basename(dirname(df_raster_pred_tiles$path_NEX))
1406
  df_raster_pred_tiles$files <- as.character(df_raster_pred_tiles$files)
1407
  df_raster_pred_tiles$tile_id <- as.character(df_raster_pred_tiles$tile_id)
1408
  
1409
  #identify residuals
1410
  
1411
  #call kriging function
1412
  
1413
  ###This can be a new function here with mclapply!!!
1414
  ## Addtional loop...
1415
  #j <- 1 #loops across tiles from set of files/tiles
1416

  
1417
  lf <- df_raster_pred_tiles$files
1418
  
1419
  ##Make this loop a function later on, testing right now
1420
  list_param_generate_residuals_raster <- list(lf,var_pred,data_df,df_raster_pred_tiles,list_formulas,NA_flag_val,file_format,out_dir,out_suffix)
1421
  names(list_param_generate_residuals_raster) <- c("lf","var_pred","data_df","df_raster_pred_tiles","list_formulas","NA_flag_val","file_format","out_dir","out_suffix")
1422

  
1423
  #debug(generate_residuals_raster)
1424
  #test_lf <- lapply(1,FUN=generate_residuals_raster,list_param=list_param_generate_residuals_raster)                           
1425
  
1426
  list_pred_res_obj <- mclapply(1:length(lf),FUN=generate_residuals_raster,list_param=list_param_generate_residuals_raster,mc.preschedule=FALSE,mc.cores = num_cores)                           
1427

  
1428
  #write output
1429
  accuracy_residuals_obj <-list(list_pred_res_obj,data_df,df_raster_pred_tiles)
1430
  names(accuracy_residuals_obj)<-c("list_pred_res_obj","data_df","df_raster_pred_tiles")
1431
  save(accuracy_residuals_obj,file= file.path(out_dir,paste("accuracy_residuals_obj_",date_processed,"_",var_pred,
1432
                                                            out_suffix_str,".RData",sep="")))
1433
  
1434
  return(accuracy_residuals_obj) 
1435
}
1436

  
1250 1437
##################### END OF SCRIPT ######################

Also available in: Unified diff