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
f1c330d6
Commit
f1c330d6
authored
10 years ago
by
Laura A DeCicco
Browse files
Options
Downloads
Patches
Plain Diff
Removed WaterML2 until fixed.
parent
345032dd
No related branches found
No related tags found
1 merge request
!25
Crannifying.
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
R/getWaterML2Data.r
+0
-75
0 additions, 75 deletions
R/getWaterML2Data.r
with
0 additions
and
75 deletions
R/getWaterML2Data.r
deleted
100644 → 0
+
0
−
75
View file @
345032dd
#' Function to return data from the WaterML2 data
#'
#' This function accepts a url parameter for a WaterML2 getObservation
#'
#' @param obs_url string containing the url for the retrieval
#' @return mergedDF a data frame containing columns agency, site, dateTime, values, and remark codes for all requested combinations
#' @export
#' @import XML
#' @importFrom dplyr rbind_all
#' @examples
#' URL <- "http://waterservices.usgs.gov/nwis/dv/?format=waterml,2.0&sites=01646500&startDT=2014-09-01&endDT=2014-09-08&statCd=00003¶meterCd=00060"
#' \dontrun{dataReturned3 <- getWaterML2Data(URL)}
getWaterML2Data
<-
function
(
obs_url
){
h
<-
basicHeaderGatherer
()
doc
=
tryCatch
({
returnedDoc
<-
getURL
(
obs_url
,
headerfunction
=
h
$
update
)
if
(
h
$
value
()[
"Content-Type"
]
==
"text/xml;charset=UTF-8"
){
xmlTreeParse
(
returnedDoc
,
getDTD
=
FALSE
,
useInternalNodes
=
TRUE
)
}
else
{
message
(
paste
(
"URL caused an error:"
,
obs_url
))
message
(
"Content-Type="
,
h
$
value
()[
"Content-Type"
])
return
(
NA
)
}
},
warning
=
function
(
w
)
{
message
(
paste
(
"URL caused a warning:"
,
obs_url
))
message
(
w
)
},
error
=
function
(
e
)
{
message
(
paste
(
"URL does not seem to exist:"
,
obs_url
))
message
(
e
)
return
(
NA
)
})
doc
<-
xmlRoot
(
doc
)
ns
<-
xmlNamespaceDefinitions
(
doc
,
simplify
=
TRUE
)
timeseries2
<-
xpathApply
(
doc
,
"//wml2:MeasurementTimeseries/wml2:point"
,
namespaces
=
ns
)
xp
<-
xpathApply
(
doc
,
"//wml2:MeasurementTimeseries/wml2:point/wml2:MeasurementTVP"
,
xpathSApply
,
".//*[not(*)]"
,
function
(
x
)
setNames
(
ifelse
(
nzchar
(
xmlValue
(
x
)),
xmlValue
(
x
),
ifelse
(
"qualifier"
==
xmlName
(
x
),
xpathSApply
(
x
,
"./@xlink:title"
,
namespaces
=
ns
),
""
)),
#originally I had the "" as xmlAttr(x)
xmlName
(
x
)),
namespaces
=
ns
)
DF2
<-
do.call
(
rbind_all
,
lapply
(
xp
,
t
))
DF2
<-
as.data.frame
(
DF2
,
stringsAsFactors
=
FALSE
)
DF2
$
time
<-
gsub
(
":"
,
""
,
DF2
$
time
)
DF2
$
time
<-
with
(
DF2
,
ifelse
(
nchar
(
time
)
>
18
,
as.POSIXct
(
strptime
(
time
,
format
=
"%Y-%m-%dT%H%M%S%z"
)),
ifelse
(
"Z"
==
substr
(
time
,(
nchar
(
time
)),
nchar
(
time
)),
as.POSIXct
(
strptime
(
time
,
format
=
"%Y-%m-%dT%H%M%S"
,
tz
=
"GMT"
)),
as.POSIXct
(
strptime
(
time
,
format
=
"%Y-%m-%dT%H%M%S"
,
tz
=
""
)))))
DF2
$
time
<-
with
(
DF2
,
as.POSIXct
(
time
,
origin
=
as.POSIXct
(
strptime
(
"1970-01-01 00:00:00"
,
"%Y-%m-%d %H:%M:%S"
,
tz
=
"UTC"
))))
DF2
$
value
<-
as.numeric
(
gsub
(
"true"
,
""
,
DF2
$
value
))
# Very specific to USGS:
defaultQualifier
<-
as.character
(
xpathApply
(
doc
,
"//wml2:defaultPointMetadata/wml2:DefaultTVPMeasurementMetadata/wml2:qualifier/@xlink:title"
,
namespaces
=
ns
))
if
(
length
(
defaultQualifier
)
==
0
&&
(
typeof
(
defaultQualifier
)
==
"character"
))
{
defaultQualifier
<-
"NA"
}
if
(
"qualifier"
%in%
names
(
DF2
)){
DF2
$
qualifier
<-
ifelse
(
defaultQualifier
!=
DF2
$
qualifier
,
DF2
$
qualifier
,
defaultQualifier
)
}
else
{
DF2
$
qualifier
<-
rep
(
defaultQualifier
,
nrow
(
DF2
))
}
DF2
$
qualifier
<-
ifelse
(
"Provisional data subject to revision."
==
DF2
$
qualifier
,
"P"
,
ifelse
(
"Approved for publication. Processing and review completed."
==
DF2
$
qualifier
,
"A"
,
DF2
$
qualifier
))
return
(
DF2
)
}
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