jwspecfit.io
Spectrum I/O: FITS and NPZ readers, Spectrum container.
Functions
|
Export per-line measurements to a text file. |
|
Load a FitResult from a .npz file saved by |
|
Create a Spectrum from a dict with keys |
|
Read a 1-D spectrum from a FITS file. |
|
Read a stacked spectrum from a NumPy .npz file. |
|
Save a FitResult to a .npz file for later replotting. |
Classes
|
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:
objectContainer 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
- R
Spectral resolving power. Overrides
gratingwhen set (useful for stacks).- Type:
float or callable or None
- sci_2d
Optional 2-D rectified spectrum image with shape
(n_spatial, n_pix)wheren_pix == len(wave_um). Populated byread_fits()when anSCIImageHDU is present and its wavelength axis matches the 1-D extraction; otherwiseNone. Read byplot_spectrum_interactive()to render a 2-D + 1-D stacked view; ignored by every other code path.- Type:
np.ndarray or 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.
- jwspecfit.io.load_result(path)[source]
Load a FitResult from a .npz file saved by
save_result().
- 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.
- 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
SPEC1DBinTable convention with columnswave(µm),flux(µJy),err(µJy). Falls through to auto-detection across all extensions when SPEC1D is absent or when hdu is given:BinTable HDUs — the first table containing wavelength- and flux-like columns wins. Recognised column-name aliases include
wave/wavelength/lam/lambda/loglamandflux/fnu/flamanderr/error/sigma/noise(orivar). Units are read fromTUNITnkeywords; common aliases (µm/Å/nm, µJy/mJy/Jy, erg/s/cm²/Å) are converted automatically.Image HDUs — a 1-D image is read with the WCS keywords
CRVAL1,CDELT1/CD1_1,CRPIX1,CTYPE1,CUNIT1; flux unit isBUNIT. 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: