Skip to content

Commit

Permalink
Merge pull request #31 from mhpob/issue-10
Browse files Browse the repository at this point in the history
Deal with Excel date-times in deployment data sheets
  • Loading branch information
mhpob authored Jun 21, 2024
2 parents 9f586d3 + ea1c5fa commit 58eb5bb
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: otndo
Title: Understand your OTN data
Version: 0.2.1
Version: 0.2.2
Authors@R:
person("Michael", "O'Brien", , "mike@obrien.page", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-1420-6395"))
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## otndo 0.2
### v 0.2.2
* Fixed behavior where `clean_otn_deployment` would return different columns if there was no internal transmitter logged. [Issue #10](https://github.com/mhpob/otndo/issues/10); [d58e2d4](https://github.com/mhpob/otndo/pull/31/commits/d58e2d46e05aed7ba4acc08b8d02672b28d79804)
* Add internal function (`convert_times`) that checks for Excel-formatted date-times and converts accordingly [f13f360](https://github.com/mhpob/otndo/pull/31/commits/f13f360fe5e4438b6ba668039a6c94f04eeafe60)

### v 0.2.1

* Fleshes out tests to get to 100% coverage (for now!). Note that this is an overestimate as I can't actually get into the QMD files to test.
Expand Down
66 changes: 37 additions & 29 deletions R/utilities_make.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @param files Character. File paths of files to be unzipped or written to a directory
#' @param temp_dir Character. File path of temporary directory
#' @param detection_file Character. File path of detections.
#'
#' @param date_time Character or numeric. Date-time to convert.
#'
#' @name utilities-make
#' @keywords internal
Expand Down Expand Up @@ -59,22 +59,8 @@ clean_otn_deployment <- function(deployment) {
deployment <- deployment[!is.na(deployment$deploy_date_time), ]
deployment <- deployment[!deployment$recovered %in% c("l", "failed", NA), ]

deployment$deploy_date_time <- as.POSIXct(
deployment$deploy_date_time,
tz = "UTC",
tryFormats = c(
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M:%S"
)
)
deployment$recover_date_time <- as.POSIXct(
deployment$recover_date_time,
tz = "UTC",
tryFormats = c(
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M:%S"
)
)
deployment$deploy_date_time <- convert_times(deployment$deploy_date_time)
deployment$recover_date_time <- convert_times(deployment$recover_date_time)

deployment <- deployment[!is.na(deployment$deploy_date_time) &
!is.na(deployment$recover_date_time), ]
Expand All @@ -86,23 +72,45 @@ clean_otn_deployment <- function(deployment) {

if ("transmitter" %in% names(deployment)) {
deployment$internal_transmitter <- deployment$transmitter
deployment[, c(
"stationname", "receiver", "internal_transmitter",
"deploy_date_time", "deploy_lat", "deploy_long",
"recover_date_time"
)]
} else {
(
deployment[, c(
"stationname", "receiver",
"deploy_date_time", "deploy_lat", "deploy_long",
"recover_date_time"
)]
)
deployment$internal_transmitter <- NA
}

deployment[, c(
"stationname", "receiver", "internal_transmitter",
"deploy_date_time", "deploy_lat", "deploy_long",
"recover_date_time"
)]
}


#' @rdname utilities-make
#' @keywords internal
convert_times <- function(date_time) {
check_times <- function(x) {
# check if Excel format
# Assumes that it starts with 5 numbers
if (grepl("^\\d{5}", x)) {
as.POSIXct(
as.numeric(x) * (60 * 60 * 24),
tz = "UTC",
origin = "1899-12-30"
)
} else {
as.POSIXct(
x,
tz = "UTC",
tryFormats = c(
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M:%S"
)
)
}
}

do.call("c", lapply(date_time, check_times))
}

#' @rdname utilities-make
#' @keywords internal
provided_file_unzip <- function(files, temp_dir) {
Expand Down
5 changes: 5 additions & 0 deletions man/utilities-make.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-clean_otn_deployment.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ test_that("Correct names when no internal transmitter columns", {
expect_named(
clean_otn_deployment(deployment_sheet1),
c(
"stationname", "receiver", "deploy_date_time", "deploy_lat", "deploy_long",
"stationname", "receiver", "internal_transmitter",
"deploy_date_time", "deploy_lat", "deploy_long",
"recover_date_time"
)
)
Expand Down
54 changes: 54 additions & 0 deletions tests/testthat/test-convert_times.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
test_datetime <- as.POSIXct("2020-04-09T11:04:59",
tz = "UTC",
format = "%Y-%m-%dT%H:%M:%S"
)

test_that("converts ISO 8601", {
expect_equal(
convert_times("2020-04-09T11:04:59"),
test_datetime
)
})




test_that("converts space-separated date-times", {
expect_equal(
convert_times("2020-04-09 11:04:59"),
test_datetime
)
})




test_that("converts Excel numeric", {
expect_equal(
convert_times(43930.461793981),
test_datetime
)
})




test_that("converts Excel character", {
expect_equal(
convert_times("43930.461793981"),
test_datetime
)
})




test_that("works with vectors", {
expect_equal(
convert_times(c(
"2020-04-09T11:04:59", "2020-04-09 11:04:59",
"43930.461793981", 43930.461793981
)),
rep(test_datetime, 4)
)
})

0 comments on commit 58eb5bb

Please sign in to comment.