1
|
################## Interpolation of Tmax Using GWR ########################################
|
2
|
########################### GWR WITH LST #######################################################
|
3
|
#This script interpolates station values for the Oregon case study using Geographically Weighted. #
|
4
|
#The script uses LST monthly averages as input variables and loads the station data #
|
5
|
#from a shape file with projection information. #
|
6
|
#Note that this program: #
|
7
|
#1)assumes that the shape file is in the current working folder #
|
8
|
#2)relevant variables were extracted from raster images before performing the regressions #
|
9
|
# and stored shapefile #
|
10
|
#3)covariate raster images are present in the current working folder #
|
11
|
#This scripts predicts tmax using autokrige, gstat and LST derived from MOD11A1. #
|
12
|
#also included and assessed using the RMSE,MAE,ME and R2 from validation dataset. #
|
13
|
#TThe dates must be provided as a textfile. #
|
14
|
#AUTHOR: Benoit Parmentier #
|
15
|
#DATE: 08/15/2012 #
|
16
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#364-- #
|
17
|
###################################################################################################
|
18
|
|
19
|
###Loading R library and packages
|
20
|
#library(gtools) # loading some useful tools
|
21
|
library(mgcv) # GAM package by Wood 2006 (version 2012)
|
22
|
library(sp) # Spatial pacakge with class definition by Bivand et al. 2008
|
23
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. 2012
|
24
|
library(rgdal) # GDAL wrapper for R, spatial utilities (Keitt et al. 2012)
|
25
|
library(gstat) # Kriging and co-kriging by Pebesma et al. 2004
|
26
|
library(automap) # Automated Kriging based on gstat module by Hiemstra et al. 2008
|
27
|
library(spgwr)
|
28
|
library(maptools)
|
29
|
library(graphics)
|
30
|
library(parallel) # Urbanek S. and Ripley B., package for multi cores & parallel processing
|
31
|
library(raster)
|
32
|
library(rasterVis)
|
33
|
library(fields) # May be used later...
|
34
|
|
35
|
### Parameters and argument
|
36
|
|
37
|
infile1<- "ghcn_or_tmax_covariates_06262012_OR83M.shp" #GHCN shapefile containing variables for modeling 2010
|
38
|
#infile2<-"list_10_dates_04212012.txt" #List of 10 dates for the regression
|
39
|
#infile2<-"list_2_dates_04212012.txt"
|
40
|
infile2<-"list_365_dates_04212012.txt"
|
41
|
infile3<-"LST_dates_var_names.txt" #LST dates name
|
42
|
infile4<-"models_interpolation_05142012.txt" #Interpolation model names
|
43
|
infile5<-"mean_day244_rescaled.rst" #Raster or grid for the locations of predictions
|
44
|
#infile6<-"lst_climatology.txt"
|
45
|
infile6<-"LST_files_monthly_climatology.txt"
|
46
|
inlistf<-"list_files_05032012.txt" #Stack of images containing the Covariates
|
47
|
|
48
|
#path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_07192012_GAM"
|
49
|
path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_07152012_GWR" #Jupiter LOCATION on Atlas for kriging
|
50
|
|
51
|
#Station location of the study area
|
52
|
#stat_loc<-read.table(paste(path,"/","location_study_area_OR_0602012.txt",sep=""),sep=",", header=TRUE)
|
53
|
#GHCN Database for 1980-2010 for study area (OR)
|
54
|
#data3<-read.table(paste(path,"/","ghcn_data_TMAXy1980_2010_OR_0602012.txt",sep=""),sep=",", header=TRUE) #Not needing at this stage...
|
55
|
|
56
|
nmodels<-8 #number of models running
|
57
|
y_var_name<-"dailyTmax" #variable value being modeled...("value" in the GHCND database)
|
58
|
predval<-1 # if set to 1, full interpolation raster produced for the study area
|
59
|
prederr<-0 # if set to 0, no uncertain error (e.g. standard error or kriging std dev) is produced
|
60
|
prop<-0.3 #Proportion of testing retained for validation
|
61
|
#prop<-0.25
|
62
|
seed_number<- 100 #Seed number for random sampling
|
63
|
out_prefix<-"_08152012_1d_gwr4" #User defined output prefix
|
64
|
setwd(path)
|
65
|
|
66
|
#source("fusion_function_07192012.R")
|
67
|
#source("KrigingUK_function_07262012.R")
|
68
|
source("GWR_function_08152012b.R")
|
69
|
############ START OF THE SCRIPT ##################
|
70
|
|
71
|
###Reading the station data and setting up for models' comparison
|
72
|
filename<-sub(".shp","",infile1) #Removing the extension from file.
|
73
|
ghcn<-readOGR(".", filename) #reading shapefile
|
74
|
|
75
|
CRS<-proj4string(ghcn) #Storing projection information (ellipsoid, datum,etc.)
|
76
|
|
77
|
mean_LST<- readGDAL(infile5) #Reading the whole raster in memory. This provides a grid for kriging
|
78
|
proj4string(mean_LST)<-CRS #Assigning coordinate information to prediction grid.
|
79
|
|
80
|
ghcn <- transform(ghcn,Northness = cos(ASPECT*pi/180)) #Adding a variable to the dataframe
|
81
|
ghcn <- transform(ghcn,Eastness = sin(ASPECT*pi/180)) #adding variable to the dataframe.
|
82
|
ghcn <- transform(ghcn,Northness_w = sin(slope*pi/180)*cos(ASPECT*pi/180)) #Adding a variable to the dataframe
|
83
|
ghcn <- transform(ghcn,Eastness_w = sin(slope*pi/180)*sin(ASPECT*pi/180)) #adding variable to the dataframe.
|
84
|
|
85
|
#Remove NA for LC and CANHEIGHT
|
86
|
ghcn$LC1[is.na(ghcn$LC1)]<-0
|
87
|
ghcn$LC3[is.na(ghcn$LC3)]<-0
|
88
|
ghcn$CANHEIGHT[is.na(ghcn$CANHEIGHT)]<-0
|
89
|
|
90
|
dates <-readLines(paste(path,"/",infile2, sep=""))
|
91
|
LST_dates <-readLines(paste(path,"/",infile3, sep=""))
|
92
|
models <-readLines(paste(path,"/",infile4, sep=""))
|
93
|
|
94
|
##Extracting the variables values from the raster files
|
95
|
|
96
|
lines<-read.table(paste(path,"/",inlistf,sep=""), sep=" ") #Column 1 contains the names of raster files
|
97
|
inlistvar<-lines[,1]
|
98
|
inlistvar<-paste(path,"/",as.character(inlistvar),sep="")
|
99
|
covar_names<-as.character(lines[,2]) #Column two contains short names for covaraites
|
100
|
|
101
|
s_raster<- stack(inlistvar) #Creating a stack of raster images from the list of variables.
|
102
|
layerNames(s_raster)<-covar_names #Assigning names to the raster layers
|
103
|
projection(s_raster)<-CRS
|
104
|
|
105
|
#stat_val<- extract(s_raster, ghcn3) #Extracting values from the raster stack for every point location in coords data frame.
|
106
|
pos<-match("ASPECT",layerNames(s_raster)) #Find column with name "value"
|
107
|
r1<-raster(s_raster,layer=pos) #Select layer from stack
|
108
|
pos<-match("slope",layerNames(s_raster)) #Find column with name "value"
|
109
|
r2<-raster(s_raster,layer=pos) #Select layer from stack
|
110
|
N<-cos(r1*pi/180)
|
111
|
E<-sin(r1*pi/180)
|
112
|
Nw<-sin(r2*pi/180)*cos(r1*pi/180) #Adding a variable to the dataframe
|
113
|
Ew<-sin(r2*pi/180)*sin(r1*pi/180) #Adding variable to the dataframe.
|
114
|
|
115
|
pos<-match("LC1",layerNames(s_raster)) #Find column with name "value"
|
116
|
LC1<-raster(s_raster,layer=pos) #Select layer from stack
|
117
|
s_raster<-dropLayer(s_raster,pos)
|
118
|
LC1[is.na(LC1)]<-0
|
119
|
pos<-match("LC3",layerNames(s_raster)) #Find column with name "value"
|
120
|
LC3<-raster(s_raster,layer=pos) #Select layer from stack
|
121
|
s_raster<-dropLayer(s_raster,pos)
|
122
|
LC3[is.na(LC3)]<-0
|
123
|
pos<-match("CANHEIGHT",layerNames(s_raster)) #Find column with name "value"
|
124
|
CANHEIGHT<-raster(s_raster,layer=pos) #Select layer from stack
|
125
|
s_raster<-dropLayer(s_raster,pos)
|
126
|
CANHEIGHT[is.na(CANHEIGHT)]<-0
|
127
|
|
128
|
xy<-coordinates(r1) #get x and y projected coordinates...
|
129
|
xy_latlon<-project(xy, CRS, inv=TRUE) # find lat long for projected coordinats (or pixels...)
|
130
|
lon<-raster(xy_latlon) #Transform a matrix into a raster object ncol=ncol(r1), nrow=nrow(r1))
|
131
|
ncol(lon)<-ncol(r1)
|
132
|
nrow(lon)<-nrow(r1)
|
133
|
extent(lon)<-extent(r1)
|
134
|
projection(lon)<-CRS #At this stage this is still an empty raster with 536 nrow and 745 ncell
|
135
|
lat<-lon
|
136
|
values(lon)<-xy_latlon[,1]
|
137
|
values(lat)<-xy_latlon[,2]
|
138
|
|
139
|
r<-stack(N,E,Nw,Ew,lon,lat,LC1,LC3,CANHEIGHT)
|
140
|
rnames<-c("Northness","Eastness","Northness_w","Eastness_w", "lon","lat","LC1","LC3","CANHEIGHT")
|
141
|
layerNames(r)<-rnames
|
142
|
s_raster<-addLayer(s_raster, r)
|
143
|
#s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame
|
144
|
|
145
|
####### Preparing LST stack of climatology...
|
146
|
|
147
|
#l=list.files(pattern="mean_month.*rescaled.rst")
|
148
|
l <-readLines(paste(path,"/",infile6, sep=""))
|
149
|
molst<-stack(l) #Creating a raster stack...
|
150
|
#setwd(old)
|
151
|
molst<-molst-273.16 #K->C #LST stack of monthly average...
|
152
|
idx <- seq(as.Date('2010-01-15'), as.Date('2010-12-15'), 'month')
|
153
|
molst <- setZ(molst, idx)
|
154
|
layerNames(molst) <- month.abb
|
155
|
|
156
|
####### CREATE A MASK FOR WATER AND
|
157
|
|
158
|
pos<-match("LC10",layerNames(s_raster)) #Find column with the current month for instance mm12
|
159
|
LC10<-raster(s_raster,pos)
|
160
|
|
161
|
LC10_mask<-LC10
|
162
|
LC10_mask[is.na(LC10_mask)]<-0
|
163
|
LC10_mask[LC10==100]<-NA
|
164
|
LC10_mask[LC10_mask<100]<-1
|
165
|
LC10_mask[is.na(LC10_mask)]<-0
|
166
|
mask_land_NA<-LC10_mask
|
167
|
mask_land_NA[mask_land_NA==0]<-NA
|
168
|
|
169
|
breakpoints<-c(0,0.99)
|
170
|
colors<-c("black","red")
|
171
|
#plot(LC10_mask, breaks=breakpoints,col=colors)
|
172
|
#quartz()
|
173
|
plot(LC10_mask, col=colors)
|
174
|
|
175
|
# ELEV_SRTM[ELEV_SRTM==-9999]<-NA
|
176
|
# avl<-c(0,60000,1)
|
177
|
# rclmat<-matrix(avl,ncol=3,byrow=TRUE)
|
178
|
# ELEV_rc<-reclass(ELEV_SRTM,rclmat)
|
179
|
# ELEV_SRTM_m <-mask(ELEV_STRM,mask_land_NA)
|
180
|
# mask2<-ELEV_SRTM_m
|
181
|
# mask2[is.na(mask)]<-1
|
182
|
# s_rst_m <-mask(s_raster,mask_land_NA) #This assigns NA to all values from LC1 that are NA in the mask_land_NA
|
183
|
#
|
184
|
####### Preparing tables for model assessment: specific diagnostic/metrics
|
185
|
|
186
|
#Model assessment: specific diagnostics/metrics
|
187
|
results_m1<- matrix(1,1,nmodels+3) #Diagnostic metrics specific to the modeleling framework
|
188
|
results_m2<- matrix(1,1,nmodels+3)
|
189
|
results_m3<- matrix(1,1,nmodels+3)
|
190
|
#results_RMSE_f<- matrix(1,length(models)+3)
|
191
|
|
192
|
#Model assessment: general diagnostic/metrics
|
193
|
results_RMSE <- matrix(1,1,nmodels+3)
|
194
|
results_MAE <- matrix(1,1,nmodels+3)
|
195
|
results_ME <- matrix(1,1,nmodels+3) #There are 8 models for kriging!!!
|
196
|
results_R2 <- matrix(1,1,nmodels+3) #Coef. of determination for the validation dataset
|
197
|
|
198
|
results_RMSE_f<- matrix(1,1,nmodels+3) #RMSE fit, RMSE for the training dataset
|
199
|
results_MAE_f <- matrix(1,1,nmodels+3)
|
200
|
results_R2_f <- matrix(1,1,nmodels+3)
|
201
|
|
202
|
######### Preparing daily values for training and testing
|
203
|
|
204
|
#Screening for bad values: value is tmax in this case
|
205
|
#ghcn$value<-as.numeric(ghcn$value)
|
206
|
ghcn_all<-ghcn
|
207
|
ghcn_test<-subset(ghcn,ghcn$value>-150 & ghcn$value<400)
|
208
|
ghcn_test2<-subset(ghcn_test,ghcn_test$ELEV_SRTM>0)
|
209
|
ghcn<-ghcn_test2
|
210
|
#coords<- ghcn[,c('x_OR83M','y_OR83M')]
|
211
|
|
212
|
##Sampling: training and testing sites...
|
213
|
|
214
|
set.seed(seed_number) #Using a seed number allow results based on random number to be compared...
|
215
|
ghcn.subsets <-lapply(dates, function(d) subset(ghcn, date==d)) #this creates a list of 10 or 365 subsets dataset based on dates
|
216
|
sampling<-vector("list",length(dates))
|
217
|
|
218
|
for(i in 1:length(dates)){
|
219
|
n<-nrow(ghcn.subsets[[i]])
|
220
|
ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows
|
221
|
nv<-n-ns #create a sample for validation with prop of the rows
|
222
|
ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly
|
223
|
ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training)
|
224
|
sampling[[i]]<-ind.training
|
225
|
}
|
226
|
|
227
|
######## Prediction for the range of dates
|
228
|
|
229
|
gwr_mod<-mclapply(1:length(dates), runGWR,mc.preschedule=FALSE,mc.cores = 8) #This is the end bracket from mclapply(...) statement
|
230
|
#fusion_mod357<-mclapply(357:365,runFusion, mc.cores=8)# for debugging
|
231
|
#test<-runKriging(1)
|
232
|
#test357<-mclapply(357:365,runFusion, mc.cores=8)# for debugging
|
233
|
#gwr_mod<-mclapply(1:1, runGWR,mc.preschedule=FALSE,mc.cores = 1) #This is the end bracket from mclapply(...) statement
|
234
|
|
235
|
#test<-mclapply(357,runFusion, mc.cores=1)# for debugging
|
236
|
|
237
|
tb<-gwr_mod[[1]][[3]][0,] #empty data frame with metric table structure that can be used in rbinding...
|
238
|
tb_tmp<-gwr_mod #copy
|
239
|
|
240
|
for (i in 1:length(tb_tmp)){
|
241
|
tmp<-tb_tmp[[i]][[3]]
|
242
|
tb<-rbind(tb,tmp)
|
243
|
}
|
244
|
rm(tb_tmp)
|
245
|
|
246
|
for(i in 4:(nmodels+3)){ # start of the for loop #1
|
247
|
tb[,i]<-as.numeric(as.character(tb[,i]))
|
248
|
}
|
249
|
tb_RMSE<-subset(tb, metric=="RMSE")
|
250
|
tb_MAE<-subset(tb,metric=="MAE")
|
251
|
tb_ME<-subset(tb,metric=="ME")
|
252
|
tb_R2<-subset(tb,metric=="R2")
|
253
|
tb_RMSE_f<-subset(tb, metric=="RMSE_f")
|
254
|
tb_MAE_f<-subset(tb,metric=="MAE_f")
|
255
|
|
256
|
tb_diagnostic1<-rbind(tb_RMSE,tb_MAE,tb_ME,tb_R2)
|
257
|
#tb_diagnostic2<-rbind(tb_,tb_MAE,tb_ME,tb_R2)
|
258
|
|
259
|
mean_RMSE<-sapply(tb_RMSE[,4:(nmodels+3)],mean)
|
260
|
mean_MAE<-sapply(tb_MAE[,4:(nmodels+3)],mean)
|
261
|
mean_R2<-sapply(tb_R2[,4:(nmodels+3)],mean)
|
262
|
mean_ME<-sapply(tb_ME[,4:(nmodels+3)],mean)
|
263
|
mean_MAE_f<-sapply(tb_MAE[,4:(nmodels+3)],mean)
|
264
|
mean_RMSE_f<-sapply(tb_RMSE_f[,4:(nmodels+3)],mean)
|
265
|
|
266
|
#Wrting metric results in textfile and model objects in .RData file
|
267
|
write.table(tb_diagnostic1, file= paste(path,"/","results2_gwr_Assessment_measure1",out_prefix,".txt",sep=""), sep=",")
|
268
|
write.table(tb, file= paste(path,"/","results2_gwr_Assessment_measure_all",out_prefix,".txt",sep=""), sep=",")
|
269
|
save(gwr_mod,file= paste(path,"/","results2_gwr_Assessment_measure_all",out_prefix,".RData",sep=""))
|
270
|
|
271
|
#### END OF SCRIPT
|