Project

General

Profile

Download (11.7 KB) Statistics
| Branch: | Revision:
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/21/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_03192013.R
51

    
52
#CALLED FROM MASTER SCRIPT:
53

    
54
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_03192013.py
55
source(file.path(script_path,"covariates_production_temperatures_03212013.R"))
56
source(file.path(script_path,"Database_stations_covariates_processing_function_03132013.R"))
57
source(file.path(script_path,"GAM_fusion_analysis_raster_prediction_multisampling_03182013.R"))
58
source(file.path(script_path,"results_interpolation_date_output_analyses_03182013.R"))
59

    
60
#CALLED FROM GAM FUSION ANALYSIS RASTER PREDICTION
61

    
62
source(file.path(script_path,"sampling_script_functions_03122013.R"))
63
source(file.path(script_path,"GAM_fusion_function_multisampling_03142013.R")) #Include GAM_CAI
64
source(file.path(script_path,"GAM_fusion_function_multisampling_validation_metrics_03182013.R"))
65

    
66

    
67
############ STAGE 1: LST Climatology ###############
68

    
69
if (stages_to_run[1]==1){
70
  #Call run through python
71
  #/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_03182013.py
72
}
73

    
74
############ STAGE 2: Covariate production ################
75

    
76
##Paths to inputs and output
77
var<-"TMIN"
78
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
79
out_path<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data/"
80

    
81
lc_path<-"/home/layers/data/land-cover/lc-consensus-global"
82
infile_modis_grid<-"modis_sinusoidal_grid_world.shp"
83
infile_elev<-"/home/layers/data/terrain/dem-cgiar-srtm-1km-tif/srtm_1km.tif"  #this is the global file: replace later with the input produced by the DEM team
84
infile_canheight<-"Simard_Pinto_3DGlobalVeg_JGR.tif"              #Canopy height
85
list_tiles_modis = c('h11v08','h11v07','h12v07','h12v08','h10v07','h10v08') #tile for Venezuel and surrounding area
86
infile_reg_outline=""  #input region outline defined by polygon
87
CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
88
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
89
out_region_name<-"_venezuela_region" #generated on the fly
90
out_suffix<-"_VE_03192013"
91
ref_rast_name<-""  #local raster name defining resolution, exent, local projection--. set on the fly??
92
#The names of covariates can be changed...these names should be output/input from covar script!!!
93
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
94
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
95
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",
96
             "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
97
             "nobs_09","nobs_10","nobs_11","nobs_12")
98
covar_names<-c(rnames,lc_names,lst_names)
99

    
100
list_param_covar_production<-list(var,in_path,out_path,lc_path,infile_modis_grid,infile_elev,infile_canheight,
101
                                  list_tiles_modis,infile_reg_outline,CRS_interp,CRS_locs_WGS84,out_region_name,
102
                                  out_suffix,ref_rast_name,covar_names) 
103

    
104
names(list_param_covar_production)<-c("var","in_path","out_path","lc_path","infile_modis_grid","infile_elev","infile_canheight",
105
                                      "list_tiles_modis","infile_reg_outline","CRS_interp","CRS_locs_WGS84","out_region_name",
106
                                      "out_suffix","ref_rast_name","covar_names") 
