Revision 0943e992
Added by Benoit Parmentier about 8 years ago
climate/research/oregon/interpolation/global_product_assessment_part2.R | ||
---|---|---|
1 |
#################################### INTERPOLATION OF TEMPERATURES ####################################### |
|
2 |
####################### Assessment of product part 1: mosaic and accuracy ############################## |
|
3 |
#This script uses the worklfow code applied to the globe. Results currently reside on NEX/PLEIADES NASA. |
|
4 |
#This part 2 of the assessment focuses on graphics to explore the spatial patterns of raster times series as figures and movie |
|
5 |
#AUTHOR: Benoit Parmentier |
|
6 |
#CREATED ON: 10/03/2016 |
|
7 |
#MODIFIED ON: 10/03/2016 |
|
8 |
#Version: 1 |
|
9 |
#PROJECT: Environmental Layers project |
|
10 |
#COMMENTS: Initial commit, script based on part NASA biodiversity conferenc |
|
11 |
#TODO: |
|
12 |
#1) Add plot broken down by year and region |
|
13 |
#2) Modify code for overall assessment accross all regions and year |
|
14 |
#3) Clean up |
|
15 |
|
|
16 |
#First source these files: |
|
17 |
#Resolved call issues from R. |
|
18 |
#source /nobackupp6/aguzman4/climateLayers/sharedModules2/etc/environ.sh |
|
19 |
# |
|
20 |
#setfacl -Rmd user:aguzman4:rwx /nobackupp8/bparmen1/output_run10_1500x4500_global_analyses_pred_1992_10052015 |
|
21 |
|
|
22 |
#COMMIT: plotting extracted predicted values and measured tmax |
|
23 |
|
|
24 |
################################################################################################# |
|
25 |
|
|
26 |
|
|
27 |
### Loading R library and packages |
|
28 |
#library used in the workflow production: |
|
29 |
library(gtools) # loading some useful tools |
|
30 |
library(mgcv) # GAM package by Simon Wood |
|
31 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
32 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
33 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
34 |
library(gstat) # Kriging and co-kriging by Pebesma et al. |
|
35 |
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines |
|
36 |
library(raster) # Hijmans et al. package for raster processing |
|
37 |
library(gdata) # various tools with xls reading, cbindX |
|
38 |
library(rasterVis) # Raster plotting functions |
|
39 |
library(parallel) # Parallelization of processes with multiple cores |
|
40 |
library(maptools) # Tools and functions for sp and other spatial objects e.g. spCbind |
|
41 |
library(maps) # Tools and data for spatial/geographic objects |
|
42 |
library(reshape) # Change shape of object, summarize results |
|
43 |
library(plotrix) # Additional plotting functions |
|
44 |
library(plyr) # Various tools including rbind.fill |
|
45 |
library(spgwr) # GWR method |
|
46 |
library(automap) # Kriging automatic fitting of variogram using gstat |
|
47 |
library(rgeos) # Geometric, topologic library of functions |
|
48 |
#RPostgreSQL # Interface R and Postgres, not used in this script |
|
49 |
library(gridExtra) |
|
50 |
#Additional libraries not used in workflow |
|
51 |
library(pgirmess) # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess} |
|
52 |
library(colorRamps) |
|
53 |
library(zoo) |
|
54 |
library(xts) |
|
55 |
library(lubridate) |
|
56 |
library(mosaic) |
|
57 |
|
|
58 |
###### Function used in the script ####### |
|
59 |
|
|
60 |
#script_path <- "/nobackupp8/bparmen1/env_layers_scripts" #path to script |
|
61 |
script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts" #path to script |
|
62 |
|
|
63 |
## NASA poster and paper related |
|
64 |
#source(file.path(script_path,"NASA2016_conference_temperature_predictions_function_05032016b.R")) |
|
65 |
|
|
66 |
#Mosaic related on NEX |
|
67 |
#script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts" |
|
68 |
function_mosaicing_functions <- "global_run_scalingup_mosaicing_function_08232016.R" #Functions used to mosaic predicted tiles |
|
69 |
function_mosaicing <-"global_run_scalingup_mosaicing_08222016.R" #main scripts for mosaicing predicted tiles |
|
70 |
|
|
71 |
source(file.path(script_path,function_mosaicing)) #source all functions used in this script |
|
72 |
source(file.path(script_path,function_mosaicing_functions)) #source all functions used in this script |
|
73 |
|
|
74 |
#Assessment on NEX |
|
75 |
function_assessment_part1_functions <- "global_run_scalingup_assessment_part1_functions_12282015.R" #PARAM12 |
|
76 |
function_assessment_part1a <-"global_run_scalingup_assessment_part1a_01042016.R" |
|
77 |
function_assessment_part2 <- "global_run_scalingup_assessment_part2_02092016.R" |
|
78 |
function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R" |
|
79 |
function_assessment_part3 <- "global_run_scalingup_assessment_part3_07292016.R" |
|
80 |
|
|
81 |
source(file.path(script_path,function_assessment_part1_functions)) #source all functions used in this script |
|
82 |
source(file.path(script_path,function_assessment_part1a)) #source all functions used in this script |
|
83 |
source(file.path(script_path,function_assessment_part2)) #source all functions used in this script |
|
84 |
source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script |
|
85 |
source(file.path(script_path,function_assessment_part3)) #source all functions used in this script |
|
86 |
|
|
87 |
#Product assessment |
|
88 |
function_product_assessment_part1_functions <- "global_product_assessment_part1_functions_09192016b.R" |
|
89 |
source(file.path(script_path,function_product_assessment_part1_functions)) #source all functions used in this script |
|
90 |
|
|
91 |
############################### |
|
92 |
####### Parameters, constants and arguments ### |
|
93 |
|
|
94 |
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #constant 1 |
|
95 |
|
|
96 |
var<-"TMAX" # variable being interpolated #param 1, arg 1 |
|
97 |
|
|
98 |
##Add for precip later... |
|
99 |
if (var == "TMAX") { |
|
100 |
y_var_name <- "dailyTmax" |
|
101 |
y_var_month <- "TMax" |
|
102 |
} |
|
103 |
if (var == "TMIN") { |
|
104 |
y_var_name <- "dailyTmin" |
|
105 |
y_var_month <- "TMin" |
|
106 |
} |
|
107 |
|
|
108 |
##Add for precip later... |
|
109 |
if (var == "TMAX") { |
|
110 |
variable_name <- "maximum temperature" |
|
111 |
} |
|
112 |
if (var == "TMIN") { |
|
113 |
variable_name <- "minimum temperature" |
|
114 |
} |
|
115 |
|
|
116 |
#interpolation_method<-c("gam_fusion") #other otpions to be added later |
|
117 |
interpolation_method<-c("gam_CAI") #param 2 |
|
118 |
CRS_interp <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0" #param 3 |
|
119 |
#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"; |
|
120 |
|
|
121 |
out_region_name<-"" |
|
122 |
list_models<-c("y_var ~ s(lat,lon,k=5) + s(elev_s,k=3) + s(LST,k=3)") #param 4 |
|
123 |
|
|
124 |
#reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia) |
|
125 |
#master directory containing the definition of tile size and tiles predicted |
|
126 |
#in_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/assessment" |
|
127 |
#in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/mosaic" |
|
128 |
in_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg1/assessment" |
|
129 |
in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg1/mosaics/mosaic" |
|
130 |
|
|
131 |
region_name <- c("reg1") #param 6, arg 3 |
|
132 |
out_suffix <- "_global_assessment_reg1_10032016" |
|
133 |
|
|
134 |
create_out_dir_param <- TRUE #param 9, arg 6 |
|
135 |
|
|
136 |
|
|
137 |
out_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg1/assessment" |
|
138 |
|
|
139 |
#run_figure_by_year <- TRUE # param 10, arg 7 |
|
140 |
|
|
141 |
file_format <- ".tif" #format for mosaiced files # param 11 |
|
142 |
NA_flag_val <- -32768 #No data value, # param 12 |
|
143 |
|
|
144 |
#num_cores <- 6 #number of cores used # param 13, arg 8 |
|
145 |
plotting_figures <- TRUE #running part2 of assessment to generate figures... # param 14 |
|
146 |
num_cores <- 11 #number of cores used # param 13, arg 8 |
|
147 |
#python_bin <- "/nobackupp6/aguzman4/climateLayers/sharedModules2/bin" #PARAM 30 |
|
148 |
python_bin <- "/usr/bin" #PARAM 30 |
|
149 |
|
|
150 |
day_start <- "1984101" #PARAM 12 arg 12 |
|
151 |
day_end <- "19991231" #PARAM 13 arg 13 |
|
152 |
#date_start <- day_start |
|
153 |
#date_end <- day_end |
|
154 |
|
|
155 |
#infile_mask <- "/nobackupp8/bparmen1/NEX_data/regions_input_files/r_mask_LST_reg4.tif" |
|
156 |
#infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg5.tif" |
|
157 |
infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg1.tif" |
|
158 |
|
|
159 |
#run_figure_by_year <- TRUE # param 10, arg 7 |
|
160 |
list_year_predicted <- "1984,2014" |
|
161 |
scaling <- 0.01 #was scaled on 100 |
|
162 |
#if scaling is null then perform no scaling!! |
|
163 |
|
|
164 |
#df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/output_reg5_1999/df_centroids_19990701_reg5_1999.txt" |
|
165 |
df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg1/mosaic/output_reg1_1984/df_centroids_19840101_reg1_1984.txt" |
|
166 |
#/nobackupp6/aguzman4/climateLayers/out/reg1/assessment//output_reg1_1984/df_assessment_files_reg1_1984_reg1_1984.txt |
|
167 |
|
|
168 |
#dates to plot and analyze |
|
169 |
|
|
170 |
#l_dates <- c("19990101","19990102","19990103","19990701","19990702","19990703") |
|
171 |
l_dates <- c("19990101","19990102","19990103","19990104","19990105") |
|
172 |
#df_points_extracted_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/data_points_extracted.txt" |
|
173 |
df_points_extracted_fname <- NULL #if null extract on the fly |
|
174 |
#r_mosaic_fname <- "r_mosaic.RData" |
|
175 |
r_mosaic_fname <- NULL #if null create a stack from input dir |
|
176 |
|
|
177 |
#NA_flag_val_mosaic <- -3399999901438340239948148078125514752.000 |
|
178 |
NA_flag_val_mosaic <- -32768 |
|
179 |
in_dir_list_filename <- NULL #if NULL, use the in_dir directory to search for info |
|
180 |
countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #Atlas |
|
181 |
|
|
182 |
|
|
183 |
##################### START SCRIPT ################# |
|
184 |
|
|
185 |
####### PART 1: Read in data ######## |
|
186 |
out_dir <- in_dir |
|
187 |
if (create_out_dir_param == TRUE) { |
|
188 |
out_dir <- create_dir_fun(out_dir,out_suffix) |
|
189 |
setwd(out_dir) |
|
190 |
}else{ |
|
191 |
setwd(out_dir) #use previoulsy defined directory |
|
192 |
} |
|
193 |
|
|
194 |
setwd(out_dir) |
|
195 |
|
|
196 |
########### #################### |
|
197 |
|
|
198 |
|
|
199 |
#https://www.r-bloggers.com/animated-plots-with-r/ |
|
200 |
|
|
201 |
|
|
202 |
if(is.null(lf_raster)){ |
|
203 |
|
|
204 |
#pattern_str <- ".*.tif" |
|
205 |
pattern_str <-"*.tif" |
|
206 |
lf_raster <- list.files(path=in_dir_mosaic,pattern=pattern_str,recursive=F,full.names=T) |
|
207 |
r_stack <- stack(lf_raster,quick=T) #this is very fast now with the quick option! |
|
208 |
#save(r_mosaic,file="r_mosaic.RData") |
|
209 |
|
|
210 |
}else{ |
|
211 |
r_stack <- stack(lf_raster,quick=T) #this is very fast now with the quick option! |
|
212 |
} |
|
213 |
|
|
214 |
|
|
215 |
############### PART5: Make raster stack and display maps ############# |
|
216 |
#### Extract corresponding raster for given dates and plot stations used |
|
217 |
|
|
218 |
## TODO: make movies from time series in png |
|
219 |
|
|
220 |
#start_date <- day_to_mosaic_range[1] |
|
221 |
#end_date <- day_to_mosaic_range[2] |
|
222 |
#start_date <- day_start #PARAM 12 arg 12 |
|
223 |
#end_date <- day_end #PARAM 13 arg 13 |
|
224 |
|
|
225 |
#date_to_plot <- seq(as.Date(strptime(start_date,"%Y%m%d")), as.Date(strptime(end_date,"%Y%m%d")), 'day') |
|
226 |
#l_dates <- format(date_to_plot,"%Y%m%d") #format back to the relevant date format for files |
|
227 |
#mask_pred <- FALSE |
|
228 |
#matching <- FALSE #to be added after mask_pred option |
|
229 |
#list_param_pre_process <- list(raster_name_lf,python_bin,infile_mask,scaling,mask_pred,NA_flag_val,out_suffix,out_dir) |
|
230 |
#names(list_param_pre_process) <- c("lf","python_bin","infile_mask","scaling","mask_pred","NA_flag_val","out_suffix","out_dir") |
|
231 |
|
|
232 |
#debug(pre_process_raster_mosaic_fun) |
|
233 |
|
|
234 |
#lf_mosaic_scaled <- mclapply(1:length(raster_name_lf),FUN=pre_process_raster_mosaic_fun,list_param=list_param_pre_process,mc.preschedule=FALSE,mc.cores = num_cores) |
|
235 |
#lf_mosaic_scaled <- mclapply(1:length(raster_name_lf),FUN=pre_process_raster_mosaic_fun,list_param=list_param_pre_process,mc.preschedule=FALSE,mc.cores = num_cores) |
|
236 |
|
|
237 |
#test <- pre_process_raster_mosaic_fun(2,list_param_pre_process) |
|
238 |
#lf_mosaic_scaled <- unlist(lf_mosaic_scaled) |
|
239 |
|
|
240 |
##################################### PART 5 ###### |
|
241 |
##### Plotting specific days for the mosaics |
|
242 |
|
|
243 |
r_mosaic_scaled <- stack(lf_mosaic_scaled) |
|
244 |
NAvalue(r_mosaic_scaled)<- -3399999901438340239948148078125514752.000 |
|
245 |
plot(r_mosaic_scaled,y=6,zlim=c(-50,50)) |
|
246 |
plot(r_mosaic_scaled,zlim=c(-50,50),col=matlab.like(255)) |
|
247 |
|
|
248 |
#layout_m<-c(1,3) #one row two columns |
|
249 |
#levelplot(r_mosaic_scaled,zlim=c(-50,50),col.regions=matlab.like(255)) |
|
250 |
#levelplot(r_mosaic_scaled,zlim=c(-50,50),col.regions=matlab.like(255)) |
|
251 |
|
|
252 |
#png(paste("Figure7a__spatial_pattern_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
253 |
# height=480*layout_m[1],width=480*layout_m[2]) |
|
254 |
#plot(r_pred,col=temp.colors(255),zlim=c(-3500,4500)) |
|
255 |
#plot(r_pred,col=matlab.like(255),zlim=c(-40,50)) |
|
256 |
#paste(raster_name[1:7],collapse="_") |
|
257 |
#add filename option later |
|
258 |
|
|
259 |
#NA_flag_val_mosaic <- -3399999901438340239948148078125514752.000 |
|
260 |
|
|
261 |
list_param_plot_raster_mosaic <- list(l_dates,r_mosaic_scaled,NA_flag_val_mosaic,out_dir,out_suffix, |
|
262 |
region_name,variable_name) |
|
263 |
names(list_param_plot_raster_mosaic) <- c("l_dates","r_mosaic_scaled","NA_flag_val_mosaic","out_dir","out_suffix", |
|
264 |
"region_name","variable_name") |
|
265 |
|
|
266 |
lf_mosaic_plot_fig <- mclapply(1:length(lf_mosaic_scaled),FUN=plot_raster_mosaic,list_param=list_param_plot_raster_mosaic,mc.preschedule=FALSE,mc.cores = num_cores) |
|
267 |
|
|
268 |
#### PLOT ACCURACY METRICS: First test #### |
|
269 |
##this will be cleaned up later: |
|
270 |
|
|
271 |
#dir_ac_mosaics <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/output_reg4_1999" |
|
272 |
lf_tmp <-list.files(path=dir_ac_mosaics,pattern="r_m_use_edge_weights_weighted_mean_mask_gam_CAI_.*.ac.*._reg4_1999.tif") |
|
273 |
|
|
274 |
#lf_tmp1 <- lf_tmp[21:24] |
|
275 |
#list_param_plot_raster_mosaic |
|
276 |
lf_tmp <-list.files(path=dir_ac_mosaics,pattern="r_m_use_edge_weights_weighted_mean_mask_gam_CAI_.*.ac.*._reg4_1999.tif",full.names=T) |
|
277 |
#Product assessment |
|
278 |
#function_product_assessment_part1_functions <- "global_product_assessment_part1_functions_06142016b.R" |
|
279 |
#source(file.path(script_path,function_product_assessment_part1_functions)) #source all functions used in this script |
|
280 |
|
|
281 |
r_mosaiced_ac <- stack(lf_tmp) |
|
282 |
l_dates <- unlist(lapply(1:length(lf_tmp),FUN=extract_date,x=basename(lf_tmp),item_no=14)) |
|
283 |
variable_name |
|
284 |
zlim_val <- NULL |
|
285 |
list_param_plot_raster_mosaic <- list(l_dates,r_mosaiced_ac,NA_flag_val_mosaic,out_dir,out_suffix, |
|
286 |
region_name,variable_name, zlim_val) |
|
287 |
names(list_param_plot_raster_mosaic) <- c("l_dates","r_mosaiced_scaled","NA_flag_val_mosaic","out_dir","out_suffix", |
|
288 |
"region_name","variable_name","zlim_val") |
|
289 |
#debug(plot_raster_mosaic) |
|
290 |
plot_raster_mosaic(1,list_param_plot_raster_mosaic) |
|
291 |
lf_mosaic_plot_fig <- mclapply(1:length(lf_tmp), |
|
292 |
FUN=plot_raster_mosaic, |
|
293 |
list_param=list_param_plot_raster_mosaic, |
|
294 |
mc.preschedule=FALSE, |
|
295 |
mc.cores = num_cores) |
|
296 |
|
|
297 |
#### Now plot kriged residuals from mosaiced surfaces |
|
298 |
|
|
299 |
lf_tmp_res <-list.files(path=dir_ac_mosaics,pattern="r_m_use_edge_weights_weighted_mean_mask_gam_CAI_.*.residuals.*._reg4_1999.tif",full.names=T) |
|
300 |
|
|
301 |
l_dates <- unlist(lapply(1:length(lf_tmp_res),FUN=extract_date,x=basename(lf_tmp),item_no=14)) |
|
302 |
variable_name |
|
303 |
zlim_val <- NULL |
|
304 |
r_mosaiced_res <- stack(lf_tmp_res) |
|
305 |
list_param_plot_raster_mosaic <- list(l_dates,r_mosaiced_res,NA_flag_val_mosaic,out_dir,out_suffix, |
|
306 |
region_name,variable_name, zlim_val) |
|
307 |
names(list_param_plot_raster_mosaic) <- c("l_dates","r_mosaiced_scaled","NA_flag_val_mosaic","out_dir","out_suffix", |
|
308 |
"region_name","variable_name","zlim_val") |
|
309 |
#debug(plot_raster_mosaic) |
|
310 |
plot_raster_mosaic(1,list_param_plot_raster_mosaic) |
|
311 |
lf_mosaic_plot_fig_res <- mclapply(1:length(lf_tmp_res), |
|
312 |
FUN=plot_raster_mosaic, |
|
313 |
list_param=list_param_plot_raster_mosaic, |
|
314 |
mc.preschedule=FALSE, |
|
315 |
mc.cores = num_cores) |
|
316 |
|
|
317 |
### New plot of residuals surface with zlim |
|
318 |
zlim_val <- c(-60,60) |
|
319 |
#r_mosaiced_res <- stack(lf_tmp_res) |
|
320 |
list_param_plot_raster_mosaic <- list(l_dates,r_mosaiced_res,NA_flag_val_mosaic,out_dir,out_suffix, |
|
321 |
region_name,variable_name, zlim_val) |
|
322 |
names(list_param_plot_raster_mosaic) <- c("l_dates","r_mosaiced_scaled","NA_flag_val_mosaic","out_dir","out_suffix", |
|
323 |
"region_name","variable_name","zlim_val") |
|
324 |
#debug(plot_raster_mosaic) |
|
325 |
#plot_raster_mosaic(1,list_param_plot_raster_mosaic) |
|
326 |
lf_mosaic_plot_fig_res <- mclapply(1:length(lf_tmp_res), |
|
327 |
FUN=plot_raster_mosaic, |
|
328 |
list_param=list_param_plot_raster_mosaic, |
|
329 |
mc.preschedule=FALSE, |
|
330 |
mc.cores = num_cores) |
|
331 |
|
|
332 |
|
|
333 |
############################ END OF SCRIPT ################################## |
Also available in: Unified diff
script global product assessment part 2: initial commit