Handy functions for manipulation of Spectrum objects

## Routine Listing¶

Operators:

Note that these operators are purely algebraic and should not be used in place of the line-of-sight functions, i.e, SerialSlabs() (>) and MergeSlabs() (//)

Functions to manipulate one spectrum:

Play with baselines:

Functions to discard all but one spectral quantity:

Keeps all spectral quantities, but make emission equal to 0 (useful when calculating contributions of line of sight slabs):

Examples

Most of these functions are implemented with the standard operators. Ex:

((s_exp - 0.1)*10).plot()   # works for a Spectrum s_exp

PerfectAbsorber(s: Spectrum) [source]

Makes a new Spectrum with the same transmittance/absorbance as Spectrum s, but with radiance set to 0. Useful to get contribution of different slabs in line-of-sight calculations (see example).

Parameters:

s (Spectrum) – Spectrum object

Returns:

s_trSpectrum object, with only the transmittance, absorbance and/or abscoeff part of s, where radiance_noslit , emisscoeff and emissivity_noslit (if they exist) have been set to 0

Return type:

Spectrum

Examples

Let’s say you have a total line of sight:

s_los = s1 > s2 > s3

If you now want to get the contribution of s2 to the line-of-sight emission, you can do:

And the contribution of s1 would be:

See more examples in Line-of-Sight module

Returns a new Spectrum with only the radiance component of s

Parameters:

s (Spectrum) – Spectrum object

Returns:

s_trSpectrum object, with only radiance defined

Return type:

Spectrum

Examples

This function is useful to use Spectrum algebra operations:

s = calc_spectrum(...)   # contains emission & absorption arrays

Equivalent to:

Returns a new Spectrum with only the radiance_noslit component of s

Parameters:

s (Spectrum) – Spectrum object

Returns:

s_trSpectrum object, with only radiance_noslit defined

Return type:

Spectrum

Examples

This function is useful to use Spectrum algebra operations:

s = calc_spectrum(...)   # contains emission & absorption arrays

Equivalent to:

Transmittance(s: Spectrum) [source]

Returns a new Spectrum with only the transmittance component of s

Parameters:

s (Spectrum) – Spectrum object

Returns:

s_trSpectrum object, with only the transmittance, absorbance and/or abscoeff part of s, where radiance_noslit , emisscoeff and emissivity_noslit (if they exist) have been set to 0

Return type:

Spectrum

Examples

This function is useful to use Spectrum algebra operations:

s = calc_spectrum(...)   # contains emission & absorption arrays
tr = Transmittance(s)    # contains 'radiance_noslit' array only
tr -= 0.1    # arithmetic operation is applied to Transmittance only

Equivalent to:

Transmittance_noslit(s: Spectrum) [source]

Returns a new Spectrum with only the transmittance_noslit component of s

Parameters:

s (Spectrum) – Spectrum object

Returns:

s_trSpectrum object, with only transmittance_noslit defined

Return type:

Spectrum

Examples

This function is useful to use Spectrum algebra operations:

s = calc_spectrum(...)   # contains emission & absorption arrays
tr = Transmittance_noslit(s) # contains 'radiance_noslit' array only
tr -= 0.1    # arithmetic operation is applied to Transmittance_noslit only

Equivalent to:

Return a new spectrum with a constant added to s[var]. Equivalent to:

s + array
Parameters:
• s (Spectrum objects) – Spectrum you want to modify

• a (numpy array, or Quantity) – array to add. Must have the same length as variable var in Spectrum s. Can be dimensioned with units.

• unit (str) – unit for a. If None, uses the default unit in s for variable var.

• var (str, or None) – ‘radiance’, ‘transmittance’, … If None, get the unique spectral quantity of s or raises an error if there is any ambiguity

• inplace (bool) – if True, modifies s directly. Else, returns a copy. Default False

Returns:

s – Spectrum object where array a is added to intensity of s[‘var’] If inplace=True, s has been modified directly.

Return type:

Spectrum

Notes

Use only for rough work. If you want to work properly with spectrum objects, see MergeSlabs.

Examples

Add Gaussian noise to your Spectrum (assuming there is only one spectral quantity defined):

s += np.random.normal(0,1,len(s.get_wavelength()))

Return a new spectrum with a constant added to s[var]. Equivalent to:

s + constant
Parameters:
• s (Spectrum objects) – Spectrum you want to modify

• cst (float) – Constant to add.

• unit (str) – unit for cst. If None, uses the default unit in s for variable var.

• var (str, or None) – ‘radiance’, ‘transmittance’, … If None, get the unique spectral quantity of s or raises an error if there is any ambiguity

• inplace (bool) – if True, modifies s directly. Else, returns a copy. Default False

Returns:

s – Spectrum object where cst is added to intensity of s[‘var’] If inplace=True, s has been modified directly.

Return type:

Spectrum

Notes

Use only for rough work. If you want to work properly with spectrum objects, see MergeSlabs().

Return a new spectrum with s2 added to s1. Equivalent to:

s1 + s2

Warning

we are just algebraically adding the quantities. If you want to merge spectra while preserving the radiative transfer equation, see MergeSlabs() and SerialSlabs()

Parameters:
• s1, s2 (Spectrum objects) – Spectrum you want to substract

• var (str) – quantity to manipulate: ‘radiance’, ‘transmittance’, … If None, get the unique spectral quantity of s1, or the unique spectral quantity of s2, or raises an error if there is any ambiguity

Returns:

s – Spectrum object with the same units and waveunits as s1

Return type:

Spectrum

concat_spectra(s1, s2, var=None)[source]

Concatenate two spectra s1 and s2 side by side.

Note: their spectral range should not overlap

Returns:

s – Spectrum object with the same units and waveunits as s1

Return type:

Spectrum

Parameters:
• s1, s2 (Spectrum objects) – Spectrum you want to concatenate

• var (str) – quantity to manipulate: ‘radiance’, ‘transmittance’, … If None, get the unique spectral quantity of s1, or the unique spectral quantity of s2, or raises an error if there is any ambiguity

Notes

Warning

the output Spectrum has the sum of the spectral ranges of s1 and s2. It won’t be evenly spaced. This means that you cannot apply a slit without side effects. Typically, you want to use this function for convolved quantities only, such as experimental spectra. Else, use MergeSlabs() with the options resample='full', out='transparent'

crop(s: Spectrum, wmin: float | None = None, wmax: float | None = None, wunit: str | None = None, inplace: str = False) [source]

Crop spectrum to wmin-wmax range in wunit

Parameters:
• s (Spectrum object) – object to crop

• wmin, wmax (float, or None) – boundaries of spectral range (in wunit)

wunit: 'nm', 'cm-1', 'nm_vac'

which waveunit to use for wmin, wmax. If default: use the default Spectrum wavespace defined with get_waveunit().

Other Parameters:

inplace (bool) – if True, modify s directly. Else, returns a copy.

Returns:

s_crop – a cropped Spectrum. if using inplace, then s_crop and s are still the same object

Return type:

Spectrum

Examples

crop(s, 420, 480, 'nm', 'air')

Or in cm-1:

crop(s, 2000, 2300, 'cm-1')
get_baseline(s, var='radiance', algorithm='als', wunit='default', Iunit='default', **kwargs)[source]

Calculate and returns a baseline

Parameters:
• s (Spectrum) – Spectrum which needs a baseline

• var (str) – on which spectral quantity to read the baseline. Default 'radiance'. See SPECTRAL_QUANTITIES

• algorithm (‘als’, ‘polynomial’) – Asymmetric least square or Polynomial fit

• **kwargs (dict) – additional parameters to send to the algorithm. By default, for ‘polynomial’:

**kwargs = **{“deg”: 1, “max_it”:500}

for ‘als’:

**kwargs = {“asymmetry_param”: 0.05,

“smoothness_param”: 1e6}

Returns:

baseline – Spectrum object where intensity is the baseline of s is computed by peakutils

Return type:

Spectrum

Examples

Remove a baseline

Remove a baseline

multiply(s, coef, unit=None, var=None, inplace=False)[source]

Multiply s[var] by the float ‘coef’

Parameters:
• s (Spectrum object) – The spectrum to multiply.

• coef (float) – Coefficient of the multiplication.

• unit (str, or Unit) – unit for coef. If None, coef is considered to be adimensioned. Else, the spectrum units is multiplied.

• var (str, or None) – ‘radiance’, ‘transmittance’, … If None, get the unique spectral quantity of s or raises an error if there is any ambiguity

• inplace (bool) – if True, modifies s directly. Else, returns a copy. Default False

Returns:

s – Spectrum object where intensity of s[‘var’] is multiplied by coef If inplace=True, s has been modified directly.

Return type:

Spectrum

offset(s: Spectrum, offset: float, unit: str, name: str | None = None, inplace: bool = False) [source]

Offset the spectrum by a wavelength or wavenumber

Parameters:
• s (Spectrum) – Spectrum you want to modify

• offset (float) – Constant to add to all quantities in the Spectrum.

• unit (‘nm’ or ‘cm-1’) – unit for offset.

Other Parameters:
• name (str) – name of output spectrum

• inplace (bool) – if True, modifies s directly. Else, returns a copy. Default False

Returns:

s – Spectrum object where cst is added to intensity of s[‘var’] If inplace=True, s has been modified directly.

Return type:

Spectrum

call

sub_baseline(s, left, right, unit=None, var=None, inplace=False)[source]

Return a new spectrum with a baseline subtracted to s[var]

Parameters:
• s (Spectrum objects) – Spectrum you want to modify

• left (Float) – Constant to substract on the left of the spectrum.

• right (Float) – Constant to substract on the right of the spectrum.

• unit (str) – unit for cst. If None, uses the default unit in s for variable var.

• var (str) – ‘radiance’, ‘transmittance’, … If None, get the unique spectral quantity of s or raises an error if there is any ambiguity

• inplace (bool) – if True, modifies s directly. Else, returns a copy. Default False

Returns:

s – Spectrum object where the baseline was subtracted to intensity of s[‘var’] If inplace=True, s has been modified directly.

Return type:

Spectrum

Notes

Use only for rough work.

get_baseline()

substract_spectra(s1, s2, var=None)[source]

Return a new spectrum with s2 subtracted from s1. Equivalent to:

s1 - s2
Parameters:
• s1, s2 (Spectrum objects) – Spectrum you want to substract

• var (str) – quantity to manipulate: ‘radiance’, ‘transmittance’, … If None, get the unique spectral quantity of s1, or the unique spectral quantity of s2, or raises an error if there is any ambiguity

Returns:

s – Spectrum object with the same units and waveunits as s1

Return type:

Spectrum