Skip to content

Commit

Permalink
Merge pull request #118 from hugomflavio/issue_103
Browse files Browse the repository at this point in the history
Issue 103 update
  • Loading branch information
hugomflavio authored Oct 9, 2024
2 parents d7f157c + e56e9ba commit 92d0904
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
41 changes: 24 additions & 17 deletions R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -688,21 +688,19 @@ splitSignals <- function(x) {
}


#' Convert Lotek CMDA log to csv
#' Convert Lotek CDMA log to csv
#'
#' Lotek CMDA logs are exported in TXT, and contain several chunks of
#' of information. Importantly, the detections are saved in the timezone
#' of the respective computer, as opposed to the more common UTC standard.
#' Additionally, the date format also depends on the locale of the computer,
#' and may therefore be incomprehensible for R without further assistance.
#' Lotek CDMA logs are exported in TXT, and contain several chunks of
#' of information. Importantly, the detections may be saved with a GMT offset,
#' as opposed to the more common UTC standard.
#' Additionally, the date format isn't the standard yyyy-mm-dd.
#'
#' This function extracts the detections from the CMDA file, converts the
#' This function extracts the detections from the CDMA file, converts the
#' dates to yyyy-mm-dd, binds the time to the date and resets it to UTC, and
#' ultimately converts the dataframe into the standard format accepted by actel.
#'
#' @param file the file name.
#' @param date_format the format used by the computer that generated the file
#' @param tz the timezone of the computer that generated the file.
#'
#' @examples
#' # create a dummy detections file to read
Expand All @@ -718,6 +716,9 @@ splitSignals <- function(x) {
#' Serial Number: WHS3K-1234567
#' Node ID: 10000
#'
#' Receiver Settings:
#' GMT Correction: 00:00
#'
#' Decoded Tag Data:
#' Date Time TOA Tag ID Type Value Power
#' =======================================================================
Expand All @@ -738,16 +739,15 @@ splitSignals <- function(x) {
#' sink()
#'
#' # now read it
#' x <- convertLotekCDMAFile(dummy_file, date_format = "%m/%d/%y",
#' tz = "Europe/Copenhagen")
#' x <- convertLotekCDMAFile(dummy_file)
#'
#' # the dummy file will be deleted automatically once you close this R session.
#'
#' @return A data frame of standardized detections from the input file.
#'
#' @export
#'
convertLotekCDMAFile <- function(file, date_format, tz) {
convertLotekCDMAFile <- function(file, date_format = "%m/%d/%y") {
file_raw <- readLines(file)

serial_n <- file_raw[grep("^Serial Number:", file_raw)]
Expand All @@ -759,17 +759,23 @@ convertLotekCDMAFile <- function(file, date_format, tz) {
code_type <- NA
}

gmt_cor <- file_raw[grep("^GMT Correction:", file_raw)]
gmt_cor <- sub("GMT Correction:\\s*", "", gmt_cor)
gmt_cor <- decimalTime(gmt_cor)

det_start <- grep("=========", file_raw)[1]
det_end <- grep("Receiver Sensor Messages:", file_raw)[1] - 2

det_names <- file_raw[det_start-1]
det_names <- sub("Tag ID", "Signal", det_names)
output <- data.table::fread(file, fill = TRUE,

output <- readr::read_fwf(file,
skip = det_start,
nrows = det_end - det_start,
showProgress = FALSE,
header = FALSE)
n_max = det_end - det_start,
show_col_types = FALSE)

output <- as.data.table(output)

colnames(output) <- unlist(strsplit(det_names, "\\s\\s*"))
output$CodeSpace <- code_type
output$Receiver <- as.numeric(serial_n)
Expand All @@ -786,7 +792,8 @@ convertLotekCDMAFile <- function(file, date_format, tz) {
"Signal", "Sensor.Value", "Sensor.Unit")
output <- output[, std_cols, with = FALSE]
output$Timestamp <- fasttime::fastPOSIXct(as.character(output$Timestamp),
tz = tz)
tz = "UTC")
output$Timestamp <- output$Timestamp - (gmt_cor * 3600)

# final checks
if (any(is.na(output$Timestamp))) {
Expand Down
23 changes: 11 additions & 12 deletions man/convertLotekCDMAFile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions tests/testthat/test_convertLotekCDMAFile.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Code Type: FSK
Serial Number: WHS3K-1234567
Node ID: 10000
Receiver Settings:
GMT Correction: 00:00
Decoded Tag Data:
Date Time TOA Tag ID Type Value Power
=======================================================================
Expand All @@ -32,8 +35,7 @@ Date Time Type Details
sink()

test_that("convertLotekCDMAfile can read lotek cdma log files", {
x <- convertLotekCDMAFile(dummy_file, date_format = "%m/%d/%y",
tz = "Europe/Copenhagen")
x <- convertLotekCDMAFile(dummy_file)
expect_is(x, "data.table")
})

Expand All @@ -50,6 +52,9 @@ Code Type: FSK
Serial Number: WHS3K-1234567
Node ID: 10000
Receiver Settings:
GMT Correction: 00:00
Decoded Tag Data:
Date Time TOA Tag ID Type Value Power
=======================================================================
Expand All @@ -71,8 +76,7 @@ sink()

test_that("convertLotekCDMAfile warns user if any timestamp is bad", {
expect_warning(
x <- convertLotekCDMAFile(dummy_file2, date_format = "%m/%d/%y",
tz = "Europe/Copenhagen"),
x <- convertLotekCDMAFile(dummy_file2),
paste0("Some timestamp values are NA. This must be fixed before these ",
"detections are used in an actel analysis."),
fixed = TRUE)
Expand All @@ -94,6 +98,9 @@ Code Type: FSK
Serial Number: WHS3K-12b67
Node ID: 10000
Receiver Settings:
GMT Correction: 00:00
Decoded Tag Data:
Date Time TOA Tag ID Type Value Power
=======================================================================
Expand All @@ -115,8 +122,7 @@ sink()

test_that("convertLotekCDMAfile warns user if any receiver serial is bad", {
expect_warning(
x <- convertLotekCDMAFile(dummy_file3, date_format = "%m/%d/%y",
tz = "Europe/Copenhagen"),
x <- convertLotekCDMAFile(dummy_file3),
paste0("Some receiver serial number values are NA. This must be fixed ",
"before these detections are used in an actel analysis."),
fixed = TRUE)
Expand All @@ -137,6 +143,9 @@ Code Type:
Serial Number: WHS3K-1234567
Node ID: 10000
Receiver Settings:
GMT Correction: 00:00
Decoded Tag Data:
Date Time TOA Tag ID Type Value Power
=======================================================================
Expand All @@ -158,8 +167,7 @@ sink()

test_that("convertLotekCDMAfile warns user if code space is bad", {
expect_warning(
x <- convertLotekCDMAFile(dummy_file4, date_format = "%m/%d/%y",
tz = "Europe/Copenhagen"),
x <- convertLotekCDMAFile(dummy_file4),
paste0("Some code space values are NA. This must be fixed ",
"before these detections are used in an actel analysis."),
fixed = TRUE)
Expand All @@ -179,6 +187,9 @@ Code Type: FSK
Serial Number: WHS3K-1234567
Node ID: 10000
Receiver Settings:
GMT Correction: 00:00
Decoded Tag Data:
Date Time TOA Tag ID Type Value Power
=======================================================================
Expand All @@ -200,8 +211,7 @@ sink()

test_that("convertLotekCDMAfile warns user if any signal is bad", {
expect_warning(
x <- convertLotekCDMAFile(dummy_file5, date_format = "%m/%d/%y",
tz = "Europe/Copenhagen"),
x <- convertLotekCDMAFile(dummy_file5),
paste0("Some signal values are NA. This must be fixed before these ",
"detections are used in an actel analysis."),
fixed = TRUE)
Expand Down

0 comments on commit 92d0904

Please sign in to comment.