R/unzipTracking.R

Defines functions unzipTracking

Documented in unzipTracking

#' Unzip and organize RAW tracking data
#'
#' @description Unzips a radiotracking log_file and creates directory structure for triangulation
#'
#' @param inZip path to the zip file
#' @param outPath output path for the unziped files
#'
#' @return NULL
#'
#' @author Marvin Ludwig
#'
#' @import filesstrings
#'
#' @description Known bug: Currently the file path cannot contain "_"
#'
#' @export
#'


unzipTracking <- function(inZip, outPath){


  utils::unzip(inZip, exdir = outPath)

  # create folder structure based on files in the zip

  # level 1

  l1 <- tail(unlist(strsplit(inZip, "/")),1)
  l1 <- unlist(strsplit(l1, "_"))[1:3]
  l1 <- paste(l1, collapse = "_")
  l1 <- paste0(outPath, "/" ,l1)

  dir.create(l1, showWarnings = FALSE)

  # level 2
  l2 <- list.files(paste0(outPath, "tmp/record"))
  l2 <- lapply(strsplit(l2, "_"), "[", 1:6)


  l2 <- lapply(l2, function(s){

    # add leading zeros just in case
    s[6] <- sprintf(fmt = "%02d", as.numeric(s[6]))

    paste(s, collapse = "_")

  })
  l2 <- do.call(c, l2)

  # create the four directories
  l2dirs <- paste0(l1, "/", unique(l2))
  sapply(l2dirs, dir.create)

  # move files from records to corresponding directory
  fls <- list.files(paste0(outPath, "/tmp/record"), full.names = TRUE)
  filesstrings::move_files(fls, destinations = paste0(l1, "/", l2), overwrite = FALSE)

  #  clean up

  if(length(list.files(paste0(outPath, "/tmp/record"))) == 0){
    filesstrings::dir.remove(paste0(outPath, "/tmp"))
  }
  else{
    print("Something went wrong: There are still files left!")
  }


}
Nature40/UM2Radiotracking documentation built on Aug. 14, 2019, 8:05 a.m.