User Guide: 2 Radiation"

knitr::opts_chunk$set(fig.width=7.2, fig.height=4.3)
# library(tibble)
options(tibble.print_max = 6, tibble.print_min = 4)


Package 'photobiology' defines a family of classes based on the tibble class, mostly compatible with R's data frames. The present package by imposing some restrictions on the naming of the member vectors, allows methods to find the data when passed one of these objects as argument. In addition, as the data are checked when the object is built or modified, there is no need to test for their validity each time a calculation is carried out. Other advantage of using spectrum objects, is that specialized versions of generic functions like print and operators like + are defined for spectra. ___spct objects are also tibble and data.frame objects, as a result of how classes have been derived. In this package we define a generic or base spectrum class, derived from tibble, which in turn has been derived from data.frame. Classes for specialized types of spectra are derived from generic_spct. This parenthood hierarchy means that spectra objects can be used almost anywhere where a data.frame is expected. Specializations of many methods including extraction (indexing) methods and partial assignment methods are defined to ensure that the expectations on the variables contained in objects of these classes is guaranteed in most situations. Other specializations of methods and functions are related to achieving a convenient and concise syntax tailored for spectral data as in the case of mathematical operators and functions.

Another important aspect is that when spectral data are stored in objects of these classes, the physical quantities and units of expression are known. Furthermore, attributes are used to keep track of both metadata related to the origin of the data and of later transformations that affect their interpretation, such as normalization or re-scaling. Although sanity tests are applied at the time of object creation, to a large extent the responsibility of ensuring that the numbers provided as argument to object constructors comply with expectations remains with the users of the package.

In addition to the classes for storing individual spectra, classes for storing collections of spectra are defined. These classes are derived from class list and can contain member spectra of different lengths and measured at different wavelength values.

We give in this vignette brief descriptions and examples of the use of different classes, methods, functions and operators. We start with the simplest and most frequently used methods.

Getting started

We load two packages in addition to 'photobiology', 'lubridate' and 'dplyr', as they will be used in the examples.



Package 'photobiology' defines several classes intended to be used to store different types of spectral data. They are all derived from generic_spct, which in turn is derived from tibble::tibble. Table 1 lists these classes. Attributes are used in objects of these classes to store metadata such as information about units of expression.

Table 1. Classes for spectral data. In addition to the required variables listed in the table, additional arbitrary variables are partly supported---some operations will not include them in returned values to avoid ambiguity and other possible conflicts. In addition to the attributes listed, all spectral objects have attributes multiple.wl, idfactor, normalized, scaled, when.measured, where.measured, what.measured plus the normal attributes of tibble (and data.frame) objects including comment.

| Class name | Required variables | Attributes | |:-----------|:----------------|:---------------------------| | generic_spct | w.length | | | calibration_spct | w.length, irrad.mult | instr.desc | | raw_spct | w.length, counts | instr.desc, instr.settings, linearized| | cps_spct | w.length, cps | instr.desc, instr.settings, linearized |
| source_spct | w.length, s.e.irrad | instr.desc, instr.settings, time.unit, bswf |
| | w.length, s.q.irrad | time.unit, bswf |
| filter_spct | w.length, Tfr | Tfr.type |
| | w.length, A | Tfr.type |
| reflector_spct | w.length, Rfr | Rfr.type |
| object_spct | w.length, Tfr, Rfr | Tfr.type, Rfr.type |
| response_spct | w.length, s.e.response | time.unit |
| | w.length, s.q.response | time.unit |
| chroma_spct | w.length, x, y, z | |

The design imposes the restriction that data from different observations are never present as different data columns, if present, additional data columns represent different properties from the same observation event. In other words, the storage format is tidy as defined by Hadley Wickham. In most cases, one spectral object should correspond to one spectral observation, but some functions are compatible or can be used to create spectral objects where the spectral data from different observations are stored "longitudinally" and "tagged" with a factor with a level for each observation event. These observations must use consistent units of expression and attribute values. This long format is useful, for example, when producing plots with package 'ggplot2'. If spectra are stored in long form, e.g. for plotting with 'ggplot2', attributes are stored as named lists or enforced to be the same across spectra. This allows to reconstruct a collection of spectra from a long-form spectral object preserving most of the metadata.

Data assumptions

A key assumption of the package is that wavelengths are always expressed in nanometres ($1~\mathrm{nm} = 1 \cdot 10^{-9}\,\mathrm{m}$). If the data to be analysed use different units for wavelengths, e.g.\ Ångstrom ($1~\textrm{Å} = 1 \cdot 10^{-10}\,\mathrm{m}$), the values need to be re-scaled before creating objects of the spectral classes. The same applies to all spectral quantities, as there is an expectation in every case, of using base SI units for expression. Table 2 lists the units of expression for the different variables and the metadata attributes that may determine variations in the expression of the quantities.

Table 2. Variables used for spectral data and their units of expression. A: as stored in objects of the spectral classes, B: also recognized by the set family of functions for spectra and automatically converted. time.unit accepts in addition to the character strings listed in the table, objects of classes lubridate::duration and period, in addition numeric values are interpreted as seconds. exposure.time accepts these same values, but not the character strings.

| Variables | Unit of expression | Attribute value | |:-------------------|:-------------------|:-----------------| | A: stored | | | | w.length | nm | | | counts | $n$ | | | cps | $n\,s^{-1}$ | | | irrad.mult |$J\,m^{-2}\,nm^{-1}\,n^{-1}$ | | | s.e.irrad | $W\,m^{-2}\,nm^{-1}$ | time.unit = "second" | | s.e.irrad | $J\,m^{-2}\,d^{-1}\,nm^{-1}$ | time.unit = "day" | | s.e.irrad | varies | time.unit = duration | | s.q.irrad | $mol\,m^{-2}\,s^{-1}\,nm^{-1}$ | time.unit = "second" | | s.q.irrad | $mol\,m^{-2}\,d^{-1}\,nm^{-1}$ | time.unit = "day" | | s.q.irrad | $mol\,m^{-2}\,nm^{-1}$ | time.unit = "exposure" | | s.q.irrad | varies | time.unit = duration | | Tfr | [0,1] | Tfr.type = "total" | | Tfr | [0,1] | Tfr.type = "internal" | | A | a.u. | Tfr.type = "internal" | | Rfr | [0,1] | Rfr.type = "total" | | Rfr | [0,1] | Rfr.type = "specular" | | s.e.response | $\mathit{x}\,J^{-1}\,s^{-1}\,nm^{-1}$ | time.unit = "second" | | s.e.response | $\mathit{x}\,mol^{-1}\,d^{-1}\,nm^{-1}$ | time.unit = "day" | | s.e.response | $\mathit{x}\,J^{-1}\,nm^{-1}$ | time.unit = "exposure" | | s.e.response | varies | time.unit = duration | | s.q.response | $\mathit{x}\,mol^{-1}\,s^{-1}\,nm^{-1}$ | time.unit = "second" | | s.q.response | $\mathit{x}\,mol^{-1}\,d^{-1}\,nm^{-1}$ | time.unit = "day" | | s.q.response | $\mathit{x}\,mol^{-1}\,nm^{-1}$ | time.unit = "exposure" | | s.q.response | varies | time.unit = duration | | x, y, z | [0,1] | | | B: converted | | | | wl $\to$ w.length | nm | | | wavelength $\to$ w.length | nm | | | Tpc $\to$ Tfr | [0,100] | Tfr.type = "total" | | Tpc $\to$ Tfr | [0,100] | Tfr.type = "internal" | | Rpc $\to$ Rfr | [0,100] | Rfr.type = "total" | | Rpc $\to$ Rfr | [0,100] | Rfr.type = "specular" | | counts.per.second $\to$ cps | $n\,s^{-1}$ | |

Energy irradiance is assumed to be expressed in $W\,m^{-2}$ and photon irradiances in $mol^{-1}\,m^{-2}\,s^{-1}$, that is to say using second as unit for time. This is the default, but it is possible to set the unit for time to day in the case of source_spct objects.

The default time unit used is second, but day and exposure can be used by supplying the arguments "day" or "exposure"---The meaning of "exposure" is the total exposure time, in other words, fluence instead of irradiance---to a parameter of the constructor of source_spct objects. In addition to these character constants objects of class lubridate:duration are also accepted.

The attributes are normally set when a spectral object is created, either using default values or with values supplied as arguments to the constructor. However, methods for querying and setting most of these attributes are also available.

Not respecting the expectations about data inputs or setting erroneous values in the metadata attributes will yield completely wrong results if calculations are attempted! It is extremely important to make sure that the wavelengths are in nanometres as this is what all functions expect. If wavelength values are in the wrong units, the action-spectra weights and quantum to/from energy units conversions will be wrongly calculated, and the values returned by most functions wrong, without warning. Errors in some cases will be triggered at the time of object creation as the data input to constructors is tested to be within the expected range of values, which in the case of some quantities frequently allows detection of mistakes in the use unit scaling factors._

If spectral irradiance data is in $W\,m^{-2}\,nm^{-1}$, and the wavelength in nm, as is the case for many Macam spectroradiometers, the data can be used directly and functions in the package will return irradiances in $W\,m^{-2}$.

If, for example, the spectral irradiance data output by a spectroradiometer is expressed in $mW\,m^{-2}\,nm^{-1}$, and the wavelengths are in Ångstrom then to obtain correct results when using any of the packages in the suite, we need to rescale the data when creating a new object.

# not run
my.spct <- source_spct(w.length = wavelength/10, s.e.irrad = irrad/1000)

In the example above, we take advantage of the behaviour of the S language: an operation between a scalar and a vector, is equivalent to applying this operation to each element of the vector. Consequently, in the code above, each value from the vector of wavelengths is divided by 10, and each value in the vector of spectral irradiances is divided by 1000.

