Skip to content

Commit

Permalink
Merge pull request #198 from DOI-USGS/contribute
Browse files Browse the repository at this point in the history
Fix up Check
  • Loading branch information
dblodgett-usgs authored Sep 22, 2023
2 parents c178df5 + e177ba5 commit fa0d9c0
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 51 deletions.
6 changes: 5 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ test_stats_huc12_modeled_performance.R
appveyor.yml
EflowStats.bbl
^appveyor\.yml$
CONDUCT.md
CONDUCT.md
code.json
DISCLAIMER.md
LICENSE.md
README.Rmd
8 changes: 4 additions & 4 deletions R/calc_durationLow.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ calc_durationLow <- function(x,yearType = "water",wyMonth=10L,digits=3,pref="mea
meanFlow = mean(discharge),
totalFlow = sum(discharge))
minRollingMean <- dplyr::summarize(dplyr::group_by(x,year_val),
minRoll3Mean = min(roll3Mean),
minRoll7Mean = min(roll7Mean),
minRoll30Mean = min(roll30Mean),
minRoll90Mean = min(roll90Mean)
minRoll3Mean = min(roll3Mean,na.rm=TRUE),
minRoll7Mean = min(roll7Mean,na.rm=TRUE),
minRoll30Mean = min(roll30Mean,na.rm=TRUE),
minRoll90Mean = min(roll90Mean,na.rm=TRUE)
)
medFlow <- median(x$discharge)

