diff --git a/R/importWQP.R b/R/importWQP.R index 7e5d721ad526b2291f34e996cb122ab97eded05e..8333d4962d0e023e6c771d4db356e63d80ab214a 100644 --- a/R/importWQP.R +++ b/R/importWQP.R @@ -27,11 +27,10 @@ importWQP <- function(url, zip=FALSE){ if(zip){ headerInfo <- HEAD(url)$headers temp <- tempfile() -# origTimeout <- getOption("timeout") options(timeout = 120) download.file(url,temp, quiet=TRUE, mode='wb') doc <- unzip(temp) -# options(timeout = origTimeout) + unlink(temp) } else { doc <- getURL(url, headerfunction = h$update) headerInfo <- h$value() @@ -51,9 +50,6 @@ importWQP <- function(url, zip=FALSE){ numToBeReturned <- as.numeric(headerInfo["Total-Result-Count"]) if (!is.na(numToBeReturned) | numToBeReturned != 0){ -# -# fileName <- strsplit(as.character(headerInfo["Content-disposition"]),"=")[[1]][2] -# fileName <- gsub('\"',"",fileName) suppressWarnings(namesData <- read.delim(if(zip) doc else textConnection(doc) , header = TRUE, quote="\"", dec=".", sep='\t', @@ -69,24 +65,19 @@ importWQP <- function(url, zip=FALSE){ colClasses=as.character(classColumns), fill = TRUE)) - if(zip){ - unlink(temp) - } - 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") - timeZoneLibrary <- setNames(c("America/New_York","America/New_York","America/Chicago","America/Chicago", - "America/Denver","America/Denver","America/Los_Angeles","America/Los_Angeles", - "America/Anchorage","America/Anchorage","America/Honolulu","America/Honolulu"), - c("EST","EDT","CST","CDT","MST","MDT","PST","PDT","AKST","AKDT","HAST","HST")) - timeZoneStart <- as.character(timeZoneLibrary[retval$ActivityStartTime.TimeZoneCode]) - timeZoneEnd <- as.character(timeZoneLibrary[retval$ActivityEndTime.TimeZoneCode]) - timeZoneStart[is.na(timeZoneStart)] <- "" - timeZoneEnd[is.na(timeZoneEnd)] <- "" + 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 != "")){ @@ -101,54 +92,27 @@ importWQP <- function(url, zip=FALSE){ } if(any(!is.na(timeZoneStart))){ - if(length(unique(timeZoneStart)) == 1){ - retval$ActivityStartDateTime <- with(retval, as.POSIXct(paste(ActivityStartDate, ActivityStartTime.Time),format="%Y-%m-%d %H:%M:%S", tz=unique(timeZoneStart))) - } else { - - mostCommonTZ <- names(sort(summary(as.factor(timeZoneStart)),decreasing = TRUE)[1]) - - retval$ActivityStartDateTime <- with(retval, as.POSIXct(paste(ActivityStartDate, ActivityStartTime.Time), - format="%Y-%m-%d %H:%M:%S", - tz=mostCommonTZ)) - additionalTZs <- names(sort(summary(as.factor(timeZoneStart)),decreasing = TRUE)[-1]) - for(i in additionalTZs){ - retval$ActivityStartDateTime[timeZoneStart == i] <- with(retval[timeZoneStart == i,], - as.POSIXct(paste(ActivityStartDate, ActivityStartTime.Time), - format="%Y-%m-%d %H:%M:%S", - tz=i)) - } - } + + 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) + } if(any(!is.na(timeZoneEnd))){ - if(length(unique(timeZoneEnd)) == 1){ - retval$ActivityEndDateTime <- with(retval, as.POSIXct(paste(ActivityEndDate, ActivityEndTime.Time), format="%Y-%m-%d %H:%M:%S",tz=unique(timeZoneEnd))) - } else { - mostCommonTZ <- names(sort(summary(as.factor(timeZoneEnd)),decreasing = TRUE)[1]) - - retval$ActivityEndDateTime <- with(retval, as.POSIXct(paste(ActivityEndDate, ActivityEndTime.Time), - format="%Y-%m-%d %H:%M:%S", - tz=mostCommonTZ)) - additionalTZs <- names(sort(summary(as.factor(timeZoneEnd)),decreasing = TRUE)[-1]) - for(i in additionalTZs){ - retval$ActivityEndDateTime[timeZoneEnd == i] <- with(retval[timeZoneEnd == i,], - as.POSIXct(paste(ActivityEndDate, ActivityEndTime.Time), - format="%Y-%m-%d %H:%M:%S", - tz=i)) - } - } + + 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) + + } return(retval) - if(zip) unlink(temp) } else { warning("No data to retrieve") return(NA) } -# } else { -# message(paste("URL caused an error:", url)) -# message("Content-Type=",h$value()["Content-Type"]) -# return(NA) -# } + } \ No newline at end of file