Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Revision:
1 fff54c78 Benoit Parmentier
##################    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 b6072700 Benoit Parmentier
#STAGE 4: Raster prediction: run interpolation method -- gam fusion, gam CAI, ...
10 fff54c78 Benoit Parmentier
#STAGE 5: Output analyses-visualization of results for specific dates...
11
#
12
#AUTHOR: Benoit Parmentier                                                                       
13 8bb1a487 Benoit Parmentier
#DATE: 04/02/2013                                                                                 
14 fff54c78 Benoit Parmentier
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 2ba31980 Benoit Parmentier
#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 fff54c78 Benoit Parmentier
49 2ba31980 Benoit Parmentier
#####SCRIPT USED FOR THE PREDICTIONS: Source all scripts here to avoid confusion on versions being run!!!!
50 fff54c78 Benoit Parmentier
51 8bb1a487 Benoit Parmentier
#source(file.path(script_path,"master_script_temp_04022013.R")) #Master script can be run directly...
52 b6072700 Benoit Parmentier
53 3fd24f79 Benoit Parmentier
#CALLED FROM MASTER SCRIPT:
54 fff54c78 Benoit Parmentier
55 3fd24f79 Benoit Parmentier
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_03192013.py
56
source(file.path(script_path,"covariates_production_temperatures_03212013.R"))
57 8bb1a487 Benoit Parmentier
source(file.path(script_path,"Database_stations_covariates_processing_function_03272013.R"))
58 2ba31980 Benoit Parmentier
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 8bb1a487 Benoit Parmentier
#source(file.path(script_path,"results_covariates_database_stations_output_analyses_04012013.R"))
61 b6072700 Benoit Parmentier
62 2ba31980 Benoit Parmentier
#FUNCTIONS CALLED FROM GAM FUSION ANALYSIS RASTER PREDICTION ARE FOUND IN...
63 b6072700 Benoit Parmentier
64 3fd24f79 Benoit Parmentier
source(file.path(script_path,"sampling_script_functions_03122013.R"))
65 8bb1a487 Benoit Parmentier
source(file.path(script_path,"GAM_fusion_function_multisampling_04022013.R")) #Include GAM_CAI
66 2ba31980 Benoit Parmentier
source(file.path(script_path,"GAM_fusion_function_multisampling_validation_metrics_03272013.R"))
67 b6072700 Benoit Parmentier
68
############ STAGE 1: LST Climatology ###############
69
70
if (stages_to_run[1]==1){
71
  #Call run through python
72 3fd24f79 Benoit Parmentier
  #/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_03182013.py
73 b6072700 Benoit Parmentier
}
74 fff54c78 Benoit Parmentier
75
############ STAGE 2: Covariate production ################
76
77 3fd24f79 Benoit Parmentier
##Paths to inputs and output
78 8bb1a487 Benoit Parmentier
var<-"TMIN"
79 3fd24f79 Benoit Parmentier
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
80
out_path<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data/"
81
82
lc_path<-"/home/layers/data/land-cover/lc-consensus-global"
83
infile_modis_grid<-"modis_sinusoidal_grid_world.shp"
84
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
85
infile_canheight<-"Simard_Pinto_3DGlobalVeg_JGR.tif"              #Canopy height
86
list_tiles_modis = c('h11v08','h11v07','h12v07','h12v08','h10v07','h10v08') #tile for Venezuel and surrounding area
87
infile_reg_outline=""  #input region outline defined by polygon
88
CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
89
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
90
out_region_name<-"_venezuela_region" #generated on the fly
91
out_suffix<-"_VE_03192013"
92
ref_rast_name<-""  #local raster name defining resolution, exent, local projection--. set on the fly??
93
#The names of covariates can be changed...these names should be output/input from covar script!!!
94
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
95
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
96
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",
97
             "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
98
             "nobs_09","nobs_10","nobs_11","nobs_12")
99
covar_names<-c(rnames,lc_names,lst_names)
100
101
list_param_covar_production<-list(var,in_path,out_path,lc_path,infile_modis_grid,infile_elev,infile_canheight,
102
                                  list_tiles_modis,infile_reg_outline,CRS_interp,CRS_locs_WGS84,out_region_name,
103
                                  out_suffix,ref_rast_name,covar_names) 
104
105
names(list_param_covar_production)<-c("var","in_path","out_path","lc_path","infile_modis_grid","infile_elev","infile_canheight",
106
                                      "list_tiles_modis","infile_reg_outline","CRS_interp","CRS_locs_WGS84","out_region_name",
107
                                      "out_suffix","ref_rast_name","covar_names") 
