crrlpy.crrls module

crrlpy.crrls.alphanum_key(s)[source]

Turn a string into a list of string and number chunks.

Parameters:

s – String

Returns:

List with strings and integers.

Return type:

list

Example:

>>> alphanum_key('z23a')
['z', 23, 'a']
crrlpy.crrls.average(data, axis, n)[source]

Averages data along the given axis by combining n adjacent values.

Parameters:
  • data (numpy array) – Data to average along a given axis.

  • axis (int) – Axis along which to average.

  • n (int) – Factor by which to average.

Returns:

Data decimated by a factor n along the given axis.

Return type:

numpy array

crrlpy.crrls.blank_lines(freq, tau, reffreqs, v0, dv)[source]

Blanks the lines in a spectra.

Parameters:
  • freq (array, MHz) – Frequency axis of the spectra.

  • tau (array) – Optical depth axis of the spectra.

  • reffreqs (list) – List with the reference frequency of the lines. Should be the rest frequency.

  • v0 (float) – Velocity shift to apply to the lines defined by reffreq. (km/s)

  • dv (float) – Velocity range to blank around the lines. (km/s)

crrlpy.crrls.blank_lines2(freq, tau, reffreqs, dv)[source]

Blanks the lines in a spectra.

Parameters:
  • freq (array) – Frequency axis of the spectra. (MHz)

  • tau (array) – Optical depth axis of the spectra.

  • reffreqs (list) – List with the reference frequency of the lines. Should be the rest frequency.

  • dv (float) – Velocity range to blank around the lines. (km/s)

crrlpy.crrls.df2dv(f0, df)[source]

Convert a frequency delta to a velocity delta given a central frequency.

Parameters:
  • f0 (float) – Rest frequency.

  • df (float) – Frequency delta.

Returns:

The equivalent velocity delta for the given frequency delta.

Return type:

float in \(\mbox{m s}^{-1}\)

crrlpy.crrls.doppler_broad(t, m, vrms, fwhm=False)[source]

Doppler broadening.

\(\frac{\Delta v}{\mbox{m s}^{-1}}=(\frac{2k_{B}T}{m}+v_{\mathrm{rms}}^2)^{1/2}\)

Parameters:
  • t (float) – Gas temperature in K.

  • m (float) – Mass of the element producing the line in amu.

  • vrms (float) – Turbulent velocity in \(\mbox{m s}^{-1}\).

Returns:

The sigma or FWHM of a Gaussian line due to Doppler broadening in \(\mbox{m s}^{-1}\).

Return type:

float

crrlpy.crrls.doppler_temp(sigma, m, vrms, fwhm=False)[source]

The temperature required to produce a Gaussian line of width sigma.

Parameters:
  • sigma (float) – The sigma or FWHM of a Gaussian line due to Doppler broadening in \(\mbox{m s}^{-1}\).

  • m (float) – Mass of the element producing the line in amu.

  • vrms (float) – Turbulent velocity in \(\mbox{m s}^{-1}\).

Returns:

Gas temperature in K.

Return type:

float

crrlpy.crrls.doppler_temp_err(sigma, sigma_err, m, vrms, vrms_err, fwhm=False)[source]
crrlpy.crrls.dv2df(f0, dv)[source]

Convert a velocity delta to a frequency delta given a central frequency.

Parameters:
  • f0 (float) – Rest frequency.

  • dv (float) – Velocity delta in m/s.

Returns:

For the given velocity delta the equivalent frequency delta.

Return type:

float

crrlpy.crrls.dv_minus_doppler(dV, ddV, dD, ddD)[source]

Returns the Lorentzian contribution to the line width assuming that the line has a Voigt profile.

Parameters:
  • dV (float) – Total line width

  • ddV (float) – Uncertainty in the total line width.

  • dD (float) – Doppler contribution to the line width.

  • ddD – Uncertainty in the Doppler contribution to the line width.

Returns:

The Lorentz contribution to the total line width.

Return type:

float

crrlpy.crrls.dv_minus_doppler2(dV, ddV, dD, ddD)[source]

Returns the Lorentzian contribution to the line width assuming that the line has a Voigt profile.

Parameters:
  • dV (float) – Total line width

  • ddV (float) – Uncertainty in the total line width.

  • dD (float) – Doppler contribution to the line width.

  • ddD – Uncertainty in the Doppler contribution to the line width.

Returns:

The Lorentz contribution to the total line width.

Return type:

float

crrlpy.crrls.f2n(f, line, n_max=1500)[source]

