Project

General

Profile

« Previous | Next » 

Revision 4912ab51

Added by Benoit Parmentier over 8 years ago

global product assessment part 1, initial commit

View differences:

climate/research/oregon/interpolation/global_product_assessment_part1.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
#Combining tables and figures for individual runs for years and tiles.
5
#AUTHOR: Benoit Parmentier 
6
#CREATED ON: 05/15/2016  
7
#MODIFIED ON: 05/16/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
#################################################################################################
23

  
24

  
25
### Loading R library and packages        
26
#library used in the workflow production:
27
library(gtools)                              # loading some useful tools 
28
library(mgcv)                                # GAM package by Simon Wood
29
library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
30
library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
31
library(rgdal)                               # GDAL wrapper for R, spatial utilities
32
library(gstat)                               # Kriging and co-kriging by Pebesma et al.
33
library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
34
library(raster)                              # Hijmans et al. package for raster processing
35
library(gdata)                               # various tools with xls reading, cbindX
36
library(rasterVis)                           # Raster plotting functions
37
library(parallel)                            # Parallelization of processes with multiple cores
38
library(maptools)                            # Tools and functions for sp and other spatial objects e.g. spCbind
39
library(maps)                                # Tools and data for spatial/geographic objects
40
library(reshape)                             # Change shape of object, summarize results
41
library(plotrix)                             # Additional plotting functions
42
library(plyr)                                # Various tools including rbind.fill
43
library(spgwr)                               # GWR method
44
library(automap)                             # Kriging automatic fitting of variogram using gstat
45
library(rgeos)                               # Geometric, topologic library of functions
46
#RPostgreSQL                                 # Interface R and Postgres, not used in this script
47
library(gridExtra)
48
#Additional libraries not used in workflow
49
library(pgirmess)                            # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess}
50
library(colorRamps)
51
library(zoo)
52
library(xts)
53
library(lubridate)
54

  
55
###### Function used in the script #######
56
  
57
#script_path <- "/nobackupp8/bparmen1/env_layers_scripts" #path to script
58
script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts" #path to script
59

  
60
## NASA poster and paper related
61
source(file.path(script_path,"NASA2016_conference_temperature_predictions_function_05032016b.R"))
62

  
63
#Mosaic related on NEX
64
#script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts"
65
function_mosaicing_functions <- "global_run_scalingup_mosaicing_function_04232016.R" #PARAM12
66
function_mosaicing <-"global_run_scalingup_mosaicing_05012016.R"
67
source(file.path(script_path,function_mosaicing)) #source all functions used in this script 
68
source(file.path(script_path,function_mosaicing_functions)) #source all functions used in this script 
69

  
70
#Assessment on NEX
71
function_assessment_part1_functions <- "global_run_scalingup_assessment_part1_functions_02112015.R" #PARAM12
72
function_assessment_part1a <-"global_run_scalingup_assessment_part1a_01042016.R"
73
function_assessment_part2 <- "global_run_scalingup_assessment_part2_02092016.R"
74
function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R"
75
function_assessment_part3 <- "global_run_scalingup_assessment_part3_04292016b.R"
76
source(file.path(script_path,function_assessment_part1_functions)) #source all functions used in this script 
77
source(file.path(script_path,function_assessment_part1a)) #source all functions used in this script 
78
source(file.path(script_path,function_assessment_part2)) #source all functions used in this script 
79
source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script 
80
source(file.path(script_path,function_assessment_part3)) #source all functions used in this script 
81

  
82
###############################
83
####### Parameters, constants and arguments ###
84

  
85
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #constant 1
86

  
87
var<-"TMAX" # variable being interpolated #param 1, arg 1
88

  
89
##Add for precip later...
90
if (var == "TMAX") {
91
  y_var_name <- "dailyTmax"
92
  y_var_month <- "TMax"
93
}
94
if (var == "TMIN") {
95
  y_var_name <- "dailyTmin"
96
  y_var_month <- "TMin"
97
}
98

  
99
##Add for precip later...
100
if (var == "TMAX") {
101
  variable_name <- "maximum temperature"
102
}
103
if (var == "TMIN") {
104
  variable_name <- "minimum temperature"
105
}
106

  
107
#interpolation_method<-c("gam_fusion") #other otpions to be added later
108
interpolation_method<-c("gam_CAI") #param 2
109
CRS_interp <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0" #param 3
110
#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";
111

  
112
out_region_name<-""
113
list_models<-c("y_var ~ s(lat,lon,k=5) + s(elev_s,k=3) + s(LST,k=3)") #param 4
114

  
115
#reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia)
116
#master directory containing the definition of tile size and tiles predicted
117
in_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/assessment"
118
in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/mosaic"
119

  
120
region_name <- c("reg4") #param 6, arg 3
121

  
122
create_out_dir_param <- TRUE #param 9, arg 6
123
out_suffix <- "_global_assessment_reg4_05152016"
124

  
125
out_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/assessment"
126

  
127
#run_figure_by_year <- TRUE # param 10, arg 7
128

  
129
file_format <- ".tif" #format for mosaiced files # param 11
130
NA_flag_val <- -32768  #No data value, # param 12
131

  
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 <- 11 #number of cores used # param 13, arg 8
135
#python_bin <- "/nobackupp6/aguzman4/climateLayers/sharedModules2/bin" #PARAM 30
136
python_bin <- "/usr/bin" #PARAM 30
137

  
138
day_start <- "1984101" #PARAM 12 arg 12
139
day_end <- "19981231" #PARAM 13 arg 13
140

  
141
#infile_mask <- "/nobackupp8/bparmen1/NEX_data/regions_input_files/r_mask_LST_reg4.tif"
142
infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg4.tif"
143

  
144
#run_figure_by_year <- TRUE # param 10, arg 7
145
list_year_predicted <- "1984,2014"
146
scaling <- 0.01 #was scaled on 100 
147

  
148
df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/output_reg4_1999/df_centroids_19990701_reg4_1999.txt"
149

  
150
raster_name_lf <- c("/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920101_reg4_1992_m_gam_CAI_dailyTmax_19920101_reg4_1992.tif",
151
                    "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920102_reg4_1992_m_gam_CAI_dailyTmax_19920102_reg4_1992.tif",
152
                    "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920103_reg4_1992_m_gam_CAI_dailyTmax_19920103_reg4_1992.tif",
153
                    "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920701_reg4_1992_m_gam_CAI_dailyTmax_19920701_reg4_1992.tif",
154
                    "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920702_reg4_1992_m_gam_CAI_dailyTmax_19920702_reg4_1992.tif",
155
                    "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920703_reg4_1992_m_gam_CAI_dailyTmax_19920703_reg4_1992.tif")
