R/asc.R

#' Reads the .asc structured time series data output generated by 'W.A.S' and 
#' creates an xts-object.
#'
#' @title Read time series data from .asc file format
#' @param file The file to be read
#' @param verbose logical. Provide additional details?
#' @return An xts-object.
#' @rdname read_asc
#' @export 
#' @seealso \code{\link[xts]{xts}}.
read_asc <- function(file, verbose=FALSE) {
  
  xts.attributes <- xts::xts()
  conn <- file(file, open = "r")
  lines <- readLines(conn)
  close(con = conn)
  
  was.attribute <- c("Serienummer/Software-Vers.",
                        "Messdatendatei",
                        "Auswertung mit",
                        "Auswertung vom",
                        "Kommentar Messstelle",
                        "Kommentar Kanal",
                        "Anzahl Messwerte",
                        "Messbeginn",
                        "Messende",
                        "Einheit",
                        "Kanalnummer",
                        "Abtastfrequenz",
                        "a-Wert",
                        "b-Wert")
  
  for (was.attribut in was.attribute) {
    attr(xts.attributes, was.attribut) <- stringr::str_sub(grep(was.attribut,
                                                                x = lines, 
                                                                value = T),
                                                           start = 30)
  }
  
  start.line <- grep("Datum/Uhrzeit", x = lines) + 2
  end.line  <- length(lines) - 5
  
  raw_timestamp <- substr(lines[start.line:end.line],1,20)
  
  timedata <- as.POSIXct(raw_timestamp,
                         format = "%d.%m.%Y %H:%M:%S",
                         tz = "GMT",
                         origin = "1970-01-01")
  
  # check if index conversion succeeded
  .check_index_conversion(index = timedata,
                          raw_timestamp = raw_timestamp, 
                          verbose = verbose)
  
  data <- substr(lines[start.line:end.line],21,40)
  data <- as.numeric(sub(",", ".", data, fixed = TRUE))
  
  xts <- xts::xts(x = data,order.by = timedata)
  
  xts::xtsAttributes(xts)  <- xts::xtsAttributes(xts.attributes)
  
  invisible(xts)
  
}

#' Reads the .asc structured time series data output generated by 'AquaZIS' and 
#' creates an xts-object.
#'
#' @title Read time series data from .asc file format
#' @param file The file to be read.
#' @param attributes How many attibutes are provided?
#' @param na.strings a character vector of strings which are to be interpreted
#' as NA values. Blank fields are also considered to be missing values in logical, 
#' integer, numeric and complex fields.
#' @param header a logical value indicating whether the file contains the names
#' of the variables as its first line. If missing, the value is determined from
#' the file format: header is set to TRUE if and only if the first row contains 
#' one fewer field than the number of columns.
#' @param sep the field separator character. Values on each line of the file 
#' are separated by this character.  If sep = "" (the default for read.table) 
#' the separator is 'white space', that is one or more spaces, tabs, newlines 
#' or carriage returns.
#' @param dec the character used in the file for decimal points.
#' @param verbose logical. Provide additional details?
#' @param ... Further arguments to be passed to read.table.
#' @return An xts-object.
#' @rdname read_asc2
#' @export 
#' @seealso \code{\link[xts]{xts}}.
read_asc2 <- function(file, attributes=16, na.strings="RWLuecke", header = F, 
                      sep = "", dec = ".", verbose=FALSE, ...) {
  
  xts.attributes <- xts::xts()
  
  conn <- file(file,open = "r")
  lines <- readLines(conn)
  close(con = conn)
  
  start.line <- grep("BEGIN", x = lines) + 1
  end.line  <- attributes + 1

  list.of.attributes <- stringr::str_split(lines[start.line:end.line], 
                                           pattern = ": ")
  
  for (attribute in list.of.attributes) {
    attr(xts.attributes, attribute[1]) <- attribute[2]
  }
  
  raw.data <- utils::read.table(file, header = header, sep = sep, dec = dec,
                                na.strings = na.strings, skip = attributes + 1, ...)
  
  raw_timestamp <- paste(raw.data$V1, raw.data$V2)
  timedata <- as.POSIXct(raw_timestamp, tz = "GMT", 
                        format = "%d.%m.%Y %H:%M:%S", origin = "1970-01-01")
  
  # check if index conversion succeeded
  .check_index_conversion(index = timedata,
                          raw_timestamp = raw_timestamp, 
                          verbose = verbose)
  
  data <- as.numeric(raw.data$V3)
  
  xts <- xts::xts(x = data, order.by = timedata)
  
  xts::xtsAttributes(xts)  <- xts::xtsAttributes(xts.attributes)
  invisible(xts)
  
}
dleutnant/tsconvert documentation built on May 15, 2019, 9:17 a.m.