Querying the class

Before giving examples of how to construct objects to store spectral data we show how to query the class of an object, and how to query the class of a spectrum. Consistently with R, the package provides is functions for querying the type of spectra objects. The only unusual function name, defined as a synomin for is.generic_spct, is is.any_spct().


In addition function class_spc() returns directly the spectrum-related class attributes. It filters out from the output of class() the underlying inherited classes.



There are basically two different approaches to the creation of spectra by users. The first group are constructors similar to the data.frame() constructor, which take vectors as arguments. The second group are constructors that convert list objects, and because of class derivation also data.frame objects, into spectral objects. Constructors in this second group are similar to from base R. In contrast to the data frame constructors, spectral object constructors require the variables or the vector arguments to be suitably named so that they can be recognized.

Here we briefly describe the as constructor functions for spectra. In the first example we create an object to store spectral irradiance data for a fictitious light source, by first creating a data frame, and creating the spectral object as a copy of it. In the example below we supply a single value, 1, for the spectral irradiance. This value gets recycled as is normal in R, but of course in real use it is more usual to supply a vector of the same length as the w.length vector.

my.df <- data.frame(w.length = 400:410, s.e.irrad = 1)
my.spct <- as.source_spct(my.df)

We can make a `generic_spct' copy of any spectrum object.

my.g.spct <- as.generic_spct(my.spct)

When constructing spectral objects from numeric vectors the names of the arguments are meaningful and convey information on the nature of the spectral data and basis of expression.

source_spct(w.length = 300:305, s.e.irrad = 1)
z <- 300:305
y <- 2
source_spct(w.length = z, s.e.irrad = y)

When argument names are not supplied explicitly as above, the names of the variables are used to identify the data vectors.

w.length <- 300:305
s.e.irrad <- 1
source_spct(w.length, s.e.irrad)

The different constructors have additional arguments to be used in setting non-default values for the attributes. These arguments have the same name as the attributes. Here we used the data frame created in the first code chunk of this section.

my.d.spct <- as.source_spct(my.df, time.unit = "day")

Argument strict.range can be used to override or make more strict the validation of the data values.

source_spct(w.length = 300:305, s.e.irrad = -1)
source_spct(w.length = 300:305, s.e.irrad = -1, strict.range = NULL)

Finally argument comment can be used to add a comment to the data at the time of construction. <- source_spct(w.length = 300:305, s.e.irrad = 1,
                          comment = "This is a comment")

Metadata attributes

Spectral objects have several attributes used to store metadata, such as the time unit used or type of spectral quantity. Some attributes are meaningful for all the classes of spectra defined in the package. These are time of measurement using attribute "when.measured", place of measurement using attribute "where.measured", a user supplied label using attribute "what.measured", and free-text comments. One can set and get comments stored in spectra by means of base R's comment() and comment() <- functions. Some of the functions in this package append additional information to comments or merge comments.

Functions setWhenMeasured() and getWhenMeasured() are used for setting or getting a date as a POSIXct value. This format is compatible with many functions from package lubridate.

my.spct <- sun.spct
setWhenMeasured(my.spct, NULL)
setWhenMeasured(my.spct, ymd_hms("2015-10-31 22:55:00", tz = "EET"))

Functions setWhereMeasured() and getWhereMeasured() are used for setting or getting a geocode stored in a data.frame. This format is compatible with function geocode() from package ggmap. We pass latitude and longitude coordinates, as shown below. The returned value is always a data frame with columns "lon" and "lat".

setWhereMeasured(my.spct, NULL)
setWhereMeasured(my.spct, lat = 60, lon = -10)

Functions setWhatMeasured() and getWhatMeasured() are used for setting or getting a text value.

setWhatMeasured(my.spct, "something")

Similar functions exist for other attributes which are not shared by all spectral classes. Several other attributes of different spectral objects can be used to store metadata in a consistent way. One example is the time unit used to express spectral irradiance. Functions are available for querying and setting the state if these attributes. is_ functions return a logical value, and get functions return the values of the attributes themselves. In addition set functions can be used to set the value attributes. Several of the set functions are very rarely needed in user code, as these attributes are set during construction or as a side effect of applying other functions and/or operators to the objects. Function setBSWFUsed() and other set functions for attributes are examples of those useful to programmers extending the package, but only exceptionally to users. One exception is the case when a wrong value has been assigned by mistake and needs to be overwritten.

For example function is_effective() returns TRUE if the spectral data has been weighted with a BSWF. The corresponding getBSWFUsed() function can be used, in this case to retrieve the name of the BSWF that was used when generating the data. Here we demonstrate with one example, where we use two different waveband objects---constructed on-the-fly with constructor function---, defining a range of wavelengths.

is_effective(sun.spct * waveband(c(400, 700)))

Sometimes it may be desired to change the time unit used for expressing spectral irradiance or spectral response, and this can be achieved with the conversion function convertTimeUnit. This function both converts spectral data to the new unit of expression and sets the time.unit attribute, preserving the validity of the data object.

ten.minutes.spct <-
  convertTimeUnit(sun.spct, time.unit = duration(10, "minutes"))

Attributes instr_desc and instr_settings are used to store measurement-related metadata, describing the instrument used and its settings. These attributes are lists, with a few default fields and possibly unlimited special attributes. The present package provides functions for operating on them and print-outs include some of the default fields if the attribute is set. The expectation is that these attributes are set by other packages such as 'ooacquire' used for direct data acquisition or raw instrument data import.

Spectral objects created with earlier (pre-release) versions of this package are missing some attributes. For this reason summary and plot functions may not work as expected with them. These old objects can be updated by adding the missing attribute using functions setTimeUnit, setBSWFUsed, setTfrType and setRfrType. However, in many cases function update_spct can be used to set the missing attributes to default values, or the scripts re-run to rebuild the data objects from raw data.

Collections of spectra


The package defines several classes intended to be used to store collections of different types of spectral data. They are all derived from generic_mspct, which in turn is derived from list. Table 3 lists them.

Table 3. Classes for collections of spectral objects. Objects of class generic_mspct can have member objects of any class derived from generic_spct and can be heterogeneous. Attributes can be queried and set with the normal R methods attr and attributes as well as with functions defined in this package. See table 1 for the attributes used in individual member spectra of collections.

| Class name | Class of member objects | Attributes | |:-----------------|:-------------------------------|--------------------| | generic_mspct | generic_spct | names, dim, comment | | calibration_mspct | calibration_spct | names, dim, comment | | raw_mspct | raw_spct | names, dim, comment | | cps_mspct | cps_spct | names, dim, comment | | source_mspct | source_spct | names, dim, comment | | filter_mspct | filter_spct | names, dim, comment | | reflector_mspct | reflector_spct | names, dim, comment | | object_mspct | object_spct | names, dim, comment | | response_mspct | response_spct | names, dim, comment | | chroma_mspct | chroma_spct | names, dim, comment |

Objects of these classes, except for those of class generic_mspct, can contain members belonging to a single class of spectra. Being all other spectral object classes derived from generic_spct, generic_mspct objects can contain heterogeneous collections of spectra. In all cases, there are no restrictions on the lengths, wavelength range and/or wavelength step size, or attributes other than class of the contained spectra. Mimicking R's arrays and matrices, a dim attribute is always present and dim methods are provided. These allows the storage of time series of spectral data, or (hyper)spectral image data, or even higher dimensional spectral data. The handling of 1D and 2D spectral collections is already implemented in the summary methods. Handling of 3D and higher dimensional data can be implemented in the future without changing the class definitions. By having implemented dim, also methods ncol and nrow are available as they use dim internally. Array-like subscripting is not implemented.



We can construct a collection using a list of spectral objects as a starting point, in this case the spectral irradiance for sunlight.

two_suns.mspct <- source_mspct(list(sun1 = sun.spct, sun2 = sun.spct))

We can also create heterogeneous collections, but this drastically limits the methods that are applicable to the resulting collection.

mixed.mspct <- generic_mspct(list(filter = clear.spct, source = sun.spct))
lapply(mixed.mspct, class_spct)

Using as. functions

The as. coercion methods for collections of spectra, not only change the class of the collection object, but also apply the corresponding {as} functions to the member objects. They copy the original objects and then convert the copy, which is returned.

two_gen.mspct <- as.generic_mspct(two_suns.mspct)
str(two_gen.mspct, max.level = 1, give.attr = FALSE)

In addition to coercion methods for lists of spectra objects, coercion methods are available for lists of data frames (or tibbles) and from matrix objects.

One additional feature is that if a single spectrum object or data frame are coerced into a collection of spectra, the behaviour is equivalent to having passed as argument a list containing such object as its only member.

one_sun.mspct <- as.source_mspct(sun.spct)
str(one_sun.mspct, max.level = 1, give.attr = FALSE)

Sometimes spectral data stored in a matrix need to be coerced into a collection of spectra. Coercion methods are defined also for this cases. Several spectra may be stored in a matrix either by row or by column, but this can be deduced automatically in the case of rectangular matrices. Wavelengths values are not expected to be part of the matrix, and need to be supplied as a separate numeric vector sorted in ascending order. As in a spectrum wavelength values never repeat, the vector of wavelengths is never recycled and uniqueness of values is enforced.

We here use artificial data, in this first example with spectra saved by column. We assume that the values in the matrix are spectral transmittance stored as percentages (hence "Tpc").

x <- matrix(1:100, ncol = 2)
wl <- 501:550 # wavelengths in nanometres
as.filter_mspct(x, wl, "Tpc")

In a second example we supply explicit names for the spectra.

as.filter_mspct(x, wl, "Tpc", spct.names = c("A", "B"))

There is no change in the call for data stored by row. We create a new matrix, with the same data as above, but stored by row, as some R packages do.

xrow <- matrix(1:100, nrow = 2, byrow = TRUE)
as.filter_mspct(xrow, wl, "Tpc")

There is only one case when an explicit argument for byrow is needed: square matrices (same number of spectra as of wavelength values in each spectrum).

When coercing collections of spectra into matrices, the metadata contained in the individual spectral objects is discarded, and only the "comment" attribute of the collection of spectra copied to the returned object. The wavelength values are preserved in an attribute named "w.length", but are not included as part of the matrix.

two_suns.mat <- as.matrix(two_suns.mspct, "s.e.irrad")
head(attr(two_suns.mat, "w.length"))

The argument byrow in the coercion into matrix methods has the same meaning as in the matrix constructor function.

two_suns.row_mat <- as.matrix(two_suns.mat, "s.e.irrad", byrow = TRUE)
head(attr(two_suns.row_mat, "w.length"))

Collections of spectra have a dim attribute but it is discarded as it describes dimensions that could require spectral data to be stored in a three dimensional array and cannot be mapped to the two dimensions of a matrix.

These functions are not fully automatic, the user needs to provide the name of the variable to extract from each spectrum. If the wavelength values are not consistent among spectra, only those with the same values as the first spectrum in the collection are retained and the remaining ones dropped with a warning.

Converting tidy data

Spectral objects containing multiple spectra identified by a factor can be created by row-binding compatible spectral objects. For for binding to succeed, the list of spectra passed as argument must be homogeneous with respect to member class, as well as for certain attributes such as time.unit. Other metadata attributes are retained as named lists.

two_suns.spct <- rbindspct(list(a = sun.spct, b = sun.spct / 2))

The reverse operation, separating the individual spectra from a spectrum object containing multiple spectra in long form and storing them as a collection, is implemented in method subset2mspct. In this case, metadata is set for the individual spectra if it is available in the object being subset. What metadata is automatically stored depends on the version of this package used when creating the long form spectrum object by means of rbindspct(). Versions 0.9.14 and later preserve most of the metadata.


If multiple spectra are stored in long form (as tidy data) in an ordinary data.frame or a tibble object with columns with suitable names, the same subset2mspct method can be used. In these case, additional arguments need to be passed indicating the target class for conversion. The name of the index variable encoding the grouping into multiple spectra will also need to be supplied as an argument. A call as in the example below, adds only default metadata to spectral objects.

test1.df <- data.frame(w.length = rep(200:210, 2),
                       s.e.irrad = rep(c(1, 2), c(11, 11)),
                       spectrum = factor(rep(c("A", "B"), c(11,11))))
subset2mspct(test1.df, member.class = "source_spct", idx.var = "spectrum")

If all member spectra share the same metadata, and the constructor has a parameter allowing it to be set, it can be passed as a named argument.

subset2mspct(test1.df, member.class = "source_spct", idx.var = "spectrum",
             time.unit = "day")

To directly convert a tidy data frame into a long form spectral object we need to pass the number of spectra through parameter multiple.wl to override the usual check for unique wavelength values (default is multiple.wl = 1L).

test2.df <- test1.df
setSourceSpct(test2.df, multiple.wl = 2L)

If multiple.wl = NULL a suitable value is guessed from the data frame passed as first argument. This should work in most cases, but is more time consuming. Using this approach in addition disables the check for a fixed number of spectra.

test3.df <- test1.df
setSourceSpct(test3.df, multiple.wl = NULL)

Converting untidy data frames

In a broad form (or untidy) data.frame the spectral values for different spectra are stored side-by-side as columns, and a single additional column used to store the shared wavelength values. To create a collection of spectral objects, with each member containing a single spectrum, we use function split2source_mspct and its equivalents for the remaining classes of spectral objects (class is determined by the function used, and columns which are not numeric are skipped). The column containing wavelength values in nanometres is recognized by means of its name. The names used for the spectra in the collection are derived from the names of the columns containing spectral values.

test2.df <- data.frame(w.length = 200:210, A = 1, B = 2, z = "A")
split2source_mspct(test2.df, = "s.q.irrad")

Also in this case, it is possible to pass additional named arguments to the constructor of spectral objects.

split2source_mspct(test2.df, = "s.q.irrad", time.unit = "day")

Conversion into a "wide" data frame

Sometimes, when exporting spectral data we may need to convert a collection of spectra into an untidy or wide form data frame. In the example below we use a collection with only two member spectra, but join_mspct() can handle collections of any length.

my.mspct <- source_mspct(list(sun1 = sun.spct, sun2 = sun.spct * 2))
my.df <- join_mspct(my.mspct)

Querying the class

is. functions are defined for all the new classes. R's class method can also be used.


In addition to using class to query the class of the collection, we can use base R's lapply together with class or class_spct to query the class of each of the members of the collection.

lapply(mixed.mspct, class_spct)
lapply(mixed.mspct, class)

Extract, replace and combine

R's extraction and replacement methods have specializations for collections of spectra and can be used with the same syntax and functionality as for R lists. However they test the class and validity of the returned objects and replacement members.

Methods [, and [<-, extract and replace parts of the collection, respectively. Even when only one member is extracted, the returned value is a collection of spectra. The expected replacement value is also, always a collection of spectra.

# not run as this triggers an error when building the vignette with 'devtools'

Avoid code like that in the chunk bellow. Collections of spectra are named lists, consequently assigning by position as shown here swaps the values without swapping the names of the slots!

# not run as this triggers an error when building the vignette with 'devtools'
two_suns.mspct[1:2] <- two_suns.mspct[2:1]

Methods [[, $ and [[<-, extract and replace individual members of the collection, respectively. They always return or expect objects of one of the spectral classes.

two_suns.mspct[["sun1"]] <- sun.spct * 2
two_suns.mspct[["sun2"]] <- NULL

As with R's list objects, can combine collections of spectra with method c() but we can not create new collections from individual spectra using this method.

c(two_suns.mspct, mixed.mspct)

Transform or apply functions

For our apply functions we follow the naming convention used in package plyr, but using ms as prefix for _mspct objects. The apply functions implemented in the 'photobiology' package are msmsply, msdply, mslply and msaply which both accept a collection of spectra as first argument and return a collection of spectra, a data frame, a list, or an array respectively (see Table 4).

Table 4. Apply functions for collections of spectra. Key: v., value returned by apply function; f.v., value returned by the applied function (argument .fun). In the table generic_mspct and generic_spct indicate objects of these classes or any class derived from them. The exact class of the collection of spectra object returned will be determined by the class(es) of the values returned by the applied function.

| apply function | first arg. class | v. class | f.v. class | f.v. length | f.v. dims | |:-------------|:--------|:-------|:-----|:-----|:----| | msmsply | generic_mspct | generic_mspct | generic_spct | 1 | any | | msdply | generic_mspct | data.frame | numeric | $1\ldots n$ | 1 | | mslply | generic_mspct | list | any | any | any | | msaply | generic_mspct | vector | any simple | 1 | 0 | | msaply | generic_mspct | matrix | any simple | $2\ldots n$ | $2\ldots n$ | | concolve_each | generic_mspct | generic_mspct | generic_spct | 1 | any |

Functions msmsply(), msdply and mslply can be used to apply a function to each member spectrum in a collection. The apply function to use depends on the return value of the applied function.

In the case of msmsply() the applied function is expected to return a transformed spectrum as another object of class generic_spct or a class derived from it. The value returned by msmsply is a collection of spectra, of a type determined by the class(es) of the member spectra in the new collection.

We start with a simple example in which we add a constant to each spectrum in the collection

two.mspct <- source_mspct(list(A = sun.spct * 1, B = sun.spct * 2))
msmsply(two.mspct, `+`, 0.1)

and continue with a more complex example in which we trim each spectrum

msmsply(two.mspct, trim_wl, range = c(281, 500), fill = NA)

In the second example we pass two arguments by name to the applied function. The number of arguments is not fixed, but the spectrum will be always passed as the first argument to the function.

In the case of msdply() the applied function is expected to return an R object of the same length for each of the member spectra.

msdply(two.mspct, max)
ranges.df <- msdply(two.mspct, range)
msdply(two.mspct, range, na.rm = TRUE)

In the case of mslply() the applied function is expected to return an R object of any length, possibly variable among members.

str(mslply(two.mspct, names))

In the case of msaply() the applied function is expected to return an R object of length 1, although a list with dimensions will be returned for longer return values.

str(msaply(two.mspct, max))
str(msaply(two.mspct, range))

For the most common cases for which one would use the apply functions described in the previous section methods and functions for operations on collections of spectra are defined in the package. For example the following statement is equivalent to the second example in the previous section. These are described in a later section and listed in Table 9.


By convolution we normally mean the multiplication value by value at matching wavelengths of two spectra. The function described in this section facilitates this and similar operations among collections of spectra. An example use case could be the convolution of spectral irradiance by spectral transmittance for all combinations of light sources and filters in a collection of source spectra and a collection of filter spectra.

Default operator (or function) is that for multiplication, either one or both of the two first arguments must be a collection of spectra. When only one argument is a collection of spectra, the other one can be a spectrum, or even a numeric vector. For multiplication the order of the operands does not affect the returned value. With operators or functions for non-transitive operations the order does matter.

convolve_each(two.mspct, sun.spct)
convolve_each(sun.spct, two.mspct)
another_two.mspct <- two.mspct
names(another_two.mspct) <- c("a", "b")
convolve_each(another_two.mspct, two.mspct)

The function convolve_each will use other operators or functions and even pass additional named arguments when these are supplied as arguments.

convolve_each(two.mspct, sun.spct, oper = `+`)

There are cases where functions convolve_each() and msmsply() can be both used, but there are also cases where their differences matter. An example is convolving two collections of spectra, a case where only convolve_each() can be used. In contrast, when one of the arguments is not a spectrum or a collection of spectra, msmsply() should be used instead.

Metadata attributes

Some of the set and get functions used with attributes have method definitions for collections of spectra. Some examples follow.

setWhenMeasured(two.mspct, ymd("2015-10-31", tz = "EET"))
                list(ymd_hm("2015-10-31 10:00", tz = "EET"),
                     ymd_hm("2015-10-31 11:00", tz = "EET")))

Other methods available are getWhereMeasured and setWhereMeasured, and getWhatMeasured and setWhatMeasured.

Functions when.measured2tb(), geocode2tb(), lon2tb(), lat2tb() and what.measured2tb() extract these same attributes from collection members into a tibble or data frame. In contrast to the "get" methods described above, if an existing data frame or tibble with a matching number of rows is passed as second argument, the values for the attribute are saved into a new column appended at the right edge of the tibble or data frame.


By default the new column is named after the name of the attribute, but this default can be overridden by the user.

when_measured2tb(two.mspct, col.names = c(when.measured = "time"))

Function add_attr2tb() adds one or more columns with attributes to a tibble or data frame. We here also demonstrate that because the data frame object is passed to the first positional argument, we can use a pipe.

q_irrad(two.mspct) %>%
              col.names = c("lon", "lat", "when.measured"))

We can set the desired names for the columns if we wish them to be something else than the default.

q_irrad(two.mspct) %>%
              col.names = c(lon = "longitude", 
                            lat = "latitude", 
                            when.measured = "time"))


When a range of wavelengths or a range of wavelengths plus a spectral weighting function (SWF) is needed for radiation summaries or transformations, methods, operators and functions defined in package 'photobiology' use waveband objects to store these data. A few other bits of information can be included to fine-tune calculations. The waveband definitions do NOT describe whether input spectral irradiances are photon or energy based, nor whether the output irradiance will be based on photon or energy units. All waveband objects belong to the S3 class waveband.


When defining a waveband which uses a SWF, a function can be supplied either based on energy effectiveness, on photon effectiveness, or one function for each one. If only one function is supplied the other one is built automatically, but if performance is a concern it is better to provide two separate functions. Another case when you might want to enter the same function twice, is if you are using an absorptance spectrum as SWF, as the percent of radiation absorbed will be independent of whether photon or energy units are used for the spectral irradiance.

To create a waveband object we use constructor function waveband, and optionally giving a name to it. We will use these objects in many of the examples below, so you will need to run the code chunk bellow to be able to reproduce those examples. It should be noted that waveband constructors for the most frequently used wavelength-range definitions are provided by package 'photobiologyWavebands'.

PAR.wb <- waveband(c(400, 700), = "PAR")
UVA.wb <- waveband(c(315, 400), = "UVA")
UVB.wb <- waveband(c(280, 315), = "UVB")
UVC.wb <- waveband(c(100, 280), = "UVC")
UV.wb  <- waveband(c(100, 400), =  "UV")
UV_bands.lst <- list(UVC.wb, UVB.wb, UVA.wb)

When including a BSWF, we can supply, one or two versions of functions returning the weights as a function of wavelength. Several such functions are defined in package 'photobiologyWavebands' as well as waveband constructors using them. Here we show how a waveband can be defined based on a SWF, using the CIE definition for the erythemal spectral weighting function. Although the constructor is smart enough to derive the missing function when only one function is supplied, performance may suffer unless two performance-optimized function are provided, one for energy-based effect and a second one for photon-based effect.

CIE_e_fun <-
function(w.length){ <- numeric(length(w.length))[w.length <= 298] <- 1[(w.length > 298) & (w.length <= 328)] <-
      10^(0.094*(298-w.length[(w.length > 298) & (w.length <= 328)]))[(w.length > 328) & (w.length <= 400)] <-
      10^(0.015*(139-w.length[(w.length > 328) & (w.length <= 400)]))[w.length > 400] <- 0
CIE.wb <- waveband(c(250, 400), weight = "SWF",
          = CIE_e_fun, SWF.norm = 298)

The first argument to waveband() does not need to be a numeric vector of length two. Any R object of a class that supplies a range() method definition that can be interpreted as a range of wavelengths in nanometres can be used. As a consequence, when wanting to construct a waveband covering the whole range of a spectrum one can simply supply the spectrum as argument, or to construct an non-weighed waveband which covers exactly the same range of wavelengths as an existing effective (weighted) waveband, one can supply a waveband object as an argument.


An "empty" waveband is returned by some functions as a "null" value.


Querying the class

The function is.waveband can the used to query any R object. This function returns a logical value.


Above, we demonstrate that PAR.wb is a waveband object, the function photobiologyWavebands::PAR() is a waveband constructor returning a waveband object. See package 'photobiologyWavebands' for details on pre-defined waveband constructors for frequently used wavelength ranges and biological spectral weighting functions (BSWFs).

Retrieving properties

The function is_effective can the used to query any R object.


Collections of wavebands

In the current implementation there is no special class used for storing collections of waveband objects. We simply use base R's list class.


List constructor

Just base R's functions used to create a list object.

wavebands <- list(waveband(c(300,400)), waveband(c(400,500)))

Special constructor

The function split_bands can be used to generate lists of non-weighed wavebands in two different ways: a) it can be used to split a range of wavelengths given by an R object into a series of adjacent wavebands, or b) with a list of objects returning ranges, it can be used to create non-adjacent and even overlapping wavebands.

The code chunk bellow shows an example of two variations of case a). With the default value for length.out of NULL each numerical value in the input is taken as a wavelength (nm) at the boundary between adjacent wavebands. If a numerical value is supplied to length.out, then the whole wavelength range of the input is split into this number of equally spaced adjacent wavebands.

split_bands(c(200, 225, 300))
split_bands(c(200, 225, 300), length.out = 2)

In both examples above, the output is a list of two wavebands, but the split boundaries are at a different wavelength. The chunk bellow gives a few more examples of the use of case a).

split_bands(sun.spct, length.out = 2)
split_bands(PAR.wb, length.out = 2)
split_bands(c(200, 800), length.out = 3)

Now we demonstrate case b). This case is handled by recursion, so each list element can be anything that is a valid input to the function, including a nested list. However, the returned value is always a flat list of wavebands.

split_bands(list(A = c(200, 300), B = c(400, 500), C = c(250, 350)))
split_bands(list(c(100, 150, 200), c(800, 825)))

In case b) if we supply a numeric value to length.out, this value is used recursively for each element of the list.

split_bands(UV_bands.lst, length.out  =  2)
split_bands(list(c(100, 150, 200), c(800, 825)), length.out = 1)

Object inspection methods


The print() method for spectra is based on the method defined in package 'tibble', consequently, it is possible to use the options from this package to control printing. In the code chunk below, tibble.print_max, the number of rows in the spectral object above which only tibble.print_min rows are printed, are both set to 5, instead of the default 20 and 10, to avoid excessive clutter in our examples.

options(tibble.print_max = 4)
options(tibble.print_min = 4)

For explicit calls to print() its argument n can be used to control the number of lines printed. If n is set to Inf the whole spectrum is always printed. The output differs from that of the print() method from package 'dplyr' in that additional metadata specific to spectra are shown.

print(sun.spct, n = 3)

Specialized print() methods for collections of spectra and for waveband objects are also defined.


The summary() method for spectra is based on base R's summary() method for data frames, and accepts the same arguments. The main difference is that the attributes containing metadata and dimensions of the original spectrum object are copied to the summary object.


Specialized print() methods for summaries of spectra are defined. The output differs from that of the print() method from base R in that additional metadata specific to spectra are shown.

Handling NAs

Functions na.omit() and na.exclude are implemented for all spectral classes. These methods test for NAs only the spectral data, not wavelength. They set the "na.action" attribute in the same way as the corresponding methods for data frames. In the case of, na.pass() and na.action(), the methods from base R, can be used with spectra.


Transformations: using operators

Binary operators

All of R's built-in maths operators have definitions for spectra. It is possible to sum, subtract, multiply and divide spectra. These operators can be used even if the spectral data is on different arbitrary sets of wavelengths. Operators by default return values expressed in energy units. Only certain operations are meaningful for a given combination of objects belonging to different classes, and meaningless combinations return NA also issuing a warning (see Table 5). By default operations are carried out on spectral energy irradiance for source_spct objects and transmittance for filter_spct objects.

Table 5. Binary operators and their operands. Validity and class of result. All operations marked \Y' are allowed, those marked\N' are forbidden and return NA and issue a warning. Operators %/% and %% follow /.

| e1 | + | - | * | / | ^ | e2 | value | |:--------|:--:|:--:|:--:|:--:|:--:|:------------|:---------------| | raw_spct | Y | Y |Y | Y | Y | raw_spct | raw_spct | | cps_spct | Y | Y |Y | Y | Y | cps_spct | cps_spct | | source_spct | Y | Y |Y | Y | Y | source_spct | source_spct | | filter_spct (T) | N | N | Y | Y | N | filter_spct | filter_spct | | filter_spct (A) | Y | Y | N | N | N | filter_spct | filter_spct | | reflector_spct | N | N | Y |Y | N | reflector_spct | reflector_spct | | object_spct | N | N | N | N |N | object_spct | -- | | response_spct | Y | Y | Y | Y | N | response_spct | response_spct | | chroma_spct | Y | Y | Y | Y | Y | chroma_spct | chroma_spct | | raw_spct | Y | Y |Y | Y | Y | numeric | raw_spct | | cps_spct | Y | Y |Y | Y | Y | numeric | cps_spct | | calibration_spct | Y | Y |Y | Y | Y | numeric | calibration_spct | | source_spct | Y | Y |Y | Y | Y | numeric | source_spct | | filter_spct | Y |Y | Y |Y | Y | numeric | filter_spct | | reflector_spct | Y |Y |Y |Y | Y | numeric | reflector_spct | | object_spct | N | N | N | N |N | numeric | -- | | response_spct | Y | Y | Y | Y | Y | numeric | response_spct | | chroma_spct | Y | Y | Y | Y | Y | numeric | chroma_spct | | cps_spct | N | N | Y | N | N | calibration_spct | source_spct | | source_spct | N | N | Y |Y | N | response_spct | response_spct | | source_spct | N | N | Y | Y | N | filter_spct (T) | source_spct | | source_spct | N | N | Y | Y | N | filter_spct (A) | source_spct | | source_spct | N | N | Y | Y | N | reflector_spct | source_spct | | source_spct | N | N | N | N | N | object_spct | -- | | source_spct | N | N | Y | N | N | waveband (no BSWF) | source_spct | | source_spct | N | N | Y | N | N | waveband (BSWF) | source_spct (effective) |

sun.spct * sun.spct

When meaningful, operations between different spectra are also allowed. For example, it is possible to simulate the effect of a filter on a light source by multiplying (or convolving) the two spectra.

sun.spct * polyester.spct

If we have two layers of the filter, this can be approximated using either of these two statements.

sun.spct * polyester.spct * polyester.spct
sun.spct * polyester.spct^2

Operators are also defined for operations between a spectrum and a numeric vector (with normal recycling).

sun.spct * 2
2 * sun.spct
sun.spct * c(0,1)

There is one special case, for chroma_spct: if the numeric operand has length three, containing three named values x, y and z, the corresponding value is used for each of the chromaticity columns in the chroma_spct. Un-named values or differently named values are not treated specially.

Operators are also defined for operations between an spectrum and a waveband object. The next to code chunks demonstrate how the class of the result depends on whether the waveband object describes a range of wavelengths or a range of wavelengths plus a BSWF.

sun.spct * UVB.wb
sun.spct * CIE.wb

And of course these operations can be combined into more complex statements, including parentheses, when needed. The example below estimates the difference in effective spectral irradiance according to the CIE98 definition, between sunlight and sunlight filtered with a polyester film. Of course, the result is valid only for the solar spectral data used, which corresponds to Southern Finland.

sun.spct * CIE.wb - sun.spct * polyester.spct * CIE.wb

Unary operators and maths functions

Many common maths functions, as well as unary minus and plus, are implemented for spectral objects (see Table 6).

Table 6. Unary operators and maths functions for spectra. Classes for which they are implemented and class of the result. All operations marked Y are allowed, those marked N are not implemented and return NA and issue a warning. Additional supported functions: log2, log10, sin, cos, tan, asin, acos, atan, sinpi, cospi, tanpi, signif, floor, ceiling, trunc, sign, abs.

| e1 | +, - | log, exp | trig. | round | sqrt | value | |:-----|:--:|:--:|:--:|:--:|:--:|:-------| | raw_spct | Y | Y |Y | Y | Y | raw_spct | | cps_spct | Y | Y |Y | Y | Y | cps_spct | | calibration_spct | Y | Y |Y | Y | Y | calibration_spct | | source_spct | Y | Y |Y | Y | Y | source_spct | | filter_spct | Y | Y | Y | Y | Y | filter_spct | | reflector_spct | Y | Y | Y |Y | Y | reflector_spct | | object_spct | N | N | N | N | N | -- | | response_spct | Y | Y | Y | Y | Y | response_spct | | chroma_spct | Y | Y | Y | Y | Y | chroma_spct |



Table 7 lists all the recognized options affecting maths operators and functions, and their default values. Within the suite all functions have a default value which is used when the options are undefined. Options are set using base R's function options, and queried with functions options and getOption.

Table 7. Options used in the 'r4photobiology suite' and recognized by methods, operators and functions in the 'photobiology' package. Option names, accepted and default values, and the purpose of the options are given. Option photobiology.verbose is set to the value of R's own verbose option at the time the 'photobiology' package is attached to the session.

| Option | values, default | purpose, unit | convenience function |:------|:---------------------|:------------------------------------|------------------| | Base R | | | | | digits | 7 | $d - 3$ used by summary | | | tibble | | | | | tibble.print_max | 20 | Maximum number of rows printed | | | tibble.print_min | 10 | Number of rows printed if row number threshold is exceeded | | | tibble.width | NULL | Output width | | r4photobiology | | | | | photobiology.radiation.unit | "energy" | $W\,m^{-2}\,nm^{-1}$ | using_energy(), energy_as_default() | | | "photon" |$\mu mol\,m^{-2}\,nm^{-1}$ | using_photon(), photon_as_default() | | photobiology.filter.qty | "transmittance" | $/1$ | using_Tfr(), Tfr_as_default() | | | "absorptance" | $/1$ | using_Afr(), Afr_as_default() | | | "absorbance" | a.u. $\log_{10}$ base | using_A(), A_as_default() | | photobiology.strict.range | NA | skip range test | strict_range_as_default(NA) | | | TRUE | trigger an error | strict_range_as_default(TRUE) | | | FALSE | trigger a warning | strict_range_as_default(FALSE) | | photobiology.waveband.trim | FALSE | exclude | wb_trim_as_default(FALSE) | | | TRUE | trim or exclude | wb_trim_as_default(TRUE) | | photobiology.use.cached.mult | FALSE | do not cache intermediate results | use_cached_mult_as_default(FALSE) | | | TRUE | cache intermediate results | use_cached_mult_as_default(TRUE) | | photobiology.verbose | FALSE | minimal warnings and messages | verbose_as_default(FALSE) | | | TRUE | all warnings and messages | verbose_as_default(TRUE) |

The behaviour of the operators defined in this package depends on the value of two global options. For example, if we would like the operators to operate on spectral photon irradiance and return spectral photon irradiance instead of spectral energy irradiance, this behaviour can be set, and will remain active until unset or reset. It is possible to change options using base R's function options() as shown in the next code chunk. The other options listed in the Table above can be set similarly, while to unset any option, they can be given a NULL value.

options(photobiology.radiation.unit = "photon")
sun.spct * UVB.wb
options(photobiology.radiation.unit = "energy")
sun.spct * UVB.wb

However using convenience functions is easier. The chunk above can be rewritten as below.

sun.spct * UVB.wb
sun.spct * UVB.wb

Furthermore, it is possible to temporarily change the options for the evaluation of a single, possibly compound, R expression using a different syntax, reminiscent of that of R's with().

using_photon(sun.spct * UVB.wb)
using_energy(sun.spct * UVB.wb)

Transformations: methods and functions

In this section we describe methods and functions that take one or more spectral objects, and in some cases also waveband objects, as arguments and return another spectral object (see Tables 8 and 9) or that take a collection of spectral objects, and in some cases also waveband objects, as arguments and return a collection of spectral objects.

Table 8. Transformation methods for spectra. Key: + available, -- not available.

| methods | raw/cps | source | response | filter | reflector | object | chroma | |:--------|:---:|:---:|:---:|:---:|:---:|:---:|:---:| | merge | + | + | + | + | + | + | + | | rbindspct | + | + | + | + | + | + | + | | e2q, q2e | -- | + | + | -- | -- | -- | -- | | A2T, T2A | -- | -- | -- | + | -- | -- | -- | | subset | + | + | + | + | + | + | + | | clip_wl | + | + | + | + | + | + | + | | trim_wl | + | + | + | + | + | + | + | | (trim_spct) | + | + | + | + | + | + | + | | interpolate_wl | -- | + | + | + | + | + | + | | (interpolate_spct) | -- | + | + | + | + | + | + | | fscale | + | + | + | + | + | -- | -- | | fshift | + | + | + | + | + | -- | -- | | normalize | + | + | + | + | + | -- | -- | | clean | -- | + | + | + | + | -- | -- | | maths operators | + | + | + | + | + | + | + | | maths functions | + | + | + | + | + | + | + | | tag | -- | + | + | + | + | + | + | | untag | -- | + | + | + | + | + | + |

Table 9. Transformation methods for collections of spectra. Key: + available, -- not available, ms use msmsply() or convolve_each() to apply function or operator to collection members.

| methods | raw/cps | source | response | filter | reflector | object | chroma | |:--------|:---:|:---:|:---:|:---:|:---:|:---:|:---:| | convolve_each | -- | + | + | + | + | + | + | | msmsply | + | + | + | + | + | + | + | | msdply | + | + | + | + | + | + | + | | mslply | + | + | + | + | + | + | + | | msaply | + | + | + | + | + | + | + | | rbindspct | + | + | + | + | + | + | + | | c | + | + | + | + | + | + | + | | maths operators | ms | ms | ms | ms | ms | ms | ms | | maths functions | ms | ms | ms | ms | ms | ms | ms | | e2q, q2e | -- | + | + | -- | -- | -- | -- | | A2T, T2A | -- | -- | -- | + | -- | -- | -- | | clip_wl | + | + | + | + | + | + | + | | trim_wl | + | + | + | + | + | + | + | | trim2overlap | + | + | + | + | + | + | + | | extend2extremes | + | + | + | + | + | + | + | | (trim_mspct) | + | + | + | + | + | + | + | | interpolate_wl | -- | + | + |+ | + | + | + | | (interpolate_mspct) | -- | + | + |+ | + | + | + | | fscale | + | + | + | + | + | -- | -- | | fshift | + | + | + | + | + | -- | -- | | normalize | + | + | + | + | + | -- | -- | | clean | -- | + | + | + | + | -- | -- | | tag | -- | + | + | + | + | + | + | | untag | -- | + | + | + | + | + | + |

Manipulating spectra

Sometimes, especially for plotting, we may want to row-bind spectra. When the aim is that the returned object retains its class and other attributes like the time unit. Package 'photobiology' provides function rbinspct for row-binding spectra, with the necessary checks for consistency of the bound spectra.

shade.spct <- sun.spct

By default an ID factor named spct.idx is added allow to identify the source of the observations after the binding. If the supplied list has named members, then these names are used as factor levels. If a character value is supplied to as idfactor argument, this is used as name for the factor.

rbindspct(list(sun.spct, shade.spct))
rbindspct(list(A = sun.spct, B = shade.spct), idfactor = "site")

The name of the ID factor is stored as metadata in attribute "idfactor" of the spectral data object.

Special Extract methods for spectral objects have been implemented. These are used by default and preserve the attributes used by this package, except when the returned value is a single column from the spectral object.

sun.spct[1:10, ]
sun.spct[1:10, 1]
sun.spct[1:10, 1, drop = TRUE]
sun.spct[1:10, "w.length", drop = TRUE]

In contrast to trim_spct, subset never interpolates or inserts hinges. On the other hand, the subset argument accepts any logical expression and can be consequently used to do subsetting, for example, based on factors. Both subset() and trim() methods preserve attributes.

subset(sun.spct, s.e.irrad > 0.2)
subset(sun.spct, w.length > 600)
subset(sun.spct, c(TRUE, rep(FALSE, 99)))

R's Extract methods $ and [[ can be used to extract whole columns. Replace methods $<- and [<- have definitions for spectral objects, which allow their safe use. They work identically to those for data frames but check the validity of the spectra after the replacement.

Conversions between radiation units

The functions e2q and q2e can be used on source spectra to convert spectral energy irradiance into spectral photon irradiance and vice versa. A second optional argument sets the action with "add" and "replace" as possible values. By default these functions use normal reference semantics.

e2q(sun.spct, "add")
e2q(sun.spct, "replace")

For filter_spct objects functions T2A and A2T allow conversion between spectral transmittance and spectral absorbance and vice versa.

Normalizing a spectrum

Function normalize permits normalizing a spectrum to a value of one at an arbitrary wavelength (nm) or to the wavelength of either the maximum or the minimum spectral value. It supports the different spectral classes, we use a source_spct object as an example.


Which is equivalent to supplying "max" as argument to norm, it is also possible to give a range within which the maximum should be searched.

normalize(sun.spct, range = PAR.wb, norm = "max")

It is also possible to normalize to an arbitrary wavelength within the range of the data, even if it is not one of the wavelength values present in the spectral object, as interpolation is used when needed.

normalize(sun.spct, norm = 600.3)

The normalization status of a spectral object can be tested with method is_normalized() and the normalization used can be recalled with method getNormalized().

my.spct <- normalize(sun.spct)

Once a spectrum is normalized, summary methods that return values in absolute units such as irrad(), will trigger an error if applied. Ratios and similar summaries that are invariant with respect to normalization can be calculated.

Applying method fscale() removes the normalization and clears the corresponding attribute where the normalization information is stored. This attribute also can be cleared with method setNormalized(), but this is rarely valid or of any use.

Rescaling a spectrum

Function fscale() rescales a spectrum by dividing each spectral data value by a value calculated with a function (f) selected by a character string ("total" or "mean"), or an actual R function which can accept the spectrum object supplied as its first argument. Additional named arguments can be also passed.

fscale(sun.spct, f = "total")
fscale(sun.spct, range = PAR.wb, f = irrad)
fscale(sun.spct, range = PAR.wb, f = q_irrad, target = 800e-6)

In the third example, the spectral data is rescaled so that the corresponding photosynthetically-active irradiance is equal to one.

The normalization status of a spectral object can be tested with method is_normalized() and the normalization used can be recalled with method getNormalized().

my.spct <- fscale(sun.spct)

Once a spectrum is scaled, summary methods that return values in absolute units such as irrad(), will trigger a warning if applied. Ratios and similar summaries that are invariant with respect to normalization do not trigger warnings.

Applying method normalize() removes the scaling and clears the corresponding attribute where the scaling information is stored. This attribute also can be cleared with method setScaled(), and this can be useful is some cases such as when irradiance has been measured separately from the emission spectrum.

Shifting the zero of the spectral data scale

Function fshift() shifts the zero of the scale of a spectrum by subtracting from each spectral data value a value calculated with a function (f) selected by a character string ("mean", "min" or "max"), or an actual R function which can accept the spectrum object supplied as its first argument. The range argument selects a region of the spectrum to be used as reference in the calculation of the summary.

fshift(sun.spct, range = UVB.wb, f = "mean")
fshift(sun.spct, range = c(280,290), f = "min")

In the first example, the spectral data shifted so that the mean spectral irradiance becomes zero for the UV-B region. In the second example the minimum value in the range of wavelengths between 280~nm and 290~nm is used as zero reference for the scale.

Replacing off-range spectral data values

Method clean() should be used with care as off-range values stem almost always from calibration errors or measuring noise. This function allows one to replace such values, but in many cases a zero shift or rescaling could be the option to be preferred. Even when the off-range values are the result of random noise, replacing them with the boundary values can cause bias, by censoring the data. Here we create artificial off-range values by subtracting a constant from each spectrum.

clean(sun.spct - 0.01, range = c(280.5, 282))
clean(polyester.spct - 0.053)

Wavelength interpolation

Converting spectra available at a given set of wavelengths values to a different one, is frequently needed when operating with several spectra of different origin. One can increase the apparent resolution by interpolation, and reduce it by local averaging or smoothing and resampling. The same function works on all spct objects, interpolating every column except w.length and replacing in this last column the old wavelength values with the new ones supplied as argument. The optional argument fill.value control what value is assigned to wavelengths in the new data that are outside the range of the old wavelengths.

interpolate_wl(sun.spct, seq(400, 500, by = 0.1))

Trimming and clipping

Method clip_wl()

Sometimes it is desirable to change the range of wavelengths included in a spectrum. If we are interested in a given part of the spectrum, there is no need to do calculations or plotting the whole spectrum. To select part a spectrum based on a range of wavelengths we may use the clip_wl method.

The range of wavelengths expressed in nanometres can be given as numeric vector of length two.

clip_wl(sun.spct, range = c(400, 402))
clip_wl(sun.spct, range = c(400, NA))

As for trim_wl() the range can be also supplied as a waveband object, or any other object for which range() returns a numeric range. Even a different spectrum object is acceptable.

clip_wl(sun.spct, range = UVA.wb)

The result can be a spectrum of length zero.

clip_wl(sun.spct, range = c(100, 200))

Method trim_wl()

Sometimes, we need more flexibility. We may want to replace the observed values outside a certain range or expand the range of wavelengths, filling the expansion of all other variables with a certain value (i.e. a number, or NA.). In contrast to clipping (or functionally equivalent, indexing, or subsetting), trimming ensures that there will be spectral data returned at the boundaries of the trimmed region. These values are obtained by interpolation when they are not already present in the data.

More flexibility is available in method trim_wl(), to which we can supply arguments range, use.hinges, and fill. By default interpolation is used at the boundaries of the range.

trim_wl(sun.spct, c(282.5, NA))
clip_wl(sun.spct, c(282.5, NA))

As for clip_wl() the range can be also supplied as a waveband object, or any other object for which range() returns a numeric range. Even a different spectrum object is acceptable.

trim_wl(sun.spct, PAR.wb)

The default for fill is NULL which results in deletion values outside the trimmed region. However, it is possible to supply a different argument, to be used to replace the off-range data values.

trim_wl(sun.spct, c(281.5, NA), fill = NA)

Furthermore, when fill is not NULL, expansion is possible.

trim_wl(sun.spct, c(275, NA), fill = 0)

By default interpolation at the boundaries is used, but setting use.hinges to FALSE results in clipping, a behaviour similar to that of clip_wl only if fill == NULL.

trim_wl(sun.spct, c(281.5, NA), fill = NA)
trim_wl(sun.spct, c(281.5, NA), fill = NA, use.hinges = FALSE)

When use.hinges == TRUE and expansion or replacement is done, two observations are inserted at each boundary, differing in wavelength by $1 \times 10^{-12}$\,nm to prevent rounding errors in later calculations.

Functions trim2ovelap() and extend2extremes()

Functions trim2ovelap() and extend2extremes() are defined only for collections of spectra. They are convenience functions, as they handle special use cases of trim_wl() with a simplified syntax. They can be used to make the wavelength range of the different members of a collection of spectra consistent, either by trimming all spectra to the range of overlapping wavelengths, or by extending the wavelength ranges as needed and filling the added spectral values with a constant value.

extend2extremes(two.mspct, fill = 0)

Functions trim_spct and trim_spct

Functions trim_spct and trim_spct are not generic, and add even more flexibility, but trim_wl should be preferred in user scripts.

Convolving weights

It is very instructive to look at weighted spectral data to understand how effective irradiances are calculated. Plotting effective spectral irradiance data can be very instructive when analysing the interaction of photoreceptors and ambient radiation. It can also illustrate the large effect that small measuring errors can have on the estimated effective irradiances or exposures when SWFs have a steep slope.

Individual spectra

The multiplication operator is defined for operations between a source_spct and a waveband, so this is the easiest way of doing the calculations.

sun.spct * CIE.wb

Tagging with bands and colours

We call tagging, to the process of adding reference information to spectral data. For example we can add a factor indicating regions or bands in the spectrum. We can add also information on the colour, as seen by humans, for each observed value, or for individual regions or bands of the spectrum. In most cases this additional information is used for annotations when plotting the spectral data.

Individual spectra

The function tag can be used to tag different parts of a spectrum according to wavebands.

tag(sun.spct, PAR.wb)
tag(sun.spct, UV_bands.lst)

The added factor and colour data can be used for further processing or for plotting. Information about the tagging and wavebands is stored in an attribute tag.attr in every tagged spectrum, this yields a more compact output and keeps a trace of the tagging.

tg.sun.spct <- tag(sun.spct, PAR.wb)
attr(tg.sun.spct, "spct.tags")

Additional functions are available which return a tagged spectrum and take as input a list of wavebands, but no spectral data. They build a spectrum from the data in the wavebands, and are useful for plotting the boundaries of wavebands.


Function wb2tagged_spct returns a tagged spectrum, with two rows for each waveband, corresponding to the low and high wavelength boundaries, while function wb2rect_spct returns a spectrum with only one row per waveband, with w.length set to its midpoint but with additional columns xmin and xmax corresponding to the low and high wavelength boundaries of the wavebands.

Function is_tagged can be used to query if an spectrum is tagged or not, and function untag removes the tags.

untg.sun.spct <- untag(tg.sun.spct)

In the chuck above, we can see how this works, using in this case the default byref = FALSE which adds the tags to a copy of the spectrum object. In contrast, setting byref = TRUE adds the tags in place, or "by reference"", to the spct object supplied as argument. Passing arguments by reference is unusual for R and is best avoided.

untag(tg.sun.spct, byref = TRUE)


Summaries can be calculated both from individual spectral objects (Table 10) and from collections of spectral objects (Table 11). They return a simpler object than the spectral data in their arguments. For example a vector of numeric values, possibly of length one, in the case of individual spectra, or a data frame containing one row of summary data for each spectrum in a collection of spectra.

Table 10. Summary methods for spectra. Key: + available, - not available, (+) object_spct objects can be converted into filter_spct or reflector_spct objects before applying these methods.

| methods | raw/cps | source | response | filter | reflector | object | chroma | |:--------|:---:|:---:|:---:|:---:|:---:|:---:|:---:| | irrad | -- | + | -- | -- | -- | -- | -- | | e_irrad | -- | + | -- | -- | -- | -- | -- | | q_irrad | -- | + | -- | -- | -- | -- | -- | | fluence | -- | + | -- | -- | -- | -- | -- | | e_fluence | -- | + | -- | -- | -- | -- | -- | | q_fluence | -- | + | -- | -- | -- | -- | -- | | ratio | -- | + | -- | -- | -- | -- | -- | | e_ratio | -- | + | -- | -- | -- | -- | -- | | q_ratio | -- | + | -- | -- | -- | -- | -- | | qe_ratio | -- | + | -- | -- | -- | -- | -- | | eq_ratio | -- | + | -- | -- | -- | -- | -- | | response | -- | -- | + | -- | -- | -- | -- | | e_response | -- | -- | + | -- | -- | -- | -- | | q_response | -- | -- | + | -- | -- | -- | -- | | transmittance | -- | -- | -- | + | -- | + | -- | | absorptance | -- | -- | -- | + | -- | + | -- | | absorbance | -- | -- | -- | + | -- | + | -- | | reflectance | -- | -- | -- | -- | + | + | -- | | wl_range | + | + | + | + | + | + | + | | wl_min | + | + | + | + | + | + | + | | wl_max | + | + | + | + | + | + | + | | wl_stepsize | + | + | + | + | + | + | + | | wl_expanse | + | + | + | + | + | + | + | | wl_midpoint | + | + | + | + | + | + | + | | labels | + | + | + | + | + | + | + | | summary | + | + | + | + | + | + | + | | peaks | -- | + | + | + | + | (+) | - | | valleys | -- | + | + | + | + | (+) | - | | wls_at_target | -- | + | + | + | + | (+) | - | | integrate_spct | + | + | + | + | + | + | + | | average_spct | + | + | + | + | + | + | + | | color_of | -- | + | -- | -- | -- | -- | -- |

As mentioned above, summary methods for collections of spectra return data frame objects. In many cases preserving the attributes from the different members of the collection in the returned value is important, and can be achieved easily by passing a suitable character vector as argument to parameter attr2tb, using the same syntax as described for function add_attr2tb.

Table 11. Summary methods for collections of spectra. Key: + available, * attr2tb supported, -- not available, ms use msmsply() to apply function to collection members, d use msdply(), l use mslply to apply function to collection members, a use msaply to apply function to collection members.

| methods | raw/cps | source | response | filter | reflector | object | chroma | |:--------|:----:|:-----:|:----:|:----:|:----:|:----:|:----:| | irrad | -- | * | -- | -- | -- | -- | -- | | e_irrad | -- | * | -- | -- | -- | -- | -- | | q_irrad | -- | * | -- | -- | -- | -- | -- | | fluence | -- | * | -- | -- | -- | -- | -- | | e_fluence | -- | * | -- | -- | -- | -- | -- | | q_fluence | -- | * | -- | -- | -- | -- | -- | | ratio | -- | * | -- | -- | -- | -- | -- | | e_ratio | -- | * | -- | -- | -- | -- | -- | | q_ratio | -- | * | -- | -- | -- | -- | -- | | qe_ratio | -- | * | -- | -- | -- | -- | -- | | eq_ratio | -- | * | -- | -- | -- | -- | -- | | response | -- | -- | * | -- | -- | -- | -- | | e_response | -- | -- | * | -- | -- | -- | -- | | q_response | -- | -- | * | -- | -- | -- | -- | | transmittance | -- | -- | -- | * | -- | * | -- | | absorptance | -- | -- | -- | * | -- | * | -- | | absorbance | -- | -- | -- | * | -- | * | -- | | reflectance | -- | -- | -- | -- | * | * | -- | | color_of | -- | + | -- | -- | -- | -- | -- | | wl_range | + | + | + | + | + | + | + | | wl_min | + | + | + | + | + | + | + | | wl_max | + | + | + | + | + | + | + | | wl_stepsize | + | + | + | + | + | + | + | | wl_spread | + | + | + | + | + | + | + | | wl_midpoint | + | + | + | + | + | + | + | | labels | l | l | l | l | l | l | l | | summary | l | l | l | l | l | l | l | | peaks | -- | + | + | + | + | (+) | - | | valleys | -- | + | + | + | + | (+) | - | | wls_at_target | -- | + | + | + | + | (+) | - | | integrate_spct | a, d, l | a, d, l | a, d, l | a, d, l | a, d, l | a, d, l | a, d, l | | average_spct | a, d, l | a, d, l | a, d, l | a, d, l | a, d, l | a, d, l | a, d, l |


Specialized definitions of summary and the corresponding print methods are available for spectral objects. Attributes "what.measured", "when.measured" and "where.measured" are included in the summary print out only if set in the spectral object summarized.



Individual spectra

The usual and three new summary methods are available for spectra, but redefined to return wavelength-based summaries in nanometres (nm). For clarity synonyms with clearer names are provided. The three new generic methods midpoint(), expanse() and stepsize() are also defined for numeric.


Collections of spectra

Most frequently used summary methods are implemented for collections of spectra. See the Table 11 for those methods that need to be applied with functions msaply, msdply or mslply to members in a collection returning the results in an array (vector, or matrix), a data frame or a list object. In many cases depending of the class desired for the result, one can chose a suitable apply function, and sometimes it is best to use such a function, even when the corresponding method is implemented for collections of spectra.

Collections of spectra can be useful not only for time-series of spectra or spectral images, but also when dealing with a small group of related spectra. In the example below we show how to use a collection of spectra for calculating summaries. The spectra in a collection do not need to have been measured at the same wavelength values, or have the same number of rows or even of columns. Consequently, in many cases applying the wavelength summary functions described above to collections of spectra can be useful. The value returned is a data frame, with a number of data columns equal to the length of the returned value by the corresponding method for individual spectra.

filters.mspct <- filter_mspct(list(none = clear.spct,
                                   pet = polyester.spct,
                                   yellow = yellow_gel.spct))

Peaks and valleys

Individual spectra

Methods peaks and valleys take spectra as first argument and return a subset of the spectral object data corresponding to local maxima and local minima of the measured variable. span defines the width of the window used as a number of observations.

peaks(sun.spct, span = 51)
valleys(sun.spct, span = 51)

In the case of source_spct and response_spct methods unit.out can be used to force peaks to be searched using either energy or photon based spectral irradiance. The default is energy, or the option "photobiology.radiation.unit" if set.

peaks(sun.spct, span = 51, unit.out = "photon")

It is possible to approximately set the width of the windows in nanometres by using function step_size. However, here we simply use an odd number of wavelengths steps.

peaks(sun.spct, span = 21)

Low level functions find_peaks, get_peaks and get_valleys take numeric vectors as arguments.

Collections of spectra

The methods are also implemented for collections of spectra.

msmsply(filters.mspct, peaks, span = 21)

Two of the filters in the collection do not have peaks, and a spectrum object of length zero is returned for them.

Wavelengths at target value

Method wls_at_target takes a spectrum as first argument and return a subset of the spectral object data or a new object corresponding to wavelengths at which the spectrum is at the target value.

wls_at_target(sun.spct, target = "half.maximum")
wls_at_target(sun.spct, target = "half.maximum", interpolate = TRUE)

As in the case of source_spct and response_spct methods parameters unit.out and filter.qty allow to change the returned value.

Collections of spectra

The method is also implemented for collections of spectra.

wls_at_target(filters.mspct, target = "half.maximum")


Individual spectra

The code using spct objects is simple, to integrate the whole spectrum we can use


and, to integrate a range of wavelengths, in the example, photosynthetically active radiation, we use the waveband PAR.wb we earlier defined.

irrad(sun.spct, PAR.wb)

It is also valid to pass as argument for a numeric range representing wavelengths in nanometres, which is converted into a waveband on-the-fly.

irrad(sun.spct, c(400, 700))

The default for irrad, when no argument unit.out is supplied, is to return the irradiance value in energy irradiance units, unless the R option photobiology.radiation.unit is set.

Functions e_irrad and q_irrad save some typing, and always return the same type of spectral irradiance quantity, independently of global option photobiology.radiation.unit.

e_irrad(sun.spct, PAR.wb) # W m-2
q_irrad(sun.spct, PAR.wb, scale.factor = 1e6) # umol s-1 m-2

It is also possible to supply a time unit to use as basis of expression for the returned value, but be aware that conversion into a logger time unit is only valid for sources like lamps, which have an output the remains constant in time.

irrad(sun.spct, PAR.wb, time.unit = "hour")

Using a shorter time unit than the original, yields an average value re-expressed on a new time unit base.

irrad(sun.daily.spct, PAR.wb, time.unit = "second")

Lists of wavebands are also accepted as argument for parameter in which case a named numeric vector of summary values is returned.

e_irrad(sun.spct, UV_bands.lst) # W m-2

These functions have an additional argument quantity, with default "total", which can take values controlling the output. The value "total" yields irradiance in $W\,m^{-2}$, integrated over wavelengths for each waveband, while "average" yields the mean spectral irradiance within each waveband in $W\,m^{-2}\,nm^{-1}$.

irrad(sun.spct, UV_bands.lst, quantity = "total")
irrad(sun.spct, UV_bands.lst, quantity = "average")

The value "contribution" is relative to the irradiance for the whole spectrum, expressed as a fraction of one, while the value "relative" is relative to the sum of the irradiances for the different wavebands given as argument, also expressed as a fraction of one.

irrad(sun.spct, UV_bands.lst, quantity = "contribution")
irrad(sun.spct, UV_bands.lst, quantity = "relative")

In contrast "contribution.pc" and "relative.pc" return the same values expressed as percentages.

irrad(sun.spct, UV_bands.lst, quantity = "contribution.pc")
irrad(sun.spct, UV_bands.lst, quantity = "relative.pc")

The total radiation received on a surface during an exposure event can be also calculated with method irrad(), but the value returned is an irradiance expressed on a very unusual time basis. These are no longer W m-2 (J s-1 m-2), but instead J per 8 hours per square metre.

irrad(sun.spct, PAR.wb, time.unit = duration(8, "hours"))

When the intention is to calculate a total fluence or exposure for an event function fluence() should be used as this will add the correct metadata attributes to the returned value: expressed as energy or photons per unit area per event. See later sections for details.

fluence(sun.spct, PAR.wb, exposure.time = duration(8, "hours"))

Collections of spectra

Collections of spectra can be useful not only for time-series of spectra or spectral images, but also when dealing with a small group of related spectra. In the example below we show how to use a collection of spectra for estimating irradiances under different filters set up in sunlight.

We reuse collection of filter spectra filters.mspct from an earlier section.


We can add to the returned data frame attributes retrieved from the members of the collection.

two_suns.mspct <- source_mspct(list(sun1 = sun.spct, sun2 = sun.spct))
q_irrad(two_suns.mspct, = list(PAR = waveband(c(400,700))),
        scale.factor = 1e6, # umol m-2 s-1
        attr2tb = c(when.measured = "time", lon = "lon", lat = "lat"))

We then convolve each filter's spectral transmittance by the spectral irradiance of the light source.

filtered_sun <- convolve_each(filters.mspct, sun.spct)
irrad(filtered_sun, list(UVA.wb, PAR.wb))

The code above can also be written as a single statement.

irrad(convolve_each(filters.mspct, sun.spct), 
      list(UVA.wb, PAR.wb))

It is also possible to use an apply function. Syntax parallels that of base R's and package plyr's. See sections apply functions and convolve for more details.

One thing to remember, is that operators in R are just normal functions with special names and call syntax. They can also be called with the usual function call syntax by enclosing their name in back quotes. We use this to pass as argument the multiplication operator * in a call to msmsply which returns, in this case, a source_multi_spct object. After this we just call the irrad method on the collection of spectra and obtain the result as a data frame with one row per spectrum and one column by waveband.

filtered_sun <- msmsply(filters.mspct, `*`, sun.spct)
irrad(filtered_sun, list(UVA.wb, PAR.wb))


Individual spectra

The calculation of fluence values (time-integrated irradiance) is identical to that for irradiance, except that a exposure.time argument needs to be supplied. The exposure time must be a lubridate::duration, but any argument accepted by as.duration can also be used. Functions fluence, e_fluence and q_fluence correspond to irrad, e_irrad and q_irrad,

fluence(sun.spct, exposure.time = duration(1, "hours"))
fluence(sun.spct, exposure.time = 3600) # seconds

and, to obtain the photon fluence for a range of wavelengths, in the example, photosynthetically active radiation, we use the PAR.wb waveband object earlier defined, for 25 minutes of exposure.

q_fluence(sun.spct, PAR.wb, exposure.time = duration(25, "minutes"))

Photon and energy ratios

Individual spectra

The functions described here, in there simplest use, calculate a ratio between two wavebands. The function q_ratio returning photon ratios. However both waveband parameters can take lists of wavebands as arguments, with normal recycling rules in effect. The corresponding function e_ratio returns energy ratios.

q_ratio(sun.spct, UVB.wb, PAR.wb)
q_ratio(sun.spct, list(UVC.wb, UVB.wb, UVA.wb, UV.wb))
q_ratio(sun.spct, UVB.wb, list(UV.wb, PAR.wb))

Function qe_ratio, has only one waveband parameter, and returns the photon to energy ratio, while its complement eq_ratio returns the energy to photon ratio.

qe_ratio(sun.spct, list(UVB.wb, PAR.wb))

Collections of spectra

q_ratio(filtered_sun, list(UVB.wb, UVA.wb, PAR.wb))

Normalized difference indexes

Individual spectra

These indexes are frequently used to summarize reflectance data, for example in remote sensing the NDVI (normalized difference vegetation index). Here we give an unusual example to demonstrate that function normalized_diff_ind() can be used to calculate, or define any similar index.

                    waveband(c(400, 700)), waveband(c(700, 1100)),

Transmittance, reflectance, absorptance and absorbance

Individual spectra

The functions transmittance, absorptance and absorbance take filter_spct as argument, while function reflectance takes reflector_spct objects as argument. Functions transmittance, reflectance and absorptance are also implemented for object_spct. These functions return as default an average value for these quantities assuming a light source with a flat spectral energy output, but this can be changed as described above for irrad().

transmittance(polyester.spct, list(UVB.wb, UVA.wb, PAR.wb))
reflectance(green_leaf.spct, waveband(c(600, 700)))

It is more likely that we would like to calculate these values with reference to light of a certain spectral quality. This needs to be calculated by hand, which is not difficult.

irrad(sun.spct * polyester.spct, list(UVB.wb, UVA.wb, PAR.wb, wb.trim = TRUE)) /
  irrad(sun.spct, list(UVB.wb, UVA.wb, PAR.wb, wb.trim = TRUE))

Collections of spectra

Here we construct a collection of filter spectra, and then we calculate the transmittance of these filters for two wavebands, obtaining the results as a data frame, with one row per filter, and one column per waveband. We reuse once more filters.mspct from an earlier section.

transmittance(filters.mspct, = list("Tfr(UVA)" = UVA.wb, "Tfr(PAR)" = PAR.wb))
     = list(Tfr = waveband(c(350, 550))),
              attr2tb = "what.measured")
              list("Tfr(UVA)" = UVA.wb, "Tfr(PAR)" = PAR.wb),
              attr2tb = "what.measured")

Integrated response

Individual spectra

The functions response, e_response and q_response take response_spct objects as arguments, and return the integrated value for each waveband (integrated over wavelength) assuming a light source with a flat spectral energy or photon output respectively. If no waveband is supplied as argument, the whole spectrum is integrated.


When a waveband, or list of wavebands, is supplied the response is calculated for the wavebands.

e_response(photodiode.spct, list(UVB.wb, UVA.wb))

This function has an additional argument quantity, with default "total", as described for irrad().

Collections of spectra

sensors <- response_mspct(list(GaAsP = photodiode.spct,
                               CCD = ccd.spct))
response(sensors, list(UVB.wb, UVA.wb, PAR.wb), quantity = "contribution")

Integration over wavelengths

When we need to integrate some \emph{non-standard} numeric variable stored in a spectral object we can use functions integrate_spct or average_spct.

Calculation from individual spectra

We can integrate the values of arbitrary numeric columns other than w.length in an spectral object. All spectral classes are derived from generic_spct, so the examples in this section apply to objects of any of the derived spectral classes as well.


The function average_spct integrates every column holding numeric values from a spectrum object, except for w.length, and divides the result by the spread or width of the wavelength range integrated, returning a value expressed in the same units as the spectral data.


RGB colours

Two functions allow calculation of simulated colour of light sources as R colour definitions. Three different functions are available, one for monochromatic light taking as argument wavelength values, and one for polychromatic light taking as argument spectral energy irradiances and the corresponding wave length values. The third function can be used to calculate a representative RGB colour for a band of the spectrum represented as a range of wavelength, based on the assumption of a flat energy irradiance across the range. By default CIE coordinates for typical human vision are used, but the functions have a parameter that can be used for supplying a different chromaticity definition.

Examples for monochromatic light:

w_length2rgb(550) # green
w_length2rgb(630) # red
w_length2rgb(c(550, 630, 380, 750)) # vectorized

Examples for wavelength ranges:


Examples for spectra as vectors, in this case for the solar spectrum:

with(sun.spct, s_e_irrad2rgb(w.length, s.e.irrad))
with(sun.spct, s_e_irrad2rgb(w.length, s.e.irrad, sens = ciexyzCMF2.spct))

Examples with source_spct objects.

rgb_spct(sun.spct, sens = ciexyzCMF2.spct)

And also a color_of method for source_spct.

color_of(sun.spct * yellow_gel.spct)

Try the photobiology package in your browser

Any scripts or data that you put into this service are public.

photobiology documentation built on Dec. 11, 2018, 9:05 a.m.