dataRetrieval.R 15.5 KB
Newer Older
Laura A DeCicco's avatar
Laura A DeCicco committed
1
## ----setup, include=FALSE, message=FALSE------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
2
library(knitr)
Laura A DeCicco's avatar
Laura A DeCicco committed
3
library(dataRetrieval)
Laura A DeCicco's avatar
Laura A DeCicco committed
4

Laura A DeCicco's avatar
Laura A DeCicco committed
5
6
7
8
9
10
11
12
13
14
15
16
17
options(continue=" ")
options(width=60)
knitr::opts_chunk$set(echo = TRUE,
                      warning = FALSE,
                      message = FALSE,
                      fig.height = 7,
                      fig.width = 7)

opts_chunk$set(highlight=TRUE, 
               tidy=TRUE, 
               keep.space=TRUE,
               keep.blank.space=FALSE,
               keep.comment=TRUE)
Laura A DeCicco's avatar
Laura A DeCicco committed
18
19
20
21
knit_hooks$set(inline = function(x) {
   if (is.numeric(x)) round(x, 3)})

## ----workflow, echo=TRUE,eval=FALSE-----------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
22
#  library(dataRetrieval)
Laura A DeCicco's avatar
Laura A DeCicco committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#  # Choptank River near Greensboro, MD
#  siteNumber <- "01491000"
#  ChoptankInfo <- readNWISsite(siteNumber)
#  parameterCd <- "00060"
#  
#  #Raw daily data:
#  rawDailyData <- readNWISdv(siteNumber,parameterCd,
#                        "1980-01-01","2010-01-01")
#  
#  # Sample data Nitrate:
#  parameterCd <- "00618"
#  qwData <- readNWISqw(siteNumber,parameterCd,
#                        "1980-01-01","2010-01-01")
#  
37
38
#  pCode <- readNWISpCode(parameterCd)
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
39

Laura A DeCicco's avatar
Laura A DeCicco committed
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
## ----echo=FALSE-------------------------------------------

library(htmlTable)

Functions <- c("readNWISdata","",
               "readNWISdv","","","","",
               "readNWISqw","","","","",
               "readNWISuv","","","",
               "readNWISrating","",
               "readNWISmeas","","",
               "readNWISpeak","","",
               "readNWISgwl","","",
               "readNWISuse","","","",
               "readNWISstat","","","","","",
               "readNWISpCode",
               "readNWISsite",
               "whatNWISsites",
               "whatNWISdata","",
               "readWQPdata",
               "readWQPqw","","","",
               "whatWQPsites")
Arguments <- c("...","service", #readNWISdata
               "siteNumber","parameterCd","startDate","endDate","statCd", #readNWISdv
               "siteNumber","parameterCd","startDate","endDate","expanded", #readNWISqw
               "siteNumber","parameterCd","startDate","endDate", #readNWISuv
               "siteNumber","type", #readNWISrating
               "siteNumber","startDate","endDate", #readNWISmeas
               "siteNumber","startDate","endDate", #readNWISpeak
               "siteNumber","startDate","endDate", #readNWISgwl
               "stateCd","countyCd","years","categories", #readNWISuse
               "siteNumbers","parameterCd","startDate","endDate","statReportType","statType", #readNWISstat
               "parameterCd", #readNWISpCode
               "siteNumber", #readNWISsite
               "...", #whatNWISsites
               "siteNumber","service", #whatNWISdata
               "...", #readWQPdata
               "siteNumber","parameterCd","startDate","endDate", #readWQPqw
               "...") #whatWQPsites
Description <- c("NWIS data using user-specified queries","", #readNWISdata
                 "NWIS daily data","","","","", #readNWISdv
                 "NWIS water quality data","","","","", #readNWISqw
                 "NWIS instantaneous value data","","","", #readNWISuv
                 "NWIS rating table for active streamgage","", #readNWISrating
                 "NWIS surface-water measurements","","", #readNWISmeas
                 "NWIS peak flow data","","", #readNWISpeak
                 "NWIS groundwater level measurements","","", #readNWISgwl
                 "NWIS water use","","","", #readNWISuse
                 "NWIS statistical service","","","","","", #readNWISstat
                 "NWIS parameter code information", #readNWISpCode
                 "NWIS site information", #readNWISsite
                 "NWIS site search using user-specified queries",
                 "NWIS data availability, including period of record and count","",
                 "WQP data using user-specified queries",
                 "WQP data","","","",
                 "WQP site search using user-specified queries")

data.df <- data.frame(`Function Name` = Functions, Arguments, Description, stringsAsFactors=FALSE)

htmlTable(data.df, 
          caption="Table 1: dataRetrieval functions",
          rnames=FALSE, align=c("l","l","l","l"),
          col.rgroup = c("none", "#F7F7F7"), 
          css.cell="padding-bottom: 0.0em; padding-right: 0.5em; padding-top: 0.0em;")


## ----tableParameterCodes, echo=FALSE----------------------


Laura A DeCicco's avatar
Laura A DeCicco committed
108
pCode <- c('00060', '00065', '00010','00045','00400')
Laura A DeCicco's avatar
Laura A DeCicco committed
109
shortName <- c("Discharge [ft<sup>3</sup>/s]","Gage height [ft]","Temperature [C]", "Precipitation [in]", "pH")
Laura A DeCicco's avatar
Laura A DeCicco committed
110
111
112

data.df <- data.frame(pCode, shortName, stringsAsFactors=FALSE)

Laura A DeCicco's avatar
Laura A DeCicco committed
113
114
115
116
htmlTable(data.df, 
          caption="Table 2: Common USGS Parameter Codes",
          rnames=FALSE, align=c("c","c"), col.rgroup = c("none", "#F7F7F7"), 
          css.cell="padding-bottom: 0.0em; padding-right: 0.5em; padding-top: 0.0em;")
Laura A DeCicco's avatar
Laura A DeCicco committed
117
118


Laura A DeCicco's avatar
Laura A DeCicco committed
119
120

## ----tableStatCodes, echo=FALSE---------------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
121
122
123
124
125
StatCode <- c('00001', '00002', '00003','00008')
shortName <- c("Maximum","Minimum","Mean", "Median")

data.df <- data.frame(StatCode, shortName, stringsAsFactors=FALSE)

Laura A DeCicco's avatar
Laura A DeCicco committed
126
127
128
129
130
131
htmlTable(data.df, 
          caption="Table 3: Commonly used USGS Stat Codes",
          rnames=FALSE, align=c("c","c"), col.rgroup = c("none", "#F7F7F7"), 
          css.cell="padding-bottom: 0.0em; padding-right: 0.5em; padding-top: 0.0em;")


Laura A DeCicco's avatar
Laura A DeCicco committed
132
133


Laura A DeCicco's avatar
Laura A DeCicco committed
134
135
136
## ----getSite, echo=TRUE, eval=FALSE-----------------------
#  siteNumbers <- c("01491000","01645000")
#  siteINFO <- readNWISsite(siteNumbers)
Laura A DeCicco's avatar
Laura A DeCicco committed
137

Laura A DeCicco's avatar
Laura A DeCicco committed
138
139
## ----siteNames3, echo=TRUE, eval=FALSE--------------------
#  comment(siteINFO)
Laura A DeCicco's avatar
Laura A DeCicco committed
140

Laura A DeCicco's avatar
Laura A DeCicco committed
141
142
143
144
145
146
147
148
## ----getSiteExtended, echo=TRUE, eval=FALSE---------------
#  # Continuing from the previous example:
#  # This pulls out just the daily, mean data:
#  
#  dailyDataAvailable <- whatNWISdata(siteNumbers,
#                      service="dv", statCd="00003")
#  
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
149

Laura A DeCicco's avatar
Laura A DeCicco committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
## ----echo=FALSE-------------------------------------------

