Project

General

Profile

« Previous | Next » 

Revision 760957d7

Added by Benoit Parmentier almost 12 years ago

GAM fusion raster prediction, further modificationfor Venzuela tmax interp

View differences:

climate/research/oregon/interpolation/GAM_fusion_analysis_raster_prediction_multisampling.R
10 10
#4)use seed number: use seed if random samples must be repeatable
11 11
#5)GAM fusion: possibilty of running GAM+FUSION or GAM separately 
12 12
#AUTHOR: Benoit Parmentier                                                                        
13
#DATE: 02/06/2013                                                                                 
13
#DATE: 02/08/2013                                                                                 
14 14
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--                                   
15 15
###################################################################################################
16 16

  
......
29 29
library(plotrix)
30 30
### Parameters and argument
31 31

  
32
infile1<- "ghcn_or_tmax_covariates_06262012_OR83M.shp"             #GHCN shapefile containing variables for modeling 2010                 
33 32
infile2<-"list_365_dates_04212012.txt"
34
#infile3<-"LST_dates_var_names.txt"                        #LST dates name
35
#infile4<-"models_interpolation_05142012.txt"              #Interpolation model names
36
#infile5<-"mean_day244_rescaled.rst"                       #Raster or grid for the locations of predictions
37
#infile6<-"lst_climatology.txt"
38
#infile6<-"LST_files_monthly_climatology.txt"
39
#inlistf<-"list_files_05032012.txt"                        #Stack of images containing the Covariates
40

  
41
infile_monthly<-"monthly_covariates_ghcn_data_TMAXy2010_2010_VE_02062013.shp"
42
infile_daily<-"daily_covariates_ghcn_data_TMAXy2010_2010_VE_02062013.shp"
43
infile_locs<-"stations_venezuela_region_y2010_2010_VE_02062013.shp"
33
infile_monthly<-"monthly_covariates_ghcn_data_TMAXy2010_2010_VE_02082013.shp"
34
infile_daily<-"daily_covariates_ghcn_data_TMAXy2010_2010_VE_02082013.shp"
35
infile_locs<-"stations_venezuela_region_y2010_2010_VE_02082013.shp"
44 36
infile3<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script
45 37

  
46 38
in_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data"
47 39
out_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data"
40
script_path<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/"
48 41
setwd(in_path)
49 42

  
50 43
nmodels<-9   #number of models running
51 44
y_var_name<-"dailyTmax"
52 45
predval<-1
53
prop<-0.3             #Proportion of testing retained for validation   
54
#prop<-0.25
55 46
seed_number<- 100  #if seed zero then no seed?                                                                 #Seed number for random sampling
56
out_prefix<-"_10d_GAM_fus5_all_lstd_020632013"                #User defined output prefix
57
#out_prefix<-"_365d_GAM_12272012"                #User defined output prefix
47
out_prefix<-"_10d_GAM_fus5_all_lstd_02082013"                #User defined output prefix
58 48

  
59 49
bias_val<-0            #if value 1 then training data is used in the bias surface rather than the all monthly stations
60 50
bias_prediction<-1     #if value 1 then use GAM for the BIAS prediction otherwise GAM direct repdiction for y_var (daily tmax)
......
64 54
step<-0         
65 55
constant<-0             #if value 1 then use the same samples as date one for the all set of dates
66 56
#projection used in the interpolation of the study area: should be read directly from the outline of the study area
67
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";
57
#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";
68 58
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
69 59

  
70
source("GAM_fusion_function_multisampling_02062013.R")
60
source(file.path(script_path,"GAM_fusion_function_multisampling_02082013.R"))
71 61

  
72 62
###################### START OF THE SCRIPT ########################
73 63

  
74 64
###Reading the daily station data and setting up for models' comparison
75
#filename<-sub(".shp","",infile1)             #Removing the extension from file.
76
#ghcn<-readOGR(".", filename)                 #reading shapefile 
77 65
ghcn<-readOGR(dsn=in_path,layer=sub(".shp","",infile_daily))
78 66
CRS_interp<-proj4string(ghcn)                       #Storing projection information (ellipsoid, datum,etc.)
79 67

  
80
#mean_LST<- readGDAL(infile5)                 #Reading the whole raster in memory. This provides a grid for kriging
81
#proj4string(mean_LST)<-CRS_interp                   #Assigning coordinate information to prediction grid.
82

  
83
#Station location of the study area
84
#stat_loc<-read.table(paste(path,"/","location_study_area_OR_0602012.txt",sep=""),sep=",", header=TRUE)
85 68
stat_loc<-readOGR(dsn=in_path,layer=sub(".shp","",infile_locs))
86 69

  
87
#GHCN Database for 1980-2010 for study area (OR) 
88
#data3<-read.table(paste(path,"/","ghcn_data_TMAXy1980_2010_OR_0602012.txt",sep=""),sep=",", header=TRUE)
89
#data3<-file.path(in_path,infile_monthly)
90 70
data3<-readOGR(dsn=in_path,layer=sub(".shp","",infile_monthly))
91 71

  
92
#Remove NA for LC and CANHEIGHT: Need to check this part
72
#Remove NA for LC and CANHEIGHT: Need to check this part after
93 73
ghcn$LC1[is.na(ghcn$LC1)]<-0
94 74
ghcn$LC3[is.na(ghcn$LC3)]<-0
95 75
ghcn$CANHEIGHT[is.na(ghcn$CANHEIGHT)]<-0
96 76
ghcn$LC4[is.na(ghcn$LC4)]<-0
97 77
ghcn$LC6[is.na(ghcn$LC6)]<-0
98 78

  
99
dates <-readLines(paste(file.path(in_path,infile2))
100
#LST_dates <-readLines(file.path(in_path,infile3))
101
#models <-readLines(paste(path,"/",infile4, sep=""))
79
dates <-readLines(file.path(in_path,infile2)) #dates to be predicted
102 80

  
103 81
##Extracting the variables values from the raster files                                             
104 82

  
105 83
#The names of covariates can be changed...
106
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
84
rnames <-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
107 85
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
108 86
lst_names<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12",
109 87
                    "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
......
115 93
names(s_raster)<-covar_names               #Assigning names to the raster layers: making sure it is included in the extraction
116 94

  
117 95
#Deal with no data value and zero      
118
pos<-match("LC1",layerNames(s_raster)) #Find column with name "value"
119
LC1<-raster(s_raster,layer=pos)             #Select layer from stack
120
s_raster<-dropLayer(s_raster,pos)
121
LC1[is.na(LC1)]<-0
122

  
123
pos<-match("LC3",layerNames(s_raster)) #Find column with name "value"
124
LC3<-raster(s_raster,layer=pos)             #Select layer from stack
125
s_raster<-dropLayer(s_raster,pos)
126
LC3[is.na(LC3)]<-0
127

  
128
pos<-match("CANHEIGHT",layerNames(s_raster)) #Find column with name "value"
129
CANHEIGHT<-raster(s_raster,layer=pos)             #Select layer from stack
130
s_raster<-dropLayer(s_raster,pos)
131
CANHEIGHT[is.na(CANHEIGHT)]<-0
132
pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM"
133
ELEV_SRTM<-raster(s_raster,layer=pos)             #Select layer from stack on 10/30
134
s_raster<-dropLayer(s_raster,pos)
135
ELEV_SRTM[ELEV_SRTM <0]<-NA
96
#pos<-match("LC1",layerNames(s_raster)) #Find column with name "value"
97
#LC1<-raster(s_raster,layer=pos)             #Select layer from stack
98
#s_raster<-dropLayer(s_raster,pos)
99
#LC1[is.na(LC1)]<-0
100

  
101
#pos<-match("LC3",layerNames(s_raster)) #Find column with name "value"
102
#LC3<-raster(s_raster,layer=pos)             #Select layer from stack
103
#s_raster<-dropLayer(s_raster,pos)
104
#LC3[is.na(LC3)]<-0
105

  
106
#pos<-match("CANHEIGHT",layerNames(s_raster)) #Find column with name "value"
107
#CANHEIGHT<-raster(s_raster,layer=pos)             #Select layer from stack
108
#s_raster<-dropLayer(s_raster,pos)
109
#CANHEIGHT[is.na(CANHEIGHT)]<-0
110
#pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM"
111
#ELEV_SRTM<-raster(s_raster,layer=pos)             #Select layer from stack on 10/30
112
#s_raster<-dropLayer(s_raster,pos)
113
#ELEV_SRTM[ELEV_SRTM <0]<-NA
136 114

  
137 115
#s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame
138 116

  
139
####### Preparing LST stack of climatology...
140

  
141
#l=list.files(pattern="mean_month.*rescaled.rst")
142
l <-readLines(paste(path,"/",infile6, sep=""))
143
molst<-stack(l)  #Creating a raster stack...
144
#setwd(old)
145
molst<-molst-273.16  #K->C          #LST stack of monthly average...
146
idx <- seq(as.Date('2010-01-15'), as.Date('2010-12-15'), 'month')
147
molst <- setZ(molst, idx)
148
layerNames(molst) <- month.abb
149

  
150 117
######  Preparing tables for model assessment: specific diagnostic/metrics
151 118

  
152 119
#Model assessment: specific diagnostics/metrics
......
164 131
results_RMSE_f<- matrix(1,1,nmodels+4)    #RMSE fit, RMSE for the training dataset
165 132
results_MAE_f <- matrix(1,1,nmodels+4)
166 133

  
167
######## Preparing monthly averages from the ProstGres database and extracting covarvariates from stack
168

  
169
# do this work outside of (before) this function
170
# to avoid making a copy of the data frame inside the function call
171
# date1<-ISOdate(data3$year,data3$month,data3$day) #Creating a date object from 3 separate column
172
# date2<-as.POSIXlt(as.Date(date1))
173
# data3$date<-date2
174
# d<-subset(data3,year>=2000 & mflag=="0" ) #Selecting dataset 2000-2010 with good quality: 193 stations
175
# #May need some screeing??? i.e. range of temp and elevation...
176
# d1<-aggregate(value~station+month, data=d, mean)  #Calculate monthly mean for every station in OR
177
# id<-as.data.frame(unique(d1$station))     #Unique station in OR for year 2000-2010: 193 but 7 loss of monthly avg    
178
# 
179
# dst<-merge(d1, stat_loc, by.x="station", by.y="STAT_ID")   #Inner join all columns are retained
180
# 
181
# #This allows to change only one name of the data.frame
182
# pos<-match("value",names(dst)) #Find column with name "value"
183
# names(dst)[pos]<-c("TMax")
184
# dst$TMax<-dst$TMax/10                #TMax is the average max temp for monthy data
185
# #dstjan=dst[dst$month==9,]  #dst contains the monthly averages for tmax for every station over 2000-2010
186
# 
187
# #Extracting covariates from stack
188
# coords<- dst[c('lon','lat')]              #Define coordinates in a data frame
189
# coordinates(dst)<-coords                      #Assign coordinates to the data frame
190
# proj4string(dst)<-CRS_locs_WGS84                  #Assign coordinates reference system in PROJ4 format
191
# dst_month<-spTransform(dst,CRS(CRS_interp))     #Project from WGS84 to new coord. system
192
# 
193
# stations_val<-extract(s_raster,dst_month)  #extraction of the infomration at station location
194
# stations_val<-as.data.frame(stations_val)
195
# dst_extract<-cbind(dst_month,stations_val)
196
# dst<-dst_extract
197
                  
198
#Now clean and screen monthly values
199
#dst_all<-dst
200
dst_all<-data3
201
dst<-data3
202
#dst<-subset(dst,dst$TMax>-15 & dst$TMax<45) #may choose different threshold??
203
#dst<-subset(dst,dst$ELEV_SRTM>0) #This will drop two stations...or 24 rows
204

  
205
######### Preparing daily values for training and testing
134
######### Preparing daily and monthly values for training and testing
206 135
                  
207
#Screening for bad values: value is tmax in this case
136
#Screening for daily bad values: value is tmax in this case
208 137
#ghcn$value<-as.numeric(ghcn$value)
209 138
#ghcn_all<-ghcn
210 139
#ghcn_test<-subset(ghcn,ghcn$value>-150 & ghcn$value<400)
......
213 142
#ghcn<-ghcn_test2
214 143
#coords<- ghcn[,c('x_OR83M','y_OR83M')]
215 144

  
145
#Now clean and screen monthly values
146
#dst_all<-dst
147
dst_all<-data3
148
dst<-data3
149
#dst<-subset(dst,dst$TMax>-15 & dst$TMax<45) #may choose different threshold??
150
#dst<-subset(dst,dst$ELEV_SRTM>0) #This will drop two stations...or 24 rows
151

  
216 152
##Sampling: training and testing sites.
217 153

  
218 154
#Make this a a function

Also available in: Unified diff