Project

General

Profile

« Previous | Next » 

Revision bffb36c8

Added by Benoit Parmentier about 9 years ago

initial commit for stage 7, mosaicing of predictions and accuracy layers productions

View differences:

climate/research/oregon/interpolation/master_script_stage_7.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 and tiles
11
#STAGE 6: Assessement of predictions by tiles and regions 
12
#STAGE 7: Mosaicing of predicted surfaces and accuracy metrics (RMSE,MAE) by regions 
13
#AUTHOR: Benoit Parmentier                                                                        
14
#CREATED ON: 01/01/2015  
15
#MODIFIED ON: 01/05/2016  
16
#PROJECT: NCEAS INPLANT: Environment and Organisms                                                                           
17

  
18
## TODO:
19
# 
20
# Clean up temporary files
21
# 
22
##################################################################################################
23

  
24
###Loading R library and packages  ou 
25
library(RPostgreSQL)
26
library(maps)
27
library(maptools)
28
library(parallel)
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(rasterVis)
38
library(spgwr)
39
library(reshape)
40
library(plotrix)
41

  
42
######## PARAMETERS FOR WORK FLOW #########################
43
### Need to add documentation ###
44

  
45
#Adding command line arguments to use mpiexec
46
args<-commandArgs(TRUE)
47
#script_path<-"/nobackupp6/aguzman4/climateLayers/finalCode/environmental-layers/climate/research/oregon/interpolation"
48
#dataHome<-"/nobackupp6/aguzman4/climateLayers/interp/testdata/"
49
#script_path2<-"/nobackupp6/aguzman4/climateLayers/finalCode/environmental-layers/climate/research/world/interpolation"
50

  
51
#CALLED FROM MASTER SCRIPT:
52

  
53
script_path <- "/nobackupp8/bparmen1/env_layers_scripts" #path to script
54
function_mosaicing_functions <- "global_run_scalingup_mosaicing_function_12192015.R" #PARAM12
55
function_mosaicing <-"global_run_scalingup_mosaicing_01012016.Rglobal_run_scalingup_mosaicing_01052016.R"
56
source(file.path(function_mosaicing)) #source all functions used in this script 
57
source(file.path(function_mosaicing_functions)) #source all functions used in this script 
58

  
59
### Parameters and arguments ###
60
  
61
var<-"TMAX" # variable being interpolated
62
if (var == "TMAX") {
63
  y_var_name <- "dailyTmax"
64
  y_var_month <- "TMax"
65
}
66
if (var == "TMIN") {
67
  y_var_name <- "dailyTmin"
68
  y_var_month <- "TMin"
69
}
70

  
71
#interpolation_method<-c("gam_fusion") #other otpions to be added later
72
interpolation_method<-c("gam_CAI")
73
CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
74
#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";
75
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
76

  
77
out_region_name<-""
78
list_models<-c("y_var ~ s(lat,lon,k=5) + s(elev_s,k=3) + s(LST,k=3)")
79

  
80
#reg1 (North Am), reg2(Europe),reg3(Asia), reg4 (South Am), reg5 (Africa), reg6 (Australia-Asia)
81
#master directory containing the definition of tile size and tiles predicted
82
in_dir1 <- "/nobackupp6/aguzman4/climateLayers/out/"
83
#/nobackupp6/aguzman4/climateLayers/out_15x45/1982
84

  
85
#region_names <- c("reg23","reg4") #selected region names, #PARAM2
86
region_name <- c("reg4") #run assessment by region, this is a unique region only
87
#region_names <- c("reg1","reg2","reg3","reg4","reg5","reg6") #selected region names, #PARAM2
88
interpolation_method <- c("gam_CAI") #PARAM4
89
out_prefix <- "run_global_analyses_pred_12282015" #PARAM5
90
out_dir <- "/nobackupp8/bparmen1/" #PARAM6
91
#out_dir <-paste(out_dir,"_",out_prefix,sep="")
92
create_out_dir_param <- TRUE #PARAM7
93

  
94
#CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
95
#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";
96
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
97

  
98
#list_year_predicted <- 1984:2004
99
list_year_predicted <- c("2014")
100
#year_predicted <- list_year_predicted[1]
101

  
102
file_format <- ".tif" #format for mosaiced files #PARAM10
103
NA_flag_val <- -9999  #No data value, #PARAM11
104
num_cores <- 6 #number of cores used #PARAM13
105
plotting_figures <- TRUE #running part2 of assessment to generate figures...
106
  
107
##Additional parameters used in part 2, some these may be removed as code is simplified
108
mosaic_plot <- FALSE #PARAM14
109
day_to_mosaic <- c("19920102","19920103","19920103") #PARAM15
110
multiple_region <- TRUE #PARAM16
111
countries_shp <- "/nobackupp8/bparmen1/NEX_data/countries.shp" #PARAM17
112
#countries_shp <-"/data/project/layers/commons/NEX_data/countries.shp" #Atlas
113
plot_region <- TRUE  #PARAM18
114
threshold_missing_day <- c(367,365,300,200)#PARAM19
115

  
116
list_param_run_assessment_prediction <- list(in_dir1,region_name,y_var_name,interpolation_method,out_prefix,
117
                                  out_dir,create_out_dir_param,CRS_locs_WGS84,list_year_predicted,
118
                                  file_format,NA_flag_val,num_cores,plotting_figures,
119
                                  mosaic_plot,day_to_mosaic,multiple_region,countries_shp,plot_region)
120
list_names <- c("in_dir1","region_name","y_var_name","interpolation_method","out_prefix",
121
                                  "out_dir","create_out_dir_param","CRS_locs_WGS84","list_year_predicted",
122
                                  "file_format","NA_flag_val","num_cores","plotting_figures",
123
                                  "mosaic_plot","day_to_mosaic","multiple_region","countries_shp","plot_region")
124
                                      
125

  
126
names(list_param_run_assessment_prediction)<-list_names
127
  
128
#max number of cells to read in memory
129
max_mem<-args[11]
130
#rasterOptions(maxmemory=1e+07,timer=TRUE)
131

  
132
#debug(run_assessment_prediction_fun)
133
#debug(debug_fun_test)
134
#debug_fun_test(list_param_raster_prediction)
135
i <- 1 #this select the first year of list_year_predicted
136
if (stages_to_run[6]==6){
137
  assessment_prediction_obj <- run_assessment_prediction_fun(i,list_param_run_assessment_prediction)
138
}
139

  
140
## Add stage 7 (mosaicing) here??
141
#i <- 1 #this select the first year of list_year_predicted
142
#if (stages_to_run[7]==7){
143
#  assessment_prediction_obj <- run_assessment_prediction_fun(i,list_param_run_assessment_prediction)
144
#}
145

  
146
###############   END OF SCRIPT   ###################
147
#####################################################
148

  
149
# #LAND COVER INFORMATION
150
# LC1: Evergreen/deciduous needleleaf trees
151
# LC2: Evergreen broadleaf trees
152
# LC3: Deciduous broadleaf trees
153
# LC4: Mixed/other trees
154
# LC5: Shrubs
155
# LC6: Herbaceous vegetation
156
# LC7: Cultivated and managed vegetation
157
# LC8: Regularly flooded shrub/herbaceous vegetation
158
# LC9: Urban/built-up
159
# LC10: Snow/ice
160
# LC11: Barren lands/sparse vegetation
161
# LC12: Open water
162

  

Also available in: Unified diff