Revision f8ba9fdc
Added by Benoit Parmentier about 11 years ago
climate/research/oregon/interpolation/multi_timescale_paper_interpolation.R | ||
---|---|---|
5 | 5 |
#Figures, tables and data for the paper are also produced in the script. |
6 | 6 |
#AUTHOR: Benoit Parmentier |
7 | 7 |
#CREATED ON: 10/31/2013 |
8 |
#MODIFIED ON: 11/08/2013
|
|
8 |
#MODIFIED ON: 11/15/2013
|
|
9 | 9 |
#Version: 1 |
10 | 10 |
#PROJECT: Environmental Layers project |
11 | 11 |
################################################################################################# |
... | ... | |
40 | 40 |
|
41 | 41 |
function_analyses_paper <-"contribution_of_covariates_paper_interpolation_functions_10152013.R" |
42 | 42 |
|
43 |
plot_transect_m2<-function(list_trans,r_stack,title_plot,disp=FALSE,m_layers){ |
|
44 |
#This function creates plot of transects for stack of raster images. |
|
45 |
#Arguments: |
|
46 |
#list_trans: list of files containing the transects lines in shapefile format |
|
47 |
#r_stack: raster stack containing the information to extect |
|
48 |
#title_plot: plot title |
|
49 |
#disp: display and save from X11 if TRUE or plot to png file if FALSE |
|
50 |
#m_layers: index for layerers containing alternate units to be drawned on a differnt scale |
|
51 |
#RETURN: |
|
52 |
#list containing transect information |
|
53 |
|
|
54 |
nb<-length(list_trans) |
|
55 |
t_col<-rainbow(nb) |
|
56 |
t_col<-c("red","green","black") |
|
57 |
lty_list<-c("dashed","solid","dotted") |
|
58 |
list_trans_data<-vector("list",nb) |
|
59 |
|
|
60 |
#For scale 1 |
|
61 |
for (i in 1:nb){ |
|
62 |
trans_file<-list_trans[[i]][1] |
|
63 |
filename<-sub(".shp","",trans_file) #Removing the extension from file. |
|
64 |
transect<-readOGR(dirname(filename), basename(filename)) #reading shapefile |
|
65 |
trans_data<-extract(r_stack, transect) |
|
66 |
if (disp==FALSE){ |
|
67 |
png(file=paste(list_trans[[i]]),".png",sep="") |
|
68 |
} |
|
69 |
#Plot layer values for specific transect |
|
70 |
for (k in 1:ncol(trans_data[[1]])){ |
|
71 |
y<-trans_data[[1]][,k] |
|
72 |
x<-1:length(y) |
|
73 |
m<-match(k,m_layers) |
|
74 |
|
|
75 |
if (k==1 & is.na(m)){ |
|
76 |
plot(x,y,type="l",xlab="transect distance from coastal origin (km)", ylab=" maximum temperature (degree C)", |
|
77 |
,cex=1.2,col=t_col[k]) |
|
78 |
#axis(2) |
|
79 |
} |
|
80 |
if (k==1 & !is.na(m)){ |
|
81 |
plot(x,y,type="l",col=t_col[k],lty="dotted",axes=F) #plotting fusion profile |
|
82 |
#axis(4,xlab="",ylab="elevation(m)") |
|
83 |
axis(4,cex=1.2) |
|
84 |
} |
|
85 |
if (k!=1 & is.na(m)){ |
|
86 |
#par(new=TRUE) # new plot without erasing old |
|
87 |
lines(x,y,type="l",xlab="",ylab="",col=t_col[k],axes=F) #plotting fusion profile |
|
88 |
#axis(2,xlab="",ylab="tmax (in degree C)") |
|
89 |
} |
|
90 |
if (k!=1 & !is.na(m)){ |
|
91 |
par(new=TRUE) # key: ask for new plot without erasing old |
|
92 |
plot(x,y,type="l",col=t_col[k],xlab="",ylab="",lty="dotted",axes=F) #plotting fusion profile |
|
93 |
#axis(4,xlab="",ylab="elevation(m)") |
|
94 |
axis(4,cex=1.2) |
|
95 |
} |
|
96 |
} |
|
97 |
title(title_plot[i]) |
|
98 |
legend("topleft",legend=layerNames(r_stack)[1:2], |
|
99 |
cex=1.2, col=t_col,lty=1,bty="n") |
|
100 |
legend("topright",legend=layerNames(r_stack)[3], |
|
101 |
cex=1.2, col=t_col[3],lty="dotted",bty="n") |
|
102 |
if (disp==TRUE){ |
|
103 |
savePlot(file=paste(list_trans[[i]][2],".png",sep=""),type="png") |
|
104 |
} |
|
105 |
if (disp==FALSE){ |
|
106 |
dev.off() |
|
107 |
} |
|
108 |
list_trans_data[[i]]<-trans_data |
|
109 |
} |
|
110 |
names(list_trans_data)<-names(list_trans) |
|
111 |
return(list_trans_data) |
|
112 |
} |
|
113 |
|
|
43 | 114 |
############################## |
44 | 115 |
#### Parameters and constants |
45 | 116 |
|
... | ... | |
83 | 154 |
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 |
84 | 155 |
y_var_name <- "dailyTmax" |
85 | 156 |
out_prefix<-"analyses_11082013" |
157 |
ref_rast_name<- "/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/mean_day244_rescaled.rst" #This is the shape file of outline of the study area. #local raster name defining resolution, exent, local projection--. set on the fly?? |
|
158 |
infile_reg_outline <- "/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/OR83M_state_outline.shp" #input region outline defined by polygon: Oregon |
|
159 |
ref_rast_name <-"/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/mean_day244_rescaled.rst" #local raster name defining resolution, exent: oregon |
|
160 |
ref_rast_d001 <-"/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/mean_day001_rescaled.rst" |
|
161 |
transect_list <-c("/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/transect_OR_1.shp", |
|
162 |
"/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/transect_OR_2.shp", |
|
163 |
"/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/transect_OR_3.shp") |
|
86 | 164 |
|
87 | 165 |
#method_interpolation <- "gam_daily" |
88 | 166 |
#covar_obj_file_1<- list.files(path=in_dir1,pattern="covar_obj.*.RData") |
... | ... | |
91 | 169 |
|
92 | 170 |
#Load objects containing training, testing, models objects |
93 | 171 |
|
94 |
met_stations_obj <- load_obj(file.path(in_dir1,met_obj_file_1) |
|
172 |
met_stations_obj <- load_obj(file.path(in_dir1,met_obj_file_1))
|
|
95 | 173 |
covar_obj <-load_obj(file.path(in_dir1,covar_obj_file_1)) #Reading covariates object for GAM daily method |
96 | 174 |
infile_covariates <- covar_obj$infile_covariates |
97 | 175 |
infile_reg_outline <- covar_obj$infile_reg_outline |
... | ... | |
188 | 266 |
#Figure 9: Image differencing and land cover |
189 | 267 |
|
190 | 268 |
################################################ |
191 |
######### Figure 1: Oregon study area |
|
269 |
######### Figure 1: Oregon study area, add labeling names to Willamette Valley an Mountain Ranges?
|
|
192 | 270 |
#3 parameters from output |
193 | 271 |
#infile_monthly<-list_outfiles$monthly_covar_ghcn_data #outile4 from database_covar script |
194 | 272 |
#infile_daily<-list_outfiles$daily_covar_ghcn_data #outfile3 from database_covar script |
... | ... | |
244 | 322 |
# Workflow figure is not generated in R |
245 | 323 |
|
246 | 324 |
################################################ |
247 |
######### Figure 3: daily mean compared to monthly mean |
|
325 |
######### Figure 3: LST averaging: daily mean compared to monthly mean |
|
326 |
|
|
327 |
### CREATE FIGURE MEAN DAILY AND MEAN MONTHLY: AAG 2013 #### |
|
328 |
|
|
329 |
lst_md<-raster(ref_rast_name) |
|
330 |
projection(lst_md)<-projection(s_raster) |
|
331 |
lst_mm_09<-subset(s_raster,"mm_09") |
|
332 |
|
|
333 |
lst_md<-raster(ref_rast_d001) |
|
334 |
lst_md<- lst_md - 273.16 |
|
335 |
lst_mm_01<-subset(s_raster,"mm_01") |
|
336 |
|
|
337 |
png(filename=paste("Comparison_daily_monthly_mean_lst",out_prefix,".png",sep=""),width=960,height=480) |
|
338 |
par(mfrow=c(1,2)) |
|
339 |
plot(lst_md) |
|
340 |
plot(interp_area,add=TRUE) |
|
341 |
title("Mean January 1") |
|
342 |
plot(lst_mm_01) |
|
343 |
plot(interp_area,add=TRUE) |
|
344 |
title("Mean for month of January") |
|
345 |
dev.off() |
|
248 | 346 |
|
249 | 347 |
################################################ |
250 | 348 |
######### Figure 4. RMSE multi-timescale mulitple hold out for FSS and CAI |
... | ... | |
255 | 353 |
################################################ |
256 | 354 |
######### Figure 6: Spatial pattern of prediction for one day (maps) |
257 | 355 |
|
356 |
y_var_name <-"dailyTmax" |
|
357 |
index<-244 #index corresponding to Sept 1 |
|
358 |
|
|
359 |
lf1 <- raster_prediction_obj_1$method_mod_obj[[index]][[y_var_name]] #select relevant raster images for the given dates |
|
360 |
lf4 <- raster_prediction_obj_4$method_mod_obj[[index]][[y_var_name]] |
|
361 |
lf7 <- raster_prediction_obj_7$method_mod_obj[[index]][[y_var_name]] |
|
362 |
|
|
363 |
date_selected <- "20109101" |
|
364 |
#methods_names <-c("gam","kriging","gwr") |
|
365 |
methods_names <-c("gam_daily","gam_CAI","gam_FSS") |
|
366 |
|
|
367 |
names_layers<-methods_names |
|
368 |
lf <- (list(lf1,lf4[1:7],lf7[1:7])) |
|
369 |
|
|
370 |
names_layers <-c("mod1 = lat*long","mod2 = lat*long + LST","mod3 = lat*long + elev","mod4 = lat*long + N_w*E_w", |
|
371 |
"mod5 = lat*long + elev + DISTOC","mod6 = lat*long + elev + LST","mod7 = lat*long + elev + LST*FOREST") |
|
372 |
nb_fig<- c("7a","7b","7c") |
|
373 |
for (i in 1:length(lf)){ |
|
374 |
pred_temp_s <-stack(lf[[i]]) |
|
375 |
|
|
376 |
#lf2 <- raster_prediction_obj_2$method_mod_obj[[index]][[y_var_name]] |
|
377 |
#lf2 #contains the models for gam |
|
378 |
|
|
379 |
#s.range <- c(min(minValue(pred_temp_s)), max(maxValue(pred_temp_s))) |
|
380 |
#s.range <- s.range+c(5,-5) |
|
381 |
#col.breaks <- pretty(s.range, n=200) |
|
382 |
#lab.breaks <- pretty(s.range, n=100) |
|
383 |
temp.colors <- colorRampPalette(c('blue', 'white', 'red')) |
|
384 |
#max_val<-s.range[2] |
|
385 |
#min_val <-s.range[1] |
|
386 |
max_val <- 40 |
|
387 |
min_val <- -10 |
|
388 |
layout_m<-c(2,4) #one row two columns |
|
389 |
|
|
390 |
png(paste("Figure_",nb_fig[i],"_spatial_pattern_tmax_prediction_models_gam_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
391 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
392 |
|
|
393 |
p <- levelplot(pred_temp_s,main="Interpolated Surfaces Model Comparison baseline 1", ylab=NULL,xlab=NULL, |
|
394 |
par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=layout_m, |
|
395 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
|
396 |
names.attr=names_layers,col.regions=temp.colors,at=seq(min_val,max_val,by=0.2)) |
|
397 |
#col.regions=temp.colors(25)) |
|
398 |
print(p) |
|
399 |
dev.off() |
|
400 |
} |
|
401 |
|
|
402 |
################################################ |
|
403 |
#Figure 7: Spatial transects for one day (maps) |
|
404 |
|
|
405 |
#######Figure 7a: Map of transects |
|
406 |
|
|
407 |
nb_transect<-3 |
|
408 |
list_transect2<-vector("list",nb_transect) |
|
409 |
rast_pred<-stack(lf[[2]]) #GAM_CAI |
|
410 |
rast_pred_selected<-subset(rast_pred,c(1,6)) #3 is referring to FSS, plot it first because it has the |
|
411 |
# the largest range. |
|
412 |
rast_pred2<-stack(rast_pred_selected,subset(s_raster,"elev_s")) |
|
413 |
|
|
414 |
#layers_names<-layerNames(rast_pred2)<-c("lat*lon","lat*lon + elev + LST","elev") |
|
415 |
layers_names<-layerNames(rast_pred2)<-c("mod1","mod6","elev") |
|
416 |
pos<-c(1,2) # postions in the layer prection |
|
417 |
transect_list |
|
418 |
list_transect2[[1]]<-c(transect_list[1],paste("figure_3_tmax_elevation_transect1_OR_",date_selected, |
|
419 |
paste("mod1_mod6",collapse="_"),out_prefix,sep="_")) |
|
420 |
list_transect2[[2]]<-c(transect_list[2],paste("figure_3_tmax_elevation_transect2_OR_",date_selected, |
|
421 |
paste("mod1_mod6",collapse="_"),out_prefix,sep="_")) |
|
422 |
list_transect2[[3]]<-c(transect_list[3],paste("figure_3_tmax_elevation_transect3_OR_",date_selected, |
|
423 |
paste("mod1_mod6",collapse="_"),out_prefix,sep="_")) |
|
424 |
|
|
425 |
names(list_transect2)<-c("transect_OR1","transect_OR2","transect_OR3") |
|
426 |
|
|
427 |
#X11(width=9,height=9) |
|
428 |
#png(paste("fig3_elevation_transect1_path_CAI_fusion_",date_selected,out_prefix,".png", sep="")) |
|
429 |
#plot(elev) |
|
430 |
#k<-1 #transect to plot |
|
431 |
#trans_file<-list_transect2[[k]][[1]] |
|
432 |
#filename<-sub(".shp","",trans_file) #Removing the extension from file. |
|
433 |
#transect<-readOGR(".", filename) #reading shapefile |
|
434 |
#plot(transect,add=TRUE) |
|
435 |
#title("Transect Oregon") |
|
436 |
#dev.off() |
|
437 |
|
|
438 |
layerNames(rast_pred2)<-layers_names |
|
439 |
title_plot2<-paste(names(list_transect2),date_selected,sep=" ") |
|
440 |
title_plot2<-paste(rep("Oregon transect on ",3), date_selected,sep="") |
|
441 |
#r_stack<-rast_pred |
|
442 |
m_layers_sc<-c(3) |
|
443 |
#title_plot2 |
|
444 |
#rast_pred2 |
|
445 |
debug(plot_transect_m2) |
|
446 |
trans_data2<-plot_transect_m2(list_transect2,rast_pred2,title_plot2,disp=FALSE,m_layers_sc) |
|
447 |
|
|
448 |
png(filename=paste("Comparison_daily_monthly_mean_lst",out_prefix,".png",sep=""),width=960,height=480) |
|
449 |
par(mfrow=c(1,2)) |
|
450 |
|
|
451 |
dev.off() |
|
258 | 452 |
|
453 |
|
|
259 | 454 |
###################### END OF SCRIPT ####################### |
260 | 455 |
|
261 | 456 |
|
Also available in: Unified diff
update script multi timescale paper draft, transects and additional figures for paper