diff --git a/R/prepFiles.R b/R/prepFiles.R index c9abe17..9d9e075 100644 --- a/R/prepFiles.R +++ b/R/prepFiles.R @@ -7,16 +7,49 @@ #' prepped_detections <- prepDetections("path-to-raw-data-file", type="vemco_vue") #' } prepDetections <- function(raw_dat, type){ - detections <- data.table::data.table() + + # gsub (and the previous version using strsplit) convert to character before + # doing their thing. The default number of millisecond digits for R to + # display is 0, so millisecond information is dropped when converted to + # character. Temporarily change this to keep milliseconds, then used on.exit + # to change the option back to what it was before when the function finishes + op <- options(digits.secs = 3) + on.exit(options(op), add = T) + + detections <- data.table::copy(raw_dat) + if (type == "vemco_vue"){ - detections[, ts := as.POSIXct(raw_dat$'Date and Time (UTC)', tz = "UTC")] - detections[, tag := as.numeric(gsub('.*-', '', raw_dat$Transmitter))] - detections[, epo := as.numeric(ts)] - detections[, frac := as.numeric(gsub('.*\\.', '', - raw_dat$"Date and Time (UTC)")) / 1000] - detections[, serial := as.numeric(gsub('.*-', '', raw_dat$Receiver))] + + # Only parse datetime if needed + ## trunc drops milliseconds but converts to POSIXlt in the process, so it needs + ## to be converted back to POSIXct for use elsewhere. + + if(!inherits(detections$`Date and Time (UTC)`, 'POSIXt')){ + + detections[, ts := + as.POSIXct( + trunc( + as.POSIXct(`Date and Time (UTC)`, tz = "UTC") + ) + )] + + } else{ + + detections[, ts := + as.POSIXct( + trunc(`Date and Time (UTC)`) + )] + + } + + detections[, ':='(tag = as.numeric(gsub('.*-', '', Transmitter)), + epo = as.numeric(ts), + frac = as.numeric(gsub('.*\\.', '', + `Date and Time (UTC)`)) / 1000, + serial = as.numeric(gsub('.*-', '', Receiver)))] } - detections[] - return(detections) + + detections[, .(ts, tag, epo, frac, serial)] + }