tableData <- data.frame(
  siteNumber = c("01491000","01491000","01645000","01491000","01491000","01491000"),
  srsname = c("Temperature, water","Stream flow, mean daily",
              "Stream flow, mean daily",
              "Specific conductance",
              "Suspended sediment concentration (SSC)",
              "Suspended sediment discharge" ),
  startDate = c("2010-10-01","1948-01-01","1930-09-26","2010-10-01","1980-10-01","1980-10-01"),
  endDate = c("2012-05-09","2017-05-17","2017-05-17","2012-05-09","1991-09-30","1991-09-30"),
  count = c("529","25340","31646","527","4017","4017"),
  units = c("deg C","ft<sup>3</sup>/s","ft<sup>3</sup>/s","uS/cm @25C","mg/l","tons/day"),
  stringsAsFactors = FALSE)


htmlTable(tableData, 
          caption="Table 4: Reformatted version of output from the whatNWISdata function for the Choptank River near Greensboro, MD, and from Seneca Creek at Dawsonville, MD from the daily values service [Some columns deleted for space considerations]",
          rnames=FALSE, 
          col.rgroup = c("none", "#F7F7F7"), 
          css.cell="padding-bottom: 0.0em; padding-right: 0.5em; padding-top: 0.0em;")

Laura A DeCicco's avatar
Laura A DeCicco committed
172

Laura A DeCicco's avatar
Laura A DeCicco committed
173
174
175
176
## ----label=getPCodeInfo, echo=TRUE, eval=FALSE------------
#  # Using defaults:
#  parameterCd <- "00618"
#  parameterINFO <- readNWISpCode(parameterCd)
Laura A DeCicco's avatar
Laura A DeCicco committed
177

Laura A DeCicco's avatar
Laura A DeCicco committed
178
179
180
181
182
183
184
185
186
187
## ----label=getNWISDaily, echo=TRUE, eval=FALSE------------
#  
#  # Choptank River near Greensboro, MD:
#  siteNumber <- "01491000"
#  parameterCd <- "00060"  # Discharge
#  startDate <- "2009-10-01"
#  endDate <- "2012-09-30"
#  
#  discharge <- readNWISdv(siteNumber,
#                      parameterCd, startDate, endDate)
Laura A DeCicco's avatar
Laura A DeCicco committed
188

Laura A DeCicco's avatar
Laura A DeCicco committed
189
190
191
192
193
194
195
196
197
198
## ----label=getNWIStemperature, echo=TRUE, eval=FALSE------
#  siteNumber <- "01491000"
#  parameterCd <- c("00010","00060")  # Temperature and discharge
#  statCd <- c("00001","00003")  # Mean and maximum
#  startDate <- "2012-01-01"
#  endDate <- "2012-05-01"
#  
#  temperatureAndFlow <- readNWISdv(siteNumber, parameterCd,
#          startDate, endDate, statCd=statCd)
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
199

Laura A DeCicco's avatar
Laura A DeCicco committed
200
201
202
203
204
## ----label=getNWIStemperature2, echo=FALSE, eval=TRUE-----
filePath <- system.file("extdata", package="dataRetrieval")
fileName <- "temperatureAndFlow.RData"
fullPath <- file.path(filePath, fileName)
load(fullPath)
Laura A DeCicco's avatar
Laura A DeCicco committed
205
206
207
208
209


## ----label=renameColumns, echo=TRUE-----------------------
names(temperatureAndFlow)

Laura A DeCicco's avatar
Laura A DeCicco committed
210
temperatureAndFlow <- renameNWISColumns(temperatureAndFlow)
Laura A DeCicco's avatar
Laura A DeCicco committed
211
212
names(temperatureAndFlow)

Laura A DeCicco's avatar
Laura A DeCicco committed
213
214
215
216
217
218
219
220
221
222

## ----label=attr1, echo=TRUE-------------------------------
#Information about the data frame attributes:
names(attributes(temperatureAndFlow))

