Revision 724177ad
Added by Benoit Parmentier almost 9 years ago
climate/research/oregon/interpolation/master_script_stage_8.R | ||
---|---|---|
1 |
################## Master script for climate predictions ####################################### |
|
2 |
############################ TMIN AND TMAX predictions ########################################## |
|
3 |
# |
|
4 |
##This script produces intperpolated surface of TMIN and TMAX for specified processing region(s) given sets |
|
5 |
#of inputs and parameters. |
|
6 |
#STAGE 1: LST climatology downloading and/or calculation |
|
7 |
#STAGE 2: Covariates preparation for study/processing area: calculation of covariates (spect,land cover,etc.) and reprojection |
|
8 |
#STAGE 3: Data preparation: meteorological station database query and extraction of covariates values from raster brick |
|
9 |
#STAGE 4: Raster prediction: run interpolation method (-- gam fusion, gam CAI, ...) and perform validation |
|
10 |
#STAGE 5: Output analyses: assessment of results for specific dates... (tile based) |
|
11 |
#STAGE 6: Assessement of predictions by tiles and region: summary tables and figures |
|
12 |
#STAGE 7: Mosaicing of predictions and accuracy layer productions |
|
13 |
#STAGE 8: Comparison of predictions across regions and years with figure generation. |
|
14 |
#AUTHOR: Benoit Parmentier |
|
15 |
#CREATED ON: 12/29/2015 |
|
16 |
#MODIFIED ON: 02/04/2016 |
|
17 |
#PROJECT: NCEAS-IPLANT-NASA: Environment Layers |
|
18 |
|
|
19 |
#First source these files: |
|
20 |
#Resolved call issues from R. |
|
21 |
#source /nobackupp6/aguzman4/climateLayers/sharedModules2/etc/environ.sh |
|
22 |
#MODULEPATH=$MODULEPATH:/nex/modules/files |
|
23 |
#module load pythonkits/gdal_1.10.0_python_2.7.3_nex |
|
24 |
|
|
25 |
## TODO: |
|
26 |
# Add assessment part 2 (figures): still testing |
|
27 |
# Make this callable from the shell |
|
28 |
# Adapt for python script |
|
29 |
# Fix figure for part2 |
|
30 |
# Call also use library(optparse) |
|
31 |
|
|
32 |
################################################################################################## |
|
33 |
# |
|
34 |
### PARAMETERS DEFINED IN THE SCRIPT |
|
35 |
#There are 21 parameters, 1 constant and 8 arguments (drawn from the parameters) for the Rscript call. |
|
36 |
#The arguments are passed directly from Rscript: |
|
37 |
#var <- args[1] # variable being interpolated #param 1, arg 1 |
|
38 |
#in_dir1 <- args[2] # This is the output directory containing global prediction e.g./nobackupp6/aguzman4/climateLayers/out/ param 5, arg 2 |
|
39 |
#region_name <- args[3] # region e.g. "reg4" param 6, arg 3 |
|
40 |
#out_prefix <- args[4] # this is used in creating an output directory,include region name? param 7, arg 4 |
|
41 |
#out_dir <- args[5] # output parent dir, can be home dir or other, param 8, arg 5) |
|
42 |
#create_out_dir_param <- args[6] # if TRUE create a output from "output"+out_prefix param 9, arg 6 |
|
43 |
#list_year_predicted <- args[7] # enter as list but currently runs on the first element of the list, param 10, arg 7 |
|
44 |
#num_cores <- args[8] #number of cores used # param 13, arg 8 |
|
45 |
#max_mem <- args[9] # maximum memory, param 21 |
|
46 |
|
|
47 |
#var = "TMAX" # variable being interpolated #param 1, arg 1 |
|
48 |
#in_dir1 = "/nobackupp6/aguzman4/climateLayers/out/" #param 5, arg 2 |
|
49 |
#region_name = "reg4" #param 6, arg 3 |
|
50 |
#out_prefix = "run_global_analyses_pred_12282015" #param 7, arg 4 |
|
51 |
#out_dir = "/nobackupp8/bparmen1/" #param 8, arg 5 |
|
52 |
#create_out_dir_param = "TRUE" #param 9, arg 6 |
|
53 |
#list_year_predicted = c(2010) # param 10, arg 7 |
|
54 |
#num_cores = 6 #number of cores used # param 13, arg 8 |
|
55 |
#max_mem = 1e+07 #param 21, arg 9 |
|
56 |
|
|
57 |
### Testing several years on the bridge before running jobs on nodes with qsub |
|
58 |
#This can be made in a data.frame to run through... |
|
59 |
#Rscript /nobackupp8/bparmen1/env_layers_scripts/master_script_stage_6_01222016.R TMAX /nobackupp6/aguzman4/climateLayers/out/ reg4 run_global_analyses_pred_12282015 /nobackupp8/bparmen1/ TRUE 2010 6 1e+07 |
|
60 |
#Rscript /nobackupp8/bparmen1/env_layers_scripts/master_script_stage_6_01222016.R TMAX /nobackupp6/aguzman4/climateLayers/out/ reg4 run_global_analyses_pred_2011_reg4 /nobackupp8/bparmen1/ TRUE 2011 6 1e+07 |
|
61 |
#Rscript /nobackupp8/bparmen1/env_layers_scripts/master_script_stage_6_01222016.R TMAX /nobackupp6/aguzman4/climateLayers/out/ reg4 run_global_analyses_pred_2012_reg4 /nobackupp8/bparmen1/ TRUE 2012 6 1e+07 |
|
62 |
#Rscript /nobackupp8/bparmen1/env_layers_scripts/master_script_stage_6_01222016.R TMAX /nobackupp6/aguzman4/climateLayers/out/ reg4 run_global_analyses_pred_2013_reg4 /nobackupp8/bparmen1/ TRUE 2013 6 1e+07 |
|
63 |
#Rscript /nobackupp8/bparmen1/env_layers_scripts/master_script_stage_6_01222016.R TMAX /nobackupp6/aguzman4/climateLayers/out/ reg4 run_global_analyses_pred_2014_reg4 /nobackupp8/bparmen1/ TRUE 2014 6 1e+07 |
|
64 |
#Rscript /nobackupp8/bparmen1/env_layers_scripts/master_script_stage_6_01222016.R TMAX /nobackupp6/aguzman4/climateLayers/out/ reg4 run_global_analyses_pred_2009_reg4 /nobackupp8/bparmen1/ TRUE 2009 6 1e+07 |
|
65 |
#Rscript /nobackupp8/bparmen1/env_layers_scripts/master_script_stage_6_01222016.R TMAX /nobackupp6/aguzman4/climateLayers/out/ reg4 run_global_analyses_pred_2010_reg4 /nobackupp8/bparmen1/ TRUE 2010 6 1e+07 |
|
66 |
|
|
67 |
###Loading R library and packages ou |
|
68 |
library(RPostgreSQL) |
|
69 |
library(maps) |
|
70 |
library(maptools) |
|
71 |
library(parallel) |
|
72 |
library(gtools) # loading some useful tools |
|
73 |
library(mgcv) # GAM package by Simon Wood |
|
74 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
75 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
76 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
77 |
library(gstat) # Kriging and co-kriging by Pebesma et al. |
|
78 |
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines |
|
79 |
library(raster) # Hijmans et al. package for raster processing |
|
80 |
library(rasterVis) |
|
81 |
library(spgwr) |
|
82 |
library(reshape) |
|
83 |
library(plotrix) |
|
84 |
|
|
85 |
######## PARAMETERS FOR WORK FLOW ######################### |
|
86 |
### Need to add documentation ### |
|
87 |
|
|
88 |
#Adding command line arguments to use mpiexec |
|
89 |
args <- commandArgs(TRUE) |
|
90 |
#script_path<-"/nobackupp6/aguzman4/climateLayers/finalCode/environmental-layers/climate/research/oregon/interpolation" |
|
91 |
#dataHome<-"/nobackupp6/aguzman4/climateLayers/interp/testdata/" |
|
92 |
#script_path2<-"/nobackupp6/aguzman4/climateLayers/finalCode/environmental-layers/climate/research/world/interpolation" |
|
93 |
#var <- args[1] # variable being interpolated #param 1, arg 1 |
|
94 |
#in_dir1 <- args[2] #param 5, arg 2 |
|
95 |
#region_name <- args[3] #param 6, arg 3 |
|
96 |
#out_prefix <- args[4] #param 7, arg 4 |
|
97 |
#out_dir <- args[5] #param 8, arg 5 |
|
98 |
#out_dir <-paste(out_dir,"_",out_prefix,sep="") |
|
99 |
#create_out_dir_param <- args[6] #param 9, arg 6 |
|
100 |
#list_year_predicted <- args[7] # param 10, arg 7 |
|
101 |
#num_cores <- args[8] #number of cores used # param 13, arg 8 |
|
102 |
#max_mem <- args[9] #param 21 |
|
103 |
|
|
104 |
#CALLED FROM MASTER SCRIPT: |
|
105 |
|
|
106 |
script_path <- "/nobackupp8/bparmen1/env_layers_scripts" #path to script |
|
107 |
function_assessment_part1_functions <- "global_run_scalingup_assessment_part1_functions_02112015.R" #PARAM12 |
|
108 |
function_assessment_part1a <-"global_run_scalingup_assessment_part1a_01042016.R" |
|
109 |
function_assessment_part2 <- "global_run_scalingup_assessment_part2_02032016.R" |
|
110 |
function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R" |
|
111 |
source(file.path(script_path,function_assessment_part1_functions)) #source all functions used in this script |
|
112 |
source(file.path(script_path,function_assessment_part1a)) #source all functions used in this script |
|
113 |
source(file.path(script_path,function_assessment_part2)) #source all functions used in this script |
|
114 |
source(file.path(script_path,function_assessment_part2_functions)) #source all functions used in this script |
|
115 |
|
|
116 |
### Parameters, constants and arguments ### |
|
117 |
stages_to_run<-c(0,0,0,0,0,6) #this stage 2 to 5 currently stored in another file. |
|
118 |
|
|
119 |
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #constant 1 |
|
120 |
|
|
121 |
#var<-"TMAX" # variable being interpolated #param 1, arg 1 |
|
122 |
var <- args[1] # variable being interpolated #param 1, arg 1 |
|
123 |
|
|
124 |
##Add for precip later... |
|
125 |
if (var == "TMAX") { |
|
126 |
y_var_name <- "dailyTmax" |
|
127 |
y_var_month <- "TMax" |
|
128 |
} |
|
129 |
if (var == "TMIN") { |
|
130 |
y_var_name <- "dailyTmin" |
|
131 |
y_var_month <- "TMin" |
|
132 |
} |
|
133 |
|
|
134 |
#interpolation_method<-c("gam_fusion") #other otpions to be added later |
|
135 |
interpolation_method<-c("gam_CAI") #param 2 |
|
136 |
CRS_interp <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0" #param 3 |
|
137 |
#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"; |
|
138 |
|
|
139 |
out_region_name<-"" |
|
140 |
list_models<-c("y_var ~ s(lat,lon,k=5) + s(elev_s,k=3) + s(LST,k=3)") #param 4 |
|
141 |
|
|
142 |
#reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia) |
|
143 |
#master directory containing the definition of tile size and tiles predicted |
|
144 |
#in_dir1 <- "/nobackupp6/aguzman4/climateLayers/out/" #param 5, arg 2 |
|
145 |
in_dir1 <- args[2] #param 5, arg 2 |
|
146 |
|
|
147 |
#region_names <- c("reg1","reg2","reg3","reg4","reg5","reg6") #selected region names, #PARAM2 |
|
148 |
#region_names <- c("reg23","reg4") #selected region names, |
|
149 |
#run assessment by region, this is a unique region only |
|
150 |
#region_name <- c("reg4") #param 6, arg 3 |
|
151 |
region_name <- args[3] #param 6, arg 3 |
|
152 |
|
|
153 |
#out_prefix <- "run_global_analyses_pred_12282015" #param 7, arg 4 |
|
154 |
#out_dir <- "/nobackupp8/bparmen1/" #param 8, arg 5 |
|
155 |
#out_dir <-paste(out_dir,"_",out_prefix,sep="") |
|
156 |
create_out_dir_param <- TRUE #param 9, arg 6 |
|
157 |
out_prefix <- args[4] #param 7, arg 4 |
|
158 |
out_dir <- args[5] #param 8, arg 5 |
|
159 |
#out_dir <-paste(out_dir,"_",out_prefix,sep="") |
|
160 |
create_out_dir_param <- args[6] #param 9, arg 6 |
|
161 |
|
|
162 |
#list_year_predicted <- 1984:2004 |
|
163 |
#list_year_predicted <- c("2014") # param 10, arg 7 |
|
164 |
#year_predicted <- list_year_predicted[1] |
|
165 |
list_year_predicted <- args[7] # param 10, arg 7 |
|
166 |
|
|
167 |
file_format <- ".tif" #format for mosaiced files # param 11 |
|
168 |
NA_flag_val <- -9999 #No data value, # param 12 |
|
169 |
#num_cores <- 6 #number of cores used # param 13, arg 8 |
|
170 |
plotting_figures <- TRUE #running part2 of assessment to generate figures... # param 14 |
|
171 |
num_cores <- args[8] #number of cores used # param 13, arg 8 |
|
172 |
|
|
173 |
##Additional parameters used in part 2, some these may be removed as code is simplified |
|
174 |
mosaic_plot <- FALSE #param 15 |
|
175 |
day_to_mosaic <- c("19920102","19920103","19920103") #param 16, not in use... |
|
176 |
multiple_region <- TRUE #param 17 |
|
177 |
countries_shp <- "/nobackupp8/bparmen1/NEX_data/countries.shp" #param 18 |
|
178 |
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #Atlas |
|
179 |
plot_region <- TRUE # param 19 |
|
180 |
threshold_missing_day <- c(367,365,300,200) # param 20 |
|
181 |
|
|
182 |
#max number of cells to read in memory |
|
183 |
max_mem <- args[9] #param 21 |
|
184 |
in_dir_list_filename <- args[10] #param 22 |
|
185 |
#rasterOptions(maxmemory=1e+07,timer=TRUE) |
|
186 |
|
|
187 |
|
|
188 |
list_param_run_assessment_part2_plotting <-list( |
|
189 |
in_dir,y_var_name, interpolation_method, out_suffix, |
|
190 |
out_dir, create_out_dir_param, mosaic_plot, proj_str, file_format, NA_flag_val, |
|
191 |
multiple_region, countries_shp, plot_region, num_cores, |
|
192 |
region_name, df_assessment_files_name, threshold_missing_day,year_predicted |
|
193 |
) |
|
194 |
|
|
195 |
names(list_param_run_assessment_part2_plotting) <- c( |
|
196 |
"in_dir","y_var_name","interpolation_method","out_suffix", |
|
197 |
"out_dir","create_out_dir_param","mosaic_plot","proj_str","file_format","NA_flag_val", |
|
198 |
"multiple_region","countries_shp","plot_region","num_cores", |
|
199 |
"region_name","df_assessment_files_name","threshold_missing_day","year_predicted" |
|
200 |
) |
|
201 |
|
|
202 |
list_param_run_assessment_combined_region_plotting_prediction <-list( |
|
203 |
in_dir_list_filename, |
|
204 |
in_dir,y_var_name, interpolation_method, out_suffix, |
|
205 |
out_dir, create_out_dir_param, mosaic_plot, proj_str, file_format, NA_flag_val, |
|
206 |
multiple_region, countries_shp, plot_region, num_cores, |
|
207 |
region_name, df_assessment_files_name, threshold_missing_day,year_predicted) |
|
208 |
|
|
209 |
names(list_param_run_assessment_combined_region_plotting_prediction) <- c( |
|
210 |
"in_dir_list_filename", |
|
211 |
"in_dir","y_var_name","interpolation_method","out_suffix", |
|
212 |
"out_dir","create_out_dir_param","mosaic_plot","proj_str","file_format","NA_flag_val", |
|
213 |
"multiple_region","countries_shp","plot_region","num_cores", |
|
214 |
"region_name","df_assessment_files_name","threshold_missing_day","year_predicted") |
|
215 |
|
|
216 |
i <- 1 #this select the first year of list_year_predicted |
|
217 |
#Step 1: run figures production by region using table (part2 assessment script) |
|
218 |
#Step 2: run figures and tables generation across region and years |
|
219 |
#Step 3: latex/slidify presentation? |
|
220 |
#Step 4: latex/slidify presentation? |
|
221 |
|
|
222 |
if (stages_to_run[8]==8){ |
|
223 |
|
|
224 |
#Step 1: run individual figure production if needed: |
|
225 |
if(run_figure_by_year==TRUE){ |
|
226 |
#debug(run_assessment_plotting_prediction_fun) |
|
227 |
df_assessment_figures_files <- |
|
228 |
run_assessment_plotting_prediction_fun(list_param_run_assessment_plotting) |
|
229 |
} |
|
230 |
|
|
231 |
#Step 2: run combination of all files... |
|
232 |
|
|
233 |
#function_assessment_part2 <- "global_run_scalingup_assessment_part2_01032016.R" |
|
234 |
#source(file.path(script_path,function_assessment_part2)) #source all functions used in this script |
|
235 |
|
|
236 |
#debug(run_assessment_combined_region_plotting_prediction_fun) |
|
237 |
df_assessment_combined_figures_files <- |
|
238 |
run_assessment_combined_region_plotting_prediction_fun(list_param_run_assessment_combined_region_plotting_prediction) |
|
239 |
} |
|
240 |
|
|
241 |
|
|
242 |
############### END OF SCRIPT ################### |
|
243 |
##################################################### |
|
244 |
|
|
245 |
# #LAND COVER INFORMATION |
|
246 |
# LC1: Evergreen/deciduous needleleaf trees |
|
247 |
# LC2: Evergreen broadleaf trees |
|
248 |
# LC3: Deciduous broadleaf trees |
|
249 |
# LC4: Mixed/other trees |
|
250 |
# LC5: Shrubs |
|
251 |
# LC6: Herbaceous vegetation |
|
252 |
# LC7: Cultivated and managed vegetation |
|
253 |
# LC8: Regularly flooded shrub/herbaceous vegetation |
|
254 |
# LC9: Urban/built-up |
|
255 |
# LC10: Snow/ice |
|
256 |
# LC11: Barren lands/sparse vegetation |
|
257 |
# LC12: Open water |
|
258 |
|
|
259 |
#"Rscript %s %s wgs84Grid %s %s %s %s/subset/mean_LST_%s_jan_6_wgs84.tif FALSE %s/%s/covar_obj_%s.RData %s/%s/%s/met_stations_outfiles_obj_gam_CAI_%s.RData 10 4800 %s %s > %s/outLogs/%s_stage4_%s.log 2> %s/outLogs/%s_stage4_err_%s.log" % (scriptFn,ll,ll,outputFolder,b[0],outputFolder,ll,outputFolder,ll,ll,outputFolder,ll,year,ll,year,yearInt,outputFolder,ll,year,outputFolder,ll,year) |
|
260 |
#print outSt |
|
261 |
|
Also available in: Unified diff
initial commit stage 8 master script