156

  
157
#l_dates <- c("19990101","19990102","19990103","19990701","19990702","19990703")
158
l_dates <- c("19920101","19920102","19920103","19920701","19920702","19990703")
159

  
160
df_points_extracted_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/data_points_extracted.txt"
161
NA_flag_val_mosaic <- -3399999901438340239948148078125514752.000
162
in_dir_list_filename <- NULL #if NULL, use the in_dir directory to search for info
163

  
164
##################### START SCRIPT #################
165

  
166
####### PART 1: Read in data ########
167
out_dir <- in_dir
168
if (create_out_dir_param == TRUE) {
169
  out_dir <- create_dir_fun(out_dir,out_suffix)
170
  setwd(out_dir)
171
}else{
172
  setwd(out_dir) #use previoulsy defined directory
173
}
174

  
175
setwd(out_dir)
176

  
177
###########  ####################
178

  
179
if(!is.null(in_dir_list_filename)){
180
  in_dir_list <- as.list(read.table(in_dir_list_filename,stringsAsFactors=F)[,1])
181
}else{
182
  pattern_str <- paste0("^output_",region_name,".*.")
183
  in_dir_list_all <- list.dirs(path=in_dir,recursive = T)
184
  in_dir_list <- in_dir_list_all[grep(pattern_str,basename(in_dir_list_all),invert=FALSE)] #select directory with shapefiles...
185
  #in_dir_shp <- file.path(in_dir_list_all,"shapefiles")
186
}
187

  
188
list_tb_fname <- list.files(path=in_dir_list,"tb_diagnostic_v_NA_.*.txt",full.names=T)
189
list_df_fname <- list.files(path=in_dir_list,"df_tile_processed_.*..txt",full.names=T)
190
list_summary_metrics_v_fname <- list.files(path=in_dir_list,"summary_metrics_v2_NA_.*.txt",full.names=T)
191
list_tb_s_fname <- list.files(path=in_dir_list,"tb_diagnostic_s_NA.*.txt",full.names=T)
192
list_tb_month_s_fname <- list.files(path=in_dir_list,"tb_month_diagnostic_s.*.txt",full.names=T)
193
list_data_month_s_fname <- list.files(path=in_dir_list,"data_month_s.*.txt",full.names=T)
194
list_data_s_fname <- list.files(path=in_dir_list,"data_day_s.*.txt",full.names=T)
195
list_data_v_fname <- list.files(path=in_dir_list,"data_day_v.*.txt",full.names=T)
196
list_pred_data_month_info_fname <- list.files(path=in_dir_list,"pred_data_month_info.*.txt",full.names=T)
197
list_pred_data_day_info_fname <- list.files(path=in_dir_list,"pred_data_day_info.*.txt",full.names=T)
198

  
199
## Use station from specific year and date?
200

  
201

  
202

  
203
pattern_str <-"*.tif"
204
lf_mosaic_list <- list.files(path=in_dir_mosaic,pattern=pattern_str,recursive=F,full.names=T)
205
r_mosaic <- stack(lf_mosaic_list)
206
save(r_mosaic,file="r_mosaic.RData")
207
#start_date <- day_to_mosaic_range[1]
208
#end_date <- day_to_mosaic_range[2]
209
#start_date <- day_start #PARAM 12 arg 12
210
#end_date <- day_end #PARAM 13 arg 13
211

  
212
#date_to_plot <- seq(as.Date(strptime(start_date,"%Y%m%d")), as.Date(strptime(end_date,"%Y%m%d")), 'day')
213
#l_dates <- format(date_to_plot,"%Y%m%d") #format back to the relevant date format for files
214
mask_pred <- FALSE
215
list_param_pre_process <- list(raster_name_lf,python_bin,infile_mask,scaling,mask_pred,NA_flag_val,out_suffix,out_dir) 
216
names(list_param_pre_process) <- c("lf","python_bin","infile_mask","scaling","mask_pred","NA_flag_val","out_suffix","out_dir") 
217
  
218
#debug(pre_process_raster_mosaic_fun)
219

  
220
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)                         
221
#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)                         
222

  
223
#test <- pre_process_raster_mosaic_fun(2,list_param_pre_process)
224
#lf_mosaic_scaled <- unlist(lf_mosaic_scaled)
225

  
226
r_mosaic_scaled <- stack(lf_mosaic_scaled)
227
NAvalue(r_mosaic_scaled)<- -3399999901438340239948148078125514752.000
228
plot(r_mosaic_scaled,y=6,zlim=c(-50,50))
229
plot(r_mosaic_scaled,zlim=c(-50,50),col=matlab.like(255))
230

  
231
#layout_m<-c(1,3) #one row two columns
232
#levelplot(r_mosaic_scaled,zlim=c(-50,50),col.regions=matlab.like(255))
233
#levelplot(r_mosaic_scaled,zlim=c(-50,50),col.regions=matlab.like(255))
234

  
235
#png(paste("Figure7a__spatial_pattern_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""),
236
#    height=480*layout_m[1],width=480*layout_m[2])
237
#plot(r_pred,col=temp.colors(255),zlim=c(-3500,4500))
238
#plot(r_pred,col=matlab.like(255),zlim=c(-40,50))
239
#paste(raster_name[1:7],collapse="_")
240
#add filename option later
241

  
242
#NA_flag_val_mosaic <- -3399999901438340239948148078125514752.000
243

  
244
list_param_plot_raster_mosaic <- list(l_dates,r_mosaic_scaled,NA_flag_val_mosaic,out_dir,out_suffix,
245
                                      region_name,variable_name)
246
names(list_param_plot_raster_mosaic) <- c("l_dates","r_mosaic_scaled","NA_flag_val_mosaic","out_dir","out_suffix",
247
                                          "region_name","variable_name")
248

  
249
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)                         
250

  
251
############### PART2: temporal profile #############
252
#### Extract time series
253
###
254
#-65,-22
255

  
256
#Use the global output??
257

  
258

  
259
df_points <- read.table(df_points_extracted_fname,sep=",") 
260
df_points_tmp <- df_points
261
df_points <- as.data.frame(t(df_points))
262
names(df_points) <- paste0("ID_",1:ncol(df_points))
263

  
264
#df_centroids <- read.table(df_centroids_fname,sep=",")
265

  
266
coordinates(df_centroids)<- c("long","lat")
267
proj4string(df_centroids) <- CRS_locs_WGS84
268
## Checking new files:
269
in_dir_mosaic <- "/nobackupp6/aguzman4/climateLayers/out/reg4/mosaics2/mosaic"
270
#/nobackupp6/aguzman4/climateLayers/out/reg4/mosaics2/mosaic/output_reg4_*/r_m_use_edge_weights_weighted_mean_mask_gam_CAI_dailyTmax_*_reg4_*.tif
271
pattern_str <- "r_m_use_edge_weights_weighted_mean_mask_gam_CAI_dailyTmax_.*._reg4_.*.tif"
272
searchStr = paste(in_dir_mosaic,"/output_reg4_2014",year_processed,"/gam_CAI_dailyTmax_predicted_",pred_mod_name,"*",day_to_mosaic[i],"*.tif",sep="")
273

  
274
#lf_mosaic_list <- list.files(path=in_dir_mosaic,pattern="*.tif",recursive=T)
275
lf_mosaic_list <- list.files(path=in_dir_mosaic,pattern=pattern_str,recursive=T)
276
lf_mosaic_list <- lapply(1:length(day_to_mosaic),
277
                         FUN=function(i){
278
                           searchStr = paste(in_dir_tiles_tmp,"/*/",year_processed,"/gam_CAI_dailyTmax_predicted_",pred_mod_name,"*",day_to_mosaic[i],"*.tif",sep="")
279
                           Sys.glob(searchStr)})
280

  
281
#r_mosaic_ts <- stack(lf_mosaic_list)
282
#df_centroids <- extract(df_centroids,r_mosaic_ts)
283

  
284
df_points$files <- lf_mosaic_list
285

  
286
#debug(extract_date)
287
#test <- extract_date(6431,lf_mosaic_list,12) #extract item number 12 from the name of files to get the data
288
list_dates_produced <- unlist(mclapply(1:length(lf_mosaic_list),FUN=extract_date,x=lf_mosaic_list,item_no=14,mc.preschedule=FALSE,mc.cores = num_cores))                         
289
#list_dates_produced <-  mclapply(6400:6431,FUN=extract_date,x=lf_mosaic_list,item_no=12,mc.preschedule=FALSE,mc.cores = num_cores)                         
290

  
291
list_dates_produced_date_val <- as.Date(strptime(list_dates_produced,"%Y%m%d"))
292
month_str <- format(list_dates_produced_date_val, "%b") ## Month, char, abbreviated
293
year_str <- format(list_dates_produced_date_val, "%Y") ## Year with century
294
day_str <- as.numeric(format(list_dates_produced_date_val, "%d")) ## numeric month
295

  
296
df_produced <- data.frame(lf_mosaic_list,list_dates_produced_date_val,month_str,year_str,day_str)
297

  
298
date_start <- "19840101"
299
date_end <- "20141231"
300
date1 <- as.Date(strptime(date_start,"%Y%m%d"))
301
date2 <- as.Date(strptime(date_end,"%Y%m%d"))
302
dates_range <- seq.Date(date1, date2, by="1 day") #sequence of dates
303

  
304
missing_dates <- setdiff(as.character(dates_range),as.character(list_dates_produced_date_val))
305

  
306
month_str <- format(list_dates_produced_date_val, "%b") ## Month, char, abbreviated
307
year_str <- format(list_dates_produced_date_val, "%Y") ## Year with century
308
day_str <- as.numeric(format(list_dates_produced_date_val, "%d")) ## numeric month
309

  
310
df_points$date <- list_dates_produced_date_val
311
df_points$month <- month_str
312
df_points$year <- year_str
313
df_points$day <- day_str
314

  
315
unique_date_tb <-table(df_points$date)
316
unique_date <- unique(df_points$date)
317

  
318
station_id <- 8
319
var_name <-paste0("ID_",station_id)
320

  
321
##Screen for unique date values
322
if(max(unique_date_tb)>1){
323
#  formula_str <- paste(var_name," ~ ","TRIP_START_DATE_f",sep="")
324
   var_pix <- aggregate(ID_8 ~ date, data = df_points, mean) #aggregate by date
325
}
326

  
327
var_pix$ID_8 <- var_pix$ID_8*scaling
328

  
329
d_z_tmp <- zoo(var_pix$ID_8,var_pix$date)
330
names(d_z_tmp)<-"ID_8"
331
min(d_z_tmp$ID_8)
332
max(d_z_tmp$ID_8)
333

  
334
plot(d_z_tmp) #this is the whole time series
335

  
336
day_start <- "1986-01-01" #PARAM 12 arg 12
337
day_end <- "1998-12-31" #PARAM 13 arg 13
338

  
339
start_date <- as.Date(day_start)
340
end_date <- as.Date(day_end)
341
start_year <- year(start_date)
342
end_year <- year(end_date)
343

  
344
d_z <- window(d_z_tmp,start=start_date,end=end_date)
345
#d_z2 <- window(d_z_tmp2,start=start_date,end=end_date)
346

  
347
res_pix <- 1000
348
#res_pix <- 480
349
col_mfrow <- 2
350
row_mfrow <- 1
351
  
