DOI-USGS / EflowStats

Calculates Hydrologic Indicator stats and fundamental properties of daily streamflow for a given set of data
Creative Commons Zero v1.0 Universal
34 stars 24 forks source link

ERROR: Error in get_peakThreshold #191

Closed ThamiFontoura closed 1 year ago

ThamiFontoura commented 1 year ago

Did someone have this error? I used peak flow values extracted with a code in R package and the columns are already with class Date and numeric, but the ERROR is still occurring.

CODE:

pasta.dados = "/Users/thamirisfontoura/Documents/RStudio/Eflowstats" setwd(pasta.dados) Qdf = read.csv("ssp245eflowstats.csv")

column Date as class Date

Qdf["Date"] <- seq(as.Date("2015-01-01"), as.Date("2100-12-31"), by=1) head(Qdf) tail(Qdf)

obtaining peakflow data

----message=FALSE, echo=TRUE, eval=FALSE--------------------------------

library(dataRetrieval) library(EflowStats)

Check data for completeness

dailyQdf <- validate_data(Qdf[c("Date","Numeric")],yearType="calendar")

annualpeakflow <- dailyQdf %>% group_by(year_val) %>% filter(discharge == max(discharge))

delete repeated data

annualpeakflow <- annualpeakflow[-c(6,7,64),] as.data.frame(annualpeakflow)

Get drainage area

drainageArea <- 139.77

annualpeakflow %>% mutate(date=as.Date(date, format = "%Y.%m.%d"))

Get flood recurence threshold

floodThresh <- get_peakThreshold(dailyQdf[c("date","discharge")], annualpeakflow[c("date","discharge")])

Calculate all hit stats

calc_allHITOut <- calc_allHIT(dailyQClean, drainArea=drainageArea, floodThreshold=floodThresh)

Calculate mag7 stats

magnifStatsOut <- calc_magnifSeven(dailyQClean,yearType="water",digits=3)

ERROR:

