R/loggerIOhelper.R

Defines functions get_logger_files read_logger_data findHeader findMidFreq safe_read_excel countCharOccurrences

Documented in get_logger_files

#' Radiotracking Input helper function
#'
#' @description Hidden function for read_logger_folder
#' @import data.table stringr fasttime
#' @author RadioTracking EU
#'
#'
#'


get_logger_files <- function() {
  path<-file.path("data","logger")
  list_of_stations<-list.dirs(path,full.names = FALSE, recursive =FALSE)
  tmp_data<-NULL

  files <- c()

  for(i in list_of_stations){
    list_of_receivers<-list.dirs(file.path(path,i), full.names = FALSE, recursive = FALSE)
    for (j in list_of_receivers) {
      list_of_records <- list.files(file.path(path,i,j), no..=T)
      for (k in list_of_records) {
        p <- file.path(path,i,j,k)
        files <- c(files, p)
      }
    }
  }

  files
}

read_logger_data <- function(filepath){
  lines_to_skip <- findHeader(filepath) #skip meta data in files
  if (lines_to_skip < 0) return(NULL)

  mid_freq <- findMidFreq(filepath) # find center frequency of tuner
  # print(mid_freq)
  if(mid_freq < 0) return(NULL)
  #data_in_file <- readLines(filepath) #reads two times...
  #last_rows_skip<-0
  #if(grepl("total transforms",data_in_file[length(data_in_file)])){
  #  print("that recording crashed")
  #  last_rows_skip <- length(data_in_file)-3-lines_to_skip
  #  if(last_rows_skip<1) return(NULL)
  #}
  # data <-
  #   read.csv2(
  #     filepath,
  #     skip = lines_to_skip,
  #     stringsAsFactors = FALSE,
  #     dec = ".",
  #     #nrows=last_rows_skip,
  #     fill=TRUE
  #   ) # zu langsam
  data <- data.table::fread(filepath, skip = lines_to_skip, fill = T, data.table=T)
  #
  if(!all(is.na(data[,-1]))){
    suppressWarnings(if(any(apply(data[,-1], 2, class) != "numeric")) {
      data <-  cbind(data[,1],apply(data[,-1],2, as.numeric))
    })
    data$max_signal[is.na(data$max_signal)]<-0
    # data<-data[countCharOccurrences("[:-]",data$timestamp)==4,] # Quatschfunktion
    # data <- data[str_count(data$timestamp,"[:-]")==4,]
    # data$timestamp <- as.POSIXct(data$timestamp, tz = "UTC")
    data$timestamp <- fasttime::fastPOSIXct(data$timestamp,tz = "UTC", 6L)
    data$signal_freq <- (data$signal_freq + mid_freq) / 1000
    data$freq_tag<-NA
    return(data)
  }else{
    return(NULL)
  }
}

findHeader <- function(file) {
  n<--1
  tryCatch(
    {
      tmp <- readLines(file, n = 30, warn=F)
      n <- grep(pattern="timestamp;samples;duration;signal_freq;signal_bw;max_signal", x=tmp, fixed=T) - 1
      if (length(n)==0) n<--1
    }, warning = function(w) {
      print(w$message)
      n<--1
    }, error = function(e) {
      n<--1
      print(e$message)
    }, finally = {
      return(n)
    }
  )
}

findMidFreq <- function(file) {
  MidFreq<--1
  tryCatch({
    tmp <- readLines(file, n = 30, warn=F)
    n <- grep("Tuned to", tmp)
    if(length(n)>0){
      MidFreq <- as.numeric(gsub("[a-z,A-Z,., ]", "", tmp[n]))
    }
  }, warning = function(w) {
    print(w$message)
    MidFreq<--1
  }, error = function(e) {
    print(e$message)
    MidFreq<--1
  }, finally = {
    return(MidFreq)
  }
  )
}

safe_read_excel <- function(filepath) {
  tryCatch({
    read_excel(filepath, sheet = 1)
  }
  )
}


countCharOccurrences <- function(char, s) {
  s2 <- gsub(char,"",s)
  return (nchar(s) - nchar(s2))
}
Nature40/UM2Radiotracking documentation built on Aug. 14, 2019, 8:05 a.m.