352
png_filename <-  file.path(out_dir,paste("Figure5a_time_series_profile_",region_name,"_",out_suffix,".png",sep =""))
353
title_str <- paste("Predicted daily ",variable_name," for the ", start_year,"-",end_year," time period",sep="")
354
  
355
png(filename=png_filename,width = col_mfrow * res_pix,height = row_mfrow * res_pix)
356

  
357
plot(d_z,ylab="tmax in deg C",xlab="Daily time steps",
358
     main=title_str,cex=3,font=2,
359
     cex.main=1.5,cex.lab=1.5,font.lab=2,
360
     lty=3)
361

  
362
dev.off()
363

  
364
#### Subset for 5b
365

  
366
day_start <- "1991-01-01" #PARAM 12 arg 12
367
day_end <- "1992-12-31" #PARAM 13 arg 13
368

  
369
start_date <- as.Date(day_start)
370
end_date <- as.Date(day_end)
371
start_year <- year(start_date)
372
end_year <- year(end_date)
373
d_z <- window(d_z_tmp,start=start_date,end=end_date)
374
#d_z2 <- window(d_z_tmp2,start=start_date,end=end_date)
375

  
376
res_pix <- 1000
377
#res_pix <- 480
378
col_mfrow <- 2
379
row_mfrow <- 1
380
  
381
png_filename <-  file.path(out_dir,paste("Figure5b_subset_time_series_profile_",region_name,"_",out_suffix,".png",sep =""))
382
title_str <- paste("Predicted daily ",variable_name," for the ", start_year,"-",end_year," time period",sep="")
383
  
384
png(filename=png_filename,width = col_mfrow * res_pix,height = row_mfrow * res_pix)
385

  
386
plot(d_z,ylab="tmax in deg C",xlab="Daily time steps",
387
     main=title_str,cex=3,font=2,
388
     cex.main=1.5,cex.lab=1.5,font.lab=2,
389
     lty=3)
390

  
391
dev.off()
392

  
393
#data_pixel <- data_df[id_selected,]
394
#data_pixel$rainfall <- as.numeric(data_pixel$rainfall)
395
#d_z_tmp <-zoo(data_pixel$rainfall,as.Date(data_pixel$date))
396
#names(d_z_tmp)<- "rainfall"
397
#data_pixel <- as.data.frame(data_pixel)
398
#d_z_tmp2 <- zoo(data_pixel[[var_name]],as.Date(data_pixel$date))
399
    
400
#df_tmp <- subset(data_var,data_var$ID_stat==id_name)
401
#if(da)
402

  
403

  
404
############################ END OF SCRIPT ##################################

Also available in: Unified diff