Project

General

Profile

Download (12.3 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/28/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
stages_to_run<-c(0,0,3,4,5) #May decide on antoher strategy later on...
48

    
49
#####SCRIPT USED FOR THE PREDICTIONS: Source all scripts here to avoid confusion on versions being run!!!!
50

    
51
#source(file.path(script_path,"master_script_temp_03282013.R")) #Master script can be run directly...
52

    
53
#CALLED FROM MASTER SCRIPT:
54

    
55
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_03192013.py
56
source(file.path(script_path,"covariates_production_temperatures_03212013.R"))
57
source(file.path(script_path,"Database_stations_covariates_processing_function_03242013.R"))
58
source(file.path(script_path,"GAM_fusion_analysis_raster_prediction_multisampling_03272013.R"))
59
source(file.path(script_path,"results_interpolation_date_output_analyses_03272013.R"))
60
#source(file.path(script_path,"results_covariates_database_stations_output_analyses_03272013.R"))
61

    
62
#FUNCTIONS CALLED FROM GAM FUSION ANALYSIS RASTER PREDICTION ARE FOUND IN...
63

    
64
source(file.path(script_path,"sampling_script_functions_03122013.R"))
65
source(file.path(script_path,"GAM_fusion_function_multisampling_03192013.R")) #Include GAM_CAI
66
source(file.path(script_path,"GAM_fusion_function_multisampling_validation_metrics_03272013.R"))
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_03182013.py
73
}
74

    
75
############ STAGE 2: Covariate production ################
76

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

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

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

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

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

    
116
############# STAGE 3: Data preparation ###############
117

    
118
#Setting up input argurments for script function...
119
#set up earlier
120
var <- "TMAX"           # name of the variables to keep: TMIN, TMAX or PRCP --already set up earlier
121

    
122
infile_covariates<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script and used in stage 3 and stage 4
123
#infile_covariates<-"covariates__venezuela_region_TMIN__VE_03192013.tif" #covariates stack for TMIN
124

    
125
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84: same as earlier
126
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
127
#covar_names see stage 2
128

    
129
#specific to this stage
130
db.name <- "ghcn"       # name of the Postgres database
131
range_years<-c("2000","2001") #right bound not included in the range!!
132
range_years_clim<-c("1981","2011") #right bound not included in the range!!
133
infile2<-"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt"                              #This is the textfile of station locations from GHCND
134
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
135
out_prefix<-"_365d_GAM_fus5_all_lstd_03282013"                #User defined output prefix
136
qc_flags_stations<-c("0","S")    #flags allowed for screening after the query from the GHCND??
137

    
138
#list of 12 parameters for input in the function...
139

    
140
list_param_prep<-list(db.name,var,range_years,range_years_clim,infile1,infile2,infile_covariates,CRS_locs_WGS84,in_path,covar_names,qc_flags_stations,out_prefix)
141
cnames<-c("db.name","var","range_years","range_years_clim","infile1","infile2","infile_covariates","CRS_locs_WGS84","in_path","covar_names","qc_flags_stations","out_prefix")
142
names(list_param_prep)<-cnames
143

    
144
##### RUN SCRIPT TO GET STATION DATA WITH COVARIATES #####
145

    
146
list_outfiles<-database_covariates_preparation(list_param_prep)
147

    
148
############### STAGE 4: RASTER PREDICTION #################
149

    
150
#Prepare parameters for for raster prediction... 
151

    
152
#Collect parameters from the previous stage: data preparation stage
153

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

    
159
#names(outfiles_obj)<- c("loc_stations","loc_stations_ghcn","daily_covar_ghcn_data","monthly_covar_ghcn_data")
160

    
161
list_param_data_prep<-list(infile_monthly,infile_daily,infile_locs,infile_covariates,covar_names,var,out_prefix,CRS_locs_WGS84)
162
names(list_param_data_prep)<-c("infile_monthly","infile_daily","infile_locs","infile_covariates","covar_names","var","out_prefix","CRS_locs_WGS84")
163

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

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

    
185
#Choose interpolation method...
186
interpolation_method<-c("gam_fusion","gam_CAI") #other otpions to be added later
187

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

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

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

    
206

    
207
#Make the function call--names to be change to raster_prediction_obj and raster_prediction_fun
208

    
209
raster_prediction_gam_fus_obj <-raster_prediction_gam_fusion(list_param_raster_prediction)
210

    
211

    
212
############## STAGE 5: OUTPUT ANALYSES ##################
213

    
214
#source(file.path(script_path,"results_interpolation_date_output_analyses_03052013.R"))
215
#Call as function...
216

    
217
###############   END OF SCRIPT   ###################
218
#####################################################
219

    
(30-30/41)