1
|
################## Master script for temperature predictions #######################################
|
2
|
############################ TMIN AND TMAX predictions ##########################################
|
3
|
#
|
4
|
##This script produces intperpolated surface of TMIN and TMAX for specified processing region given sets
|
5
|
#of inputs and parameters.
|
6
|
#STAGE 1: LST climatology calculation
|
7
|
#STAGE 2: Covariates preparation: aspect, land cover, distance to coast etc.
|
8
|
#STAGE 3: Data preparation: database query, extraction of covariates from stack
|
9
|
#STAGE 4: Raster prediction: run interpolation method -- gam fusion, gam CAI, ...
|
10
|
#STAGE 5: Output analyses-visualization of results for specific dates...
|
11
|
#
|
12
|
#AUTHOR: Benoit Parmentier
|
13
|
#DATE: 03/14/2013
|
14
|
|
15
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363, TASK$568--
|
16
|
|
17
|
##Comments and TODO:
|
18
|
# Modify code for stage 1 and call python script from R
|
19
|
# Modify code for stage 2, make it a function and fully automated (distoc var)
|
20
|
# Add options to run only specific stage + additional out_suffix?
|
21
|
# Make master script a function?
|
22
|
# Add log file for master script,add function to collect inputs and outputs
|
23
|
##################################################################################################
|
24
|
|
25
|
###Loading R library and packages
|
26
|
library(RPostgreSQL)
|
27
|
library(maps)
|
28
|
library(maptools)
|
29
|
library(parallel)
|
30
|
library(gtools) # loading some useful tools
|
31
|
library(mgcv) # GAM package by Simon Wood
|
32
|
library(sp) # Spatial pacakge with class definition by Bivand et al.
|
33
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al.
|
34
|
library(rgdal) # GDAL wrapper for R, spatial utilities
|
35
|
library(gstat) # Kriging and co-kriging by Pebesma et al.
|
36
|
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines
|
37
|
library(raster) # Hijmans et al. package for raster processing
|
38
|
library(rasterVis)
|
39
|
library(reshape)
|
40
|
library(plotrix)
|
41
|
|
42
|
### Parameters and arguments
|
43
|
|
44
|
script_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/"
|
45
|
#list_script_files<-
|
46
|
stages_to_run<-c(1,2,3,4,5) #May decide on antoher strategy later on...
|
47
|
|
48
|
#####SCRIPT USED FOR THE PREDICTIONS
|
49
|
|
50
|
#master_script_temp_03052013.R
|
51
|
|
52
|
#IN MASTER SCRIPT:
|
53
|
|
54
|
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_01252013b.py
|
55
|
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/covariates_production_temperatures_02062013.R
|
56
|
|
57
|
#source(file.path(script_path,"Database_stations_covariates_processing_function_03052013.R"))
|
58
|
#source(file.path(script_path,"GAM_fusion_analysis_raster_prediction_multisampling_03052013.R"))
|
59
|
#source(file.path(script_path,"results_interpolation_date_output_analyses_03052013.R"))
|
60
|
|
61
|
#CALLED FROM GAM FUSION ANALYSIS RASTER PREDICTION
|
62
|
|
63
|
#source(file.path(script_path,"sampling_script_functions_03052013.R"))
|
64
|
#source(file.path(script_path,"GAM_fusion_function_multisampling_03052013.R")) #Include GAM_CAI
|
65
|
#source(file.path(script_path,"GAM_fusion_function_multisampling_validation_metrics_02262013.R"))
|
66
|
|
67
|
|
68
|
############ STAGE 1: LST Climatology ###############
|
69
|
|
70
|
if (stages_to_run[1]==1){
|
71
|
#Call run through python
|
72
|
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_01252013b.py
|
73
|
}
|
74
|
|
75
|
############ STAGE 2: Covariate production ################
|
76
|
|
77
|
if (stages_to_run[2]==2){
|
78
|
#Transform into function...
|
79
|
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/covariates_production_temperatures_02062013.R
|
80
|
}
|
81
|
|
82
|
############# STAGE 3: Data preparation ###############
|
83
|
|
84
|
source(file.path(script_path,"Database_stations_covariates_processing_function_03132013.R"))
|
85
|
|
86
|
#Setting up input argurments for script function...
|
87
|
|
88
|
db.name <- "ghcn" # name of the Postgres database
|
89
|
var <- "TMAX" # name of the variables to keep: TMIN, TMAX or PRCP
|
90
|
range_years<-c("2000","2001") #right bound not included in the range!!
|
91
|
range_years_clim<-c("2000","2011") #right bound not included in the range!!
|
92
|
infile1<- "outline_venezuela_region__VE_01292013.shp" #This is the shape file of outline of the study area #It is an input/output of the covariate script
|
93
|
infile2<-"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt" #This is the textfile of station locations from GHCND
|
94
|
infile_covariates<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script and used in stage 3 and stage 4
|
95
|
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84: same as earlier
|
96
|
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
|
97
|
out_prefix<-"_365d_GAM_fus5_all_lstd_03142013" #User defined output prefix
|
98
|
#qc_flags<- flags allowe for the query from the GHCND??
|
99
|
|
100
|
#The names of covariates can be changed...these names should be output/input from covar script!!!
|
101
|
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
|
102
|
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
|
103
|
lst_names<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12",
|
104
|
"nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
|
105
|
"nobs_09","nobs_10","nobs_11","nobs_12")
|
106
|
covar_names<-c(rnames,lc_names,lst_names)
|
107
|
|
108
|
#list of 11 parameters for input in the function...
|
109
|
|
110
|
list_param_prep<-list(db.name,var,range_years,range_years_clim,infile1,infile2,infile_covariates,CRS_locs_WGS84,in_path,covar_names,out_prefix)
|
111
|
cnames<-c("db.name","var","range_years","range_years_clim","infile1","infile2","infile_covariates","CRS_locs_WGS84","in_path","covar_names","out_prefix")
|
112
|
names(list_param_prep)<-cnames
|
113
|
|
114
|
##### RUN SCRIPT TO GET STATION DATA WITH COVARIATES #####
|
115
|
|
116
|
list_outfiles<-database_covariates_preparation(list_param_prep)
|
117
|
|
118
|
############### STAGE 4: RASTER PREDICTION #################
|
119
|
|
120
|
#Prepare parameters for for raster prediction...
|
121
|
|
122
|
#Collect parameters from the previous stage: data preparation stage
|
123
|
|
124
|
#3 parameters from output
|
125
|
infile_monthly<-list_outfiles$monthly_covar_ghcn_data #outile4 from database_covar script
|
126
|
infile_daily<-list_outfiles$daily_covar_ghcn_data #outfile3 from database_covar script
|
127
|
infile_locs<- list_outfiles$loc_stations_ghcn #outfile2? from database covar script
|
128
|
|
129
|
#names(outfiles_obj)<- c("loc_stations","loc_stations_ghcn","daily_covar_ghcn_data","monthly_covar_ghcn_data")
|
130
|
|
131
|
list_param_data_prep<-list(infile_monthly,infile_daily,infile_locs,infile_covariates,covar_names,var,out_prefix,CRS_locs_WGS84)
|
132
|
names(list_param_data_prep)<-c("infile_monthly","infile_daily","infile_locs","infile_covariates","covar_names","var","out_prefix","CRS_locs_WGS84")
|
133
|
|
134
|
#Set additional parameters
|
135
|
#Input for sampling function...
|
136
|
seed_number<- 100 #if seed zero then no seed?
|
137
|
nb_sample<-1 #number of time random sampling must be repeated for every hold out proportion
|
138
|
step<-0
|
139
|
constant<-0 #if value 1 then use the same samples as date one for the all set of dates
|
140
|
prop_minmax<-c(0.3,0.3) #if prop_min=prop_max and step=0 then predicitons are done for the number of dates...
|
141
|
#dates_selected<-c("20100101","20100102","20100103","20100901") # Note that the dates set must have a specific format: yyymmdd
|
142
|
dates_selected<-"" # if empty string then predict for the full year specified earlier
|
143
|
|
144
|
#Models to run...this can be change for each run
|
145
|
list_models<-c("y_var ~ s(elev_1)",
|
146
|
"y_var ~ s(LST)",
|
147
|
"y_var ~ s(elev_1,LST)",
|
148
|
"y_var ~ s(lat) + s(lon)+ s(elev_1)",
|
149
|
"y_var ~ s(lat,lon,elev_1)",
|
150
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST)",
|
151
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC2)",
|
152
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC6)",
|
153
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(DISTOC)")
|
154
|
|
155
|
#Choose interpolation method...
|
156
|
interpolation_method<-c("gam_fusion","gam_CAI") #other otpions to be added later
|
157
|
|
158
|
#Default name of LST avg to be matched
|
159
|
lst_avg<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12")
|
160
|
in_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data"
|
161
|
#Create on the fly output folder...
|
162
|
out_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data"
|
163
|
script_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/"
|
164
|
|
165
|
#Collect all parameters in a list
|
166
|
list_param_raster_prediction<-list(list_param_data_prep,
|
167
|
seed_number,nb_sample,step,constant,prop_minmax,dates_selected,
|
168
|
list_models,lst_avg,in_path,out_path,script_path,
|
169
|
interpolation_method)
|
170
|
|
171
|
names(list_param_raster_prediction)<-c("list_param_data_prep",
|
172
|
"seed_number","nb_sample","step","constant","prop_minmax","dates_selected",
|
173
|
"list_models","lst_avg","in_path","out_path","script_path",
|
174
|
"interpolation_method")
|
175
|
|
176
|
#Source file
|
177
|
source(file.path(script_path,"GAM_fusion_analysis_raster_prediction_multisampling_03132013.R"))
|
178
|
|
179
|
#Make the function call
|
180
|
raster_prediction_gam_fus_obj <-raster_prediction_gam_fusion(list_param_raster_prediction)
|
181
|
|
182
|
|
183
|
############## STAGE 5: OUTPUT ANALYSES ##################
|
184
|
|
185
|
source(file.path(script_path,"results_interpolation_date_output_analyses_03052013.R"))
|
186
|
|
187
|
|
188
|
############### END OF SCRIPT ###################
|
189
|
#####################################################
|
190
|
|