Revision b345fd0a
Added by Benoit Parmentier over 11 years ago
climate/research/oregon/interpolation/Database_stations_covariates_processing_function.R | ||
---|---|---|
9 | 9 |
# 2) var: the variable of interest - "TMAX","TMIN" or "PRCP" |
10 | 10 |
# 3) range_years: range of records used in the daily interpolation, note that upper bound year is not included |
11 | 11 |
# 4) range_years_clim: range of records used in the monthly climatology interpolation, note that upper bound is not included |
12 |
# 5) infile1: region outline as a shape file - used in the interpolation stage too
|
|
13 |
# 6) infile2: ghcnd stations locations as a textfile name with lat-long fields
|
|
12 |
# 5) infile_reg_outline: region outline as a shape file - used in the interpolation stage too
|
|
13 |
# 6) infile_ghncd_data: ghcnd stations locations as a textfile name with lat-long fields
|
|
14 | 14 |
# 7) infile_covarariates: tif file of raser covariates for the interpolation area: it should have a local projection |
15 | 15 |
# 8) CRS_locs_WGS84: longlat EPSG 4326 used as coordinates reference system (proj4)for stations locations |
16 | 16 |
# 9) in_path: input path for covariates data and other files, this is also the output? |
... | ... | |
28 | 28 |
#6) monthly_covar_ghcn_data: ghcn monthly averaged data with covariates for the year range of interpolation (locally projected) |
29 | 29 |
|
30 | 30 |
#AUTHOR: Benoit Parmentier |
31 |
#DATE: 05/07/2013
|
|
31 |
#DATE: 05/21/2013
|
|
32 | 32 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363-- |
33 | 33 |
#Comments and TODO |
34 | 34 |
#-Add buffer option... |
... | ... | |
72 | 72 |
year_end <-list_param_prep$range_years[2] #"2011" #end year for the query (excluded) |
73 | 73 |
year_start_clim <-list_param_prep$range_years_clim[1] #right bound not included in the range!! starting year for monthly query to calculate clime |
74 | 74 |
year_end_clim <-list_param_prep$range_years_clim[2] #right bound not included in the range!! starting year for monthly query to calculate clime |
75 |
infile1<- list_param_prep$infile1 #This is the shape file of outline of the study area #It is an input/output of the covariate script
|
|
76 |
infile2<-list_param_prep$infile2 #"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt" #This is the textfile of station locations from GHCND
|
|
77 |
infile3<-list_param_prep$infile_covariates #"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script
|
|
75 |
infile_reg_outline<- list_param_prep$infile_reg_outline #This is the shape file of outline of the study area #It is an input/output of the covariate script
|
|
76 |
infile_ghncd_data<-list_param_prep$infile_ghncd_data #"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt" #This is the textfile of station locations from GHCND
|
|
77 |
infile_covariates<-list_param_prep$infile_covariates #"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script
|
|
78 | 78 |
CRS_locs_WGS84<-list_param_prep$CRS_locs_WGS84 #Station coords WGS84: same as earlier |
79 | 79 |
in_path <- list_param_prep$in_path #CRS_locs_WGS84"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/" |
80 | 80 |
out_path <- list_param_prep$out_path #CRS_locs_WGS84"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/" |
... | ... | |
83 | 83 |
out_prefix<-list_param_prep$out_prefix #"_365d_GAM_fus5_all_lstd_03012013" #User defined output prefix |
84 | 84 |
|
85 | 85 |
## working directory is the same for input and output for this function |
86 |
setwd(in_path) |
|
87 |
|
|
86 |
#setwd(in_path)
|
|
87 |
setwd(out_path) |
|
88 | 88 |
##### STEP 1: Select station in the study area |
89 | 89 |
|
90 |
filename<-sub(".shp","",infile1) #Removing the extension from file.
|
|
91 |
interp_area <- readOGR(".",filename)
|
|
90 |
filename<-sub(".shp","",infile_reg_outline) #Removing the extension from file.
|
|
91 |
interp_area <- readOGR(dsn=dirname(filename),basename(filename))
|
|
92 | 92 |
CRS_interp<-proj4string(interp_area) #Storing the coordinate information: geographic coordinates longlat WGS84 |
93 | 93 |
|
94 | 94 |
#Read in GHCND database station locations |
95 |
dat_stat <- read.fwf(infile2,
|
|
95 |
dat_stat <- read.fwf(infile_ghncd_data,
|
|
96 | 96 |
widths = c(11,9,10,7,3,31,4,4,6),fill=TRUE) |
97 | 97 |
colnames(dat_stat)<-c("STAT_ID","lat","lon","elev","state","name","GSNF","HCNF","WMOID") |
98 | 98 |
coords<- dat_stat[,c('lon','lat')] |
... | ... | |
164 | 164 |
### STEP 4: Extract values at stations from covariates stack of raster images |
165 | 165 |
#Eventually this step may be skipped if the covariates information is stored in the database... |
166 | 166 |
|
167 |
s_raster<-stack(file.path(in_path,infile3)) #read in the data stack |
|
167 |
#s_raster<-stack(file.path(in_path,infile_covariates)) #read in the data stack |
|
168 |
s_raster<-brick(infile_covariates) #read in the data stack |
|
168 | 169 |
names(s_raster)<-covar_names #Assigning names to the raster layers: making sure it is included in the extraction |
169 | 170 |
stat_val<- extract(s_raster, data_reg) #Extracting values from the raster stack for every point location in coords data frame. |
170 | 171 |
|
Also available in: Unified diff
data preparation, debugging, clean up of input parameters names