1 |
5adb2c5c
|
Benoit Parmentier
|
################## Functions for use in the raster prediction stage #######################################
|
2 |
|
|
############################ Interpolation in a given tile/region ##########################################
|
3 |
|
|
#This script contains 5 functions used in the interpolation of temperature in the specfied study/processing area:
|
4 |
|
|
# 1)predict_raster_model<-function(in_models,r_stack,out_filename)
|
5 |
|
|
# 2)fit_models<-function(list_formulas,data_training)
|
6 |
|
|
# 3)runClimCAI<-function(j) : not working yet
|
7 |
|
|
# 4)runClim_KGFusion<-function(j,list_param)
|
8 |
|
|
# 5)runGAMFusion <- function(i,list_param)
|
9 |
|
|
#
|
10 |
|
|
#AUTHOR: Benoit Parmentier
|
11 |
|
|
#DATE: 03/12/2013
|
12 |
|
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--
|
13 |
|
|
|
14 |
|
|
##Comments and TODO:
|
15 |
|
|
#This script is meant to be for general processing tile by tile or region by region.
|
16 |
|
|
# Note that the functions are called from GAM_fusion_analysis_raster_prediction_mutlisampling.R.
|
17 |
|
|
# This will be expanded to other methods.
|
18 |
|
|
|
19 |
|
|
##################################################################################################
|
20 |
0163d0e2
|
Benoit Parmentier
|
|
21 |
57a4fede
|
Benoit Parmentier
|
|
22 |
|
|
predict_raster_model<-function(in_models,r_stack,out_filename){
|
23 |
|
|
#This functions performs predictions on a raster grid given input models.
|
24 |
|
|
#Arguments: list of fitted models, raster stack of covariates
|
25 |
|
|
#Output: spatial grid data frame of the subset of tiles
|
26 |
|
|
list_rast_pred<-vector("list",length(in_models))
|
27 |
|
|
for (i in 1:length(in_models)){
|
28 |
|
|
mod <-in_models[[i]] #accessing GAM model ojbect "j"
|
29 |
|
|
raster_name<-out_filename[[i]]
|
30 |
|
|
if (inherits(mod,"gam")) { #change to c("gam","autoKrige")
|
31 |
a96491e0
|
Benoit Parmentier
|
raster_pred<- predict(object=r_stack,model=mod,na.rm=FALSE) #Using the coeff to predict new values.
|
32 |
57a4fede
|
Benoit Parmentier
|
names(raster_pred)<-"y_pred"
|
33 |
|
|
writeRaster(raster_pred, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
34 |
a96491e0
|
Benoit Parmentier
|
#print(paste("Interpolation:","mod", j ,sep=" "))
|
35 |
57a4fede
|
Benoit Parmentier
|
list_rast_pred[[i]]<-raster_name
|
36 |
|
|
}
|
37 |
|
|
}
|
38 |
|
|
if (inherits(mod,"try-error")) {
|
39 |
|
|
print(paste("no gam model fitted:",mod[1],sep=" ")) #change message for any model type...
|
40 |
|
|
}
|
41 |
|
|
return(list_rast_pred)
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
fit_models<-function(list_formulas,data_training){
|
45 |
|
|
#This functions several models and returns model objects.
|
46 |
|
|
#Arguments: - list of formulas for GAM models
|
47 |
|
|
# - fitting data in a data.frame or SpatialPointDataFrame
|
48 |
|
|
#Output: list of model objects
|
49 |
|
|
list_fitted_models<-vector("list",length(list_formulas))
|
50 |
|
|
for (k in 1:length(list_formulas)){
|
51 |
|
|
formula<-list_formulas[[k]]
|
52 |
|
|
mod<- try(gam(formula, data=data_training)) #change to any model!!
|
53 |
|
|
#mod<- try(autoKrige(formula, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
|
54 |
|
|
model_name<-paste("mod",k,sep="")
|
55 |
|
|
assign(model_name,mod)
|
56 |
|
|
list_fitted_models[[k]]<-mod
|
57 |
|
|
}
|
58 |
|
|
return(list_fitted_models)
|
59 |
|
|
}
|
60 |
|
|
|
61 |
0163d0e2
|
Benoit Parmentier
|
####
|
62 |
|
|
#TODO:
|
63 |
|
|
#Add log file and calculate time and sizes for processes-outputs
|
64 |
eec6f6d5
|
Benoit Parmentier
|
runClimCAI<-function(j,list_param){
|
65 |
|
|
|
66 |
57a4fede
|
Benoit Parmentier
|
#Make this a function with multiple argument that can be used by mcmapply??
|
67 |
eec6f6d5
|
Benoit Parmentier
|
#Arguments:
|
68 |
|
|
#1)list_index: j
|
69 |
|
|
#2)covar_rast: covariates raster images used in the modeling
|
70 |
|
|
#3)covar_names: names of input variables
|
71 |
|
|
#4)lst_avg: list of LST climatogy names, may be removed later on
|
72 |
|
|
#5)list_models: list input models for bias calculation
|
73 |
|
|
#6)dst: data at the monthly time scale
|
74 |
|
|
#7)var: TMAX or TMIN, variable being interpolated
|
75 |
|
|
#8)y_var_name: output name, not used at this stage
|
76 |
|
|
#9)out_prefix
|
77 |
|
|
#
|
78 |
|
|
#The output is a list of four shapefile names produced by the function:
|
79 |
|
|
#1) clim: list of output names for raster climatogies
|
80 |
|
|
#2) data_month: monthly training data for bias surface modeling
|
81 |
|
|
#3) mod: list of model objects fitted
|
82 |
|
|
#4) formulas: list of formulas used in bias modeling
|
83 |
|
|
|
84 |
|
|
### PARSING INPUT ARGUMENTS
|
85 |
|
|
#list_param_runGAMFusion<-list(i,clim_yearlist,sampling_obj,var,y_var_name, out_prefix)
|
86 |
|
|
|
87 |
|
|
index<-list_param$j
|
88 |
|
|
s_raster<-list_param$covar_rast
|
89 |
|
|
covar_names<-list_param$covar_names
|
90 |
|
|
lst_avg<-list_param$lst_avg
|
91 |
|
|
list_models<-list_param$list_models
|
92 |
|
|
dst<-list_param$dst #monthly station dataset
|
93 |
|
|
var<-list_param$var
|
94 |
|
|
y_var_name<-list_param$y_var_name
|
95 |
|
|
out_prefix<-list_param$out_prefix
|
96 |
|
|
|
97 |
57a4fede
|
Benoit Parmentier
|
#Model and response variable can be changed without affecting the script
|
98 |
|
|
prop_month<-0 #proportion retained for validation
|
99 |
|
|
run_samp<-1
|
100 |
|
|
|
101 |
eec6f6d5
|
Benoit Parmentier
|
#### STEP 2: PREPARE DATA
|
102 |
|
|
|
103 |
57a4fede
|
Benoit Parmentier
|
data_month<-dst[dst$month==j,] #Subsetting dataset for the relevant month of the date being processed
|
104 |
|
|
LST_name<-lst_avg[j] # name of LST month to be matched
|
105 |
|
|
data_month$LST<-data_month[[LST_name]]
|
106 |
|
|
|
107 |
eec6f6d5
|
Benoit Parmentier
|
list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
|
108 |
|
|
|
109 |
57a4fede
|
Benoit Parmentier
|
#TMax to model...
|
110 |
eec6f6d5
|
Benoit Parmentier
|
if (var=="TMAX"){
|
111 |
|
|
data_month$y_var<-data_month$TMax #Adding TMax as the variable modeled
|
112 |
|
|
}
|
113 |
|
|
if (var=="TMIN"){
|
114 |
|
|
data_month$y_var<-data_month$TMin #Adding TMi as the variable modeled
|
115 |
|
|
}
|
116 |
|
|
|
117 |
57a4fede
|
Benoit Parmentier
|
mod_list<-fit_models(list_formulas,data_month) #only gam at this stage
|
118 |
|
|
cname<-paste("mod",1:length(mod_list),sep="") #change to more meaningful name?
|
119 |
|
|
names(mod_list)<-cname
|
120 |
|
|
#Adding layer LST to the raster stack
|
121 |
|
|
pos<-match("elev",names(s_raster))
|
122 |
|
|
layerNames(s_raster)[pos]<-"elev_1"
|
123 |
|
|
|
124 |
|
|
pos<-match("LST",names(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
|
125 |
|
|
s_raster<-dropLayer(s_raster,pos) # If it exists drop layer
|
126 |
|
|
LST<-subset(s_raster,LST_name)
|
127 |
|
|
names(LST)<-"LST"
|
128 |
|
|
#Screen for extreme values": this needs more thought, min and max val vary with regions
|
129 |
|
|
#min_val<-(-15+273.16) #if values less than -15C then screen out (note the Kelvin units that will need to be changed later in all datasets)
|
130 |
|
|
#r1[r1 < (min_val)]<-NA
|
131 |
|
|
s_raster<-addLayer(s_raster,LST) #Adding current month
|
132 |
|
|
|
133 |
|
|
#Now generate file names for the predictions...
|
134 |
|
|
list_out_filename<-vector("list",length(mod_list))
|
135 |
|
|
names(list_out_filename)<-cname
|
136 |
|
|
|
137 |
|
|
for (k in 1:length(list_out_filename)){
|
138 |
|
|
#j indicate which month is predicted
|
139 |
eec6f6d5
|
Benoit Parmentier
|
data_name<-paste(var,"_clim_month_",j,"_",cname[k],"_",prop_month,
|
140 |
57a4fede
|
Benoit Parmentier
|
"_",run_samp,sep="")
|
141 |
|
|
raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
142 |
|
|
list_out_filename[[k]]<-raster_name
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
#now predict values for raster image...
|
146 |
|
|
rast_clim_list<-predict_raster_model(mod_list,s_raster,list_out_filename)
|
147 |
|
|
names(rast_clim_list)<-cname
|
148 |
|
|
#Some modles will not be predicted...remove them
|
149 |
|
|
rast_clim_list<-rast_clim_list[!sapply(rast_clim_list,is.null)] #remove NULL elements in list
|
150 |
|
|
|
151 |
|
|
#Adding Kriging for Climatology options
|
152 |
|
|
|
153 |
|
|
clim_xy<-coordinates(data_month)
|
154 |
|
|
fitclim<-Krig(clim_xy,data_month$TMax,theta=1e5) #use TPS or krige
|
155 |
|
|
mod_krtmp1<-fitclim
|
156 |
|
|
model_name<-"mod_kr"
|
157 |
|
|
|
158 |
|
|
clim_rast<-interpolate(LST,fitclim) #interpolation using function from raster package
|
159 |
|
|
#Saving kriged surface in raster images
|
160 |
|
|
#data_name<-paste("clim_month_",j,"_",model_name,"_",prop_month,
|
161 |
|
|
# "_",run_samp,sep="")
|
162 |
|
|
#raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
163 |
|
|
#writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
164 |
|
|
|
165 |
|
|
#now climatology layer
|
166 |
|
|
#clim_rast<-LST-bias_rast
|
167 |
|
|
data_name<-paste("clim_month_",j,"_",model_name,"_",prop_month,
|
168 |
|
|
"_",run_samp,sep="")
|
169 |
|
|
raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
170 |
|
|
writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
171 |
|
|
|
172 |
|
|
#Adding to current objects
|
173 |
|
|
mod_list[[model_name]]<-mod_krtmp1
|
174 |
|
|
#rast_bias_list[[model_name]]<-raster_name_bias
|
175 |
|
|
rast_clim_list[[model_name]]<-raster_name_clim
|
176 |
|
|
|
177 |
|
|
#Prepare object to return
|
178 |
|
|
clim_obj<-list(rast_clim_list,data_month,mod_list,list_formulas)
|
179 |
|
|
names(clim_obj)<-c("clim","data_month","mod","formulas")
|
180 |
|
|
|
181 |
|
|
save(clim_obj,file= paste("clim_obj_month_",j,"_",out_prefix,".RData",sep=""))
|
182 |
|
|
|
183 |
|
|
return(clim_obj)
|
184 |
|
|
}
|
185 |
|
|
#
|
186 |
0163d0e2
|
Benoit Parmentier
|
|
187 |
5adb2c5c
|
Benoit Parmentier
|
runClim_KGFusion<-function(j,list_param){
|
188 |
a96491e0
|
Benoit Parmentier
|
|
189 |
0163d0e2
|
Benoit Parmentier
|
#Make this a function with multiple argument that can be used by mcmapply??
|
190 |
5adb2c5c
|
Benoit Parmentier
|
#Arguments:
|
191 |
|
|
#1)list_index: j
|
192 |
|
|
#2)covar_rast: covariates raster images used in the modeling
|
193 |
|
|
#3)covar_names: names of input variables
|
194 |
|
|
#4)lst_avg: list of LST climatogy names, may be removed later on
|
195 |
|
|
#5)list_models: list input models for bias calculation
|
196 |
|
|
#6)dst: data at the monthly time scale
|
197 |
|
|
#7)var: TMAX or TMIN, variable being interpolated
|
198 |
|
|
#8)y_var_name: output name, not used at this stage
|
199 |
|
|
#9)out_prefix
|
200 |
|
|
#
|
201 |
|
|
#The output is a list of four shapefile names produced by the function:
|
202 |
|
|
#1) clim: list of output names for raster climatogies
|
203 |
|
|
#2) data_month: monthly training data for bias surface modeling
|
204 |
|
|
#3) mod: list of model objects fitted
|
205 |
|
|
#4) formulas: list of formulas used in bias modeling
|
206 |
0163d0e2
|
Benoit Parmentier
|
|
207 |
5adb2c5c
|
Benoit Parmentier
|
### PARSING INPUT ARGUMENTS
|
208 |
|
|
#list_param_runGAMFusion<-list(i,clim_yearlist,sampling_obj,var,y_var_name, out_prefix)
|
209 |
0163d0e2
|
Benoit Parmentier
|
|
210 |
5adb2c5c
|
Benoit Parmentier
|
index<-list_param$j
|
211 |
|
|
s_raster<-list_param$covar_rast
|
212 |
|
|
covar_names<-list_param$covar_names
|
213 |
|
|
lst_avg<-list_param$lst_avg
|
214 |
|
|
list_models<-list_param$list_models
|
215 |
|
|
dst<-list_param$dst #monthly station dataset
|
216 |
|
|
var<-list_param$var
|
217 |
|
|
y_var_name<-list_param$y_var_name
|
218 |
|
|
out_prefix<-list_param$out_prefix
|
219 |
|
|
|
220 |
0163d0e2
|
Benoit Parmentier
|
#Model and response variable can be changed without affecting the script
|
221 |
|
|
prop_month<-0 #proportion retained for validation
|
222 |
5adb2c5c
|
Benoit Parmentier
|
run_samp<-1 #This option can be added later on if/when neeeded
|
223 |
0163d0e2
|
Benoit Parmentier
|
|
224 |
5adb2c5c
|
Benoit Parmentier
|
#### STEP 2: PREPARE DATA
|
225 |
a96491e0
|
Benoit Parmentier
|
|
226 |
0163d0e2
|
Benoit Parmentier
|
data_month<-dst[dst$month==j,] #Subsetting dataset for the relevant month of the date being processed
|
227 |
|
|
LST_name<-lst_avg[j] # name of LST month to be matched
|
228 |
|
|
data_month$LST<-data_month[[LST_name]]
|
229 |
|
|
|
230 |
|
|
#Adding layer LST to the raster stack
|
231 |
a96491e0
|
Benoit Parmentier
|
covar_rast<-s_raster
|
232 |
5adb2c5c
|
Benoit Parmentier
|
#names(s_raster)<-covar_names
|
233 |
0163d0e2
|
Benoit Parmentier
|
pos<-match("LST",names(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
|
234 |
|
|
s_raster<-dropLayer(s_raster,pos) # If it exists drop layer
|
235 |
|
|
LST<-subset(s_raster,LST_name)
|
236 |
|
|
names(LST)<-"LST"
|
237 |
|
|
s_raster<-addLayer(s_raster,LST) #Adding current month
|
238 |
|
|
|
239 |
a96491e0
|
Benoit Parmentier
|
#LST bias to model...
|
240 |
eec6f6d5
|
Benoit Parmentier
|
if (var==TMAX){
|
241 |
|
|
data_month$LSTD_bias<-data_month$LST-data_month$TMax
|
242 |
|
|
data_month$y_var<-data_month$LSTD_bias #Adding bias as the variable modeled
|
243 |
|
|
}
|
244 |
|
|
if (var==TMIN){
|
245 |
|
|
data_month$LSTD_bias<-data_month$LST-data_month$TMin
|
246 |
|
|
data_month$y_var<-data_month$LSTD_bias #Adding bias as the variable modeled
|
247 |
|
|
}
|
248 |
a96491e0
|
Benoit Parmentier
|
|
249 |
|
|
#### STEP3: NOW FIT AND PREDICT MODEL
|
250 |
|
|
|
251 |
|
|
list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
|
252 |
|
|
|
253 |
|
|
mod_list<-fit_models(list_formulas,data_month) #only gam at this stage
|
254 |
|
|
cname<-paste("mod",1:length(mod_list),sep="") #change to more meaningful name?
|
255 |
|
|
names(mod_list)<-cname
|
256 |
|
|
|
257 |
0163d0e2
|
Benoit Parmentier
|
#Now generate file names for the predictions...
|
258 |
3e1b1ed4
|
Benoit Parmentier
|
list_out_filename<-vector("list",length(mod_list))
|
259 |
|
|
names(list_out_filename)<-cname
|
260 |
|
|
|
261 |
0163d0e2
|
Benoit Parmentier
|
for (k in 1:length(list_out_filename)){
|
262 |
eec6f6d5
|
Benoit Parmentier
|
#j indicate which month is predicted, var indicates TMIN or TMAX
|
263 |
|
|
data_name<-paste(var,"_bias_LST_month_",j,"_",cname[k],"_",prop_month,
|
264 |
0163d0e2
|
Benoit Parmentier
|
"_",run_samp,sep="")
|
265 |
|
|
raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
266 |
|
|
list_out_filename[[k]]<-raster_name
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
#now predict values for raster image...
|
270 |
3e1b1ed4
|
Benoit Parmentier
|
rast_bias_list<-predict_raster_model(mod_list,s_raster,list_out_filename)
|
271 |
|
|
names(rast_bias_list)<-cname
|
272 |
|
|
#Some modles will not be predicted...remove them
|
273 |
|
|
rast_bias_list<-rast_bias_list[!sapply(rast_bias_list,is.null)] #remove NULL elements in list
|
274 |
|
|
|
275 |
|
|
mod_rast<-stack(rast_bias_list) #stack of bias raster images from models
|
276 |
0163d0e2
|
Benoit Parmentier
|
rast_clim_list<-vector("list",nlayers(mod_rast))
|
277 |
3e1b1ed4
|
Benoit Parmentier
|
names(rast_clim_list)<-names(rast_bias_list)
|
278 |
0163d0e2
|
Benoit Parmentier
|
for (k in 1:nlayers(mod_rast)){
|
279 |
|
|
clim_fus_rast<-LST-subset(mod_rast,k)
|
280 |
3e1b1ed4
|
Benoit Parmentier
|
data_name<-paste("clim_LST_month_",j,"_",names(rast_clim_list)[k],"_",prop_month,
|
281 |
0163d0e2
|
Benoit Parmentier
|
"_",run_samp,sep="")
|
282 |
|
|
raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
283 |
|
|
rast_clim_list[[k]]<-raster_name
|
284 |
|
|
writeRaster(clim_fus_rast, filename=raster_name,overwrite=TRUE) #Wri
|
285 |
|
|
}
|
286 |
3e1b1ed4
|
Benoit Parmentier
|
|
287 |
a96491e0
|
Benoit Parmentier
|
#### STEP 4:Adding Kriging for Climatology options
|
288 |
3e1b1ed4
|
Benoit Parmentier
|
|
289 |
|
|
bias_xy<-coordinates(data_month)
|
290 |
|
|
fitbias<-Krig(bias_xy,data_month$LSTD_bias,theta=1e5) #use TPS or krige
|
291 |
|
|
mod_krtmp1<-fitbias
|
292 |
|
|
model_name<-"mod_kr"
|
293 |
a96491e0
|
Benoit Parmentier
|
|
294 |
3e1b1ed4
|
Benoit Parmentier
|
|
295 |
|
|
bias_rast<-interpolate(LST,fitbias) #interpolation using function from raster package
|
296 |
|
|
#Saving kriged surface in raster images
|
297 |
|
|
data_name<-paste("bias_LST_month_",j,"_",model_name,"_",prop_month,
|
298 |
|
|
"_",run_samp,sep="")
|
299 |
|
|
raster_name_bias<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
300 |
|
|
writeRaster(bias_rast, filename=raster_name_bias,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
301 |
|
|
|
302 |
|
|
#now climatology layer
|
303 |
|
|
clim_rast<-LST-bias_rast
|
304 |
|
|
data_name<-paste("clim_LST_month_",j,"_",model_name,"_",prop_month,
|
305 |
|
|
"_",run_samp,sep="")
|
306 |
|
|
raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
307 |
|
|
writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
308 |
|
|
|
309 |
|
|
#Adding to current objects
|
310 |
|
|
mod_list[[model_name]]<-mod_krtmp1
|
311 |
|
|
rast_bias_list[[model_name]]<-raster_name_bias
|
312 |
|
|
rast_clim_list[[model_name]]<-raster_name_clim
|
313 |
|
|
|
314 |
a96491e0
|
Benoit Parmentier
|
#### STEP 5: Prepare object and return
|
315 |
|
|
|
316 |
3e1b1ed4
|
Benoit Parmentier
|
clim_obj<-list(rast_bias_list,rast_clim_list,data_month,mod_list,list_formulas)
|
317 |
0163d0e2
|
Benoit Parmentier
|
names(clim_obj)<-c("bias","clim","data_month","mod","formulas")
|
318 |
3e1b1ed4
|
Benoit Parmentier
|
|
319 |
|
|
save(clim_obj,file= paste("clim_obj_month_",j,"_",out_prefix,".RData",sep=""))
|
320 |
|
|
|
321 |
0163d0e2
|
Benoit Parmentier
|
return(clim_obj)
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
## Run function for kriging...?
|
325 |
|
|
|
326 |
a96491e0
|
Benoit Parmentier
|
#runGAMFusion <- function(i) { # loop over dates
|
327 |
|
|
runGAMFusion <- function(i,list_param) { # loop over dates
|
328 |
|
|
#### Change this to allow explicitly arguments...
|
329 |
3e1b1ed4
|
Benoit Parmentier
|
#Arguments:
|
330 |
5adb2c5c
|
Benoit Parmentier
|
#1)index: loop list index for individual run/fit
|
331 |
|
|
#2)clim_year_list: list of climatology files for all models...(12*nb of models)
|
332 |
|
|
#3)sampling_obj: contains, data per date/fit, sampling information
|
333 |
|
|
#4)dst: data at the monthly time scale
|
334 |
|
|
#5)var: variable predicted -TMAX or TMIN
|
335 |
|
|
#6)y_var_name: name of the variable predicted - dailyTMax, dailyTMin
|
336 |
|
|
#7)out_prefix
|
337 |
|
|
#
|
338 |
|
|
#The output is a list of four shapefile names produced by the function:
|
339 |
|
|
#1) list_temp: y_var_name
|
340 |
|
|
#2) rast_clim_list: list of files for temperature climatology predictions
|
341 |
|
|
#3) delta: list of files for temperature delta predictions
|
342 |
|
|
#4) data_s: training data
|
343 |
|
|
#5) data_v: testing data
|
344 |
|
|
#6) sampling_dat: sampling information for the current prediction (date,proportion of holdout and sample number)
|
345 |
|
|
#7) mod_kr: kriging delta fit, field package model object
|
346 |
a96491e0
|
Benoit Parmentier
|
|
347 |
|
|
### PARSING INPUT ARGUMENTS
|
348 |
|
|
|
349 |
|
|
#list_param_runGAMFusion<-list(i,clim_yearlist,sampling_obj,var,y_var_name, out_prefix)
|
350 |
|
|
rast_clim_yearlist<-list_param$clim_yearlist
|
351 |
|
|
sampling_obj<-list_param$sampling_obj
|
352 |
|
|
ghcn.subsets<-sampling_obj$ghcn_data_day
|
353 |
|
|
sampling_dat <- sampling_obj$sampling_dat
|
354 |
|
|
sampling <- sampling_obj$sampling_index
|
355 |
|
|
var<-list_param$var
|
356 |
|
|
y_var_name<-list_param$y_var_name
|
357 |
|
|
out_prefix<-list_param$out_prefix
|
358 |
|
|
dst<-list_param$dst #monthly station dataset
|
359 |
4719fdd7
|
Benoit Parmentier
|
|
360 |
a96491e0
|
Benoit Parmentier
|
##########
|
361 |
|
|
# STEP 1 - interpolate delta across space
|
362 |
|
|
############# Read in information and get traiing and testing stations
|
363 |
0163d0e2
|
Benoit Parmentier
|
|
364 |
4719fdd7
|
Benoit Parmentier
|
date<-strptime(sampling_dat$date[i], "%Y%m%d") # interpolation date being processed
|
365 |
|
|
month<-strftime(date, "%m") # current month of the date being processed
|
366 |
|
|
LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
|
367 |
a96491e0
|
Benoit Parmentier
|
proj_str<-proj4string(dst) #get the local projection information from monthly data
|
368 |
d93bea39
|
Benoit Parmentier
|
|
369 |
4719fdd7
|
Benoit Parmentier
|
###Regression part 1: Creating a validation dataset by creating training and testing datasets
|
370 |
0f4426cb
|
Benoit Parmentier
|
data_day<-ghcn.subsets[[i]]
|
371 |
|
|
mod_LST <- ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))] #Match interpolation date and monthly LST average
|
372 |
|
|
data_day$LST <- as.data.frame(mod_LST)[,1] #Add the variable LST to the dataset
|
373 |
|
|
dst$LST<-dst[[LST_month]] #Add the variable LST to the monthly dataset
|
374 |
|
|
|
375 |
4719fdd7
|
Benoit Parmentier
|
ind.training<-sampling[[i]]
|
376 |
0f4426cb
|
Benoit Parmentier
|
ind.testing <- setdiff(1:nrow(data_day), ind.training)
|
377 |
|
|
data_s <- data_day[ind.training, ] #Training dataset currently used in the modeling
|
378 |
|
|
data_v <- data_day[ind.testing, ] #Testing/validation dataset using input sampling
|
379 |
4719fdd7
|
Benoit Parmentier
|
|
380 |
|
|
ns<-nrow(data_s)
|
381 |
|
|
nv<-nrow(data_v)
|
382 |
|
|
#i=1
|
383 |
|
|
date_proc<-sampling_dat$date[i]
|
384 |
|
|
date_proc<-strptime(sampling_dat$date[i], "%Y%m%d") # interpolation date being processed
|
385 |
|
|
mo<-as.integer(strftime(date_proc, "%m")) # current month of the date being processed
|
386 |
|
|
day<-as.integer(strftime(date_proc, "%d"))
|
387 |
|
|
year<-as.integer(strftime(date_proc, "%Y"))
|
388 |
|
|
|
389 |
a96491e0
|
Benoit Parmentier
|
##########
|
390 |
|
|
# STEP 2 - JOIN DAILY AND MONTHLY STATION INFORMATION
|
391 |
|
|
##########
|
392 |
|
|
|
393 |
55056785
|
Benoit Parmentier
|
modst<-dst[dst$month==mo,] #Subsetting dataset for the relevant month of the date being processed
|
394 |
eec6f6d5
|
Benoit Parmentier
|
|
395 |
|
|
if (var=="TMIN"){
|
396 |
|
|
modst$LSTD_bias <- modst$LST-modst$TMin; #That is the difference between the monthly LST mean and monthly station mean
|
397 |
|
|
}
|
398 |
|
|
if (var=="TMAX"){
|
399 |
|
|
modst$LSTD_bias <- modst$LST-modst$TMax; #That is the difference between the monthly LST mean and monthly station mean
|
400 |
|
|
}
|
401 |
|
|
#This may be unnecessary since LSTD_bias is already in dst?? check the info
|
402 |
a96491e0
|
Benoit Parmentier
|
|
403 |
|
|
#Clearn out this part: make this a function call
|
404 |
0f4426cb
|
Benoit Parmentier
|
x<-as.data.frame(data_v)
|
405 |
|
|
d<-as.data.frame(data_s)
|
406 |
|
|
#x[x$value==-999.9]<-NA
|
407 |
|
|
for (j in 1:nrow(x)){
|
408 |
|
|
if (x$value[j]== -999.9){
|
409 |
|
|
x$value[j]<-NA
|
410 |
|
|
}
|
411 |
|
|
}
|
412 |
|
|
for (j in 1:nrow(d)){
|
413 |
|
|
if (d$value[j]== -999.9){
|
414 |
|
|
d$value[j]<-NA
|
415 |
|
|
}
|
416 |
|
|
}
|
417 |
|
|
#x[x$value==-999.9]<-NA
|
418 |
|
|
#d[d$value==-999.9]<-NA
|
419 |
4719fdd7
|
Benoit Parmentier
|
pos<-match("value",names(d)) #Find column with name "value"
|
420 |
|
|
#names(d)[pos]<-c("dailyTmax")
|
421 |
|
|
names(d)[pos]<-y_var_name
|
422 |
|
|
names(x)[pos]<-y_var_name
|
423 |
|
|
#names(x)[pos]<-c("dailyTmax")
|
424 |
|
|
pos<-match("station",names(d)) #Find column with name "value"
|
425 |
|
|
names(d)[pos]<-c("id")
|
426 |
|
|
names(x)[pos]<-c("id")
|
427 |
|
|
names(modst)[1]<-c("id") #modst contains the average tmax per month for every stations...
|
428 |
55056785
|
Benoit Parmentier
|
|
429 |
5588b17c
|
Benoit Parmentier
|
dmoday <-merge(modst,d,by="id",suffixes=c("",".y2"))
|
430 |
|
|
xmoday <-merge(modst,x,by="id",suffixes=c("",".y2"))
|
431 |
55056785
|
Benoit Parmentier
|
mod_pat<-glob2rx("*.y2")
|
432 |
|
|
var_pat<-grep(mod_pat,names(dmoday),value=FALSE) # using grep with "value" extracts the matching names
|
433 |
|
|
dmoday<-dmoday[,-var_pat]
|
434 |
|
|
mod_pat<-glob2rx("*.y2")
|
435 |
|
|
var_pat<-grep(mod_pat,names(xmoday),value=FALSE) # using grep with "value" extracts the matching names
|
436 |
|
|
xmoday<-xmoday[,-var_pat] #Removing duplicate columns
|
437 |
4719fdd7
|
Benoit Parmentier
|
|
438 |
|
|
data_v<-xmoday
|
439 |
|
|
|
440 |
|
|
#dmoday contains the daily tmax values for training with TMax being the monthly station tmax mean
|
441 |
|
|
#xmoday contains the daily tmax values for validation with TMax being the monthly station tmax mean
|
442 |
|
|
|
443 |
|
|
##########
|
444 |
a96491e0
|
Benoit Parmentier
|
# STEP 3 - interpolate daily delta across space
|
445 |
4719fdd7
|
Benoit Parmentier
|
##########
|
446 |
|
|
|
447 |
a96491e0
|
Benoit Parmentier
|
#Change to take into account TMin and TMax
|
448 |
3e1b1ed4
|
Benoit Parmentier
|
daily_delta<-dmoday$dailyTmax-dmoday$TMax
|
449 |
|
|
daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
|
450 |
|
|
fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
|
451 |
d93bea39
|
Benoit Parmentier
|
mod_krtmp2<-fitdelta
|
452 |
|
|
model_name<-paste("mod_kr","day",sep="_")
|
453 |
4719fdd7
|
Benoit Parmentier
|
data_s<-dmoday #put the
|
454 |
|
|
data_s$daily_delta<-daily_delta
|
455 |
|
|
|
456 |
|
|
#########
|
457 |
a96491e0
|
Benoit Parmentier
|
# STEP 4 - Calculate daily predictions - T(day) = clim(month) + delta(day)
|
458 |
4719fdd7
|
Benoit Parmentier
|
#########
|
459 |
|
|
|
460 |
3e1b1ed4
|
Benoit Parmentier
|
rast_clim_list<-rast_clim_yearlist[[mo]] #select relevant month
|
461 |
|
|
rast_clim_month<-raster(rast_clim_list[[1]])
|
462 |
4719fdd7
|
Benoit Parmentier
|
|
463 |
3e1b1ed4
|
Benoit Parmentier
|
daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
|
464 |
4719fdd7
|
Benoit Parmentier
|
|
465 |
|
|
#Saving kriged surface in raster images
|
466 |
|
|
data_name<-paste("daily_delta_",sampling_dat$date[i],"_",sampling_dat$prop[i],
|
467 |
|
|
"_",sampling_dat$run_samp[i],sep="")
|
468 |
3e1b1ed4
|
Benoit Parmentier
|
raster_name_delta<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
469 |
|
|
writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
470 |
|
|
|
471 |
|
|
#Now predict daily after having selected the relevant month
|
472 |
|
|
temp_list<-vector("list",length(rast_clim_list))
|
473 |
|
|
for (j in 1:length(rast_clim_list)){
|
474 |
|
|
rast_clim_month<-raster(rast_clim_list[[j]])
|
475 |
|
|
temp_predicted<-rast_clim_month+daily_delta_rast
|
476 |
4719fdd7
|
Benoit Parmentier
|
|
477 |
3e1b1ed4
|
Benoit Parmentier
|
data_name<-paste(y_var_name,"_predicted_",names(rast_clim_list)[j],"_",
|
478 |
|
|
sampling_dat$date[i],"_",sampling_dat$prop[i],
|
479 |
|
|
"_",sampling_dat$run_samp[i],sep="")
|
480 |
|
|
raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
481 |
|
|
writeRaster(temp_predicted, filename=raster_name,overwrite=TRUE)
|
482 |
|
|
temp_list[[j]]<-raster_name
|
483 |
4719fdd7
|
Benoit Parmentier
|
}
|
484 |
|
|
|
485 |
a96491e0
|
Benoit Parmentier
|
##########
|
486 |
|
|
# STEP 5 - Prepare output object to return
|
487 |
|
|
##########
|
488 |
|
|
|
489 |
3e1b1ed4
|
Benoit Parmentier
|
mod_krtmp2<-fitdelta
|
490 |
|
|
model_name<-paste("mod_kr","day",sep="_")
|
491 |
5588b17c
|
Benoit Parmentier
|
names(temp_list)<-names(rast_clim_list)
|
492 |
3e1b1ed4
|
Benoit Parmentier
|
coordinates(data_s)<-cbind(data_s$x,data_s$y)
|
493 |
|
|
proj4string(data_s)<-proj_str
|
494 |
|
|
coordinates(data_v)<-cbind(data_v$x,data_v$y)
|
495 |
|
|
proj4string(data_v)<-proj_str
|
496 |
|
|
|
497 |
|
|
delta_obj<-list(temp_list,rast_clim_list,raster_name_delta,data_s,
|
498 |
|
|
data_v,sampling_dat[i,],mod_krtmp2)
|
499 |
|
|
|
500 |
|
|
obj_names<-c(y_var_name,"clim","delta","data_s","data_v",
|
501 |
|
|
"sampling_dat",model_name)
|
502 |
|
|
names(delta_obj)<-obj_names
|
503 |
|
|
save(delta_obj,file= paste("delta_obj_",sampling_dat$date[i],"_",sampling_dat$prop[i],
|
504 |
4719fdd7
|
Benoit Parmentier
|
"_",sampling_dat$run_samp[i],out_prefix,".RData",sep=""))
|
505 |
3e1b1ed4
|
Benoit Parmentier
|
return(delta_obj)
|
506 |
|
|
|
507 |
|
|
}
|
508 |
|
|
|