Expand Down
17 changes: 9 additions & 8 deletions R/calc_timingAverage.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ calc_timingAverage <- function(x,yearType = "water",wyMonth=10L,digits=3,pref="m

#calculate some stuff for use later
x$month_val <- lubridate::month(x$date)
x$dayom_val <- lubridate::mday(x$date)
meanFlow <- mean(x$discharge)

###Remove leap year data to calculate Colwell matrix
#Remove feb29th for Colwell matrix
x <- x[!(x$month_val == 2 &
yday(x$date) == 29),]
# remove all February 29th from the time series to calculate
# Colwell matrix
x <- x[!((x$month_val == 2) & (x$dayom_val == 29)),]

# #Get a new julian day value that does not count leap years
# after removing the 29th of Feb, we can count the rows sequentially
# for each unique water year; https://stackoverflow.com/a/46613159
x$day <- sequence(rle(x$year_val)$lengths)

#Get a new julian day value that does not count leap years
x$day[is.leapyear(x$year_val) &
x$day > 152] <- x$day[is.leapyear(x$year_val) &
x$day > 152] - 1
#Calculate the colwell matrix...
log_meanFlow = log10(meanFlow)
x$log_discharge = log10(x$discharge)
Expand Down
9 changes: 6 additions & 3 deletions R/cut_dataToWaterYear.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#' Cuts the discharge time series to full water years
#' @param x data.frame containing a vector of date values in the first
#' column and vector of numeric flow values in the second column.
#' @param yearType A character of either "water" or "calendar" indicating
#' whether to use water years or calendar years, respectively.
#' @param wyMonth A numeric. The month of the year in which the water year starts
#' (1=January, 12=December). The water year begins on the first day of wyMonth.
#' @return data.frame in original structure, but cut to full water years
Expand All @@ -9,14 +11,15 @@
#' #' \enumerate{
#' \item First column must be of class `Date`.
#' \item Second must be of class `numeric`.
#' \item `yearType` input must be either "water" or "calendar".
#' \item `wyMonth`input must be of class `integer`.
#' }
#' @examples
#' x <- sampleData[, c('date', 'discharge')]
#' cut_dataToWaterYear(x,10L)
#' @export
#'
cut_dataToWaterYear <- function(x,wyMonth=10L) {
cut_dataToWaterYear <- function(x, yearType, wyMonth=10L) {
###rename dataframe for convenient use inside function
old_names <- colnames(x)
names(x) <- c("date","discharge")
Expand All @@ -34,8 +37,8 @@ cut_dataToWaterYear <- function(x,wyMonth=10L) {
ndays_last_year <- nrow(x[x$year_val == last_year,])

# get the target number of days (depends if water year is in a leap year or not)
ndays_first_year_target <- ifelse(lubridate::leap_year(first_year), 366, 365)
ndays_last_year_target <- ifelse(lubridate::leap_year(last_year), 366, 365)
ndays_first_year_target <- ifelse(is.leapyear(first_year, yearType, wyMonth), 366, 365)
ndays_last_year_target <- ifelse(is.leapyear(last_year, yearType, wyMonth), 366, 365)

# remove the first and last year if number of days is less than target number
if(ndays_first_year < ndays_first_year_target){
Expand Down
52 changes: 37 additions & 15 deletions R/get_waterYear.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,41 @@
#' get_waterYear(sampleData$date)
#'
get_waterYear <- function(x, wyMonth=10L, numeric=TRUE) {
## Coding history:
## 2005Jul14 DLLorenz Initial dated verion
## 2010Feb17 DLLorenz Added option to return numerics
## 2011Jun07 DLLorenz Conversion to R
## 2012Aug11 DLLorenz Integer fixes
## 2013Feb15 DLLorenz Prep for gitHub
## 2016Aug12 Joe Mills copied this from smwrBase
x <- as.POSIXlt(x)
yr <- x$year + 1900L
mn <- x$mon + 1L
## adjust for water year
yr <- yr + ifelse(mn < wyMonth, 0L, 1L)
if(numeric)
return(yr)
ordered(yr)
## Coding history:
## 2005Jul14 DLLorenz Initial dated verion
## 2010Feb17 DLLorenz Added option to return numerics
## 2011Jun07 DLLorenz Conversion to R
## 2012Aug11 DLLorenz Integer fixes
## 2013Feb15 DLLorenz Prep for gitHub
## 2016Aug12 Joe Mills copied this from smwrBase
## 2023Aug04 Jens Kiesel added different calculation if wyMonth < 7
## (see explanation below)
x <- as.POSIXlt(x)
yr <- x$year + 1900L
mn <- x$mon + 1L # x$mon starts at 0 for Jan!
if (wyMonth < 7){
# it is most intuitive that the water year value should be the calendar
# year with the most dates in the water year. Jan.-Jun. would be assigned
# the previous calendar year as the water year value and Jul.-Dec. would be assigned

# the current calendar year as the water year value.


# If the water year is supposed to start in Jan-Jun (e.g. April):
# yr for Jan-Mar must be the previous calendar year,

# yr for Apr-Dec must be the current calendar year

yr <- yr - ifelse(mn < wyMonth, 1L, 0L)
}else{
# If the water year is supposed to start in Jul-Dec (e.g. September):
# yr for Jan-August must be the current calendar year,

# yr for Sep-Dec must be the next calendar year

yr <- yr + ifelse(mn < wyMonth, 0L, 1L)
}
if(numeric)
return(yr)
ordered(yr)
}
23 changes: 20 additions & 3 deletions R/isleapYear.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#courtesy of R bloggers for is.leapyear
is.leapyear <- function(year){
#http://en.wikipedia.org/wiki/Leap_year
return(((year %% 4 == 0) & (year %% 100 != 0)) | (year %% 400 == 0))
is.leapyear <- function(year, yearType='calendar', wyMonth=10L){
if (yearType=='calendar' || wyMonth<3 || wyMonth>6) {
#http://en.wikipedia.org/wiki/Leap_year
return(((year %% 4 == 0) & (year %% 100 != 0)) | (year %% 400 == 0))
}else if ((yearType=='water') && (wyMonth>=3 && wyMonth <=6)) {
#
# add 1 year if water year is supposed to start in Mar, Apr, May, Jun.
# For these cases, the water year is assigned the previous
# year value but contains the leap day.
#
# Example:
# Time series: 01Apr1999 - 31Mar2000
# wyMonth = 4
# Assigned water year: 1999
# Number of days: 366 (because it contains 29Feb2000)
# -> Function must return true for 1999 (and any other year that
# precedes a leap year)
#
return((((year+1) %% 4 == 0) & ((year+1) %% 100 != 0)) | ((year+1) %% 400 == 0))
}
}
3 changes: 2 additions & 1 deletion R/validate_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ validate_data <- function(x,yearType,wyMonth=10L) {
}

#check for complete years
x$leapYear <- is.leapyear(as.numeric(as.character(x$year_val)))
x$leapYear <- is.leapyear(as.numeric(as.character(x$year_val)),
yearType, wyMonth)

fullYearCheck <- dplyr::summarize(dplyr::group_by(x,year_val),
completeYear =
Expand Down
6 changes: 5 additions & 1 deletion man/cut_dataToWaterYear.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tests/testthat/data/tests_calc_allHIT.rds
Binary file not shown.
Binary file modified tests/testthat/data/tests_calc_allHIT_bug.rds
Binary file not shown.
Binary file modified tests/testthat/data/tests_calc_allHIT_med.rds
Binary file not shown.
Binary file modified tests/testthat/data/tests_calc_durationLow.rds
Binary file not shown.
Binary file modified tests/testthat/data/tests_calc_durationLow_nwis_mean.rds
Binary file not shown.
Binary file modified tests/testthat/data/tests_calc_durationLow_nwis_median.rds
Binary file not shown.
Binary file modified tests/testthat/data/tests_calc_timingAverage_1158Thresh.rds
Binary file not shown.
17 changes: 8 additions & 9 deletions tests/testthat/tests_calc_durationLow.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ test_that("duration low pref mean", {
x<-sampleData[c("date","discharge")]

calc_durationLowTest <- calc_durationLow(x=x,yearType="water",pref = "mean")
calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow.rds")
#calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow.rds")

expect_equal(calc_durationLowTest,calc_durationLowTestCheck)
expect_equal_to_reference(calc_durationLowTest,"data/tests_calc_durationLow.rds")

x <- readRDS("data/sample_nwis_data.rds")
calc_durationLowTest <- calc_durationLow(x=x,yearType="water",pref = "mean")
calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow_nwis_mean.rds")
expect_equal(calc_durationLowTest,calc_durationLowTestCheck)
#calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow_nwis_mean.rds")
expect_equal_to_reference(calc_durationLowTest,"data/tests_calc_durationLow_nwis_mean.rds")

})

Expand All @@ -21,12 +21,11 @@ test_that("duration low pref median", {
x<-sampleData[c("date","discharge")]

calc_durationLowTest <- calc_durationLow(x=x,yearType="water", pref = "median")
calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow.rds")

expect_equal(calc_durationLowTest,calc_durationLowTestCheck)
#calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow.rds")
expect_equal_to_reference(calc_durationLowTest,"data/tests_calc_durationLow.rds")

x <- readRDS("data/sample_nwis_data.rds")
calc_durationLowTest <- calc_durationLow(x=x,yearType="water", pref = "median")
calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow_nwis_median.rds")
expect_equal(calc_durationLowTest,calc_durationLowTestCheck)
#calc_durationLowTestCheck <- readRDS("data/tests_calc_durationLow_nwis_median.rds")
expect_equal_to_reference(calc_durationLowTest,"data/tests_calc_durationLow_nwis_median.rds")
})
7 changes: 3 additions & 4 deletions tests/testthat/tests_calc_timingAverage.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ test_that("timing average", {
x<-sampleData[c("date","discharge")]

calc_timingAverageOutTest <- calc_timingAverage(x, floodThreshold = 1158)
calc_timingAverageOut <- readRDS("data/tests_calc_timingAverage_1158Thresh.rds")

expect_equal(calc_timingAverageOutTest,calc_timingAverageOut)
#calc_timingAverageOut <- readRDS("data/tests_calc_timingAverage_1158Thresh.rds")
expect_equal_to_reference(calc_timingAverageOutTest,"data/tests_calc_timingAverage_1158Thresh.rds")
})

test_that("timing average works with very low flows", {
Expand All @@ -17,7 +16,7 @@ test_that("timing average works with very low flows", {
x$discharge <- x$discharge/1000
calc_timingAverageOutTest <- calc_timingAverage(x, floodThreshold = 1)

expect_equal(calc_timingAverageOutTest$statistic,c(0.306,80.386, 0.5))
expect_equal(calc_timingAverageOutTest$statistic,c(0.305,80.359, 0.5))

})

4 changes: 2 additions & 2 deletions tests/testthat/tests_cut_dataToWaterYear.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("cut water year", {
x <- sampleData[, c('date', 'discharge')]

cut <- cut_dataToWaterYear(x, 8)
cut <- cut_dataToWaterYear(x, 'water', 8)

expect_equal(cut$date[1], structure(15187, class = "Date"))

Expand All @@ -13,7 +13,7 @@ test_that("cut water year", {

expect_equal(nrow(cut), 366)

cut <- cut_dataToWaterYear(x, 11)
cut <- cut_dataToWaterYear(x, 'water', 11)

expect_equal(cut$date[1], structure(14914, class = "Date"))

Expand Down

0 comments on commit fa0d9c0

Please sign in to comment.