Skip to content

stack_spectra

Combine every spectrum in ctx.spectra into one stacked spectrum.

Category Stacking
Backend numpy - implemented here on top of numpy primitives
Version 2.0.0
Reads ctx.spectra (a list of Spectrum1D)
Writes spectrum, metrics.n_stacked, metrics.n_nan_samples

Median stacking is the most robust against cosmic rays and outliers; mean maximises SNR for clean data; sum is useful for co-adding sub-exposures. Each spectrum contributes only inside its own wavelength range: samples of the reference grid outside a spectrum's coverage are NaN for that spectrum and ignored by the NaN-aware combiners (a sample covered by no spectrum at all is NaN in the output; count in metrics.n_nan_samples). v2.0.0: v1 let numpy.interp clamp to the edge value, so a spectrum was extrapolated as a constant over the whole grid — with s1 = 1.0 on 4000-5000 Å and s2 = 3.0 on 4500-5500 Å the v1 mean at 4000 Å was 2.0; it is now 1.0 (2.0 inside the overlap).

Parameters

Parameter Default Required Description
method 'median' - Combination method: mean, median or sum.
reference_index 0 - Index in ctx.spectra whose wavelength grid is the target.

Use it

from spectro_kernel import run_algorithm

output = run_algorithm("stack_spectra", ctx, {
    "method": "median",
    "reference_index": 0
})

The CLI loads a single 1-D spectrum with --input; this algorithm needs ctx.spectra (a list of Spectrum1D). Run it from Python or as a step of a pipeline preset.

{
  "tool": "stack_spectra",
  "arguments": {
    "session_id": "<session_id>",
    "params": {
      "method": "median",
      "reference_index": 0
    }
  }
}

Every algorithm is an MCP tool of the same name; describe_algorithm returns this page's metadata as JSON.

References

  • Tody 1993, ASP Conf. Ser. 52, 173 — IRAF scombine: spectra interpolated to a common dispersion, then summed / averaged / medianed sample by sample.