1 |
e7bf2d1b
|
Benoit Parmentier
|
################## MULTI SAMPLING GAM FUSION METHOD ASSESSMENT ####################################
|
2 |
|
|
############################ Merging LST and station data ##########################################
|
3 |
fb039a6b
|
Benoit Parmentier
|
#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 |
|
|
#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 |
1508a57e
|
Benoit Parmentier
|
#5)GAM fusion: possibilty of running GAM+FUSION or GAM+CAI and other options added
|
12 |
0f602e87
|
Benoit Parmentier
|
#The interpolation is done first at the monthly time scale then delta surfaces are added.
|
13 |
fb039a6b
|
Benoit Parmentier
|
#AUTHOR: Benoit Parmentier
|
14 |
1508a57e
|
Benoit Parmentier
|
#DATE: 02/26/2013
|
15 |
|
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#568--
|
16 |
e7bf2d1b
|
Benoit Parmentier
|
###################################################################################################
|
17 |
|
|
|
18 |
|
|
###Loading R library and packages
|
19 |
|
|
library(gtools) # loading some useful tools
|
20 |
|
|
library(mgcv) # GAM package by Simon Wood
|
21 |
|
|
library(sp) # Spatial pacakge with class definition by Bivand et al.
|
22 |
|
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al.
|
23 |
|
|
library(rgdal) # GDAL wrapper for R, spatial utilities
|
24 |
|
|
library(gstat) # Kriging and co-kriging by Pebesma et al.
|
25 |
0163d0e2
|
Benoit Parmentier
|
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines
|
26 |
e7bf2d1b
|
Benoit Parmentier
|
library(raster) # Hijmans et al. package for raster processing
|
27 |
|
|
library(rasterVis)
|
28 |
|
|
library(parallel) # Urbanek S. and Ripley B., package for multi cores & parralel processing
|
29 |
fb039a6b
|
Benoit Parmentier
|
library(reshape)
|
30 |
|
|
library(plotrix)
|
31 |
0f602e87
|
Benoit Parmentier
|
library(maptools)
|
32 |
c1e1cc30
|
Benoit Parmentier
|
|
33 |
e7bf2d1b
|
Benoit Parmentier
|
### Parameters and argument
|
34 |
|
|
|
35 |
fb039a6b
|
Benoit Parmentier
|
infile2<-"list_365_dates_04212012.txt"
|
36 |
760957d7
|
Benoit Parmentier
|
infile_monthly<-"monthly_covariates_ghcn_data_TMAXy2010_2010_VE_02082013.shp"
|
37 |
|
|
infile_daily<-"daily_covariates_ghcn_data_TMAXy2010_2010_VE_02082013.shp"
|
38 |
|
|
infile_locs<-"stations_venezuela_region_y2010_2010_VE_02082013.shp"
|
39 |
96c5053f
|
Benoit Parmentier
|
infile3<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script
|
40 |
e7bf2d1b
|
Benoit Parmentier
|
|
41 |
96c5053f
|
Benoit Parmentier
|
in_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data"
|
42 |
|
|
out_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data"
|
43 |
760957d7
|
Benoit Parmentier
|
script_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/"
|
44 |
96c5053f
|
Benoit Parmentier
|
setwd(in_path)
|
45 |
e7bf2d1b
|
Benoit Parmentier
|
|
46 |
c1e1cc30
|
Benoit Parmentier
|
y_var_name<-"dailyTmax"
|
47 |
|
|
out_prefix<-"_365d_GAM_fus5_all_lstd_02202013" #User defined output prefix
|
48 |
|
|
seed_number<- 100 #if seed zero then no seed?
|
49 |
fb039a6b
|
Benoit Parmentier
|
nb_sample<-1 #number of time random sampling must be repeated for every hold out proportion
|
50 |
|
|
prop_min<-0.3 #if prop_min=prop_max and step=0 then predicitons are done for the number of dates...
|
51 |
|
|
prop_max<-0.3
|
52 |
|
|
step<-0
|
53 |
0766370d
|
Benoit Parmentier
|
constant<-0 #if value 1 then use the same samples as date one for the all set of dates
|
54 |
96c5053f
|
Benoit Parmentier
|
#projection used in the interpolation of the study area: should be read directly from the outline of the study area
|
55 |
760957d7
|
Benoit Parmentier
|
#CRS_interp<-"+proj=lcc +lat_1=43 +lat_2=45.5 +lat_0=41.75 +lon_0=-120.5 +x_0=400000 +y_0=0 +ellps=GRS80 +units=m +no_defs";
|
56 |
fb039a6b
|
Benoit Parmentier
|
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
|
57 |
|
|
|
58 |
c1e1cc30
|
Benoit Parmentier
|
#Models to run...this can be change for each run
|
59 |
|
|
list_models<-c("y_var ~ s(elev_1)",
|
60 |
|
|
"y_var ~ s(LST)",
|
61 |
|
|
"y_var ~ s(elev_1,LST)",
|
62 |
|
|
"y_var ~ s(lat) + s(lon)+ s(elev_1)",
|
63 |
|
|
"y_var ~ s(lat,lon,elev_1)",
|
64 |
|
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST)",
|
65 |
|
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC2)",
|
66 |
|
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(LC6)",
|
67 |
|
|
"y_var ~ s(lat,lon) + s(elev_1) + s(N_w,E_w) + s(LST) + s(DISTOC)")
|
68 |
|
|
|
69 |
|
|
#Default name of LST avg to be matched
|
70 |
|
|
lst_avg<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12")
|
71 |
|
|
|
72 |
1508a57e
|
Benoit Parmentier
|
source(file.path(script_path,"GAM_fusion_function_multisampling_02262013.R"))
|
73 |
|
|
source(file.path(script_path,"GAM_fusion_function_multisampling_validation_metrics_02262013.R"))
|
74 |
|
|
|
75 |
fb039a6b
|
Benoit Parmentier
|
###################### START OF THE SCRIPT ########################
|
76 |
e7bf2d1b
|
Benoit Parmentier
|
|
77 |
1508a57e
|
Benoit Parmentier
|
################# CREATE LOG FILE #####################
|
78 |
|
|
|
79 |
|
|
#create log file to keep track of details such as processing times and parameters.
|
80 |
c1e1cc30
|
Benoit Parmentier
|
|
81 |
|
|
log_fname<-paste("R_log_t",out_prefix, ".log",sep="")
|
82 |
|
|
|
83 |
|
|
if (file.exists(log_fname)){ #Stop the script???
|
84 |
|
|
file.remove(log_fname)
|
85 |
|
|
log_file<-file(log_fname,"w")
|
86 |
|
|
}
|
87 |
|
|
if (!file.exists(log_fname)){
|
88 |
|
|
log_file<-file(log_fname,"w")
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
time1<-proc.time() #Start stop watch
|
92 |
|
|
writeLines(paste("Starting script at this local Date and Time: ",as.character(Sys.time()),sep=""),
|
93 |
|
|
con=log_file,sep="\n")
|
94 |
|
|
writeLines("Starting script process time:",con=log_file,sep="\n")
|
95 |
|
|
writeLines(as.character(time1),con=log_file,sep="\n")
|
96 |
|
|
|
97 |
1508a57e
|
Benoit Parmentier
|
############### Reading the daily station data and other data #################
|
98 |
|
|
|
99 |
96c5053f
|
Benoit Parmentier
|
ghcn<-readOGR(dsn=in_path,layer=sub(".shp","",infile_daily))
|
100 |
|
|
CRS_interp<-proj4string(ghcn) #Storing projection information (ellipsoid, datum,etc.)
|
101 |
e7bf2d1b
|
Benoit Parmentier
|
|
102 |
96c5053f
|
Benoit Parmentier
|
stat_loc<-readOGR(dsn=in_path,layer=sub(".shp","",infile_locs))
|
103 |
e7bf2d1b
|
Benoit Parmentier
|
|
104 |
96c5053f
|
Benoit Parmentier
|
data3<-readOGR(dsn=in_path,layer=sub(".shp","",infile_monthly))
|
105 |
e7bf2d1b
|
Benoit Parmentier
|
|
106 |
760957d7
|
Benoit Parmentier
|
#Remove NA for LC and CANHEIGHT: Need to check this part after
|
107 |
e7bf2d1b
|
Benoit Parmentier
|
ghcn$LC1[is.na(ghcn$LC1)]<-0
|
108 |
|
|
ghcn$LC3[is.na(ghcn$LC3)]<-0
|
109 |
|
|
ghcn$CANHEIGHT[is.na(ghcn$CANHEIGHT)]<-0
|
110 |
0766370d
|
Benoit Parmentier
|
ghcn$LC4[is.na(ghcn$LC4)]<-0
|
111 |
|
|
ghcn$LC6[is.na(ghcn$LC6)]<-0
|
112 |
e7bf2d1b
|
Benoit Parmentier
|
|
113 |
760957d7
|
Benoit Parmentier
|
dates <-readLines(file.path(in_path,infile2)) #dates to be predicted
|
114 |
e7bf2d1b
|
Benoit Parmentier
|
|
115 |
|
|
##Extracting the variables values from the raster files
|
116 |
|
|
|
117 |
96c5053f
|
Benoit Parmentier
|
#The names of covariates can be changed...
|
118 |
760957d7
|
Benoit Parmentier
|
rnames <-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
|
119 |
96c5053f
|
Benoit Parmentier
|
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
|
120 |
|
|
lst_names<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12",
|
121 |
|
|
"nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
|
122 |
|
|
"nobs_09","nobs_10","nobs_11","nobs_12")
|
123 |
|
|
|
124 |
|
|
covar_names<-c(rnames,lc_names,lst_names)
|
125 |
|
|
|
126 |
|
|
s_raster<-stack(infile3) #read in the data stack
|
127 |
|
|
names(s_raster)<-covar_names #Assigning names to the raster layers: making sure it is included in the extraction
|
128 |
|
|
|
129 |
|
|
#Deal with no data value and zero
|
130 |
760957d7
|
Benoit Parmentier
|
#pos<-match("LC1",layerNames(s_raster)) #Find column with name "value"
|
131 |
|
|
#LC1<-raster(s_raster,layer=pos) #Select layer from stack
|
132 |
|
|
#s_raster<-dropLayer(s_raster,pos)
|
133 |
|
|
#LC1[is.na(LC1)]<-0
|
134 |
|
|
|
135 |
|
|
#pos<-match("LC3",layerNames(s_raster)) #Find column with name "value"
|
136 |
|
|
#LC3<-raster(s_raster,layer=pos) #Select layer from stack
|
137 |
|
|
#s_raster<-dropLayer(s_raster,pos)
|
138 |
|
|
#LC3[is.na(LC3)]<-0
|
139 |
|
|
|
140 |
|
|
#pos<-match("CANHEIGHT",layerNames(s_raster)) #Find column with name "value"
|
141 |
|
|
#CANHEIGHT<-raster(s_raster,layer=pos) #Select layer from stack
|
142 |
|
|
#s_raster<-dropLayer(s_raster,pos)
|
143 |
|
|
#CANHEIGHT[is.na(CANHEIGHT)]<-0
|
144 |
|
|
#pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM"
|
145 |
|
|
#ELEV_SRTM<-raster(s_raster,layer=pos) #Select layer from stack on 10/30
|
146 |
|
|
#s_raster<-dropLayer(s_raster,pos)
|
147 |
|
|
#ELEV_SRTM[ELEV_SRTM <0]<-NA
|
148 |
e7bf2d1b
|
Benoit Parmentier
|
|
149 |
|
|
#s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame
|
150 |
|
|
|
151 |
760957d7
|
Benoit Parmentier
|
######### Preparing daily and monthly values for training and testing
|
152 |
96c5053f
|
Benoit Parmentier
|
|
153 |
760957d7
|
Benoit Parmentier
|
#Screening for daily bad values: value is tmax in this case
|
154 |
e7bf2d1b
|
Benoit Parmentier
|
#ghcn$value<-as.numeric(ghcn$value)
|
155 |
96c5053f
|
Benoit Parmentier
|
#ghcn_all<-ghcn
|
156 |
|
|
#ghcn_test<-subset(ghcn,ghcn$value>-150 & ghcn$value<400)
|
157 |
|
|
#ghcn_test<-ghcn
|
158 |
|
|
#ghcn_test2<-subset(ghcn_test,ghcn_test$elev_1>0)
|
159 |
|
|
#ghcn<-ghcn_test2
|
160 |
e7bf2d1b
|
Benoit Parmentier
|
#coords<- ghcn[,c('x_OR83M','y_OR83M')]
|
161 |
|
|
|
162 |
760957d7
|
Benoit Parmentier
|
#Now clean and screen monthly values
|
163 |
|
|
#dst_all<-dst
|
164 |
|
|
dst_all<-data3
|
165 |
|
|
dst<-data3
|
166 |
|
|
#dst<-subset(dst,dst$TMax>-15 & dst$TMax<45) #may choose different threshold??
|
167 |
|
|
#dst<-subset(dst,dst$ELEV_SRTM>0) #This will drop two stations...or 24 rows
|
168 |
|
|
|
169 |
1508a57e
|
Benoit Parmentier
|
##### Create sampling: select training and testing sites ###
|
170 |
e7bf2d1b
|
Benoit Parmentier
|
|
171 |
1508a57e
|
Benoit Parmentier
|
#Make this a a function!!!!
|
172 |
96c5053f
|
Benoit Parmentier
|
|
173 |
fb039a6b
|
Benoit Parmentier
|
if (seed_number>0) {
|
174 |
|
|
set.seed(seed_number) #Using a seed number allow results based on random number to be compared...
|
175 |
|
|
}
|
176 |
e7bf2d1b
|
Benoit Parmentier
|
nel<-length(dates)
|
177 |
|
|
dates_list<-vector("list",nel) #list of one row data.frame
|
178 |
|
|
|
179 |
fb039a6b
|
Benoit Parmentier
|
prop_range<-(seq(from=prop_min,to=prop_max,by=step))*100 #range of proportion to run
|
180 |
|
|
sn<-length(dates)*nb_sample*length(prop_range) #Number of samples to run
|
181 |
e7bf2d1b
|
Benoit Parmentier
|
|
182 |
|
|
for(i in 1:length(dates)){
|
183 |
|
|
d_tmp<-rep(dates[i],nb_sample*length(prop_range)) #repeating same date
|
184 |
|
|
s_nb<-rep(1:nb_sample,length(prop_range)) #number of random sample per proportion
|
185 |
|
|
prop_tmp<-sort(rep(prop_range, nb_sample))
|
186 |
|
|
tab_run_tmp<-cbind(d_tmp,s_nb,prop_tmp)
|
187 |
|
|
dates_list[[i]]<-tab_run_tmp
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
sampling_dat<-as.data.frame(do.call(rbind,dates_list))
|
191 |
|
|
names(sampling_dat)<-c("date","run_samp","prop")
|
192 |
|
|
|
193 |
|
|
for(i in 2:3){ # start of the for loop #1
|
194 |
|
|
sampling_dat[,i]<-as.numeric(as.character(sampling_dat[,i]))
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
sampling_dat$date<- as.character(sampling_dat[,1])
|
198 |
|
|
#ghcn.subsets <-lapply(dates, function(d) subset(ghcn, date==d)) #this creates a list of 10 or 365 subsets dataset based on dates
|
199 |
|
|
ghcn.subsets <-lapply(as.character(sampling_dat$date), function(d) subset(ghcn, date==d)) #this creates a list of 10 or 365 subsets dataset based on dates
|
200 |
|
|
|
201 |
0f602e87
|
Benoit Parmentier
|
#Make this a function??
|
202 |
fb039a6b
|
Benoit Parmentier
|
## adding choice of constant sample
|
203 |
|
|
if (seed_number>0) {
|
204 |
|
|
set.seed(seed_number) #Using a seed number allow results based on random number to be compared...
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
sampling<-vector("list",length(ghcn.subsets))
|
208 |
|
|
sampling_station_id<-vector("list",length(ghcn.subsets))
|
209 |
e7bf2d1b
|
Benoit Parmentier
|
for(i in 1:length(ghcn.subsets)){
|
210 |
|
|
n<-nrow(ghcn.subsets[[i]])
|
211 |
|
|
prop<-(sampling_dat$prop[i])/100
|
212 |
|
|
ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows
|
213 |
|
|
nv<-n-ns #create a sample for validation with prop of the rows
|
214 |
|
|
ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly
|
215 |
|
|
ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training)
|
216 |
fb039a6b
|
Benoit Parmentier
|
#Find the corresponding
|
217 |
|
|
data_sampled<-ghcn.subsets[[i]][ind.training,] #selected the randomly sampled stations
|
218 |
|
|
station_id.training<-data_sampled$station #selected id for the randomly sampled stations (115)
|
219 |
|
|
#Save the information
|
220 |
e7bf2d1b
|
Benoit Parmentier
|
sampling[[i]]<-ind.training
|
221 |
fb039a6b
|
Benoit Parmentier
|
sampling_station_id[[i]]<- station_id.training
|
222 |
|
|
}
|
223 |
|
|
## Use same samples across the year...
|
224 |
|
|
if (constant==1){
|
225 |
|
|
sampled<-sampling[[1]]
|
226 |
|
|
data_sampled<-ghcn.subsets[[1]][sampled,] #selected the randomly sampled stations
|
227 |
|
|
station_sampled<-data_sampled$station #selected id for the randomly sampled stations (115)
|
228 |
|
|
list_const_sampling<-vector("list",sn)
|
229 |
|
|
list_const_sampling_station_id<-vector("list",sn)
|
230 |
|
|
for(i in 1:sn){
|
231 |
|
|
station_id.training<-intersect(station_sampled,ghcn.subsets[[i]]$station)
|
232 |
|
|
ind.training<-match(station_id.training,ghcn.subsets[[i]]$station)
|
233 |
|
|
list_const_sampling[[i]]<-ind.training
|
234 |
|
|
list_const_sampling_station_id[[i]]<-station_id.training
|
235 |
|
|
}
|
236 |
|
|
sampling<-list_const_sampling
|
237 |
|
|
sampling_station_id<-list_const_sampling_station_id
|
238 |
e7bf2d1b
|
Benoit Parmentier
|
}
|
239 |
|
|
|
240 |
1508a57e
|
Benoit Parmentier
|
########### PREDICT FOR MONTHLY SCALE #############
|
241 |
e7bf2d1b
|
Benoit Parmentier
|
|
242 |
0f602e87
|
Benoit Parmentier
|
#First predict at the monthly time scale: climatology
|
243 |
c1e1cc30
|
Benoit Parmentier
|
writeLines("Predictions at monthly scale:",con=log_file,sep="\n")
|
244 |
|
|
t1<-proc.time()
|
245 |
|
|
gamclim_fus_mod<-mclapply(1:12, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
|
246 |
|
|
#gamclim_fus_mod<-mclapply(1:12, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 4) #This is the end bracket from mclapply(...) statement
|
247 |
0f602e87
|
Benoit Parmentier
|
save(gamclim_fus_mod,file= paste("gamclim_fus_mod",out_prefix,".RData",sep=""))
|
248 |
c1e1cc30
|
Benoit Parmentier
|
t2<-proc.time()-t1
|
249 |
|
|
writeLines(as.character(t2),con=log_file,sep="\n")
|
250 |
e7bf2d1b
|
Benoit Parmentier
|
|
251 |
0f602e87
|
Benoit Parmentier
|
#now get list of raster clim layers
|
252 |
e7bf2d1b
|
Benoit Parmentier
|
|
253 |
0f602e87
|
Benoit Parmentier
|
list_tmp<-vector("list",length(gamclim_fus_mod))
|
254 |
|
|
for (i in 1:length(gamclim_fus_mod)){
|
255 |
|
|
tmp<-gamclim_fus_mod[[i]]$clim
|
256 |
|
|
list_tmp[[i]]<-tmp
|
257 |
e7bf2d1b
|
Benoit Parmentier
|
}
|
258 |
|
|
|
259 |
1508a57e
|
Benoit Parmentier
|
################## PREDICT AT DAILY TIME SCALE #################
|
260 |
|
|
|
261 |
0f602e87
|
Benoit Parmentier
|
#put together list of clim models per month...
|
262 |
|
|
rast_clim_yearlist<-list_tmp
|
263 |
|
|
#Second predict at the daily time scale: delta
|
264 |
|
|
|
265 |
|
|
#gam_fus_mod<-mclapply(1:1, runGAMFusion,mc.preschedule=FALSE,mc.cores = 1) #This is the end bracket from mclapply(...) statement
|
266 |
c1e1cc30
|
Benoit Parmentier
|
writeLines("Predictions at the daily scale:",con=log_file,sep="\n")
|
267 |
|
|
t1<-proc.time()
|
268 |
0f602e87
|
Benoit Parmentier
|
gam_fus_mod<-mclapply(1:length(ghcn.subsets), runGAMFusion,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
269 |
|
|
save(gam_fus_mod,file= paste("gam_fus_mod",out_prefix,".RData",sep=""))
|
270 |
c1e1cc30
|
Benoit Parmentier
|
t2<-proc.time()-t1
|
271 |
|
|
writeLines(as.character(t2),con=log_file,sep="\n")
|
272 |
e7bf2d1b
|
Benoit Parmentier
|
|
273 |
1508a57e
|
Benoit Parmentier
|
############### NOW RUN VALIDATION #########################
|
274 |
|
|
|
275 |
0f602e87
|
Benoit Parmentier
|
list_tmp<-vector("list",length(gam_fus_mod))
|
276 |
|
|
for (i in 1:length(gam_fus_mod)){
|
277 |
|
|
tmp<-gam_fus_mod[[i]][[y_var_name]] #y_var_name is the variable predicted (tmax or tmin)
|
278 |
|
|
list_tmp[[i]]<-tmp
|
279 |
|
|
}
|
280 |
c1e1cc30
|
Benoit Parmentier
|
rast_day_yearlist<-list_tmp #list of predicted images
|
281 |
0f602e87
|
Benoit Parmentier
|
|
282 |
c1e1cc30
|
Benoit Parmentier
|
writeLines("Validation step:",con=log_file,sep="\n")
|
283 |
|
|
t1<-proc.time()
|
284 |
0f602e87
|
Benoit Parmentier
|
#calculate_accuary_metrics<-function(i)
|
285 |
|
|
gam_fus_validation_mod<-mclapply(1:length(gam_fus_mod), calculate_accuracy_metrics,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
286 |
|
|
#gam_fus_validation_mod<-mclapply(1:1, calculate_accuracy_metrics,mc.preschedule=FALSE,mc.cores = 1) #This is the end bracket from mclapply(...) statement
|
287 |
|
|
save(gam_fus_validation_mod,file= paste("gam_fus_validation_mod",out_prefix,".RData",sep=""))
|
288 |
c1e1cc30
|
Benoit Parmentier
|
t2<-proc.time()-t1
|
289 |
|
|
writeLines(as.character(t2),con=log_file,sep="\n")
|
290 |
0766370d
|
Benoit Parmentier
|
|
291 |
1508a57e
|
Benoit Parmentier
|
#################### ASSESSMENT OF PREDICTIONS: PLOTS OF ACCURACY METRICS ###########
|
292 |
0766370d
|
Benoit Parmentier
|
|
293 |
1508a57e
|
Benoit Parmentier
|
##Create data.frame with valiation metrics for a full year
|
294 |
|
|
tb_diagnostic_v<-extract_from_list_obj(gam_fus_validation_mod,"metrics_v")
|
295 |
|
|
rownames(tb_diagnostic_v)<-NULL #remove row names
|
296 |
e7bf2d1b
|
Benoit Parmentier
|
|
297 |
1508a57e
|
Benoit Parmentier
|
#Call function to create plots of metrics for validation dataset
|
298 |
|
|
metric_names<-c("rmse","mae","me","r","m50")
|
299 |
|
|
summary_metrics<-boxplot_from_tb(tb_diagnostic_v,metric_names,out_prefix)
|
300 |
|
|
names(summary_metrics)<-c("avg","median")
|
301 |
|
|
##Write out information concerning accuracy and predictions
|
302 |
|
|
outfile<-file.path(in_path,paste("assessment_measures_",out_prefix,".txt",sep=""))
|
303 |
|
|
write.table(tb_diagnostic_v,file= outfile,row.names=FALSE,sep=",")
|
304 |
|
|
write.table(summary_metrics[[1]], file= outfile, append=TRUE,sep=",") #write out avg
|
305 |
|
|
write.table(summary_metrics[[2]], file= outfile, append=TRUE,sep=",") #write out median
|
306 |
|
|
|
307 |
|
|
#################### CLOSE LOG FILE #############
|
308 |
e7bf2d1b
|
Benoit Parmentier
|
|
309 |
c1e1cc30
|
Benoit Parmentier
|
#close log_file connection and add meta data
|
310 |
|
|
writeLines("Finished script process time:",con=log_file,sep="\n")
|
311 |
|
|
time2<-proc.time()-time1
|
312 |
|
|
writeLines(as.character(time2),con=log_file,sep="\n")
|
313 |
|
|
#later on add all the paramters used in the script...
|
314 |
|
|
writeLines(paste("Finished script at this local Date and Time: ",as.character(Sys.time()),sep=""),
|
315 |
|
|
con=log_file,sep="\n")
|
316 |
|
|
writeLines("End of script",con=log_file,sep="\n")
|
317 |
|
|
close(log_file)
|
318 |
|
|
|
319 |
1508a57e
|
Benoit Parmentier
|
############################################################
|
320 |
|
|
######################## END OF SCRIPT #####################
|