From 716bac3628c1d0f76dfcf6746f07a1a134f117a5 Mon Sep 17 00:00:00 2001 From: Laura DeCicco Date: Tue, 26 Jul 2016 08:31:29 -0500 Subject: [PATCH 1/2] fix to makeAnnualSeries --- R/makeAnnualSeries.R | 74 ++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/R/makeAnnualSeries.R b/R/makeAnnualSeries.R index 2b7b9688..0131218c 100644 --- a/R/makeAnnualSeries.R +++ b/R/makeAnnualSeries.R @@ -30,9 +30,8 @@ makeAnnualSeries<-function(eList, edgeAdjust = TRUE) { localINFO <- getInfo(eList) localDaily <- getDaily(eList) - if (sum(c("paStart", "paLong", "window") %in% names(localINFO)) == - 3) { + 3) { paLong <- localINFO$paLong paStart <- localINFO$paStart window <- localINFO$window @@ -41,11 +40,9 @@ makeAnnualSeries<-function(eList, edgeAdjust = TRUE) { paStart <- 10 window <- 20 } - - if("edgeAdjust" %in% names(localINFO)){ + if ("edgeAdjust" %in% names(localINFO)) { edgeAdjust <- localINFO$edgeAdjust } - numDays <- length(localDaily$DecYear) yearFirst <- trunc(localDaily$DecYear[1]) yearLast <- trunc(localDaily$DecYear[numDays]) @@ -58,73 +55,70 @@ makeAnnualSeries<-function(eList, edgeAdjust = TRUE) { Starts <- seq(paStartLow, monthSeqLast, 12) Ends <- Starts + paLong - 1 startEndSeq <- data.frame(Starts, Ends) - startEndSeq <- subset(startEndSeq, (Ends >= monthSeqFirst) & (Starts <= monthSeqLast)) + startEndSeq <- subset(startEndSeq, (Ends >= monthSeqFirst) & + (Starts <= monthSeqLast)) numYSeq <- length(startEndSeq$Ends) - for (i in 1:numYSeq) { startSeq <- startEndSeq$Starts[i] endSeq <- startEndSeq$Ends[i] - yearDaily <- localDaily[localDaily$MonthSeq >= startSeq & (localDaily$MonthSeq <= endSeq),] - + yearDaily <- localDaily[localDaily$MonthSeq >= startSeq & + (localDaily$MonthSeq <= endSeq), ] goodDay <- length(yearDaily$Q) - sum(is.na(yearDaily$Q)) - - if (goodDay > 26 * paLong){ - annualSeries[1, 1:3, i] <- mean(yearDaily$DecYear,na.rm = TRUE) - annualSeries[2, 1, i] <- min(yearDaily$Q, na.rm = TRUE) - annualSeries[2, 2, i] <- min(yearDaily$Q7, na.rm = TRUE) - annualSeries[2, 3, i] <- min(yearDaily$Q30, na.rm = TRUE) + if (goodDay > 26 * paLong) { + annualSeries[1, 1:3, i] <- mean(yearDaily$DecYear, + na.rm = TRUE) + annualSeries[2, 1, i] <- min(yearDaily$Q, na.rm = TRUE) + annualSeries[2, 2, i] <- min(yearDaily$Q7, na.rm = TRUE) + annualSeries[2, 3, i] <- min(yearDaily$Q30, na.rm = TRUE) } - } Starts <- seq(paStart, monthSeqLast, 12) Ends <- Starts + paLong - 1 startEndSeq <- data.frame(Starts, Ends) - startEndSeq <- subset(startEndSeq, (Ends >= monthSeqFirst) & (Starts <= monthSeqLast)) + startEndSeq <- subset(startEndSeq, (Ends >= monthSeqFirst) & + (Starts <= monthSeqLast)) numYSeq <- length(startEndSeq$Ends) for (i in 1:numYSeq) { startSeq <- startEndSeq$Starts[i] endSeq <- startEndSeq$Ends[i] - yearDaily <- localDaily[localDaily$MonthSeq >= startSeq & (localDaily$MonthSeq <= endSeq),] - + yearDaily <- localDaily[localDaily$MonthSeq >= startSeq & + (localDaily$MonthSeq <= endSeq), ] goodDay <- length(yearDaily$Q) - sum(is.na(yearDaily$Q)) - - if(goodDay > 26 * paLong){ - annualSeries[1, 4:8, i] <- mean(yearDaily$DecYear, na.rm = TRUE) + if (goodDay > 26 * paLong) { + annualSeries[1, 4:8, i] <- mean(yearDaily$DecYear, + na.rm = TRUE) annualSeries[2, 4, i] <- median(yearDaily$Q, na.rm = TRUE) annualSeries[2, 5, i] <- mean(yearDaily$Q, na.rm = TRUE) annualSeries[2, 6, i] <- max(yearDaily$Q30, na.rm = TRUE) annualSeries[2, 7, i] <- max(yearDaily$Q7, na.rm = TRUE) - annualSeries[2, 8, i] <- max(yearDaily$Q, na.rm = TRUE) + annualSeries[2, 8, i] <- max(yearDaily$Q, na.rm = TRUE) } } for (istat in 1:8) { x <- annualSeries[1, istat, ] y <- log(annualSeries[2, istat, ]) - originalYear <- x - numYear <- length(x) - xy <- data.frame(x, y) + baseYear <- trunc(x[1]) + numYears <- length(x) + xVec <- seq(1,numYears) + xy <- data.frame(x,y,xVec) xy <- na.omit(xy) - numXY <- length(xy$x) + goodYears <- length(xy$x) x <- xy$x - newYear <- x - numNewYear <- length(newYear) - x1 <- newYear[1] - xn <- newYear[numNewYear] - orYear <- originalYear[1:numNewYear] - diff <- newYear - orYear - meanDiff <- mean(diff, na.rm = TRUE) - offset <- round(meanDiff) - for (i in 1:numXY) { + x1 <- x[1] + xn <- x[goodYears] + for (i in 1:goodYears) { xi <- x[i] - distToEdge <- min((xi - x1),(xn - xi)) + distToEdge <- min((xi - x1), (xn - xi)) close <- (distToEdge < window) - thisWindow <- if(edgeAdjust & close) (2*window) - distToEdge else window + thisWindow <- if (edgeAdjust & close) + (2 * window) - distToEdge + else window w <- triCube(x - xi, thisWindow) mod <- lm(xy$y ~ x, weights = w) new <- data.frame(x = x[i]) z <- exp(predict(mod, new)) - ioffset <- i + offset - annualSeries[3, istat, ioffset] <- z + iYear <- xy$xVec[i] + annualSeries[3, istat, iYear] <- z } } return(annualSeries) From 1782a2ade00cc89770a9d3ab08ca5d2c3c6693ee Mon Sep 17 00:00:00 2001 From: Laura DeCicco Date: Tue, 26 Jul 2016 09:26:37 -0500 Subject: [PATCH 2/2] Fixes #89 --- R/readNWISDaily.r | 25 ++++++++++++++++++++----- R/readNWISSample.r | 29 +++++++++++++++++++++++------ R/readWQPSample.R | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/R/readNWISDaily.r b/R/readNWISDaily.r index 311c59b0..05d99d12 100644 --- a/R/readNWISDaily.r +++ b/R/readNWISDaily.r @@ -53,12 +53,27 @@ readNWISDaily <- function (siteNumber,parameterCd="00060", if(nrow(data)>0){ names(data) <- c('agency', 'site', 'dateTime', 'value', 'code') data$dateTime <- as.Date(data$dateTime) + ##################################### + qConvert <- ifelse("00060" == parameterCd, 35.314667, 1) + qConvert<- ifelse(convert,qConvert,1) + + localDaily <- populateDaily(data,qConvert,interactive=interactive) + } else { + localDaily <- data.frame(Date=as.Date(character()), + Q=numeric(), + Julian=numeric(), + Month=numeric(), + Day=numeric(), + DecYear=numeric(), + MonthSeq=numeric(), + Qualifier=character(), + i=integer(), + LogQ=numeric(), + Q7=numeric(), + Q30=numeric(), + stringsAsFactors=FALSE) } - ##################################### - qConvert <- ifelse("00060" == parameterCd, 35.314667, 1) - qConvert<- ifelse(convert,qConvert,1) - - localDaily <- populateDaily(data,qConvert,interactive=interactive) + return (localDaily) } diff --git a/R/readNWISSample.r b/R/readNWISSample.r index 2191b178..6a81f3c3 100644 --- a/R/readNWISSample.r +++ b/R/readNWISSample.r @@ -42,12 +42,29 @@ readNWISSample <- function(siteNumber,parameterCd,startDate="",endDate="",interactive=TRUE){ rawSample <- dataRetrieval::readNWISqw(siteNumber,parameterCd,startDate,endDate, expanded=FALSE) - dataColumns <- grep("p\\d{5}",names(rawSample)) - remarkColumns <- grep("r\\d{5}",names(rawSample)) - totalColumns <-c(grep("sample_dt",names(rawSample)), dataColumns, remarkColumns) - totalColumns <- totalColumns[order(totalColumns)] - compressedData <- compressData(rawSample[,totalColumns], interactive=interactive) - Sample <- populateSampleColumns(compressedData) + if(nrow(rawSample) > 0){ + dataColumns <- grep("p\\d{5}",names(rawSample)) + remarkColumns <- grep("r\\d{5}",names(rawSample)) + totalColumns <-c(grep("sample_dt",names(rawSample)), dataColumns, remarkColumns) + totalColumns <- totalColumns[order(totalColumns)] + compressedData <- compressData(rawSample[,totalColumns], interactive=interactive) + Sample <- populateSampleColumns(compressedData) + } else { + Sample <- data.frame(Date=as.Date(character()), + ConcLow=numeric(), + ConcHigh=numeric(), + Uncen=numeric(), + ConcAve=numeric(), + Julian=numeric(), + Month=numeric(), + Day=numeric(), + DecYear=numeric(), + MonthSeq=numeric(), + SinDY=numeric(), + CosDY=numeric(), + stringsAsFactors=FALSE) + } + return(Sample) } diff --git a/R/readWQPSample.R b/R/readWQPSample.R index db812e7f..35c79e69 100644 --- a/R/readWQPSample.R +++ b/R/readWQPSample.R @@ -40,21 +40,36 @@ readWQPSample <- function(siteNumber,characteristicName,startDate,endDate,intera url <- constructWQPURL(siteNumber,characteristicName,startDate,endDate) retval <- importWQP(url) - - #Check for pcode: - if(all(nchar(characteristicName) == 5)){ - suppressWarnings(pCodeLogic <- all(!is.na(as.numeric(characteristicName)))) - } else { - pCodeLogic <- FALSE - } - if(nrow(retval) > 0){ - data <- processQWData(retval,pCodeLogic) + #Check for pcode: + if(all(nchar(characteristicName) == 5)){ + suppressWarnings(pCodeLogic <- all(!is.na(as.numeric(characteristicName)))) + } else { + pCodeLogic <- FALSE + } + + if(nrow(retval) > 0){ + data <- processQWData(retval,pCodeLogic) + } else { + data <- NULL + } + + compressedData <- compressData(data, interactive=interactive) + Sample <- populateSampleColumns(compressedData) } else { - data <- NULL + Sample <- data.frame(Date=as.Date(character()), + ConcLow=numeric(), + ConcHigh=numeric(), + Uncen=numeric(), + ConcAve=numeric(), + Julian=numeric(), + Month=numeric(), + Day=numeric(), + DecYear=numeric(), + MonthSeq=numeric(), + SinDY=numeric(), + CosDY=numeric(), + stringsAsFactors=FALSE) } - - compressedData <- compressData(data, interactive=interactive) - Sample <- populateSampleColumns(compressedData) return(Sample) }