statInfo <- attr(temperatureAndFlow, "statisticInfo")
variableInfo <- attr(temperatureAndFlow, "variableInfo")
siteInfo <- attr(temperatureAndFlow, "siteInfo")


Laura A DeCicco's avatar
Laura A DeCicco committed
223
## ---------------------------------------------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
224
225
226
variableInfo <- attr(temperatureAndFlow, "variableInfo")
siteInfo <- attr(temperatureAndFlow, "siteInfo")

Laura A DeCicco's avatar
Laura A DeCicco committed
227
228
par(mar=c(5,5,5,5)) #sets the size of the plot window

Laura A DeCicco's avatar
Laura A DeCicco committed
229
plot(temperatureAndFlow$Date, temperatureAndFlow$Wtemp_Max,
Laura A DeCicco's avatar
Laura A DeCicco committed
230
  ylab=variableInfo$parameter_desc[1],xlab="" )
Laura A DeCicco's avatar
Laura A DeCicco committed
231
par(new=TRUE)
Laura A DeCicco's avatar
Laura A DeCicco committed
232
plot(temperatureAndFlow$Date, temperatureAndFlow$Flow,
Laura A DeCicco's avatar
Laura A DeCicco committed
233
  col="red",type="l",xaxt="n",yaxt="n",xlab="",ylab="",axes=FALSE
Laura A DeCicco's avatar
Laura A DeCicco committed
234
  )
Laura A DeCicco's avatar
Laura A DeCicco committed
235
axis(4,col="red",col.axis="red")
Laura A DeCicco's avatar
Laura A DeCicco committed
236
237
238
mtext(variableInfo$parameter_desc[2],side=4,line=3,col="red")
title(paste(siteInfo$station_nm,"2012"))
legend("topleft", variableInfo$param_units, 
Laura A DeCicco's avatar
Laura A DeCicco committed
239
240
       col=c("black","red"),lty=c(NA,1),pch=c(1,NA))

Laura A DeCicco's avatar
Laura A DeCicco committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
## ----label=readNWISuv, eval=FALSE-------------------------
#  
#  parameterCd <- "00060"  # Discharge
#  startDate <- "2012-05-12"
#  endDate <- "2012-05-13"
#  dischargeUnit <- readNWISuv(siteNumber, parameterCd,
#          startDate, endDate)
#  dischargeUnit <- renameNWISColumns(dischargeUnit)

## ----label=getQW, echo=TRUE, eval=FALSE-------------------
#  
#  # Dissolved Nitrate parameter codes:
#  parameterCd <- c("00618","71851")
#  startDate <- "1985-10-01"
#  endDate <- "2012-09-30"
#  
#  dfLong <- readNWISqw(siteNumber, parameterCd,
#        startDate, endDate)
#  
#  # Or the wide return:
Laura A DeCicco's avatar
Laura A DeCicco committed
261
262
#  dfWide <- readNWISqw(siteNumber, parameterCd,
#        startDate, endDate, reshape=TRUE)
Laura A DeCicco's avatar
Laura A DeCicco committed
263
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
264

Laura A DeCicco's avatar
Laura A DeCicco committed
265
266
267
268
## ----qwmeta, echo=TRUE, eval=FALSE------------------------
#  
#  comment(dfLong)
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
269

Laura A DeCicco's avatar
Laura A DeCicco committed
270
271
272
## ----gwlexample, echo=TRUE, eval=FALSE--------------------
#  siteNumber <- "434400121275801"
#  groundWater <- readNWISgwl(siteNumber)
Laura A DeCicco's avatar
Laura A DeCicco committed
273

Laura A DeCicco's avatar
Laura A DeCicco committed
274
275
276
277
## ----peakexample, echo=TRUE, eval=FALSE-------------------
#  siteNumber <- '01594440'
#  peakData <- readNWISpeak(siteNumber)
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
278

