Skip to content

Commit

Permalink
Switch prepDetections to gsub. (#39) (#41)
Browse files Browse the repository at this point in the history
* Switch to gsub.

* Change options() to keep millisecond information. Copy raw data and change by reference.

* Change frac calculation and drop resulting excess.

* Explicity provide date format. Round off ts, epo, and frac.

Co-authored-by: Mike O'Brien <[email protected]>
  • Loading branch information
baktoft and mhpob authored Dec 18, 2021
1 parent 6bdc92e commit 5865e0f
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions R/prepFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,33 @@
#' prepped_detections <- prepDetections("path-to-raw-data-file", type="vemco_vue")
#' }
prepDetections <- function(raw_dat, type){
detections <- data.table::data.table()
if (type == "vemco_vue"){
detections[, ts:=as.POSIXct(raw_dat$'Date and Time (UTC)', tz="UTC")]
detections[, tag:=as.numeric(sapply(raw_dat$Transmitter, function(x) strsplit(x, "-")[[1]][3]))]
detections[, epo:=as.numeric(ts)]
detections[, frac:= (as.numeric(sapply(raw_dat$'Date and Time (UTC)', function(x) strsplit(x, "\\.")[[1]][2]))) / 1000]
detections[, serial:=as.numeric(sapply(raw_dat$Receiver, function(x) strsplit(x, "-")[[1]][2]))]
}
detections[]
return(detections)

detections <- data.table::copy(raw_dat)

if (type == "vemco_vue"){

# Only parse datetime if needed
if(!inherits(detections$`Date and Time (UTC)`, 'POSIXt')){
detections[, ts := as.POSIXct(`Date and Time (UTC)`,
format = '%Y-%m-%d %H:%M:%OS',
tz = 'UTC')]
} else{
detections[, ts := `Date and Time (UTC)`]
}


detections[, ':='(tag = as.numeric(gsub('.*-', '', Transmitter)),
serial = as.numeric(gsub('.*-', '', Receiver)),
epo = as.numeric(ts))]
detections[, frac := round(epo - floor(epo), 3)]
detections[, epo := floor(epo)]
detections[, ts := as.POSIXct(epo,
origin = '1970-01-01',
tz = 'UTC')]

}

detections[, .(ts, tag, epo, frac, serial)]

}

0 comments on commit 5865e0f

Please sign in to comment.