Converts a given frequency to a principal quantum number \(n\) for a given line.

Parameters:
  • f (array) – Frequency to convert. (MHz)

  • line (string) – The equivalent \(n\) will be referenced to this line.

  • n_max (int) – Maximum n number to include in the search. (optional, Default 1)

Returns:

Corresponding \(n\) for a given frequency and line. If the frequency is not an exact match, then it will return an empty array.

Return type:

array

crrlpy.crrls.find_lines_sb(freq, line, z=0, verbose=False)[source]

Finds if there are any lines of a given type in the frequency range. The line frequencies are corrected for redshift.

Parameters:
  • freq (array) – Frequency axis in which to search for lines (MHz). It should not contain NaN or inf values.

  • line (string) – Line type to search for.

  • z (float) – Redshift to apply to the rest frequencies.

  • verbose (bool) – Verbose output?

Returns:

Lists with the princpipal quantum number and the reference frequency of the line. The frequencies are redshift corrected in MHz.

Return type:

array.

See also

load_ref

Describes the format of line and the available ones.

Examples

>>> from crrlpy import crrls
>>> freq = [10, 11]
>>> ns, rf = crrls.find_lines_sb(freq, 'RRL_CIalpha')
>>> ns
array([843., 844., 845., 846., 847., 848., 849., 850., 851., 852., 853.,
       854., 855., 856., 857., 858., 859., 860., 861., 862., 863., 864.,
       865., 866., 867., 868., 869.])
crrlpy.crrls.freq2vel(f0, f)[source]

Convert a frequency axis to a velocity axis given a central frequency. Uses the radio definition of velocity.

Parameters:
  • f0 (float) – Rest frequency for the conversion. (Hz)

  • f (numpy array) – Frequencies to be converted to velocity. (Hz)

Returns:

f converted to velocity given a rest frequency \(f_{0}\).

Return type:

numpy array

crrlpy.crrls.fwhm2sigma(fwhm)[source]

Converts a FWHM to the standard deviation, \(\sigma\) of a Gaussian distribution.

Parameters:

fwhm (array) – FWHM of the Gaussian.

Returns:

Equivalent standard deviation of a Gausian with a Full Width at Half Maximum fwhm.

Return type:

array

Example:

>>> 1/fwhm2sigma(1)
2.3548200450309493
crrlpy.crrls.gauss_area(amplitude, sigma)[source]

Returns the area under a Gaussian of a given amplitude and sigma.

Parameters:
  • amplitude – Amplitude of the Gaussian, \(A\).

  • sigma (array) – Standard deviation fo the Gaussian, \(\sigma\).

Returns:

The area under a Gaussian of a given amplitude and standard deviation.

Return type:

array

crrlpy.crrls.gauss_area2peak(area, sigma)[source]

Returns the maximum value of a Gaussian function given its amplitude and standard deviation “math:sigma.

crrlpy.crrls.gauss_area2peak_err(amplitude, area, darea, sigma, dsigma)[source]

Returns the maximum value of a Gaussian function given its amplitude, area and standard deviation “math:sigma.

crrlpy.crrls.gauss_area_err(amplitude, amplitude_err, sigma, sigma_err)[source]

Returns the error on the area of a Gaussian of a given amplitude and sigma with their corresponding errors. It assumes no correlation between amplitude and sigma.

Parameters:
  • amplitude (array) – Amplitude of the Gaussian.

  • amplitude_err (array) – Error on the amplitude.

  • sigma – Standard deviation of the Gaussian.

  • sigma_err – Error on sigma.

Returns:

The error on the area.

Return type:

array

crrlpy.crrls.gaussian(x, sigma, center, amplitude)[source]

Gaussian function in one dimension.

Parameters:
  • x (array) – x values for which to evaluate the Gaussian.

  • sigma (float) – Standard deviation of the Gaussian.

  • center (float) – Center of the Gaussian.

  • amplitude (float) – Peak value of the Gaussian.

Returns:

Gaussian function of the given amplitude and standard deviation evaluated at x.

Return type:

array

crrlpy.crrls.get_axis(header, axis)[source]

Constructs a cube axis.

Parameters:
  • header (pyfits header) – Fits cube header.

  • axis (int) – Axis to reconstruct.

Returns:

cube axis

Return type:

numpy array

crrlpy.crrls.get_line_mask(freq, reffreq, v0, dv)[source]

Return a mask with ranges where a line is expected in the given frequency range for a line with a given reference frequency at expected velocity v0 and line width dv0.

