monochromator.py#

This script collects Ocean Optics spectrometer measurements of a spectral light source (ie. monochromator), processes them and saves into a single H5 file.

monochromator.main(args=None, **kw)[source]#

Collate and rebin monochromator output spectral irradiances.

  • Scans the input directory for Ocean Optics spectrometer files named {wavelength} ND{-density}.txt

  • Loads them with the OOSpectrum class

  • The spectra are rebinned according to range and step.

  • Noise levels are estimated from the electronic dark pixels

  • The data are saved using the MonochromatorSpectra class.

If called without parameters, parses arguments from the command line. Arguments may be passed as a list of strings or as keyword arguments. E.g.:

main(['-i','data/path/','--quiet'])
# or
main(input='data/path/',quiet=True)
Parameters:
  • range (-r, --range, [float, float], default (250.0, 850.0)) – (min, max) of output bin centers, inclusive.

  • step (-s, --step, float, default 1.0) – output bin width. Note: the range will be divided into 1 + int((max - min)/step) bins.

  • input (-i, --input, Path) – path to directory containing spectra files (OceanOptics spectrometer text output files)

  • output (-o, --output, Path) – output h5 file path

  • quiet (--quiet, bool) – suppress status message printing

class monochromator.MonochromatorSpectra(path: pathlib.Path | None = None, load_raw: bool = False)[source]#

Class for loading and saving monochromator output spectral irradiances.

Parameters:
  • pathNone or path to h5 file to load.

  • load_raw – if True, loads raw spectra in addition to processed spectra.

spectrometer: str#

Spectrometer model and serial number

date: str#

Date string of measurements

nom_wl: NDArray#

Nominal peak wavelength for each spectrum. Shape (N).

wl: NDArray#

Wavelengths for each sample in the spectrum. Shape (M).

wl_units: str#

Units of nom_wl and wl.

mean: NDArray#

Mean spectrum at each nominal peak wavelength. Shape (N,M).

std: NDArray#

Standard deviation of spectrum at east nominal peak wavelength. Shape (N,M).

units: str#

Units of mean and std.

n: int#

Number of spectra averaged for each nominal peak wavelength.

scale: float#

Scale of mean and std

raw_dark_pixels: tuple[int, int]#

(begin,end) indices of optically masked dark pixels. dark = raw_irrad[:, begin:end]

raw_optical_density: NDArray#

Optical density attenuating each spectrum at each nominal peak wavelength. Shape (N).

raw_wl: NDArray#

Wavelengths for each sample in the raw spectrum. Shape (K).

raw_wl_units: str#

Units of raw_wl.

raw_n: int#

Number of spectra averaged (within the spectrometer software) for each nominal peak wavelength.

raw_irrad: NDArray#

Raw spectral measurements. Shape (N, K).

raw_irrad_units: str#

Units of raw_irrad

raw_edc: bool#

True if the spectrometer’s internal dark correction was applied.

raw_nlc: bool#

True if the spectrometer’s internal non-linearity correction was applied.

load(path: Path, load_raw: bool = False)[source]#

Load data from path.

Load data from H5 file at path. Missing items will be set to None. self.raw_* items will only be loaded if load_raw is True.

save(path: Path)[source]#

Save data to path.

Save data to h5 file at path. Existing files will be overwritten. Raw data will only be stored if raw_wl and raw_irrad are not None.

normalize(mode: Literal['peak', 'total'] = 'peak')[source]#

Normalize peak or total value to 1. mode is ‘peak’ or ‘total’

denormalize()[source]#

Remove scaling from normalize.

class monochromator.OOSpectrum(path: Path)[source]#

Load and parse OceanOptics spectral data.

Parameters:

path – Path to Ocean Optics spectrometer data file to load.

metadata: dict[str, str]#

metadata loaded from the file header section

raw_wls: NDArray#

raw wavelengths (not resampled). shape (K)

raw_irrad: NDArray#

raw irradiance data (not resampled). shape (K)

wls: NDArray#

wavelengths (after resampling by rebin). shape (M)

irrad: NDArray#

irradiance (after resampling by rebin). shape (M)

irrad_std: NDArray#

irradiance noise estimate. shape (M)

wl_units: str#

units for raw_wls and wls

irrad_units: str#

units for irrad and raw_irrad

spectrometer: str#

spectrometer model and serial

date: str#

date string of measurement

time: float#

integration time

count: int#

number of spectra averaged per sample

edc: bool#

True if the spectrometer’s internal dark correction has been applied.

nlc: bool#

True if the spectrometer’s internal nonlinearity correction has been applied

boxcar: int#

number of pixels averaged by the spectrometer’s internal boxcar filter.

dark_pixels: tuple[int, int]#

range of dark pixels – hardcoded for the Ocean Optics USB4000

rebin(new_bins, nd=None, correct_dark=True, compute_noise=False)[source]#

rebin spectral data using rebin_samples and estimate noise from dark pixels.

resampled data are stored in wls, irrad and irrad_std.

Parameters:
  • new_bins – As per rebin_samples

  • nd – if not None, the irradiance is divided by 10**nd to account for a neutral density filter’s transmittance.

  • correct_dark – if True use the optically masked dark pixels to correct the irradiance

  • compute_noise – if True use the optically masked dark pixels to estimate the temporal noise (N.B. does not model shot noise!)

monochromator.bin_edges(centers, axis=0)[source]#

Compute edges midway between center points along axis.

monochromator.rebin_samples(centers, samples, new_bins=None, input_density=False, output_density=False)[source]#

Rebin binned samples.

  • Assumes each sample is the integral of f(x) over each bin’s limits.

  • Assumes the bin edges are evenly spaced between the bin centers.

New sample values are taken by interpolating the cumulative sum of the samples at new bin limits.

Parameters:
  • centers (array_like, 1D) –

  • samples (array_like, same shape as bin_centers) –

  • new_bins (None, scalar, or array_like) – if None: uses the same centers if scalar: uses arange(centers[0], centers[-1], new_bins) if array_like: new centers

  • input_density (boolean) –

    if True, indicates that the input is normalized by the bin widths.

    e.g. for a photospectrometer the input’s units are photons/nm rather than just photons

  • output_density (boolean) – if True, indicates that the ouput should be normalized by the bin widths

Returns:

  • new_centers (ndarray)

  • new_samples (ndarray)