kinextract.load_spectrum_for_fit

kinextract.load_spectrum_for_fit(cfg: FitConfig, gal_file: str | None = None, gal_errors=None)[source]

Load a galaxy spectrum file and return arrays trimmed to the fit window.

Supports two input formats and, for .norm files, both continuum modes:

  • Legacy pre-normalized .norm files (~kinextract.io.read_norm_spectrum): if cfg.fit_continuum is False (pre-normalized mode), the already continuum-divided normflux/normflux_err columns are used directly. If cfg.fit_continuum is True (continuum-cofit mode), the original (non-divided) orig_flux/orig_flux_err columns are used instead so kinextract.joint’s continuum can be co-fit downstream; this requires the .norm file to include those columns (columns 4+). Optionally applies fortran_rebin_after_redshift to match the legacy Fortran imfit uniform-grid convention, and (in pre-normalized mode) an additional Fortran-style flux-range mask.

  • Raw .spec files (~kinextract.io.read_galaxy_index_flux_err): always effectively continuum-cofit-compatible (the flux column is not pre-divided). Supports both integer-index columns (requiring cfg.wavemin_full/cfg.step) and literal wavelength columns in the first field, with the wavelength converted to rest-frame by dividing by 1 + cfg.zgal.

In all cases, gal_errors, if given, overrides the file’s per-pixel flux errors (either a scalar broadcast to every pixel, or a full per-pixel array), and cfg.use_spectrum_errors=False (only consulted when gal_errors is not given) replaces per-pixel errors with their uniform median.

Parameters:
  • cfg (FitConfig) – Fit configuration; supplies fit_continuum, zgal, wavefitmin/wavefitmax, norm_wave_frame, norm_error_mode, use_spectrum_errors, fortran_rebin_norm, step, wavemin_full, spec_col3_is_variance, norm_apply_fortran_flux_mask, and related range-mask settings.

  • gal_file (str, optional) – Path to the spectrum file. Required; a .norm extension (case-insensitive) selects the pre-normalized-file path, anything else is treated as a raw .spec file.

  • gal_errors (float or ndarray, optional) – Optional override for the flux uncertainty: a scalar to use a uniform error for every pixel, or an array to use per-pixel errors directly, bypassing whatever error column the file provides.

Returns:

  • x_reg (ndarray) – Rest-frame wavelength array (Å) trimmed to [cfg.wavefitmin, cfg.wavefitmax].

  • g_reg (ndarray) – Flux array corresponding to x_reg (continuum-divided in pre-normalized mode, raw/undivided in continuum-cofit mode).

  • ge_reg (ndarray) – Flux uncertainty array corresponding to x_reg; pixels flagged bad by range or flux masks are set to BIG.

  • step_used (float) – Wavelength step (Å) used or estimated for this spectrum.

  • prenorm (bool) – True if the pre-normalized (continuum-already-divided) path was used; False if this is continuum-cofit mode or a raw .spec file.

  • extra (dict) – Auxiliary arrays useful to downstream code (e.g. the full unwindowed wavelength array for LOSVD-grid reference, and, for .norm files, copies of the various flux/error/continuum columns both full-length and windowed to the fit region).