Project

General

Profile

« Previous | Next » 

Revision 1c2cbc4d

Added by Benoit Parmentier about 8 years ago

initial commit for script to check missing tiles and predict gaps in mosaic: script part 0 global product assessment

View differences:

climate/research/oregon/interpolation/global_product_assessment_part0.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/24/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 -Rm u:aguzman4:rwx /nobackupp6/aguzman4/climateLayers/LST_tempSpline/
21
#COMMIT: generating animation for region 4 for multiple years sequences
22

  
23
#################################################################################################
24

  
25

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

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

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

  
65
#Mosaic related on NEX
66
#script_path <- "/home/parmentier/Data/IPLANT_project/env_layers_scripts"
67
function_mosaicing_functions <- "global_run_scalingup_mosaicing_function_09282016.R" #Functions used to mosaic predicted tiles
68
function_mosaicing <-"global_run_scalingup_mosaicing_09282016.R" #main scripts for mosaicing predicted tiles
69

  
70
source(file.path(script_path,function_mosaicing)) #source all functions used in this script 
71
source(file.path(script_path,function_mosaicing_functions)) #source all functions used in this script 
72

  
73
#Assessment on NEX
74
function_assessment_part1_functions <- "global_run_scalingup_assessment_part1_functions_12282015.R" #PARAM12
75
function_assessment_part1a <-"global_run_scalingup_assessment_part1a_01042016.R"
76
function_assessment_part2 <- "global_run_scalingup_assessment_part2_02092016.R"
77
function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R"
78
function_assessment_part3 <- "global_run_scalingup_assessment_part3_07292016.R"
79

  
80
source(file.path(script_path,function_assessment_part1_functions)) #source all functions used in this script 
81
source(file.path(script_path,function_assessment_part1a)) #source all functions used in this script 
82
source(file.path(script_path,function_assessment_part2)) #source all functions used in this script 
83
source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script 
84
source(file.path(script_path,function_assessment_part3)) #source all functions used in this script 
85

  
86
#Product assessment
87
function_product_assessment_part1_functions <- "global_product_assessment_part1_functions_09192016b.R"
88
source(file.path(script_path,function_product_assessment_part1_functions)) #source all functions used in this script 
89
function_product_assessment_part2_functions <- "global_product_assessment_part2_functions_10222016.R"
90
source(file.path(script_path,function_product_assessment_part2_functions)) #source all functions used in this script 
91

  
92
###############################
93
####### Parameters, constants and arguments ###
94

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

  
97
var<-"TMAX" # variable being interpolated #param 1, arg 1
98

  
99
##Add for precip later...
100
if (var == "TMAX") {
101
  y_var_name <- "dailyTmax"
102
  y_var_month <- "TMax"
103
}
104
if (var == "TMIN") {
105
  y_var_name <- "dailyTmin"
106
  y_var_month <- "TMin"
107
}
108

  
109
##Add for precip later...
110
if (var == "TMAX") {
111
  variable_name <- "maximum temperature"
112
}
113
if (var == "TMIN") {
114
  variable_name <- "minimum temperature"
115
}
116

  
117
#interpolation_method<-c("gam_fusion") #other otpions to be added later
118
interpolation_method<-c("gam_CAI") #param 2
119
CRS_interp <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0" #param 3
120
#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";
121

  
122
#out_region_name<-""
123
#list_models<-c("y_var ~ s(lat,lon,k=5) + s(elev_s,k=3) + s(LST,k=3)") #param 4
124
metric_name <- "var_pred" #use RMSE if accuracy
125

  
126
#reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia)
127
#master directory containing the definition of tile size and tiles predicted
128
#in_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/assessment"
129
#in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/mosaic"
130
in_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg6/assessment"
131
in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg6/mosaics/mosaic" #predicted mosaic
132
#in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg1/mosaics/mosaic"
133
#in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaics/mosaic"
134
#in_dir_mosaic <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/mosaic" #note dropped the s in mosaics
135

  
136
region_name <- c("reg6") #param 6, arg 3
137
out_suffix <- "global_assessment_reg6_10232016"
138

  
139
create_out_dir_param <- TRUE #param 9, arg 6
140

  
141

  
142
out_dir <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg6/assessment"
143

  
144
#run_figure_by_year <- TRUE # param 10, arg 7
145

  
146
file_format <- ".tif" #format for mosaiced files # param 11
147
NA_flag_val <- -32768  #No data value, # param 12
148

  
149
#num_cores <- 6 #number of cores used # param 13, arg 8
150
plotting_figures <- TRUE #running part2 of assessment to generate figures... # param 14
151
num_cores <- 11 #number of cores used # param 13, arg 8
152
#python_bin <- "/nobackupp6/aguzman4/climateLayers/sharedModules2/bin" #PARAM 30
153
python_bin <- "/usr/bin" #PARAM 30
154

  
155
day_start <- "1984101" #PARAM 12 arg 12
156
day_end <- "20141231" #PARAM 13 arg 13
157
#date_start <- day_start
158
#date_end <- day_end
159

  
160
#infile_mask <- "/nobackupp8/bparmen1/NEX_data/regions_input_files/r_mask_LST_reg4.tif"
161
#infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg5.tif"
162
#infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg4.tif"
163
#infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg6.tif"
164

  
165
#run_figure_by_year <- TRUE # param 10, arg 7
166
#list_year_predicted <- "1984,2014"
167
scaling <- 0.01 #was scaled on 100 
168
#if scaling is null then perform no scaling!!
169

  
170
#df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/output_reg5_1999/df_centroids_19990701_reg5_1999.txt"
171
#df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/output_reg4_1999/df_centroids_19990701_reg4_1999.txt"
172
#df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg6/mosaic/output_reg6_1984/df_centroids_19840101_reg6_1984.txt"
173
#/nobackupp6/aguzman4/climateLayers/out/reg1/assessment//output_reg1_1984/df_assessment_files_reg1_1984_reg1_1984.txt
174

  
175
#dates to plot and analyze
176

  
177
#l_dates <- c("19990101","19990102","19990103","19990701","19990702","19990703")
178
#l_dates <- c("19990101","19990102","19990103","19990104","19990105") 
179
#df_points_extracted_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/data_points_extracted.txt"
180
#df_points_extracted_fname <- NULL #if null extract on the fly
181
#r_mosaic_fname <- "r_mosaic.RData"
182
#r_mosaic_fname <- NULL #if null create a stack from input dir
183

  
184
#NA_flag_val_mosaic <- -3399999901438340239948148078125514752.000
185
NA_flag_val_mosaic <- -32768
186
in_dir_list_filename <- NULL #if NULL, use the in_dir directory to search for info
187
countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #Atlas
188
lf_raster <- NULL #list of raster to consider
189
item_no <- 13
190

  
191
##################### START SCRIPT #################
192

  
193
####### PART 1: Read in data ########
194
out_dir <- in_dir
195
if (create_out_dir_param == TRUE) {
196
  out_dir <- create_dir_fun(out_dir,out_suffix)
197
  setwd(out_dir)
198
}else{
199
  setwd(out_dir) #use previoulsy defined directory
200
}
201

  
202
#setwd(out_dir)
203

  
204
###########  ####################
205

  
206
############ Using predicting first ##########
207

  
208
## using predictions
209
#pattern_str <- ".*.tif"
210
pattern_str <-"*.tif"
211
lf_raster <- list.files(path=in_dir_mosaic,pattern=pattern_str,recursive=F,full.names=T)
212
r_stack <- stack(lf_raster,quick=T) #this is very fast now with the quick option!
213
#save(r_mosaic,file="r_mosaic.RData")
214

  
215
#### check for missing dates from list of tif
216

  
217
###This should be a function!!
218

  
219
#####  This could be moved in a separate file!!
220
###############  PART4: Checking for mosaic produced for given region ##############
221
## From list of mosaic files predicted extract dates
222
## Check dates predicted against date range for a given date range
223
## Join file information to centroids of tiles data.frame
224
#list_dates_produced <- unlist(mclapply(1:length(lf_mosaic_list),FUN=extract_date,x=lf_mosaic_list,item_no=13,mc.preschedule=FALSE,mc.cores = num_cores))                         
225
#list_dates_produced <-  mclapply(1:2,FUN=extract_date,x=lf_mosaic_list,item_no=13,mc.preschedule=FALSE,mc.cores = 2)                         
226
item_no <- 13
227
date_start <- day_start
228
date_end <- day_end
229
#day_start <- "1984101" #PARAM 12 arg 12
230
#day_end <- "20141231" #PARAM 13 arg 13
231

  
232
#Using default values for parameters exectpt for num_cores=11 instead of 1
233
#debug(check_missing)
234
#test_missing <- check_missing(lf=lf_raster, 
235
#                              pattern_str=NULL,
236
#                              in_dir=".", #this is not used if lf is given
237
#                              date_start="1984101",
238
#                              date_end="20141231",
239
#                              item_no=13,
240
#                              out_suffix="",
241
#                              num_cores=num_cores,
242
#                              out_dir=".")
243
  
