Revision 3ff91d1a
Added by Benoit Parmentier over 8 years ago
climate/research/oregon/interpolation/global_run_scalingup_assessment_part5.R | ||
---|---|---|
1 |
############################## INTERPOLATION OF TEMPERATURES ####################################### |
|
2 |
####################### Script for assessment of scaling up on NEX : part5 ############################## |
|
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 |
#This script complements part1 and part2 of the accuracy assessment and group tables and outputs |
|
6 |
#from run of accuracy assessement generated earlier. |
|
7 |
#It focuses on additional analyses, figures, tables of accuracy values. In particular, extreme values by tiles. |
|
8 |
#AUTHOR: Benoit Parmentier |
|
9 |
#CREATED ON: 02/25/2016 |
|
10 |
#MODIFIED ON: 02/28/2016 |
|
11 |
#Version: 5 |
|
12 |
#PROJECT: Environmental Layers project |
|
13 |
#COMMENTS: Initial commit, script based on part 3 of assessment, will be modified further for overall assessment |
|
14 |
#TODO: |
|
15 |
#1) |
|
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 |
#on ATLAS |
|
35 |
#in_dir1 <- "/data/project/layers/commons/NEX_data/test_run1_03232014/output" #On Atlas |
|
36 |
#parent output dir : contains subset of the data produced on NEX |
|
37 |
#in_dir1 <- "/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/output20Deg2" |
|
38 |
# parent output dir for the curent script analyes |
|
39 |
#out_dir <-"/data/project/layers/commons/NEX_data/output_run3_global_analyses_06192014/" #On NCEAS Atlas |
|
40 |
# input dir containing shapefiles defining tiles |
|
41 |
#in_dir_shp <- "/data/project/layers/commons/NEX_data/output_run5_global_analyses_08252014/output/subset/shapefiles" |
|
42 |
|
|
43 |
#On NEX |
|
44 |
#contains all data from the run by Alberto |
|
45 |
#in_dir1 <- " /nobackupp6/aguzman4/climateLayers/out_15x45/" #On NEX |
|
46 |
#parent output dir for the current script analyes |
|
47 |
#out_dir <- "/nobackup/bparmen1/" #on NEX |
|
48 |
#in_dir_shp <- "/nobackupp4/aguzman4/climateLayers/output4/subset/shapefiles/" |
|
49 |
|
|
50 |
#in_dir <- "/data/project/layers/commons/NEX_data/reg4_assessment" |
|
51 |
#list_in_dir_run <- |
|
52 |
#in_dir_list <- c("output_run_global_analyses_pred_2009_reg4","output_run_global_analyses_pred_2010_reg4", |
|
53 |
# "output_run_global_analyses_pred_2011_reg4","output_run_global_analyses_pred_2012_reg4", |
|
54 |
# "output_run_global_analyses_pred_2013_reg4","output_run_global_analyses_pred_2014_reg4") |
|
55 |
#in_dir_list_filename <- "/data/project/layers/commons/NEX_data/reg4_assessment/stage6_reg4_in_dir_list_02072016.txt" |
|
56 |
#in_dir <- "" #PARAM 0 |
|
57 |
#y_var_name <- "dailyTmax" #PARAM1 |
|
58 |
#interpolation_method <- c("gam_CAI") #PARAM2 |
|
59 |
#out_suffix <- "global_analyses_overall_assessment_reg4_02072016" |
|
60 |
#out_suffix <- "output_run10_1000x3000_global_analyses_02102015" |
|
61 |
#out_suffix <- "run10_1500x4500_global_analyses_pred_1992_10052015" #PARAM3 |
|
62 |
#out_dir <- "/data/project/layers/commons/NEX_data/output_run10_1500x4500_global_analyses_pred_1992_10052015" #PARAM4 |
|
63 |
#create_out_dir_param <- TRUE #PARAM 5 |
|
64 |
#mosaic_plot <- FALSE #PARAM6 |
|
65 |
#if daily mosaics NULL then mosaicas all days of the year |
|
66 |
#day_to_mosaic <- c("19920101","19920102","19920103") #PARAM7 |
|
67 |
#CRS_WGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 #CONSTANT1 |
|
68 |
#CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 |
|
69 |
#proj_str<- CRS_WGS84 #PARAM 8 #check this parameter |
|
70 |
#file_format <- ".rst" #PARAM 9 |
|
71 |
#NA_value <- -9999 #PARAM10 |
|
72 |
#NA_flag_val <- NA_value |
|
73 |
#multiple_region <- TRUE #PARAM 12 |
|
74 |
#region_name <- "world" #PARAM 13 |
|
75 |
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #PARAM 13, copy this on NEX too |
|
76 |
#plot_region <- TRUE |
|
77 |
#num_cores <- 6 #PARAM 14 |
|
78 |
#region_name <- c("reg4") #reference region to merge if necessary, if world all the regions are together #PARAM 16 |
|
79 |
#use previous files produced in step 1a and stored in a data.frame |
|
80 |
#df_assessment_files <- "df_assessment_files_reg4_1984_run_global_analyses_pred_12282015.txt" #PARAM 17 |
|
81 |
#threshold_missing_day <- c(367,365,300,200) #PARM18 |
|
82 |
|
|
83 |
#list_param_run_assessment_plottingin_dir <- list(in_dir,y_var_name, interpolation_method, out_suffix, |
|
84 |
# out_dir, create_out_dir_param, mosaic_plot, proj_str, file_format, NA_value, |
|
85 |
# multiple_region, countries_shp, plot_region, num_cores, |
|
86 |
# region_name, df_assessment_files, threshold_missing_day) |
|
87 |
|
|
88 |
#names(list_param_run_assessment_plottingin_dir) <- c("in_dir","y_var_name","interpolation_method","out_suffix", |
|
89 |
# "out_dir","create_out_dir_param","mosaic_plot","proj_str","file_format","NA_value", |
|
90 |
# "multiple_region","countries_shp","plot_region","num_cores", |
|
91 |
# "region_name","df_assessment_files","threshold_missing_day") |
|
92 |
|
|
93 |
#run_assessment_plotting_prediction_fun(list_param_run_assessment_plottingin_dir) |
|
94 |
|
|
95 |
run_assessment_combined_region_plotting_prediction_fun <-function(list_param_run_assessment_plotting){ |
|
96 |
|
|
97 |
#### |
|
98 |
#1) in_dir: input directory containing data tables and shapefiles for plotting #PARAM 0 |
|
99 |
#2) y_var_name : variables being predicted e.g. dailyTmax #PARAM1 |
|
100 |
#3) interpolation_method: method used #c("gam_CAI") #PARAM2 |
|
101 |
#4) out_suffix: output suffix #PARAM3 |
|
102 |
#5) out_dir # |
|
103 |
#6) create_out_dir_param # FALSE #PARAM 5 |
|
104 |
#7) mosaic_plot #FALSE #PARAM6 |
|
105 |
#8) proj_str # projection/coordinates system e.g. CRS_WGS84 #PARAM 8 #check this parameter |
|
106 |
#9) file_format #".rst" #PARAM 9 |
|
107 |
#10) NA_value #-9999 #PARAM10 |
|
108 |
#11) multiple_region # <- TRUE #PARAM 12 |
|
109 |
#12) countries_shp #<- "world" #PARAM 13 |
|
110 |
#13) plot_region #<- TRUE |
|
111 |
#14) num_cores <- number of cores used # 6 #PARAM 14 |
|
112 |
#15) region_name #<- c("reg4"), world if full assessment #reference region to merge if necessary #PARAM 16 |
|
113 |
#16) df_assessment_files #PARAM 16 |
|
114 |
#17) threshold_missing_day #PARM18 |
|
115 |
#18) year_predicted |
|
116 |
|
|
117 |
### Loading R library and packages |
|
118 |
#library used in the workflow production: |
|
119 |
library(gtools) # loading some useful tools |
|
120 |
library(mgcv) # GAM package by Simon Wood |
|
121 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
122 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
123 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
124 |
library(gstat) # Kriging and co-kriging by Pebesma et al. |
|
125 |
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines |
|
126 |
library(raster) # Hijmans et al. package for raster processing |
|
127 |
library(gdata) # various tools with xls reading, cbindX |
|
128 |
library(rasterVis) # Raster plotting functions |
|
129 |
library(parallel) # Parallelization of processes with multiple cores |
|
130 |
library(maptools) # Tools and functions for sp and other spatial objects e.g. spCbind |
|
131 |
library(maps) # Tools and data for spatial/geographic objects |
|
132 |
library(reshape) # Change shape of object, summarize results |
|
133 |
library(plotrix) # Additional plotting functions |
|
134 |
library(plyr) # Various tools including rbind.fill |
|
135 |
library(spgwr) # GWR method |
|
136 |
library(automap) # Kriging automatic fitting of variogram using gstat |
|
137 |
library(rgeos) # Geometric, topologic library of functions |
|
138 |
#RPostgreSQL # Interface R and Postgres, not used in this script |
|
139 |
library(gridExtra) |
|
140 |
#Additional libraries not used in workflow |
|
141 |
library(pgirmess) # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess} |
|
142 |
library(colorRamps) |
|
143 |
library(zoo) |
|
144 |
library(xts) |
|
145 |
|
|
146 |
####### Function used in the script ####### |
|
147 |
|
|
148 |
#script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts" |
|
149 |
#function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R" |
|
150 |
#source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script |
|
151 |
|
|
152 |
####### PARSE INPUT ARGUMENTS/PARAMETERS ##### |
|
153 |
in_dir_list_filename <- list_param_run_assessment_plotting$in_dir_list_filename #PARAM 0 |
|
154 |
in_dir <- list_param_run_assessment_plotting$in_dir #PARAM 1 |
|
155 |
y_var_name <- list_param_run_assessment_plotting$y_var_name #PARAM2 |
|
156 |
interpolation_method <- list_param_run_assessment_plotting$interpolation_method #c("gam_CAI") #PARAM3 |
|
157 |
out_suffix <- list_param_run_assessment_plotting$out_suffix #PARAM4 |
|
158 |
out_dir <- list_param_run_assessment_plotting$out_dir # PARAM5 |
|
159 |
create_out_dir_param <- list_param_run_assessment_plotting$create_out_dir_param # FALSE #PARAM 6 |
|
160 |
mosaic_plot <- list_param_run_assessment_plotting$mosaic_plot #FALSE #PARAM7 |
|
161 |
proj_str<- list_param_run_assessment_plotting$proj_str #CRS_WGS84 #PARAM 8 #check this parameter |
|
162 |
file_format <- list_param_run_assessment_plotting$file_format #".rst" #PARAM 9 |
|
163 |
NA_flag_val <- list_param_run_assessment_plotting$NA_flag_val #-9999 #PARAM10 |
|
164 |
multiple_region <- list_param_run_assessment_plotting$multiple_region # <- TRUE #PARAM 11 |
|
165 |
countries_shp <- list_param_run_assessment_plotting$countries_shp #<- "world" #PARAM 12 |
|
166 |
plot_region <- list_param_run_assessment_plotting$plot_region # PARAM13 |
|
167 |
num_cores <- list_param_run_assessment_plotting$num_cores # 6 #PARAM 14 |
|
168 |
region_name <- list_param_run_assessment_plotting$region_name #<- "world" #PARAM 15 |
|
169 |
#df_assessment_files_name <- list_param_run_assessment_plotting$df_assessment_files_name #PARAM 16 |
|
170 |
threshold_missing_day <- list_param_run_assessment_plotting$threshold_missing_day #PARM17 |
|
171 |
year_predicted <- list_param_run_assessment_plotting$year_predicted |
|
172 |
|
|
173 |
NA_value <- NA_flag_val |
|
174 |
metric_name <- "rmse" #to be added to the code later... |
|
175 |
|
|
176 |
##################### START SCRIPT ################# |
|
177 |
|
|
178 |
####### PART 1: Read in data ######## |
|
179 |
out_dir <- in_dir |
|
180 |
if(create_out_dir_param==TRUE){ |
|
181 |
out_dir <- create_dir_fun(out_dir,out_suffix) |
|
182 |
setwd(out_dir) |
|
183 |
}else{ |
|
184 |
setwd(out_dir) #use previoulsy defined directory |
|
185 |
} |
|
186 |
|
|
187 |
setwd(out_dir) |
|
188 |
|
|
189 |
list_outfiles <- vector("list", length=35) #collect names of output files, this should be dynamic? |
|
190 |
list_outfiles_names <- vector("list", length=35) #collect names of output files |
|
191 |
counter_fig <- 0 #index of figure to collect outputs |
|
192 |
|
|
193 |
#i <- year_predicted |
|
194 |
###Table 1: Average accuracy metrics |
|
195 |
###Table 2: daily accuracy metrics for all tiles |
|
196 |
|
|
197 |
in_dir_list <- as.list(read.table(in_dir_list_filename,stringsAsFactors=F)[,1]) |
|
198 |
|
|
199 |
##Read in data list from in_dir_list |
|
200 |
list_tb_fname <- list.files(path=file.path(in_dir,in_dir_list),"tb_diagnostic_v_NA_.*.txt",full.names=T) |
|
201 |
list_df_fname <- list.files(path=file.path(in_dir,in_dir_list),"df_tile_processed_.*..txt",full.names=T) |
|
202 |
list_summary_metrics_v_fname <- list.files(path=file.path(in_dir,in_dir_list),"summary_metrics_v2_NA_.*.txt",full.names=T) |
|
203 |
list_tb_s_fname <- list.files(path=file.path(in_dir,in_dir_list),"tb_diagnostic_s_NA.*.txt",full.names=T) |
|
204 |
list_tb_month_s_fname <- list.files(path=file.path(in_dir,in_dir_list),"tb_month_diagnostic_s.*.txt",full.names=T) |
|
205 |
list_data_month_s_fname <- list.files(path=file.path(in_dir,in_dir_list),"data_month_s.*.txt",full.names=T) |
|
206 |
list_data_s_fname <- list.files(path=file.path(in_dir,in_dir_list),"data_day_s.*.txt",full.names=T) |
|
207 |
list_data_v_fname <- list.files(path=file.path(in_dir,in_dir_list),"data_day_v.*.txt",full.names=T) |
|
208 |
list_pred_data_month_info_fname <- list.files(path=file.path(in_dir,in_dir_list),"pred_data_month_info.*.txt",full.names=T) |
|
209 |
list_pred_data_day_info_fname <- list.files(path=file.path(in_dir,in_dir_list),"pred_data_day_info.*.txt",full.names=T) |
|
210 |
|
|
211 |
#need to fix this !! has all of the files in one list (for a region) |
|
212 |
#list_shp <- list.files(path=file.path(in_dir,file.path(in_dir_list,"shapefiles")),"*.shp",full.names=T) |
|
213 |
|
|
214 |
## Step 2: only read what is necessary at this stage... |
|
215 |
list_tb <- lapply(list_tb_fname,function(x){read.table(x,stringsAsFactors=F,sep=",")}) |
|
216 |
tb <- do.call(rbind,list_tb) |
|
217 |
list_tb_s <- lapply(list_tb_s_fname,function(x){read.table(x,stringsAsFactors=F,sep=",")}) |
|
218 |
tb_s <- do.call(rbind,list_tb_s) |
|
219 |
|
|
220 |
list_df_tile_processed <- lapply(list_df_fname,function(x){read.table(x,stringsAsFactors=F,sep=",")}) |
|
221 |
df_tile_processed <- do.call(rbind,list_df_tile_processed) |
|
222 |
list_summary_metrics_v <- lapply(list_summary_metrics_v_fname,function(x){read.table(x,stringsAsFactors=F,sep=",")}) |
|
223 |
summary_metrics_v <- do.call(rbind,list_summary_metrics_v) |
|
224 |
|
|
225 |
list_tb_month_s <- lapply(list_tb_month_s_fname,function(x){read.table(x,stringsAsFactors=F,sep=",")}) |
|
226 |
tb_month_s <- do.call(rbind,list_tb_month_s) |
|
227 |
|
|
228 |
list_tb_data_s <- lapply(list_data_s_fname,function(x){read.table(x,stringsAsFactors=F,sep=",")}) |
|
229 |
tb_data_s <- do.call(rbind,list_tb_data_s) |
|
230 |
|
|
231 |
list_tb_data_v <- lapply(list_data_v_fname,function(x){read.table(x,stringsAsFactors=F,sep=",")}) |
|
232 |
tb_data_v <- do.call(rbind,list_tb_data_v) |
|
233 |
|
|
234 |
##Stop added |
|
235 |
##Screen for non shapefiles tiles due to dir |
|
236 |
df_tile_processed <- df_tile_processed[!is.na(df_tile_processed$shp_files),] |
|
237 |
|
|
238 |
#add column for tile size later on!!! |
|
239 |
|
|
240 |
tb$pred_mod <- as.character(tb$pred_mod) |
|
241 |
summary_metrics_v$pred_mod <- as.character(summary_metrics_v$pred_mod) |
|
242 |
summary_metrics_v$tile_id <- as.character(summary_metrics_v$tile_id) |
|
243 |
df_tile_processed$tile_id <- as.character(df_tile_processed$tile_id) |
|
244 |
|
|
245 |
tb_month_s$pred_mod <- as.character(tb_month_s$pred_mod) |
|
246 |
tb_month_s$tile_id<- as.character(tb_month_s$tile_id) |
|
247 |
tb_s$pred_mod <- as.character(tb_s$pred_mod) |
|
248 |
tb_s$tile_id <- as.character(tb_s$tile_id) |
|
249 |
|
|
250 |
#multiple regions? #this needs to be included in the previous script!!! |
|
251 |
#if(multiple_region==TRUE){ |
|
252 |
df_tile_processed$reg <- as.character(df_tile_processed$reg) |
|
253 |
tb <- merge(tb,df_tile_processed,by="tile_id") |
|
254 |
tb_s <- merge(tb_s,df_tile_processed,by="tile_id") |
|
255 |
tb_month_s<- merge(tb_month_s,df_tile_processed,by="tile_id") |
|
256 |
summary_metrics_v <- merge(summary_metrics_v,df_tile_processed,by="tile_id") |
|
257 |
#test <- merge(summary_metrics_v,df_tile_processed,by="tile_id",all=F) |
|
258 |
#duplicate columns...need to be cleaned up later |
|
259 |
try(tb$year_predicted <- tb$year_predicted.x) |
|
260 |
try(tb$reg <- tb$reg.x) |
|
261 |
try(summary_metrics_v$year_predicted <- summary_metrics_v$year_predicted.x) |
|
262 |
try(summary_metrics_v$reg <- summary_metrics_v$reg.x) |
|
263 |
try(summary_metrics_v$lat <- summary_metrics_v$lat.x) |
|
264 |
try(summary_metrics_v$lon <- summary_metrics_v$lon.x) |
|
265 |
|
|
266 |
############ PART 2: PRODUCE FIGURES ################ |
|
267 |
|
|
268 |
|
|
269 |
tb_subset <- subset(tb,pred_mod=="mod1") |
|
270 |
|
|
271 |
#sqrt(var(tb_subset$rmse)) |
|
272 |
|
|
273 |
std_dev_val <- sqrt(var(tb_subset[[metric_name]])) #e.g. rmse |
|
274 |
mean_val <- mean(tb_subset[[metric_name]]) |
|
275 |
median_val <- median(tb_subset[[metric_name]]) |
|
276 |
max_val <- max(tb_subset[[metric_name]]) |
|
277 |
min_val <- min(tb_subset[[metric_name]]) |
|
278 |
n_val <- length(tb_subset[[metric_name]]) |
|
279 |
|
|
280 |
#mode(tb_subset[[metric_name]]) |
|
281 |
|
|
282 |
stat_name <- c("std_dev","mean","median","max","min","n") |
|
283 |
stat_val <- c(std_dev_val,mean_val,median_val,max_val,min_val,n_val) |
|
284 |
df_stat <- data.frame(stat_name=stat_name,stat_val=stat_val) |
|
285 |
|
|
286 |
threshol_val <- c(5,6,10) |
|
287 |
n_threshold_val <- sum((tb_subset[[metric_name]]) > threshol_val[1]) |
|
288 |
100*n_threshold_val/n_val |
|
289 |
|
|
290 |
n_threshold_val <- sum((tb_subset[[metric_name]]) > threshol_val[2]) |
|
291 |
100*n_threshold_val/n_val |
|
292 |
|
|
293 |
n_threshold_val <- sum((tb_subset[[metric_name]]) > threshol_val[3]) |
|
294 |
100*n_threshold_val/n_val |
|
295 |
|
|
296 |
#Find out where these values are located...by mapping extremes! |
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
########################### |
|
304 |
### Figure 1: plot location of the study area with tiles processed |
|
305 |
|
|
306 |
#df_tiled_processed <- na.omit(df_tile_processed) #remove other list of folders irrelevant |
|
307 |
#list_shp_reg_files <- df_tiled_processed$shp_files |
|
308 |
|
|
309 |
#list_shp_reg_files<- as.character(df_tile_processed$shp_files) #this could be the solution!! |
|
310 |
list_shp_reg_files <- as.character(basename(unique(df_tile_processed$shp_files))) #this could be the solution!! |
|
311 |
#the shapefiles must be copied in the proper folder!!! |
|
312 |
#list_shp_reg_files<- file.path(in_dir,in_dir_list[1],"shapefile",list_shp_reg_files) |
|
313 |
#list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir, |
|
314 |
# as.character(df_tile_processed$tile_coord),"shapefiles",basename(list_shp_reg_files)) |
|
315 |
#list_shp_reg_files <- file.path("/data/project/layers/commons/NEX_data/",out_dir, |
|
316 |
#"shapefiles",basename(list_shp_reg_files)) |
|
317 |
|
|
318 |
### Potential function starts here: |
|
319 |
#function(in_dir,out_dir,list_shp_reg_files,title_str,region_name,num_cores,out_suffix,out_suffix) |
|
320 |
|
|
321 |
### First get background map to display where study area is located |
|
322 |
#can make this more general later on..should have this already in a local directory on Atlas or NEX!!!! |
|
323 |
|
|
324 |
#http://www.diva-gis.org/Data |
|
325 |
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" |
|
326 |
path_to_shp <- dirname(countries_shp) |
|
327 |
layer_name <- sub(".shp","",basename(countries_shp)) |
|
328 |
reg_layer <- readOGR(path_to_shp, layer_name) |
|
329 |
#proj4string(reg_layer) <- CRS_locs_WGS84 |
|
330 |
#reg_shp<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]]))) |
|
331 |
|
|
332 |
centroids_pts <- vector("list",length(list_shp_reg_files)) |
|
333 |
shps_tiles <- vector("list",length(list_shp_reg_files)) |
|
334 |
#collect info: read in all shapfiles |
|
335 |
#This is slow...make a function and use mclapply?? |
|
336 |
#/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles |
|
337 |
|
|
338 |
in_dir_shp <- file.path(in_dir,in_dir_list[[1]],"shapefiles") #this should be set as a input parameter!!! |
|
339 |
for(i in 1:length(list_shp_reg_files)){ |
|
340 |
#path_to_shp <- dirname(list_shp_reg_files[[i]]) |
|
341 |
path_to_shp <- in_dir_shp |
|
342 |
layer_name <- sub(".shp","",basename(list_shp_reg_files[[i]])) |
|
343 |
shp1 <- try(readOGR(path_to_shp, layer_name)) #use try to resolve error below |
|
344 |
#shp_61.0_-160.0 |
|
345 |
#Geographical CRS given to non-conformant data: -186.331747678 |
|
346 |
|
|
347 |
#shp1<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]]))) |
|
348 |
if (!inherits(shp1,"try-error")) { |
|
349 |
pt <- gCentroid(shp1) |
|
350 |
centroids_pts[[i]] <- pt |
|
351 |
}else{ |
|
352 |
pt <- shp1 |
|
353 |
centroids_pts[[i]] <- pt |
|
354 |
} |
|
355 |
shps_tiles[[i]] <- shp1 |
|
356 |
#centroids_pts[[i]] <- centroids |
|
357 |
} |
|
358 |
|
|
359 |
#fun <- function(i,list_shp_files) |
|
360 |
#coord_names <- c("lon","lat") |
|
361 |
#l_ras#t <- rasterize_df_fun(test,coord_names,proj_str,out_suffix=out_suffix,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005) |
|
362 |
|
|
363 |
#remove try-error polygons...we loose three tiles because they extend beyond -180 deg |
|
364 |
tmp <- shps_tiles |
|
365 |
shps_tiles <- remove_errors_list(shps_tiles) #[[!inherits(shps_tiles,"try-error")]] |
|
366 |
#shps_tiles <- tmp |
|
367 |
length(tmp)-length(shps_tiles) #number of tiles with error message |
|
368 |
|
|
369 |
tmp_pts <- centroids_pts |
|
370 |
centroids_pts <- remove_errors_list(centroids_pts) #[[!inherits(shps_tiles,"try-error")]] |
|
371 |
#centroids_pts <- tmp_pts |
|
372 |
|
|
373 |
#plot info: with labels |
|
374 |
res_pix <-1200 |
|
375 |
col_mfrow <- 1 |
|
376 |
row_mfrow <- 1 |
|
377 |
|
|
378 |
png(filename=paste("Figure1_tile_processed_region_",region_name,"_",out_suffix,".png",sep=""), |
|
379 |
width=col_mfrow*res_pix,height=row_mfrow*res_pix) |
|
380 |
|
|
381 |
plot(reg_layer) |
|
382 |
#Add polygon tiles... |
|
383 |
for(i in 1:length(shps_tiles)){ |
|
384 |
shp1 <- shps_tiles[[i]] |
|
385 |
pt <- centroids_pts[[i]] |
|
386 |
#if(!inherits(shp1,"try-error")){ |
|
387 |
# plot(shp1,add=T,border="blue") |
|
388 |
# #plot(pt,add=T,cex=2,pch=5) |
|
389 |
# label_id <- df_tile_processed$tile_id[i] |
|
390 |
# text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1.3,font=2,col=c("red")) |
|
391 |
#} |
|
392 |
#to be able to run on NEX set font and usePolypath, maybe add option NEX? |
|
393 |
if(!inherits(shp1,"try-error")){ |
|
394 |
plot(shp1,add=T,border="blue",usePolypath = FALSE) #added usePolypath following error on brige and NEX |
|
395 |
#plot(pt,add=T,cex=2,pch=5) |
|
396 |
label_id <- df_tile_processed$tile_id[i] |
|
397 |
text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1.3,font=2,col=c("red"),family="HersheySerif") |
|
398 |
} |
|
399 |
|
|
400 |
} |
|
401 |
#title(paste("Tiles ", tile_size,region_name,sep="")) |
|
402 |
|
|
403 |
dev.off() |
|
404 |
|
|
405 |
list_outfiles[[counter_fig+1]] <- paste("Figure1_tile_processed_region_",region_name,"_",out_suffix,".png",sep="") |
|
406 |
counter_fig <- counter_fig+1 |
|
407 |
#this will be changed to be added to data.frame on the fly |
|
408 |
r1 <-c("figure_1","Tiles processed for the region",NA,NA,region_name,year_predicted,list_outfiles[[1]]) |
|
409 |
|
|
410 |
|
|
411 |
###################################################### |
|
412 |
##### Prepare objet to return #### |
|
413 |
|
|
414 |
assessment_obj <- list(list_df_assessment_files, df_assessment_figures_files) |
|
415 |
names(assessment_obj) <- c("df_assessment_files", "df_assessment_figures_files") |
|
416 |
## Prepare list of files to return... |
|
417 |
return(assessment_obj) |
|
418 |
|
|
419 |
} |
|
420 |
|
|
421 |
##################### END OF SCRIPT ###################### |
Also available in: Unified diff
initial commit new code for extreme values study, scaling up assessment part 5