Find wavelength values corresponding to a target y value in any spectrum. The name of the column of the spectral data to be used to match the target needs to be passed as argument unless the spectrum contains a single numerical variable in addition to "w.length".
Usage
find_wls(
x,
target = NULL,
col.name.x = NULL,
col.name = NULL,
.fun = `<=`,
interpolate = FALSE,
idfactor = length(target) > 1,
na.rm = FALSE
)
Arguments
- x
an R object
- target
numeric or character. A numeric value indicates the spectral quantity value for which wavelengths are to be searched. A character representing a number is converted to a number. A character value representing a number followed by a function name, will be also accepted and decoded, such that
"0.1max"
is interpreted as targetting one tenthof the maximum value in a column. The character strings "half.maximum" and "HM" are synonyms for "0.5max" while "half.range" and "HR" are synonyms for "0.5range". These synonyms are converted to the cannonical form before saving them to the returned value.- col.name.x
character The name of the column in which to the independent variable is stored. Defaults to "w.length" for objects of class
"generic_spct"
or derived.- col.name
character The name of the column in which to search for the target value.
- .fun
function A binary comparison function or operator.
- interpolate
logical Indicating whether the nearest wavelength value in
x
should be returned or a value calculated by linear interpolation between wavelength values stradling the target.- idfactor
logical or character Generates an index column of factor type. If
idfactor = TRUE
then the column is auto named target.idx. Alternatively the column name can be directly passed as argument toidfactor
as a character string.- na.rm
logical indicating whether
NA
values should be stripped before searching for the target.
Value
A spectrum object of the same class as x
with fewer rows,
possibly even no rows. If FALSE
is passed to interpolate
a
subset of x
is returned, otherwise a new object of the same class
containing interpolated wavelenths for the target
value is returned.
Note
This function is used internally by method wls_at_target()
, and
these methods should be preferred in user code and scripts.
Examples
find_wls(white_led.source_spct)
#> Object: source_spct [0 x 2]
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 0 × 2
#> # ℹ 2 variables: w.length <dbl>, s.e.irrad <dbl>
find_wls(white_led.source_spct, target = "0.5max")
#> Object: source_spct [2 x 2]
#> Wavelength range 540.94-660.78 nm, step 119.84 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 2 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 541. 0.328
#> 2 661. 0.330
find_wls(white_led.source_spct, target = 0.4)
#> Object: source_spct [2 x 2]
#> Wavelength range 556.08-651.28 nm, step 95.2 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 2 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 556. 0.399
#> 2 651. 0.401
find_wls(white_led.source_spct, target = 0.4, interpolate = TRUE)
#> Object: source_spct [2 x 2]
#> Wavelength range 556.21711-651.38801 nm, step 95.1709 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 2 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 556. 0.4
#> 2 651. 0.4
find_wls(white_led.source_spct, target = c(0.3, 0.4))
#> Object: source_spct [4 x 2]
#> containing 2 spectra in long form
#> Wavelength range 533.12-664.84 nm, step 13.56-95.2 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 4 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 533. 0.299
#> 2 556. 0.399
#> 3 651. 0.401
#> 4 665. 0.301
find_wls(white_led.source_spct, target = c(0.3, 0.4), idfactor = "target")
#> Object: source_spct [4 x 3]
#> containing 2 spectra in long form
#> Wavelength range 533.12-664.84 nm, step 13.56-95.2 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 4 × 3
#> w.length s.e.irrad target
#> <dbl> <dbl> <fct>
#> 1 533. 0.299 0.3
#> 2 556. 0.399 0.4
#> 3 651. 0.401 0.4
#> 4 665. 0.301 0.3
find_wls(white_led.source_spct, target = c(0.3, 0.4), idfactor = TRUE)
#> Object: source_spct [4 x 3]
#> containing 2 spectra in long form
#> Wavelength range 533.12-664.84 nm, step 13.56-95.2 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 4 × 3
#> w.length s.e.irrad target.idx
#> <dbl> <dbl> <fct>
#> 1 533. 0.299 0.3
#> 2 556. 0.399 0.4
#> 3 651. 0.401 0.4
#> 4 665. 0.301 0.3
find_wls(white_led.source_spct, target = "0.5max")
#> Object: source_spct [2 x 2]
#> Wavelength range 540.94-660.78 nm, step 119.84 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 2 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 541. 0.328
#> 2 661. 0.330
find_wls(white_led.source_spct, target = "0.05max")
#> Object: source_spct [2 x 2]
#> Wavelength range 432.7-741.61 nm, step 308.91 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 2 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 433. 0.0326
#> 2 742. 0.0329
find_wls(white_led.source_spct, target = "0.5range")
#> Object: source_spct [2 x 2]
#> Wavelength range 540.94-660.78 nm, step 119.84 nm
#> Label: led_desk201
#> Measured on 2016-12-19 16:19:57.298874 UTC
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 2 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 541. 0.328
#> 2 661. 0.330
led.df <- as.data.frame(white_led.source_spct)
find_wls(led.df)
#> [1] w.length s.e.irrad
#> <0 rows> (or 0-length row.names)
find_wls(led.df, col.name = "s.e.irrad", col.name.x = "w.length")
#> [1] w.length s.e.irrad
#> <0 rows> (or 0-length row.names)
find_wls(led.df, col.name = "s.e.irrad", col.name.x = "w.length",
target = 0.4)
#> w.length s.e.irrad
#> 1 556.08 0.3993410
#> 2 651.28 0.4008396
find_wls(led.df, col.name = "s.e.irrad", col.name.x = "w.length",
target = c(0.3, 0.4))
#> w.length s.e.irrad
#> 1 533.12 0.2994991
#> 3 556.08 0.3993410
#> 4 651.28 0.4008396
#> 2 664.84 0.3009634
find_wls(led.df, col.name = "s.e.irrad", col.name.x = "w.length",
target = 0.4, idfactor = "target")
#> w.length s.e.irrad target
#> 1 556.08 0.3993410 0.4
#> 2 651.28 0.4008396 0.4