Revision 8c68c923
Added by Benoit Parmentier over 10 years ago
climate/research/oregon/interpolation/results_interpolation_date_output_analyses.R | ||
---|---|---|
5 | 5 |
#Part 2: Examine |
6 | 6 |
#AUTHOR: Benoit Parmentier |
7 | 7 |
#DATE: 08/05/2013 |
8 |
#DATE MODIFIED: 05/21/2014 |
|
8 | 9 |
|
9 | 10 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#???-- |
10 | 11 |
|
... | ... | |
37 | 38 |
#DATE: 08/05/2013 |
38 | 39 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363-- |
39 | 40 |
|
40 |
#1) in_path |
|
41 |
#1) in_path_tile: location of files, if NULL then code is run on NEX node or as stage 5
|
|
41 | 42 |
#2) out_path |
42 | 43 |
#3) script_path |
43 | 44 |
#4) raster_prediction_obj |
... | ... | |
61 | 62 |
|
62 | 63 |
### BEGIN SCRIPT |
63 | 64 |
#Parse input parameters |
64 |
|
|
65 |
#date_selected_results <- c("x") |
|
65 | 66 |
date_selected<-list_param$date_selected_results #dates for plot creation |
66 | 67 |
var<-list_param$var #variable being interpolated |
67 | 68 |
out_path <- list_param$out_path |
68 | 69 |
interpolation_method <- list_param$interpolation_method |
69 |
infile_covariates <- list_param$covar_obj$infile_covariates |
|
70 |
covar_names<-list_param$covar_obj$covar_names
|
|
70 |
|
|
71 |
in_path_tile <- list_param$in_path_tile
|
|
71 | 72 |
|
73 |
if(!is.null(in_path_tile)){ |
|
74 |
covar_obj <- load_obj(list_param$covar_obj) |
|
75 |
infile_covariates <- file.path(in_path_tile,basename(covar_obj$infile_covariates)) |
|
76 |
covar_names <- covar_obj$covar_names |
|
77 |
}else{ #we are on the node or running as stage 5 |
|
78 |
infile_covariates <- list_param$covar_obj$infile_covariates #already loaded in memory |
|
79 |
covar_names<-list_param$covar_obj$covar_names |
|
80 |
} |
|
81 |
|
|
82 |
#if raster_obj has not been loaded in memory then we have |
|
83 |
#the name of the RData object for a specific tile |
|
72 | 84 |
raster_prediction_obj<-list_param$raster_prediction_obj |
73 |
method_mod_obj<-raster_prediction_obj$method_mod_obj |
|
74 |
validation_mod_obj<-raster_prediction_obj$validation_mod_obj |
|
85 |
if(class(raster_prediction_obj)=="character"){ |
|
86 |
raster_prediction_obj <- load_obj(raster_prediction_obj) |
|
87 |
} |
|
88 |
|
|
89 |
method_mod_obj <- raster_prediction_obj$method_mod_obj |
|
90 |
validation_mod_obj <-raster_prediction_obj$validation_mod_obj |
|
75 | 91 |
|
92 |
if(interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){ |
|
93 |
multi_timescale <- TRUE |
|
94 |
} |
|
76 | 95 |
#This should not be set here...? master script |
77 | 96 |
if (var=="TMAX"){ |
78 | 97 |
y_var_name<-"dailyTmax" |
... | ... | |
82 | 101 |
y_var_name<-"dailyTmin" |
83 | 102 |
y_var_month <-"TMin" |
84 | 103 |
} |
104 |
#add precip option later... |
|
85 | 105 |
|
86 | 106 |
## Read covariate brick... |
87 |
s_raster<-brick(infile_covariates)
|
|
88 |
names(s_raster)<-covar_names #Assigning names to the raster layers: making sure it is included in the extraction |
|
107 |
s_raster <- brick(infile_covariates) #stack produced for specific tile
|
|
108 |
names(s_raster)<- covar_names #Assigning names to the raster layers: making sure it is included in the extraction
|
|
89 | 109 |
|
90 | 110 |
## Prepare study area mask: based on LC12 (water) |
91 | 111 |
|
92 |
LC_mask<-subset(s_raster,"LC12")
|
|
93 |
LC_mask[LC_mask==100]<-NA |
|
112 |
LC_mask <- subset(s_raster,"LC12")
|
|
113 |
LC_mask[LC_mask==100]<- NA
|
|
94 | 114 |
LC_mask <- LC_mask < 100 |
95 | 115 |
LC_mask_rec<-LC_mask |
96 |
LC_mask_rec[is.na(LC_mask_rec)]<-0 |
|
116 |
LC_mask_rec[is.na(LC_mask_rec)]<- 0
|
|
97 | 117 |
|
98 | 118 |
#determine index position matching date selected |
99 | 119 |
i_dates<-vector("list",length(date_selected)) |
... | ... | |
106 | 126 |
} |
107 | 127 |
#Examine the first select date add loop or function later |
108 | 128 |
#j=1 |
109 |
date<-strptime(date_selected[j], "%Y%m%d") # interpolation date being processed
|
|
110 |
month<-strftime(date, "%m") # current month of the date being processed
|
|
129 |
date <- strptime(date_selected[j], "%Y%m%d") # interpolation date being processed
|
|
130 |
month <- strftime(date, "%m") # current month of the date being processed
|
|
111 | 131 |
|
112 | 132 |
#Get raster stack of interpolated surfaces |
113 |
index<-i_dates[[j]] |
|
114 |
pred_temp<-as.character(method_mod_obj[[index]][[y_var_name]]) #list of daily prediction files with path included |
|
115 |
rast_pred_temp_s <-stack(pred_temp) #stack of temperature predictions from models (daily) |
|
116 |
rast_pred_temp <-mask(rast_pred_temp_s,LC_mask,file=file.path(out_path,"test.tif"),overwrite=TRUE) |
|
133 |
index <- i_dates[[j]] |
|
134 |
##The path of production is not the same if input_path_tile is not NULL |
|
135 |
if(!is.null(in_path_tile)){ |
|
136 |
#infile_covariates <- file.path(in_path_tile,basename(list_param$covar_obj$infile_covariates)) |
|
137 |
pred_temp <- basename(as.character(method_mod_obj[[index]][[y_var_name]])) #list of daily prediction files with path included |
|
138 |
pred_temp <- file.path(in_path_tile,pred_temp) |
|
139 |
}else{ |
|
140 |
pred_temp <- as.character(method_mod_obj[[index]][[y_var_name]]) #list of daily prediction files with path included |
|
141 |
} |
|
142 |
|
|
143 |
rast_pred_temp_s <- stack(pred_temp) #stack of temperature predictions from models (daily) |
|
144 |
rast_pred_temp <- mask(rast_pred_temp_s,LC_mask,file=file.path(out_path,"test.tif"),overwrite=TRUE) |
|
117 | 145 |
|
118 | 146 |
#Get validation metrics, daily spdf training and testing stations, monthly spdf station input |
119 | 147 |
sampling_dat<-method_mod_obj[[index]]$sampling_dat |
120 | 148 |
metrics_v<-validation_mod_obj[[index]]$metrics_v |
121 | 149 |
metrics_s<-validation_mod_obj[[index]]$metrics_s |
122 |
data_v<-validation_mod_obj[[index]]$data_v |
|
150 |
data_v<-validation_mod_obj[[index]]$data_v #testing with residuals
|
|
123 | 151 |
data_s<-validation_mod_obj[[index]]$data_s |
124 |
formulas<-method_mod_obj[[index]]$formulas |
|
152 |
#no formula if multi-timescale method |
|
153 |
if(multi_timescale==TRUE){ |
|
154 |
formulas<- raster_prediction_obj$clim_method_mod_obj[[as.integer(month)]]$formulas #models ran |
|
155 |
}else{ |
|
156 |
formulas<- method_mod_obj[[index]]$formulas #models ran |
|
157 |
} |
|
125 | 158 |
|
126 |
|
|
127 | 159 |
#Adding layer LST to the raster stack of covariates |
128 | 160 |
#The names of covariates can be changed... |
129 | 161 |
|
... | ... | |
147 | 179 |
rmse_f<-metrics_s$rmse[nrow(metrics_s)] |
148 | 180 |
|
149 | 181 |
#Set as constant in master script ?? : c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion") |
150 |
if (interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){ |
|
182 |
#if (interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){ |
|
183 |
if (multi_timescale==TRUE){ |
|
151 | 184 |
#if multi-time scale method then the raster prediction object contains a "clim_method_mod_obj" |
152 | 185 |
clim_method_mod_obj <- raster_prediction_obj$clim_method_mod_obj |
153 |
data_month<-clim_method_mod_obj[[index]]$data_month
|
|
186 |
data_month <-clim_method_mod_obj[[mo]]$data_month
|
|
154 | 187 |
|
155 | 188 |
png(file.path(out_path,paste("LST_",y_var_month,"_scatterplot_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp, |
156 | 189 |
out_prefix,".png", sep=""))) |
157 |
plot(data_month[[y_var_month]],data_month$LST,xlab=paste("Station mo ",y_var_month,sep=""),ylab=paste("LST mo ",y_var_month,sep=""))
|
|
158 |
title(paste("LST vs ", y_var_month,"for",datelabel,sep=" "))
|
|
190 |
plot(data_month[[y_var_month]],data_month$LST,xlab=paste("Station mo ",y_var_month,sep=""),ylab=paste("LST month ",mo," ",sep=""))
|
|
191 |
title(paste("LST vs ", y_var_month,"for month ",mo,sep=" "))
|
|
159 | 192 |
abline(0,1) |
160 | 193 |
nb_point<-paste("n=",length(data_month[[y_var_month]]),sep="") |
161 | 194 |
LSTD_bias <- data_month$TMax - data_month$LST #in case it is a CAI method, calculate bias |
... | ... | |
165 | 198 |
dev.off() |
166 | 199 |
|
167 | 200 |
## Figure 2: Daily_tmax_monthly_TMax_scatterplot, modify for TMin!! |
168 |
|
|
169 |
png(file.path(out_path,paste("Month_day_scatterplot_",y_var_name,"_",y_var_month,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp, |
|
170 |
out_prefix,".png", sep=""))) |
|
171 |
plot(data_s[[y_var_name]]~data_s[[y_var_month]],xlab=paste("Month") ,ylab=paste("Daily for",datelabel),main="across stations in VE") |
|
172 |
nb_point<-paste("ns=",length(data_s[[y_var_month]]),sep="") |
|
173 |
nb_point2<-paste("ns_obs=",length(data_s[[y_var_month]])-sum(is.na(data_s[[y_var_name]])),sep="") |
|
174 |
nb_point3<-paste("n_month=",length(data_month[[y_var_month]]),sep="") |
|
201 |
#This is not stored in data_s$TMax? |
|
202 |
#png(file.path(out_path,paste("Month_day_scatterplot_",y_var_name,"_",y_var_month,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
|
203 |
# out_prefix,".png", sep="")))
|
|
204 |
#plot(data_s[[y_var_name]]~data_s[[y_var_month]],xlab=paste("Month") ,ylab=paste("Daily for",datelabel),main="across stations in VE")
|
|
205 |
#nb_point<-paste("ns=",length(data_s[[y_var_month]]),sep="")
|
|
206 |
#nb_point2<-paste("ns_obs=",length(data_s[[y_var_month]])-sum(is.na(data_s[[y_var_name]])),sep="")
|
|
207 |
#nb_point3<-paste("n_month=",length(data_month[[y_var_month]]),sep="")
|
|
175 | 208 |
#Add the number of data points on the plot |
176 |
legend("topleft",legend=c(nb_point,nb_point2,nb_point3),bty="n",cex=0.8) |
|
177 |
dev.off() |
|
209 |
#legend("topleft",legend=c(nb_point,nb_point2,nb_point3),bty="n",cex=0.8)
|
|
210 |
#dev.off()
|
|
178 | 211 |
|
179 | 212 |
## Figure 3: monthly stations used |
180 | 213 |
|
181 | 214 |
png(file.path(out_path,paste("Monthly_data_study_area_", y_var_name, |
182 | 215 |
out_prefix,".png", sep=""))) |
183 |
plot(raster(rast_pred_temp,layer=5))
|
|
216 |
plot(raster(rast_pred_temp,layer=1))
|
|
184 | 217 |
plot(data_month,col="black",cex=1.2,pch=4,add=TRUE) |
185 |
title("Monthly ghcn station in Venezuela for January")
|
|
218 |
title("Monthly ghcn station in tile for January")
|
|
186 | 219 |
dev.off() |
187 | 220 |
|
188 |
} |
|
221 |
} #End of if multi_timescale=TRUE |
|
222 |
|
|
189 | 223 |
## Figure 4: Predicted_tmax_versus_observed_scatterplot |
190 | 224 |
|
191 | 225 |
names_mod <- names(method_mod_obj[[index]][[y_var_name]]) #names of models to plot |
... | ... | |
219 | 253 |
} |
220 | 254 |
|
221 | 255 |
## Figure 5a: prediction raster images |
222 |
png(file.path(out_path,paste("Raster_prediction_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp, |
|
256 |
png(file.path(out_path,paste("Raster_prediction_levelplot_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
|
223 | 257 |
out_prefix,".png", sep=""))) |
224 | 258 |
#paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":") |
225 | 259 |
names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":") |
... | ... | |
228 | 262 |
dev.off() |
229 | 263 |
|
230 | 264 |
## Figure 5b: prediction raster images |
231 |
png(file.path(out_path,paste("Raster_prediction_plot",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp, |
|
265 |
png(file.path(out_path,paste("Raster_prediction_plot_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp,
|
|
232 | 266 |
out_prefix,".png", sep=""))) |
233 | 267 |
#paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":") |
234 | 268 |
names(rast_pred_temp)<-paste(metrics_v$pred_mod,format(metrics_v$rmse,digits=3),sep=":") |
235 | 269 |
plot(rast_pred_temp) |
236 | 270 |
dev.off() |
237 | 271 |
|
238 |
## Figure 6: training and testing stations used |
|
272 |
## Figure 6: training and testing daily stations used
|
|
239 | 273 |
png(file.path(out_path,paste("Training_testing_stations_map_",y_var_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_",sampling_dat$run_samp, |
240 | 274 |
out_prefix,".png", sep=""))) |
241 |
plot(raster(rast_pred_temp,layer=5))
|
|
275 |
plot(raster(rast_pred_temp,layer=1))
|
|
242 | 276 |
plot(data_s,col="black",cex=1.2,pch=2,add=TRUE) |
243 | 277 |
plot(data_v,col="red",cex=1.2,pch=1,add=TRUE) |
244 | 278 |
legend("topleft",legend=c("training stations", "testing stations"), |
245 | 279 |
cex=1, col=c("black","red"), |
246 | 280 |
pch=c(2,1),bty="n") |
281 |
title(paste("Daily stations ", datelabel,sep="")) |
|
282 |
nb_point1<-paste("ns_obs=",nrow(data_s),sep="") |
|
283 |
nb_point2<-paste("nv_obs=",nrow(data_v),sep="") |
|
284 |
legend("bottomright",legend=c(nb_point1,nb_point2),bty="n",cex=0.8) |
|
285 |
|
|
247 | 286 |
dev.off() |
248 | 287 |
|
249 | 288 |
## Figure 7: delta surface and bias |
... | ... | |
251 | 290 |
if (interpolation_method%in% c("gam_fusion","kriging_fusion","gwr_fusion")){ |
252 | 291 |
png(file.path(out_path,paste("Bias_delta_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i], |
253 | 292 |
"_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))) |
254 |
|
|
255 |
bias_rast<-stack(clim_method_mod_obj[[index]]$bias) |
|
256 |
delta_rast<-raster(method_mod_obj[[index]]$delta) #only one delta image!!! |
|
293 |
##The path of production is not the same if input_path_tile is not NULL |
|
294 |
if(!is.null(in_path_tile)){ |
|
295 |
#infile_covariates <- file.path(in_path_tile,basename(list_param$covar_obj$infile_covariates)) |
|
296 |
bias_lf <- basename(as.character(clim_method_mod_obj[[index]]$bias)) #list of daily prediction files with path included |
|
297 |
bias_lf <- file.path(in_path_tile,bias_lf) |
|
298 |
delta_lf <- basename(unlist(method_mod_obj[[index]]$delta)) |
|
299 |
delta_lf <- file.path(in_path,delta_lf) |
|
300 |
}else{ |
|
301 |
bias_lf <- clim_method_mod_obj[[index]]$bias #list of daily prediction files with path included |
|
302 |
delta_lf <- method_mod_obj[[index]]$delta |
|
303 |
} |
|
304 |
|
|
305 |
bias_rast<-stack(bias_lf) |
|
306 |
delta_rast<-raster(delta_lf) #only one delta image!!! |
|
257 | 307 |
names(delta_rast)<-"delta" |
258 | 308 |
rast_temp_date<-stack(bias_rast,delta_rast) |
259 | 309 |
layers_names <- names(rast_temp_date) |
... | ... | |
263 | 313 |
plot(rast_temp_date) |
264 | 314 |
dev.off() |
265 | 315 |
} |
266 |
|
|
316 |
#if CAI |
|
267 | 317 |
if (interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){ |
268 | 318 |
png(file.path(out_path,paste("clim_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i], |
269 | 319 |
"_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))) |
270 |
|
|
271 |
clim_rast<-stack(clim_method_mod_obj[[index]]$clim) |
|
272 |
delta_rast<-raster(method_mod_obj[[index]]$delta) #only one delta image!!! |
|
273 |
names(delta_rast)<-"delta" |
|
274 |
layers_names <- c(names(clim_rast),"delta") |
|
275 |
rast_temp_date<-stack(clim_rast,delta_rast) |
|
276 |
rast_temp_date<-mask(rast_temp_date,LC_mask,file=file.path(out_path,"test.tif"),overwrite=TRUE) #loosing names here |
|
320 |
##The path of production is not the same if input_path_tile is not NULL |
|
321 |
if(!is.null(in_path_tile)){ |
|
322 |
#infile_covariates <- file.path(in_path_tile,basename(list_param$covar_obj$infile_covariates)) |
|
323 |
clim_lf <- basename(as.character(clim_method_mod_obj[[mo]]$clim)) #list of daily prediction files with path included |
|
324 |
clim_lf <- file.path(in_path_tile,clim_lf) |
|
325 |
delta_lf <- basename(unlist(method_mod_obj[[index]]$delta)) |
|
326 |
delta_lf <- file.path(in_path,delta_lf) |
|
327 |
}else{ |
|
328 |
clim_lf <- clim_method_mod_obj[[index]]$clim #list of monthly prediction files with path included |
|
329 |
delta_lf <- method_mod_obj[[index]]$delta |
|
330 |
} |
|
331 |
clim_rast<-stack(clim_lf) |
|
332 |
delta_rast<-stack(delta_lf) #this is a stack now... delta images!!! |
|
333 |
|
|
334 |
names(delta_rast)<- paste(names_mod,"_delta",sep="") |
|
335 |
names(clim_rast) <- paste(names_mod,"_month",mo,sep="") |
|
336 |
#layers_names <- c(names(clim_rast),"delta") |
|
337 |
#rast_temp_date<-stack(clim_rast,delta_rast) |
|
338 |
#rast_temp_date<-mask(rast_temp_date,LC_mask,file=file.path(out_path,"test.tif"),overwrite=TRUE) #loosing names here |
|
277 | 339 |
#bias_d_rast<-raster("fusion_bias_LST_20100103_30_1_10d_GAM_fus5_all_lstd_02082013.rst") |
278 |
names(rast_temp_date) <-layers_names |
|
279 |
plot(rast_temp_date) |
|
340 |
#names(rast_temp_date) <-layers_names |
|
341 |
#plot(rast_temp_date) |
|
342 |
plot(clim_rast) |
|
343 |
#title("Climatology for month ", mo, sep="") |
|
344 |
|
|
345 |
dev.off() |
|
280 | 346 |
|
347 |
png(file.path(out_path,paste("delta_surface_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i], |
|
348 |
"_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))) |
|
349 |
plot(delta_rast) |
|
350 |
dev.off() |
|
351 |
} |
|
352 |
|
|
353 |
### Figure 9: map of residuals... |
|
354 |
|
|
355 |
for (k in 1:length(names_mod)){ |
|
356 |
model_name <- names_mod[k] |
|
357 |
#fig_name <- file.path(out_path,paste("Figure_residuals_map_",y_var_name,"_",model_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_", |
|
358 |
# sampling_dat$run_samp,out_prefix,".png", sep="")) |
|
359 |
|
|
360 |
png(file.path(out_path,paste("Figure_residuals_map_",y_var_name,"_",model_name,"_",sampling_dat$date,"_",sampling_dat$prop,"_", |
|
361 |
sampling_dat$run_samp,out_prefix,".png", sep=""))) |
|
362 |
res_model_name <- paste("res",model_name,sep="_") |
|
363 |
elev <- subset(s_raster,"elev_s") |
|
364 |
p1 <- levelplot(elev,scales = list(draw = FALSE), colorkey = FALSE,col.regions=rev(terrain.colors(255))) |
|
365 |
#add legend.. |
|
366 |
cx <- ((data_v[[res_model_name]])^2)/10 |
|
367 |
p2 <- spplot(data_v,res_model_name, cex=1 * cx,main=paste("Residuals for ",res_model_name," ",datelabel,sep="")) |
|
368 |
p3 <-p2+p1+p2 #to force legend... |
|
369 |
#p2 |
|
370 |
print(p3) |
|
281 | 371 |
dev.off() |
282 | 372 |
} |
283 | 373 |
|
284 |
#Figure 9: histogram for all images... |
|
374 |
#Figure 9: histogram for all images/residuals...
|
|
285 | 375 |
## Add later...? distance to closest fitting station? |
286 | 376 |
|
287 | 377 |
#tb_diagnostic_v <- raster_prediction_obj$tb_diagnostic_v |
Also available in: Unified diff
global scalingup assessment modifications of stage 5 workflow code for diagnosis of issues