Automated SpectroPhotometric Image REDuction (ASPIRED)

We provide a suite of public open-source spectral data-reduction software to rapidly obtain scientific products from all forms of long-slit-like spectroscopic observations. Automated SpectroPhotometric REDuction (ASPIRED) is a Python-based spectral data-reduction toolkit. It is designed to be a general toolkit with high flexibility for users to refine and optimize their data-reduction routines for the individual characteristics of their instruments. The default configuration is suitable for low-resolution long-slit spectrometers and provides a quick-look quality output. However, for repeatable science-ready reduced spectral data, some moderate one-time effort is necessary to modify the configuration. Fine-tuning and additional (pre)processing may be required to extend the reduction to systems with more complex setups. It is important to emphasize that although only a few parameters need updating, ensuring their correctness and suitability for generalization to the instrument can take time due to factors such as instrument stability. We compare some example spectra reduced with ASPIRED to published data processed with iraf-based and STARLINK-based pipelines, and find no loss in the quality of the final product. The Python-based, iraf-free ASPIRED can significantly ease the effort of an astronomer in constructing their own data-reduction workflow, enabling simpler solutions to data-reduction automation. This availability of near-real-time science-ready data will allow adaptive observing strategies, particularly important in, but not limited to, time-domain astronomy.


INTRODUCTION
With major global investments in multiwavelength and multimessenger surveys, time-domain astronomy is entering a golden age. In order to maximally exploit discoveries from these facilities, rapid spectroscopic follow-up observations of transient objects (e.g., supernovae, gravitational-wave optical counterparts, etc.) are needed to provide crucial astrophysical interpretations.
Part of the former OPTICON 1 project coordinates the operation of a network of largely self-funded European robotic and conventional telescopes, collating common science goals and providing the tools to deliver science-ready photometric and spectroscopic data in near real time. The goal is to facilitate automated or interactive decision-making, allowing "on-the-fly" modification of observing strategies and rapid triggering of other facilities. As part of the network's activity, a software development work package was commissioned un-der the working title of Automated SpectroPhotometric REDuction (ASPIRED), coordinated on Github 2 .
The "industrial standard" of spectral and image reduction is undoubtedly the iraf framework (Tody 1986(Tody , 1993. It has powered many reduction engines in the past and present. However, unfortunately, the National Optical Astronomy Observatory discontinued the support of the software in 2013, and it is now entirely relying on community support 3 . The STARLINK library 4 (Currie et al. 2014;Berry et al. 2022) includes substantial resources for data-reduction tasks in the Figaro package. It was first initiated in 1980 under the STAR-LINK Project, which became defunct in 2005, but the software survived, and its maintenance effort has since transferred to the East Asian Observatory. It also comes with a Python wrapper 5 to enable low-level code access from high-level programs. These software tools have made significant contributions to the entire astrophysics community. They are very often overlooked when they are so deeply embedded into many systems, and users do not interact directly with them in this era of big data and high data rates. However, such tools are not always available. Many smaller facilities and individual users are still required to commission their own datareduction routines. Existing powerful libraries and tools also come with a few drawbacks; the most notable obstacles concern the ease of installation and linkage of libraries, which users often find difficult.
In this generation of user-side astronomy data handling and processing, as well as the emphasis on computing courses for scientists, Python is among the most popular languages due to its ease to use with a shallow learning curve, readable syntax and simple way to "glue" different pieces of software together. Its flexibility to serve as a scripting and an object-oriented language makes it useful in many use cases: developing visual tools with little overhead, prototyping, web-serving, and compiling if desired. While this broad range of functionality and high-level usage make it relatively inefficient. Python is an excellent choice of language for building wrappers on top of highly efficient and well-established codes. Some of the most used packages, scipy (Virtanen et al. 2020) and numpy (Harris et al. 2020), are written in Fortran and C, respectively, to deliver high performance. Multithreading and multiprocessing are also possible with built-in and other third-party packages, such as mpi4py (Dalcin et al. 2011).
Various efforts are being made to develop software for the current and next generation of spectral data reduction. For example, PypeIt (Prochaska et al. 2020b,a) is designed for tailor-made reduction for a range of instruments, and PyReduce (Piskunov et al. 2021) is designed for optimal echelle spectral reduction (but only handles sky subtraction if a sky frame is provided). In the Astropy "universe" (Astropy Collaboration et al. 2013Collaboration et al. , 2018, specreduce 6 (Pickering et al. 2022) is likely to be the next-generation, user-focused data-reduction package of choice, but it is still in early stages of deployment at the time of writing. pyDIS 7 has all the essential ingredients for reducing spectra but has been out of maintenance since 2019, and specutils handles spectral analysis and manipulation but not the reduction itself.
In ASPIRED, we have a different vision of how and what should be abstracted from the users. Instead of providing a ready-to-go "black box" suited for a specific instrument or observations made under particular conditions, we provide a toolkit that is as general as possible for users to have a set of high-level data-reduction building blocks with which process the data in the ways most appropriate to their instruments and observations. This shifts more of the work and maintenance to the user end, but allows rapid modification of the data-reduction workflow to any changes in the instrumental configuration (such as a detector being refitted, or the detector plane shifted and rotated by a few pixels during engineering work) and to new instruments. ASPIRED is thus especially useful for works that repeatedly make use of multiple spectrographs requiring rapid follow-ups. Using ASPIRED only requires basic coding skills acquired by most graduate-level astronomers. Similar to iraf, PypeIt and other open-source software, we welcome and encourage users to provide feedback as well as to contribute to the code base.
We use the SPRAT spectrograph (Piascik et al. 2014) mounted on the Liverpool Telescope (LT) as our "first-light" instrument for development. AS-PIRED is currently used by the transient science research group at Tel Aviv University for a proprietary spectral data-reduction pipeline of the Las Cumbres Observatory FLOYDS instrument. It is also known to be used for quick-look reduction of data obtained by the MISTRAL spectrograph mounted on the 1.93 m telescope at the Observatoire de Haute-Provence 8 . However, as mentioned, we aim to allow high-level tools for users to build and fine-tune their pipelines to support a wide range of configurations . As of the time of writing, we have used ASPIRED to reduce data from the William Herschel Telescope Intermediate-dispersion Spectrograph and Imaging System 9 (WHT/ISIS) and the Auxiliary-port CAMera (ACAM; Benn et al. 2008), the Las Cumbres Observatory FLOYDS (Las Cumbres/FLOYDS; Brown et al. 2013), the Gemini Observatory Gemini Multi-Object Spectrographs's longslit mode (Gemini/GMOS-LS; Hook et al. 2004), the Gran Telescopio Canarias Optical System for Imaging and low-Intermediate-Resolution Integrated Spectroscopy (GTC/OSIRIS; Cepa et al. 2000), the Telescopio Nazionale Galileo Device Optimised for the LOw RESolution 10 (TNG/DOLORES; Molinari et al. 1999), the Very Large Telescope FOcal Reducer/low dispersion Spectrograph 2 (VLT/FORS2); and the Southern Astrophysical Research Telescope Goodman High Throughput Spectrograph (SOAR/GHTS Clemens et al. 2004).
We show examples of these reductions in Section 5 11 , and excerpts of example codes in Appendix A.
This article is organized as follows. Section §2 covers the development and organization of the software. Then, we discuss the details of the spectral image-reduction procedures in Sections §3 and 4. In Section §5 we show a few example spectra and compare then with published reductions. Section §6 explains how ASPIRED can be installed. Finally in Section §7, we summarize and discuss future plans.
This article is not intended to serve as an API document 12 , nor as a review of the various methods concerning spectral extraction. Only the high-level descriptions and the scientific and mathematical technicalities that are important to the data-reduction processes are discussed here.

DEVELOPMENT AND STRUCTURE OF
ASPIRED One of the development goals of ASPIRED is to design a piece of software that is as modular and portable as possible, such that it is operable on Linux, Mac and Windows systems (while software for astronomy and astrophysics are strongly leaning toward Unix systems, Windows has the highest market share worldwide; furthermore, demographically it is the common choice among university-managed computing systems, students and lower-income countries). On top of that, AS-PIRED is designed to rely on as few external dependencies as possible. The ones we do use are those that would require a substantial programming effort to reproduce and have a proven track record of reliability and/or plan to remain maintained in the foreseeable future. The explicit top-level dependencies areastros-crappy (van Dokkum 2001;McCully et al. 2018) Astropy (Astropy Collaboration et al. 2013, ccdproc (Craig et al. 2017), numpy (Harris et al. 2020) plotly (Plotly Technologies Inc. 2015), rascal (Veitch-Michaelis & Lam 2020a), scipy (Virtanen et al. 2020), spectresc (Carnall 2017; Lam 2023), specutils (Earl et al. 2023), and statsmodels (Seabold & Perktold 2010).
We host our source code on Github, which provides version control and other utilities to facilitate its development. It uses git 13 , issue and bug tracking, high-level project management, and automation with Github Actions upon each commit for the following: The initial work package divided the project broadly into three high-level independent components, which we now detail. GRAPHICAL USER INTERFACE (NOT IN ACTIVE DEVELOPMENT) In the beginning, we commissioned a prototype of the graphical user interface 17 with Electron 18 , which wraps on top of ASPIRED without needing to adapt the code. Such a setup works seamlessly for a few reasons. First, Python is an interpreter; it can execute in run time. It is hence straightforward to run a Python server to interact with an Electron instance. Second, Plotly 19 has a good balance in generating interactive and static plots; it requires little effort to interact with both Python and Electron. Third, there is a JavaScript version of SAOImageDS9 20 , JS9, that allows rapid and easy interactive image manipulation with an interface that astronomers are accustomed to (Joye & Mandel 2003;Mandel & Vikhlinin 2022). Electron and JS9 work like a web service; this prototype also shows that an ASPIRED graphical user interface can be deployed as an online tool. Such a high level of interactivity is, however, no longer in active development and is only served as a one-off technology demonstration of its capability.

ARC FITTING FOR THE PIXEL-WAVELENGTH RELATION (A CONCURRENT DEVELOPMENT)
The wavelength calibration process involves identifying distinctive emission lines from an arc-lamp spectrum, to which a function such as a polynomial can be fit to map the pixel position to the wavelength value. Manual calibration is straightforward but tedious: first, a user has to locate the peaks of an arc spectrum in pixel space. Then, they have to assign a wavelength to each peak based on the emission lines present from the element(s) of the arc lamp. Finally, the function to map pixel values to a set of wavelengths is found. Typically a list of known strong calibration lines is used, as provided by the instrument operators in the form of an atlas or a template calibration spectrum. Automation via template matching is a viable and commonly used approach for spectral calibration if the instrument configuration is stable (Prochaska et al. 2020a). However, this introduces a strong dependency on instrument-specific properties (such as the vacuum/contamination condition of the lamp, the combination of elements in the lamp, the vignetting on the focal plane, the response as a function of wavelength across the detector, and saturation issues). There is also noise in peak-finding routines, for example due to detector noise or quantization (e.g. not using subpixel peak finding), as well as complications due to blended lines. Providing a general, template-free tool for such a purpose for a range of specifications is far from easy, but would be much more flexible for cases when templates are unavailable or the instrument configuration changes often.
In ASPIRED, the wavelength calibration module is powered by the RANdom SAmple Consensus (RANSAC) Assisted Spectral CALibration (rascal, Veitch-Michaelis & Lam 2020b,a, Veitch-Michaelis & Lam, in preperation) library, a concurrent software development project. rascal does not require templates and is designed to work with an arbitrary instrument configuration. Users are only required to supply a list of calibration lines (wavelengths), a list of peaks (pixels), and some knowledge (e.g. minimum and maximum wavelength limits) of the system to obtain the most appropriate pixel-to-wavelength solution. rascal builds on the work by Song et al. (2018), which searches only for plausible sets of arc line/peak correspondences that follow a linear approximation. RANSAC (Fischler & Bolles 1981) is used to fit a higher-order polynomial model to the candidate correspondences robustly. rascal extends this strategy to consider the top N sets of linear relations simultaneously: for each peak (pixel), the most common best-fit atlas line (wavelength) is chosen from those N subsets that have a merit function better than a given threshold. This acts like a piecewise linear fit and allows us to extract most of the correct matches from both the red and blue ends of the spectrum -a feature that was not available in Song et al. (2018).

DATA REDUCTION AND OTHER CALIBRATIONS (THIS WORK)
This is the core of ASPIRED, which mainly handles the spectral extraction and calibration processes. At the user level, the software is organized into two main parts: image reduction and spectral reduction. They are designed to be as modular as possible. Users are free to use third-party reduction tools to perform part(s) of the reduction process and return to continue the reduction with image reduction and spectral reduction.
In addition, the data structure class SpectrumOneD handles the data and metadata of the extracted informa-tion. This provides a uniform format to store the metadata and the extracted data throughout the entire spectral extraction process. Since multiple spectra can be extracted from a TwoDSpec object (e.g., extended target or multiple targets aligned on the slit), each extracted spectrum is stored in an independent SpectrumOneD object that gets passed from a TwoDSpec object. In astronomy, the handling of observational data is almost exclusively done with FITS files, but they are not human-readable. In view of that, ASPIRED provides input and output handling of FITS files, and outputs in a CSV format. All the higher-level functions are wrappers on top of a spectrumOneD object.

IMAGE REDUCTION
ASPIRED only intends to provide image-reduction methods for bias, dark, and flat corrections. It is sufficient for simple instrumental setups, which are the primary targets of ASPIRED, e.g., a long-slit spectrograph with a rectilinear two-dimensional spectrum illuminated on the detector plane, i.e., where the dispersion and spatial directions are perfectly perpendicular to each other across the entire detector plane. In systems with more complex optical designs, for example instruments with multiple nonparallel spectra illuminated on the detector plane, ASPIRED is not immediately usable. Extra image manipulation is needed to further transform the data into simpler forms that ASPIRED can handle. In the FLOYDS reduction example below, we demonstrate that it is possible to extract two nonparallel curved spectra from a single two-dimensional image. This is possible only because the two spectra (first-order red and second-order blue) that are simultaneously imaged by the detector can be cleanly separated before spectral reduction, and each spectrum is treated independently.
This module allows mean-, median-, and sumcombining, subtracting and dividing by dark, bias, and flat frames. The FITS header of the first light frame provided is retained in the reduced product, while the headers from the other frames are not stored -only the file paths are appended to the end of the FITS header. The FITS file is defaulted to contain an empty PrimaryHDU with the process data stored in the first ImageHDU extension as recommended in the FITS Standard document 21 , but it can also be stored directly in the PrimaryHDU as most users opt for. Many forms of interactive and static images can be exported, powered by the Plotly image renderer. 21 https://fits.gsfc.nasa.gov/fits documentation.html This module only allows basic image reduction; users have to rely on third-party software to correct for any other effects before continuing the reduction with spectral reduction.

SPECTRAL REDUCTION
In the highest-level terms, ASPIRED's spectralreduction procedures after image reduction include (i) identifying and tracing the spectrum(a), (ii) correcting for image distortion, (iii) extracting the spectrum(a), (iv) performing wavelength calibration of the science target and a standard star, (v) computing the sensitivity function using the standard star, (vi) performing atmospheric extinction correction; and, finally (vii) removing the telluric absorption features, (viii) resampling, and (ix) exporting the data. We omit a few other processes, which we believe should be considered as part of the postextraction processes or spectral analysis and manipulation. There are many sophisticated software packages with good and long track records to handle these processes post hoc. For example, sky-glow subtraction with Skycorr (Noll et al. 2014), or telluric absorption removal without supplementary calibration data with MoleFit (Smette et al. 2015; Kausch et al. 2015).
The spectral reduction is a large module that covers both the two-dimensional and one-dimensional operations through the TwoDSpec and OneDSpec classes, respectively.
The former provides image rectification, spectral tracing, spectral extraction of the target and the arc, and locating the peaks of the arc lines. The latter performs wavelength calibration (with the WavelengthCalibration class), flux calibration (with the FluxCalibration class), atmospheric extinction correction, telluric absorption correction, and spectral resampling. Diagnostic images can be exported at each step of the two-and one-dimensional operations. This also includes the wavelength calibration diagnostic that wraps around rascal and uses its native plotting capability. Since the flux calibration has to be performed with a standard star spectrum, it is necessary for the OneDSpec object to have information on both the target and the standard onedimensional spectra. Thus, a OneDSpec object keeps a dictionary of lists:science spectrum list and standard spectrum list. At the time of writing, AS-PIRED only supports wavelength calibration from one (total stacked) arc frame and flux calibration from one (total stacked) standard star frame.
The following describes the technical details of the nine reduction steps mentioned above. We use the following convention throughout this article: the spectral image is dispersed on the detector plane such that the left side (small pixel number) corresponds to the blue part of the spectrum and the right side (large pixel number) to the red part in the dispersion direction, while the direction perpendicular to it is the spatial direction.
spectral reduction accepts images that are rotated or flipped compared to the description above, but extra arguments have to be provided in the initialization step to set the data into the orientation as defined above.

Spectral Identification and Tracing
Conventionally, to obtain the position of a spectrum in a frame (i.e., a trace), the center of the spectrum in the dispersion direction is first identified. Then, a choice of algorithm scans across the spectral image to identify the spectral position over the entire dispersion direction. In ASPIRED, the ap trace function works by dividing the two-dimensional spectrum into subspectra. Then, each part is summed along the dispersion direction before cross-correlating with the adjacent subspectra, using an optional variable scaling factor that models a linear change in the width of the trace (this optional scaling factor is turned off by default). This allows both the shift and the scaling of the spectrum(a) along the dispersion direction to be found simultaneously. The middle of the two-dimensional spectrum is used as the zero-point of the procedure. This tracing procedure is illustrated in Figure 1, and described in more detail as follows: 1. The input two-dimensional spectrum is divided into nwindow overlapping subspectra.
2. Each subspectrum is summed along the dispersion direction in order to improve the signal(s) of the spectrum(a) along the spatial direction to generate the line-spread function of that subspectrum.
3. Each line-spread function is upscaled by a factor of resample factor to allow for subpixel correlation precision. This utilizes the scipy.signal.resample function to fit the spatial profile with a cubic spline.
4. The ith line-spread function is then crosscorrelated to the (i + 1)th line spread function.
The shift and scale correspond to the maximum correlation within the tolerance limit, tol, and are stored.
5. The shifts relative to the central subspectrum are used to fit for a polynomial solution with a constant arbitrary offset.
6. While the spatial spectra are cross-correlated, they are also aligned and stacked to find the peak of the line-spread profile. Peak finding is performed with scipy.signal.find peaks and returns the list of peaks sorted by their prominence, which is defined as the maximum count of the peak subtracted by the local background. The total stacked line-spread profile is fitted with a Gaussian function to get the spread of the spectrum (standard deviation). This would not be used in the case of top-hat extraction (see section §4.3).
Manually supplying a trace (determined a priori or copied from another observation) is possible with the add trace function. This is particularly useful for faint and/or confused sources that are beyond the capability of automated tracing. For example, if there is a faint transient source on top of a galaxy, a manual offset may be needed for extraction if the host is dominating the flux to a level that forced extraction is necessary. For an optical design where the source spectrum is always illuminated at the same pixels of a detector, it is possible to reuse the trace from the standard frame for spectral extraction of the science target.
In the cross-correlation process, ASPIRED supports the upsampling of the data. Because more than one target could be incident on the slit, we do not attempt to fit any profile at this stage. Hence, the shifts found are always quantized to an integer pixel level. However, the polynomial fitted through the shifts as a function of the dispersion direction is a continuous function. The final precision depends on the upsampling rate. If no upsampling is performed and the spectrum is tilted by one pixel across the entire range of dispersion, for example, this step will not be able to get the tilt correctly. By default, we set the upsampling rate to four. This should be sufficient to correct for a tilt in the spectrum as little as 0.25 pixels across the entire range of dispersion.

Image Rectification
In some cases, a spectrum on the detector plane is not only tilted, but it is also sufficiently distorted that the spatial and dispersion direction (in the detector's Cartesian coordinates) are no longer orthogonal to each other. While this does not cause any complication in the tracing process, the extraction has to be performed along a curve so that the process is no longer a onedimensional process. Without proper weighting in extracting flux at the subpixel level, significant under-or oversubtraction of the sky background can occur. More complex methods have to be used to extract the spectra directly (e.g. Piskunov et al. 2021), as implemented in PyReduce, which can optimally extract highly dis- Top: a significantly tilted spectrum is used to illustrate how cross-correlation is used to find the shift between neighboring subspectra with a resampling factor of 1.0. The light gray, dark gray and black boxes are three example subspectra. Bottom left: the three subspectra are summed in the dispersion direction to generate the three profiles that are cross-correlated to compute the shift. Bottom right: the cross-correlated function for the light gray subspectrum relative to the dark gray subspectrum (plotted in alternating light and dark gray dashes), and the one for the black subspectrum relative to the dark gray subspectrum (plotted in alternating dark gray and black dashes). torted spectra. However, PyReduce cannot perform sky subtraction using the "wings" in the sky region on either side of the line-spread profile. This has limited the usage of PyReduce in typical single-frame extractions without accompanying sky observations exposed using an identical slit setting. In ASPIRED, a TwoDSpec object uses the spectral trace for alignment in the spatial direction, and uses the sky emission lines, and optionally coadded with the arc image, for alignment in the dispersion direction. This process is to perfectly align the two-dimensional spectral coordinate system with the detector's x − y axes. Coadding the arc frame for the procedure can improve the reliability of the rectification, as more bright lines are available in the coadded image for computing the rectification function that shifts and scales the image.
The rectification is done independently first in the spatial direction and then in the dispersion direction. In the spatial direction, the process only depends on the trace. Each column of pixels is (scaled and) shifted by resampling it to align to the center of the spectrum (middle  Figure 2. A two-dimensional spectrum from Las Cumbres/FLOYDS is used to demonstrate the rectification procedure. The dotted white line is aligned in the spatial direction to serve as a visual guide. Top: the trimmed image of a FLOYDS spectrum. Middle: the two-dimensional spectrum is resampled in the spatial direction based on the polynomial function of the trace. Bottom: the two-dimensional spectrum is resampled in the dispersion direction. The shifts and scales are found by cross-correlating the subspectra divided in the spatial direction. This is perpendicular to the tracing process. panel of Fig. 2). This process usually produces a spectrum that is tilted or curved in only the dispersion direction. Therefore, a similar procedure has to be performed in the dispersion direction. However, lines are not traced in this direction. In order to find the size of the scaling and shifting, we repeat the tracing procedures in Sec. 4.1 from steps 1 through 5 in the perpendicular (dispersion) direction. A first-or second-order polynomial is usually sufficient to fit the shift as a function of the number of pixels from the trace. Then, the spectral image is resampled a second time but only in the dispersion direction. The final result is shown in the bottom panel of Fig. 2.
At the time of writing, this process only works on a single trace. If more than one trace is found/provided, only the one with the highest prominence will be processed, where the prominence 22 is defined as the maximum count of the peak subtracted by the local background. The resampling is performed with spectresc.
It is possible to supply a set of precomputed polynomials to perform the rectification, which can significantly speed up the data-reduction process of a stable optical system.

Spectral Extraction
There are a few commonly used extraction methods; some work for all kinds of spectral images, and some only work with specific observing strategies (e.g. the flat-relative optimal extraction; Zechmeister et al. 2014). The standard textbook method is commonly called the top-hat extraction or the normal extraction. It simply sums the electron counts over a given size of the aperture and is robust and easy to use. However, this method does not deliver the maximal signal-to-noise ratio (S/N) from the available data. Various optimal extraction algorithms can maximize the S/N. They work by downweighting the wings of the spectral profile, where almost all the photons come from the sky background rather than the source (see Fig. 3). The improvement is particularly significant for background-limited sources, which are the case in most observations (see Fig. 4). The extracted spectra and their associated uncertainties and sky-background counts can be plotted for inspection. The residual image can also be exported for diagnostics.

TOPHAT/NORMAL EXTRACTION
The top-hat extraction does not weight the pixels for extraction, so every pixel has an equal contribution to the source count. Thus, it is very robust in obtaining the total electron count across a slice of pixels. The sky-background count can be extracted from the regions outside the extraction aperture to be subtracted from the spectrum.

HORNE-86 OPTIMAL EXTRACTION
Horne (1986, hereafter H86) is the golden standard of optimal extraction of spectra from modern electronic detectors. We follow the H86 process except for the profile modelling, where we provide three options: the first is a fixed Gaussian profile, the second uses LOcally Weighted Scatterplot Smoothing (LOWESS; Cleveland 1979) regression to fit for a polynomial, and the third accepts a manually supplied profile.
The default option is to use the Gaussian fit because it is more robust against noise, cosmic-ray contamination, and artifacts. It is particularly useful in extracting a faint spectrum when the image is dominated by background noise as the Gaussian profile is constructed by fitting the line-spread function from the total stack of all the subspectra (as described in Sec. 4.1). LOWESS, on the other hand, would do a better job in fitting the profile of a resolved galaxy. In any case, the quality of the  extraction from a valid profile should be at least as good as that performed with the top-hat extraction method.

MARSH-89 OPTIMAL EXTRACTION
Marsh (1989, hereafter M89) extends on the H86 algorithm by fitting the change in the shape and centroid of the profile from one end of the spectrum to the other. It is very suited for extracting a highly tilted spectrum where the tilting direction is aligned with one direction in the x-axis or the y-axis of the detector. The algorithm still relies on the assumption that the spatial and dispersion directions are orthogonal across the entire frame (in the detector pixel coordinates). In ASPIRED, we  adapt Prof. Ian Crossfield's set of public python 2 code for astronomy 23 to python 3.

Wavelength Calibration
As mentioned, the wavelength calibration is powered by rascal, which is a concurrent development to this work. All the public functions from rascal are available in ASPIRED so that users can have fine control over the calibration. It works by applying a Hough transform to a set of peak-line (pixel-wavelength) pairs (i.e., Hough pairs), and then identifying the most probable set of Hough pairs that can be described by a polynomial function which satisfies the required residual tolerance limit (see more details in Veitch-Michaelis & Lam 2020a) The diagnostic plots available are the set provided by rascal. They include (i) the spectrum of the arc lamp extracted using the traces from the science and standard frames, wherein the peak of the arc lines are also marked; the residual of the solution, and the pixel-wavelength relation (Fig. 5).
Alternatively, the polynomial coefficients for the wavelength calibration can be supplied manually, which would be useful for stable instruments in which the variations in the dispersion are negligible. All the lines in the range of 100 -30000Å available from the search form of the National Institute of Standards and Technology Physical Measurement Laboratory are included, but we strongly recommend providing arc lines manually since arc lamps, even of similar type, vary between manufacturers and the conditions.

Flux Calibration
ASPIRED is designed to address the common case where targets and reference standards are observed contemporaneously in close succession. It is well suited to constructing unsupervized, automated pipelines for stable and repeatable instruments. In such cases, an instrument sensitivity function can be created from several standard targets, stored and applied to many science target observations. At the time of writing, ASPIRED cannot construct a sensitivity function from multiple frames, but it can accept a user-supplied polynomial function to apply the calibration.
The process of deriving a sensitivity function is by comparing a wavelength-calibrated standard spectrum with the literature values to map the electron count to flux as a function of wavelength. All the standard stars available in iraf, on the Issac Newton Group of Tele-scopes web page, and on the European Southern Observatory (ESO) web page (Optical and UV Spectrophotometric Standard Stars) are available on ASPIRED 24 . We call the set of data a library, and provide below the complete listing of sources and references for each of the libraries where available.

EUROPEAN SOUTHERN OBSERVATORY
The ESO standard spectra are grouped into five sets, they can be downloaded from the ESO site. 25 .
1. esoctiostan -CTIO standards from Hamuy et al. (1992Hamuy et al. ( , 1994 2. esohststan -HST standards from Bohlin et al. (1995); Bohlin (1996) 3. esookestan -Oke standards from Oke (1990) 4. esowdstan -White dwarf standards from Bohlin et al. (1995) 5 The complete listing of iraf standards can be found online 27 . References are included where they are available.  Baldwin & Stone (1984) 4. irafctionewcal -The updated CTIO standards Hamuy et al. (1992Hamuy et al. ( , 1994 5. irafiidscal -KPNO IIDS standards Massey et al. (1988) 6. irafirscal -KPNO IRS standards Massey et al. (1988) 7. irafoke1990 -HST standards Oke (1990)  The calibration can be done in either AB magnitude or flux density (per unit wavelength). The two should give similar results, but the response functions found would not be equivalent because fitting to magnitudes is in logarithmic space and smoothing (see below) will have a different effect compared to flux fitting. SMOOTHING A Savitzky-Golay smoothing filter 28 (Savitzky & Golay 1964, hereafter, SG-filter) can be applied to the data before computing the sensitivity curve. This function works by fitting low-order polynomials to localized subsets of the data to suppress noise in the data at each point interval. It is similar to the commonly used median boxcar filter but uses more weighted information to retain information better while removing noise. This can be used independently or with continuum fitting (see below). By default, the smoothing is turned off, and when it is used it is defaulted to remove only significant noise (e.g. from unsubtracted cosmic-rays).

CONTINUUM FITTING
It is optional for the sensitivity function to be derived from the continuum of the standard spectrum. This removes absorption lines in the standard spectrum. This process is strongly dependent on both the resolution of the literature and the observed standards, as well as the absorption lines present in them. The continuum is found using the specutils's fits continuum. This can remove any outlying random noise and absorption lines when computing the sensitivity. Users are reminded to be cautious with this procedure as removing the absorption features in computing the sensitivity function can significantly affect the flux calibration near the absorption features.

SENSITIVITY FUNCTION
The sensitivity function is computed by dividing the observed standard spectrum by the literature one and then interpolating the result using a spline or a polynomial function (Fig. 6). This can be done with or without any presmoothing and/or continuum fitting. In case of a high-resolution literature standard spectrum (e.g. the ESO X-Shooter standards), the absorption-line profiles should be used as part of the sensitivity computation, while in the case of the standard stars from Oke (1990), which removed absorption lines when producing the standard spectra, a smooth continuum of the observed spectrum should be used for computing the sensitivity function. Users can also manually supply a sensitivity function as a callable function that accepts a wavelength value and returns the log of the sensitivity value at that wavelength. When such a function is provided manually, it is assumed to be in the units of flux per second. The extinction table of La Silla Observatory terminates at 9000Å. The rise at the red end of the curve is an undesired artefact due to extrapolation using a cubic spline. We are explicitly plotting this range to serve as a warning since we opt to preserve the raw data as provided without appending fake data points.

Atmospheric Extinction Correction
ASPIRED currently has four built-in atmospheric extinction curves (Fig. 7 These curves describe the extinction in magnitude per air mass as a function of wavelength, roughly between 3000 and 10000Å. Alternatively, a callable function in the appropriate units can be supplied to perform the extinction correction. The air mass of the observation can be found from the header if it is reported using the conventional keywords for air mass.

Telluric Absorption Removal
During the process of generating the sensitivity function, masks are used over the telluric regions to generate the telluric absorption profile from the standard star. The default masking regions are 6850 − 6960 and 7580−7700Å only. This telluric profile can then be multiplied by a factor to be determined in order to remove the telluric absorption features in the science target. The best multiplicative factor is found by minimizing the difference between the continuum and the telluricabsorption-corrected spectrum simultaneously for both the sampled telluric regions (there are only two ranges in the default setting, but if more regions are provided, this process will apply to all of them simultaneously). An extra multiplier can be manually provided to adjust the strength of the subtraction. This is designed for manually fine-tuning the absorption factor; otherwise, it defaults to 1.0.

Resampling
All the operations above are performed as a function of the native detector pixels. This includes the telluric absorption corrections as the telluric profile is generated in the standard spectrum pixel/wavelength coordinates. The correction applied to the target spectrum is performed using an interpolated function of the profile at the wavelengths of the target spectrum.
Many legacy software packages can only read the spectrum using the header information to create the wavelength coordinates using only three parameters: the wavelength of the first element of the spectral array, the bin width of each wavelength coordinate, and the length of the spectral array. This requires a uniformly sampled wavelength axis, which never happens naturally as there is always some level of image distortion at the detector plane. A resampling is necessary to turn the data into such a format.
The resampling is performed with the spectresc package. Although this package allows nonuniform wavelength spacing resampling, we are only using it for uniform resampling for the purpose of exporting the data as FITS files.

Output
At the time of writing, 22 types of output to CSV and FITS files are supported. Each CSV has the various exported data (see below) stored in a separate columns, while the FITS has the data stored in separate header data units (HDUs). Each spectrum is exported as a separate file. At the time of writing, the header is not exported when the output option is CSV. The options for output are as follows: 1. trace: two columns/HDUs containing the pixel coordinates of the trace and its width in the spatial direction.
2. count: three columns/HDUs containing the electron count of the spectrum, its uncertainty, and the sky background.
3. weight map: one column/HDU for the extraction profile; in top-hat and horne86 extraction this is a one-dimensional array, while the marsh90 extraction returns a two-dimensional array.
4. arc spec: three columns/HDUs for the onedimensional arc spectrum, pixel coordinates of the arc line position (subpixel precision), and the pixel coordinates of the arc line effective position (e.g. accounting for chip gaps) in the dispersion direction.
5. wavecal: one column/HDU for the polynomial coefficients for wavelength calibration. The header contains the information regarding the polynomial type.
6. wavelength: one column/HDU for the wavelength at the native pixel position.
7. sensitivity: one column/HDU containing the sensitivity function of the detector as a function of the native detector pixel.
8. flux: three columns/HDUs containing the flux of the spectrum, its uncertainty, and the sky background.
9. atm ext: one column/HDU containing the atmospheric extinction correction factor at each wavelength position.
10. flux atm ext corrected: three columns/HDUs containing the atmospheric-extinction-corrected flux of the spectrum, its uncertainty, and the sky background.
11. telluric profile: one column/HDU containing the telluric absorption profile from the standard spectrum.
14. wavelength resampled: one column/HDU containing the wavelength at each resampled position.
15. count resampled: three columns/HDUs containing the electron count of the spectrum, its uncertainty, and the sky background being subtracted during the extraction process at the resampled wavelength.
16. sensitivity resampled: one column/HDU containing the sensitivity function of the detector as a function of resampled wavelength.
17. flux resampled: three columns/HDUs containing the flux of the spectrum, its uncertainty, and the sky background at the resampled wavelength.
18. atm ext resampled: one column/HDU containing the atmospheric extinction correction factor at the resampled wavelength.
19. flux resampled atm ext corrected: three columns/HDUs containing the atmosphericextinction-corrected flux of the spectrum, its uncertainty, and the sky background at the resampled wavelength. 20. telluric profile resampled: one column/HDU containing the telluric absorption profile from the standard spectrum at the resampled wavelength.
21. flux resampled telluric corrected: three columns/HDUs containing the telluric-absorptioncorrected flux, uncertainty, and the sky background at the resampled wavelength.

flux resampled atm ext telluric corrected:
three columns/HDUs containing the atmosphericextinction-corrected and telluric-absorptioncorrected flux, uncertainty, and the sky background at the resampled wavelength.

VALIDATION
Being a flexible toolkit, ASPIRED is not designed to reduce data for a specific instrument. Instead, it can be customized to suit many configurations, as long as they are long-slit-like. We demonstrate this here with data products from eight different instruments, most of which have conventional long-slit settings. A summary of the following example reductions is tabulated in Table 1.
The purpose of the comparison made here is to demonstrate that the reductions are sufficiently consistent with representative spectra in the literature for a range of instruments and astrophysical sources. However, a full systematic comparison is beyond the scope of this work. Such a level of detailed validation might be required for any service providers who wish to adopt ASPIRED as their official reduction tool, and will be included in future work.

Example Data Reduction Results
In the case of Gemini/GMOS, a single spectrum is exposed onto three detectors, and each detector is read out with four amplifiers. The bias subtraction is also unconventional, so all the image data-reduction procedures have to be done outside of ASPIRED beforehand. We compare our reduction against the official one for the kilonova AT 2017gfo 31 (McCully et al. 2017).
Las Cumbres Observatory/FLOYDS has a beam splitter to expose the first-order red beam and the secondorder blue beam into two nonparallel curved spectra on a single chip. Both the northern and southern FLOYDS suffer from strong fringing effects. This is not handled natively through the API of ASPIRED. Instead, the correction was done by directly modifying the electron count of the OneDSpec object. The fringe subtraction workflow follows that of the official pipeline 32 . Here, the Type II supernova iPTF14hls is used as an example for comparison (Arcavi et al. 2017).
LT/SPRAT is a high-throughput low-resolution spectrograph with a conventional optical design; in this case, we compare our reduction to that published for a nova shell, DO Aql, from a stack of four individual exposures 33 (Harvey et al. 2020).
The VLT/FORS2 data of the helium accretor (AM CVn) V418 Ser, stacked from 21 individual exposures, is used as our last comparison (Green et al. 2020).
The first three spectra were compared against data reduction from iraf-based pipelines, while the last one is reduced with Molly 34 (Marsh 2019), which is built on top of STARLINK. All of our reductions are resampled to match the resolution in the equivalent published spectra. Our comparisons are shown in Figure 8.
Aside from the comparison against the independently and externally calibrated spectra above, we also illustrate a few cases of extractions concerning other use cases that ASPIRED is capable of dealing with. In the top set of spectra in Figure 9, the GTC/OSIRIS data show a blue large-amplitude pulsator with many resolved absorption lines in both the science and the standard targets (Feige 110 35 ; McWhirter & Lam 2022). With a poorly computed sensitivity curve, we expect many small bump features in the spectra and that their continuum would not agree in the overlapping wavelength range, as polynomials tend to run away at the edges of the fitted range of the wavelength solution. In the middle, the TNG/DOLORES case shows a simultaneous tracing of a common proper motion system (three bodies resolved as two components) containing an M dwarf and an eclipsing M dwarf-white dwarf binary (Keller et al. 2022), separated by 5" incident on the same long-slit in a single exposure. At the bottom is a set of three spectra taken with WHT/ISIS and ACAM. It demonstrates the reduction of a set of data with extremely low S/N for an ultracool white dwarf with a smooth blackbody-like spectrum (Lam et al. 2020). The tracing and extraction are only possible after careful cosmic-ray removal and trimming of the images. The spectral shapes of the ISIS spectra in both the blue and the red arms are in good agreement with the ACAM spectrum, and these independently flux-calibrated spectra agree well with each other in the overlapping wavelength ranges.
To summarize, our test cases cover the most commonly encountered possible problematic datared-or blue-dominated spectra, narrow/broad absorption/emission lines, featureless spectra, closely spaced (resolved) line spectra and curved spectra -and demonstrate that ASPIRED can produce spectral products at a quality comparable to some selected published spectra.

Repeatability
In order to demonstrate reproducibility and repeatability, we opt to calibrate the standard star BD33+2642 against Hiltner 102 on eight nights for which LT/SPRAT observed both on the same night during semester 2022 B. We require the transparency to be photometric, and the seeing be better than 2" as reported by the autoguider. Because of the robotic observing nature of the LT, standard stars are only taken at the beginning and at the end of the night. The data used were not taken immediately before or after each other, and therefore weather effects cannot be calibrated out. We only apply atmospheric extinction corrections and telluric absorption corrections. The spectra are consistent with each other 35 https://www.eso.org/sci/observing/tools/standards/spectra/ feige110.html to within 1%-2% (apart from the telluric absorption regions and at the Hγ line at 4340Å that are observed at low detector efficiency; see Fig. 10).

DISTRIBUTION
ASPIRED is released under the BSD (3-Clause) License. The source code is hosted on Github. The DOI of each version can be found on zenodo: https: //zenodo.org/record/4127294. For more straightforward installation, ASPIRED is also available with the Python Package Index (PyPI) so users can install the software with a single command:

>> pip install aspired
The latest stable version can be installed with It is also possible to clone the entire repository and install with the setup script using >> git clone [url to main/dev/a specific commit] >> python setup.py .
All the reduction scripts for the spectra used in this article can be found online 36 . There are also example reduction scripts available from the companion repository ASPIRED-example 37 which are tagged by version number starting from v0.4. This work is most accurately reflects the software release of the v0.5-series. We encourage users to reduce example spectra available in these repositories, using their own pipelines, for comparison.

SUMMARY AND FUTURE WORK
We expect to continue the development and maintenance of ASPIRED in the future. Some of the main planned developments include the following: VLT/FORS2 -V418 Ser ASPIRED reduction iraf-based reduction as per publication starlink-based reduction as per publication Figure 8. From top to bottom are the comparison spectra taken with VLT/FORS2, Las Cumbres/FLOYDS, LT/SPRAT and Gemini/GMOS in long-slit mode. The published FORS2 spectrum was reduced using Molly (green) powered by the STARLINK library. The other three (red) were reduced using the respective iraf-based pipelines. All spectra reduced with ASPIRED (blue) are optimally extracted using the Horne (1986) algorithm and resampled to match the respective published spectra. Telluric correction is applied using the default setting. . Example extractions of some special cases. Each set of spectra uses the same normalization. All spectra are optimally extracted using the Horne (1986) algorithm. The GTC/OSIRIS (top set in blue hue) spectra were flux calibrated in the shorter wavelengths where there are a lot of absorption features. With a bad sensitivity curve, we expect many small bump features in the spectra and that their continuum would not agree in the overlapping wavelength range, as polynomials tend to run away at the edges. The TNG/DOLORES (middle set in red hue) spectra were of two targets where the tracing was done simultaneously and then the extraction done sequentially. The WHT/ISIS and ACAM (bottom set in green hue) data demonstrate extracting a target with a very low S/N ratio. The independently flux-calibrated spectra agree well with each other in the overlapping wavelength ranges.
1. Improving the quality of the flux calibration by carefully masking out the absorption lines in the standard spectra, and treating the high-and lowresolution templates differently.
2. Accepting arcs taken at different times and using the effective wavelength calibration solution over a long observing block.
3. Accepting standards taken at different times and computing the effective sensitivity function that takes into account the nonnegligible change in the flux of the target over a long observing block.
4. Exporting reduced spectra as data type specutil.spectrum1D for smooth integration to the Astropy analytical tools.
5. Improving static image output support.  Figure 10. Fractional residual in the calibrated flux of BD 33+2642 using Hiltner 102 as a standard star taken with LT/SPRAT. Each gray line depicts the calibration residual between the median spectrum of the eight spectra taken on different nights. The blue area shows the one standard deviation of the scatter in the residual. The spikes above 5% residual are at the Hγ line at 4340Å and the oxygen telluric features at 6869 and 7605Å . The residuals that are larger on the two ends are expected as the S/N ratios drop with the detector efficiency on either side.
6. Supporting more forms of optimal extractions. We anticipate that ASPIRED will be useful for many individual researchers as well as observatories. It offers a complete set of methods in Python for spectral data reduction from raw images to final products. It sits between specreduce, which provides low-level reduction functions, and PypeIt, which offers preconfigured tailor-made reduction methods for specific instrument configuration. ASPIRED's simple structure and syntax, with a rich set of examples and documentation, is aimed to make the learning curve as gentle as possible. The relatively lightweight and cross-platform design allow a wide audience, including those with less computing equipment and power, to perform all necessary reduction tasks.