docs: def collapsed_spectrum(fitsfile, ns1, ns2,
                         method='mean', nwin_background=0,
                         reverse=False, out_sp=None, debugplot=0):
      """Compute a collapsed spectrum from a 2D image using scans in [ns1,ns2].
  
      Parameters
      ----------
      fitsfile : file object
          File name of FITS file containing the spectra to be calibrated.
      ns1 : int
          First scan (from 1 to NAXIS2).
      ns2 : int
          Last scan (from 1 to NAXIS2).
      method : string
          Indicates collapsing method. Possible values are "mean" or
          "median".
      nwin_background : int
          Window size for the computation of background using a median
          filtering with that window width. This background is computed
          and subtracted only if this parameter is > 0.
      reverse : bool
          If True, reserve wavelength direction prior to wavelength
          calibration.
      out_sp : string or None
          File name to save the selected spectrum in FITS format before
          performing the wavelength calibration.
      debugplot : int
          Determines whether intermediate computations and/or plots
          are displayed. The valid codes are defined in
          numina.array.display.pause_debugplot.
  
      Returns
      -------
      sp : 1d numpy array
          Collapsed spectrum.
  
      """
  
      # read FITS file
      with fits.open(fitsfile) as hdulist:
          image2d = hdulist[0].data
      naxis2, naxis1 = image2d.shape
      if abs(debugplot) >= 10:
          print('>>> Reading file:', fitsfile.name)
          print('>>> NAXIS1:', naxis1)
          print('>>> NAXIS2:', naxis2)
  
      if 1 <= ns1 <= ns2 <= naxis2:
          # extract spectrum
          if method == "mean":
              sp = np.mean(image2d[(ns1 - 1):ns2], axis=0)
          elif method == "median":
              sp = np.median(image2d[(ns1 - 1):ns2], axis=0)
          else:
              raise ValueError("Invalid method '" + str(method) + "'")
  
          # reverse spectrum if necessary
          if reverse:
              sp = sp[::-1]
  
          # fit and subtract background
          if nwin_background > 0:
              background = ndimage.filters.median_filter(
                  sp, size=nwin_background
              )
              sp -= background
  
          # save spectrum before wavelength calibration in external
          # FITS file
          if out_sp is not None:
              hdu = fits.PrimaryHDU(sp)
              hdu.writeto(out_sp, overwrite=True)
      else:
          raise ValueError("Invalid ns1=" + str(ns1) + ", ns2=" + str(ns2) +
                           " values")
  
      return sp