diff --git a/R/extract_metadata.R b/R/extract_metadata.R index 6f9c2c71..5e70e37c 100644 --- a/R/extract_metadata.R +++ b/R/extract_metadata.R @@ -16,7 +16,7 @@ get_basic_metadata <- function(ds_list) { get_meta_timestamp <- function(ds_list) { text <- ds_list$history$text - history <- paste0(text, collapse = "") + history <- paste0(text, collapse = "") save_file_time <- gsub(".*Save File\t\t(\\d.*)\t\t\t.*", "\\1", history) time_hour_tz <- strsplit(x = save_file_time, split = " ")[[1L]] @@ -41,7 +41,7 @@ get_meta_timestamp <- function(ds_list) { get_meta_utc_datetime <- function(timestamp) { tz <- timestamp$timezone utc_diff <- as.integer(gsub("\\D+(\\+\\d)", "\\1", tz)) - utc_datetime <- as.POSIXct(strptime(timestamp$datetime, + utc_datetime <- as.POSIXct(strptime(timestamp$datetime, format = "%Y-%m-%d %H:%M:%S", tz = "UTC" )) - (utc_diff * 3600) diff --git a/plot.R b/plot.R new file mode 100644 index 00000000..58349ced --- /dev/null +++ b/plot.R @@ -0,0 +1,87 @@ +#' Plot spectra read from OPUS files +#' +#' @param data list of class "opusreader2" (single OPUS file) or both +#' "opusreader2" and "list_opusreader2", containing spectral data parsed from +#' Bruker OPUS binary files. +#' @param data_type character vector of length one with desired spectral data +#' type to plot. +#' @param plot_type plot type, either `"base"``, printing a base plot, or +#' `"ggplot2"``, which returns a ggplot2 object. +#' @family core +#' @return a base R plot with spectra +#' +#' @export +plot <- function(data, + data_type = c( + "ref_no_atm_comp", "refl", + "ab_no_atm_comp", "ab", + "sc_sample", "ig_sample", + "sc_ref", "ig_ref" + ), + plot_type = c("base", "ggplot2")) { + UseMethod("plot") +} + + +plot.opusreader2 <- function(data, data_type, plot_type) { + data_type <- match.arg(data_type) + plot_type <- match.arg(plot_type) + + validate_plot(data, data_type, plot_type) + + # get the spectra (y) and x (x-axis; e.g. wavenumber) + if (inherits(data, "list_opusreader2")) { + spectra <- do.call( + rbind, + Map(function(x) x[[y]], x = data, y = data_type) + ) + } else { + spectra <- data[[data_type]] + } + + ylab <- switch( + EXPR = data_type, + "ref_no_atm_comp" = "Reflectance (no atm. comp.)", + "refl" = "Reflectance", + "ab_no_atm_comp" = "Absorbance (no atm. comp.)", + "ab" = "Absorbance", + "sc_sample" = "Single channel sample", + "ig_sample" = "Interferogram sample", + "sc_ref" = "Single channel reference", + "ig_ref" = "Interferogram sample" + ) + + matplot( + x = x, + y = t(spectra), + type = "l", + lwd = 1.5, + xlab = xlab, + ylab = ylab + ) +} + +validate_plot <- function(data, data_type, plot_type) { + stopifnot( + inherits(data, "opusreader2") + ) + validate_plot_data_type(data, data_type) +} + +validate_plot_data_type <- function(data, data_type, plot_type) { + if (inherits(data, "list_opusreader2")) { + all_data_types <- all( + unlist(lapply(data, function(x) data_type %in% names(x))) + ) + } else { + all_data_types <- data_type %in% names(data) + } + + if (!isTRUE(all_data_types)) { + stop("All `data` elements need to contain `data_type`", + paste0(data_type, "."), + call. = FALSE + ) + } + invisible(all_data_types) +} diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index af7a7c66..9af6ab83 100644 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -50,4 +50,4 @@ For single OPUS files, there is also a lower level implementation. ```r data_single <- read_opus_single(dsn = file) -``` \ No newline at end of file +```