---
title: "User Guide: 2 Radiation"
subtitle: "Package 'photobiology' `r packageVersion('photobiology')` "
author: "Pedro J. Aphalo"
date: "`r Sys.Date()`"
output: 
  rmarkdown::html_vignette:
    toc: yes
vignette: >
  %\VignetteIndexEntry{User Guide: 2 Radiation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include=FALSE, echo=FALSE}
knitr::opts_chunk$set(fig.width=7.2, fig.height=4.3)
```

```{r, printing-spectra, eval=TRUE, include=FALSE}
# library(tibble)
options(tibble.print_max = 6, tibble.print_min = 4)
```

## Spectra

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.

```{r, pkg-load, eval=TRUE}
library(photobiology)
library(lubridate)
library(dplyr)
```

### Classes

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.

<hr>

**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` | |

<hr>

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.

<hr>

**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}$ |  |

<hr>

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.

<hr>
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._
<hr>

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.

```{r, example-1, eval=FALSE}
# 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()`.

```{r, query-class-1}
is.any_spct(sun.spct)
is.generic_spct(sun.spct)
is.source_spct(sun.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.

```{r, query-class-2}
class_spct(sun.spct)
class(sun.spct)
```

### Construction

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 `as.data.frame` 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.

```{r, construction-1}
my.df <- data.frame(w.length = 400:410, s.e.irrad = 1)
my.spct <- as.source_spct(my.df)
class(my.spct)
class(my.df)
my.spct
```

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

```{r, construction-2}
my.g.spct <- as.generic_spct(my.spct)
class(my.g.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.

```{r, construction-3}
source_spct(w.length = 300:305, s.e.irrad = 1)
```

```{r, construction-4}
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.

```{r, construction-5}
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.

```{r, construction-6}
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.

```{r, construction-7}
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.

```{r, construction-8}
my.cm.spct <- source_spct(w.length = 300:305, s.e.irrad = 1,
                          comment = "This is a comment")
comment(my.cm.spct)
```

### 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`.

```{r, attr-1}
my.spct <- sun.spct
setWhenMeasured(my.spct, NULL)
getWhenMeasured(my.spct)
setWhenMeasured(my.spct, ymd_hms("2015-10-31 22:55:00", tz = "EET"))
getWhenMeasured(my.spct)
```

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"`.

```{r, attr-2}
setWhereMeasured(my.spct, NULL)
getWhereMeasured(my.spct)
setWhereMeasured(my.spct, lat = 60, lon = -10)
getWhereMeasured(my.spct)
getWhereMeasured(my.spct)$lon
my.spct
```

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

```{r, attr-2a}
setWhatMeasured(my.spct, "something")
getWhatMeasured(my.spct)
my.spct
```

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.

```{r, attr-3}
is_effective(sun.spct)
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.

```{r, attr-4}
ten.minutes.spct <-
  convertTimeUnit(sun.spct, time.unit = duration(10, "minutes"))
ten.minutes.spct
getTimeUnit(ten.minutes.spct)
```

<hr>
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.
<hr>

<hr>
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.
<hr>

## Collections of spectra

### Classes

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.

<hr>

**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` |

<hr>

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.

### Construction

#### Constructors

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

```{r, col-construction-1}
two_suns.mspct <- source_mspct(list(sun1 = sun.spct, sun2 = sun.spct))
two_suns.mspct
```

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

```{r, col-construction-2}
mixed.mspct <- generic_mspct(list(filter = clear.spct, source = sun.spct))
class(mixed.mspct)
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.

```{r, col-construction-3}
two_gen.mspct <- as.generic_mspct(two_suns.mspct)
class(two_gen.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. 

```{r, col-construction-4}
one_sun.mspct <- as.source_mspct(sun.spct)
class(one_sun.mspct)
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").

```{r, col-construction-5}
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.

```{r, col-construction-6}
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.

```{r, col-construction-7}
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.

```{r, col-construction-8}
two_suns.mat <- as.matrix(two_suns.mspct, "s.e.irrad")
class(two_suns.mat)
dim(two_suns.mat)
head(dimnames(two_suns.mat)$spct)
head(dimnames(two_suns.mat)$w.length)
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.

```{r, col-construction-9}
two_suns.row_mat <- as.matrix(two_suns.mat, "s.e.irrad", byrow = TRUE)
class(two_suns.row_mat)
dim(two_suns.row_mat)
head(dimnames(two_suns.row_mat)$spct)
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.

```{r, bind-1}
two_suns.spct <- rbindspct(list(a = sun.spct, b = sun.spct / 2))
two_suns.spct
```


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.

```{r, bind-1a}
subset2mspct(two_suns.spct)
```

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.

```{r, bind-2}
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.

```{r, bind-3}
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`).

```{r, set-class-1}
test2.df <- test1.df
setSourceSpct(test2.df, multiple.wl = 2L)
getMultipleWl(test2.df)
```

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.

```{r, set-class-2}
test3.df <- test1.df
setSourceSpct(test3.df, multiple.wl = NULL)
getMultipleWl(test3.df)
```

#### 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.

```{r, split-1}
test2.df <- data.frame(w.length = 200:210, A = 1, B = 2, z = "A")
split2source_mspct(test2.df)
split2source_mspct(test2.df, spct.data.var = "s.q.irrad")
```

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

```{r, split-2}
split2source_mspct(test2.df, spct.data.var = "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.

```{r, join-mspct-01}
my.mspct <- source_mspct(list(sun1 = sun.spct, sun2 = sun.spct * 2))
my.df <- join_mspct(my.mspct)
head(my.df)
```


### Querying the class

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

```{r, col-query-class-1}
is.source_mspct(two_suns.mspct)
class(two_suns.mspct)
```

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.

```{r, col-query-class-2}
is.filter_mspct(mixed.mspct)
is.any_mspct(mixed.mspct)
class(mixed.mspct)
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.

<hr>
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. 
<hr>

```{r, extract-1, eval=FALSE}
# not run as this triggers an error when building the vignette with 'devtools'
two_suns.mspct[1]
two_suns.mspct[1:2]
```

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!

```{r, extract-2, eval=FALSE}
# not run as this triggers an error when building the vignette with 'devtools'
two_suns.mspct[1:2] <- two_suns.mspct[2:1]
```

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

```{r, extract-3}
two_suns.mspct[[1]]
two_suns.mspct$sun1
two_suns.mspct[["sun1"]]
```


```{r, extract-4}
two_suns.mspct[["sun1"]] <- sun.spct * 2
two_suns.mspct[["sun2"]] <- NULL
two_suns.mspct
```

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. 

```{r, extract-5}
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).

<hr>

**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 |

<hr>

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
```{r, apply-1}
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
```{r, apply-2}
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.

```{r, apply-3}
msdply(two.mspct, max)
```

```{r, apply-4}
ranges.df <- msdply(two.mspct, range)
ranges.df
cat(comment(ranges.df))
```

```{r, apply-5}
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.

```{r, apply-6}
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.

```{r, apply-7}
str(msaply(two.mspct, max))
```

```{r, apply-8}
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.

### Summary spectra

Starting from a collection of spectra we may want to obtain a single spectrum containing a summary of them. The current implementation of the methods described in this section expects that all spectra in a collection are of the same class, and data is available for each of them at exactly the same set of wavelengths.

| functions | class of argument | class of returned spectrum | variables in spectrum |
|:--------------------|:----------|:---------------|:------------------------|
| `s_sum`| `source_mspct` | `source_spct` | same as input |
| `s_sum` | `response_mspct` | `response_spct` | same as input |
| `s_sum` | `filter_mspct` | `generic_spct` | input tagged `.sum` |
| `s_sum` | `reflector_mspct` | `generic_spct` | input tagged `.sum` |
| `s_sum` | `calibration_mspct` | `generic_spct` | input tagged `.sum` |
| `s_prod`| `source_mspct` | `source_spct` | input tagged `.prod`  |
| `s_prod` | `response_mspct` | `response_spct` | input tagged `.prod`  |
| `s_prod` | `filter_mspct` | `filter_spct` | same as input |
| `s_prod` | `reflector_mspct` | `reflector_spct` | same as input |
| `s_prod` | `calibration_mspct` | `generic_spct` | input tagged `.prod` |
| `s_mean` or `s_median` | `source_mspct` | `source_spct` | same as input |
| `s_mean` or `s_median` | `response_mspct` | `response_spct` | same as input |
| `s_mean` or `s_median` | `filter_mspct` | `filter_spct` | same as input |
| `s_mean` or `s_median` | `reflector_mspct` | `reflector_spct` | same as input |
| `s_mean` or `s_median` | `calibration_mspct` | `calibration_spct` | same as input |
| `s_var`, `s_se` or `s_sd` | `source_mspct` | `generic_spct` | input tagged `.var`, `.se` or `.sd` |
| `s_var`, `s_se` or `s_sd` | `response_mspct` | `generic_spct` | input tagged `.var`, `.se` or `.sd` |
| `s_var`, `s_se` or `s_sd` | `filter_mspct` | `generic_spct` | input tagged `.var`, `.se` or `.sd` |
| `s_var`, `s_se` or `s_sd` | `reflector_mspct` | `generic_spct` | input tagged `.var`, `.se` or `.sd` |
| `s_var`, `s_se` or `s_sd` | `calibration_mspct` | `generic_spct` | input tagged `.var`, `.se` or `.sd` |
| `s_mean_se`| `source_mspct` | `source_spct` | same as input plus col. tagged `.se` |
| `s_mean_se` | `response_mspct` | `response_spct` | same as input plus col. tagged `.se` |
| `s_mean_se` | `filter_mspct` | `filter_spct` | same as input plus col. tagged `.se` |
| `s_mean_se` | `reflector_mspct` | `reflector_spct` | same as input plus col. tagged `.se` |
| `s_mean_se` | `calibration_mspct` | `calibration_spct` | same as input plus col. tagged `.se` |
| `s_range` | `source_mspct` | `generic_spct` | input tagged `.min` and `.max` |
| `s_range` | `response_mspct` | `generic_spct` | input tagged `.min` and `.max` |
| `s_range` | `filter_mspct` | `generic_spct` | input tagged `.min` and `.max` |
| `s_range` | `reflector_mspct` | `generic_spct` | input tagged `.min` and `.max` |
| `s_range` | `calibration_mspct` | `generic_spct` | input tagged `.min` and `.max` | 

The calculations are done wavelength by wavelength yielding a spectrum at the same wavelengths than those in the input. Removal of `NA` values is done wavelength by wavelength. As shown in the table above, depending on the operation the returned quantiy may be equivalent or not to the quatitiy in the input. When the quantity changes, even if units of expression remain the same, the name of the variable and class of the returned spectrum are different to those in the input.

Computing the mean returns values of the same quantity as in the input, and we obtain a `source_spct` object.

```{r}
s_mean(two.mspct)
```

If we compute the standard error, the quantity returned is not the same. The object returned is in this case a `generic_spct` and the variable name has been tagged with `.se`.

```{r}
s_se(two.mspct)
```

We can also compute both the mean and standard error simultaneously.

```{r}
s_mean_se(two.mspct)
```

The other functions in these family function in a similar way to those described here.

### Convolution

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.

```{r, convolve-1}
convolve_each(two.mspct, sun.spct)
```

```{r, convolve-2}
convolve_each(sun.spct, two.mspct)
```

```{r, convolve-3}
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.
```{r}
convolve_each(two.mspct, sun.spct, oper = `+`)
```

<hr>
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.
<hr>

### Metadata attributes

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

```{r, col-attr-1}
getWhenMeasured(two.mspct)
setWhenMeasured(two.mspct, ymd("2015-10-31", tz = "EET"))
getWhenMeasured(two.mspct)
setWhenMeasured(two.mspct,
                list(ymd_hm("2015-10-31 10:00", tz = "EET"),
                     ymd_hm("2015-10-31 11:00", tz = "EET")))
getWhenMeasured(two.mspct)
two.mspct
```

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.

```{r}
when_measured2tb(two.mspct)
```

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

```{r}
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_.

```{r}
q_irrad(two.mspct) %>%
  add_attr2tb(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.

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

## Wavebands

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`.

### Construction

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**'.

```{r, wb-1}
PAR.wb <- waveband(c(400, 700), wb.name = "PAR")
UVA.wb <- waveband(c(315, 400), wb.name = "UVA")
UVB.wb <- waveband(c(280, 315), wb.name = "UVB")
UVC.wb <- waveband(c(100, 280), wb.name = "UVC")
UV.wb  <- waveband(c(100, 400), wb.name =  "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.

```{r, wb-2}
CIE_e_fun <-
function(w.length){
    CIE.energy <- numeric(length(w.length))
    CIE.energy[w.length <= 298] <- 1
    CIE.energy[(w.length > 298) & (w.length <= 328)] <-
      10^(0.094*(298-w.length[(w.length > 298) & (w.length <= 328)]))
    CIE.energy[(w.length > 328) & (w.length <= 400)] <-
      10^(0.015*(139-w.length[(w.length > 328) & (w.length <= 400)]))
    CIE.energy[w.length > 400] <- 0
    return(CIE.energy)
}
```

```{r, wb-3}
CIE.wb <- waveband(c(250, 400), weight = "SWF",
                   SWF.e.fun = 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.

```{r, wb-4}
waveband(sun.spct)
```

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

```{r}
waveband()
```

### Querying the class

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

```{r, wb-5}
is.waveband(PAR.wb)
```

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.

```{r, wb-6}
is_effective(waveband(c(400,500)))
```

## 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.

### Construction

#### List constructor

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

```{r, wb-list-1}
wavebands <- list(waveband(c(300,400)), waveband(c(400,500)))
wavebands
```

#### 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.

```{r, wb-split-1}
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).

```{r, wb-split-2}
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.

```{r, wb-split-3}
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.

```{r, wb-split-4}
split_bands(UV_bands.lst, length.out  =  2)
split_bands(list(c(100, 150, 200), c(800, 825)), length.out = 1)
```

## Object _inspection_ methods

### Printing

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.

```{r, set-up-printing, eval=FALSE}
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.

```{r, print-1, eval=FALSE}
print(sun.spct, n = 3)
```

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

### Summary

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.

```{r, print-2, eval=FALSE}
summary(sun.spct)
```

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 `NA`s

Functions `na.omit()` and `na.exclude` are implemented for all spectral classes. These methods test for `NA`s 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.fail()`, `na.pass()` and `na.action()`, the methods from base R, can be used with spectra. 

```{r}
na.omit(sun.spct)
na.exclude(sun.spct)
```

## 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.

<hr>

**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) |

<hr>

```{r, bin-oper-1}
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.

```{r, bin-oper-2}
sun.spct * polyester.spct
```

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

```{r, bin-oper-3}
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).

```{r, bin-oper-4}
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.

```{r, bin-oper-5}
sun.spct * UVB.wb
```

```{r, bin-oper-6, eval=FALSE}
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.

```{r, bin-oper-7, warning=TRUE, purl=FALSE}
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).

<hr>

**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` |

<hr>

```{r, unary-oper-1}
-sun.spct
sqrt(sun.spct)
```

### Options

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`.

<hr>

**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.

```{r, options-1}
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.

```{r, options-2}
photon_as_default()
sun.spct * UVB.wb
energy_as_default()
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()`.

```{r, options-3}
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.

<hr>

**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 | -- | + | + | + | + | + | +  |

<hr>

<hr>

**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 | -- | + | + | + | + | + | + |

<hr>

### 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.

```{r, manip-1}
# STOPGAP
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.

```{r, manip-2}
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.

```{r, manip-3}
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.

```{r, manip-4}
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.

```{r, manip-5}
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.

```{r, manip-6}
normalize(sun.spct)
```

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.

```{r, manip-7}
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.

```{r, manip-8}
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()`.

```{r}
my.spct <- normalize(sun.spct)
is_normalized(my.spct)
getNormalized(my.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.

```{r, manip-9}
fscale(sun.spct)
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()`.

```{r}
my.spct <- fscale(sun.spct)
is_scaled(my.spct)
getScaled(my.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.

```{r, manip-10}
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.

```{r, manip-11}
clean(sun.spct - 0.01, range = c(280.5, 282))
```

```{r, manip-12}
clean(polyester.spct - 0.053)
```
### Smoothing of spectra

We can use smoothing both to remove randon variation among nearby pixels caused by measuring noise, or to filter out the real fine structure of the spectrum to better assess the larger features. The methods described here work by averaging the spectral data from neighbouring wavelengths. The simplest methods are running medians and running means (or "boxcar smoothing" in the terminology used in some software). There are several methods for smoothing available in R using different algorithms. Some of these methods also automatically set the degree of smoothing based on the data being smoothed. Package 'photobiology' defines function `smooth_spct`, which at moment supports three different methods. Two are wrappers on R's own methods and a third one, `"custom"`, is designed to use stronger smoothing of values close to zero, where noise in spectral irardiance measurements is proportionally more.

```{r}
smooth_spct(sun.spct)
```

```{r}
smooth_spct(polyester.spct, method = "supsmu", strength = 2)
```

### 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.

```{r, manip-13}
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.

```{r, trim-1}
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.

```{r, trim-2}
clip_wl(sun.spct, range = UVA.wb)
```

The result can be a spectrum of length zero.

```{r, trim-3}
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`.

```{r, trim-4}
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.

```{r, trim-5}
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.

```{r, trim-6}
trim_wl(sun.spct, c(281.5, NA), fill = NA)
```

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

```{r, trim-7}
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`.

```{r, trim-8}
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.

```{r}
trim2overlap(two.mspct)
```

```{r}
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.

```{r, weights-1}
sun.spct * CIE.wb
```

<!-- #### Vectors -->

<!-- It is also possible to use vectors. -->

<!-- ```{r, vectors-1, tidy=FALSE} -->
<!-- weighted.s.e.irrad <- -->
<!--   with(sun.spct, -->
<!--        s.e.irrad * calc_multipliers(w.length, 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.

```{r, tag-1}
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.

```{r, tag-2}
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.

```{r, tag-3}
wb2tagged_spct(UV_bands.lst)
wb2rect_spct(UV_bands.lst)
```

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.

```{r, tag-4}
tg.sun.spct
is_tagged(tg.sun.spct)
untg.sun.spct <- untag(tg.sun.spct)
is_tagged(untg.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.

```{r}
is_tagged(untg.sun.spct)
untag(tg.sun.spct, byref = TRUE)
is_tagged(untg.sun.spct)

```

## Summaries

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.

<hr>

**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 | -- | + | -- | -- | -- | -- | -- |

<hr>

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`.

<hr>

**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** | 

<hr>

### Summary

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.

```{r, summary-1}
summary(sun.spct)
```

```{r, summary-2}
summary(two_suns.spct)
```

### Wavelength

#### 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`.

```{r, summary-3}
wl_range(sun.spct)
wl_min(sun.spct)
wl_max(sun.spct)
wl_midpoint(sun.spct)
wl_expanse(sun.spct)
wl_stepsize(sun.spct)
```

#### Collections of spectra

<hr>
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.
<hr>

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.

```{r, summary-4}
filters.mspct <- filter_mspct(list(none = clear.spct,
                                   pet = polyester.spct,
                                   yellow = yellow_gel.spct))
wl_range(filters.mspct)
```

### 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.

```{r, summary-5}
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.

```{r, summary-6}
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_.

```{r, summary-7}
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.

```{r, col-summary-1}
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.

```{r, find-wls-1}
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.

```{r, find-wls-2}
wls_at_target(filters.mspct, target = "half.maximum")
```

### Irradiance

#### Individual spectra

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

```{r, irrad-1}
irrad(sun.spct)
```

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

```{r, irrad-2}
irrad(sun.spct, PAR.wb)
```

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

```{r, irrad-3}
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`.

```{r, irrad-4}
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.

```{r, irrad-5}
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.

```{r, irrad-6}
irrad(sun.daily.spct, PAR.wb, time.unit = "second")
```

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

```{r, irrad-7}
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}$.

```{r, irrad-8a}
irrad(sun.spct, UV_bands.lst, quantity = "total")
```

```{r, irrad-8b}
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.

```{r, irrad-8c}
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.
  
```{r, irrad-8d}
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.

```{r}
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.

```{r}
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.

```{r, col-names-1}
names(filters.mspct)
```

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

```{r}
two_suns.mspct <- source_mspct(list(sun1 = sun.spct, sun2 = sun.spct))
q_irrad(two_suns.mspct, 
        w.band = 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.

```{r, col-convolve-1}
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.

```{r, col-convolve-2}
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.

```{r, col-convolve-3}
filtered_sun <- msmsply(filters.mspct, `*`, sun.spct)
irrad(filtered_sun, list(UVA.wb, PAR.wb))
```

### Fluence

#### 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`,

```{r, fluence-1}
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.

```{r, fluence-2}
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.

```{r}
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.

```{r, ratios-2}
qe_ratio(sun.spct, list(UVB.wb, PAR.wb))
```

#### Collections of spectra

```{r, ratios-3}
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.

```{r}
normalized_diff_ind(sun.spct,
                    waveband(c(400, 700)), waveband(c(700, 1100)),
                    irrad)
```

### 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()`.

```{r}
transmittance(polyester.spct, list(UVB.wb, UVA.wb, PAR.wb))
```

```{r}
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.

```{r}
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.

```{r}
transmittance(filters.mspct, w.band = list("Tfr(UVA)" = UVA.wb, "Tfr(PAR)" = PAR.wb))
```

```{r}
transmittance(filters.mspct,
              w.band = list(Tfr = waveband(c(350, 550))),
              attr2tb = "what.measured")
```

```{r}
transmittance(filters.mspct, 
              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.

```{r}
response(photodiode.spct)
```

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

```{r}
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

```{r}
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.

```{r}
integrate_spct(sun.spct)
```

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.

```{r}
average_spct(sun.spct)
```

## 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:

```{r}
w_length2rgb(550) # green
w_length2rgb(630) # red
w_length2rgb(c(550, 630, 380, 750)) # vectorized
```


Examples for wavelength ranges:

```{r}
w_length_range2rgb(c(400,700))
```

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

```{r}
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.

```{r}
rgb_spct(sun.spct)
rgb_spct(sun.spct, sens = ciexyzCMF2.spct)
```

And also a `color_of` method for `source_spct`.

```{r}
color_of(sun.spct)
color_of(sun.spct * yellow_gel.spct)
```
