ALR 2014-8-7
Load headwaters package
library(devtools)
##
## Attaching package: 'devtools'
##
## The following objects are masked from 'package:utils':
##
## ?, help
##
## The following object is masked from 'package:base':
##
## system.file
install_github(repo = "headwaters", username = "anarosner")
## Installing github repo headwaters/master from anarosner
## Downloading master.zip from https://github.com/anarosner/headwaters/archive/master.zip
## Installing package from C:\Users\arosner\AppData\Local\Temp\1\Rtmpy4eE90/master.zip
## Installing headwaters
## "C:/R/R-31~1.1/bin/x64/R" --vanilla CMD INSTALL \
## "C:\Users\arosner\AppData\Local\Temp\1\Rtmpy4eE90\devtools1b9855463978\headwaters-master" \
## --library="C:/R/R-3.1.1/library" --install-tests
library(headwaters)
## Loading required package: reshape2
## Loading required package: ggplot2
## Loading required package: DataCombine
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
##
## Loading required package: EcoHydRology
## Loading required package: operators
##
## Attaching package: 'operators'
##
## The following object is masked from 'package:dplyr':
##
## %>%
##
## The following object is masked from 'package:base':
##
## options
##
## Loading required package: topmodel
## Loading required package: DEoptim
##
## DEoptim package
## Differential Evolution algorithm in R
## Authors: D. Ardia, K. Mullen, B. Peterson and J. Ulrich
##
## Loading required package: XML
## Loading required package: foreign
## Loading required package: lme4
## Loading required package: Matrix
## Loading required package: Rcpp
## Loading required package: lubridate
## Loading required package: maptools
## Loading required package: sp
## Checking rgeos availability: TRUE
## Loading required package: MASS
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
##
## Loading required package: nlme
##
## Attaching package: 'nlme'
##
## The following object is masked from 'package:lme4':
##
## lmList
##
## The following object is masked from 'package:dplyr':
##
## collapse
##
## Loading required package: plyr
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
##
## Attaching package: 'plyr'
##
## The following object is masked from 'package:lubridate':
##
## here
##
## The following objects are masked from 'package:dplyr':
##
## arrange, desc, failwith, id, mutate, summarise, summarize
##
## Loading required package: rgdal
## rgdal: version: 0.8-16, (SVN revision 498)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.11.0, released 2014/04/16
## Path to GDAL shared files: C:/R/R-3.1.1/library/rgdal/gdal
## GDAL does not use iconv for recoding strings.
## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
## Path to PROJ.4 shared files: C:/R/R-3.1.1/library/rgdal/proj
## Loading required package: rgeos
## rgeos version: 0.3-6, (SVN revision 450)
## GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921
## Polygon checking: TRUE
##
## Loading required package: deldir
## deldir 0.1-6
## Loading required package: USGSwsStats
## Loading required package: USGSwsBase
## Loading required package: memoise
## Loading required package: digest
##
## Attaching package: 'USGSwsBase'
##
## The following object is masked from 'package:MASS':
##
## select
##
## The following object is masked from 'package:dplyr':
##
## select
##
## Loading required package: leaps
## Loading required package: USGSwsGraphs
## Loading required package: KernSmooth
## KernSmooth 2.23 loaded
## Copyright M. P. Wand 1997-2009
## Loading required package: akima
## Loading required package: boot
## Loading required package: waterData
## Loading required package: lattice
##
## Attaching package: 'lattice'
##
## The following object is masked from 'package:boot':
##
## melanoma
##
## Loading required package: latticeExtra
## Loading required package: RColorBrewer
##
## Attaching package: 'latticeExtra'
##
## The following object is masked from 'package:ggplot2':
##
## layer
##
## Loading required package: xtable
##
## Attaching package: 'xtable'
##
## The following object is masked from 'package:maptools':
##
## label
##
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'headwaters'
##
## The following objects are masked _by_ '.GlobalEnv':
##
## capitalize, ls.objects, summary.na
Temporarily, load datat that will later on be contained in package data
#Everything in the package's data dir *should* load using load_all, or explicitly using load_data.
#But, that's not working yet... this is a temp fix
setwd("C:/ALR/Models/headwaters")
for (i in list.files(path = "./data"))
load(file.path("data",i), verbose = T )
## Loading objects:
## states.spatial
## Loading objects:
## weather.grid.coords
## Loading objects:
## weather.grid.poly
Specify file locations
model.dir<-"C:/ALR/Models/headwaters_sdoah"
buffer.file<-"park_coast_buffer"
# buffer<-readShapePoly(file.path(model.dir,"sdoah_inputs",buffer.file))
catchment.file<-"C:/ALR/Data/StreamData/NHDplus/NHDPlusCatchment/sdoah/Catchment"
Load flow gages
#Retrieve gages
g.spatial<-gage.retrieve(buffer.file = file.path(model.dir,"sdoah_inputs",buffer.file),temp.dir = file.path(model.dir,"temp"),log.dir = file.path(model.dir,"logs") )
## Warning: EOF within quoted string
## Warning: Missing geographic coordinates:
## 7 sites do not have lat coordinates and are being ignored
## 7 sites do not have long coordinates and are being ignored
## Gage retrieval complete
920 gages identified
Drainage area >0 and <=50 sq km)
#Plot gages
plot(g.spatial,col="red",pch=16)
# plot(buffer,add=T,border="red")
plot(states.spatial,add=T)
#Place gages into NHDplus catchments
g.spatial<-gage.place.nhdplus(gages.spatial = g.spatial, catchment.file = catchment.file)
## Starting to load catchments...
(this part could take a while)
Completed loading catchments...
Starting to plot gages to catchments
Completed plotting gages to catchments
## Warning: 37 gages did not map to a NHDplus catchment:
## Warning: 02045200; 02046500; 02050500; 02053400; 02053510; 0205356401; 02055100; 02063000; 02076500; 02077240; 0208273070; 02082731; 02084903; 02084904; 02084905; 02084908; 02084909; 0208524090; 02086000; 0208650112; 0209330990; 0209331325; 0209437850; 0211351575; 0211397263; 03465000; 03466228; 03473500; 03477500; 03480500; 03486311; 03486500; 03487501; 03488445; 03489850; 03491544; 03492006
#Trace dams upstream
# (not using basin characteristics impoundments, because don't have that processed for sdoah)
g.spatial<-gage.trace.dams(gages.spatial = g.spatial)
## Loading dam information...
Starting to trace network upstream for 346 gages...
... 1... 2... 3... 4... 5... 6... 7... 8... 9... 10... 11... 12... 13... 14... 15... 16... 17... 18... 19... 20... 21... 22... 23... 24... 25... 26... 27... 28... 29... 30... 31... 32... 33... 34... 35... 36... 37... 38... 39... 40... 41... 42... 43... 44... 45... 46... 47... 48... 49... 50... 51... 52... 53... 54... 55... 56... 57... 58... 59... 60... 61... 62... 63... 64... 65... 66... 67... 68... 69... 70... 71... 72... 73... 74... 75... 76... 77... 78... 79... 80... 81... 82... 83... 84... 85... 86... 87... 88... 89... 90... 91... 92... 93... 94... 95... 96... 97... 98... 99... 100... 101... 102... 103... 104... 105... 106... 107... 108... 109... 110... 111... 112... 113... 114... 115... 116... 117... 118... 119... 120... 121... 122... 123... 124... 125... 126... 127... 128... 129... 130... 131... 132... 133... 134... 135... 136... 137... 138... 139... 140... 141... 142... 143... 144... 145... 146... 147... 148... 149... 150... 151... 152... 153... 154... 155... 156... 157... 158... 159... 160... 161... 162... 163... 164... 165... 166... 167... 168... 169... 170... 171... 172... 173... 174... 175... 176... 177... 178... 179... 180... 181... 182... 183... 184... 185... 186... 187... 188... 189... 190... 191... 192... 193... 194... 195... 196... 197... 198... 199... 200... 201... 202... 203... 204... 205... 206... 207... 208... 209... 210... 211... 212... 213... 214... 215... 216... 217... 218... 219... 220... 221... 222... 223... 224... 225... 226... 227... 228... 229... 230... 231... 232... 233... 234... 235... 236... 237... 238... 239... 240... 241... 242... 243... 244... 245... 246... 247... 248... 249... 250... 251... 252... 253... 254... 255... 256... 257... 258... 259... 260... 261... 262... 263... 264... 265... 266... 267... 268... 269... 270... 271... 272... 273... 274... 275... 276... 277... 278... 279... 280... 281... 282... 283... 284... 285... 286... 287... 288... 289... 290... 291... 292... 293... 294... 295... 296... 297... 298... 299... 300... 301... 302... 303... 304... 305... 306... 307... 308... 309... 310... 311... 312... 313... 314... 315... 316... 317... 318... 319... 320... 321... 322... 323... 324... 325... 326... 327... 328... 329... 330... 331... 332... 333... 334... 335... 336... 337... 338... 339... 340... 341... 342... 343... 344... 345... 346
nrow(g.spatial) #number including dams
## [1] 409
g.spatial<-gage.filter.dams(gages.spatial = g.spatial)
nrow(g.spatial) #number excluding dams
## [1] 298
plot(g.spatial,col="blue",pch=16, add=T)

