Skip to content
Snippets Groups Projects
Commit 8d8e4f98 authored by Laura A DeCicco's avatar Laura A DeCicco
Browse files

Changed the method to get and sort data to a more R-friendly way. This is also...

Changed the method to get and sort data to a more R-friendly way. This is also more robust at handling 'Ice' values.
parent b79e12d4
No related branches found
No related tags found
No related merge requests found
......@@ -11,12 +11,13 @@
#' @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
#' @return data dataframe with agency, site, dateTime, value, and code columns
#' @export
#' @examples
#' # These examples require an internet connection to run
#' rawDailyFlowData <- retrieveNWISData('01594440','00060', '1985-01-01', '1985-01-31')
#' rawDailyTemperatureData <- retrieveNWISData('05114000','00010', '1985-01-01', '1985-01-31', StatCd='00001',interactive=FALSE)
#' rawDailyFlowAndTemperatureData <- retrieveNWISData('04085427','00010,00060', '2012-01-01', '2012-06-30', interactive=FALSE)
retrieveNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,StatCd="00003",interactive=TRUE){
# Checking for 8 digit site ID:
......@@ -56,23 +57,14 @@ retrieveNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,StatCd="0
fill = TRUE,
comment.char="#")
retval <- lapply(tmp, function(x) {
Typ <- x[1] # The type
x <- x[-c(1)] # the data - takes away the first 1st row (non-header)
if(regexpr('d$', Typ) > 0) { # Must be date
ret.val <- try(as.Date(x)) # The data are in standard format, but...
if(class(ret.val) == "try-error")
ret.val <- x
}
# else if(regexpr('n$', Typ) > 0) # Must be numeric...be careful of ice
# ret.val <- as.numeric(x)
else # Must be character
ret.val <- x
return(ret.val)})
dataType <- tmp[1,]
data <- tmp[-1,]
data[,regexpr('d$', dataType) > 0] <- as.Date(data[,regexpr('d$', dataType) > 0])
retval <- as.data.frame(retval, stringsAsFactors=FALSE)
# colNames <- names(retval)
return (retval)
tempDF <- data[,which(regexpr('n$', dataType) > 0)]
tempDF <- suppressWarnings(sapply(tempDF, function(x) as.numeric(x)))
data[,which(regexpr('n$', dataType) > 0)] <- tempDF
row.names(data) <- NULL
return (data)
}
\ No newline at end of file
......@@ -10,13 +10,13 @@
#' @param EndDate string ending date for data retrieval in the form YYYY-MM-DD.
#' @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, time zone, value, and code columns
#' @return data dataframe with agency, site, dateTime, time zone, value, and code columns
#' @export
#' @examples
#' siteNumber <- '05114000'
#' ParameterCd <- '00060'
#' StartDate <- '2012-05-01'
#' EndDate <- '2012-05-02'
#' StartDate <- as.character(Sys.Date())
#' EndDate <- as.character(Sys.Date())
#' # These examples require an internet connection to run
#' rawData <- retrieveUnitNWISData(siteNumber,ParameterCd,StartDate,EndDate,interactive=FALSE)
retrieveUnitNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,interactive=TRUE){
......@@ -55,18 +55,14 @@ retrieveUnitNWISData <- function (siteNumber,ParameterCd,StartDate,EndDate,inter
fill = TRUE,
comment.char="#")
retval <- lapply(tmp, function(x) {
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)] # the data - takes away the first 1st row
if(regexpr('d$', Typ) > 0) { # Must be date
ret.val <- as.POSIXct(strptime(x, "%Y-%m-%d %H:%M"))
}
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', 'tzone', 'value', 'code')
return (retval)
dataType <- tmp[1,]
data <- tmp[-1,]
data[,regexpr('d$', dataType) > 0] <- as.POSIXct(strptime(data[,regexpr('d$', dataType) > 0], "%Y-%m-%d %H:%M"))
tempDF <- data[,which(regexpr('n$', dataType) > 0)]
tempDF <- suppressWarnings(sapply(tempDF, function(x) as.numeric(x)))
data[,which(regexpr('n$', dataType) > 0)] <- tempDF
row.names(data) <- NULL
return (data)
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment