Skip to content

Commit

Permalink
Add internal function to check and convert Excel times. Closes #10. CC
Browse files Browse the repository at this point in the history
  • Loading branch information
mhpob committed Jun 21, 2024
1 parent 66560cf commit f13f360
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 32 deletions.
76 changes: 45 additions & 31 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 All @@ -15,20 +15,20 @@ clean_otn_deployment <- function(deployment) {
# Find which sheet has deployment data. If none are explicitly labeled, assume
# it's sheet 1
sheet_id <- grep("dep", readxl::excel_sheets(deployment),
ignore.case = T, value = T
ignore.case = T, value = T
)
if (length(sheet_id) == 0) {
sheet_id <- 1
}

# Check for header: If the first row has no columns, it likely contains it.
if (ncol(readxl::read_excel(deployment,
sheet = sheet_id,
range = "A1"
sheet = sheet_id,
range = "A1"
)) == 0) {
deployment <- readxl::read_excel(deployment,
sheet = sheet_id,
skip = 3
sheet = sheet_id,
skip = 3
)
} else {
deployment <- readxl::read_excel(deployment, sheet = sheet_id)
Expand All @@ -38,12 +38,12 @@ clean_otn_deployment <- function(deployment) {
check_head <- read.csv(deployment, nrows = 1, check.names = FALSE)
if (ncol(check_head) > length(unique(names(check_head)))) {
deployment <- read.csv(deployment,
skip = 3,
na.strings = c("NA", "")
skip = 3,
na.strings = c("NA", "")
)
} else {
deployment <- read.csv(deployment,
na.strings = c("NA", "")
na.strings = c("NA", "")
)
}
} else {
Expand All @@ -59,28 +59,14 @@ 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), ]
!is.na(deployment$recover_date_time), ]
deployment$receiver <- paste(deployment$ins_model_no,
deployment$ins_serial_no,
sep = "-"
deployment$ins_serial_no,
sep = "-"
)
deployment$stationname <- deployment$station_no

Expand All @@ -98,6 +84,34 @@ clean_otn_deployment <- function(deployment) {
}


#' @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 All @@ -109,8 +123,8 @@ provided_file_unzip <- function(files, temp_dir) {
to_unzip,
function(.) {
unzip(.,
exdir = temp_dir,
setTimes = FALSE
exdir = temp_dir,
setTimes = FALSE
)
}
)
Expand Down Expand Up @@ -144,7 +158,7 @@ write_to_tempdir <- function(type, files, temp_dir) {
## Write file to temporary directory
filepath <- file.path(temp_dir, paste0(type, ".csv"))
write.csv(files, filepath,
row.names = F
row.names = F
)


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
39 changes: 39 additions & 0 deletions tests/testthat/test-convert_times.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
)
})

0 comments on commit f13f360

Please sign in to comment.