R/station_functions.R

Defines functions station_read station_keepalive station_cleanup station_match

Documented in station_cleanup station_keepalive station_match station_read

#' Station read
#' @description Various function for processing one radiotracking station file
#'
#' @author Jannis Gottwald
#' @export


station_read <- function(path){
  data = data.table::fread(path)
  # set colnames
  colnames(data)<-c("timestamp","samples","duration","signal_freq","signal_bw","max_signal","noise", "V8", "receiver", "station")
  #in posixct
  data$timestamp <- fasttime::fastPOSIXct(data$timestamp)
  return(data)
}



#' Station Keepalive
#' @author Jannis Gottwald
#' @export
#' @import dplyr
#' @import data.table
#'



station_keepalive <- function(station){

  station = as.data.frame(station)
  #filter keepalives and sort by time
  kplv <- station[station$max_signal < 20,]
  kplv <- kplv[order(kplv$timestamp),]

  #keepalives ID by consecutive unique receivers
  kplv <- kplv %>% dplyr::mutate(kplv = cumsum(c(0, diff(lengths(Reduce(function(x, y)
    if (max(tabulate(c(x,y)) > 1))
      y
    else
      c(x,y), as.integer(as.factor(receiver)), accumulate = TRUE)))) <= 0))

  #number of receivers per keep allive- if less than 4 one receiver missing
  kplv <- kplv %>% dplyr::group_by(kplv) %>% dplyr::add_tally()

  #save keepallives
  kplv <- as.data.frame(kplv[, c("timestamp","receiver","station","kplv", "n")])
  kplv<-kplv[order(kplv$timestamp),]
  return(kplv)
}



#' Station frequency filter
#' @author Jannis Gottwald
#' @export




station_cleanup = function(station, freq){

      # throw out keep alives
      data_freq = dplyr::filter(station, .data$signal_freq > (freq-10) & .data$signal_freq < (freq+10))

      # get central frequency

      data_freq_mid = dplyr::filter(data_freq, .data$max_signal >= 65 & .data$max_signal <= 70)

      data_freq_mid <- data_freq[data_freq$max_signal>=65 & data_freq$max_signal<=70,]
      mid_freq <- mean(data_freq_mid$signal_freq)
      sd_freq <- 4*sd(data_freq_mid$signal_freq)

      #identify frequency range
      print(paste0("Warning! SD=", sd_freq," Please check signal distribution of ", data_freq_mid$station[1], " visually"))

      #filter data below 70 Khz by frequency range
      data_freq_sm <- dplyr::filter(data_freq, .data$max_signal <= 70)
      data_freq_sm <- dplyr::filter(data_freq_sm, !(.data$signal_freq < (mid_freq - sd_freq) | .data$signal_freq > (mid_freq + sd_freq)))


      # Data above 70 Khz isclean in most cases
      data_freq_c<-rbind(data_freq_sm,data_freq[data_freq$max_signal>70,])
      data_freq_c<-data_freq_c[order(data_freq_c$timestamp),]
      return(data_freq_c)


}


#' Match keep alive
#' @author Jannis Gottwald
#' @export
#'

station_match = function(station, kplv){

  # init new column
  data_freq_c$kplv<-NA

  print(paste0("Start the loop at ", Sys.time()))
  # matching
  for(j in 1:(max(kplv$kplv)-1)){
    start <- max(kplv$timestamp[kplv$kplv==j])
    stop <- max(kplv$timestamp[kplv$kplv==j+1])
    data_freq_c$kplv[data_freq_c$timestamp>=start & data_freq_c$timestamp<=stop]<-j
  }
  print(paste0("End the loop at ", Sys.time()))

  kplv<-kplv[, c("kplv", "n")]
  data_freq_c <- dplyr::left_join(data_freq_c, kplv, by="kplv")
  data_freq_c<-data_freq_c[!duplicated(data_freq_c[,c("timestamp", "receiver")]),]
  return(data_freq_c)

}
Nature40/Rrt documentation built on March 31, 2020, 12:45 a.m.