108
109 b6072700 Benoit Parmentier
if (stages_to_run[2]==2){
110
  #Transform into function...
111 3fd24f79 Benoit Parmentier
  #/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/covariates_production_temperatures_03212013.R
112
  infile_covar_brick<-covariates_production_temperature(list_param_covar_production)
113 b6072700 Benoit Parmentier
}
114 fff54c78 Benoit Parmentier
115
############# STAGE 3: Data preparation ###############
116
117
#Setting up input argurments for script function...
118 3fd24f79 Benoit Parmentier
#set up earlier
119 8bb1a487 Benoit Parmentier
#var <- "TMIN"           # name of the variables to keep: TMIN, TMAX or PRCP --already set up earlier
120 2ba31980 Benoit Parmentier
121 8bb1a487 Benoit Parmentier
#infile_covariates<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script and used in stage 3 and stage 4
122
infile_covariates<-"covariates__venezuela_region_TMIN__VE_03192013.tif" #covariates stack for TMIN
123 2ba31980 Benoit Parmentier
124 3fd24f79 Benoit Parmentier
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84: same as earlier
125
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
126
#covar_names see stage 2
127 fff54c78 Benoit Parmentier
128 3fd24f79 Benoit Parmentier
#specific to this stage
129 fff54c78 Benoit Parmentier
db.name <- "ghcn"       # name of the Postgres database
130 8bb1a487 Benoit Parmentier
range_years<-c("2000","2001") #right bound not included in the range!!
131
range_years_clim<-c("1981","2011") #right bound not included in the range!!
132 fff54c78 Benoit Parmentier
infile2<-"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt"                              #This is the textfile of station locations from GHCND
133
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
134 8bb1a487 Benoit Parmentier
out_prefix<-"_365d_GAM_fus5_all_lstd_04022013"                #User defined output prefix
135 2ba31980 Benoit Parmentier
qc_flags_stations<-c("0","S")    #flags allowed for screening after the query from the GHCND??
136 fff54c78 Benoit Parmentier
137 2ba31980 Benoit Parmentier
#list of 12 parameters for input in the function...
138 fff54c78 Benoit Parmentier
139 2ba31980 Benoit Parmentier
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)
140
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")
141 fff54c78 Benoit Parmentier
names(list_param_prep)<-cnames
142
143
##### RUN SCRIPT TO GET STATION DATA WITH COVARIATES #####
144 d7f715b5 Benoit Parmentier
145
list_outfiles<-database_covariates_preparation(list_param_prep)
146 fff54c78 Benoit Parmentier
147
############### STAGE 4: RASTER PREDICTION #################
148
149 b6072700 Benoit Parmentier
#Prepare parameters for for raster prediction... 
150
151
#Collect parameters from the previous stage: data preparation stage
152
153
#3 parameters from output
154
infile_monthly<-list_outfiles$monthly_covar_ghcn_data #outile4 from database_covar script
155
infile_daily<-list_outfiles$daily_covar_ghcn_data  #outfile3 from database_covar script
156
infile_locs<- list_outfiles$loc_stations_ghcn #outfile2? from database covar script
157
158 d7f715b5 Benoit Parmentier
#names(outfiles_obj)<- c("loc_stations","loc_stations_ghcn","daily_covar_ghcn_data","monthly_covar_ghcn_data")
159 b6072700 Benoit Parmentier
160 d7f715b5 Benoit Parmentier
list_param_data_prep<-list(infile_monthly,infile_daily,infile_locs,infile_covariates,covar_names,var,out_prefix,CRS_locs_WGS84)
161 b6072700 Benoit Parmentier
names(list_param_data_prep)<-c("infile_monthly","infile_daily","infile_locs","infile_covariates","covar_names","var","out_prefix","CRS_locs_WGS84")
162
163
#Set additional parameters
164
#Input for sampling function...
165
seed_number<- 100  #if seed zero then no seed?     
166
nb_sample<-1           #number of time random sampling must be repeated for every hold out proportion
167
step<-0         
168
constant<-0             #if value 1 then use the same samples as date one for the all set of dates
169
prop_minmax<-c(0.3,0.3)  #if prop_min=prop_max and step=0 then predicitons are done for the number of dates...
170 d7f715b5 Benoit Parmentier
#dates_selected<-c("20100101","20100102","20100103","20100901") # Note that the dates set must have a specific format: yyymmdd
171
dates_selected<-"" # if empty string then predict for the full year specified earlier
172 b6072700 Benoit Parmentier
173
#Models to run...this can be change for each run
174
list_models<-c("y_var ~ s(elev_1)",
175
               "y_var ~ s(LST)",
176
               "y_var ~ s(elev_1,LST)",
177
               "y_var ~ s(lat) + s(lon)+ s(elev_1)",
178
               "y_var ~ s(lat,lon,elev_1)",
179
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST)", 
180
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC2)",
181
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC6)", 
182
               "y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(DISTOC)")
183
184
#Choose interpolation method...
185
interpolation_method<-c("gam_fusion","gam_CAI") #other otpions to be added later
186
187
#Default name of LST avg to be matched               
188
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")  
189
in_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data"
190
#Create on the fly output folder...
191
out_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data"
192
script_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/"
193
194
#Collect all parameters in a list
195
list_param_raster_prediction<-list(list_param_data_prep,
196 d7f715b5 Benoit Parmentier
                                seed_number,nb_sample,step,constant,prop_minmax,dates_selected,
197 b6072700 Benoit Parmentier
                                list_models,lst_avg,in_path,out_path,script_path,
198
                                interpolation_method)
199
200
names(list_param_raster_prediction)<-c("list_param_data_prep",
201 d7f715b5 Benoit Parmentier
                                "seed_number","nb_sample","step","constant","prop_minmax","dates_selected",
202 b6072700 Benoit Parmentier
                                "list_models","lst_avg","in_path","out_path","script_path",
203
                                "interpolation_method")
204
205
206 2ba31980 Benoit Parmentier
#Make the function call--names to be change to raster_prediction_obj and raster_prediction_fun
207
208 b6072700 Benoit Parmentier
raster_prediction_gam_fus_obj <-raster_prediction_gam_fusion(list_param_raster_prediction)
209 fff54c78 Benoit Parmentier
210
211
############## STAGE 5: OUTPUT ANALYSES ##################
212
213 8bb1a487 Benoit Parmentier
#source(file.path(script_path,"results_interpolation_date_output_analyses_03272013.R"))
214 3fd24f79 Benoit Parmentier
#Call as function...
215 fff54c78 Benoit Parmentier
216
###############   END OF SCRIPT   ###################
217
#####################################################