Project

General

Profile

« Previous | Next » 

Revision b67dfd67

Added by Benoit Parmentier about 10 years ago

initial commit for spatial subsampling code for NEX tile with many stations

View differences:

climate/research/oregon/interpolation/subsampling_data.R
1
####################################  INTERPOLATION OF TEMPERATURES  #######################################
2
############################  Script for manuscript analyses,tables and figures #######################################
3
#This script uses the worklfow code applied to the Oregon case study. Daily methods (GAM,GWR, Kriging) are tested with
4
#different covariates using two baselines. Accuracy methods are added in the the function script to evaluate results.
5
#Figures, tables and data for the contribution of covariate paper are also produced in the script.
6
#AUTHOR: Benoit Parmentier                                                                      
7
#MODIFIED ON: 09/11/2014            
8
#Version: 5
9
#PROJECT: Environmental Layers project                                     
10
#################################################################################################
11

  
12
### Loading R library and packages        
13
#library used in the workflow production:
14
library(gtools)                              # loading some useful tools 
15
library(mgcv)                                # GAM package by Simon Wood
16
library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
17
library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
18
library(rgdal)                               # GDAL wrapper for R, spatial utilities
19
library(gstat)                               # Kriging and co-kriging by Pebesma et al.
20
library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
21
library(raster)                              # Hijmans et al. package for raster processing
22
library(gdata)                               # various tools with xls reading, cbindX
23
library(rasterVis)                           # Raster plotting functions
24
library(parallel)                            # Parallelization of processes with multiple cores
25
library(maptools)                            # Tools and functions for sp and other spatial objects e.g. spCbind
26
library(maps)                                # Tools and data for spatial/geographic objects
27
library(reshape)                             # Change shape of object, summarize results 
28
library(plotrix)                             # Additional plotting functions
29
library(plyr)                                # Various tools including rbind.fill
30
library(spgwr)                               # GWR method
31
library(automap)                             # Kriging automatic fitting of variogram using gstat
32
library(rgeos)                               # Geometric, topologic library of functions
33
#RPostgreSQL                                 # Interface R and Postgres, not used in this script
34
library(gridExtra)                           # Combining lattice plots
35
library(colorRamps)                          # Palette/color ramps for symbology
36
#Additional libraries not used in workflow
37
library(pgirmess)                            # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess}  
38
library(ncf)                                 # No paramtric covariance function
39

  
40
#### FUNCTION USED IN SCRIPT
41

  
42
function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_07182014.R" #first interp paper
43
function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_10062014.R"
44

  
45
##############################
46
#### Parameters and constants  
47

  
48
script_path<-"/home/parmentier/Data/IPLANT_project/env_layers_scripts/" #path to script
49
source(file.path(script_path,function_analyses_paper1)) #source all functions used in this script 1.
50
source(file.path(script_path,function_analyses_paper2)) #source all functions used in this script 2.
51

  
52
#Multi time scale - CAI: gam, kriging, gwr
53
in_dir4 <-"/data/project/layers/commons/Oregon_interpolation/output_data_365d_gam_cai_lst_comb5_11032013"
54
raster_obj_file_4 <- "raster_prediction_obj_gam_CAI_dailyTmax_365d_gam_cai_lst_comb5_11032013.RData"
55

  
56
raster_prediction_obj_4 <- load_obj(file.path(in_dir4,raster_obj_file_4))
57

  
58
names(raster_prediction_obj_4)
59
names(raster_prediction_obj_4$method_mod_obj[[1]])
60

  
61
(raster_prediction_obj_4$method_mod_obj[[1]]$data_month_v) #no holdout...
62
data_month <- raster_prediction_obj_4$method_mod_obj[[1]]$data_month_s #no holdout...
63
dim(data_month)
64
test <- zerodist(data_month, zero = 0.0, unique.ID = FALSE)
65

  
66
target_max_nb <- 200
67
target_min_nb <- 100
68
max_dist <- 10000
69
min_dist <- 0
70
step_dist <- 1000
71
target_range_nb <- c(target_min_nb,target_max_nb)
72
#debug(sub_sampling_by_dist)
73
#First increase distance till 5km
74
#then use random sampling...to get the extact target?
75
test <- sub_sampling_by_dist(target_range_nb,dist=min_dist,step=step_dist,data_in=data_month)
76

  
77
dist_range <- c(0,5000) 
78

  
79

  
80
sub_sampling_stat <- function(target_range_nb=,sampling=T)
81

  
82
sub_sampling_by_dist <- function(target_range_nb=c(10000,10000),dist=0.0,step,data_in){
83
  data <- data_in
84
  target_min_nb <- target_range_nb[1]
85
  station_nb <- nrow(data_in)
86
  while(station_nb > target_min_nb){  #} #|| nrow > 0){
87
      #test <- zerodist(data, zero = 0.0, unique.ID = FALSE)
88
      #test <- remove.duplicates(data_month, zero = 5000)
89
      data <- remove.duplicates(data, zero = dist) #spatially sub sample...
90
      dist <- dist + step
91
      station_nb <- nrow(data)
92
  }
93
  obj_sub_sampling <- list(data,dist)
94
  names(obj_sub_sampling) <- c("data","dist")
95
  return(obj_sub_sampling)
96
}
97

  

Also available in: Unified diff