Spectrum — Spectral line data container.¶
This module defines the Spectrum class.

class
admit.util.Spectrum.
Spectrum
(spec=None, freq=None, chans=None, contin=None, noise=None, mask=[False])[source]¶ Class for holding a spectrum. It holds entries for the spectrum, frequency axis, channel axis, vlsr, and rest_frequency. A mask is also available to mask bad data points. The mask is common to all axes.
A spectrum needs to hold at least 2 channels.
Parameters: spec : array like
The spectrum.
freq : array like
The frequency axis, one entry per spectral point.
chans : array like
The channel axis, one entry per spectral point.
contin : array like or float
The continuum for each channel, or for all channels if it is an int.
noise : float
The rms noise of the spectrum.
mask : array like or bool
The mask for the spectrum, one entry per spectral point or a single boolean value, Defaults to all good.
Methods
calcdelta
()centerfreq
()chans
([masked])Method to get the channel axis, the mask is optionally returned contin
([masked])Method to get the continuum, the mask is optionally returned delta
()fix_invalid
([mask_value])Method to replace invalid spectral data with a specific value and mask it. freq
([masked])Method to get the frequency axis, the mask is optionally returned freqdispersion
([chanrange])Compute the frequency dispersion of the spectrum in the given channel range. fwhm
([chanrange])Compute the fullwidth at halfmaximum velocity (km/s). getchan
(frq)Method to get the channel number given the frequency getchanindex
(chan)getfreq
(chan)Method to get the frequency of the given channel, the channel can contain a fraction of a channel (e.g. integritycheck
()Method to check that all axes are the same length. invert
()Multiply this spectrum and continuum by 1 mask
()Method to return the mask as a numpy array mask_between
(limit1, limit2[, axis])Method to mask any data bewteen the the given values. mask_equal
(value[, axis])Method to mask data equal to the given value. mask_ge
(limit[, axis])Method to mask any data less than or equal to the given value. mask_gt
(limit[, axis])Method to mask any data greater than the given value. mask_invalid
()Method to mask all invalid spectral data. mask_le
(limit[, axis])Method to mask any data less than or erqual to the given value. mask_lt
(limit[, axis])Method to mask any data less than the given value. mask_outside
(limit1, limit2[, axis])Method to mask any data less than or greater than the given values. meanfrequency
([chanrange])Compute the powerweighted mean frequency of this spectrum, aka the first moment. meanvelocity
([chanrange])Compute the powerweighted mean velocity of this spectrum. moment
([chanrange, p])Compute the powerweighted moment of a spectrum If a mask exists, this function operates on the masked spectrum. momenta
([chanrange, p])abs(intensity)weighted moment momenti
([chanrange, p])Intensityweighted moment noise
()Method to return the noise of the spectrum. peak
([chanrange])Return the peak intensity in the given channel range If a mask exists, this function operates on the masked spectrum. rms
()If the noise has been set, return the noise, otherwise compute and return the rootmeansquare value of the spectrum. set_chans
(chans)Method to set the channel axis. set_contin
(contin)Method to set the continuum. set_freq
(freq)Method to set the frequency axis. set_mask
(mask)Method to set the mask item set_noise
(noise)Method to set the noise value for the spectrum. set_spec
(spec[, mask])Method to set the spectrum, an optionally the mask. spec
([csub, masked])Method to get the spectrum, the mask is optionally returned veldispersion
([chanrange])Compute the velocity dispersion (km/s) of the spectrum in the given channel range. 
chans
(masked=True)[source]¶ Method to get the channel axis, the mask is optionally returned
Parameters: masked : bool
If True then return the channel axis as a masked array with the current mask applied. False will return a numpy array with no masking done.
Returns: Array like object containing the channel axis

contin
(masked=True)[source]¶ Method to get the continuum, the mask is optionally returned
Parameters: masked : bool
If True then return the continuum as a masked array with the current mask applied. False will return a numpy array with no masking done.
Returns: Array like object containing the continuum

fix_invalid
(mask_value=0.0)[source]¶ Method to replace invalid spectral data with a specific value and mask it. Invalid values are: NaN, Inf, Inf
Parameters: mask_value : float
The value to replace the invalid data with. Default: 0.0
Returns: None

freq
(masked=True)[source]¶ Method to get the frequency axis, the mask is optionally returned
Parameters: masked : bool
If True then return the frequency axis as a masked array with the current mask applied. False will return a numpy array with no masking done.
Returns: Array like object containing the frequency axis

freqdispersion
(chanrange=None)[source]¶ Compute the frequency dispersion of the spectrum in the given channel range. If a mask exists, this function operates on the masked spectrum.
Parameters: chanrange: range of channels over which to compute dispersion
[startchan, endchan]
Returns: Powerweighted frequency dispersion df as computed by
df = sqrt(mom2)
where mom2 is the 2nd moment as computed by the moment() method.

fwhm
(chanrange=None)[source]¶ Compute the fullwidth at halfmaximum velocity (km/s). If a mask exists, this function operates on the masked spectrum.
Parameters: chanrange: range of channels over which to compute dispersion
[startchan, endchan]
Returns: fwhm computed by
fwhm = dv * sqrt(8*ln(2))
where dv is the velocity dispersion computered by
the veldispersion() method.

getchan
(frq)[source]¶ Method to get the channel number given the frequency
Parameters: frq : float
The frequency whose channel number is to be found
Returns: int containing the channel number, will be 0 or len(self.x)1 if
the frequency is outside of the frequency range in the class

getfreq
(chan)[source]¶ Method to get the frequency of the given channel, the channel can contain a fraction of a channel (e.g. 1.45)
Parameters: chan : float
The channel to convert
Returns: Float of the frequency corresponding to the channel

integritycheck
()[source]¶ Method to check that all axes are the same length. Axes that have no data are ignored.
Parameters: None Returns: Bool, True if all match, False if there is an inconsistency

mask
()[source]¶ Method to return the mask as a numpy array
Parameters: None Returns: Array like object containing the mask

mask_between
(limit1, limit2, axis='spec')[source]¶ Method to mask any data bewteen the the given values. Any axis can be used (“spec”, “freq”, “chans”) as the basis for the masking.
Parameters: limit1 : float or int
The value above which all data of the given axis will be masked.
limit2 : float or int
The value below which all data of the given axis will be masked.
axis : str
The axis which is used to determine the flags. Default: “spec”
Returns: None

mask_equal
(value, axis='spec')[source]¶ Method to mask data equal to the given value. Any axis can be used (“spec”, “freq”, “chans”) as the basis for the masking.
Parameters: value : float or int
The value equal to which all data of the given axis will be masked.
axis : str
The axis which is used to determine the flags. Default: “spec”
Returns: None

mask_ge
(limit, axis='spec')[source]¶ Method to mask any data less than or equal to the given value. Any axis can be used (“spec”, “freq”, “chans”) as the basis for the masking.
Parameters: limit : float or int
The value which all data, of the given axis, greater than or equal to, will be masked.
axis : str
The axis which is used to determine the flags. Default: “spec”
Returns: None

mask_gt
(limit, axis='spec')[source]¶ Method to mask any data greater than the given value. Any axis can be used (“spec”, “freq”, “chans”) as the basis for the masking.
Parameters: limit : float or int
The value above which all data of the given axis will be masked.
axis : str
The axis which is used to determine the flags. Default: “spec”
Returns: None

mask_invalid
()[source]¶ Method to mask all invalid spectral data. Invalid values are: NaN, Inf, Inf
Parameters: None Returns: None

mask_le
(limit, axis='spec')[source]¶ Method to mask any data less than or erqual to the given value. Any axis can be used (“spec”, “freq”, “chans”) as the basis for the masking.
Parameters: limit : float or int
The value which all data, of the given axis, less than or equal to, will be masked.
axis : str
The axis which is used to determine the flags. Default: “spec”
Returns: None

mask_lt
(limit, axis='spec')[source]¶ Method to mask any data less than the given value. Any axis can be used (“spec”, “freq”, “chans”) as the basis for the masking.
Parameters: limit : float or int
The value below which all data of the given axis will be masked.
axis : str
The axis which is used to determine the flags. Default: “spec”
Returns: None

mask_outside
(limit1, limit2, axis='spec')[source]¶ Method to mask any data less than or greater than the given values. Any axis can be used (“spec”, “freq”, “chans”) as the basis for the masking.
Parameters: limit1 : float or int
The value below which all data of the given axis will be masked.
limit2 : float or int
The value above which all data of the given axis will be masked.
axis : str
The axis which is used to determine the flags. Default: “spec”
Returns: None

meanfrequency
(chanrange=None)[source]¶ Compute the powerweighted mean frequency of this spectrum, aka the first moment. If a channel range is given, the mean frequency is computed over that channel range, otherwise over the entire spectrum. If a mask exists, this function operates on the masked spectrum.
Parameters: chanrange: range of channels over which to compute frequency
[startchan, endchan]
Returns: Powerweighted mean frequency as computed by
f_mean = Sum(spec[i]**2*f[i]])/Sum(spec[i]**2)
where spec[i] is the intensity at channel i and f[i] is
the frequency at channel i.

meanvelocity
(chanrange=None)[source]¶ Compute the powerweighted mean velocity of this spectrum. derived from the mean frequency and channel width:
mean_v = C * delta()/meanfrequency(chanrange)

moment
(chanrange=None, p=1)[source]¶ Compute the powerweighted moment of a spectrum If a mask exists, this function operates on the masked spectrum.
f_mean = Sum(spec[i]**2*(f[i]mom{p1})^p])/Sum(spec[i]**2)where spec[i] is the intensity at channel i (spec[i]**2 is he power) and f[i] is the frequency at channel i, p is the moment power, and mom{p1} is the p1th moment [for p >1].
Parameters: chanrange: range of channels over which to compute moment
[startchan, endchan]
p: the moment to compute (the power of the frequency in the sum)
Returns:
The computed moment

momenta
(chanrange=None, p=1)[source]¶ abs(intensity)weighted moment Does somewhat better than signed intensityweighted moment.
Parameters: chanrange: range of channels over which to compute moment
[startchan, endchan]
p: the moment to compute (the power of the frequency in the sum)
Returns:
The computed moment

momenti
(chanrange=None, p=1)[source]¶ Intensityweighted moment Note this moment does poorly for very narrow line segments where some channels may be negative.”
Parameters: chanrange: range of channels over which to compute moment
[startchan, endchan]
p: the moment to compute (the power of the frequency in the sum)
Returns:
The computed moment

noise
()[source]¶ Method to return the noise of the spectrum.
Parameters: None Returns: Float of the rms noise.

peak
(chanrange=None)[source]¶ Return the peak intensity in the given channel range If a mask exists, this function operates on the masked spectrum.
Parameters: chanrange: range of channels over which to compute dispersion
[startchan, endchan]
Returns: Maximum of the absolute value of the spectrum in the channel range
max(abs(spectrum[startchan:endchan]))

rms
()[source]¶ If the noise has been set, return the noise, otherwise compute and return the rootmeansquare value of the spectrum.

set_chans
(chans)[source]¶ Method to set the channel axis.
Parameters: chans : array like
The channel axis to use
Returns: None

set_contin
(contin)[source]¶ Method to set the continuum.
Parameters: freq : array like
The continuum to use
Returns: None

set_freq
(freq)[source]¶ Method to set the frequency axis.
Parameters: freq : array like
The frequency axis to use
Returns: None

set_mask
(mask)[source]¶ Method to set the mask item
Parameters: mask : array like or bool
The mask to use, must be equal in dimmension to the spectral axis, or a single boolean value which is applied to all data.
Returns: None

set_noise
(noise)[source]¶ Method to set the noise value for the spectrum.
Parameters: noise : float
The noise of the spectrum
Returns: None

set_spec
(spec, mask=None)[source]¶ Method to set the spectrum, an optionally the mask.
Parameters: spec : array like
The spectrum to set
mask : array like or single bool
The mask to apply to the spectrum. Default: None (no mask)
Returns: None

spec
(csub=True, masked=True)[source]¶ Method to get the spectrum, the mask is optionally returned
Parameters: csub : bool
If True return the spectrum with the continuum subtracted, False will return the pure spectrum Default: True
masked : bool
If True then return the spectrum as a masked array with the current mask applied. False will return a numpy array with no masking done. Default: True
Returns: Array like object containing the spectrum

veldispersion
(chanrange=None)[source]¶ Compute the velocity dispersion (km/s) of the spectrum in the given channel range. If a mask exists, this function operates on the masked spectrum.
Parameters: chanrange: range of channels over which to compute dispersion
[startchan, endchan]
Returns: Powerweighted velocity dv as computed by
dv = C * df/mf
where df is the frequency dispersion computered by
the frequencydispersion() method,
mf is the mean frequency from the meanfrequency() method,
and C is the speed of light.