Parameters:
  • freq (numpy array or list) – Frequency axis where the line is located.

  • reffreq (float) – Reference frequency for the line.

  • v0 (float, km/s) – Velocity of the line.

  • dv (float, km/s) – Velocity range to mask.

Returns:

Mask centered at the line center and width dv0 referenced to the input freq.

crrlpy.crrls.get_line_mask2(freq, reffreq, dv)[source]

Return a mask with ranges where a line is expected in the given frequency range for a line with a given reference frequency and line width dv.

Parameters:
  • freq (numpy array or list) – Frequency axis where the line is located.

  • reffreq (float) – Reference frequency for the line.

  • dv (float, km/s) – Velocity range to mask.

Returns:

Mask centered at the line center and width dv0 referenced to the input freq.

crrlpy.crrls.get_rchi2(x_obs, x_mod, y_obs, y_mod, dy_obs, dof)[source]

Computes the reduced \(\chi\) squared, \(\chi_{\nu}^{2}=\chi^{2}/dof\).

Parameters:
  • x_obs (array) – Abscissa values of the observations.

  • x_mod (array) – Abscissa values of the model.

  • y_obs (array) – Ordinate values of the observations.

  • y_mod (array) – Ordinate values of the model.

  • dy_obs (array) – Error on the ordinate values of the observations.

  • dof (float) – Degrees of freedom.

crrlpy.crrls.get_rms(data, axis=None)[source]

Computes the rms of the given data.

Parameters:
  • data (numpy array or list) – Array with values where to compute the rms.

  • axis (int) – Axis over which to compute the rms. Default: None

Returns:

The rms of data.

\[\mbox{rms}=\sqrt{\langle\mbox{data}\rangle^{2}+V[\mbox{data}]}\]

where \(V\) is the variance of the data.

crrlpy.crrls.is_number(str)[source]

Checks wether a string is a number or not.

Parameters:

str (string) – String.

Returns:

True if str can be converted to a float.

Return type:

bool

Example:

>>> is_number('10')
True
crrlpy.crrls.lambda2vel(wav0, wav)[source]

Convert a wavelength axis to a velocity axis given a rest wavelength. Uses the optical definition of velocity.

Parameters:
  • wav0 (float) – Rest frequency for the conversion.)

  • wav (numpy array) – Frequencies to be converted to velocity.

Returns:

Velocity. (Default: m/s)

Return type:

numpy array

crrlpy.crrls.levelpopc(t)[source]

Fraction of carbon atoms in the 3/2 state relative to the 1/2 state.

Parameters:

t (float) – Kinetic temperature.

crrlpy.crrls.linear(x, a, b)[source]

Linear model.

Parameters:
  • x (array) – x values where to evalueate the line.

  • a (float) – Slope of the line.

  • b (float) – y value for x equals 0.

Returns:

A line defined by \(ax+b\).

Return type:

array

crrlpy.crrls.load_model(prop, specie, temp, dens, other=None)[source]

Loads a model for the CRRL emission.

crrlpy.crrls.load_ref(line)[source]

Loads the reference spectrum for the specified line.

Available lines:
RRL_CIalpha
RRL_CIbeta
RRL_CIdelta
RRL_CIgamma
RRL_CI13alpha
RRL_HeIalpha
RRL_HeIbeta
RRL_HIalpha
RRL_HIbeta
RRL_SIalpha
RRL_SIbeta

More lines can be added by including a list in the linelist directory.

Parameters:
linestring

Line for which the principal quantum number and reference frequencies are desired.

Returns:
narray

Principal quantum numbers.

reference_frequenciesarray

Reference frequencies of the lines inside the spectrum in MHz.

crrlpy.crrls.lookup_freq(n, line)[source]

Returns the frequency of a line given the transition number n.

Parameters:
  • n (int) – Principal quantum number to look up for.

  • line (string) – Line for which the frequency is desired.

Returns:

Frequency of line(n).

Return type:

float

crrlpy.crrls.lorentz_width(n, ne, Te, Tr, W, dn=1)[source]

Gives the Lorentzian line width due to a combination of radiation and collisional broadening. The width is the FWHM in Hz. It uses the models of Salgado et al. (2015).

Parameters:
  • n (array) – Principal quantum number for which to evaluate the Lorentz widths.

  • ne (float) – Electron density to use in the collisional broadening term.

  • Te (float) – Electron temperature to use in the collisional broadening term.

  • Tr (float) – Radiation field temperature.

  • W (float) – Cloud covering factor used in the radiation broadening term.

  • dn (int) – Separation between the levels of the transition. e.g., dn=1 for CIalpha.

