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
Merge requests
!58
Improving error handling.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Improving error handling.
ldecicco-USGS:master
into
master
Overview
6
Commits
7
Pipelines
0
Changes
6
Merged
Laura A DeCicco
requested to merge
ldecicco-USGS:master
into
master
10 years ago
Overview
6
Commits
7
Pipelines
0
Changes
6
Expand
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
8533d6e1
7 commits,
6 years ago
6 files
+
185
−
171
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
6
Search (e.g. *.vue) (Ctrl+P)
R/importWQP.R
+
94
−
85
Options
@@ -28,107 +28,116 @@
#' }
importWQP
<-
function
(
url
,
zip
=
FALSE
,
tz
=
""
){
h
<-
basicHeaderGatherer
()
if
(
zip
){
headerInfo
<-
HEAD
(
url
)
$
headers
temp
<-
tempfile
()
options
(
timeout
=
120
)
possibleError
<-
tryCatch
(
download.file
(
url
,
temp
,
quiet
=
TRUE
,
mode
=
'wb'
),
error
=
function
(
e
)
e
)
}
else
{
h
<-
basicHeaderGatherer
()
possibleError
<-
tryCatch
(
doc
<-
getURL
(
url
,
headerfunction
=
h
$
update
),
error
=
function
(
e
)
e
)
}
tryCatch
({
if
(
!
inherits
(
possibleError
,
"error"
)){
if
(
zip
){
headerInfo
<-
HEAD
(
url
)
$
headers
temp
<-
tempfile
()
options
(
timeout
=
120
)
download.file
(
url
,
temp
,
quiet
=
TRUE
,
mode
=
'wb'
)
doc
<-
unzip
(
temp
)
unlink
(
temp
)
}
else
{
doc
<-
getURL
(
url
,
headerfunction
=
h
$
update
)
headerInfo
<-
h
$
value
()
}
if
(
tz
!=
""
){
tz
<-
match.arg
(
tz
,
c
(
"America/New_York"
,
"America/Chicago"
,
"America/Denver"
,
"America/Los_Angeles"
,
"America/Anchorage"
,
"America/Honolulu"
,
"America/Jamaica"
,
"America/Managua"
,
"America/Phoenix"
,
"America/Metlakatla"
))
}
},
warning
=
function
(
w
)
{
message
(
paste
(
"URL caused a warning:"
,
url
))
message
(
w
)
},
error
=
function
(
e
)
{
message
(
paste
(
"URL does not seem to exist:"
,
url
))
message
(
e
)
return
(
NA
)
})
if
(
tz
!=
""
){
tz
<-
match.arg
(
tz
,
c
(
"America/New_York"
,
"America/Chicago"
,
"America/Denver"
,
"America/Los_Angeles"
,
"America/Anchorage"
,
"America/Honolulu"
,
"America/Jamaica"
,
"America/Managua"
,
"America/Phoenix"
,
"America/Metlakatla"
))
}
numToBeReturned
<-
as.numeric
(
headerInfo
[
"Total-Result-Count"
])
numToBeReturned
<-
as.numeric
(
headerInfo
[
"Total-Result-Count"
])
if
(
!
is.na
(
numToBeReturned
)
&
numToBeReturned
!=
0
){
if
(
!
is.na
(
numToBeReturned
)
|
numToBeReturned
!=
0
){
suppressWarnings
(
namesData
<-
read.delim
(
if
(
zip
)
doc
else
textConnection
(
doc
)
,
header
=
TRUE
,
quote
=
"\""
,
dec
=
"."
,
sep
=
'\t'
,
colClasses
=
'character'
,
fill
=
TRUE
,
nrow
=
1
))
classColumns
<-
setNames
(
rep
(
'character'
,
ncol
(
namesData
)),
names
(
namesData
))
classColumns
[
grep
(
"MeasureValue"
,
names
(
classColumns
))]
<-
NA
suppressWarnings
(
retval
<-
read.delim
(
if
(
zip
)
doc
else
textConnection
(
doc
),
header
=
TRUE
,
quote
=
"\""
,
dec
=
"."
,
sep
=
'\t'
,
colClasses
=
as.character
(
classColumns
),
fill
=
TRUE
))
actualNumReturned
<-
nrow
(
retval
)
retval
[,
names
(
which
(
sapply
(
retval
[,
grep
(
"MeasureValue"
,
names
(
retval
))],
function
(
x
)
all
(
is.na
(
x
)))))]
<-
""
if
(
actualNumReturned
!=
numToBeReturned
)
warning
(
numToBeReturned
,
" sample results were expected, "
,
actualNumReturned
,
" were returned"
)
offsetLibrary
<-
setNames
(
c
(
5
,
4
,
6
,
5
,
7
,
6
,
8
,
7
,
9
,
8
,
10
,
10
),
c
(
"EST"
,
"EDT"
,
"CST"
,
"CDT"
,
"MST"
,
"MDT"
,
"PST"
,
"PDT"
,
"AKST"
,
"AKDT"
,
"HAST"
,
"HST"
))
timeZoneStart
<-
offsetLibrary
[
retval
$
ActivityStartTime.TimeZoneCode
]
timeZoneEnd
<-
offsetLibrary
[
retval
$
ActivityEndTime.TimeZoneCode
]
timeZoneStart
[
is.na
(
timeZoneStart
)]
<-
0
timeZoneEnd
[
is.na
(
timeZoneEnd
)]
<-
0
if
(
"ActivityStartDate"
%in%
names
(
retval
)){
if
(
any
(
retval
$
ActivityStartDate
!=
""
)){
suppressWarnings
(
retval
$
ActivityStartDate
<-
as.Date
(
parse_date_time
(
retval
$
ActivityStartDate
,
c
(
"Ymd"
,
"mdY"
))))
suppressWarnings
(
namesData
<-
read.delim
(
if
(
zip
)
doc
else
textConnection
(
doc
)
,
header
=
TRUE
,
quote
=
"\""
,
dec
=
"."
,
sep
=
'\t'
,
colClasses
=
'character'
,
fill
=
TRUE
,
nrow
=
1
))
classColumns
<-
setNames
(
rep
(
'character'
,
ncol
(
namesData
)),
names
(
namesData
))
classColumns
[
grep
(
"MeasureValue"
,
names
(
classColumns
))]
<-
NA
suppressWarnings
(
retval
<-
read.delim
(
if
(
zip
)
doc
else
textConnection
(
doc
),
header
=
TRUE
,
quote
=
"\""
,
dec
=
"."
,
sep
=
'\t'
,
colClasses
=
as.character
(
classColumns
),
fill
=
TRUE
))
actualNumReturned
<-
nrow
(
retval
)
retval
[,
names
(
which
(
sapply
(
retval
[,
grep
(
"MeasureValue"
,
names
(
retval
))],
function
(
x
)
all
(
is.na
(
x
)))))]
<-
""
if
(
actualNumReturned
!=
numToBeReturned
)
warning
(
numToBeReturned
,
" sample results were expected, "
,
actualNumReturned
,
" were returned"
)
offsetLibrary
<-
setNames
(
c
(
5
,
4
,
6
,
5
,
7
,
6
,
8
,
7
,
9
,
8
,
10
,
10
),
c
(
"EST"
,
"EDT"
,
"CST"
,
"CDT"
,
"MST"
,
"MDT"
,
"PST"
,
"PDT"
,
"AKST"
,
"AKDT"
,
"HAST"
,
"HST"
))
timeZoneStart
<-
offsetLibrary
[
retval
$
ActivityStartTime.TimeZoneCode
]
timeZoneEnd
<-
offsetLibrary
[
retval
$
ActivityEndTime.TimeZoneCode
]
timeZoneStart
[
is.na
(
timeZoneStart
)]
<-
0
timeZoneEnd
[
is.na
(
timeZoneEnd
)]
<-
0
if
(
"ActivityStartDate"
%in%
names
(
retval
)){
if
(
any
(
retval
$
ActivityStartDate
!=
""
)){
suppressWarnings
(
retval
$
ActivityStartDate
<-
as.Date
(
parse_date_time
(
retval
$
ActivityStartDate
,
c
(
"Ymd"
,
"mdY"
))))
}
}
if
(
"ActivityEndDate"
%in%
names
(
retval
)){
if
(
any
(
retval
$
ActivityEndDate
!=
""
)){
suppressWarnings
(
retval
$
ActivityEndDate
<-
as.Date
(
parse_date_time
(
retval
$
ActivityEndDate
,
c
(
"Ymd"
,
"mdY"
))))
}
}
if
(
any
(
!
is.na
(
timeZoneStart
))){
retval
$
ActivityStartDateTime
<-
with
(
retval
,
as.POSIXct
(
paste
(
ActivityStartDate
,
ActivityStartTime.Time
),
format
=
"%Y-%m-%d %H:%M:%S"
,
tz
=
"UTC"
))
retval
$
ActivityStartDateTime
<-
retval
$
ActivityStartDateTime
+
timeZoneStart
*
60
*
60
retval
$
ActivityStartDateTime
<-
as.POSIXct
(
retval
$
ActivityStartDateTime
)
attr
(
retval
$
ActivityStartDateTime
,
"tzone"
)
<-
"UTC"
}
}
if
(
"ActivityEndDate"
%in%
names
(
retval
)){
if
(
any
(
retval
$
ActivityEndDate
!=
""
)){
suppressWarnings
(
retval
$
ActivityEndDate
<-
as.Date
(
parse_date_time
(
retval
$
ActivityEndDate
,
c
(
"Ymd"
,
"mdY"
))))
}
}
if
(
any
(
!
is.na
(
timeZoneStart
))){
retval
$
ActivityStartDateTime
<-
with
(
retval
,
as.POSIXct
(
paste
(
ActivityStartDate
,
ActivityStartTime.Time
),
format
=
"%Y-%m-%d %H:%M:%S"
,
tz
=
"UTC"
))
retval
$
ActivityStartDateTime
<-
retval
$
ActivityStartDateTime
+
timeZoneStart
*
60
*
60
retval
$
ActivityStartDateTime
<-
as.POSIXct
(
retval
$
ActivityStartDateTime
)
attr
(
retval
$
ActivityStartDateTime
,
"tzone"
)
<-
"UTC"
if
(
any
(
!
is.na
(
timeZoneEnd
))){
retval
$
ActivityEndDateTime
<-
with
(
retval
,
as.POSIXct
(
paste
(
ActivityEndDate
,
ActivityEndTime.Time
),
format
=
"%Y-%m-%d %H:%M:%S"
,
tz
=
"UTC"
))
retval
$
ActivityEndDateTime
<-
retval
$
ActivityEndDateTime
+
timeZoneEnd
*
60
*
60
retval
$
ActivityEndDateTime
<-
as.POSIXct
(
retval
$
ActivityEndDateTime
)
attr
(
retval
$
ActivityEndDateTime
,
"tzone"
)
<-
"UTC"
}
}
if
(
any
(
!
is.na
(
timeZoneEnd
))){
retval
$
ActivityEndDateTime
<-
with
(
retval
,
as.POSIXct
(
paste
(
ActivityEndDate
,
ActivityEndTime.Time
),
format
=
"%Y-%m-%d %H:%M:%S"
,
tz
=
"UTC"
))
retval
$
ActivityEndDateTime
<-
retval
$
ActivityEndDateTime
+
timeZoneEnd
*
60
*
60
retval
$
ActivityEndDateTime
<-
as.POSIXct
(
retval
$
ActivityEndDateTime
)
attr
(
retval
$
ActivityEndDateTime
,
"tzone"
)
<-
"UTC"
}
if
(
all
(
is.na
(
retval
$
ActivityEndDateTime
))){
retval
$
ActivityEndDateTime
<-
NULL
}
return
(
retval
)
if
(
all
(
is.na
(
retval
$
ActivityEndDateTime
))){
retval
$
ActivityEndDateTime
<-
NULL
}
else
{
warning
(
"No data to retrieve"
)
return
(
NA
)
}
return
(
retval
)
}
else
{
warning
(
"No data to retrieve"
)
return
(
NA
)
if
(
zip
){
unlink
(
temp
)
}
message
(
e
)
}
}
\ No newline at end of file
Loading