Files
pyrometry/grab_spectrum.py
T
2026-09-10 15:11:55 +01:00

50 lines
1.7 KiB
Python

#!/usr/bin/env python
"""Grab one spectrum from the first available Ocean Optics spectrometer.
Prints the peak and, with --response, applies a relative-response calibration.
Usage: python grab_spectrum.py [integration_us] [out.csv] [--response R.csv --dark D.csv]
"""
import argparse
import numpy as np
from seabreeze.spectrometers import Spectrometer
from response import add_calibration_args, apply_response, dark_at, load_csv
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("integration_us", nargs="?", type=int, default=50000,
help="integration time in microseconds (default: 50000)")
parser.add_argument("outfile", nargs="?", default=None,
help="optional CSV to write (wavelength, value)")
add_calibration_args(parser)
args = parser.parse_args()
cal = load_csv(args.response) if args.response else None
dark = dark_at(args.dark, args.integration_us)
spec = Spectrometer.from_first_available()
spec.integration_time_micros(args.integration_us)
wl = spec.wavelengths()
y = spec.intensities(correct_nonlinearity=True)
print(f"{spec.model} serial={spec.serial_number} pixels={spec.pixels}")
if cal is not None:
y = apply_response(wl, y, cal, dark)
unit = "relative_irradiance"
print(f"applied response {args.response}" + (f" + dark {args.dark}" if dark else ""))
else:
unit = "counts"
pk = int(np.nanargmax(y))
print(f"integration: {args.integration_us} us")
print(f"range: {wl[0]:.2f} - {wl[-1]:.2f} nm")
print(f"peak: {wl[pk]:.2f} nm {unit}={y[pk]:.4g}")
if args.outfile:
np.savetxt(args.outfile, np.column_stack([wl, y]),
delimiter=",", header=f"wavelength_nm,{unit}", comments="")
print(f"wrote {args.outfile}")
spec.close()