Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dataRetrieval
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Water
dataRetrieval
Commits
b279a8e4
Commit
b279a8e4
authored
12 years ago
by
Laura A DeCicco
Browse files
Options
Downloads
Patches
Plain Diff
Added a function to retrieve NWIS unit value data.
parent
5789e2b4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
R/retrieveUnitNWISData.r
+69
-0
69 additions, 0 deletions
R/retrieveUnitNWISData.r
with
69 additions
and
0 deletions
R/retrieveUnitNWISData.r
0 → 100644
+
69
−
0
View file @
b279a8e4
#' Data Import for Instantaneous USGS NWIS Data
#'
#' Imports data from NWIS web service. This function gets the data from here: \url{http://waterservices.usgs.gov/}
#' A list of parameter codes can be found here: \url{http://nwis.waterdata.usgs.gov/nwis/pmcodes/}
#' A list of statistic codes can be found here: \url{http://nwis.waterdata.usgs.gov/nwis/help/?read_file=stat&format=table}
#'
#' @param siteNumber string USGS site number. This is usually an 8 digit number
#' @param ParameterCd string USGS parameter code. This is usually an 5 digit number.
#' @param StartDate string starting date for data retrieval in the form YYYY-MM-DD.
#' @param EndDate string ending date for data retrieval in the form YYYY-MM-DD.
#' @param StatCd string USGS statistic code. This is usually 5 digits. Daily mean (00003) is the default.
#' @param interactive logical Option for interactive mode. If true, there is user interaction for error handling and data checks.
#' @keywords data import USGS web service
#' @return retval dataframe with agency, site, dateTime, value, and code columns
#' @export
#' @examples
#' siteNumber <- '05114000'
#' ParameterCd <- '00060'
#' StartDate <- '2012-05-01'
#' EndDate <- '2012-05-31'
#' # These examples require an internet connection to run
#' retrieveUnitNWISData(siteNumber,ParameterCd,StartDate,EndDate,interactive=FALSE)
retrieveUnitNWISData
<-
function
(
siteNumber
,
ParameterCd
,
StartDate
,
EndDate
,
interactive
=
TRUE
){
siteNumber
<-
formatCheckSiteNumber
(
siteNumber
,
interactive
=
interactive
)
ParameterCd
<-
formatCheckParameterCd
(
ParameterCd
,
interactive
=
interactive
)
StartDate
<-
formatCheckDate
(
StartDate
,
"StartDate"
,
interactive
=
interactive
)
EndDate
<-
formatCheckDate
(
EndDate
,
"EndDate"
,
interactive
=
interactive
)
dateReturn
<-
checkStartEndDate
(
StartDate
,
EndDate
,
interactive
=
interactive
)
StartDate
<-
dateReturn
[
1
]
EndDate
<-
dateReturn
[
2
]
baseURL
<-
"http://waterservices.usgs.gov/nwis/iv?site="
url
<-
paste
(
baseURL
,
siteNumber
,
"&ParameterCd="
,
ParameterCd
,
"&format=rdb,1.0"
,
sep
=
""
)
if
(
nzchar
(
StartDate
))
{
url
<-
paste
(
url
,
"&startDT="
,
StartDate
,
sep
=
""
)
}
else
url
<-
paste
(
url
,
"&startDT="
,
"1851-01-01"
,
sep
=
""
)
if
(
nzchar
(
EndDate
))
{
url
<-
paste
(
url
,
"&endDT="
,
EndDate
,
sep
=
""
)
}
tmp
<-
read.delim
(
url
,
header
=
FALSE
,
quote
=
"\""
,
dec
=
"."
,
sep
=
'\t'
,
colClasses
=
c
(
'character'
),
fill
=
TRUE
,
comment.char
=
"#"
)
col.nm
<-
make.names
(
unlist
(
tmp
[
1
,,
drop
=
TRUE
]),
allow_
=
FALSE
)
retval
<-
lapply
(
tmp
,
function
(
x
)
{
Typ
<-
x
[
2
]
# The type - the second row shows the type (such as 5s = a string with 5 letters, 20d = a date, etc)
x
<-
x
[
-
c
(
1
,
2
)]
# the data - takes away the first 2 rows (1st = header, 2nd = type)
if
(
regexpr
(
'd$'
,
Typ
)
>
0
)
{
# Must be date
ret.val
<-
as.POSIXct
(
strptime
(
x
,
"%Y-%m-%d %H:%M"
))
# The data are in standard format, but...
}
else
if
(
regexpr
(
'n$'
,
Typ
)
>
0
)
# Must be numeric
ret.val
<-
as.numeric
(
x
)
else
# Must be character
ret.val
<-
x
return
(
ret.val
)})
retval
<-
as.data.frame
(
retval
,
stringsAsFactors
=
FALSE
)
names
(
retval
)
<-
c
(
'agency'
,
'site'
,
'dateTime'
,
'value'
,
'code'
)
return
(
retval
)
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment