Revision 4adca9a2
Added by Benoit Parmentier over 11 years ago
climate/research/oregon/interpolation/Database_stations_covariates_processing_function.R | ||
---|---|---|
94 | 94 |
#Read in GHCND database station locations |
95 | 95 |
dat_stat <- read.fwf(infile_ghncd_data, |
96 | 96 |
widths = c(11,9,10,7,3,31,4,4,6),fill=TRUE) |
97 |
colnames(dat_stat)<-c("STAT_ID","lat","lon","elev","state","name","GSNF","HCNF","WMOID")
|
|
98 |
coords<- dat_stat[,c('lon','lat')]
|
|
97 |
colnames(dat_stat)<-c("STAT_ID","latitude","longitude","elev","state","name","GSNF","HCNF","WMOID")
|
|
98 |
coords<- dat_stat[,c('longitude','latitude')]
|
|
99 | 99 |
coordinates(dat_stat)<-coords |
100 | 100 |
proj4string(dat_stat)<-CRS_locs_WGS84 #this is the WGS84 projection |
101 | 101 |
#proj4string(dat_stat)<-CRS_interp |
... | ... | |
131 | 131 |
|
132 | 132 |
#Transform the subset data frame in a spatial data frame and reproject |
133 | 133 |
data_reg<-data_table #Make a copy of the data frame |
134 |
coords<- data_reg[c('lon','lat')] #Define coordinates in a data frame: clean up here!!
|
|
134 |
coords<- data_reg[c('longitude','latitude')] #Define coordinates in a data frame: clean up here!!
|
|
135 | 135 |
coordinates(data_reg)<-coords #Assign coordinates to the data frame |
136 | 136 |
proj4string(data_reg)<-CRS_locs_WGS84 #Assign coordinates reference system in PROJ4 format |
137 | 137 |
data_reg<-spTransform(data_reg,CRS(CRS_interp)) #Project from WGS84 to new coord. system |
... | ... | |
168 | 168 |
s_raster<-brick(infile_covariates) #read in the data stack |
169 | 169 |
names(s_raster)<-covar_names #Assigning names to the raster layers: making sure it is included in the extraction |
170 | 170 |
stat_val<- extract(s_raster, data_reg) #Extracting values from the raster stack for every point location in coords data frame. |
171 |
#stat_val_test<- extract(s_raster, data_reg,def=TRUE) |
|
171 | 172 |
|
172 | 173 |
#create a shape file and data_frame with names |
173 | 174 |
|
... | ... | |
223 | 224 |
#Save the query data here... |
224 | 225 |
data_m<-merge(data_m, stat_reg, by.x="station", by.y="STAT_ID") #Inner join all columns are retained |
225 | 226 |
#Extracting covariates from stack for the monthly dataset... |
226 |
coords<- data_m[c('lon','lat')] #Define coordinates in a data frame
|
|
227 |
coords<- data_m[c('longitude','latitude')] #Define coordinates in a data frame
|
|
227 | 228 |
coordinates(data_m)<-coords #Assign coordinates to the data frame |
228 | 229 |
proj4string(data_m)<-CRS_locs_WGS84 #Assign coordinates reference system in PROJ4 format |
229 | 230 |
data_m<-spTransform(data_m,CRS(CRS_interp)) #Project from WGS84 to new coord. system |
... | ... | |
258 | 259 |
} |
259 | 260 |
|
260 | 261 |
#Extracting covariates from stack for the monthly dataset... |
261 |
coords<- dst[c('lon','lat')] #Define coordinates in a data frame |
|
262 |
#names(dst)[5:6] <-c('latitude','longitude') |
|
263 |
coords<- dst[c('longitude','latitude')] #Define coordinates in a data frame |
|
264 |
|
|
262 | 265 |
coordinates(dst)<-coords #Assign coordinates to the data frame |
263 | 266 |
proj4string(dst)<-CRS_locs_WGS84 #Assign coordinates reference system in PROJ4 format |
264 | 267 |
dst_month<-spTransform(dst,CRS(CRS_interp)) #Project from WGS84 to new coord. system |
... | ... | |
292 | 295 |
|
293 | 296 |
outfiles_obj<-list(outfile1,outfile2,outfile3,outfile4,outfile5,outfile6) |
294 | 297 |
names(outfiles_obj)<- c("loc_stations","loc_stations_ghcn","daily_query_ghcn_data","daily_covar_ghcn_data","monthly_query_ghcn_data","monthly_covar_ghcn_data") |
298 |
save(outfiles_obj,file= file.path(out_path,paste("met_stations_outfiles_obj_",interpolation_method,"_", out_prefix,".RData",sep=""))) |
|
299 |
|
|
295 | 300 |
return(outfiles_obj) |
296 | 301 |
|
297 | 302 |
#END OF FUNCTION # |
Also available in: Unified diff
data preparation, modifications to run anywhere, Queensland test