Project

General

Profile

« Previous | Next » 

Revision 6f687814

Added by Benoit Parmentier almost 12 years ago

Data preparation, reduction of arguments, and gathering input and outputs to create function

View differences:

climate/research/oregon/interpolation/Database_stations_extraction_raster_covariates_processing.R
9 9
# 5)the location of raser covariate stack.                                                                                             
10 10
#The outputs are text files and a shape file of a time subset of the database                    
11 11
#AUTHOR: Benoit Parmentier                                                                       
12
#DATE: 02/08/2013                                                                                 
12
#DATE: 03/01/2013                                                                                 
13 13
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--     
14 14
#Comments and TODO
15 15
#-Add buffer option...
16 16
#-Add calculation of monthly mean...
17
#Outputs are:
18
#
17 19
##################################################################################################
18 20

  
19 21
###Loading R library and packages   
......
33 35
var <- "TMAX"                    #name of the variables to keep: TMIN, TMAX or PRCP
34 36
year_start<-"2010"               #starting year for the query (included)
35 37
year_end<-"2011"                 #end year for the query (excluded)
36
infile1<- "outline_venezuela_region__VE_01292013.shp"      #This is the shape file of outline of the study area.                                              #It is projected alreaday
37
infile2<-"ghcnd-stations.txt"                             #This is the textfile of station locations from GHCND
38
year_start_clim<-"2000"          #starting year for monthly query to calculate clime
39
infile1<- "outline_venezuela_region__VE_01292013.shp"      #This is the shape file of outline of the study area 
40
                                                           #It is an input/output of the covariate script
41
infile2<-"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt"                              #This is the textfile of station locations from GHCND
38 42
infile3<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script
39 43

  
40 44
new_proj<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
41
locs_coord<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0")
42
CRS_locs_WGS84<-"+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0"
45
#CRS_locs_WGS84<-"+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0"
46
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84: same as earlier
47

  
43 48
##Paths to inputs and output
44
in_path <- "/home/parmentier/Data/benoit_test"
45 49
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
46
out_path<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data/"
47
ghcnd_path<- "/home/layers/data/climate/ghcn/v2.92-upd-2012052822"
50

  
48 51
setwd(in_path) 
49
out_suffix<-"y2010_2010_VE_02082013"                                                 #User defined output prefix
50
out_region_name<-"_venezuela_region"
51
#out_suffix<-"_VE_01292013"
52

  
53
out_prefix<-"_365d_GAM_fus5_all_lstd_02202013"                #User defined output prefix
54

  
55
#PUT ALL THE INPUT ARGUMENTS IN ONE OBJECT??? read the object as input!!!
52 56

  
53 57
### Functions used in the script
54 58

  
......
74 78
interp_area <- readOGR(".",filename)
75 79
CRS_interp<-proj4string(interp_area)         #Storing the coordinate information: geographic coordinates longlat WGS84
76 80

  
77
dat_stat <- read.fwf(file.path(ghcnd_path,"ghcnd-stations.txt"), widths = c(11,9,10,7,3,31,4,4,6),fill=TRUE)
81
#infile2<-"ghcnd-stations.txt"                              #This is the textfile of station locations from GHCND
82
dat_stat <- read.fwf(infile2, 
83
                     widths = c(11,9,10,7,3,31,4,4,6),fill=TRUE)
78 84
colnames(dat_stat)<-c("STAT_ID","lat","lon","elev","state","name","GSNF","HCNF","WMOID")
79 85
coords<- dat_stat[,c('lon','lat')]
80 86
coordinates(dat_stat)<-coords
81
proj4string(dat_stat)<-locs_coord #this is the WGS84 projection
87
proj4string(dat_stat)<-CRS_locs_WGS84 #this is the WGS84 projection
82 88
#proj4string(dat_stat)<-CRS_interp
83 89
dat_stat2<-spTransform(dat_stat,CRS(new_proj))         # Project from WGS84 to new coord. system
84 90

  
......
94 100
#only 357 station for Venezuela??
95 101

  
96 102
####
97
##Add buffer option? 
103
##TODO: Add buffer option? 
98 104
####
99 105

  
100 106
#### STEP 2: Connecting to the database and query for relevant data 
......
126 132
                                                   #Wrong label...it is in fact projected...
127 133
coordinates(data_reg)<-coords                      #Assign coordinates to the data frame
128 134
#proj4string(data3)<-locs_coord                  #Assign coordinates reference system in PROJ4 format
129
proj4string(data_reg)<-locs_coord                #Assign coordinates reference system in PROJ4 format
135
proj4string(data_reg)<-CRS_locs_WGS84                #Assign coordinates reference system in PROJ4 format
130 136
data_reg<-spTransform(data_reg,CRS(new_proj))     #Project from WGS84 to new coord. system
131 137

  
138
#png...output?
132 139
plot(interp_area, axes =TRUE)
133 140
plot(stat_reg, pch=1, col="red", cex= 0.7, add=TRUE)
134 141
plot(data_reg,pch=2,col="blue",cex=2,add=TRUE)
......
136 143
##################################################################
137 144
### STEP 3: Save results and outuput in textfile and a shape file
138 145

  
139
#Save a textfile of the locations of meteorological stations in the study area
140
write.table(as.data.frame(stat_reg), file=file.path(in_path,paste("stations",out_region_name,"_",
141
                                                          out_suffix,".txt",sep="")),sep=",")
