R/batch_awk.R

Defines functions batch.awk

Documented in batch.awk

#' create batch file to prefilter tagged individuals using awk, Installation following the instruction (http://gnuwin32.sourceforge.net/packages/gawk.htm) necessary. Executing multiple .bat files possible 
#'
#' @description Creates .bat files for prefiltering with awk
#'
#'
#' @author Marvin Ludwig, Nicolas Friess & 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 
#'
#'
#' @export
#'
#'



batch.awk<-function(animal, projList, freq_er_l, freq_er_r, min_dur, max_dur, in_project, path_to_folder){

file.create(paste0(projList$path$awk, "/",animal$meta$animalID, ".bat"))

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

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(1:length(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($1,1,10) ~ ", date_list,")")
  # keep the keep alive signal
  awk_kpvl = "($6 < 20)"
  # frequency +- 10
  awk_freq = paste0("($4 >= ", animal$meta$freq-freq_er_l, " && $4 <= ", animal$meta$freq+freq_er_r, ")")
  awk_dur = paste0("($3 >= ", min_dur, " && $3 <= ", max_dur, ")")
  # noise threshold 60
  #awk_noise = "(sqrt($7**2) >= 60)"
  
  # combine frequency and noise with AND
  awk_freq_dur = paste0("(", awk_freq, " && ", awk_dur, ")")
  
  
  
  # putting everything together
  awk_command = paste0('awk -F, "', awk_date , " && (", awk_kpvl, " || ", awk_freq_dur, ') {print}" ',
                       station_file, " > ", result_file)
  
  return(awk_command)})

awk_lst<-unlist(awk_lst) 

fileConn<-file(filePath)
write(awk_lst, fileConn)
close(fileConn)

}
JannisGottwald/N40RTS documentation built on Feb. 1, 2021, 5:13 p.m.