# Spectra and Spectral Libraries

The Spectrum Plot shows the spectrum of any pixel you click. Collect several
spectra to compare them, and import a library to identify them.

Show the plot with the **Spectrum Plot** pane toggle on the main toolbar.

:::{figure} ../_static/tutorials/t2_collected.png
:width: 90%
:align: center
:alt: The spectrum plot with several collected AVIRIS spectra listed below it
:::

For a guided first pass see {doc}`Tutorial 2 <../tutorials/02-spectra>`.

---

## Getting a spectrum

Click any pixel in the main window or the zoom pane. A crosshair marks it and
its spectrum is plotted.

- The **x-axis** is wavelength when the dataset supplies wavelengths, band
  number otherwise. Units follow the header: nanometers, micrometers, or
  whatever the file declares.
- The **y-axis** is labeled with the quantity the data declares: a netCDF
  variable's `long_name` and `units` (EMIT reflectance reads "Surface
  Reflectance (unitless)"), an ENVI header's `z plot titles` y title and
  `data units`, or a GeoTIFF unit type shared by every band. When the displayed
  spectra declare different quantities, or some declare one and others none,
  the label is "Data value (mixed units)"; when none declares one it is
  "Value". WISER never guesses a quantity the file does not declare.
- A **single-band** dataset, such as an NDVI band-math result, gives a
  one-value spectrum. It is drawn as a large marker in the selected-point
  symbol, with its value written beside it to 4 significant figures, and the
  x-axis is centered on it.
- A pixel whose values are all NaN or masked has nothing to draw; the plot
  shows **No data at this pixel** instead (followed by the spectra's names when
  only some of the displayed spectra have no data).
- **Bad bands** flagged in the header are left out of the line, which is why a
  spectrum from an airborne cube has gaps at the atmospheric water-vapor
  regions.
- Pixels equal to the dataset's **data-ignore value** are left out as gaps
  too. Integer datasets behave the same way: no-data and bad bands are gaps,
  never 0, and a real value of 0 stays 0.
- The **status bar** reports the pixel coordinate and, for georeferenced data,
  the ground position.
- Clicking on the plot itself shows the `(x, y)` value at that point;
  right-click to hide it.

### Active versus collected

The spectrum you just clicked is the **active** spectrum, replaced the moment
you click elsewhere. Press **Collect spectrum** to keep it. Collected spectra
are listed under **Spectra and Spectral Libraries** below the plot, stay drawn
together, and are saved in a {doc}`project <projects>`.

From the list you can untick a spectrum to hide it, **Edit...** to rename it or
change its color, or **Save to file...** to write it out. Rename and recolor
spectra as you collect them: WISER gives each one a random color, which says
nothing about what it is.

### Which dataset the plot reads

:::{figure} ../_static/images/plot_clicked_dataset.png
:width: 55%
:align: center
:alt: The spectrum plot's dataset selector
:::

The control at the plot's top left chooses which dataset spectra are pulled
from. Left alone, the plot follows whichever image you click. Pinned to one
dataset, a click anywhere, including on a different linked image in grid view,
gives you that dataset's spectrum at the corresponding pixel (the same ground
location when both images are georeferenced).

---

## Averaging

A single pixel's spectrum carries the sensor's full noise. In the
configuration dialog (the **gear** icon), **Default area-average size**
averages an *n* × *n* box around each click, with a choice of **mean** or
**median**.

- **Mean** suppresses random noise.
- **Median** is better when a few pixels in the box are outliers, such as a bad
  detector element, a specular highlight, a mixed edge pixel.

This changes the spectrum you extract, not the cube. For a stable class
signature use a
{doc}`Region of Interest <regions-of-interest>` instead: it averages over an
area you chose deliberately rather than a square box.

---

## Configuring the plot

The **gear** icon opens the configuration dialog. The plot's right-click menu
offers the same thing as **Configure plot...**:

| Section | Settings |
|---|---|
| **Plot** | **Font:** family; **Font Sizes** for the title, legend, axis labels, selected point and tick labels; **Plot Title:**; **Legend:** placement |
| **Selected Point** | **Draw with symbol:** and **Draw crosshair through selected point** |
| **X Axis** | **Manually specify X axis range** (**Minimum value:**, **Maximum value:**); **X Axis Units:**; **Manually configure X axis tick marks** (**Major tick marks every**, **Minor tick marks every**); **X Axis Label:** |
| **Y Axis** | The same range, tick-mark and label settings as the X axis. A **Y Axis Label:** you enter replaces the automatic label (the data's declared quantity); clearing the field returns to the automatic label |
| **New Spectra** | **Default area-average size** (X and Y) and **Default average mode:** (mean or median) |

:::{figure} ../_static/images/plot_config.png
:width: 55%
:align: center
:alt: The spectrum plot configuration dialog
:::

The most useful setting here is the **x-axis range**: set it to bracket one
absorption feature. A 400–2500 nm plot hides a 40 nm-wide band that a
2100–2300 nm plot makes obvious.

To save a figure, right-click the plot and choose **Export plot to image...**:
EPS, PDF, PNG or SVG at 72, 100 or 300 dpi.

---

## Importing spectra

The **Load or import spectra** button in the plot toolbar offers:

- **Load spectral library...** — an ENVI spectral library, `.sli` with its
  `.hdr`
- **Import ASCII spectral data...** — a text file; WISER opens a dialog to pick
  the column delimiter and identify which column holds wavelength and which the
  values

:::{figure} ../_static/images/spectra_import.png
:width: 55%
:align: center
:alt: The import-spectra dialog for ASCII files
:::

The same operation is on the **File** menu as **Import spectra from text
file...**.

Imported libraries are **listed but not drawn**, because several hundred spectra
at once would be unreadable. Right-click a spectrum name and choose **Show
spectrum** to draw that one. Right-click the library name and choose **Show all
spectra** or **Hide all spectra** to draw or hide all of it.

### Libraries that ship with WISER

- A **USGS mineral spectral library** is preloaded in the
  {doc}`SAM <data-analysis-tools/spectral-angle-mapper>` and
  {doc}`SFF <data-analysis-tools/spectral-feature-fitting>` tools.
- `src/test_utils/test_spectra/usgs_resampHeadwallSWIR.hdr` in the source tree
  holds 481 USGS mineral spectra resampled to a 285-band Headwall SWIR sensor.

### Where to get more

- [USGS Spectral Library Version 7](https://dx.doi.org/10.5066/F7RR1WDJ) — the
  standard mineral reference, with versions convolved to AVIRIS-Classic, HyMap,
  Hyperion, CRISM, M3, VIMS, ASTER, Landsat 8 OLI, Sentinel-2 MSI and
  WorldView-3
- [ECOSTRESS Spectral Library](https://speclib.jpl.nasa.gov/) — minerals,
  rocks, soils, vegetation, water, snow and man-made materials

```{tip}
Prefer a library **convolved to your sensor** where one exists. The detection
tools interpolate references onto the target's wavelength grid anyway, but
starting from the right sampling is better.
```

---

## Continuum removal

Right-click a spectrum's name in the list below the plot for:

- **Continuum Removal: Single Spectrum** — that spectrum
- **Continuum Removal: Collected Spectra** — everything collected

WISER fits the upper convex hull of the spectrum and divides by it. For each
spectrum it processes, WISER adds two collected spectra:
`<name> Continuum Removed` and the hull, `Convex Hull <name>`. Overall
brightness and slope are removed; the position, depth and shape of every
absorption band remain.

That is what makes a sunlit and a shadowed pixel of the same material
comparable, and what makes two spectra from different instruments comparable at
all. It is also the first step
{doc}`Spectral Feature Fitting <data-analysis-tools/spectral-feature-fitting>`
performs internally. Full detail:
{doc}`Continuum Removal <data-analysis-tools/continuum-removal>`.

The same operation runs on a whole cube: right-click the image and choose
**Continuum Removal: Image**.

---

## Exporting spectra

- **Right-click a spectrum ▸ Save to file...** — one spectrum as text
- **ROI context menu ▸ Export all spectra in ROI...** — every pixel's spectrum
  in a region. See {doc}`Regions of Interest <regions-of-interest>`.
- **Save Project** — collected spectra, the active spectrum and imported
  libraries all travel in a `.wiserproj`

---

## See also

- {doc}`Tutorial 2 — Reading Spectra <../tutorials/02-spectra>`
- {doc}`Spectrum Plot internals <../developer-content/spectrum-plot>`
