jwspecfit.io

Spectrum I/O: FITS and NPZ readers, Spectrum container.

Functions

export_lines_txt(result, path[, z])

Export per-line measurements to a text file.

load_result(path)

Load a FitResult from a .npz file saved by save_result().

read_dict(data[, z, grating, R])

Create a Spectrum from a dict with keys wave/lam, flux, err.

read_fits(path[, z, hdu, wave_col, ...])

Read a 1-D spectrum from a FITS file.

read_npz(path[, z, R])

Read a stacked spectrum from a NumPy .npz file.

save_result(result, path)

Save a FitResult to a .npz file for later replotting.

Classes

Spectrum(wave_um, flux_ujy, err_ujy[, ...])

Container for a 1-D spectrum.

class jwspecfit.io.Spectrum(wave_um, flux_ujy, err_ujy, grating=None, z=None, R=None, meta=<factory>, sci_2d=None)[source]

Bases: object

Container for a 1-D spectrum.

Parameters:
wave_um

Observed wavelength in microns.

Type:

np.ndarray

flux_ujy

Flux density in micro-Jansky.

Type:

np.ndarray

err_ujy

1-sigma uncertainty in micro-Jansky.

Type:

np.ndarray

grating

Grating name (e.g. "PRISM", "G395M"). None for stacked spectra.

Type:

str or None

z

Source redshift (set by user, not from header).

Type:

float or None

R

Spectral resolving power. Overrides grating when set (useful for stacks).

Type:

float or callable or None

meta

Arbitrary metadata from the FITS header or user.

Type:

dict

sci_2d

Optional 2-D rectified spectrum image with shape (n_spatial, n_pix) where n_pix == len(wave_um). Populated by read_fits() when an SCI ImageHDU is present and its wavelength axis matches the 1-D extraction; otherwise None. Read by plot_spectrum_interactive() to render a 2-D + 1-D stacked view; ignored by every other code path.

Type:

np.ndarray or None

R: float | None = None
copy()[source]

Return a shallow copy with copied arrays.

Return type:

Spectrum

property dlam_A: ndarray

Pixel widths in Angstroms.

property err_flam: ndarray

Error in erg/s/cm²/Å.

err_ujy: ndarray
property flux_flam: ndarray

Flux density in erg/s/cm²/Å.

flux_ujy: ndarray
grating: str | None = None
mask_valid()[source]

Boolean mask: True where flux and error are finite and err > 0.

Return type:

ndarray

meta: dict[str, Any]
property n_pix: int
sci_2d: ndarray | None = None
property wave_A: ndarray

Wavelength in Angstroms.

property wave_edges_A: ndarray

Pixel-edge wavelengths in Angstroms (length n_pix + 1).

wave_um: ndarray
z: float | None = None
jwspecfit.io.export_lines_txt(result, path, z=None)[source]

Export per-line measurements to a text file.

Columns: name, rest_wave_A, centroid_A, flux, flux_err, ew_A, sigma_v_kms, snr_integrated, snr_peak.

Parameters:
  • result (FitResult) – Fit result.

  • path (str or Path) – Output text file path.

  • z (float, optional) – Redshift (for velocity calculation). If None, uses result.spectrum.z.

Return type:

None

jwspecfit.io.load_result(path)[source]

Load a FitResult from a .npz file saved by save_result().

Parameters:

path (str or Path) – Path to the .npz file.

Return type:

FitResult

jwspecfit.io.read_dict(data, z=None, grating=None, R=None)[source]

Create a Spectrum from a dict with keys wave/lam, flux, err.

Wavelength assumed in microns.

Parameters:
  • data (dict) – Must contain "wave" or "lam" (µm), "flux" (µJy), "err" (µJy).

  • z (float, optional) – Source redshift.

  • grating (str, optional) – Grating name.

  • R (float, optional) – Resolving power.

Return type:

Spectrum

jwspecfit.io.read_fits(path, z=None, *, hdu=None, wave_col=None, flux_col=None, err_col=None)[source]

Read a 1-D spectrum from a FITS file.

By default tries the JWST/NIRSpec SPEC1D BinTable convention with columns wave (µm), flux (µJy), err (µJy). Falls through to auto-detection across all extensions when SPEC1D is absent or when hdu is given:

  1. BinTable HDUs — the first table containing wavelength- and flux-like columns wins. Recognised column-name aliases include wave/wavelength/lam/lambda/loglam and flux/fnu/flam and err/error/sigma/noise (or ivar). Units are read from TUNITn keywords; common aliases (µm/Å/nm, µJy/mJy/Jy, erg/s/cm²/Å) are converted automatically.

  2. Image HDUs — a 1-D image is read with the WCS keywords CRVAL1, CDELT1/CD1_1, CRPIX1, CTYPE1, CUNIT1; flux unit is BUNIT. Errors are not available from an image and are set to zero.

Parameters:
  • path (str or Path) – Path to the FITS file.

  • z (float, optional) – Source redshift to attach to the returned Spectrum.

  • hdu (str or int, optional) – Force a specific HDU to read (name or index). When None (default), tries SPEC1D first, then auto-detects.

  • wave_col (str, optional) – Force specific column names instead of auto-detecting. Only used for BinTable HDUs.

  • flux_col (str, optional) – Force specific column names instead of auto-detecting. Only used for BinTable HDUs.

  • err_col (str, optional) – Force specific column names instead of auto-detecting. Only used for BinTable HDUs.

Return type:

Spectrum

jwspecfit.io.read_npz(path, z=None, R=None)[source]

Read a stacked spectrum from a NumPy .npz file.

Expected keys: wave_angstrom, flux, err. Optionally n_stacked.

Parameters:
  • path (str or Path) – Path to the .npz file.

  • z (float, optional) – Source redshift.

  • R (float, optional) – Effective spectral resolving power of the stack.

Return type:

Spectrum

jwspecfit.io.save_result(result, path)[source]

Save a FitResult to a .npz file for later replotting.

Parameters:
  • result (FitResult) – Fit result to save.

  • path (str or Path) – Output file path (should end in .npz).

Return type:

None