Skip to content

Commit

Permalink
Merge pull request #105 from spectral-cockpit/104-fix-timezone-parsing
Browse files Browse the repository at this point in the history
- fix sample and time metadata parsing for `read_opus(dsn, data_only = TRUE)`
  • Loading branch information
philipp-baumann authored Mar 12, 2024
2 parents 96c88be + 2449d67 commit fb58297
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Description: Read data from OPUS binary files of Fourier-Transform infrared
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
URL: https://github.com/spectral-cockpit/opusreader2
BugReports: https://github.com/spectral-cockpit/opusreader2/issues
Suggests: knitr, rmarkdown, testthat (>= 3.0.0), future.apply, future, progressr
Expand Down
3 changes: 1 addition & 2 deletions R/extract_metadata.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
get_basic_metadata <- function(ds_list) {
timestamp <- get_meta_timestamp(ds_list)


basic_metadata <- data.frame(
# opus_filename,
# opus_path,
Expand All @@ -26,7 +25,7 @@ get_meta_timestamp <- function(ds_list) {

time_hour_tz <- strsplit(x = save_file_time, split = " ")[[1L]]
time_hour <- paste(time_hour_tz[1L], time_hour_tz[2L])
tz <- gsub(pattern = "\\(|\\)", "", x = time_hour_tz[3L])
tz <- gsub(pattern = "\\(|\\)|\\s+", "", x = time_hour_tz[3L])
etc_tz <- paste0("Etc/", tz) # see ?strptime for details

# note that negative offsets denote UTC+x time stamps
Expand Down
16 changes: 10 additions & 6 deletions R/parse_opus.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ parse_opus <- function(raw, data_only) {

dataset_list <- name_output_list(dataset_list)


if (data_only) {
if (any(grepl("^ab$|^refl$", names(dataset_list)))) {
dataset_list <- extract_data(
dataset_list,
c("ab", "refl", "ab_data_param", "refl_data_param")
c("ab", "refl", "ab_data_param", "refl_data_param", "history", "sample")
)
} else {
dataset_list <- extract_data( # nolint
Expand All @@ -140,14 +139,16 @@ parse_opus <- function(raw, data_only) {
"ab_no_atm_comp",
"refl_no_atm_comp",
"ab_no_atm_comp_data_param",
"refl_no_atm_comp_data_param"
"refl_no_atm_comp_data_param",
"history",
"sample"
)
)
}
} else {
dataset_list <- lapply(dataset_list, calc_parameter_chunk_size)
}

dataset_list <- lapply(dataset_list, calc_parameter_chunk_size)

dataset_list <- lapply(dataset_list, function(x) parse_chunk(x, raw))

data_types <- get_data_types(dataset_list) # nolint
Expand All @@ -158,7 +159,10 @@ parse_opus <- function(raw, data_only) {
)

if (data_only) {
dataset_list <- dataset_list[lapply(dataset_list, class) == "data"]
dataset_list <- dataset_list[
lapply(dataset_list, class) == "data" | names(dataset_list) == "history" |
names(dataset_list) == "sample"
]
}

dataset_list <- sort_list_by(dataset_list)
Expand Down
4 changes: 4 additions & 0 deletions R/read_opus.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ read_opus_single <- function(dsn, data_only = FALSE) {

data <- c(list(basic_metadata = basic_metadata), parsed_data)

if (isTRUE(data_only)) {
data <- data[!names(data) %in% c("history", "sample")]
}

attr(data, "dsn_filename") <- dsn_filename

return(data)
Expand Down
5 changes: 5 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Agroecosystems
ai
al
analytics
artefacts
aut
automatec
Baumann
Expand All @@ -15,6 +16,7 @@ Config
Continous
continous
cph
cran
cre
CSIRO
decrypter
Expand Down Expand Up @@ -64,11 +66,14 @@ PARAM
parsable
Philipp
PhSm
pierreroudier
postfixed
pre
precessor
progressr
PRs
pypi
qedsoftware
Quant
QUANT
Raman
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-read_opus.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
test_that("all test files are parsed without warning", {
test_that("all test files are parsed without warnings and errors", {
expect_no_error(read_opus(dsn = "../../inst/extdata/test_data"))
expect_no_warning(read_opus(dsn = "../../inst/extdata/test_data"))
})

test_that("all test files are parsed without warnings and errors when
`data_only = TRUE`", {
expect_no_error(
read_opus(dsn = "../../inst/extdata/test_data", data_only = TRUE)
)
expect_no_warning(
read_opus(dsn = "../../inst/extdata/test_data", data_only = TRUE)
)
})

0 comments on commit fb58297

Please sign in to comment.