1
|
################## CLIMATE INTERPOLATION FUSION METHOD #######################################
|
2
|
############################ Merging LST and station data ##########################################
|
3
|
#This script interpolates tmax values using MODIS LST and GHCND station data #
|
4
|
#interpolation area. It requires the text file of stations and a shape file of the study area. #
|
5
|
#Note that the projection for both GHCND and study area is lonlat WGS84. #
|
6
|
#AUTHOR: Brian McGill #
|
7
|
#DATE: 07/11/2012 #
|
8
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363-- #
|
9
|
###################################################################################################
|
10
|
|
11
|
###Loading R library and packages
|
12
|
library(gtools) # loading some useful tools
|
13
|
library(mgcv) # GAM package by Simon Wood
|
14
|
library(sp) # Spatial pacakge with class definition by Bivand et al.
|
15
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al.
|
16
|
library(rgdal) # GDAL wrapper for R, spatial utilities
|
17
|
library(gstat) # Kriging and co-kriging by Pebesma et al.
|
18
|
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines
|
19
|
library(raster) # Hijmans et al. package for raster processing
|
20
|
library(parallel) # Urbanek S. and Ripley B., package for multi cores & parralel processing
|
21
|
|
22
|
### Parameters and argument
|
23
|
|
24
|
infile1<- "ghcn_or_tmax_covariates_06262012_OR83M.shp" #GHCN shapefile containing variables for modeling 2010
|
25
|
#infile2<-"list_10_dates_04212012.txt" #List of 10 dates for the regression
|
26
|
#infile2<-"list_2_dates_04212012.txt"
|
27
|
infile2<-"list_365_dates_04212012.txt"
|
28
|
infile3<-"LST_dates_var_names.txt" #LST dates name
|
29
|
infile4<-"models_interpolation_05142012.txt" #Interpolation model names
|
30
|
infile5<-"mean_day244_rescaled.rst" #Raster or grid for the locations of predictions
|
31
|
#infile6<-"lst_climatology.txt"
|
32
|
infile6<-"LST_files_monthly_climatology.txt"
|
33
|
#path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations"
|
34
|
path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations"
|
35
|
#path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_07152012" #Jupiter LOCATION on Atlas for kriging"
|
36
|
#path<-"M:/Data/IPLANT_project/data_Oregon_stations" #Locations on Atlas
|
37
|
|
38
|
#Station location of the study area
|
39
|
stat_loc<-read.table(paste(path,"/","location_study_area_OR_0602012.txt",sep=""),sep=",", header=TRUE)
|
40
|
#GHCN Database for 1980-2010 for study area (OR)
|
41
|
data3<-read.table(paste(path,"/","ghcn_data_TMAXy1980_2010_OR_0602012.txt",sep=""),sep=",", header=TRUE)
|
42
|
|
43
|
prop<-0.3 #Proportion of testing retained for validation
|
44
|
#prop<-0.25
|
45
|
seed_number<- 100 #Seed number for random sampling
|
46
|
out_prefix<-"_07152012_10d_fusion17" #User defined output prefix
|
47
|
setwd(path)
|
48
|
bias_val<-0 #if value 1 then training data is used in the bias surface rather than the all monthly stations
|
49
|
|
50
|
#source("fusion_function_07192012.R")
|
51
|
source("fusion_function_07192012.R")
|
52
|
############ START OF THE SCRIPT ##################
|
53
|
#
|
54
|
#
|
55
|
### Step 0/Step 6 in Brian's code...preparing year 2010 data for modeling
|
56
|
#
|
57
|
|
58
|
|
59
|
###Reading the station data and setting up for models' comparison
|
60
|
filename<-sub(".shp","",infile1) #Removing the extension from file.
|
61
|
ghcn<-readOGR(".", filename) #reading shapefile
|
62
|
|
63
|
CRS<-proj4string(ghcn) #Storing projection information (ellipsoid, datum,etc.)
|
64
|
|
65
|
mean_LST<- readGDAL(infile5) #Reading the whole raster in memory. This provides a grid for kriging
|
66
|
proj4string(mean_LST)<-CRS #Assigning coordinate information to prediction grid.
|
67
|
|
68
|
ghcn = transform(ghcn,Northness = cos(ASPECT*pi/180)) #Adding a variable to the dataframe
|
69
|
ghcn = transform(ghcn,Eastness = sin(ASPECT*pi/180)) #adding variable to the dataframe.
|
70
|
ghcn = transform(ghcn,Northness_w = sin(slope*pi/180)*cos(ASPECT*pi/180)) #Adding a variable to the dataframe
|
71
|
ghcn = transform(ghcn,Eastness_w = sin(slope*pi/180)*sin(ASPECT*pi/180)) #adding variable to the dataframe.
|
72
|
|
73
|
#Remove NA for LC and CANHEIGHT
|
74
|
ghcn$LC1[is.na(ghcn$LC1)]<-0
|
75
|
ghcn$LC3[is.na(ghcn$LC3)]<-0
|
76
|
ghcn$CANHEIGHT[is.na(ghcn$CANHEIGHT)]<-0
|
77
|
|
78
|
dates <-readLines(paste(path,"/",infile2, sep=""))
|
79
|
LST_dates <-readLines(paste(path,"/",infile3, sep=""))
|
80
|
models <-readLines(paste(path,"/",infile4, sep=""))
|
81
|
|
82
|
# #Model assessment: specific diagnostic/metrics for GAM
|
83
|
# results_AIC<- matrix(1,length(dates),length(models)+3)
|
84
|
# results_GCV<- matrix(1,length(dates),length(models)+3)
|
85
|
# results_DEV<- matrix(1,length(dates),length(models)+3)
|
86
|
# results_RMSE_f<- matrix(1,length(dates),length(models)+3)
|
87
|
#
|
88
|
# #Model assessment: general diagnostic/metrics
|
89
|
# results_RMSE <- matrix(1,length(dates),length(models)+4)
|
90
|
# results_MAE <- matrix(1,length(dates),length(models)+4)
|
91
|
# results_ME <- matrix(1,length(dates),length(models)+4) #There are 8+1 models
|
92
|
# results_R2 <- matrix(1,length(dates),length(models)+4) #Coef. of determination for the validation dataset
|
93
|
#
|
94
|
# results_RMSE_f<- matrix(1,length(dates),length(models)+4) #RMSE fit, RMSE for the training dataset
|
95
|
|
96
|
#Model assessment: specific diagnostic/metrics for GAM
|
97
|
results_AIC<- matrix(1,1,length(models)+3)
|
98
|
results_GCV<- matrix(1,1,length(models)+3)
|
99
|
results_DEV<- matrix(1,1,length(models)+3)
|
100
|
#results_RMSE_f<- matrix(1,length(models)+3)
|
101
|
|
102
|
#Model assessment: general diagnostic/metrics
|
103
|
results_RMSE <- matrix(1,1,length(models)+4)
|
104
|
results_MAE <- matrix(1,1,length(models)+4)
|
105
|
results_ME <- matrix(1,1,length(models)+4) #There are 8+1 models
|
106
|
results_R2 <- matrix(1,1,length(models)+4) #Coef. of determination for the validation dataset
|
107
|
|
108
|
results_RMSE_f<- matrix(1,1,length(models)+4) #RMSE fit, RMSE for the training dataset
|
109
|
results_MAE_f <- matrix(1,1,length(models)+4)
|
110
|
|
111
|
#Screening for bad values: value is tmax in this case
|
112
|
#ghcn$value<-as.numeric(ghcn$value)
|
113
|
ghcn_all<-ghcn
|
114
|
ghcn_test<-subset(ghcn,ghcn$value>-150 & ghcn$value<400)
|
115
|
ghcn_test2<-subset(ghcn_test,ghcn_test$ELEV_SRTM>0)
|
116
|
ghcn<-ghcn_test2
|
117
|
#coords<- ghcn[,c('x_OR83M','y_OR83M')]
|
118
|
|
119
|
set.seed(seed_number) #Using a seed number allow results based on random number to be compared...
|
120
|
ghcn.subsets <-lapply(dates, function(d) subset(ghcn, date==d)) #this creates a list of 10 or 365 subsets dataset based on dates
|
121
|
sampling<-vector("list",length(dates))
|
122
|
|
123
|
for(i in 1:length(dates)){
|
124
|
n<-nrow(ghcn.subsets[[i]])
|
125
|
ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows
|
126
|
nv<-n-ns #create a sample for validation with prop of the rows
|
127
|
ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly
|
128
|
ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training)
|
129
|
sampling[[i]]<-ind.training
|
130
|
}
|
131
|
|
132
|
#Start loop here...
|
133
|
|
134
|
## looping through the dates...this is the main part of the code
|
135
|
#i=1 #for debugging
|
136
|
#j=1 #for debugging
|
137
|
#for(i in 1:length(dates)){ [[ # start of the for loop #1
|
138
|
#i=1
|
139
|
|
140
|
|
141
|
#mclapply(1:length(dates), runFusion, mc.cores = 8)#This is the end bracket from mclapply(...) statement
|
142
|
|
143
|
fusion_mod<-mclapply(1:length(dates), runFusion, mc.cores = 8)#This is the end bracket from mclapply(...) statement
|
144
|
#fusion_mod357<-mclapply(357:365,runFusion, mc.cores=8)# for debugging
|
145
|
#test<-runFusion(362) #date 362 has problems with GAM
|
146
|
#test<-mclapply(357,runFusion, mc.cores=1)# for debugging
|
147
|
|
148
|
## Plotting and saving diagnostic measures
|
149
|
accuracy_tab_fun<-function(i,f_list){
|
150
|
tb<-f_list[[i]][[3]]
|
151
|
return(tb)
|
152
|
}
|
153
|
|
154
|
|
155
|
tb<-fusion_mod[[1]][[3]][0,] #empty data frame with metric table structure that can be used in rbinding...
|
156
|
tb_tmp<-fusion_mod #copy
|
157
|
|
158
|
for (i in 1:length(tb_tmp)){
|
159
|
tmp<-tb_tmp[[i]][[3]]
|
160
|
tb<-rbind(tb,tmp)
|
161
|
}
|
162
|
rm(tb_tmp)
|
163
|
|
164
|
for(i in 4:12){ # start of the for loop #1
|
165
|
tb[,i]<-as.numeric(as.character(tb[,i]))
|
166
|
}
|
167
|
|
168
|
tb_RMSE<-subset(tb, metric=="RMSE")
|
169
|
tb_MAE<-subset(tb,metric=="MAE")
|
170
|
tb_ME<-subset(tb,metric=="ME")
|
171
|
tb_R2<-subset(tb,metric=="R2")
|
172
|
tb_RMSE_f<-subset(tb, metric=="RMSE_f")
|
173
|
tb_MAE_f<-subset(tb,metric=="MAE_f")
|
174
|
|
175
|
|
176
|
tb_diagnostic1<-rbind(tb_RMSE,tb_MAE,tb_ME,tb_R2)
|
177
|
#tb_diagnostic2<-rbind(tb_,tb_MAE,tb_ME,tb_R2)
|
178
|
|
179
|
mean_RMSE<-sapply(tb_RMSE[,4:12],mean)
|
180
|
mean_MAE<-sapply(tb_MAE[,4:12],mean)
|
181
|
|
182
|
#tb<-sapply(fusion_mod,accuracy_tab_fun)
|
183
|
|
184
|
write.table(tb_diagnostic1, file= paste(path,"/","results2_fusion_Assessment_measure1",out_prefix,".txt",sep=""), sep=",")
|
185
|
write.table(tb, file= paste(path,"/","results2_fusion_Assessment_measure_all",out_prefix,".txt",sep=""), sep=",")
|
186
|
|
187
|
#tb<-as.data.frame(tb_diagnostic1)
|
188
|
|
189
|
#write.table(tb_1, file= paste(path,"/","results2_fusion_Assessment_measure1",out_prefix,".txt",sep=""), sep=",")
|
190
|
|
191
|
#write.table(tb_diagnostic2, file= paste(path,"/","results_fusion_Assessment_measure2",out_prefix,".txt",sep=""), sep=",")
|
192
|
|
193
|
#### END OF SCRIPT
|