From 8d8e4f9844b020f624584761da3f8a1759c0a426 Mon Sep 17 00:00:00 2001 From: Laura DeCicco <ldecicco@usgs.gov> Date: Tue, 22 Jan 2013 16:07:10 -0600 Subject: [PATCH] Changed the method to get and sort data to a more R-friendly way. This is also more robust at handling 'Ice' values. --- R/retrieveNWISData.r | 30 +++++++++++------------------- R/retrieveUnitNWISData.r | 30 +++++++++++++----------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/R/retrieveNWISData.r b/R/retrieveNWISData.r index 8e3ca945..8454e861 100644 --- a/R/retrieveNWISData.r +++ b/R/retrieveNWISData.r @@ -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 diff --git a/R/retrieveUnitNWISData.r b/R/retrieveUnitNWISData.r index 3aa6d395..e2b7c1b3 100644 --- a/R/retrieveUnitNWISData.r +++ b/R/retrieveUnitNWISData.r @@ -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 -- GitLab