Skip to content

Commit

Permalink
Improve wtf_metadata_match messages and ensure stringsAsFactors=FALSE (
Browse files Browse the repository at this point in the history
…#34)

* Clarify wtf_metadata_match message

* Ensure stringsAsFactors=FALSE for all reads
  • Loading branch information
bpbond authored Feb 29, 2024
1 parent 0c51d11 commit fca3fbf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ wtf_metadata_match <- function(data_timestamps,
matches <- rep(NA_real_, length(data_timestamps))
entries <- seq_along(start_timestamps)
for(i in entries) {
mtch <- data_timestamps >= start_timestamps[i] & data_timestamps <= stop_timestamps[i]
mtch <- data_timestamps >= start_timestamps[i] & data_timestamps < stop_timestamps[i]
matches[mtch] <- i
}

# Metadata rows with zero matches seems unexpected
no_matches <- base::setdiff(entries, unique(matches, na.omit(matches)))
if(length(no_matches)) {
wtf_message("Entries with no timestamp matches: ", paste(no_matches, collapse = ", "))
lnm <- length(no_matches)
if(lnm) {
wtf_message(lnm, ifelse(lnm == 1, " entry", " entries"),
" had no timestamp matches!")
}

return(matches)
Expand Down
9 changes: 7 additions & 2 deletions R/read_data_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ wtf_read_LI78x0 <- function(file, model) {

# Read the data, construct TIMESTAMP, add metadata,
# and remove unneeded LI-COR DATE and TIME columns
dat <- read.table(textConnection(dat_raw), na.strings = "nan", header = TRUE)
dat <- read.table(textConnection(dat_raw),
na.strings = "nan",
header = TRUE,
stringsAsFactors = FALSE)
dat$TIMESTAMP <- lubridate::ymd_hms(paste(dat$DATE, dat$TIME), tz = tz)
dat$TZ <- tz
dat$SN <- sn
Expand Down Expand Up @@ -91,7 +94,9 @@ wtf_read_LGR915 <- function(file, tz = "UTC") {
dat_raw <- readLines(file)

# A single header line encodes version number, date, and serial number
dat <- read.csv(textConnection(dat_raw[-1]), check.names = FALSE)
dat <- read.csv(textConnection(dat_raw[-1]),
check.names = FALSE,
stringsAsFactors = FALSE)
dat$Time <- mdy_hms(dat$Time, tz = tz)
dat$SN <- trimws(gsub(".*SN:", "", dat_raw[1]))
dat$MODEL <- "915-0011"
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ test_that("wtf_metadata_match works", {
s_d <- c("2024-01-01", "2024-01-01", "2024-01-10")
s_t <- c("13:00:00", "13:05:00", "13:10:00")
expect_message(wtf_metadata_match(d_t, s_d, s_t, c(60, 60, 60)),
regexp = "no timestamp matches: 3")
regexp = "1 entry had no timestamp matches")
})

0 comments on commit fca3fbf

Please sign in to comment.