142
outfile<-paste("stations",out_region_name,"_",
143
               out_suffix,sep="")
144
writeOGR(stat_reg,dsn= ".",layer= outfile, driver="ESRI Shapefile",overwrite_layer=TRUE)
146
#Save shape files of the locations of meteorological stations in the study area
147
outfile1<-file.path(in_path,paste("stations","_",out_prefix,".shp",sep=""))
148
writeOGR(stat_reg,dsn= ".",layer= sub(".shp","",outfile1), driver="ESRI Shapefile",overwrite_layer=TRUE)
145 149

  
146
outfile<-paste("ghcn_data_",var,out_suffix,sep="")         #Name of the file
150
outfile2<-file.path(in_path,paste("ghcn_data_",var,"_",year_start_clim,"_",year_end,out_prefix,".shp",sep=""))         #Name of the file
147 151
#writeOGR(data_proj, paste(outfile, "shp", sep="."), outfile, driver ="ESRI Shapefile") #Note that the layer name is the file name without extension
148
writeOGR(data_reg,dsn= ".",layer= outfile, driver="ESRI Shapefile",overwrite_layer=TRUE)
152
writeOGR(data_reg,dsn= ".",layer= sub(".shp","",outfile2), driver="ESRI Shapefile",overwrite_layer=TRUE)
149 153

  
150 154
###################################################################
151 155
### STEP 4: Extract values at stations from covariates stack of raster images
152 156
#Eventually this step may be skipped if the covariates information is stored in the database...
153 157

  
154
#The names of covariates can be changed...
158
#The names of covariates can be changed...these names should be output/input from covar script!!!
155 159
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
156 160
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
157 161
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",
......
185 189
}
186 190

  
187 191
#write out a new shapefile (including .prj component)
188
outfile<-paste("daily_covariates_ghcn_data_",var,out_suffix,sep="")         #Name of the file
189
writeOGR(data_RST_SDF,dsn= ".",layer= outfile, driver="ESRI Shapefile",overwrite_layer=TRUE)
192
outfile3<-file.path(in_path,paste("daily_covariates_ghcn_data_",var,out_prefix,".shp",sep=""))         #Name of the file
193
writeOGR(data_RST_SDF,dsn= ".",layer= sub(".shp","",outfile3), driver="ESRI Shapefile",overwrite_layer=TRUE)
190 194

  
191 195
###############################################################
192 196
######## STEP 5: Preparing monthly averages from the ProstGres database
......
194 198
drv <- dbDriver("PostgreSQL")
195 199
db <- dbConnect(drv, dbname=db.name)
196 200

  
197
year_start<-2000
201
#year_start_clim: set at the start of the script
198 202
year_end<-2011
199 203
time1<-proc.time()    #Start stop watch
200 204
list_s<-format_s(stat_reg$STAT_ID)
201 205
data_m<-dbGetQuery(db, paste("SELECT *
202 206
                            FROM ghcn
203 207
                            WHERE element=",shQuote(var),
204
                            "AND year>=",year_start,
208
                            "AND year>=",year_start_clim,
205 209
                            "AND year<",year_end,
206 210
                            "AND station IN ",list_s,";",sep=""))  #Selecting station using a SQL query
207 211
time_duration<-proc.time()-time1             #Time for the query may be long given the size of the database
......
246 250

  
247 251
####
248 252
#write out a new shapefile (including .prj component)
249
outfile<-paste("monthly_covariates_ghcn_data_",var,out_suffix,sep="")         #Name of the file
250 253
dst$OID<-1:nrow(dst) #need a unique ID?
251
writeOGR(dst,dsn= ".",layer= outfile, driver="ESRI Shapefile",overwrite_layer=TRUE)
254
outfile4<-file.path(in_path,paste("monthly_covariates_ghcn_data_",var,"_",year_start_clim,"_",year_end,out_prefix,".shp",sep=""))  #Name of the file
255
writeOGR(dst,dsn= ".",layer= sub(".shp","",outfile4), driver="ESRI Shapefile",overwrite_layer=TRUE)
256

  
257
### list of output return
252 258

  
259
outfiles_obj<-list(outfile1,outfile2,outfile3,outfile4,outfile5)
260
outfiles_obj<cname("loc_stations","loc_stations_ghcn","daily_covar_ghcn_data","monthly_covar_ghcn_data")
261
#return(outfiles_obj)
253 262
##### END OF SCRIPT ##########

Also available in: Unified diff