Error in get_peakThreshold(dailyQdf[c("date", "discharge")], annualpeakflow[c("date", : First column of peakValues must contain a vector of class date. Second column of peakValues must contain a vector of class numeric. In addition: Warning messages: 1: In validate_data(x, yearType) : Every year as defined by the yearType argument must be complete, the following years have missing data: 2015,2101 2: In col1_class != "Date" && col2_class != "numeric" : 'length(x) = 3 > 1' in coercion to 'logical(1)' 3: In col1_class != "Date" && col2_class != "numeric" : 'length(x) = 3 > 1' in coercion to 'logical(1)'

ssp245eflowstats.csv

dblodgett-usgs commented 1 year ago

Hi There -- it looks like you've come across an awkward error message.

Really, the function should just fail with the warning:

Every year as defined by the yearType argument must be complete,
the following years have missing data: 2015,2101

If you change

floodThresh <- get_peakThreshold(dailyQdf[c("date","discharge")],
                                                         annualpeakflow[c("date","discharge")])

to

floodThresh <- get_peakThreshold(dailyQdf[c("date","discharge")],
                                                         annualpeakflow[c("date","discharge")],
                                                         yearType = "calendar")

It will function. If you want a yearType = "wateryear" to work, you'll have to provide complete data for the water year you want to run.

ThamiFontoura commented 1 year ago

Hi There -- it looks like you've come across an awkward error message.

Really, the function should just fail with the warning:

Every year as defined by the yearType argument must be complete,
the following years have missing data: 2015,2101

If you change

floodThresh <- get_peakThreshold(dailyQdf[c("date","discharge")],
                                                         annualpeakflow[c("date","discharge")])

to

floodThresh <- get_peakThreshold(dailyQdf[c("date","discharge")],
                                                         annualpeakflow[c("date","discharge")],
                                                         yearType = "calendar")

It will function. If you want a yearType = "wateryear" to work, you'll have to provide complete data for the water year you want to run.

Hello, It works , in part. Is still occurring the error:

Error in get_peakThreshold(dailyQdf[c("date", "discharge")], annualpeakflow[c("date", : First column of peakValues must contain a vector of class date. Second column of peakValues must contain a vector of class numeric. In addition: Warning messages: 1: In col1_class != "Date" && col2_class != "numeric" : 'length(x) = 3 > 1' in coercion to 'logical(1)' 2: In col1_class != "Date" && col2_class != "numeric" : 'length(x) = 3 > 1' in coercion to 'logical(1)'

dblodgett-usgs commented 1 year ago

Do you mind constructing a reprex like below? I see it working with the reprex pasted further down.

reprex::reprex({
  #code goes here
  }, wd = "work/directory/", session_info = TRUE)
library(EflowStats)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> USGS Research Package: https://owi.usgs.gov/R/packages.html#research
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

f <- "ssp245eflowstats.csv"

Qdf = read.csv(f)
#column Date as class Date
Qdf["Date"] <- seq(as.Date("2015-01-01"), as.Date("2100-12-31"), by=1)

#Check data for completeness
dailyQdf <- validate_data(Qdf[c("Date","Numeric")], yearType="calendar")

annualpeakflow <- dailyQdf %>% group_by(year_val) %>% filter(discharge == max(discharge))

annualpeakflow <- annualpeakflow[-c(6,7,64),]

x <- dailyQdf[c("date","discharge")]
y <- as.data.frame(annualpeakflow[c("date","discharge")])

get_peakThreshold(x, y, yearType = "calendar")
#> [1] 607.41

Created on 2023-02-04 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.2 (2022-10-31 ucrt) #> os Windows 10 x64 (build 19044) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz America/Chicago #> date 2023-02-04 #> pandoc 2.19.2 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.0 2023-01-09 [1] CRAN (R 4.2.2) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.2.2) #> curl 5.0.0 2023-01-12 [1] CRAN (R 4.2.2) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.2) #> dplyr * 1.1.0 2023-01-29 [1] CRAN (R 4.2.2) #> EflowStats * 5.1.0 2022-11-29 [1] local #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.2) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.2) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.2) #> forecast 8.20 2023-01-06 [1] CRAN (R 4.2.2) #> fracdiff 1.5-2 2022-10-31 [1] CRAN (R 4.2.2) #> fs 1.6.0 2023-01-23 [1] CRAN (R 4.2.2) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.2) #> ggplot2 3.4.0 2022-11-04 [1] CRAN (R 4.2.2) #> ggtext 0.1.2 2022-09-16 [1] CRAN (R 4.2.1) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.2) #> gridtext 0.1.5 2022-09-16 [1] CRAN (R 4.2.1) #> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.2) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.2) #> imputeTS 3.3 2022-09-09 [1] CRAN (R 4.2.1) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.2) #> lattice 0.20-45 2021-09-22 [2] CRAN (R 4.2.2) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.1) #> lmom 2.9 2022-05-29 [1] CRAN (R 4.2.0) #> lmtest 0.9-40 2022-03-21 [1] CRAN (R 4.2.1) #> lubridate 1.9.1 2023-01-24 [1] CRAN (R 4.2.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.2) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.2) #> nlme 3.1-160 2022-10-10 [2] CRAN (R 4.2.2) #> nnet 7.3-18 2022-09-28 [2] CRAN (R 4.2.2) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.2) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.2) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.2) #> quadprog 1.5-8 2019-11-20 [1] CRAN (R 4.2.0) #> quantmod 0.4.20 2022-04-29 [1] CRAN (R 4.2.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.2) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.2) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.2) #> Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.2.2) #> RcppRoll 0.3.0 2018-06-05 [1] CRAN (R 4.2.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.1) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.2) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.2) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.2) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.2) #> stinepack 1.4 2018-07-30 [1] CRAN (R 4.2.0) #> styler 1.9.0 2023-01-15 [1] CRAN (R 4.2.2) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.2.2) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.1) #> timechange 0.2.0 2023-01-11 [1] CRAN (R 4.2.2) #> timeDate 4022.108 2023-01-07 [1] CRAN (R 4.2.2) #> tseries 0.10-52 2022-10-10 [1] CRAN (R 4.2.2) #> TTR 0.24.3 2021-12-12 [1] CRAN (R 4.2.1) #> urca 1.3-3 2022-08-29 [1] CRAN (R 4.2.1) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.2) #> vctrs 0.5.2 2023-01-23 [1] CRAN (R 4.2.2) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.2) #> xfun 0.36 2022-12-21 [1] CRAN (R 4.2.2) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.2) #> xts 0.12.2 2022-10-16 [1] CRAN (R 4.2.2) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.2) #> zoo 1.8-11 2022-09-17 [1] CRAN (R 4.2.1) #> #> [1] C:/Users/dblodgett/AppData/Local/R/win-library/4.2 #> [2] C:/Program Files/R/R-4.2.2/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
ThamiFontoura commented 1 year ago

Done! Thank you