#' Reads the .csv structured time series data output generated by the stormwater
#' management model 'SWMM' and creates an xts-object. For reading the binary output
#' please check the swmmr-package (https://github.com/dleutnant/swmmr).
#'
#' @title Read time series data from SWMM
#' @param file The file to be read
#' @param header s. read.table
#' @param sep s. read.table
#' @param dec s. read.table
#' @param skip s. read.table
#' @param stringsAsFactors s. read.table
#' @param fileEncoding s. read.table
#' @param format s. read.table
#' @param tz Sets the time zone.
#' @param verbose logical. Provide additional details?
#' @return An xts-object.
#' @rdname read_swmm_dat
#' @export
#' @seealso \code{\link[xts]{xts}}.
read_swmm_dat <- function(file,
header = FALSE,
sep = "",
dec = ".",
skip = 1,
stringsAsFactors = F,
fileEncoding = "UTF-8",
format = "%m/%d/%Y %H:%M",
tz = "GMT",
verbose = FALSE) {
if (verbose) print(file)
# get attributes of first lines
## TODO
data <- utils::read.table(file = file,
header = header,
sep = sep,
dec = dec,
skip = skip,
stringsAsFactors = stringsAsFactors,
fileEncoding = fileEncoding)
coredata <- as.numeric(data$V3)
timedata <- paste(data$V1, data$V2)
timedata <- as.POSIXct(timedata,
format = format,
tz = tz)
swmmdata <- xts::xts(coredata,timedata)
invisible(swmmdata)
}
#' Writes a xts-object to .dat structured time series data format usually
#' interpreted by the stormwater management model 'SWMM'.
#'
#' @title Write xts-objects to .dat file format
#' @param xts The xts object to be written.
#' @param file A character string naming the file to write to.
#' @param is_rainfall logical. TRUE shifts coredata values with specified
#' seconds (default is -60)
#' @param seconds The seconds to shift to.
#' @param is_caldata logical. Modifies the header if .dat output.
#' @param digits Number of digits to write.
#' @rdname write_swmm_dat
#' @export
#' @seealso \code{\link{xts}}, \code{\link{write.table}}.
write_swmm_dat <- function(xts, file,
is_rainfall = FALSE,
seconds = -60,
is_caldata = FALSE,
digits = 6) {
mname <- attr(xts,which = "ZRID")
if (identical(mname, character(0))) mname <- "NoName"
mpara <- attr(xts,which = "Parameter")
if (identical(mpara, character(0))) mpara <- "NoParameter"
munit <- attr(xts,which = "Einheit")
if (identical(munit, character(0))) munit <- "NoUnit"
xts <- xts[zoo::coredata(xts) != 0]
# respect blockanfang
if (is_rainfall) {
xts <- .shift.index(xts, seconds = seconds)
}
if (is_caldata) {
colstring <- c("index", "data")
} else {
colstring <- c(";",paste("Name:",mname,";Messgroesse:", mpara, ";Einheit:",munit) )
}
utils::write.table(data.frame(format.POSIXct(zoo::index(xts),
format = "%m/%d/%Y %H:%M"),
round(x = zoo::coredata(xts), digits = digits)),
file = file,
sep = " ",
quote = FALSE,
row.names = FALSE,
col.names = colstring)
}
# write.SWMM.calData <- function(xts, file){
#
# mname <- attr(xts,which="ZRID")
# if (identical(mname, character(0))) mname <- "NoName"
# mpara <- attr(xts,which="Parameter")
# if (identical(mpara, character(0))) mpara <- "NoParameter"
# munit <- attr(xts,which="Einheit")
# if (identical(munit, character(0))) munit <- "NoUnit"
#
# xts <- xts[coredata(xts)!=0]
#
# write.table(data.frame(format.POSIXct(index(xts),format="%m/%d/%y %H:%M"),
# coredata(xts)),
# file=file,
# sep=",",
# quote=FALSE,
# row.names=FALSE,
# col.names=c("index", "data") )
# }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.