Creates a date frame (and file export) from Continuous Data in the format used by the IHA package.
Filename (no directory) of data file. Must be CSV file. Includes Date/Time and Discharge fields.
Format of benchmark date. This should be the same format of the date in the data file. Default is %Y-%m-%d (e.g., 2017-12-31).
Directory for import data. Default is current working directory.
Directory for export data. Default is current working directory.
Start date for IHA analysis. File will be filtered on this date. Default is NA (no filtering).
End date for IHA analysis. File will be filtered on this date. Default is NA (no filtering).
Column name in myFile with Date/Time. Assumes format of %Y-%m-%d %H:%M:%S. Default = "Date.Time".
Column name in myFile for measurements. Default = "Discharge.ft3.s".
Returns a data frame with daily mean values by date (in the specified range). Also, a csv file is saved to the specified directory with the prefix "IHA" and the date range before the file extension.
The function assumes the provided data is sampled more often then daily. The daily means are calculated internally.
The IHA package is not included in the ContDataQC package. But an example is provided.
To run the example IHA calculations you will need the IHA package (from GitHub) and for the example export the XLConnect packages. Install commands are included in the example.
myDir.BASE <- tempdir()
# 1. Get Gage Data
#
# 1.A. Use ContDataQC and Save (~1min for download)
myData.Operation <- "GetGageData" #Selection.Operation[1]
myData.SiteID <- "01187300" # Hubbard River near West Hartland, CT
myData.Type <- "Gage" #Selection.Type[4]
myData.DateRange.Start <- "2013-01-01"
myData.DateRange.End <- "2014-12-31"
myDir.import <- ""
myDir.export <- file.path(myDir.BASE, Selection.SUB[2])
#> Error in file.path(myDir.BASE, Selection.SUB[2]): object 'Selection.SUB' not found
ContDataQC(myData.Operation
, myData.SiteID
, myData.Type
, myData.DateRange.Start
, myData.DateRange.End
, myDir.import
, myDir.export)
#> Error in fun.GageData(fun.myData.SiteID, fun.myData.Type, fun.myData.DateRange.Start, fun.myData.DateRange.End, fun.myDir.export): object 'myDir.export' not found
#
# 1.B. Use saved data
myData.SiteID <- "01187300"
myFile <- "01187300_Gage_20150101_20161231.csv"
myCol.DateTime <- "Date.Time"
myCol.Discharge <- "Discharge.ft3.s"
#
# 2. Prep Data
myData.IHA <- Export.IHA(myFile
, fun.myDir.import = tempdir()
, fun.myDir.export = tempdir()
, fun.myCol.DateTime = myCol.DateTime
, fun.myCol.Parameter = myCol.Discharge
)
#> Warning: cannot open file '01187300_Gage_20150101_20161231.csv': No such file or directory
#> Error in file(file, "rt"): cannot open the connection
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 3. Run IHA
# Example using returned DF with IHA
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# User info
SiteID <- myData.SiteID
Notes.User <- Sys.getenv("USERNAME")
#~~~~~
# Library (install if needed)
# devtools::install_github("jasonelaw/IHA")
# install.packages("XLConnect")
# Library (load)
require(IHA)
#> Loading required package: IHA
#> Loading required package: zoo
#>
#> Attaching package: ‘zoo’
#> The following objects are masked from ‘package:base’:
#>
#> as.Date, as.Date.numeric
require(XLConnect)
#> Loading required package: XLConnect
#> XLConnect 1.0.6 by Mirai Solutions GmbH [aut],
#> Martin Studer [cre],
#> The Apache Software Foundation [ctb, cph] (Apache POI),
#> Graph Builder [ctb, cph] (Curvesapi Java library),
#> Brett Woolridge [ctb, cph] (SparseBitSet Java library)
#> https://mirai-solutions.ch
#> https://github.com/miraisolutions/xlconnect
#~~~~~
# IHA
myYr <- "calendar" # "water" or "calendar"
# IHA Metrics
## IHA parameters group 1; Magnitude of monthly water conditions
Analysis.Group.1 <- group1(myData.IHA, year=myYr)
#> Error in is.zoo(x): object 'myData.IHA' not found
## IHA parameters group 2: Magnitude of monthly water condition and include
# 12 parameters
Analysis.Group.2 <- group2(myData.IHA, year=myYr)
#> Error in is.zoo(x): object 'myData.IHA' not found
Analysis.Group.3 <- group3(myData.IHA, year=myYr)
#> Error in is.zoo(x): object 'myData.IHA' not found
## IHA parameters group 4; Frequency and duration of high and low pulses
# defaults to 25th and 75th percentiles
Analysis.Group.4 <- group4(myData.IHA, year=myYr)
#> Error in is.zoo(x): object 'myData.IHA' not found
## IHA parameters group 5; Rate and frequency of water condition changes
Analysis.Group.5 <- group5(myData.IHA, year=myYr)
#> Error in is.zoo(x): object 'myData.IHA' not found
#~~~~~
# Save Results to Excel (each group on its own worksheet)
Group.Desc <- c("Magnitude of monthly water conditions"
,"Magnitude of monthly water condition and include 12 parameters"
,"Timing of annual extreme water conditions"
,"Frequency and duration of high and low pulses"
,"Rate and frequency of water condition changes")
df.Groups <- as.data.frame(cbind(paste0("Group",1:5),Group.Desc))
#
myDate <- format(Sys.Date(),"%Y%m%d")
myTime <- format(Sys.time(),"%H%M%S")
# Notes section (add min/max dates)
Notes.Names <- c("Dataset (SiteID)","IHA.Year","Analysis.Date (YYYYMMDD)"
,"Analysis.Time (HHMMSS)","Analysis.User")
Notes.Data <- c(SiteID, myYr, myDate, myTime, Notes.User)
df.Notes <- as.data.frame(cbind(Notes.Names,Notes.Data))
Notes.Summary <- summary(myData.IHA)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'myData.IHA' not found
# Open/Create file
myFile.XLSX <- paste("IHA", SiteID, myYr, myDate, myTime, "xlsx", sep=".")
# load workbook, create if not existing
wb <- loadWorkbook(myFile.XLSX, create = TRUE)
# create sheets
createSheet(wb, name = "NOTES")
createSheet(wb, name = "Group1")
createSheet(wb, name = "Group2")
createSheet(wb, name = "Group3")
createSheet(wb, name = "Group4")
createSheet(wb, name = "Group5")
# write to worksheet
writeWorksheet(wb, df.Notes, sheet = "NOTES", startRow=1)
writeWorksheet(wb, Notes.Summary, sheet = "NOTES", startRow=10)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'data' in selecting a method for function 'writeWorksheet': object 'Notes.Summary' not found
writeWorksheet(wb, df.Groups, sheet="NOTES", startRow=25)
writeWorksheet(wb, Analysis.Group.1, sheet = "Group1")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'data' in selecting a method for function 'writeWorksheet': object 'Analysis.Group.1' not found
writeWorksheet(wb, Analysis.Group.2, sheet = "Group2")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'data' in selecting a method for function 'writeWorksheet': object 'Analysis.Group.2' not found
writeWorksheet(wb, Analysis.Group.3, sheet = "Group3")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'data' in selecting a method for function 'writeWorksheet': object 'Analysis.Group.3' not found
writeWorksheet(wb, Analysis.Group.4, sheet = "Group4")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'data' in selecting a method for function 'writeWorksheet': object 'Analysis.Group.4' not found
writeWorksheet(wb, Analysis.Group.5, sheet = "Group5")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'data' in selecting a method for function 'writeWorksheet': object 'Analysis.Group.5' not found
# save workbook
saveWorkbook(wb, myFile.XLSX)