diff --git a/NAMESPACE b/NAMESPACE index b0f47033..5584e349 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,8 @@ S3method(adj_intens,OpenSpecy) S3method(adj_intens,default) +S3method(adj_wave,OpenSpecy) +S3method(adj_wave,default) S3method(ai_classify,OpenSpecy) S3method(ai_classify,default) S3method(as.data.frame,OpenSpecy) @@ -76,6 +78,7 @@ export(OpenSpecy) export(adj_intens) export(adj_neg) export(adj_res) +export(adj_wave) export(ai_classify) export(as_OpenSpecy) export(as_hyperSpec) diff --git a/R/adj_wave.R b/R/adj_wave.R new file mode 100644 index 00000000..9a333a94 --- /dev/null +++ b/R/adj_wave.R @@ -0,0 +1,44 @@ +#' @rdname adj_wave +#' @title Adjust wavelength to wavenumbers for Raman +#' +#' @description +#' Functions for converting between wave* units. +#' +#' @return +#' An \code{OpenSpecy} object with new units converted from wavelength +#' to wavenumbers or a vector with the same conversion. +#' +#' @examples +#' data("raman_hdpe") +#' raman_wavelength <- raman_hdpe +#' raman_wavelength$wavenumber <- (-1*(raman_wavelength$wavenumber/10^7-1/530))^(-1) +#' adj_wave(raman_wavelength, laser = 530) +#' adj_wave(raman_wavelength$wavenumber, laser = 530) +#' +#' @param x an \code{OpenSpecy} object with wavenumber units specified as +#' wavelength in nm or a wavelength vector. +#' @param laser the wavelength in nm of the Raman laser. +#' @param \ldots additional arguments passed to submethods. +#' +#' @author +#' Win Cowger, Zacharias Steinmetz +#' +#' @export +adj_wave <- function(x, ...) { + UseMethod("adj_wave") +} + +#' @rdname adj_wave +#' +#' @export +adj_wave.default <- function(x, laser, ...) { + 10^7*(1/laser - 1/x) +} + +#' @rdname adj_wave +#' +#' @export +adj_wave.OpenSpecy <- function(x, laser, ...) { + x$wavenumber <- adj_wave(x$wavenumber, laser) + return(x) +} \ No newline at end of file diff --git a/man/adj_wave.Rd b/man/adj_wave.Rd new file mode 100644 index 00000000..3ebc9b72 --- /dev/null +++ b/man/adj_wave.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/adj_wave.R +\name{adj_wave} +\alias{adj_wave} +\alias{adj_wave.default} +\alias{adj_wave.OpenSpecy} +\title{Adjust wavelength to wavenumbers for Raman} +\usage{ +adj_wave(x, ...) + +\method{adj_wave}{default}(x, laser, ...) + +\method{adj_wave}{OpenSpecy}(x, laser, ...) +} +\arguments{ +\item{x}{an \code{OpenSpecy} object with wavenumber units specified as +wavelength in nm or a wavelength vector.} + +\item{laser}{the wavelength in nm of the Raman laser.} + +\item{\ldots}{additional arguments passed to submethods.} +} +\value{ +An \code{OpenSpecy} object with new units converted from wavelength +to wavenumbers or a vector with the same conversion. +} +\description{ +Functions for converting between wave* units. +} +\examples{ +data("raman_hdpe") +raman_wavelength <- raman_hdpe +raman_wavelength$wavenumber <- (-1*(raman_wavelength$wavenumber/10^7-1/530))^(-1) +adj_wave(raman_wavelength, laser = 530) +adj_wave(raman_wavelength$wavenumber, laser = 530) + +} +\author{ +Win Cowger, Zacharias Steinmetz +} diff --git a/tests/testthat/test-adj_wave.R b/tests/testthat/test-adj_wave.R new file mode 100644 index 00000000..18e12e0c --- /dev/null +++ b/tests/testthat/test-adj_wave.R @@ -0,0 +1,17 @@ + data("raman_hdpe") + raman_wavelength <- raman_hdpe + raman_wavelength$wavenumber <- (-1*(raman_wavelength$wavenumber/10^7-1/530))^(-1) + + test_that("adj_wave() handles input errors and inputs correctly", { + adj_wave(1:1000) |> expect_error() + adj_wave(raman_wavelength, laser = 530) |> expect_silent() + adj_wave(raman_wavelength$wavenumber, laser = 530) |> expect_silent() + #identical with different structured inputs. + expect_identical(adj_wave(raman_wavelength, laser = 530)$wavenumber, + adj_wave(raman_wavelength$wavenumber, laser = 530)) + #expected values. + expect_identical(round(adj_wave(raman_wavelength, laser = 530)$wavenumber)[1:2], c(301,305)) + #minimal shifts in wavenumbers + expect_true(all(abs(round(adj_wave(raman_wavelength, laser = 530)$wavenumber) - round(raman_hdpe$wavenumber))<=1)) + }) +