R/moment.R

#' Reads the .csv (time series) or .mdb (results) structured data output generated by the stormwater 
#' quality model 'MOMENT' and creates a list of xts-objects or a data.frames, respectively.
#'
#' @title Read data from MOMENT
#' @param file The file to be read
#' @param verbose logical. Should informative outputs printed during function evaluation?
#' @return A list of xts-objects or data.frames.
#' @rdname read_moment
#' @export 
#' @seealso \code{\link{xts}}.
read_moment  <- function(file, verbose=FALSE) {
  
  if (verbose) print(file) 
  
  # determine file type 
  file_type <- switch(tools::file_ext(file), 
                      "csv" = "csv",
                      "mdb" = "mdb")
  
  if (file_type == "mdb") {
    
    # check if namespace of RODBC is available
    if (!requireNamespace("RODBC", quietly = TRUE)) {
      stop("RODBC needed for this function to work. Please install it.",
           call. = FALSE)
    }
    
    # set channel 
    channel <- RODBC::odbcDriverConnect(paste0("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=", file))
    
    # get all table names
    tbl_names <- RODBC::sqlTables(channel, tableType = "TABLE")$TABLE_NAME
    
    # fetch all data from tbl
    list_of_df <- lapply(tbl_names, function(x) RODBC::sqlFetch(channel = channel, sqtable = x))
    
    # set names
    names(list_of_df) <- tbl_names
    
    RODBC::odbcClose(channel)
    
    return(list_of_df)
    
  } else if (file_type == "csv") {
    
    mom <- utils::read.table(file,header = T,sep = ";", dec = ".")
    
    timedata <- as.POSIXct(as.character(mom[,1]),
                           format = "%d.%m.%Y %H:%M",
                           tz = "GMT")
    coredata <- data.matrix(mom[,2:ncol(mom)])
    parameter <- colnames(coredata)
    
    xts.list <- lapply(seq(ncol(coredata)),
                       FUN = function(x) {xts::xts(order.by = timedata, 
                                                   x = coredata[,x],
                                                   # set Ident Attributes
                                                   Parameter = as.character(parameter[x]),
                                                   Ort = "",
                                                   Subort = "",
                                                   Defart = "K",
                                                   Aussage = "",
                                                   XDistanz = "E",
                                                   XFaktor = "",
                                                   Herkunft = "S",
                                                   Reihenart = "Z",
                                                   Version = "0",
                                                   Quelle = "P",
                                                   # set Descr Atttributes 
                                                   X = "",
                                                   Y = "",
                                                   Hoehe = "",
                                                   Messgenau = "",
                                                   FToleranz = "",
                                                   NWGrenze = "",
                                                   Einheit = "",
                                                   Kommentar = paste0(Sys.time(), " MOMENT Import"))}
                       )
    
    
    names(xts.list) <- parameter
    
    invisible(xts.list)
    
  }
  
}


#' Reads the .reg structured rainfall time series data output usually used by 
#' the stormwater quality model 'MOMENT' and creates a xts-object.
#'
#' @title Read rainfall time series data from .reg file format
#' @param file The file to be read.
#' @param skip Number of initial lines to skip; see \code{\link[utils]{read.table}}.
#' @param format Character string giving a date-time format as used by strptime.
#' @param verbose logical. Provide additional details?
#' @return A list of xts-objects.
#' @note [1/1000 mm in 5 min]
#' @rdname read_reg
#' @export 
#' @seealso \code{\link[xts]{xts}}.
read_reg <- function(file, 
                     skip, 
                     format = c("%d.%m.%Y %H","%d %m %Y %H"), 
                     verbose=FALSE) {
  
  #file <- "testdata/moment/N7.reg"
  data <- read.fwf(file,widths = c(5, 10, 5, rep(5, 12)), skip = skip, 
                   stringsAsFactors = FALSE, strip.white = TRUE)
  
  ## times
  timedata <- as.POSIXct(paste(as.character(data$V2),as.character(data$V3)),
                         format = format,
                         tz = "GMT",
                         origin = "1970-01-01")
  
  times <- as.POSIXct(sapply(timedata,  
                             function(x) seq(x, x + 11*5*60, by = 5*60)),
                      origin = "1970-01-01",
                      tz = "GMT")
  
  ## data
  m <- c(t(as.matrix(data[,4:15])))
  m <- m/1000
  
  ## xts
  ts <- xts::xts(x = m, order.by = times)
  
  invisible(ts)
}
dleutnant/tsconvert documentation built on May 15, 2019, 9:17 a.m.