Project

General

Profile

Download (14.4 KB) Statistics
| Branch: | Revision:
1
##################    Validation and analyses of results  #######################################
2
############################ Covariate production for a given tile/region ##########################################
3
#This script examines inputs and outputs from the interpolation step.                             
4
#Part 1: Script produces plots for every selected date
5
#Part 2: Examine 
6
#AUTHOR: Benoit Parmentier                                                                       
7
#DATE: 02/22/2013                                                                                 
8

    
9
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#???--   
10

    
11
##Comments and TODO:
12
#Separate inteprolation results analyses from covariates analyses 
13

    
14
##################################################################################################
15

    
16
###Loading R library and packages   
17
library(RPostgreSQL)
18
library(sp)                                             # Spatial pacakge with class definition by Bivand et al.
19
library(spdep)                                          # Spatial pacakge with methods and spatial stat. by Bivand et al.
20
library(rgdal)                                          # GDAL wrapper for R, spatial utilities
21
library(raster)
22
library(gtools)
23
library(rasterVis)
24
library(graphics)
25
library(grid)
26
library(lattice)
27

    
28
### Parameters and arguments
29

    
30
##Paths to inputs and output
31
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
32
out_path<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data/"
33
infile3<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script
34

    
35
setwd(in_path)
36

    
37
### Functions used in the script
38

    
39
load_obj <- function(f) 
40
{
41
  env <- new.env()
42
  nm <- load(f, env)[1]	
43
  env[[nm]]
44
}
45

    
46
### PLOTTING RESULTS FROM VENEZUELA INTERPOLATION FOR ANALYSIS
47

    
48
#Select relevant dates and load R objects created during the interpolation step
49

    
50
date_selected<-c("20100103")
51

    
52
gam_fus_mod<-load_obj("gam_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
53
validation_obj<-load_obj("gam_fus_validation_mod_365d_GAM_fus5_all_lstd_02202013.RData")
54
clim_obj<-load_obj("gamclim_fus_mod_365d_GAM_fus5_all_lstd_02202013.RData")
55

    
56
## Read covariate stack...
57
rnames <-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
58
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
59
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",
60
             "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
61
             "nobs_09","nobs_10","nobs_11","nobs_12")
62

    
63
covar_names<-c(rnames,lc_names,lst_names)
64

    
65
s_raster<-stack(infile3)                   #read in the data stack
66
names(s_raster)<-covar_names               #Assigning names to the raster layers: making sure it is included in the extraction
67

    
68
## Figure 0: study area based on LC12 (water) mask
69

    
70
LC_mask<-subset(s_raster,"LC12")
71
LC_mask[LC_mask==100]<-NA
72
LC_mask <- LC_mask < 100
73
LC_mask_rec<-LC_mask
74
LC_mask_rec[is.na(LC_mask_rec)]<-0
75

    
76
png(paste("Study_area_",
77
          out_prefix,".png", sep=""))
78
plot(LC_mask_rec,legend=FALSE,col=c("black","red"))
79
legend("topright",legend=c("Outside","Inside"),title="Study area",
80
       pt.cex=0.9,fill=c("black","red"),bty="n")
