$ show astropy/specutils
specutils is an Astropy-affiliated Python library providing a unified framework for representing, reading, writing, analyzing, and manipulating 1D astronomical spectra. Its architecture centers on the `Spectrum` class, which encapsulates flux, spectral axis (via WCS), uncertainty, and mask data. The library is structured as a monolith with well-defined subpackages for spectra, IO, analysis, manipulation, and fitting, heavily leveraging the Astropy ecosystem (units, modeling, WCS, NDData) and GWCS. In production, specutils serves as a critical bridge between raw astronomical data and scientific analysis. Its primary data flow involves ingesting spectra from dozens of survey-specific FITS formats (SDSS, JWST, DESI, APOGEE, HST, etc.) via a plugin-based IO registry, transforming them into a standard `Spectrum` object, applying analysis (line flux, centroid, FWHM, template matching) or manipulation (resampling, smoothing, region extraction), and optionally writing results. The library handles complex real-world data concerns including uncertainty propagation, mask handling, and multi-dimensional flux arrays. The codebase demonstrates high craftsmanship through several genuinely reusable components: the `QuantityModel` wrapper for unit-aware Astropy modeling, the `SpectralRegion` class with interval algebra (union, intersection, XOR), the `data_loader` decorator pattern for extensible IO, and a family of resampling algorithms using the Strategy pattern. While some survey-specific loaders are inherently fragile due to format evolution, the core abstractions are robust, well-tested, and exhibit non-obvious domain expertise. Maintenance is active with recent commits and modern dependency pinning, resulting in low technical drift.
The LLM identified 6 files worth highlighting for reuse:
Wraps an `astropy.modeling.Model` to automatically handle input/output unit conversions. This solves a fundamental impedance mismatch in Astropy where models are unitless but data carries units. It is a clean, isolated, and highly reusable pattern for any project using Astropy modeling with physical quantities.
Implements a decorator-based plugin registration system (`data_loader`, `custom_writer`) for reading and writing spectra. This allows new file formats to be seamlessly integrated into the `Spectrum.read()` / `Spectrum.write()` interface. The pattern is a textbook example of the Plugin and Decorator patterns in Python, highly reusable for any project needing extensible IO.
Defines `SpectralRegion` and `CompoundSpectralRegion` classes that support logical operations (union, intersection, XOR) on 1D spectral intervals. This is a well-encapsulated data structure with a clear algebra, reusable in any domain requiring boolean operations on intervals.
Provides a family of resampling algorithms (`FluxConservingResampler`, `LinearInterpolatedResampler`, `SplineInterpolatedResampler`) using a base class and strategy pattern. The flux-conserving resampler is a non-trivial, domain-specific algorithm implemented cleanly. This module is a strong example of the Strategy pattern for numerical algorithms.
Contains `gwcs_from_array` for constructing spectral WCS from tabular data, and `vac_to_air`/`air_to_vac` for wavelength conversion. These are self-contained, well-tested utilities solving recurring problems in astronomical spectroscopy.
A sophisticated, parameterized FITS loader framework for the Data Central survey archive. It maps FITS HDUs to `Spectrum` objects using configurable WCS, units, and purpose keywords. Demonstrates a highly reusable pattern for building generic survey data loaders.