Skip to content

Commit

Permalink
fasttime -> fast_strptime. Closes ocean-tracking-network#211.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhpob committed Mar 7, 2024
1 parent f016ab1 commit 3fdcadb
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ importFrom(graphics,par)
importFrom(graphics,points)
importFrom(graphics,symbols)
importFrom(graphics,text)
importFrom(lubridate,parse_date_time)
importFrom(lubridate,fast_strptime)
importFrom(magrittr,"%>%")
importFrom(stats,approx)
importFrom(stats,dnorm)
Expand Down
15 changes: 6 additions & 9 deletions R/load-read_glatos_detections.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#' never need to set this argument (`NULL` should work).
#'
#' @details Data are loaded using [fread][data.table::fread] and timestamps are
#' coerced to POSIXct using [fastPOSIXct][fasttime::fastPOSIXct]. All times must
#' be in UTC timezone per GLATOS standard.
#' coerced to POSIXct using [fast_strptime][lubridate::fast_strptime]. All times
#' must be in UTC timezone per GLATOS standard.
#'
#' @details Column `animal_id` is considered a required column by many other
#' functions in this package, so it will be created if any records are `NULL`.
Expand All @@ -40,7 +40,7 @@
#'
#' det <- read_glatos_detections(det_file)
#'
#' @importFrom lubridate parse_date_time
#' @importFrom lubridate fast_strptime
#'
#' @export
read_glatos_detections <- function(det_file, version = NULL) {
Expand Down Expand Up @@ -84,16 +84,13 @@ read_glatos_detections <- function(det_file, version = NULL) {
na.strings = c("", "NA")
)

# coerce timestamps to POSIXct; note that with fastPOSIXct raw
# timestamp must be in UTC; and tz argument sets the tzone attr only
options(lubridate.fasttime = TRUE)
# coerce timestamps to POSIXct
for (j in timestamp_cols) {
data.table::set(dtc,
j = glatos_detection_schema[[vversion]]$name[j],
value = lubridate::parse_date_time(
value = lubridate::fast_strptime(
dtc[[glatos_detection_schema[[vversion]]$name[j]]],
orders = "ymd HMS",
tz = "UTC"
format = "%Y-%m-%d %H:%M:%S", tz = "UTC", lt = FALSE
)
)
}
Expand Down
13 changes: 5 additions & 8 deletions R/load-read_glatos_receivers.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' `"1.0"`. Any other values will trigger an error.
#'
#' @details Data are loaded using [fread][data.table::fread] and timestamps are
#' coerced to POSIXct using [fastPOSIXct][fasttime::fastPOSIXct]. All
#' coerced to POSIXct using [fast_strptime][lubridate::fast_strptime]. All
#' timestamps must be 'YYYY-MM-DD HH:MM' format and in UTC timezone per GLATOS
#' standard.
#'
Expand All @@ -36,7 +36,7 @@
#'
#' rcv <- read_glatos_receivers(rec_file)
#'
#' @importFrom lubridate parse_date_time
#' @importFrom lubridate fast_strptime
#'
#' @export
read_glatos_receivers <- function(rec_file, version = NULL) {
Expand Down Expand Up @@ -80,16 +80,13 @@ read_glatos_receivers <- function(rec_file, version = NULL) {
# read data
rec <- data.table::fread(rec_file, sep = ",", colClasses = col_classes)

# coerce timestamps to POSIXct; note that with fastPOSIXct raw
# timestamp must be in UTC; and tz argument sets the tzone attr only
options(lubridate.fasttime = TRUE)
# coerce timestamps to POSIXct
for (j in timestamp_cols) {
data.table::set(rec,
j = glatos_receivers_schema[[ver_txt]]$name[j],
value = lubridate::parse_date_time(
value = lubridate::fast_strptime(
rec[[glatos_receivers_schema[[ver_txt]]$name[j]]],
orders = "ymd HMS",
tz = "UTC"
format = "%Y-%m-%d %H:%M:%S", tz = "UTC", lt = FALSE
)
)
}
Expand Down
11 changes: 6 additions & 5 deletions R/load-read_otn_deployments.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' @details
#' Data are loaded using [data.table::fread()] package and timestamps
#' are coerced to POSIXct using the [fasttime::fastPOSIXct()]. All
#' are coerced to POSIXct using [lubridate::fast_strptime()]. All
#' times must be in UTC timezone per GLATOS standard.
#'
#' @details
Expand All @@ -39,7 +39,7 @@
#' dep <- read_otn_deployments(deployment_file)
#' }
#'
#' @importFrom lubridate parse_date_time
#' @importFrom lubridate fast_strptime
#' @importFrom tidyr extract
#' @importFrom dplyr mutate
#' @importFrom magrittr "%>%"
Expand All @@ -60,8 +60,7 @@ read_otn_deployments <- function(deployment_file,
na.strings = c("", "NA")
)

# coerce timestamps to POSIXct; note that with fastPOSIXct raw
# timestamp must be in UTC; and tz argument sets the tzone attr only
# coerce timestamps to POSIXct
dtc <- dtc %>% tidyr::extract(deploy_date_col, into = "deploy_date", regex = "(\\d+-\\d+-\\d+)")
dtc <- dtc %>% tidyr::extract(recovery_date_col, into = "recovery_date", regex = "(\\d+-\\d+-\\d+)")
dtc <- dtc %>% tidyr::extract(last_download_col, into = "last_download", regex = "(\\d+-\\d+-\\d+)")
Expand All @@ -70,7 +69,9 @@ read_otn_deployments <- function(deployment_file,
for (j in timestamp_cols) {
data.table::set(dtc,
j = otn_deployments_schema$name[j],
value = lubridate::parse_date_time(dtc[[otn_deployments_schema$name[j]]], orders = "ymd", tz = "UTC")
value = lubridate::fast_strptime(
dtc[[otn_deployments_schema$name[j]]],
format = "%Y-%m-%d %H:%M:%S", tz = "UTC", lt = FALSE)
)
}
# coerce dates to date
Expand Down
21 changes: 11 additions & 10 deletions R/load-read_otn_detections.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#'
#' @details
#' Data are loaded using [data.table::fread()] package and timestamps
#' are coerced to POSIXct using the [fasttime::fastPOSIXct()]. All
#' are coerced to POSIXct using [lubridate::fast_strptime()]. All
#' times must be in UTC timezone per GLATOS standard.
#'
#' @details
Expand All @@ -28,7 +28,7 @@
#' )
#' det <- read_otn_detections(det_file)
#'
#' @importFrom lubridate parse_date_time
#' @importFrom lubridate fast_strptime
#'
#' @export
read_otn_detections <- function(det_file) {
Expand All @@ -38,11 +38,11 @@ read_otn_detections <- function(det_file) {
timestamp_cols <- which(col_classes == "POSIXct")
date_cols <- which(col_classes == "Date")
col_classes[c(timestamp_cols, date_cols)] <- "character"

# read data, suppressWarnings because some columns could be missing
dtc <- suppressWarnings(data.table::fread(det_file,
sep = ",", colClasses = col_classes,
na.strings = c("", "NA")
sep = ",", colClasses = col_classes,
na.strings = c("", "NA")
))
# This check is for non-matched detection extracts. They are missing some required columns, this attempts to create them.
# More info on OTN detection extracts here: https://members.oceantrack.org/data/otn-detection-extract-documentation-matched-to-animals
Expand All @@ -53,13 +53,14 @@ read_otn_detections <- function(det_file) {
dtc$tagname <- dtc$fieldnumber
dtc$codespace <- purrr::map(dtc$fieldnumber, get_codemap)
}
# coerce timestamps to POSIXct; note that with fastPOSIXct raw
# timestamp must be in UTC; and tz argument sets the tzone attr only
options(lubridate.fasttime = TRUE)
# coerce timestamps to POSIXct
for (j in timestamp_cols) {
data.table::set(dtc,
j = otn_detection_schema$name[j],
value = lubridate::parse_date_time(dtc[[otn_detection_schema$name[j]]], orders = "ymd HMS", tz = "UTC")
j = otn_detection_schema$name[j],
value = lubridate::fast_strptime(
dtc[[otn_detection_schema$name[j]]],
format = "%Y-%m-%d %H:%M:%S", tz = "UTC", lt = FALSE
)
)
}
# coerce dates to date
Expand Down
4 changes: 2 additions & 2 deletions man/read_glatos_detections.Rd

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

2 changes: 1 addition & 1 deletion man/read_glatos_receivers.Rd

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

2 changes: 1 addition & 1 deletion man/read_otn_deployments.Rd

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

2 changes: 1 addition & 1 deletion man/read_otn_detections.Rd

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

0 comments on commit 3fdcadb

Please sign in to comment.