81
title("Study area")
82
dev.off()
83

    
84
#determine index position matching date selected
85

    
86
i_dates<-vector("list",length(date_selected))
87
for (i in 1:length(gam_fus_mod)){
88
  for (j in 1:length(date_selected)){
89
    if(gam_fus_mod[[i]]$sampling_dat$date==date_selected[j]){  
90
      i_dates[[j]]<-i
91
    }
92
  }
93
}
94

    
95
#Examine the first select date add loop or function later
96
j=1
97

    
98
date<-strptime(date_selected[j], "%Y%m%d")   # interpolation date being processed
99
month<-strftime(date, "%m")          # current month of the date being processed
100

    
101
#Get raster stack of interpolated surfaces
102
index<-i_dates[[j]]
103
pred_temp<-as.character(gam_fus_mod[[index]]$dailyTmax) #list of files
104
rast_pred_temp<-stack(pred_temp) #stack of temperature predictions from models 
105

    
106
#Get validation metrics, daily spdf training and testing stations, monthly spdf station input
107
sampling_dat<-gam_fus_mod[[index]]$sampling_dat
108
metrics_v<-validation_obj[[index]]$metrics_v
109
metrics_s<-validation_obj[[index]]$metrics_s
110
data_v<-validation_obj[[index]]$data_v
111
data_s<-validation_obj[[index]]$data_s
112
data_month<-clim_obj[[index]]$data_month
113
formulas<-clim_obj[[index]]$formulas
114

    
115
#Adding layer LST to the raster stack of covariates
116
#The names of covariates can be changed...
117

    
118
LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
119
pos<-match("LST",layerNames(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
120
s_raster<-dropLayer(s_raster,pos)      # If it exists drop layer
121
pos<-match(LST_month,layerNames(s_raster)) #Find column with the current month for instance mm12
122
r1<-raster(s_raster,layer=pos)             #Select layer from stack
123
layerNames(r1)<-"LST"
124
#Get mask image!!
125

    
126
date_proc<-strptime(sampling_dat$date, "%Y%m%d")   # interpolation date being processed
127
mo<-as.integer(strftime(date_proc, "%m"))          # current month of the date being processed
128
day<-as.integer(strftime(date_proc, "%d"))
129
year<-as.integer(strftime(date_proc, "%Y"))
130
datelabel=format(ISOdate(year,mo,day),"%b %d, %Y")
131

    
132
## Figure 1: LST_TMax_scatterplot 
133

    
134
rmse<-metrics_v$rmse[nrow(metrics_v)]
135
rmse_f<-metrics_s$rmse[nrow(metrics_s)]  
136

    
137
png(paste("LST_TMax_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
138
          out_prefix,".png", sep=""))
139
plot(data_month$TMax,data_month$LST,xlab="Station mo Tmax",ylab="LST mo Tmax")
140
title(paste("LST vs TMax for",datelabel,sep=" "))
141
abline(0,1)
142
nb_point<-paste("n=",length(data_month$TMax),sep="")
143
mean_bias<-paste("Mean LST bias= ",format(mean(data_month$LSTD_bias,na.rm=TRUE),digits=3),sep="")
144
#Add the number of data points on the plot
145
legend("topleft",legend=c(mean_bias,nb_point),bty="n")
146
dev.off()
147

    
148
## Figure 2: Daily_tmax_monthly_TMax_scatterplot 
149

    
150
png(paste("Daily_tmax_monthly_TMax_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
151
          out_prefix,".png", sep=""))
152
plot(dailyTmax~TMax,data=data_s,xlab="Mo Tmax",ylab=paste("Daily for",datelabel),main="across stations in VE")
153
nb_point<-paste("ns=",length(data_s$TMax),sep="")
154
nb_point2<-paste("ns_obs=",length(data_s$TMax)-sum(is.na(data_s[[y_var_name]])),sep="")
155
nb_point3<-paste("n_month=",length(data_month$TMax),sep="")
156
#Add the number of data points on the plot
157
legend("topleft",legend=c(nb_point,nb_point2,nb_point3),bty="n",cex=0.8)
158
dev.off()
159

    
160
## Figure 3: Predicted_tmax_versus_observed_scatterplot 
161

    
162
#This is for mod_kr!! add other models later...
163
png(paste("Predicted_tmax_versus_observed_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
164
          out_prefix,".png", sep=""))
165
#plot(data_s$mod_kr~data_s[[y_var_name]],xlab=paste("Actual daily for",datelabel),ylab="Pred daily")
166

    
167
y_range<-range(c(data_s$mod_kr,data_v$mod_kr),na.rm=T)
168
x_range<-range(c(data_s[[y_var_name]],data_v[[y_var_name]]),na.rm=T)
169
col_t<- c("black","red")
170
pch_t<- c(1,2)
171
plot(data_s$mod_kr,data_s[[y_var_name]], 
172
     xlab=paste("Actual daily for",datelabel),ylab="Pred daily", 
173
     ylim=y_range,xlim=x_range,col=col_t[1],pch=pch_t[1])
174
points(data_v$mod_kr,data_v[[y_var_name]],col=col_t[2],pch=pch_t[2])
175
grid(lwd=0.5, col="black")
176
#plot(data_v$mod_kr~data_v[[y_var_name]],xlab=paste("Actual daily for",datelabel),ylab="Pred daily")
177
abline(0,1)
178
legend("topleft",legend=c("training","testing"),pch=pch_t,col=col_t,bty="n",cex=0.8)
179
title(paste("Predicted_tmax_versus_observed_scatterplot for",datelabel,sep=" "))
180
nb_point1<-paste("ns_obs=",length(data_s$TMax)-sum(is.na(data_s[[y_var_name]])),sep="")
181
rmse_str1<-paste("RMSE= ",format(rmse,digits=3),sep="")
182
rmse_str2<-paste("RMSE_f= ",format(rmse_f,digits=3),sep="")
183

    
184
#Add the number of data points on the plot
185
legend("bottomright",legend=c(nb_point1,rmse_str1,rmse_str2),bty="n",cex=0.8)
186
dev.off()
187

    
188
## Figure 5: prediction raster images
189
png(paste("Raster_prediction_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
190
          out_prefix,".png", sep=""))
191
#paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
192
names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":")
193
#plot(rast_pred_temp)
194
levelplot(rast_pred_temp)
195
dev.off()
196

    
197
## Figure 6: training and testing stations used
198
png(paste("Training_testing_stations_map_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
199
          out_prefix,".png", sep=""))
200
plot(raster(rast_pred_temp,layer=5))
201
plot(data_s,col="black",cex=1.2,pch=2,add=TRUE)
202
plot(data_v,col="red",cex=1.2,pch=1,add=TRUE)
203
legend("topleft",legend=c("training stations", "testing stations"), 
204
       cex=1, col=c("black","red"),
205
       pch=c(2,1),bty="n")
206
dev.off()
207

    
208
## Figure 7: monthly stations used
209

    
210
png(paste("Monthly_data_study_area",
211
          out_prefix,".png", sep=""))
212
plot(raster(rast_pred_temp,layer=5))
213
plot(data_month,col="black",cex=1.2,pch=4,add=TRUE)
214
title("Monthly ghcn station in Venezuela for January")
215
dev.off()
216

    
217
## Figure 8: delta surface and bias
218

    
219
png(paste("Bias_delta_surface_",sampling_dat$date[i],"_",sampling_dat$prop[i],
220
          "_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))
221

    
222
bias_rast<-stack(clim_obj[[index]]$bias)
223
delta_rast<-raster(gam_fus_mod[[index]]$delta) #only one delta image!!!
224
names(delta_rast)<-"delta"
225
rast_temp_date<-stack(bias_rast,delta_rast)
226
rast_temp_date<-mask(rast_temp_date,LC_mask,file="test.tif",overwrite=TRUE)
227
#bias_d_rast<-raster("fusion_bias_LST_20100103_30_1_10d_GAM_fus5_all_lstd_02082013.rst")
228
plot(rast_temp_date)
229

    
230
dev.off()
231

    
232
#Figure 9: histogram for all images...
233

    
234
histogram(rast_pred_temp)
235

    
236
## Summarize information for the day: write out textfile...
237

    
238
#Number of station per month
239
#Number of station per day (training, testing,NA)
240
#metrics_v,metrics_s
241
#
242

    
243
# ################
244
# #PART 2: Region Covariate analyses ###
245
# ################
246
# 
247
# # This should be in a separate script to analyze covariates from region.
248
# 
249
# #MAP1:Study area with LC mask and tiles/polygon outline
250
# 
251
# 
252
# #MAP 2: plotting land cover in the study region:
253
# 
254
# l1<-"LC1,Evergreen/deciduous needleleaf trees"
255
# l2<-"LC2,Evergreen broadleaf trees"
256
# l3<-"LC3,Deciduous broadleaf trees"
257
# l4<-"LC4,Mixed/other trees"
258
# l5<-"LC5,Shrubs"
259
# l6<-"LC6,Herbaceous vegetation"
260
# l7<-"LC7,Cultivated and managed vegetation"
261
# l8<-"LC8,Regularly flooded shrub/herbaceous vegetation"
262
# l9<-"LC9,Urban/built-up"
263
# l10<-"LC10,Snow/ice"
264
# l11<-"LC11,Barren lands/sparse vegetation"
265
# l12<-"LC12,Open water"
266
# lc_names_str<-c(l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12)
267
# 
268
# names(lc_reg_s)<-lc_names_str
269
# 
270
# png(paste("LST_TMax_scatterplot_",sampling_dat$date[i],"_",sampling_dat$prop[i],"_",sampling_dat$run_samp[i], out_prefix,".png", sep=""))
271
# plot(modst$TMax,sta_tmax_from_lst,xlab="Station mo Tmax",ylab="LST mo Tmax",main=paste("LST vs TMax for",datelabel,sep=" "))
272
# abline(0,1)
273
# nb_point<-paste("n=",length(modst$TMax),sep="")
274
# mean_bias<-paste("LST bigrasas= ",format(mean(modst$LSTD_bias,na.rm=TRUE),digits=3),sep="")
275
# #Add the number of data points on the plot
276
# legend("topleft",legend=c(mean_bias,nb_point),bty="n")
277
# dev.off()
278
# 
279
# #Map 3: Elevation and LST in January
280
# tmp_s<-stack(LST,elev_1)
281
# png(paste("LST_elev_",sampling_dat$date[i],"_",sampling_dat$prop[i],"_",sampling_dat$run_samp[i], out_prefix,".png", sep=""))
282
# plot(tmp_s)
283
# 
284
# #Map 4: LST climatology per month
285
#         
286
# names_tmp<-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")
287
# LST_s<-subset(s_raster,names_tmp)
288
# names_tmp<-c("nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
289
#              "nobs_09","nobs_10","nobs_11","nobs_12")
290
# LST_nobs<-subset(s_raster,names_tmp)
291
# 
292
# LST_nobs<-mask(LST_nobs,LC_mask,filename="test2.tif")
293
# LST_s<-mask(LST_s,LC_mask,filename="test3.tif")
294
# c("Jan","Feb")
295
# plot(LST_s)
296
# plot(LST_nobs)
297
# 
298
# #Map 5: LST and TMax
299
# 
300
# #note differnces in patternin agricultural areas and 
301
# min_values<-cellStats(LST_s,"min")
302
# max_values<-cellStats(LST_s,"max")
303
# mean_values<-cellStats(LST_s,"mean")
304
# sd_values<-cellStats(LST_s,"sd")
305
# #median_values<-cellStats(molst,"median") Does not extist
306
# statistics_LST_s<-cbind(min_values,max_values,mean_values,sd_values) #This shows that some values are extremes...especially in October
307
# LST_stat_data<-as.data.frame(statistics_LST_s)
308
# names(LST_stat_data)<-c("min","max","mean","sd")
309
# # Statistics for number of valid observation stack
310
# min_values<-cellStats(nobslst,"min")
311
# max_values<-cellStats(nobslst,"max")
312
# mean_values<-cellStats(nobslst,"mean")
313
# sd_values<-cellStats(nobslst,"sd")
314
# statistics_LSTnobs_s<-cbind(min_values,max_values,mean_values,sd_values) #This shows that some values are extremes...especially in October
315
# LSTnobs_stat_data<-as.data.frame(statistics_LSTnobs_s)
316
# 
317
# X11(width=12,height=12)
318
# #Plot statiscs (mean,min,max) for monthly LST images
319
# plot(1:12,LST_stat_data$mean,type="b",ylim=c(-15,60),col="black",xlab="month",ylab="tmax (degree C)")
320
# lines(1:12,LST_stat_data$min,type="b",col="blue")
321
# lines(1:12,LST_stat_data$max,type="b",col="red")
322
# text(1:12,LST_stat_data$mean,rownames(LST_stat_data),cex=1,pos=2)
323
# 
324
# legend("topleft",legend=c("min","mean","max"), cex=1.5, col=c("blue","black","red"),
325
#        lty=1)
326
# title(paste("LST statistics for Oregon", "2010",sep=" "))
327
# savePlot("lst_statistics_OR.png",type="png")
328
# 
329
# #Plot number of valid observations for LST
330
# plot(1:12,LSTnobs_stat_data$mean,type="b",ylim=c(0,280),col="black",xlab="month",ylab="tmax (degree C)")
331
# lines(1:12,LSTnobs_stat_data$min,type="b",col="blue")
332
# lines(1:12,LSTnobs_stat_data$max,type="b",col="red")
333
# text(1:12,LSTnobs_stat_data$mean,rownames(LSTnobs_stat_data),cex=1,pos=2)
334
# 
335
# legend("topleft",legend=c("min","mean","max"), cex=1.5, col=c("blue","black","red"),
336
#        lty=1)
337
# title(paste("LST number of valid observations for Oregon", "2010",sep=" "))
338
# savePlot("lst_nobs_OR.png",type="png")
339
# 
340
# plot(data_month$TMax,add=TRUE)
341
# 
342
# ### Map 6: station in the region
343
# 
344
# plot(tmax_predicted)
345
# plot(data_s,col="black",cex=1.2,pch=4,add=TRUE)
346
# plot(data_v,col="blue",cex=1.2,pch=2,add=TRUE)
347
# 
348
# plot(tmax_predicted)
349
# plot(data_month,col="black",cex=1.2,pch=4,add=TRUE)
350
# title("Monthly ghcn station in Venezuela for 2000-2010")
351
# 
352
#png...output?
353
# plot(interp_area, axes =TRUE)
354
# plot(stat_reg, pch=1, col="red", cex= 0.7, add=TRUE)
355
# plot(data_reg,pch=2,col="blue",cex=2,add=TRUE)
356

    
357
#### End of script ####
(37-37/37)