Spectrum

class specmatchemp.spectrum.Spectrum(w, s, serr=None, mask=None, name=None, header=None, attrs={})

Spectrum class.

This object is a container for a spectrum and its properties.

Parameters:
  • w (np.ndarray) – Wavelength scale
  • s (np.ndarray) – Spectrum
  • serr (np.ndarray, optional) – Error in spectrum
  • mask (np.ndarray, optional) – Boolean array to mask out telluric lines
  • name (str, optional) – Name associated with spectrum
  • header (FITS header, optional) – Header from fits file
  • attrs (dict, optional) – Any further attributes
w

np.ndarray – Wavelength scale

s

np.ndarray – Spectrum

serr

np.ndarray – Measurement error in spectrum

mask

np.ndarray – Boolean array with telluric line positions

name

str – Name associted with spectrum

header

FITS header – Header from FITS file

attrs

dict – A dictionary of further attributes

static combine_spectra(spectra, w_ref, name=None, prefixes=None)

Combines several spectra into one spectrum object.

Combine multiple spectrum objects into a single spectrum. Places np.nan for the gaps between the spectra, and averages overlapping portions.

Parameters:
  • spectra (iterable) – Iterable of spectrum objects
  • w_ref (np.ndarray) – Reference wavelength scale. This scale is used to fill in the gaps between the spectra.
  • name (str, optional) – Name for the flattend spectrum
  • prefixes (str, optional) – Prefixes for attributes
copy()

Returns a deep copy of the Spectrum object

cut(minw, maxw)

Truncate the spectrum between the given limits

Parameters:
  • minw (float) – Minimum wavelength
  • maxw (float) – Maximum wavelength
Returns:

Truncated spectrum object

Return type:

Spectrum

extend(w)

Extend the spectrum onto a new wavelength scale, placing np.nan for points which do not exist on the old scale.

Parameters:w (np.ndarray) – New wavlength array.
on_scale(w)

Checks if spectrum is on the given wavelength scale

Parameters:w (np.ndarray) – Wavelength scale to check against
plot(wavlim='all', offset=0, label='_nolegend_', showmask=False, normalize=True, plt_kw={'color': 'RoyalBlue'}, text='', text_kw={})

Plots the spectrum.

Parameters:
  • offset (float, optional) – Vertical offset of the spectrum
  • label (str, optional) – Label of spectrum (appears in plt.legend)
  • showmask (bool, optional) – Whether to highlight the telluric mask in the plot. Defaults to False.
  • plt_kw (dict, optional) – Keyword arguments to pass to plt.plot
  • text (str, optional) – String to label the spectrum
  • text_kw (dict, optional) – Keyword arguments to pass to plt.text
rescale(w)

Puts the spectrum onto a new wavelength scale, interpolating between points if necessary. Places np.nan for points beyond existing range.

Parameters:w (np.ndarray) – New wavelength scale.
to_fits(outpath, clobber=True)

Saves the spectrum to a fits file.

Parameters:outpath (str) – Path to output file
to_hdf(outfile, suffix='')

Saves the spectrum to a hdf file.

Parameters:
  • outfile (str or h5 file) – Output path or file handle
  • suffix (str, optional) – Suffix to append to h5 field names
to_hdu()

Creates a fits.BinTableHDU object from spectrum data

Returns:A binary table HDU object
Return type:fits.BinTableHDU
wavlim()

Gets the wavelength range of the spectrum

class specmatchemp.spectrum.HiresSpectrum(w, s, serr=None, mask=None, mask_table=None, name=None, header=None, attrs={})

Spectrum class for raw HIRES spectra.

Is read by the read_hires_fits function.

w

np.ndarray – Wavelength scale

s

np.ndarray – Spectrum

serr

None or np.ndarray – Measurement error in spectrum

mask

np.ndarray – Boolean array with telluric line positions

name

str – Name associted with spectrum

header

FITS header – Header from FITS file

attrs

dict – A dictionary of further attributes

Parameters:
  • w (np.ndarray) – Wavelength scale
  • s (np.ndarray) – Spectrum
  • serr (np.ndarray, optional) – Error in spectrum
  • mask (np.ndarray, optional) – Boolean array to mask out telluric lines
  • mask_table (pd.DataFrame, optional) – Table containing masked regions
  • name (str, optional) – Name associated with spectrum
  • header (FITS header, optional) – Header from fits file
  • attrs (dict, optional) – Any further attributes
static combine_spectra(spectra, name=None, prefixes=None)

Combines several raw HIRES spectra into one HIRES spectrum object.

Parameters:
  • spectra (iterable) – Iterable of spectrum objects
  • name (str, optional) – Name for the flattend spectrum
  • prefixes (str, optional) – Prefixes for attributes
plot(wavlim='all', offset=0, label='_nolegend_', normalize=False, showmask=False, plt_kw={'color': 'RoyalBlue'}, text='', text_kw={})

Plots the spectrum

Parameters:
  • wavlim (optional [tuple]) – Wavelength limit to plot
  • offset (optional [float]) – Vertical offset of the spectrum
  • label (optional [str]) – Label of spectrum (appears in plt.legend)
  • normalize (optional [bool]) – Whether to normalize the spectrum. Defaults to False
  • showmask (bool, optional) – Whether to highlight the telluric mask in the plot. Defaults to False.
  • plt_kw (optional [dict]) – Keyword arguments to pass to plt.plot
  • text (str, optional) – String to label the spectrum
  • text_kw (dict, optional) – Keyword arguments to pass to plt.text
to_hdulist(primary=True)

Creates a list of fits.ImageHDU to store HIRES spectrum.

Parameters:primary (bool) – True if first HDU should be a primary HDU
Returns:l[0] = spectrum l[1] = spectrum error l[2] = wavelength
Return type:list of fits.ImageHDU
specmatchemp.spectrum.read_fits(infile, wavlim=None)

Reads a spectrum from a fits file

Parameters:
  • infile (str) – Path to input fits file
  • wavlim (tuple, optional) – Wavelength limits to read
Returns:

Spectrum object

Return type:

Spectrum

specmatchemp.spectrum.read_hires_fits(infile, maskfile=None)

Reads a spectrum from a fits file that was produced by HIRES.

Parameters:
  • infile (str) – Path to input fits file
  • maskfile (optional [str]) – Path to file containing telluric lines mask.
Returns:

Spectrum object

Return type:

spec (HiresSpectrum)

specmatchemp.spectrum.read_hdf(infile, suffix='')

Reads a spectrum from a hdf file

Parameters:
  • infile (str or h5 file) – Input path or file handle
  • suffix (str, optional) – Suffix on h5 keys
Returns:

Spectrum object

Return type:

spec (Spectrum)