Returns:

The Lorentz width of a line due to radiation and collisional broadening.

Return type:

array

crrlpy.crrls.mask_outliers(data, m=2)[source]

Masks values larger than m times the data median. This is similar to sigma clipping.

Parameters:

data (array) – Data to mask.

Returns:

An array of the same shape as data with True where the data should be flagged.

Return type:

array

Example:

>>> data = [1,2,3,4,5,6]
>>> mask_outliers(data, m=1)
array([ True, False, False, False, False,  True])
crrlpy.crrls.n2f(n, line, n_min=1, n_max=1500, unitless=True)[source]

Converts a given principal quantum number n to the frequency of a given line.

crrlpy.crrls.natural_sort(list)[source]

Sort the given list in the way that humans expect. Sorting is done in place.

Parameters:

list (list) – List to sort.

Example:

>>> my_list = ['spec_3', 'spec_4', 'spec_1']
>>> natural_sort(my_list)
>>> my_list
['spec_1', 'spec_3', 'spec_4']
crrlpy.crrls.ngaussian(x, sigma, center)[source]

Normalized Gaussian distribution.

crrlpy.crrls.pressure_broad(n, te, ne)[source]

Pressure induced broadening in Hz. Shaver (1975) Eq. (64a) for te <= 1000 K and Eq. (61) for te > 1000 K.

crrlpy.crrls.pressure_broad_coefs(Te)[source]

Defines the values of the constants \(a\) and \(\gamma\) that go into the collisional broadening formula of Salgado et al. (2017).

Parameters:

Te (float) – Electron temperature.

Returns:

The values of \(a\) and \(\gamma\).

Return type:

list

crrlpy.crrls.pressure_broad_salgado(n, te, ne, dn=1)[source]

Pressure induced broadening in Hz. This gives the FWHM of a Lorentzian line. Salgado et al. (2017)

Parameters:
  • n (float or array) – Principal quantum number for which to compute the line broadening.

  • Te (float) – Electron temperature to use when computing the collisional line width.

  • ne (float) – Electron density to use when computing the collisional line width.

  • dn (int) – Difference between the upper and lower level for which the line width is computed. (default 1)

Returns:

The collisional broadening FWHM in Hz using Salgado et al. (2015) formulas.

Return type:

float or array

crrlpy.crrls.radiation_broad(n, W, tr)[source]

Radiation induced broadening in Hz. Shaver (1975)

crrlpy.crrls.radiation_broad_salgado(n, w, tr)[source]

Radiation induced broadening in Hz. This gives the FWHM of a Lorentzian line. Salgado et al. (2017)

crrlpy.crrls.radiation_broad_salgado_general(n, w, tr, nu0, alpha)[source]

Radiation induced broadening in Hz. This gives the FWHM of a Lorentzian line. The expression is valid for power law like radiation fields. Salgado et al. (2017)

crrlpy.crrls.rval(te, ne, nh, rates='TH1985')[source]
crrlpy.crrls.sigma2fwhm(sigma)[source]

Converts the \(\sigma\) parameter of a Gaussian distribution to its FWHM.

Parameters:

sigma (float) – \(\sigma\) value of the Gaussian distribution.

Returns:

The FWHM of a Gaussian with a standard deviation \(\sigma\).

Return type:

float

crrlpy.crrls.sigma2fwhm_err(dsigma)[source]

Converts the error on the sigma parameter of a Gaussian distribution to the error on the FWHM.

Parameters:

dsigma – Error on sigma of the Gaussian distribution.

Returns:

The error on the FWHM of a Gaussian with a standard deviation \(\sigma\).

Return type:

float

crrlpy.crrls.sigma2fwtm(sigma)[source]

Converts the \(\sigma\) parameter of a Gaussian distribution to its FWTM.

\(\mbox{FWTM}=2(2\log(10))^{1/2}\sigma\)

Parameters:

sigma (float) – Standard deviation of the Gaussian distribution.

Returns:

Full width at a tenth of the maximum.

Return type:

float

crrlpy.crrls.signal2noise(snr0, fwhm, dx, prop='amplitude')[source]

Signal to noise ratio of the corresponding line property, Lenz & Ayres (1992).

Parameters:
  • snr0 (float) – Signal-to-noise ratio computed as peak/rms.

  • fwhm (float) – FWHM of the line.

  • dx (float) – Channel spacing.

  • prop (str) – Line property. Can be one of ‘amplitude’, ‘center’, ‘FWHM’ or ‘area’.

