Revision 8a064626
Added by Benoit Parmentier over 8 years ago
climate/research/oregon/interpolation/NASA2016_conference_temperature_predictions.R | ||
---|---|---|
1 |
#################################### INTERPOLATION OF TEMPERATURES ####################################### |
|
2 |
####################### NASA 2016 Meeting: biodiversity and ecological forecasting ############################## |
|
3 |
#This script uses the worklfow code applied to the globe. Results currently reside on NEX/PLEIADES NASA. |
|
4 |
#Combining tables and figures for individual runs for years and tiles. |
|
5 |
#Figures and data for the AAG conference are also produced. |
|
6 |
#AUTHOR: Benoit Parmentier |
|
7 |
#CREATED ON: 05/01/2016 |
|
8 |
#MODIFIED ON: 05/01/2016 |
|
9 |
#Version: 1 |
|
10 |
#PROJECT: Environmental Layers project |
|
11 |
#COMMENTS: Initial commit, script based on part 2 of assessment, will be modified further for overall assessment |
|
12 |
#TODO: |
|
13 |
#1) Add plot broken down by year and region |
|
14 |
#2) Modify code for overall assessment accross all regions and year |
|
15 |
#3) Clean up |
|
16 |
|
|
17 |
#First source these files: |
|
18 |
#Resolved call issues from R. |
|
19 |
#source /nobackupp6/aguzman4/climateLayers/sharedModules2/etc/environ.sh |
|
20 |
# |
|
21 |
#setfacl -Rmd user:aguzman4:rwx /nobackupp8/bparmen1/output_run10_1500x4500_global_analyses_pred_1992_10052015 |
|
22 |
|
|
23 |
################################################################################################# |
|
24 |
|
|
25 |
#### FUNCTION USED IN SCRIPT |
|
26 |
|
|
27 |
#function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_07182014.R" #first interp paper |
|
28 |
#function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_08132014.R" |
|
29 |
#function_global_run_assessment_part2 <- "global_run_scalingup_assessment_part2_functions_0923015.R" |
|
30 |
|
|
31 |
############################################ |
|
32 |
#### Parameters and constants |
|
33 |
|
|
34 |
|
|
35 |
### Loading R library and packages |
|
36 |
#library used in the workflow production: |
|
37 |
library(gtools) # loading some useful tools |
|
38 |
library(mgcv) # GAM package by Simon Wood |
|
39 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
40 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
41 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
42 |
library(gstat) # Kriging and co-kriging by Pebesma et al. |
|
43 |
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines |
|
44 |
library(raster) # Hijmans et al. package for raster processing |
|
45 |
library(gdata) # various tools with xls reading, cbindX |
|
46 |
library(rasterVis) # Raster plotting functions |
|
47 |
library(parallel) # Parallelization of processes with multiple cores |
|
48 |
library(maptools) # Tools and functions for sp and other spatial objects e.g. spCbind |
|
49 |
library(maps) # Tools and data for spatial/geographic objects |
|
50 |
library(reshape) # Change shape of object, summarize results |
|
51 |
library(plotrix) # Additional plotting functions |
|
52 |
library(plyr) # Various tools including rbind.fill |
|
53 |
library(spgwr) # GWR method |
|
54 |
library(automap) # Kriging automatic fitting of variogram using gstat |
|
55 |
library(rgeos) # Geometric, topologic library of functions |
|
56 |
#RPostgreSQL # Interface R and Postgres, not used in this script |
|
57 |
library(gridExtra) |
|
58 |
#Additional libraries not used in workflow |
|
59 |
library(pgirmess) # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess} |
|
60 |
library(colorRamps) |
|
61 |
library(zoo) |
|
62 |
library(xts) |
|
63 |
|
|
64 |
###### Function used in the script ####### |
|
65 |
|
|
66 |
#script_path <- "/nobackupp8/bparmen1/env_layers_scripts" #path to script |
|
67 |
script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts" #path to script |
|
68 |
|
|
69 |
#Mosaic related |
|
70 |
#script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts" |
|
71 |
function_mosaicing_functions <- "global_run_scalingup_mosaicing_function_04232016.R" #PARAM12 |
|
72 |
function_mosaicing <-"global_run_scalingup_mosaicing_05012016.R" |
|
73 |
source(file.path(script_path,function_mosaicing)) #source all functions used in this script |
|
74 |
source(file.path(script_path,function_mosaicing_functions)) #source all functions used in this script |
|
75 |
|
|
76 |
#Assessment |
|
77 |
function_assessment_part1_functions <- "global_run_scalingup_assessment_part1_functions_02112015.R" #PARAM12 |
|
78 |
function_assessment_part1a <-"global_run_scalingup_assessment_part1a_01042016.R" |
|
79 |
function_assessment_part2 <- "global_run_scalingup_assessment_part2_02092016.R" |
|
80 |
function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R" |
|
81 |
function_assessment_part3 <- "global_run_scalingup_assessment_part3_04292016b.R" |
|
82 |
source(file.path(script_path,function_assessment_part1_functions)) #source all functions used in this script |
|
83 |
source(file.path(script_path,function_assessment_part1a)) #source all functions used in this script |
|
84 |
source(file.path(script_path,function_assessment_part2)) #source all functions used in this script |
|
85 |
source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script |
|
86 |
source(file.path(script_path,function_assessment_part3)) #source all functions used in this script |
|
87 |
|
|
88 |
### Parameters, constants and arguments ### |
|
89 |
|
|
90 |
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #constant 1 |
|
91 |
|
|
92 |
var<-"TMAX" # variable being interpolated #param 1, arg 1 |
|
93 |
|
|
94 |
##Add for precip later... |
|
95 |
if (var == "TMAX") { |
|
96 |
y_var_name <- "dailyTmax" |
|
97 |
y_var_month <- "TMax" |
|
98 |
} |
|
99 |
if (var == "TMIN") { |
|
100 |
y_var_name <- "dailyTmin" |
|
101 |
y_var_month <- "TMin" |
|
102 |
} |
|
103 |
|
|
104 |
#interpolation_method<-c("gam_fusion") #other otpions to be added later |
|
105 |
interpolation_method<-c("gam_CAI") #param 2 |
|
106 |
CRS_interp <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0" #param 3 |
|
107 |
#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"; |
|
108 |
|
|
109 |
out_region_name<-"" |
|
110 |
list_models<-c("y_var ~ s(lat,lon,k=5) + s(elev_s,k=3) + s(LST,k=3)") #param 4 |
|
111 |
|
|
112 |
#reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia) |
|
113 |
#master directory containing the definition of tile size and tiles predicted |
|
114 |
in_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/assessment" |
|
115 |
in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics" |
|
116 |
|
|
117 |
region_name <- c("reg4") #param 6, arg 3 |
|
118 |
|
|
119 |
create_out_dir_param <- TRUE #param 9, arg 6 |
|
120 |
out_suffix <- "_meeting_NASA_reg4_04292016" |
|
121 |
|
|
122 |
out_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/assessment" |
|
123 |
|
|
124 |
create_out_dir_param <- TRUE #param 9, arg |
|
125 |
|
|
126 |
#run_figure_by_year <- TRUE # param 10, arg 7 |
|
127 |
list_year_predicted <- "1984,2014" |
|
128 |
|
|
129 |
file_format <- ".tif" #format for mosaiced files # param 11 |
|
130 |
NA_flag_val <- -32768 #No data value, # param 12 |
|
131 |
#-32768 |
|
132 |
#num_cores <- 6 #number of cores used # param 13, arg 8 |
|
133 |
plotting_figures <- TRUE #running part2 of assessment to generate figures... # param 14 |
|
134 |
#num_cores <- args[8] #number of cores used # param 13, arg 8 |
|
135 |
num_cores <- 11 #number of cores used # param 13, arg 8 |
|
136 |
|
|
137 |
day_start <- "19990101" #PARAM 12 arg 12 |
|
138 |
day_end <- "19990103" #PARAM 13 arg 13 |
|
139 |
|
|
140 |
#infile_mask <- "/nobackupp8/bparmen1/NEX_data/regions_input_files/r_mask_LST_reg4.tif" |
|
141 |
infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg4.tif" |
|
142 |
|
|
143 |
#run_figure_by_year <- TRUE # param 10, arg 7 |
|
144 |
list_year_predicted <- "1984,2014" |
|
145 |
scaling <- 100 |
|
146 |
|
|
147 |
##################### START SCRIPT ################# |
|
148 |
|
|
149 |
####### PART 1: Read in data ######## |
|
150 |
out_dir <- in_dir |
|
151 |
if (create_out_dir_param == TRUE) { |
|
152 |
out_dir <- create_dir_fun(out_dir,out_suffix) |
|
153 |
setwd(out_dir) |
|
154 |
}else{ |
|
155 |
setwd(out_dir) #use previoulsy defined directory |
|
156 |
} |
|
157 |
|
|
158 |
setwd(out_dir) |
|
159 |
|
|
160 |
|
|
161 |
########### #################### |
|
162 |
|
|
163 |
pred_temp_s <- raster("/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990101_reg4_1999_m_gam_CAI_dailyTmax_19990101_reg4_1999.tif") |
|
164 |
|
|
165 |
if(mask_pred==TRUE){ |
|
166 |
|
|
167 |
r_mask <- raster(infile_mask) |
|
168 |
extension_str <- extension(filename(pred_temp_s )) |
|
169 |
raster_name_tmp <- |
|
170 |
gsub(extension_str,"",basename(filename(pred_temp_s ))) |
|
171 |
raster_name <- file.path(out_dir,paste(raster_name_tmp,"_masked.tif",sep = "")) |
|
172 |
r_pred <- mask(pred_temp_s,r_mask,filename = raster_name,overwrite = TRUE) |
|
173 |
|
|
174 |
} |
|
175 |
|
|
176 |
r_pred <- mask(pred_temp_s*1/scaling,r_mask,filename=) |
|
177 |
#predictions<-mask(predictions,mask_rast) |
|
178 |
|
|
179 |
|
|
180 |
NAvalue(pred_temp_s) <- NA_flag_val |
|
181 |
pred_temp_s <- setMinMax(pred_temp_s) |
|
182 |
|
|
183 |
|
|
184 |
#s.range <- c(min(minValue(pred_temp_s)), max(maxValue(pred_temp_s))) |
|
185 |
#s.range <- s.range+c(5,-5) |
|
186 |
#col.breaks <- pretty(s.range, n=200) |
|
187 |
#lab.breaks <- pretty(s.range, n=100) |
|
188 |
temp.colors <- colorRampPalette(c('blue', 'white', 'red')) |
|
189 |
max_val<-s.range[2] |
|
190 |
min_val <-s.range[1] |
|
191 |
#max_val<- -10 |
|
192 |
min_val <- 0 |
|
193 |
|
|
194 |
layout_m<-c(1,3) #one row two columns |
|
195 |
|
|
196 |
#png(paste("Figure7a__spatial_pattern_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
197 |
# height=480*layout_m[1],width=480*layout_m[2]) |
|
198 |
plot(pred_temp_s,col=temp.colors(255),zlim=c(-5000,5000)) |
|
199 |
plot(pred_temp_s,col=heat.colors(255),zlim=c(-5000,5000)) |
|
200 |
plot(pred_temp_s,zlim=c(-5000,5000)) |
|
201 |
date_proc <- l_dates[i] |
|
202 |
#paste(raster_name[1:7],collapse="_") |
|
203 |
#add filename option later |
|
204 |
|
|
205 |
res_pix <- 1200 |
|
206 |
#res_pix <- 480 |
|
207 |
|
|
208 |
col_mfrow <- 1 |
|
209 |
row_mfrow <- 1 |
|
210 |
|
|
211 |
png( |
|
212 |
filename = file.path( |
|
213 |
out_dir_str, |
|
214 |
paste( |
|
215 |
"Figure9_clim_mosaics_day_test","_",date_proc,"_",reg_name,"_",out_suffix,".png",sep = |
|
216 |
"" |
|
217 |
) |
|
218 |
), |
|
219 |
width = col_mfrow * res_pix,height = row_mfrow * res_pix |
|
220 |
) |
|
221 |
|
|
222 |
plot( |
|
223 |
r_pred,main = paste("Predicted on ",date_proc , " ", reg_name,sep = ""),cex.main = |
|
224 |
1.5 |
|
225 |
) |
|
226 |
dev.off() |
|
227 |
|
|
228 |
#col.regions=temp.colors(25)) |
|
229 |
#dev.off() |
|
230 |
|
|
231 |
############################ END OF SCRIPT ################################## |
Also available in: Unified diff
initial commit for code generating figures and analyses for NASA Biodiversity conference poster