R/Z_batch_awk_tRackIT.R

Defines functions batch.awk.tRackIT

Documented in batch.awk.tRackIT

#' create batch file to prefilter tagged individuals using awk, Installation following the instruction (http://gnuwin32.sourceforge.net/packages/gawk.htm) necessary. Execution of multiple.bat files possible
#'
#' @description Creates .bat files for prefiltering with awk (only for data recorded with tRackIT.OS)
#'
#'
#' @author Jannis Gottwald
#'
#' @param animal list, generated by initAnimal function
#' @param projList list, generatet by initProject function
#' @param freq_er_l num, accepted frequency range minus tag frequency (khz)
#' @param freq_er_r num, accepted frequency  range plus tag frequency (khz)
#' @param min_dur num, minimum duration of signal
#' @param max_dur num, maximum duration of signal
#' @param in_project logical, if true the awk command is created from the data in the project folder
#' @param path_to_folder string, awk command is created from the data in the indicated folder
#' @param OS string, operating system ("Windows", "MacOS", "Linux")
#'
#' @export
#'
#'


batch.awk.tRackIT <- function(animal, projList, freq_er_l, freq_er_r, min_dur, max_dur, in_project, path_to_folder, OS) {
  if (OS == "Windows") {
    file.create(paste0(projList$path$awk, "/", animal$meta$animalID, ".bat"))

    filePath <- paste0(projList$path$awk, "/", animal$meta$animalID, ".bat")
  }

  if (OS == "MacOS") {
    file.create(paste0(projList$path$awk, "/", animal$meta$animalID, ".command"))

    filePath <- paste0(projList$path$awk, "/", animal$meta$animalID, ".command")
  }

  if (OS == "Linux") {
    file.create(paste0(projList$path$awk, "/", animal$meta$animalID, ".sh"))

    filePath <- paste0(projList$path$awk, "/", animal$meta$animalID, ".sh")
  }
  #
  #
  #

  cat_lst <- list.files(projList$path$catalogues, pattern = "data_catalogue", full.names = T)
  df_data <- plyr::ldply(cat_lst, function(x) {
    read.csv(x)
  })
  df_data$start <- as.POSIXct(df_data$start)
  df_data$end <- as.POSIXct(df_data$end)

  df <- df_data[df_data$end >= as.character(animal$meta$start), ]
  df <- df[df$start <= as.POSIXct(animal$meta$end), ]

  if (in_project) {
    an_fls <- paste0(animal$path$raw, df$file)
  }
  if (!in_project) {
    an_fls <- paste0(path_to_folder, df$file)
  }
  print(head(df))
  print(nrow(df))
  print(animal$meta$animalID)
  print(animal$meta$start)
  print(animal$meta$end)


  date_list <- seq(as.Date(animal$meta$start), as.Date(animal$meta$end), by = "days")
  date_list <- paste0("/", paste(date_list, collapse = "|"), "/")


  awk_lst <- lapply(seq_len(an_fls), function(x) {
    station_file <- an_fls[x]
    print(station_file)
    # filename of the output
    result_file <- paste0(animal$path$filtered_awk, "/", basename(station_file))

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

    # date slice
    awk_date <- paste0("(substr($2,1,10) ~ ", date_list, ")")
    # keep the keep alive signal
    awk_kpvl <- "($5 < -100)"
    # frequency +- 10
    awk_freq <- paste0("($3 >= ", as.numeric(animal$meta$freq) - freq_er_l, " && $3 <= ", as.numeric(animal$meta$freq) + freq_er_r, ")")


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

    return(awk_command)
  })

  awk_lst <- unlist(awk_lst)

  fileConn <- file(filePath)
  write(awk_lst, fileConn)
  close(fileConn)
}
Nature40/tRackIT documentation built on Nov. 21, 2023, 3:43 a.m.