Laura A DeCicco's avatar
Laura A DeCicco committed
279
280
281
282
## ----ratingexample, echo=TRUE, eval=FALSE-----------------
#  ratingData <- readNWISrating(siteNumber, "base")
#  attr(ratingData, "RATING")
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
283

Laura A DeCicco's avatar
Laura A DeCicco committed
284
285
286
## ----surfexample, echo=TRUE, eval=FALSE-------------------
#  surfaceData <- readNWISmeas(siteNumber)
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
287

Laura A DeCicco's avatar
Laura A DeCicco committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
## ----eval=FALSE-------------------------------------------
#  allegheny <- readNWISuse(stateCd = "Pennsylvania",
#                           countyCd = "Allegheny")
#  
#  
#  national <- readNWISuse(stateCd = NULL,
#                          countyCd = NULL,
#                          transform = TRUE)
#  

## ----eval=FALSE-------------------------------------------
#  discharge_stats <- readNWISstat(siteNumbers=c("02319394"),
#                     parameterCd=c("00060"),
#                     statReportType="annual")
#  

Laura A DeCicco's avatar
Laura A DeCicco committed
304
305
## ----label=getQWData, echo=TRUE, eval=FALSE---------------
#  specificCond <- readWQPqw('WIDNR_WQX-10032762',
Laura A DeCicco's avatar
Laura A DeCicco committed
306
307
#                            'Specific conductance',
#                            '2011-05-01','2011-09-30')
Laura A DeCicco's avatar
Laura A DeCicco committed
308

Laura A DeCicco's avatar
Laura A DeCicco committed
309
310
311
312
## ----siteSearch, eval=FALSE-------------------------------
#  sites <- whatNWISsites(bBox=c(-83.0,36.5,-81.0,38.5),
#                        parameterCd=c("00010","00060"),
#                        hasDataTypeCd="dv")
Laura A DeCicco's avatar
Laura A DeCicco committed
313

Laura A DeCicco's avatar
Laura A DeCicco committed
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
## ----echo=FALSE-------------------------------------------

Service <- c("dv","iv","gwlevels","qwdata","measurements","peak","stat")
Description <- c("Daily","Instantaneous","Groundwater Levels","Water Quality","Surface Water Measurements","Peak Flow","Statistics Service")
URL <- c("<a href='https://waterservices.usgs.gov/rest/DV-Test-Tool.html' target='_blank'>https://waterservices.usgs.gov/rest/DV-Test-Tool.html<a>",
         "<a href='https://waterservices.usgs.gov/rest/IV-Test-Tool.html' target='_blank'>https://waterservices.usgs.gov/rest/IV-Test-Tool.html<a>",
         "<a href='https://waterservices.usgs.gov/rest/GW-Levels-Test-Tool.html' target='_blank'>https://waterservices.usgs.gov/rest/GW-Levels-Test-Tool.html<a>",
         "<a href='https://nwis.waterdata.usgs.gov/nwis/qwdata' target='_blank'>https://nwis.waterdata.usgs.gov/nwis/qwdata<a>",
         "<a href='https://waterdata.usgs.gov/nwis/measurements/' target='_blank'>https://waterdata.usgs.gov/nwis/measurements/<a>",
         "<a href='https://nwis.waterdata.usgs.gov/usa/nwis/peak/' target='_blank'>https://nwis.waterdata.usgs.gov/usa/nwis/peak/<a>",
         "<a href='https://waterservices.usgs.gov/rest/Statistics-Service-Test-Tool.html' target='_blank'>https://waterservices.usgs.gov/rest/Statistics-Service-Test-Tool.html<a>")

tableData <- data.frame(Service,
                        Description,
                        URL,
  stringsAsFactors = FALSE)


htmlTable(tableData, 
          caption="Table 5: NWIS general data calls",
          rnames=FALSE, align=c("l","l","l"),
          col.rgroup = c("none", "#F7F7F7"), 
          css.cell="padding-bottom: 0.0em; padding-right: 0.5em; padding-top: 0.0em;")


