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
c5957453
Commit
c5957453
authored
12 years ago
by
Laura A DeCicco
Browse files
Options
Downloads
Patches
Plain Diff
Updating the NWIS code to be a little cleaner, by using the header=TRUE option in read.delim.
parent
01f6cc51
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
R/retrieveNWISData.r
+16
-6
16 additions, 6 deletions
R/retrieveNWISData.r
R/retrieveUnitNWISData.r
+6
-6
6 additions, 6 deletions
R/retrieveUnitNWISData.r
with
22 additions
and
12 deletions
R/retrieveNWISData.r
+
16
−
6
View file @
c5957453
...
@@ -18,11 +18,18 @@
...
@@ -18,11 +18,18 @@
#' rawDailyFlowData <- retrieveNWISData('01594440','00060', '1985-01-01', '1985-01-31')
#' rawDailyFlowData <- retrieveNWISData('01594440','00060', '1985-01-01', '1985-01-31')
#' rawDailyTemperatureData <- retrieveNWISData('05114000','00010', '1985-01-01', '1985-01-31', StatCd='00001',interactive=FALSE)
#' rawDailyTemperatureData <- retrieveNWISData('05114000','00010', '1985-01-01', '1985-01-31', StatCd='00001',interactive=FALSE)
retrieveNWISData
<-
function
(
siteNumber
,
ParameterCd
,
StartDate
,
EndDate
,
StatCd
=
"00003"
,
interactive
=
TRUE
){
retrieveNWISData
<-
function
(
siteNumber
,
ParameterCd
,
StartDate
,
EndDate
,
StatCd
=
"00003"
,
interactive
=
TRUE
){
# Checking for 8 digit site ID:
siteNumber
<-
formatCheckSiteNumber
(
siteNumber
,
interactive
=
interactive
)
siteNumber
<-
formatCheckSiteNumber
(
siteNumber
,
interactive
=
interactive
)
# Check for 5 digit parameter code:
ParameterCd
<-
formatCheckParameterCd
(
ParameterCd
,
interactive
=
interactive
)
ParameterCd
<-
formatCheckParameterCd
(
ParameterCd
,
interactive
=
interactive
)
# Check date format:
StartDate
<-
formatCheckDate
(
StartDate
,
"StartDate"
,
interactive
=
interactive
)
StartDate
<-
formatCheckDate
(
StartDate
,
"StartDate"
,
interactive
=
interactive
)
EndDate
<-
formatCheckDate
(
EndDate
,
"EndDate"
,
interactive
=
interactive
)
EndDate
<-
formatCheckDate
(
EndDate
,
"EndDate"
,
interactive
=
interactive
)
# Check that start date happens before end date:
dateReturn
<-
checkStartEndDate
(
StartDate
,
EndDate
,
interactive
=
interactive
)
dateReturn
<-
checkStartEndDate
(
StartDate
,
EndDate
,
interactive
=
interactive
)
StartDate
<-
dateReturn
[
1
]
StartDate
<-
dateReturn
[
1
]
EndDate
<-
dateReturn
[
2
]
EndDate
<-
dateReturn
[
2
]
...
@@ -41,28 +48,31 @@ retrieveNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,StatCd="0
...
@@ -41,28 +48,31 @@ retrieveNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,StatCd="0
tmp
<-
read.delim
(
tmp
<-
read.delim
(
url
,
url
,
header
=
FALS
E
,
header
=
TRU
E
,
quote
=
"\""
,
quote
=
"\""
,
dec
=
"."
,
dec
=
"."
,
sep
=
'\t'
,
sep
=
'\t'
,
colClasses
=
c
(
'character'
),
colClasses
=
c
(
'character'
),
fill
=
TRUE
,
fill
=
TRUE
,
comment.char
=
"#"
)
comment.char
=
"#"
)
col.nm
<-
make.names
(
unlist
(
tmp
[
1
,,
drop
=
TRUE
]),
allow_
=
FALSE
)
retval
<-
lapply
(
tmp
,
function
(
x
)
{
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)
Typ
<-
x
[
1
]
# The type
x
<-
x
[
-
c
(
1
,
2
)]
# the data - takes away the first
2
row
s
(
1st = header, 2nd = type
)
x
<-
x
[
-
c
(
1
)]
# the data - takes away the first
1st
row (
non-header
)
if
(
regexpr
(
'd$'
,
Typ
)
>
0
)
{
# Must be date
if
(
regexpr
(
'd$'
,
Typ
)
>
0
)
{
# Must be date
ret.val
<-
try
(
as.Date
(
x
))
# The data are in standard format, but...
ret.val
<-
try
(
as.Date
(
x
))
# The data are in standard format, but...
if
(
class
(
ret.val
)
==
"try-error"
)
if
(
class
(
ret.val
)
==
"try-error"
)
ret.val
<-
x
ret.val
<-
x
}
}
else
if
(
regexpr
(
'n$'
,
Typ
)
>
0
)
# Must be numeric
else
if
(
regexpr
(
'n$'
,
Typ
)
>
0
)
# Must be numeric
...be careful of ice
ret.val
<-
as.numeric
(
x
)
ret.val
<-
as.numeric
(
x
)
else
# Must be character
else
# Must be character
ret.val
<-
x
ret.val
<-
x
return
(
ret.val
)})
return
(
ret.val
)})
retval
<-
as.data.frame
(
retval
,
stringsAsFactors
=
FALSE
)
retval
<-
as.data.frame
(
retval
,
stringsAsFactors
=
FALSE
)
names
(
retval
)
<-
c
(
'agency'
,
'site'
,
'dateTime'
,
'value'
,
'code'
)
colNames
<-
names
(
retval
)
names
(
retval
)
<-
c
(
'agency'
,
'site'
,
'dateTime'
,
'value'
,
'code'
)
# do a merge instead?
return
(
retval
)
return
(
retval
)
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
R/retrieveUnitNWISData.r
+
6
−
6
View file @
c5957453
...
@@ -28,7 +28,7 @@ retrieveUnitNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,inter
...
@@ -28,7 +28,7 @@ retrieveUnitNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,inter
# Check date format:
# Check date format:
StartDate
<-
formatCheckDate
(
StartDate
,
"StartDate"
,
interactive
=
interactive
)
StartDate
<-
formatCheckDate
(
StartDate
,
"StartDate"
,
interactive
=
interactive
)
EndDate
<-
formatCheckDate
(
EndDate
,
"EndDate"
,
interactive
=
interactive
)
EndDate
<-
formatCheckDate
(
EndDate
,
"EndDate"
,
interactive
=
interactive
)
#Check that
#Check that
start date happens before end date:
dateReturn
<-
checkStartEndDate
(
StartDate
,
EndDate
,
interactive
=
interactive
)
dateReturn
<-
checkStartEndDate
(
StartDate
,
EndDate
,
interactive
=
interactive
)
StartDate
<-
dateReturn
[
1
]
StartDate
<-
dateReturn
[
1
]
EndDate
<-
dateReturn
[
2
]
EndDate
<-
dateReturn
[
2
]
...
@@ -47,19 +47,19 @@ retrieveUnitNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,inter
...
@@ -47,19 +47,19 @@ retrieveUnitNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,inter
tmp
<-
read.delim
(
tmp
<-
read.delim
(
url
,
url
,
header
=
FALS
E
,
header
=
TRU
E
,
quote
=
"\""
,
quote
=
"\""
,
dec
=
"."
,
dec
=
"."
,
sep
=
'\t'
,
sep
=
'\t'
,
colClasses
=
c
(
'character'
),
colClasses
=
c
(
'character'
),
fill
=
TRUE
,
fill
=
TRUE
,
comment.char
=
"#"
)
comment.char
=
"#"
)
col.nm
<-
make.names
(
unlist
(
tmp
[
1
,,
drop
=
TRUE
]),
allow_
=
FALSE
)
retval
<-
lapply
(
tmp
,
function
(
x
)
{
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)
Typ
<-
x
[
1
]
# The type - the
first non-header
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)
x
<-
x
[
-
c
(
1
)]
# the data - takes away the first
1st row
if
(
regexpr
(
'd$'
,
Typ
)
>
0
)
{
# Must be date
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...
ret.val
<-
as.POSIXct
(
strptime
(
x
,
"%Y-%m-%d %H:%M"
))
}
}
else
if
(
regexpr
(
'n$'
,
Typ
)
>
0
)
# Must be numeric
else
if
(
regexpr
(
'n$'
,
Typ
)
>
0
)
# Must be numeric
ret.val
<-
as.numeric
(
x
)
ret.val
<-
as.numeric
(
x
)
...
...
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