Spec Class

Class to store and manipulate spectra.

class spookyspec.Spec.Spec(l, f, **kwargs)

Store 1D spectral data

This class was designed to be easy to use, especially to create figures spectra are transformed by appending class methods to the variable. For example, to redshift a spectrum spec by 10 km/s, call spec.dopshift(10) self.show() returns the axes as astropy.Quantity objects.

Parameters
  • l (np.array) – wavelength points

  • f (np.array) – flux points

Keyword Arguments
  • u_l (str) – unit of wavelength axis. Should be readable by astropy.units.Unit(). Default ‘Angstrom’

  • u_f (str) – unit of flux axis. Should be readable by astropy.units.Unit(). Default ‘erg cm-2 s-1 Angstrom-1’

  • stype (str) – type of information stored. Either ‘data’ or ‘model’. This is important for finding the continuum

  • 'data' (and normalization. Default) –

  • hdr (astropy.io.fits.header.Header) – Fits header to be stored with the spectrum. Default None

air()

Vacuum to Air

Convert a spectrum from vacuum to air.

Parameters

None

Returns

Converted spectrum

Return type

(Spec)

combine(*others)

Combine

Combine multiple Spec objects to increase snr in a wavelength range that they share

Parameters

others (list of type Spec) – Spec objects to be combined with self

Returns

new combined spectrum

Return type

(Spec)

continuum(w1, w2, **kwargs)

Continuum

Get a polynomial fit of the continuum.

Parameters
  • w1 (float) – starting wavelength to fit in units of self.u_l

  • w2 (float) – ending wavelength to fit in units of self.u_l

Keyword Arguments

degree (int) – degree of polynomial to fit

Returns

flux of the continuum. Shape is same as self.l

Return type

(np.array)

dopshift(v)

Doppler Shift

Shift the spectrum by some radial velocity

Parameters

v (float) – velocity in km/s

Returns

Doppler shifted spectrum

Return type

(Spec)

ew(w1, w2, W1, W2, **kwargs)

Equivalent Width

Get the equivalent width of a region of spectrum.

Parameters
  • w1 (float) – starting wavelength of EW region in units of self.u_l

  • w2 (float) – ending wavelength of EW region in units of self.u_l

  • W1 (float) – starting wavelength of continuum region in units of self.u_l

  • W2 (float) – ending wavelength of continuum region in units of self.u_l

Keyword Arguments

plot (bool) – whether or not to plot the line and region. Default False

Returns

Equivalent width of the region

Return type

(astropy.Quantity)

fnu()

Fnu

Convert from Flambda to Fnu. This function should not allow a spectrum that is already in Fnu to be converted.

Parameters

None

Returns

new spectrum in Fnu

Return type

(Spec)

gauss(R=None, sigma=None)

Gauss Convolve to instrument resolutuion with gaussian. May have to convert to evenly spaced x axis for this to work not implemented yet

Parameters

None

Keyword Arguments
  • R (float) – Resolving power of convolution.

  • sigma (float) – resolution of convolution in units of self.u_l

Returns

None – Raises NotImplementedError

new_units(**kwargs)

Change unit keyword Change the units stored in self.u_l and/or self.u_f. Does not change the arrays that store data. This is for fixing mistakes and other data management actions. To cast your data to another unit, please use astropy.units.to().

Parameters

None

Keyword Arguments
  • u_l (str) – new unit of wavelength axis. Should be readable by astropy.units.Unit().

  • u_f (str) – new unit of flux axis. Should be readable by astropy.units.Unit().

normalize(w1, w2, **kwargs)

Normalize

Normalize a region of the spectrum. Great if comparing a model to uncalibrated echelle spectra.

Parameters
  • w1 (float) – starting wavelength to normalize in units of self.u_l

  • w2 (float) – ending wavelength to normalize in units of self.u_l

Keyword Arguments

degree (int) – degree of polynomial to fit

Returns

normalized spectrum

Return type

(Spec)

quick_plot(w1, w2)

Plot a simple view

Show the spectrum in a simple non-interactive window. This is good to quickly check your work.

Parameters
  • w1 (float) – starting wavelength in units of self.u_l

  • w2 (float) – ending wavelength in units of self.u_l

Returns

None

regions(*wavelengths)

Select Regions

Choose regions of the spectrum to keep, discarding the rest.

Parameters

wavelength (array-like of type astropy.Quantity) – start-stop points for each region. Must have even length.

Returns

Spectrum containing only selected regions

Return type

(Spec)

resample(lnew)

Resample

Resample the flux at new wavelengths using scipy.interp1d

Parameters

lnew (np.array) – new wavelength values to sample at in units of self.u_l

Returns

new resampled spectrum

Return type

(Spec)

scale(k)

Scale spectrum

Multiply flux by a scaler, preserving equivalent width.

Parameters

k (float) – scale factor

Returns

new spectrum

Return type

(Spec)

set_snr(l0, snr0, thrux=None, thruy=None, snr_max=inf)

Set SNR

Add noise to the spectrum to simulate an observation. The signal-to-noise ratio (SNR) is set at a reference wavelength and from there scales with the square root of the flux according to Poisson satistics. Optionally, a description of the instrument throughput can be given and applied to the SNR calculation. This is best when used in conjunction with an exposure time calculator.

Parameters
  • l0 (float) – wavelength of the reference SNR in units of self.u_l

  • snr0 (float) – reference SNR in SNR per pixel. For SNR per resolution element, smooth first (e.g. self.smooth(5) for COS)

Keyword Arguments
  • thrux (np.array) – wavelength values of throughput data in self.u_l. Default None

  • thruy (np.array) – thoughput at each wavelength point in thrux. Only the relative value is important.

  • snr_max (float) – the maximum SNR achievable with this instrument. e.g. for COS use 45

Returns

spectrum with noise added

Return type

(Spec)

show()

Show spectrum

Return wavelength and flux so spectrum can be plotted or otherwise used

Parameters

None

Returns

wavelengths (astropy.Quantity array-like): fluxes

Return type

(astropy.Quantity array-like)

smooth(shape)

Smooth

Smooth with a boxcar function

Parameters

shape (int) – size of boxcar in pixels

Returns

Smoothed spectrum

Return type

(Spec)

yoffset(dy)

y-offset

Add a constant offset to the spectra. For plotting only. This does not preserve equivalent width.

Parameters

dy (float) – constant to be added to the flux in units of self.u_f

Returns

new spectrum with offset applied

Return type

(Spec)

spookyspec.Spec.read(filename, **kwargs)

Read file

Read a spectrum from a fits file or a two-column file

Parameters

filename (str) – path to the file

Keyword Arguments
  • u_l (str) – unit of wavelength axis. Should be readable by astropy.units.Unit(). Default ‘Angstrom’

  • u_f (str) – unit of flux axis. Should be readable by astropy.units.Unit(). Default ‘erg cm-2 s-1 Angstrom-1’

  • stype (str) – type of information stored. Either ‘data’ or ‘model’. This is important for finding the continuum

  • 'data' (and normalization. Default) –

  • col (str) – column of psg file to read. Default ‘Total’

Returns

Spectrum contained in fits file

Return type

(Spec)

spookyspec.Spec.readfits(filename, u_l=Unit('Angstrom'), u_f=Unit('erg / (Angstrom cm2 s)'), stype='data')

Read fits

Read a spectrum from a fits file

Parameters

filename (str) – path to the file

Keyword Arguments
  • u_l (str) – unit of wavelength axis. Should be readable by astropy.units.Unit(). Default ‘Angstrom’

  • u_f (str) – unit of flux axis. Should be readable by astropy.units.Unit(). Default ‘erg cm-2 s-1 Angstrom-1’

  • stype (str) – type of information stored. Either ‘data’ or ‘model’. This is important for finding the continuum

  • 'data' (and normalization. Default) –

Returns

Spectrum contained in fits file

Return type

(Spec)