107

    
108
if (stages_to_run[2]==2){
109
  #Transform into function...
110
  #/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/covariates_production_temperatures_03212013.R
111
  infile_covar_brick<-covariates_production_temperature(list_param_covar_production)
112
}
113

    
114
############# STAGE 3: Data preparation ###############
115

    
116

    
117
#Setting up input argurments for script function...
118
#set up earlier
119
infile_covariates<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script and used in stage 3 and stage 4
120
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84: same as earlier
121
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
122
#covar_names see stage 2
123
var <- "TMAX"           # name of the variables to keep: TMIN, TMAX or PRCP
124

    
125
#specific to this stage
126
db.name <- "ghcn"       # name of the Postgres database
127
range_years<-c("2000","2001") #right bound not included in the range!!
128
range_years_clim<-c("1980","2011") #right bound not included in the range!!
129
infile2<-"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt"                              #This is the textfile of station locations from GHCND
130
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
131
out_prefix<-"_365d_GAM_fus5_all_lstd_03182013"                #User defined output prefix
132
#qc_flags<-    flags allowe for the query from the GHCND??
133

    
134

    
135
#list of 11 parameters for input in the function...
136

    
137
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)
138
cnames<-c("db.name","var","range_years","range_years_clim","infile1","infile2","infile_covariates","CRS_locs_WGS84","in_path","covar_names","out_prefix")
139
names(list_param_prep)<-cnames
140

    
141
##### RUN SCRIPT TO GET STATION DATA WITH COVARIATES #####
142

    
143
list_outfiles<-database_covariates_preparation(list_param_prep)
144

    
145
############### STAGE 4: RASTER PREDICTION #################
146

    
147
#Prepare parameters for for raster prediction... 
148

    
149
#Collect parameters from the previous stage: data preparation stage
150

    
151
#3 parameters from output
152
infile_monthly<-list_outfiles$monthly_covar_ghcn_data #outile4 from database_covar script
153
infile_daily<-list_outfiles$daily_covar_ghcn_data  #outfile3 from database_covar script
154
infile_locs<- list_outfiles$loc_stations_ghcn #outfile2? from database covar script
155

    
156
#names(outfiles_obj)<- c("loc_stations","loc_stations_ghcn","daily_covar_ghcn_data","monthly_covar_ghcn_data")
157

    
158
list_param_data_prep<-list(infile_monthly,infile_daily,infile_locs,infile_covariates,covar_names,var,out_prefix,CRS_locs_WGS84)
159
names(list_param_data_prep)<-c("infile_monthly","infile_daily","infile_locs","infile_covariates","covar_names","var","out_prefix","CRS_locs_WGS84")
160

    
161
#Set additional parameters
162
#Input for sampling function...
163
seed_number<- 100  #if seed zero then no seed?     
164
nb_sample<-1           #number of time random sampling must be repeated for every hold out proportion
165
step<-0         
166
constant<-0             #if value 1 then use the same samples as date one for the all set of dates
167
prop_minmax<-c(0.3,0.3)  #if prop_min=prop_max and step=0 then predicitons are done for the number of dates...
168
#dates_selected<-c("20100101","20100102","20100103","20100901") # Note that the dates set must have a specific format: yyymmdd
169
dates_selected<-"" # if empty string then predict for the full year specified earlier
170

    
171
#Models to run...this can be change for each run
172
list_models<-c("y_var ~ s(elev_1)",
173
               "y_var ~ s(LST)",
174
               "y_var ~ s(elev_1,LST)",
175
               "y_var ~ s(lat) + s(lon)+ s(elev_1)",
176
               "y_var ~ s(lat,lon,elev_1)",
177
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST)", 
178
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC2)",
179
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC6)", 
180
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(DISTOC)")
181

    
182
#Choose interpolation method...
183
interpolation_method<-c("gam_fusion","gam_CAI") #other otpions to be added later
184

    
185
#Default name of LST avg to be matched               
186
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")  
187
in_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data"
188
#Create on the fly output folder...
189
out_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data"
190
script_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/"
191

    
192
#Collect all parameters in a list
193
list_param_raster_prediction<-list(list_param_data_prep,
194
                                seed_number,nb_sample,step,constant,prop_minmax,dates_selected,
195
                                list_models,lst_avg,in_path,out_path,script_path,
196
                                interpolation_method)
197

    
198
names(list_param_raster_prediction)<-c("list_param_data_prep",
199
                                "seed_number","nb_sample","step","constant","prop_minmax","dates_selected",
200
                                "list_models","lst_avg","in_path","out_path","script_path",
201
                                "interpolation_method")
202

    
203
#Source file
204

    
205
#Make the function call
206
raster_prediction_gam_fus_obj <-raster_prediction_gam_fusion(list_param_raster_prediction)
207

    
208

    
209
############## STAGE 5: OUTPUT ANALYSES ##################
210

    
211
#source(file.path(script_path,"results_interpolation_date_output_analyses_03052013.R"))
212
#Call as function...
213

    
214
###############   END OF SCRIPT   ###################
215
#####################################################
216

    
(30-30/40)