#Load basin characteristics
g.spatial<-gage.load.char(gages.spatial = g.spatial, basin.char.file = file.path(model.dir,"sdoah_inputs","shenandoahUpstreamStats2.RData"))
## Warning: 101 records in y cannot be matched to x
#Place gages into weather grid polygons
g.spatial<-gage.place.weather.grid( gages.spatial=g.spatial, plot=T )
## Mapping gages to weather grid cells...
Completed mapping gages to weather grid cells...
194 unique weather files to be used for 298 flow gages
Drawing plot...

#View sample of gage info
head(g.spatial@data[!is.na(g.spatial$forest),])
## FEATUREID agency_cd site_no station_nm
## 890 5908591 USGS 01628150 DEEP RUN NEAR GROTTOES, VA
## 1037 8466033 USGS 01662500 RUSH RIVER AT WASHINGTON, VA
## 1124 8566663 USGS 02031500 N F MOORMANS RIVER NEAR WHITE HALL, VA
## dec_lat_va dec_long_va coord_acy_cd dec_coord_datum_cd huc_cd
## 890 38.27 -78.76 U NAD83 02070005
## 1037 38.71 -78.15 U NAD83 02080103
## 1124 38.14 -78.75 U NAD83 02080204
## drain_area_va sv_begin_date sv_end_date sv_count_nu da_sqkm large
## 890 1.18 1979-11-01 1982-09-27 30 3.056 0
## 1037 14.60 1982-06-02 2006-08-15 21 37.814 0
## 1124 11.20 1942-05-04 1984-11-01 183 29.008 0
## small X ArcSite SiteID Latitude Longitude TotDASqKM LengthKM
## 890 0 264 222 3F265 38.28 -78.76 5.399 6.947
## 1037 0 43 28 1F223 38.75 -78.22 60.239 16.211
## 1124 0 209 178 3F081 38.14 -78.75 2.954 2.263
## ReachSlope ReachElev Calc_TotDASqKM tminann_brkt tmaxann_brkt
## 890 4.034 452.0 5.399 5.829 17.92
## 1037 1.320 233.4 60.239 5.804 17.98
## 1124 19.522 521.9 2.954 5.520 17.19
## so4dep_brkt prcpwin_brkt prcpann_brkt no3dep_brkt prcpnov_brkt
## 890 7.438 212.1 1097 6.616 87.15
## 1037 8.498 218.5 1189 7.404 98.82
## 1124 11.000 275.6 1362 9.993 112.19
## elev_nalcc slope_deg wtlnd_or_wtr wetland water undev_forest
## 890 526.7 15.31 0.02216 0.000000 0.02216 94.80
## 1037 366.7 11.14 0.39267 0.007893 0.38478 71.64
## 1124 714.4 21.10 0.00000 0.000000 0.00000 100.00
## impervious herbacious herb_or_ag forest developed develnotopen
## 890 26.04 0.000000 5.075 92.89 2.017 0.4211
## 1037 46.58 0.007893 26.785 68.63 4.201 0.8268
## 1124 10.41 0.000000 0.000 96.95 3.050 0.0000
## conus_wetland conus_opnwtr agriculture DA_Discrepancy
## 890 0.37677 0.02216 5.075 0
## 1037 0.02565 0.34532 26.777 0
## 1124 0.00000 0.00000 0.000 0
## weather.filename region
## 890 data_38.3125_-78.8125 east
## 1037 data_38.6875_-78.1875 east
## 1124 data_38.1875_-78.8125 east
Load flow observations
#Limit to gages that have basin data for sdoah
# as it turns out, only 3 gages have basin info, so it should make for a nice, quick demonstration
g.spatial<-g.spatial[!is.na(g.spatial$forest),]
#Load/calculate/aggregate flow data for seasonal and annual timesteps
q.matrices<-flow.retrieve( gages.spatial = g.spatial,
periods = c("seasonal","annual"), log.dir = file.path(model.dir,"logs") )
## Begin loading and aggregating stream flow observations...
-- Loading/aggregating gage 1 of 3 -- Gage 01628150, loaded 1096 rows
-- Loading/aggregating gage 2 of 3 -- Gage 01662500, loaded 8766 rows
-- Loading/aggregating gage 3 of 3 -- Gage 02031500, loaded 11871 rows
#View sample of flow data
q.matrices[["seasonal"]][30:40,3,]
## mean max min low records.period records.period.rolling
## 1956-05-28 12.672 66.0 2.8 2.9571 92 92
## 1956-08-28 3.842 32.0 0.5 0.7857 92 92
## 1956-11-28 23.051 384.0 0.7 0.9000 91 91
## 1957-02-28 20.679 260.0 3.0 3.4000 90 90
## 1957-05-28 23.287 115.0 5.2 6.0714 92 92
## 1957-08-28 3.111 30.0 0.1 0.1000 92 92
## 1957-11-28 4.171 28.0 0.1 0.1000 89 83
## 1958-02-28 26.286 122.0 5.9 6.9714 90 90
## 1958-05-28 48.450 250.0 6.8 7.7143 92 92
## 1958-08-28 2.729 8.5 0.6 0.7429 92 92
## 1958-11-28 0.967 8.0 0.2 0.2000 91 91
q.matrices[["seasonal"]][30:40,,1]
## 01628150 01662500 02031500
## 1956-05-28 NA 12.987 12.672
## 1956-08-28 NA 12.123 3.842
## 1956-11-28 NA 10.409 23.051
## 1957-02-28 NA 15.962 20.679
## 1957-05-28 NA 21.961 23.287
## 1957-08-28 NA 3.580 3.111
## 1957-11-28 NA 3.591 4.171
## 1958-02-28 NA 26.671 26.286
## 1958-05-28 NA 37.346 48.450
## 1958-08-28 NA 8.248 2.729
## 1958-11-28 NA 1.933 0.967
q.matrices[["seasonal"]][130:140,,1]
## 01628150 01662500 02031500
## 1981-05-28 0.53946 NA NA
## 1981-08-28 0.55696 NA NA
## 1981-11-28 0.05637 NA NA
## 1982-02-28 1.00711 NA NA
## 1982-05-28 2.11033 NA NA
## 1982-08-28 1.35457 NA NA
## 1982-11-28 NA NA 4.264
## 1983-02-28 NA NA 24.229
## 1983-05-28 NA NA 72.500
## 1983-08-28 NA NA 3.286
## 1983-11-28 NA NA 21.637
q.matrices[["annual"]][25:40,,1]
## 01628150 01662500 02031500
## 1973-09-30 NA 25.51 NA
## 1974-09-30 NA 15.01 NA
## 1975-09-30 NA 21.08 NA
## 1976-09-30 NA 15.07 NA
## 1977-09-30 NA 12.08 NA
## 1978-09-30 NA NA NA
## 1979-09-30 NA NA NA
## 1980-09-30 1.7551 NA NA
## 1981-09-30 0.3487 NA NA
## 1982-09-30 1.1439 NA NA
## 1983-09-30 NA NA 26.08
## 1984-09-30 NA NA 30.91
## 1985-09-30 NA NA NA
## 1986-09-30 NA NA NA
## 1987-09-30 NA NA NA
## 1988-09-30 NA NA NA
Create weather grid polygons from file contents
#Will only need to do this very occassionally, when adding a new geographic region,
# and it takes a while
# so I'm going to skip it for this markdown
# weather.grid.poly <- weather.grid.create( regions=c("east","ohio") )
Load weather observations
#Load/calculate/aggregate weather data for seasonal and annual timesteps
w.matrices <- weather.retrieve(gages.spatial=g.spatial, periods=c("seasonal","annual"))
## Begin reading 3 met files...
-- loading file 1 of 3 --
-- loading file 2 of 3 --
-- loading file 3 of 3 --
tail(w.matrices[["seasonal"]][,1,])
## precip.total precip.e precip.e.lag1 precip.e.lag2 precip.e.lag3
## 2009-11-28 271.4 271.4 249.0 282.7 162.5
## 2010-02-28 322.1 278.8 271.4 249.0 282.7
## 2010-05-28 203.7 247.0 278.8 271.4 249.0
## 2010-08-28 156.1 156.1 247.0 278.8 271.4
## 2010-11-28 207.4 207.4 156.1 247.0 278.8
## 2011-02-28 NA NA 207.4 156.1 247.0
## tmin tmax tavg pet pet.lag1 pet.lag2 gdd gdd.lag1
## 2009-11-28 7.606 18.484 13.045 0.20853 0.55048 0.34868 364.5 1118.6
## 2010-02-28 -5.706 3.623 -1.042 0.07014 0.20853 0.55048 0.0 364.5
## 2010-05-28 6.723 19.985 13.354 0.37579 0.07014 0.20853 413.2 0.0
## 2010-08-28 18.166 30.773 24.469 0.58804 0.37579 0.07014 1331.2 413.2
## 2010-11-28 6.992 21.034 14.013 0.23894 0.58804 0.37579 463.7 1331.2
## 2011-02-28 NA NA NA NA 0.23894 0.58804 NA 463.7
## gdd.lag2 frozen melt.doy melt.doy.lag1 melt.doy.lag2
## 2009-11-28 335.7 5 0 0 62
## 2010-02-28 1118.6 83 59 0 0
## 2010-05-28 364.5 10 62 59 0
## 2010-08-28 0.0 0 0 62 59
## 2010-11-28 413.2 9 0 0 62
## 2011-02-28 1331.2 NA NA 0 0
tail(w.matrices[["seasonal"]][,,1])
## data_38.3125_-78.8125 data_38.6875_-78.1875
## 2009-11-28 271.4 270.9
## 2010-02-28 322.1 239.3
## 2010-05-28 203.7 276.0
## 2010-08-28 156.1 230.6
## 2010-11-28 207.4 319.2
## 2011-02-28 NA NA
## data_38.1875_-78.8125
## 2009-11-28 337.0
## 2010-02-28 401.3
## 2010-05-28 245.5
## 2010-08-28 183.0
## 2010-11-28 248.9
## 2011-02-28 NA
tail(w.matrices[["annual"]][,,1])
## data_38.3125_-78.8125 data_38.6875_-78.1875
## 2006-09-30 1137.1 1159.5
## 2007-09-30 1051.0 931.5
## 2008-09-30 921.9 1090.7
## 2009-09-30 874.0 904.0
## 2010-09-30 955.2 1137.1
## 2011-09-30 NA NA
## data_38.1875_-78.8125
## 2006-09-30 1306.6
## 2007-09-30 1167.6
## 2008-09-30 1061.9
## 2009-09-30 979.9
## 2010-09-30 1162.5
## 2011-09-30 NA
Merge gage (basin char and drainage area), flow, and weather
#Next and last thing for data prep to add to package...