  Grating-based spectrographs suffer from smile and keystone distortion, which
are problematic for hyperspectral data applications. Due to this, spectral
lines will appear curved and roughly parabola-shaped. Smile and keystone need
to be measured and corrected for accurate spectral and spatial calibration. In
this paper, we present a novel method to accurately identify and correct curved
spectral lines in an image of a spectrum, using a clustering algorithm we
developed specifically for grating spectrographs, inspired by K-means
clustering. Our algorithm will be used for calibrating a multi-object
spectrograph (MOS) based on a digital micromirror device (DMD). For each
spectral line in a spectrum image, our algorithm automatically finds the
equation of the parabola which models it. Firstly, the positions of spectral
peaks are identified by fitting Gaussian functions to the spectrum image. The
peaks are then grouped into a given number of parabola-shaped clusters: each
peak is iteratively assigned to the nearest parabola-shaped cluster, such that
the orthogonal distances from the parabola are minimized. Smile can then be
measured from the parabolas, and keystone as well if a marked slit is used. Our
method has been verified on real-world data from a long-slit grating
spectrograph with sub-pixel error, and on simulated data from a DMD-based MOS.
Compared to traditional approaches, our method can measure distortions
automatically and accurately while making use of more spectral lines. With a
precise model and measurement of distortion, a corrected hyperspectral data
cube can be created, which can be applied for real-time data processing.
