1
|
################## 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
|
|
21
|
|
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
|
raster_pred<- predict(object=r_stack,model=mod,na.rm=FALSE) #Using the coeff to predict new values.
|
32
|
names(raster_pred)<-"y_pred"
|
33
|
writeRaster(raster_pred, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
34
|
#print(paste("Interpolation:","mod", j ,sep=" "))
|
35
|
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
|
####
|
62
|
#TODO:
|
63
|
#Add log file and calculate time and sizes for processes-outputs
|
64
|
runClimCAI<-function(j,list_param){
|
65
|
|
66
|
#Make this a function with multiple argument that can be used by mcmapply??
|
67
|
#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
|
#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
|
#### STEP 2: PREPARE DATA
|
102
|
|
103
|
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
|
list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
|
108
|
|
109
|
#TMax to model...
|
110
|
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
|
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
|
data_name<-paste(var,"_clim_month_",j,"_",cname[k],"_",prop_month,
|
140
|
"_",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$y_var,theta=1e5) #use TPS or krige
|
155
|
#fitclim<-Krig(clim_xy,data_month$TMax,theta=1e5) #use TPS or krige
|
156
|
mod_krtmp1<-fitclim
|
157
|
model_name<-"mod_kr"
|
158
|
|
159
|
clim_rast<-interpolate(LST,fitclim) #interpolation using function from raster package
|
160
|
#Saving kriged surface in raster images
|
161
|
#data_name<-paste("clim_month_",j,"_",model_name,"_",prop_month,
|
162
|
# "_",run_samp,sep="")
|
163
|
#raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
164
|
#writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
165
|
|
166
|
#now climatology layer
|
167
|
#clim_rast<-LST-bias_rast
|
168
|
data_name<-paste(var,"_clim_month_",j,"_",model_name,"_",prop_month,
|
169
|
"_",run_samp,sep="")
|
170
|
raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
171
|
writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
172
|
|
173
|
#Adding to current objects
|
174
|
mod_list[[model_name]]<-mod_krtmp1
|
175
|
#rast_bias_list[[model_name]]<-raster_name_bias
|
176
|
rast_clim_list[[model_name]]<-raster_name_clim
|
177
|
|
178
|
#Prepare object to return
|
179
|
clim_obj<-list(rast_clim_list,data_month,mod_list,list_formulas)
|
180
|
names(clim_obj)<-c("clim","data_month","mod","formulas")
|
181
|
|
182
|
save(clim_obj,file= paste("clim_obj_month_",j,"_",out_prefix,".RData",sep=""))
|
183
|
|
184
|
return(clim_obj)
|
185
|
}
|
186
|
#
|
187
|
|
188
|
runClim_KGFusion<-function(j,list_param){
|
189
|
|
190
|
#Make this a function with multiple argument that can be used by mcmapply??
|
191
|
#Arguments:
|
192
|
#1)list_index: j
|
193
|
#2)covar_rast: covariates raster images used in the modeling
|
194
|
#3)covar_names: names of input variables
|
195
|
#4)lst_avg: list of LST climatogy names, may be removed later on
|
196
|
#5)list_models: list input models for bias calculation
|
197
|
#6)dst: data at the monthly time scale
|
198
|
#7)var: TMAX or TMIN, variable being interpolated
|
199
|
#8)y_var_name: output name, not used at this stage
|
200
|
#9)out_prefix
|
201
|
#
|
202
|
#The output is a list of four shapefile names produced by the function:
|
203
|
#1) clim: list of output names for raster climatogies
|
204
|
#2) data_month: monthly training data for bias surface modeling
|
205
|
#3) mod: list of model objects fitted
|
206
|
#4) formulas: list of formulas used in bias modeling
|
207
|
|
208
|
### PARSING INPUT ARGUMENTS
|
209
|
#list_param_runGAMFusion<-list(i,clim_yearlist,sampling_obj,var,y_var_name, out_prefix)
|
210
|
|
211
|
index<-list_param$j
|
212
|
s_raster<-list_param$covar_rast
|
213
|
covar_names<-list_param$covar_names
|
214
|
lst_avg<-list_param$lst_avg
|
215
|
list_models<-list_param$list_models
|
216
|
dst<-list_param$dst #monthly station dataset
|
217
|
var<-list_param$var
|
218
|
y_var_name<-list_param$y_var_name
|
219
|
out_prefix<-list_param$out_prefix
|
220
|
|
221
|
#Model and response variable can be changed without affecting the script
|
222
|
prop_month<-0 #proportion retained for validation
|
223
|
run_samp<-1 #This option can be added later on if/when neeeded
|
224
|
|
225
|
#### STEP 2: PREPARE DATA
|
226
|
|
227
|
data_month<-dst[dst$month==j,] #Subsetting dataset for the relevant month of the date being processed
|
228
|
LST_name<-lst_avg[j] # name of LST month to be matched
|
229
|
data_month$LST<-data_month[[LST_name]]
|
230
|
|
231
|
#Adding layer LST to the raster stack
|
232
|
covar_rast<-s_raster
|
233
|
#names(s_raster)<-covar_names
|
234
|
pos<-match("LST",names(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
|
235
|
s_raster<-dropLayer(s_raster,pos) # If it exists drop layer
|
236
|
LST<-subset(s_raster,LST_name)
|
237
|
names(LST)<-"LST"
|
238
|
s_raster<-addLayer(s_raster,LST) #Adding current month
|
239
|
|
240
|
#LST bias to model...
|
241
|
if (var=="TMAX"){
|
242
|
data_month$LSTD_bias<-data_month$LST-data_month$TMax
|
243
|
data_month$y_var<-data_month$LSTD_bias #Adding bias as the variable modeled
|
244
|
}
|
245
|
if (var=="TMIN"){
|
246
|
data_month$LSTD_bias<-data_month$LST-data_month$TMin
|
247
|
data_month$y_var<-data_month$LSTD_bias #Adding bias as the variable modeled
|
248
|
}
|
249
|
|
250
|
#### STEP3: NOW FIT AND PREDICT MODEL
|
251
|
|
252
|
list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
|
253
|
|
254
|
mod_list<-fit_models(list_formulas,data_month) #only gam at this stage
|
255
|
cname<-paste("mod",1:length(mod_list),sep="") #change to more meaningful name?
|
256
|
names(mod_list)<-cname
|
257
|
|
258
|
#Now generate file names for the predictions...
|
259
|
list_out_filename<-vector("list",length(mod_list))
|
260
|
names(list_out_filename)<-cname
|
261
|
|
262
|
for (k in 1:length(list_out_filename)){
|
263
|
#j indicate which month is predicted, var indicates TMIN or TMAX
|
264
|
data_name<-paste(var,"_bias_LST_month_",j,"_",cname[k],"_",prop_month,
|
265
|
"_",run_samp,sep="")
|
266
|
raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
267
|
list_out_filename[[k]]<-raster_name
|
268
|
}
|
269
|
|
270
|
#now predict values for raster image...
|
271
|
rast_bias_list<-predict_raster_model(mod_list,s_raster,list_out_filename)
|
272
|
names(rast_bias_list)<-cname
|
273
|
#Some modles will not be predicted...remove them
|
274
|
rast_bias_list<-rast_bias_list[!sapply(rast_bias_list,is.null)] #remove NULL elements in list
|
275
|
|
276
|
mod_rast<-stack(rast_bias_list) #stack of bias raster images from models
|
277
|
rast_clim_list<-vector("list",nlayers(mod_rast))
|
278
|
names(rast_clim_list)<-names(rast_bias_list)
|
279
|
for (k in 1:nlayers(mod_rast)){
|
280
|
clim_fus_rast<-LST-subset(mod_rast,k)
|
281
|
data_name<-paste("clim_LST_month_",j,"_",names(rast_clim_list)[k],"_",prop_month,
|
282
|
"_",run_samp,sep="")
|
283
|
raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
284
|
rast_clim_list[[k]]<-raster_name
|
285
|
writeRaster(clim_fus_rast, filename=raster_name,overwrite=TRUE) #Wri
|
286
|
}
|
287
|
|
288
|
#### STEP 4:Adding Kriging for Climatology options
|
289
|
|
290
|
bias_xy<-coordinates(data_month)
|
291
|
fitbias<-Krig(bias_xy,data_month$LSTD_bias,theta=1e5) #use TPS or krige
|
292
|
mod_krtmp1<-fitbias
|
293
|
model_name<-"mod_kr"
|
294
|
|
295
|
|
296
|
bias_rast<-interpolate(LST,fitbias) #interpolation using function from raster package
|
297
|
#Saving kriged surface in raster images
|
298
|
data_name<-paste("bias_LST_month_",j,"_",model_name,"_",prop_month,
|
299
|
"_",run_samp,sep="")
|
300
|
raster_name_bias<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
301
|
writeRaster(bias_rast, filename=raster_name_bias,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
302
|
|
303
|
#now climatology layer
|
304
|
clim_rast<-LST-bias_rast
|
305
|
data_name<-paste("clim_LST_month_",j,"_",model_name,"_",prop_month,
|
306
|
"_",run_samp,sep="")
|
307
|
raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
308
|
writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
309
|
|
310
|
#Adding to current objects
|
311
|
mod_list[[model_name]]<-mod_krtmp1
|
312
|
rast_bias_list[[model_name]]<-raster_name_bias
|
313
|
rast_clim_list[[model_name]]<-raster_name_clim
|
314
|
|
315
|
#### STEP 5: Prepare object and return
|
316
|
|
317
|
clim_obj<-list(rast_bias_list,rast_clim_list,data_month,mod_list,list_formulas)
|
318
|
names(clim_obj)<-c("bias","clim","data_month","mod","formulas")
|
319
|
|
320
|
save(clim_obj,file= paste("clim_obj_month_",j,"_",out_prefix,".RData",sep=""))
|
321
|
|
322
|
return(clim_obj)
|
323
|
}
|
324
|
|
325
|
## Run function for kriging...?
|
326
|
|
327
|
#runGAMFusion <- function(i) { # loop over dates
|
328
|
runGAMFusion <- function(i,list_param) { # loop over dates
|
329
|
#### Change this to allow explicitly arguments...
|
330
|
#Arguments:
|
331
|
#1)index: loop list index for individual run/fit
|
332
|
#2)clim_year_list: list of climatology files for all models...(12*nb of models)
|
333
|
#3)sampling_obj: contains, data per date/fit, sampling information
|
334
|
#4)dst: data at the monthly time scale
|
335
|
#5)var: variable predicted -TMAX or TMIN
|
336
|
#6)y_var_name: name of the variable predicted - dailyTMax, dailyTMin
|
337
|
#7)out_prefix
|
338
|
#
|
339
|
#The output is a list of four shapefile names produced by the function:
|
340
|
#1) list_temp: y_var_name
|
341
|
#2) rast_clim_list: list of files for temperature climatology predictions
|
342
|
#3) delta: list of files for temperature delta predictions
|
343
|
#4) data_s: training data
|
344
|
#5) data_v: testing data
|
345
|
#6) sampling_dat: sampling information for the current prediction (date,proportion of holdout and sample number)
|
346
|
#7) mod_kr: kriging delta fit, field package model object
|
347
|
|
348
|
### PARSING INPUT ARGUMENTS
|
349
|
|
350
|
#list_param_runGAMFusion<-list(i,clim_yearlist,sampling_obj,var,y_var_name, out_prefix)
|
351
|
rast_clim_yearlist<-list_param$clim_yearlist
|
352
|
sampling_obj<-list_param$sampling_obj
|
353
|
ghcn.subsets<-sampling_obj$ghcn_data_day
|
354
|
sampling_dat <- sampling_obj$sampling_dat
|
355
|
sampling <- sampling_obj$sampling_index
|
356
|
var<-list_param$var
|
357
|
y_var_name<-list_param$y_var_name
|
358
|
out_prefix<-list_param$out_prefix
|
359
|
dst<-list_param$dst #monthly station dataset
|
360
|
|
361
|
##########
|
362
|
# STEP 1 - interpolate delta across space
|
363
|
############# Read in information and get traiing and testing stations
|
364
|
|
365
|
date<-strptime(sampling_dat$date[i], "%Y%m%d") # interpolation date being processed
|
366
|
month<-strftime(date, "%m") # current month of the date being processed
|
367
|
LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
|
368
|
proj_str<-proj4string(dst) #get the local projection information from monthly data
|
369
|
|
370
|
###Regression part 1: Creating a validation dataset by creating training and testing datasets
|
371
|
data_day<-ghcn.subsets[[i]]
|
372
|
mod_LST <- ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))] #Match interpolation date and monthly LST average
|
373
|
data_day$LST <- as.data.frame(mod_LST)[,1] #Add the variable LST to the dataset
|
374
|
dst$LST<-dst[[LST_month]] #Add the variable LST to the monthly dataset
|
375
|
|
376
|
ind.training<-sampling[[i]]
|
377
|
ind.testing <- setdiff(1:nrow(data_day), ind.training)
|
378
|
data_s <- data_day[ind.training, ] #Training dataset currently used in the modeling
|
379
|
data_v <- data_day[ind.testing, ] #Testing/validation dataset using input sampling
|
380
|
|
381
|
ns<-nrow(data_s)
|
382
|
nv<-nrow(data_v)
|
383
|
#i=1
|
384
|
date_proc<-sampling_dat$date[i]
|
385
|
date_proc<-strptime(sampling_dat$date[i], "%Y%m%d") # interpolation date being processed
|
386
|
mo<-as.integer(strftime(date_proc, "%m")) # current month of the date being processed
|
387
|
day<-as.integer(strftime(date_proc, "%d"))
|
388
|
year<-as.integer(strftime(date_proc, "%Y"))
|
389
|
|
390
|
##########
|
391
|
# STEP 2 - JOIN DAILY AND MONTHLY STATION INFORMATION
|
392
|
##########
|
393
|
|
394
|
modst<-dst[dst$month==mo,] #Subsetting dataset for the relevant month of the date being processed
|
395
|
|
396
|
if (var=="TMIN"){
|
397
|
modst$LSTD_bias <- modst$LST-modst$TMin; #That is the difference between the monthly LST mean and monthly station mean
|
398
|
}
|
399
|
if (var=="TMAX"){
|
400
|
modst$LSTD_bias <- modst$LST-modst$TMax; #That is the difference between the monthly LST mean and monthly station mean
|
401
|
}
|
402
|
#This may be unnecessary since LSTD_bias is already in dst?? check the info
|
403
|
|
404
|
#Clearn out this part: make this a function call
|
405
|
x<-as.data.frame(data_v)
|
406
|
d<-as.data.frame(data_s)
|
407
|
#x[x$value==-999.9]<-NA
|
408
|
for (j in 1:nrow(x)){
|
409
|
if (x$value[j]== -999.9){
|
410
|
x$value[j]<-NA
|
411
|
}
|
412
|
}
|
413
|
for (j in 1:nrow(d)){
|
414
|
if (d$value[j]== -999.9){
|
415
|
d$value[j]<-NA
|
416
|
}
|
417
|
}
|
418
|
#x[x$value==-999.9]<-NA
|
419
|
#d[d$value==-999.9]<-NA
|
420
|
pos<-match("value",names(d)) #Find column with name "value"
|
421
|
#names(d)[pos]<-c("dailyTmax")
|
422
|
names(d)[pos]<-y_var_name
|
423
|
names(x)[pos]<-y_var_name
|
424
|
#names(x)[pos]<-c("dailyTmax")
|
425
|
pos<-match("station",names(d)) #Find column with name "value"
|
426
|
names(d)[pos]<-c("id")
|
427
|
names(x)[pos]<-c("id")
|
428
|
names(modst)[1]<-c("id") #modst contains the average tmax per month for every stations...
|
429
|
|
430
|
dmoday <-merge(modst,d,by="id",suffixes=c("",".y2"))
|
431
|
xmoday <-merge(modst,x,by="id",suffixes=c("",".y2"))
|
432
|
mod_pat<-glob2rx("*.y2")
|
433
|
var_pat<-grep(mod_pat,names(dmoday),value=FALSE) # using grep with "value" extracts the matching names
|
434
|
dmoday<-dmoday[,-var_pat]
|
435
|
mod_pat<-glob2rx("*.y2")
|
436
|
var_pat<-grep(mod_pat,names(xmoday),value=FALSE) # using grep with "value" extracts the matching names
|
437
|
xmoday<-xmoday[,-var_pat] #Removing duplicate columns
|
438
|
|
439
|
data_v<-xmoday
|
440
|
|
441
|
#dmoday contains the daily tmax values for training with TMax being the monthly station tmax mean
|
442
|
#xmoday contains the daily tmax values for validation with TMax being the monthly station tmax mean
|
443
|
|
444
|
##########
|
445
|
# STEP 3 - interpolate daily delta across space
|
446
|
##########
|
447
|
|
448
|
#Change to take into account TMin and TMax
|
449
|
if (var=="TMIN"){
|
450
|
daily_delta<-dmoday$dailyTmin-dmoday$TMin
|
451
|
}
|
452
|
if (var=="TMAX"){
|
453
|
daily_delta<-dmoday$dailyTmax-dmoday$TMax
|
454
|
}
|
455
|
|
456
|
daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
|
457
|
fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
|
458
|
mod_krtmp2<-fitdelta
|
459
|
model_name<-paste("mod_kr","day",sep="_")
|
460
|
data_s<-dmoday #put the
|
461
|
data_s$daily_delta<-daily_delta
|
462
|
|
463
|
#########
|
464
|
# STEP 4 - Calculate daily predictions - T(day) = clim(month) + delta(day)
|
465
|
#########
|
466
|
|
467
|
rast_clim_list<-rast_clim_yearlist[[mo]] #select relevant month
|
468
|
rast_clim_month<-raster(rast_clim_list[[1]])
|
469
|
|
470
|
daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
|
471
|
|
472
|
#Saving kriged surface in raster images
|
473
|
data_name<-paste("daily_delta_",sampling_dat$date[i],"_",sampling_dat$prop[i],
|
474
|
"_",sampling_dat$run_samp[i],sep="")
|
475
|
raster_name_delta<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
476
|
writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI)
|
477
|
|
478
|
#Now predict daily after having selected the relevant month
|
479
|
temp_list<-vector("list",length(rast_clim_list))
|
480
|
for (j in 1:length(rast_clim_list)){
|
481
|
rast_clim_month<-raster(rast_clim_list[[j]])
|
482
|
temp_predicted<-rast_clim_month+daily_delta_rast
|
483
|
|
484
|
data_name<-paste(y_var_name,"_predicted_",names(rast_clim_list)[j],"_",
|
485
|
sampling_dat$date[i],"_",sampling_dat$prop[i],
|
486
|
"_",sampling_dat$run_samp[i],sep="")
|
487
|
raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
|
488
|
writeRaster(temp_predicted, filename=raster_name,overwrite=TRUE)
|
489
|
temp_list[[j]]<-raster_name
|
490
|
}
|
491
|
|
492
|
##########
|
493
|
# STEP 5 - Prepare output object to return
|
494
|
##########
|
495
|
|
496
|
mod_krtmp2<-fitdelta
|
497
|
model_name<-paste("mod_kr","day",sep="_")
|
498
|
names(temp_list)<-names(rast_clim_list)
|
499
|
coordinates(data_s)<-cbind(data_s$x,data_s$y)
|
500
|
proj4string(data_s)<-proj_str
|
501
|
coordinates(data_v)<-cbind(data_v$x,data_v$y)
|
502
|
proj4string(data_v)<-proj_str
|
503
|
|
504
|
delta_obj<-list(temp_list,rast_clim_list,raster_name_delta,data_s,
|
505
|
data_v,sampling_dat[i,],mod_krtmp2)
|
506
|
|
507
|
obj_names<-c(y_var_name,"clim","delta","data_s","data_v",
|
508
|
"sampling_dat",model_name)
|
509
|
names(delta_obj)<-obj_names
|
510
|
save(delta_obj,file= paste("delta_obj_",sampling_dat$date[i],"_",sampling_dat$prop[i],
|
511
|
"_",sampling_dat$run_samp[i],out_prefix,".RData",sep=""))
|
512
|
return(delta_obj)
|
513
|
|
514
|
}
|
515
|
|