244
##Run this on reg4 and reg5 after
245
#Add report by year in text file?
246
#Using specified values for parameters
247
test_missing <- check_missing(lf=lf_raster, 
248
                              pattern_str=NULL,
249
                              in_dir=in_dir_mosaic,
250
                              date_start="1984101",
251
                              date_end="20141231",
252
                              item_no=13,
253
                              out_suffix=out_suffix,
254
                              num_cores=num_cores,
255
                              out_dir=".")
256

  
257
df_time_series <- test_missing$df_time_series
258
head(df_time_series)
259

  
260
table(df_time_series$missing)
261
table(df_time_series$year)
262

  
263
#############################
264
##### Creating animation based on prediction
265

  
266
#####
267
NAvalue(r_stack)
268
plot(r_stack,y=6,zlim=c(-10000,10000)) #this is not rescaled
269
#plot(r_stack,zlim=c(-50,50),col=matlab.like(255))
270
var_name <- "dailyTmax"
271

  
272
#debug(plot_and_animate_raster_time_series)
273

  
274
#metric_name <- "var_pred" #use RMSE if accuracy
275
#df_raster <- read.table("df_raster_global_assessment_reg6_10102016.txt",sep=",",header=T)
276
#plot_figure <- 
277
#function_product_assessment_part2_functions <- "global_product_assessment_part2_functions_10222016.R"
278
#source(file.path(script_path,function_product_assessment_part2_functions)) #source all functions used in this script 
279

  
280
#undebug(plot_and_animate_raster_time_series)
281
range_year <- c(1984,1985)
282
subset_df_time_series <- subset(df_time_series,year%in% range_year)
283
subset_df_time_series <- subset_df_time_series[!is.na(subset_df_time_series$lf),]
284

  
285
lf_subset <- file.path(subset_df_time_series$dir,subset_df_time_series$lf)
286
range_year_str <- paste(range_year, sep = "_", collapse = "_")
287

  
288
out_suffix_str <- paste(range_year_str,out_suffix,sep="_")
289

  
290
#started on 10/22/2016 at 9.57
291
animation_obj <- plot_and_animate_raster_time_series(lf_subset, 
292
                                                     item_no,
293
                                                     region_name,
294
                                                     var_name,
295
                                                     metric_name,
296
                                                     NA_flag_val,
297
                                                     filenames_figures=NULL,
298
                                                     frame_speed=60,
299
                                                     animation_format=".gif",
300
                                                     zlim_val=NULL,
301
                                                     plot_figure=T,
302
                                                     generate_animation=T,
303
                                                     num_cores=num_cores,
304
                                                     out_suffix=out_suffix_str,
305
                                                     out_dir=out_dir)
306
  
307
zlim_val <- c(-2000,5000)
308
animation_obj <- plot_and_animate_raster_time_series(lf_subset, 
309
                                                     item_no,
310
                                                     region_name,
311
                                                     var_name,
312
                                                     metric_name,
313
                                                     NA_flag_val,
314
                                                     filenames_figures=NULL,
315
                                                     frame_speed=60,
316
                                                     animation_format=".gif",
317
                                                     zlim_val=zlim_val,
318
                                                     plot_figure=T,
319
                                                     generate_animation=T,
320
                                                     num_cores=num_cores,
321
                                                     out_suffix=out_suffix_str,
322
                                                     out_dir=out_dir)
323

  
324
#ffmpeg -i yeay.gif outyeay.mp4
325

  
326
#/Applications/ffmpeg -r 25 -i input%3d.png -vcodec libx264 -x264opts keyint=25 -pix_fmt yuv420p -r 25 ../output.mp4
327

  
328
#ffmpeg -f gif -i file.gif -c:v libx264 outfile.mp4
329

  
330
#ffmpeg -i animation_frame_60_-2500_6000_.gif animation_frame_60_-2500_6000_.mp4
331

  
332
#ffmpeg -i animation_frame_60_-2500_6000_.gif animation_frame_60_-2500_6000_.mp4
333

  
334
#ffmpeg -f gif -i animation_frame_60_-2500_6000_.gif -vcodec libx264 -x264opts keyint=25 -pix_fmt yuv420p -r 25 outfile.mp4
335
#ffmpeg -f gif -i animation_frame_60_-2500_6000_.gif -vcodec libx264 -x264opts keyint=11 -pix_fmt yuv420p -r 11 outfile.mp4
336

  
337
#ffmpeg -r 10 -i animation_frame_60_-2500_6000_.gif animation.avi
338

  
339
#ffmpeg -f gif -i animation_frame_60_-2500_6000_.gif -vcodec libx264 -x264opts -pix_fmt yuv420p outfile.mp4
340

  
341

  
342

  
343
############################ END OF SCRIPT ##################################

Also available in: Unified diff