339
340
341
342
343
344
345
346
347
## ----dataExample, eval=FALSE------------------------------
#  dischargeWI <- readNWISdata(service="dv",
#                             stateCd="WI",
#                             parameterCd="00060",
#                             drainAreaMin="50",
#                             statCd="00003")
#  
#  siteInfo <- attr(dischargeWI, "siteInfo")
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
348

Laura A DeCicco's avatar
Laura A DeCicco committed
349
350
351
352
353
354
355
356
357
## ----NJChloride, eval=FALSE-------------------------------
#  
#  sitesNJ <- whatWQPsites(statecode="US:34",
#                         characteristicName="Chloride")
#  

## ----phData, eval=FALSE-----------------------------------
#  dataPH <- readWQPdata(statecode="US:55",
#                   characteristicName="pH")
Laura A DeCicco's avatar
Laura A DeCicco committed
358
359
360
361
362
363
364
365
366
367
368

## ----eval=FALSE-------------------------------------------
#  type <- "Stream"
#  sites <- whatWQPdata(countycode="US:55:025",siteType=type)

## ----eval=FALSE-------------------------------------------
#  site <- whatWQPsamples(siteid="USGS-01594440")

## ----eval=FALSE-------------------------------------------
#  type <- "Stream"
#  sites <- whatWQPmetrics(countycode="US:55:025",siteType=type)
Laura A DeCicco's avatar
Laura A DeCicco committed
369

370
371
372
373
374
375
376
377
## ----meta1, eval=FALSE------------------------------------
#  
#  attr(dischargeWI, "url")
#  
#  attr(dischargeWI, "queryTime")
#  
#  siteInfo <- attr(dischargeWI, "siteInfo")
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
378

379
380
381
382
## ----meta2, eval=FALSE------------------------------------
#  
#  names(attributes(dischargeWI))
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
383

384
385
386
387
388
389
390
## ----meta3, eval=FALSE------------------------------------
#  
#  siteInfo <- attr(dischargeWI, "siteInfo")
#  
#  variableInfo <- attr(dischargeWI, "variableInfo")
#  
#  
Laura A DeCicco's avatar
Laura A DeCicco committed
391

Laura A DeCicco's avatar
Laura A DeCicco committed
392
## ----meta5, eval=FALSE------------------------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
393
394
395
#  comment(peakData)
#  
#  #Which is equivalent to:
Laura A DeCicco's avatar
Laura A DeCicco committed
396
#  attr(peakData, "comment")
Laura A DeCicco's avatar
Laura A DeCicco committed
397
398

## ----seeVignette,eval = FALSE-----------------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
399
#  vignette(dataRetrieval)
Laura A DeCicco's avatar
Laura A DeCicco committed
400
401

## ----installFromCran,eval = FALSE-------------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
402
#  install.packages("dataRetrieval")
Laura A DeCicco's avatar
Laura A DeCicco committed
403
404

## ----openLibraryTest, eval=FALSE--------------------------
Laura A DeCicco's avatar
Laura A DeCicco committed
405
#  library(dataRetrieval)
Laura A DeCicco's avatar
Laura A DeCicco committed
406

Laura A DeCicco's avatar
Laura A DeCicco committed
407
408
409
410
411
412
413
414
415
416
417
418
## ----label=getSiteApp, echo=TRUE, eval=FALSE--------------
#  availableData <- whatNWISdata(siteNumber, "dv")
#  dailyData <- availableData["00003" == availableData$stat_cd,]
#  
#  tableData <- with(dailyData,
#        data.frame(
#          shortName=srsname,
#          Start=begin_date,
#          End=end_date,
#          Count=count_nu,
#          Units=parameter_units)
#        )
Laura A DeCicco's avatar
Laura A DeCicco committed
419
420
421
422
423

## ----label=saveData, echo=TRUE, eval=FALSE----------------
#  write.table(tableData, file="tableData.tsv",sep="\t",
#              row.names = FALSE,quote=FALSE)