1 |
65d96c1a
|
Benoit Parmentier
|
######################### Raster prediction ####################################
|
2 |
1cd7ecf2
|
Benoit Parmentier
|
############################ Interpolation of temperature for given processing region ##########################################
|
3 |
|
|
#This script interpolates temperature values using MODIS LST, covariates and GHCND station data.
|
4 |
|
|
#It requires the text file of stations and a shape file of the study area.
|
5 |
fb039a6b
|
Benoit Parmentier
|
#Note that the projection for both GHCND and study area is lonlat WGS84.
|
6 |
|
|
#Options to run this program are:
|
7 |
|
|
#1) Multisampling: vary the porportions of hold out and use random samples for each run
|
8 |
|
|
#2)Constant sampling: use the same sample over the runs
|
9 |
|
|
#3)over dates: run over for example 365 dates without mulitsampling
|
10 |
|
|
#4)use seed number: use seed if random samples must be repeatable
|
11 |
a2f26ded
|
Benoit Parmentier
|
#5)possibilty of running single and multiple time scale methods:
|
12 |
|
|
# gam_daily, kriging_daily,gwr_daily,gam_CAI,gam_fusion,kriging_fusion,gwr_fusion and other options added.
|
13 |
|
|
#For multiple time scale methods, the interpolation is done first at the monthly time scale then delta surfaces are added.
|
14 |
fb039a6b
|
Benoit Parmentier
|
#AUTHOR: Benoit Parmentier
|
15 |
a2f26ded
|
Benoit Parmentier
|
#DATE: 07/30/2013
|
16 |
7506a985
|
Benoit Parmentier
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#568--
|
17 |
|
|
#
|
18 |
|
|
# TO DO:
|
19 |
a2f26ded
|
Benoit Parmentier
|
#Add methods to for CAI
|
20 |
7506a985
|
Benoit Parmentier
|
|
21 |
e7bf2d1b
|
Benoit Parmentier
|
###################################################################################################
|
22 |
|
|
|
23 |
65d96c1a
|
Benoit Parmentier
|
raster_prediction_fun <-function(list_param_raster_prediction){
|
24 |
ae46eb91
|
Benoit Parmentier
|
|
25 |
|
|
##Function to predict temperature interpolation with 21 input parameters
|
26 |
7506a985
|
Benoit Parmentier
|
#9 parameters used in the data preparation stage and input in the current script
|
27 |
|
|
#1)list_param_data_prep: used in earlier code for the query from the database and extraction for raster brick
|
28 |
a2f26ded
|
Benoit Parmentier
|
#2)infile_monthly: monthly averages with covariates for GHCND stations obtained after query
|
29 |
|
|
#3)infile_daily: daily GHCND stations with covariates, obtained after query
|
30 |
|
|
#4)infile_locs: vector file with station locations for the processing/study area (ESRI shapefile)
|
31 |
7506a985
|
Benoit Parmentier
|
#5)infile_covariates: raster covariate brick, tif file
|
32 |
|
|
#6)covar_names: covar_names #remove at a later stage...
|
33 |
|
|
#7)var: variable being interpolated-TMIN or TMAX
|
34 |
|
|
#8)out_prefix
|
35 |
|
|
#9)CRS_locs_WGS84
|
36 |
c8354a28
|
Benoit Parmentier
|
#10)screen_data_training
|
37 |
7506a985
|
Benoit Parmentier
|
#
|
38 |
|
|
#6 parameters for sampling function
|
39 |
|
|
#10)seed_number
|
40 |
|
|
#11)nb_sample
|
41 |
|
|
#12)step
|
42 |
|
|
#13)constant
|
43 |
|
|
#14)prop_minmax
|
44 |
|
|
#15)dates_selected
|
45 |
|
|
#
|
46 |
|
|
#6 additional parameters for monthly climatology and more
|
47 |
|
|
#16)list_models: model formulas in character vector
|
48 |
|
|
#17)lst_avg: LST climatology name in the brick of covariate--change later
|
49 |
|
|
#18)n_path
|
50 |
|
|
#19)out_path
|
51 |
|
|
#20)script_path: path to script
|
52 |
|
|
#21)interpolation_method: c("gam_fusion","gam_CAI") #other otpions to be added later
|
53 |
ae46eb91
|
Benoit Parmentier
|
|
54 |
|
|
###Loading R library and packages
|
55 |
|
|
|
56 |
|
|
library(gtools) # loading some useful tools
|
57 |
|
|
library(mgcv) # GAM package by Simon Wood
|
58 |
|
|
library(sp) # Spatial pacakge with class definition by Bivand et al.
|
59 |
|
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al.
|
60 |
|
|
library(rgdal) # GDAL wrapper for R, spatial utilities
|
61 |
|
|
library(gstat) # Kriging and co-kriging by Pebesma et al.
|
62 |
|
|
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines
|
63 |
|
|
library(raster) # Hijmans et al. package for raster processing
|
64 |
|
|
library(rasterVis)
|
65 |
|
|
library(parallel) # Urbanek S. and Ripley B., package for multi cores & parralel processing
|
66 |
|
|
library(reshape)
|
67 |
|
|
library(plotrix)
|
68 |
|
|
library(maptools)
|
69 |
7506a985
|
Benoit Parmentier
|
library(gdata) #Nesssary to use cbindX
|
70 |
3b4bb643
|
Benoit Parmentier
|
library(automap) #autokrige function
|
71 |
|
|
library(spgwr) #GWR method
|
72 |
|
|
|
73 |
ae46eb91
|
Benoit Parmentier
|
### Parameters and arguments
|
74 |
|
|
#PARSING INPUTS/ARGUMENTS
|
75 |
e63d760c
|
Benoit Parmentier
|
#
|
76 |
|
|
# names(list_param_raster_prediction)<-c("list_param_data_prep",
|
77 |
|
|
# "seed_number","nb_sample","step","constant","prop_minmax","dates_selected",
|
78 |
|
|
# "list_models","lst_avg","in_path","out_path","script_path",
|
79 |
|
|
# "interpolation_method")
|
80 |
ae46eb91
|
Benoit Parmentier
|
|
81 |
|
|
#9 parameters used in the data preparation stage and input in the current script
|
82 |
|
|
list_param_data_prep<-list_param_raster_prediction$list_param_data_prep
|
83 |
|
|
infile_monthly<-list_param_data_prep$infile_monthly
|
84 |
|
|
infile_daily<-list_param_data_prep$infile_daily
|
85 |
|
|
infile_locs<-list_param_data_prep$infile_locs
|
86 |
|
|
infile_covariates<-list_param_data_prep$infile_covariates #raster covariate brick, tif file
|
87 |
|
|
covar_names<- list_param_data_prep$covar_names #remove at a later stage...
|
88 |
|
|
var<-list_param_data_prep$var
|
89 |
|
|
out_prefix<-list_param_data_prep$out_prefix
|
90 |
|
|
CRS_locs_WGS84<-list_param_data_prep$CRS_locs_WGS84
|
91 |
c8354a28
|
Benoit Parmentier
|
|
92 |
ae46eb91
|
Benoit Parmentier
|
|
93 |
|
|
#6 parameters for sampling function
|
94 |
|
|
seed_number<-list_param_raster_prediction$seed_number
|
95 |
|
|
nb_sample<-list_param_raster_prediction$nb_sample
|
96 |
|
|
step<-list_param_raster_prediction$step
|
97 |
|
|
constant<-list_param_raster_prediction$constant
|
98 |
|
|
prop_minmax<-list_param_raster_prediction$prop_minmax
|
99 |
e63d760c
|
Benoit Parmentier
|
dates_selected<-list_param_raster_prediction$dates_selected
|
100 |
ae46eb91
|
Benoit Parmentier
|
|
101 |
|
|
#6 additional parameters for monthly climatology and more
|
102 |
|
|
list_models<-list_param_raster_prediction$list_models
|
103 |
|
|
lst_avg<-list_param_raster_prediction$lst_avg
|
104 |
|
|
out_path<-list_param_raster_prediction$out_path
|
105 |
|
|
script_path<-list_param_raster_prediction$script_path
|
106 |
|
|
interpolation_method<-list_param_raster_prediction$interpolation_method
|
107 |
c8354a28
|
Benoit Parmentier
|
screen_data_training <-list_param_raster_prediction$screen_data_training
|
108 |
ae46eb91
|
Benoit Parmentier
|
|
109 |
2d6af3b8
|
Benoit Parmentier
|
setwd(out_path)
|
110 |
ae46eb91
|
Benoit Parmentier
|
|
111 |
|
|
###################### START OF THE SCRIPT ########################
|
112 |
b2563a44
|
Benoit Parmentier
|
|
113 |
ae46eb91
|
Benoit Parmentier
|
if (var=="TMAX"){
|
114 |
|
|
y_var_name<-"dailyTmax"
|
115 |
|
|
}
|
116 |
c8354a28
|
Benoit Parmentier
|
|
117 |
ae46eb91
|
Benoit Parmentier
|
if (var=="TMIN"){
|
118 |
|
|
y_var_name<-"dailyTmin"
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
################# CREATE LOG FILE #####################
|
122 |
|
|
|
123 |
|
|
#create log file to keep track of details such as processing times and parameters.
|
124 |
|
|
|
125 |
5ed5c7a3
|
Benoit Parmentier
|
#log_fname<-paste("R_log_raster_prediction",out_prefix, ".log",sep="")
|
126 |
b2563a44
|
Benoit Parmentier
|
log_fname<-paste("R_log_raster_prediction",out_prefix, ".log",sep="")
|
127 |
5ed5c7a3
|
Benoit Parmentier
|
#sink(log_fname) #create new log file
|
128 |
fc7c30c1
|
Benoit Parmentier
|
file.create(file.path(out_path,log_fname)) #create new log file
|
129 |
ae46eb91
|
Benoit Parmentier
|
|
130 |
|
|
time1<-proc.time() #Start stop watch
|
131 |
5ed5c7a3
|
Benoit Parmentier
|
|
132 |
|
|
cat(paste("Starting script at this local Date and Time: ",as.character(Sys.time()),sep=""),
|
133 |
|
|
file=log_fname,sep="\n")
|
134 |
|
|
cat("Starting script process time:",file=log_fname,sep="\n",append=TRUE)
|
135 |
|
|
cat(as.character(time1),file=log_fname,sep="\n",append=TRUE)
|
136 |
ae46eb91
|
Benoit Parmentier
|
|
137 |
|
|
############### READING INPUTS: DAILY STATION DATA AND OTEHR DATASETS #################
|
138 |
|
|
|
139 |
c8354a28
|
Benoit Parmentier
|
ghcn<-readOGR(dsn=dirname(infile_daily),layer=sub(".shp","",basename(infile_daily)))
|
140 |
ae46eb91
|
Benoit Parmentier
|
CRS_interp<-proj4string(ghcn) #Storing projection information (ellipsoid, datum,etc.)
|
141 |
c8354a28
|
Benoit Parmentier
|
stat_loc<-readOGR(dsn=dirname(infile_locs),layer=sub(".shp","",basename(infile_locs)))
|
142 |
e63d760c
|
Benoit Parmentier
|
#dates2 <-readLines(file.path(in_path,dates_selected)) #dates to be predicted, now read directly from the file
|
143 |
|
|
if (dates_selected==""){
|
144 |
|
|
dates<-as.character(sort(unique(ghcn$date))) #dates to be predicted
|
145 |
|
|
}
|
146 |
|
|
if (dates_selected!=""){
|
147 |
|
|
dates<-dates_selected #dates to be predicted
|
148 |
|
|
}
|
149 |
ae46eb91
|
Benoit Parmentier
|
|
150 |
fc7c30c1
|
Benoit Parmentier
|
#Reading in covariate brickcan be changed...
|
151 |
ae46eb91
|
Benoit Parmentier
|
|
152 |
|
|
s_raster<-brick(infile_covariates) #read in the data brck
|
153 |
|
|
names(s_raster)<-covar_names #Assigning names to the raster layers: making sure it is included in the extraction
|
154 |
e7efff8e
|
Benoit Parmentier
|
|
155 |
|
|
#Reading monthly data
|
156 |
c8354a28
|
Benoit Parmentier
|
dst<-readOGR(dsn=dirname(infile_monthly),layer=sub(".shp","",basename(infile_monthly)))
|
157 |
fc7c30c1
|
Benoit Parmentier
|
|
158 |
ae46eb91
|
Benoit Parmentier
|
########### CREATE SAMPLING -TRAINING AND TESTING STATIONS ###########
|
159 |
|
|
|
160 |
|
|
#Input for sampling function...
|
161 |
|
|
|
162 |
|
|
#dates #list of dates for prediction
|
163 |
|
|
#ghcn_name<-"ghcn" #infile daily data
|
164 |
|
|
|
165 |
|
|
list_param_sampling<-list(seed_number,nb_sample,step,constant,prop_minmax,dates,ghcn)
|
166 |
|
|
#list_param_sampling<-list(seed_number,nb_sample,step,constant,prop_minmax,dates,ghcn_name)
|
167 |
|
|
names(list_param_sampling)<-c("seed_number","nb_sample","step","constant","prop_minmax","dates","ghcn")
|
168 |
|
|
|
169 |
e63d760c
|
Benoit Parmentier
|
#run function, note that dates must be a character vector!!
|
170 |
ae46eb91
|
Benoit Parmentier
|
sampling_obj<-sampling_training_testing(list_param_sampling)
|
171 |
|
|
|
172 |
|
|
########### PREDICT FOR MONTHLY SCALE ##################
|
173 |
|
|
|
174 |
|
|
#First predict at the monthly time scale: climatology
|
175 |
5ed5c7a3
|
Benoit Parmentier
|
cat("Predictions at monthly scale:",file=log_fname,sep="\n", append=TRUE)
|
176 |
|
|
cat(paste("Local Date and Time: ",as.character(Sys.time()),sep=""),
|
177 |
|
|
file=log_fname,sep="\n")
|
178 |
ae46eb91
|
Benoit Parmentier
|
t1<-proc.time()
|
179 |
|
|
j=12
|
180 |
2d6af3b8
|
Benoit Parmentier
|
#browser() #Missing out_path for gam_fusion list param!!!
|
181 |
b493e133
|
Benoit Parmentier
|
#if (interpolation_method=="gam_fusion"){
|
182 |
|
|
if (interpolation_method %in% c("gam_fusion","kriging_fusion","gwr_fusion")){
|
183 |
2d6af3b8
|
Benoit Parmentier
|
list_param_runClim_KGFusion<-list(j,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, out_prefix,out_path)
|
184 |
|
|
names(list_param_runClim_KGFusion)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","out_prefix","out_path")
|
185 |
65d96c1a
|
Benoit Parmentier
|
#source(file.path(script_path,"GAM_fusion_function_multisampling_03122013.R"))
|
186 |
|
|
clim_method_mod_obj<-mclapply(1:12, list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
|
187 |
44f43362
|
Benoit Parmentier
|
#clim_method_mod_obj<-mclapply(1:6, list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
|
188 |
b493e133
|
Benoit Parmentier
|
#test<-runClim_KGFusion(1,list_param=list_param_runClim_KGFusion)
|
189 |
b2563a44
|
Benoit Parmentier
|
save(clim_method_mod_obj,file= file.path(out_path,paste(interpolation_method,"_mod_",y_var_name,out_prefix,".RData",sep="")))
|
190 |
|
|
list_tmp<-vector("list",length(clim_method_mod_obj))
|
191 |
|
|
for (i in 1:length(clim_method_mod_obj)){
|
192 |
|
|
tmp<-clim_method_mod_obj[[i]]$clim
|
193 |
|
|
list_tmp[[i]]<-tmp
|
194 |
|
|
}
|
195 |
|
|
clim_yearlist<-list_tmp
|
196 |
65d96c1a
|
Benoit Parmentier
|
}
|
197 |
87a06ee7
|
Benoit Parmentier
|
|
198 |
cbd65694
|
Benoit Parmentier
|
if (interpolation_method %in% c("gam_CAI","kriging_CAI", "gwr_CAI")){
|
199 |
5ed5c7a3
|
Benoit Parmentier
|
list_param_runClim_KGCAI<-list(j,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, out_prefix,out_path)
|
200 |
|
|
names(list_param_runClim_KGCAI)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","out_prefix","out_path")
|
201 |
65d96c1a
|
Benoit Parmentier
|
clim_method_mod_obj<-mclapply(1:12, list_param=list_param_runClim_KGCAI, runClim_KGCAI,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
|
202 |
|
|
#test<-runClim_KGCAI(1,list_param=list_param_runClim_KGCAI)
|
203 |
b2563a44
|
Benoit Parmentier
|
#gamclim_fus_mod<-mclapply(1:6, list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6)
|
204 |
|
|
save(clim_method_mod_obj,file= file.path(out_path,paste(interpolation_method,"_mod_",y_var_name,out_prefix,".RData",sep="")))
|
205 |
|
|
list_tmp<-vector("list",length(clim_method_mod_obj))
|
206 |
|
|
for (i in 1:length(clim_method_mod_obj)){
|
207 |
|
|
tmp<-clim_method_mod_obj[[i]]$clim
|
208 |
|
|
list_tmp[[i]]<-tmp
|
209 |
|
|
}
|
210 |
|
|
clim_yearlist<-list_tmp
|
211 |
65d96c1a
|
Benoit Parmentier
|
}
|
212 |
b2563a44
|
Benoit Parmentier
|
t2<-proc.time()-t1
|
213 |
fc7c30c1
|
Benoit Parmentier
|
cat(as.character(t2),file=log_fname,sep="\n", append=TRUE)
|
214 |
|
|
|
215 |
ae46eb91
|
Benoit Parmentier
|
################## PREDICT AT DAILY TIME SCALE #################
|
216 |
65d96c1a
|
Benoit Parmentier
|
#Predict at daily time scale from single time scale or multiple time scale methods: 2 methods availabe now
|
217 |
ae46eb91
|
Benoit Parmentier
|
|
218 |
|
|
#put together list of clim models per month...
|
219 |
|
|
#rast_clim_yearlist<-list_tmp
|
220 |
b2563a44
|
Benoit Parmentier
|
|
221 |
ae46eb91
|
Benoit Parmentier
|
#Second predict at the daily time scale: delta
|
222 |
|
|
|
223 |
65d96c1a
|
Benoit Parmentier
|
#method_mod_obj<-mclapply(1:1, runGAMFusion,mc.preschedule=FALSE,mc.cores = 1) #This is the end bracket from mclapply(...) statement
|
224 |
5ed5c7a3
|
Benoit Parmentier
|
cat("Predictions at the daily scale:",file=log_fname,sep="\n", append=TRUE)
|
225 |
ae46eb91
|
Benoit Parmentier
|
t1<-proc.time()
|
226 |
5ed5c7a3
|
Benoit Parmentier
|
cat(paste("Local Date and Time: ",as.character(Sys.time()),sep=""),
|
227 |
|
|
file=log_fname,sep="\n")
|
228 |
ae46eb91
|
Benoit Parmentier
|
|
229 |
3b4bb643
|
Benoit Parmentier
|
#TODO : Same call for all functions!!! Replace by one "if" for all multi time scale methods...
|
230 |
a2f26ded
|
Benoit Parmentier
|
#The methods could be defined earlier as constant??
|
231 |
cbd65694
|
Benoit Parmentier
|
if (interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){
|
232 |
65d96c1a
|
Benoit Parmentier
|
#input a list:note that ghcn.subsets is not sampling_obj$data_day_ghcn
|
233 |
44f43362
|
Benoit Parmentier
|
i<-1
|
234 |
5ed5c7a3
|
Benoit Parmentier
|
list_param_run_prediction_daily_deviation <-list(i,clim_yearlist,sampling_obj,dst,var,y_var_name, interpolation_method,out_prefix,out_path)
|
235 |
|
|
names(list_param_run_prediction_daily_deviation)<-c("list_index","clim_yearlist","sampling_obj","dst","var","y_var_name","interpolation_method","out_prefix","out_path")
|
236 |
65d96c1a
|
Benoit Parmentier
|
#test<-mclapply(1:18, runGAMFusion,list_param=list_param_runGAMFusion,mc.preschedule=FALSE,mc.cores = 9)
|
237 |
|
|
#test<-runGAMFusion(1,list_param=list_param_runGAMFusion)
|
238 |
|
|
|
239 |
|
|
method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_daily_deviation,run_prediction_daily_deviation,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
240 |
b2563a44
|
Benoit Parmentier
|
save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
|
241 |
b493e133
|
Benoit Parmentier
|
}
|
242 |
b2563a44
|
Benoit Parmentier
|
|
243 |
3b4bb643
|
Benoit Parmentier
|
#TODO : Same call for all functions!!! Replace by one "if" for all daily single time scale methods...
|
244 |
b2563a44
|
Benoit Parmentier
|
if (interpolation_method=="gam_daily"){
|
245 |
|
|
#input a list:note that ghcn.subsets is not sampling_obj$data_day_ghcn
|
246 |
44f43362
|
Benoit Parmentier
|
i<-1
|
247 |
c8354a28
|
Benoit Parmentier
|
list_param_run_prediction_gam_daily <-list(i,s_raster,covar_names,lst_avg,list_models,dst,screen_data_training,var,y_var_name, sampling_obj,interpolation_method,out_prefix,out_path)
|
248 |
|
|
names(list_param_run_prediction_gam_daily)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","screen_data_training","var","y_var_name","sampling_obj","interpolation_method","out_prefix","out_path")
|
249 |
b2563a44
|
Benoit Parmentier
|
#test <- runGAM_day_fun(1,list_param_run_prediction_gam_daily)
|
250 |
fc7c30c1
|
Benoit Parmentier
|
|
251 |
c8354a28
|
Benoit Parmentier
|
method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_gam_daily,runGAM_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
|
252 |
|
|
#method_mod_obj<-mclapply(1:11,list_param=list_param_run_prediction_gam_daily,runGAM_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
|
253 |
65d96c1a
|
Benoit Parmentier
|
|
254 |
b2563a44
|
Benoit Parmentier
|
save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
|
255 |
65d96c1a
|
Benoit Parmentier
|
|
256 |
b2563a44
|
Benoit Parmentier
|
}
|
257 |
|
|
|
258 |
fc7c30c1
|
Benoit Parmentier
|
if (interpolation_method=="kriging_daily"){
|
259 |
|
|
#input a list:note that ghcn.subsets is not sampling_obj$data_day_ghcn
|
260 |
|
|
i<-1
|
261 |
|
|
list_param_run_prediction_kriging_daily <-list(i,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, sampling_obj,interpolation_method,out_prefix,out_path)
|
262 |
|
|
names(list_param_run_prediction_kriging_daily)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","sampling_obj","interpolation_method","out_prefix","out_path")
|
263 |
|
|
#test <- runKriging_day_fun(1,list_param_run_prediction_kriging_daily)
|
264 |
|
|
method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_kriging_daily,runKriging_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
265 |
|
|
#method_mod_obj<-mclapply(1:18,list_param=list_param_run_prediction_kriging_daily,runKriging_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
266 |
|
|
|
267 |
|
|
save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
|
268 |
|
|
|
269 |
3b4bb643
|
Benoit Parmentier
|
}
|
270 |
|
|
|
271 |
|
|
if (interpolation_method=="gwr_daily"){
|
272 |
|
|
#input a list:note that ghcn.subsets is not sampling_obj$data_day_ghcn
|
273 |
|
|
i<-1
|
274 |
|
|
list_param_run_prediction_gwr_daily <-list(i,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, sampling_obj,interpolation_method,out_prefix,out_path)
|
275 |
|
|
names(list_param_run_prediction_gwr_daily)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","sampling_obj","interpolation_method","out_prefix","out_path")
|
276 |
|
|
#test <- run_interp_day_fun(1,list_param_run_prediction_gwr_daily)
|
277 |
c8354a28
|
Benoit Parmentier
|
method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_gwr_daily,run_interp_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
|
278 |
3b4bb643
|
Benoit Parmentier
|
#method_mod_obj<-mclapply(1:9,list_param=list_param_run_prediction_gwr_daily,run_interp_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
279 |
|
|
#method_mod_obj<-mclapply(1:18,list_param=list_param_run_prediction_kriging_daily,runKriging_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
280 |
|
|
|
281 |
|
|
save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
|
282 |
|
|
|
283 |
fc7c30c1
|
Benoit Parmentier
|
}
|
284 |
ae46eb91
|
Benoit Parmentier
|
t2<-proc.time()-t1
|
285 |
5ed5c7a3
|
Benoit Parmentier
|
cat(as.character(t2),file=log_fname,sep="\n", append=TRUE)
|
286 |
e63d760c
|
Benoit Parmentier
|
#browser()
|
287 |
7506a985
|
Benoit Parmentier
|
|
288 |
ae46eb91
|
Benoit Parmentier
|
############### NOW RUN VALIDATION #########################
|
289 |
7506a985
|
Benoit Parmentier
|
#SIMPLIFY THIS PART: one call
|
290 |
ae46eb91
|
Benoit Parmentier
|
|
291 |
65d96c1a
|
Benoit Parmentier
|
list_tmp<-vector("list",length(method_mod_obj))
|
292 |
|
|
for (i in 1:length(method_mod_obj)){
|
293 |
|
|
tmp<-method_mod_obj[[i]][[y_var_name]] #y_var_name is the variable predicted (dailyTmax or dailyTmin)
|
294 |
ae46eb91
|
Benoit Parmentier
|
list_tmp[[i]]<-tmp
|
295 |
|
|
}
|
296 |
b2563a44
|
Benoit Parmentier
|
rast_day_yearlist<-list_tmp #list of predicted images over full year...
|
297 |
ae46eb91
|
Benoit Parmentier
|
|
298 |
5ed5c7a3
|
Benoit Parmentier
|
cat("Validation step:",file=log_fname,sep="\n", append=TRUE)
|
299 |
ae46eb91
|
Benoit Parmentier
|
t1<-proc.time()
|
300 |
5ed5c7a3
|
Benoit Parmentier
|
cat(paste("Local Date and Time: ",as.character(Sys.time()),sep=""),
|
301 |
|
|
file=log_fname,sep="\n")
|
302 |
ae46eb91
|
Benoit Parmentier
|
|
303 |
5ed5c7a3
|
Benoit Parmentier
|
list_param_validation<-list(i,rast_day_yearlist,method_mod_obj,y_var_name, out_prefix, out_path)
|
304 |
|
|
names(list_param_validation)<-c("list_index","rast_day_year_list","method_mod_obj","y_var_name","out_prefix", "out_path") #same names for any method
|
305 |
ae46eb91
|
Benoit Parmentier
|
|
306 |
5ed5c7a3
|
Benoit Parmentier
|
validation_mod_obj <-mclapply(1:length(method_mod_obj), list_param=list_param_validation, calculate_accuracy_metrics,mc.preschedule=FALSE,mc.cores = 9)
|
307 |
b493e133
|
Benoit Parmentier
|
#test_val<-calculate_accuracy_metrics(1,list_param_validation)
|
308 |
5ed5c7a3
|
Benoit Parmentier
|
save(validation_mod_obj,file= file.path(out_path,paste(interpolation_method,"_validation_mod_obj_",y_var_name,out_prefix,".RData",sep="")))
|
309 |
ae46eb91
|
Benoit Parmentier
|
t2<-proc.time()-t1
|
310 |
5ed5c7a3
|
Benoit Parmentier
|
cat(as.character(t2),file=log_fname,sep="\n", append=TRUE)
|
311 |
ae46eb91
|
Benoit Parmentier
|
|
312 |
|
|
#################### ASSESSMENT OF PREDICTIONS: PLOTS OF ACCURACY METRICS ###########
|
313 |
|
|
|
314 |
bd5b6cd5
|
Benoit Parmentier
|
##Create data.frame with validation and fit metrics for a full year/full numbe of runs
|
315 |
|
|
tb_diagnostic_v<-extract_from_list_obj(validation_mod_obj,"metrics_v")
|
316 |
|
|
#tb_diagnostic_v contains accuracy metrics for models sample and proportion for every run...if full year then 365 rows maximum
|
317 |
ae46eb91
|
Benoit Parmentier
|
rownames(tb_diagnostic_v)<-NULL #remove row names
|
318 |
bd5b6cd5
|
Benoit Parmentier
|
tb_diagnostic_v$method_interp <- interpolation_method
|
319 |
|
|
tb_diagnostic_s<-extract_from_list_obj(validation_mod_obj,"metrics_s")
|
320 |
|
|
rownames(tb_diagnostic_s)<-NULL #remove row names
|
321 |
|
|
tb_diagnostic_s$method_interp <- interpolation_method #add type of interpolation...out_prefix too??
|
322 |
ae46eb91
|
Benoit Parmentier
|
|
323 |
5ed5c7a3
|
Benoit Parmentier
|
#Call functions to create plots of metrics for validation dataset
|
324 |
ae46eb91
|
Benoit Parmentier
|
metric_names<-c("rmse","mae","me","r","m50")
|
325 |
bd5b6cd5
|
Benoit Parmentier
|
summary_metrics_v<- boxplot_from_tb(tb_diagnostic_v,metric_names,out_prefix,out_path) #if adding for fit need to change outprefix
|
326 |
c0c600a6
|
Benoit Parmentier
|
names(summary_metrics_v)<-c("avg","median")
|
327 |
5ed5c7a3
|
Benoit Parmentier
|
summary_month_metrics_v<- boxplot_month_from_tb(tb_diagnostic_v,metric_names,out_prefix,out_path)
|
328 |
ae46eb91
|
Benoit Parmentier
|
|
329 |
|
|
#################### CLOSE LOG FILE ####################
|
330 |
|
|
|
331 |
|
|
#close log_file connection and add meta data
|
332 |
5ed5c7a3
|
Benoit Parmentier
|
cat("Finished script process time:",file=log_fname,sep="\n", append=TRUE)
|
333 |
ae46eb91
|
Benoit Parmentier
|
time2<-proc.time()-time1
|
334 |
5ed5c7a3
|
Benoit Parmentier
|
cat(as.character(time2),file=log_fname,sep="\n", append=TRUE)
|
335 |
|
|
#later on add all the parameters used in the script...
|
336 |
|
|
cat(paste("Finished script at this local Date and Time: ",as.character(Sys.time()),sep=""),
|
337 |
|
|
file=log_fname,sep="\n", append=TRUE)
|
338 |
|
|
cat("End of script",file=log_fname,sep="\n", append=TRUE)
|
339 |
|
|
#close(log_fname)
|
340 |
ae46eb91
|
Benoit Parmentier
|
|
341 |
|
|
################### PREPARE RETURN OBJECT ###############
|
342 |
|
|
#Will add more information to be returned
|
343 |
|
|
|
344 |
cbd65694
|
Benoit Parmentier
|
if (interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){
|
345 |
b2563a44
|
Benoit Parmentier
|
raster_prediction_obj<-list(clim_method_mod_obj,method_mod_obj,validation_mod_obj,tb_diagnostic_v,
|
346 |
bd5b6cd5
|
Benoit Parmentier
|
tb_diagnostic_s,summary_metrics_v,summary_month_metrics_v)
|
347 |
b2563a44
|
Benoit Parmentier
|
names(raster_prediction_obj)<-c("clim_method_mod_obj","method_mod_obj","validation_mod_obj","tb_diagnostic_v",
|
348 |
bd5b6cd5
|
Benoit Parmentier
|
"tb_diagnostic_s","summary_metrics_v","summary_month_metrics_v")
|
349 |
b2563a44
|
Benoit Parmentier
|
save(raster_prediction_obj,file= file.path(out_path,paste("raster_prediction_obj_",interpolation_method,"_", y_var_name,out_prefix,".RData",sep="")))
|
350 |
|
|
|
351 |
|
|
}
|
352 |
c8354a28
|
Benoit Parmentier
|
|
353 |
3b4bb643
|
Benoit Parmentier
|
#use %in% instead of "|" operator
|
354 |
|
|
if (interpolation_method=="gam_daily" | interpolation_method=="kriging_daily" | interpolation_method=="gwr_daily"){
|
355 |
b2563a44
|
Benoit Parmentier
|
raster_prediction_obj<-list(method_mod_obj,validation_mod_obj,tb_diagnostic_v,
|
356 |
bd5b6cd5
|
Benoit Parmentier
|
tb_diagnostic_s,summary_metrics_v,summary_month_metrics_v)
|
357 |
b2563a44
|
Benoit Parmentier
|
names(raster_prediction_obj)<-c("method_mod_obj","validation_mod_obj","tb_diagnostic_v",
|
358 |
bd5b6cd5
|
Benoit Parmentier
|
"tb_diagnostic_s","summary_metrics_v","summary_month_metrics_v")
|
359 |
b2563a44
|
Benoit Parmentier
|
save(raster_prediction_obj,file= file.path(out_path,paste("raster_prediction_obj_",interpolation_method,"_", y_var_name,out_prefix,".RData",sep="")))
|
360 |
|
|
|
361 |
|
|
}
|
362 |
ae46eb91
|
Benoit Parmentier
|
|
363 |
|
|
return(raster_prediction_obj)
|
364 |
e7bf2d1b
|
Benoit Parmentier
|
}
|
365 |
|
|
|
366 |
ae46eb91
|
Benoit Parmentier
|
####################################################################
|
367 |
|
|
######################## END OF SCRIPT/FUNCTION #####################
|