R/station_subset.R

Defines functions station_subset

Documented in station_subset

#' Subset radiotracking stations datasets
#' @description Extracts data between two dates, the keep alive and a frequency
#'
#' @author Marvin Ludwig
#'
#' @param animal list, a list from initAnimal
#' @param station_file string, filepath to a raw station file
#'
#' @details This function usually is first step in (process animal). Makes use of awk so might only work for linux atm.
#'
#'
#'
#' @export
#'
#'
#'
#'


station_subset = function(animal, station_file){

  # get a list of days between start and end for the regex
  date_list = seq(as.Date(animal$meta$start), as.Date(animal$meta$end), by="days")
  date_list = paste0("/", paste(date_list, collapse = "|"), "/")

  # filename of the output
  result_file = paste0(animal$path$stations, "/", basename(station_file))

  #---- build awk command ----

  # date slice
  awk_date = paste0("(substr($1,1,10) ~ ", date_list,")")
  # keep the keep alive signal
  awk_kpvl = "($6 < 20)"
  # frequency +- 10
  awk_freq = paste0("($4 > ", animal$meta$freq-10, " && $4 < ", animal$meta$freq+10, ")")
  # noise threshold 60
  awk_noise = "(sqrt($7**2) >= 60)"

  # combine frequency and noise with AND
  awk_freq_noise = paste0("(", awk_freq, " && ", awk_noise, ")")


  # putting everything together
  awk_command = paste0("awk -F, '", awk_date , " && (", awk_kpvl, " || ", awk_freq_noise, ") {print}' ",
                       station_file, " > ", result_file)
  awk_command

  system(awk_command)

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