Returns:

Signal-to-noise ratio assuming a Gaussian line profile.

Return type:

float

crrlpy.crrls.tryint(str)[source]

Returns an integer if str can be represented as one.

Parameters:

str (string) – String to check.

Returns:

int(str) if str can be cast to an int, else str.

Return type:

int or str

crrlpy.crrls.vel2freq(f0, vel)[source]

Convert a velocity axis to a frequency axis given a central frequency. Uses the radio definition, \(\nu=f_{0}(1-v/c)\).

Parameters:
  • f0 (float) – Rest frequency in Hz.

  • vel (float or array) – Velocity to convert in m/s.

Returns:

The frequency which is equivalent to vel.

Return type:

float or array

crrlpy.crrls.voigt(x, sigma, gamma, center, amplitude)[source]

The Voigt line shape in terms of its physical parameters.

Parameters:
  • x – independent variable

  • sigma – HWHM of the Gaussian

  • gamma – HWHM of the Lorentzian

  • center – the line center

  • amplitude – the line area

crrlpy.crrls.voigt_(x, y)[source]
crrlpy.crrls.voigt_area(amp, fwhm, gamma, sigma)[source]

Returns the area under a Voigt profile. This approximation has an error of less than 0.5%

crrlpy.crrls.voigt_area2(peak, fwhm, gamma, sigma)[source]

Area under the Voigt profile using the expression provided by Sorochenko & Smirnov (1990).

Parameters:
peakfloat

Peak of the Voigt line profile.

fwhmfloat

Full width at half maximum of the Voigt profile.

gammafloat

Full width at half maximum of the Lorentzian profile.

sigmafloat

Full width at half maximum of the Doppler profile.

crrlpy.crrls.voigt_area_err(area, amp, damp, fwhm, dfwhm, gamma, sigma)[source]

Returns the error of the area under a Voigt profile. Assumes that the parameter c has an error of 0.5%.

crrlpy.crrls.voigt_fwhm(dD, dL)[source]

Computes the FWHM of a Voigt profile. http://en.wikipedia.org/wiki/Voigt_profile#The_width_of_the_Voigt_profile

\[FWHM_{\rm{V}}=0.5346dL+\sqrt{0.2166dL^{2}+dD^{2}}\]
Parameters:
  • dD (array) – FWHM of the Gaussian core.

  • dL (array) – FWHM of the Lorentz wings.

Returns:

The FWHM of a Voigt profile.

Return type:

array

crrlpy.crrls.voigt_fwhm_err(dD, dL, ddD, ddL)[source]

Computes the error in the FWHM of a Voigt profile. http://en.wikipedia.org/wiki/Voigt_profile#The_width_of_the_Voigt_profile

Parameters:
  • dD (array) – FWHM of the Gaussian core.

  • dL (array) – FWHM of the Lorentz wings.

  • ddD (array) – Error on the FWHM of the Gaussian.

  • ddL (array) – Error on the FWHM of the Lorentzian.

Returns:

The FWHM of a Voigt profile.

Return type:

array

crrlpy.crrls.voigt_peak(A, alphaD, alphaL)[source]

Gives the peak of a Voigt profile given its Area and the Half Width at Half Maximum of the Gaussian and Lorentz profiles.

Parameters:
  • A (array) – Area of the Voigt profile.

  • alphaD (array) – HWHM of the Gaussian core.

  • alphaL (array) – HWHM of the Lorentz wings.

Returns:

The peak of the Voigt profile.

Return type:

array

crrlpy.crrls.voigt_peak2area(peak, alphaD, alphaL)[source]

Converts the peak of a Voigt profile into the area under the profile given the Half Width at Half Maximum of the profile components.

Parameters:
  • peak (array) – Peak of the Voigt profile.

  • alphaD (array) – HWHM of the Gaussian core.

  • alphaL (array) – HWHM of the Lorentz wings.

Returns:

The area under the Voigt profile.

Return type:

array

crrlpy.crrls.voigt_peak_err(peak, A, dA, alphaD, dalphaD)[source]

Gives the error on the peak of the Voigt profile. It assumes no correlation between the parameters and that they are normally distributed.

Parameters:
  • peak (array) – Peak of the Voigt profile.

  • A – Area under the Voigt profile.

  • dA (array) – Error on the area A.

  • alphaD (array) – HWHM of the Gaussian core.