Beispiel #1
0
def get_baseline(s, var="radiance", Iunit=None):
    """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 :py:data:`~radis.spectrum.utils.SPECTRAL_QUANTITIES`

    Returns    
    -------
    baseline: Spectrum
        Spectrum object where intenisity is the baseline of s is computed by peakutils
        
    See Also
    --------
    
    :py:func:`~radis.spectrum.operations.sub_baseline`
    """
    import peakutils

    w1, I1 = s.get(var=var, Iunit=Iunit)
    baseline = peakutils.baseline(I1, deg=1, max_it=500)
    baselineSpectrum = Spectrum.from_array(
        w1, baseline, var, unit=Iunit, name=s.get_name() + "_baseline"
    )
    return baselineSpectrum
Beispiel #2
0
def get_baseline(s, var='radiance', wunit='nm', medium='air', Iunit=None):
    '''Calculate and returns a baseline 

    Parameters    
    ----------
    s: Spectrum
        Spectrum which needs a baseline

    Returns    
    -------
    baseline: Spectrum
        Spectrum object where intenisity is the baseline of s is computed by peakutils
        
    See Also
    --------
    
    :py:func:`~radis.spectrum.operations.sub_baseline`
    '''
    import peakutils
    w1, I1 = s.get(var=var, Iunit=Iunit, wunit=wunit, medium=medium)
    baseline = peakutils.baseline(I1, deg=1, max_it=500)
    baselineSpectrum = Spectrum.from_array(w1,
                                           baseline,
                                           var,
                                           waveunit=wunit,
                                           unit=Iunit,
                                           conditions={'medium': medium},
                                           name=s.get_name() + '_baseline')
    return baselineSpectrum
Beispiel #3
0
def concat_spectra(s1, s2, var=None):
    """ Concatenate two spectra ``s1`` and ``s2`` side by side.
    
    Note: their spectral range should not overlap 
    
    Returns    
    -------
    
    s: Spectrum
        Spectrum object with the same units and waveunits as ``s1``
        
    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
        :func:`~radis.los.slabs.MergeSlabs` with the options 
        ``resample='full', out='transparent'``
        
    See Also
    --------
    
    :func:`~radis.spectrum.operations.add_spectra`, 
    :func:`~radis.los.slabs.MergeSlabs`
    
    """

    # Get variable
    if var is None:
        try:
            var = _get_unique_var(
                s2, var, inplace=False
            )  # unique variable of 2nd spectrum
        except KeyError:
            var = _get_unique_var(
                s1, var, inplace=False
            )  # if doesnt exist, unique variable of 1st spectrum
            # if it fails, let it fail
    # Make sure it is in both Spectra
    if var not in s1.get_vars():
        raise KeyError("Variable {0} not in Spectrum {1}".format(var, s1.get_name()))
    if var not in s2.get_vars():
        raise KeyError("Variable {0} not in Spectrum {1}".format(var, s1.get_name()))

    if var in ["transmittance_noslit", "transmittance"]:
        warn(
            "It does not make much physical sense to sum transmittances. Are "
            + "you sure of what you are doing? See also // (MergeSlabs) and > "
            + "(SerialSlabs)"
        )

    # Use same units
    Iunit1 = s1.units[var]
    wunit1 = s1.get_waveunit()

    # Get the value, on the same wunit)
    w1, I1 = s1.get(var=var, copy=False)  # @dev: faster to just get the stored value.
    # it's copied in hstack() below anyway).
    w2, I2 = s2.get(var=var, Iunit=Iunit1, wunit=wunit1)

    if not (w1.max() < w2.min() or w2.max() > w1.min()):
        raise ValueError(
            "You cannot use concat_spectra for overlapping spectral ranges. "
            + "Got: {0:.2f}-{1:.2f} and {2:.2f}-{3:.2f} {4}. ".format(
                w1.min(), w1.max(), w2.min(), w2.max(), wunit1
            )
            + "Use MergeSlabs instead, with the correct `out=` parameter "
            + "for your case"
        )

    w_tot = hstack((w1, w2))
    I_tot = hstack((I1, I2))

    name = s1.get_name() + "&" + s2.get_name()  # use "&" instead of "+"

    concat = Spectrum.from_array(
        w_tot, I_tot, var, waveunit=wunit1, unit=Iunit1, name=name
    )

    return concat
Beispiel #4
0
def substract_spectra(s1, s2, var=None):
    """Return a new spectrum with ``s2`` substracted 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
        Spectrum object with the same units and waveunits as ``s1``
        
    See Also
    --------
    
    :func:`~radis.spectrum.operations.add_spectra`
    
    """

    # Get variable
    if var is None:
        try:
            var = _get_unique_var(
                s2, var, inplace=False
            )  # unique variable of 2nd spectrum
        except KeyError:
            var = _get_unique_var(
                s1, var, inplace=False
            )  # if doesnt exist, unique variable of 1st spectrum
            # if it fails, let it fail
    # Make sure it is in both Spectra
    if var not in s1.get_vars():
        raise KeyError("Variable {0} not in Spectrum {1}".format(var, s1.get_name()))
    if var not in s2.get_vars():
        raise KeyError("Variable {0} not in Spectrum {1}".format(var, s1.get_name()))

    # Use same units
    Iunit1 = s1.units[var]
    wunit1 = s1.get_waveunit()

    # Resample s2 on s1
    s2 = s2.resample(s1, inplace=False)

    # Substract
    w1, I1 = s1.get(var=var, Iunit=Iunit1, wunit=wunit1)
    w2, I2 = s2.get(var=var, Iunit=Iunit1, wunit=wunit1)

    name = s1.get_name() + "-" + s2.get_name()

    sub = Spectrum.from_array(w1, I1 - I2, var, waveunit=wunit1, unit=Iunit1, name=name)
    #    warn("Conditions of the left spectrum were copied in the substraction.", Warning)
    return sub
Beispiel #5
0
def add_spectra(s1, s2, var=None, force=False):
    """Return a new spectrum with ``s2`` added to ``s1``. 
    Equivalent to::
        
        s1 + s2
    
    .. warning::
        we are just algebrically adding the quantities. If you want to merge
        spectra while preserving the radiative transfer equation, see 
        :func:`~radis.los.slabs.MergeSlabs` and :func:`~radis.los.slabs.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
        Spectrum object with the same units and waveunits as ``s1``
        
    See Also
    --------
    
    :func:`~radis.los.slabs.MergeSlabs`,
    :func:`~radis.spectrum.operations.substract_spectra`
    
    """

    # Get variable
    if var is None:
        try:
            var = _get_unique_var(
                s2, var, inplace=False
            )  # unique variable of 2nd spectrum
        except KeyError:
            var = _get_unique_var(
                s1, var, inplace=False
            )  # if doesnt exist, unique variable of 1st spectrum
            # if it fails, let it fail
    # Make sure it is in both Spectra
    if var not in s1.get_vars():
        raise KeyError("Variable {0} not in Spectrum {1}".format(var, s1.get_name()))
    if var not in s2.get_vars():
        raise KeyError("Variable {0} not in Spectrum {1}".format(var, s1.get_name()))

    if var in ["transmittance_noslit", "transmittance"] and not force:
        raise ValueError(
            "It does not make much physical sense to sum transmittances. Are "
            + "you sure of what you are doing? See also `//` (MergeSlabs), `>` "
            + "(SerialSlabs) and `concat_spectra`. If you're sure, use `force=True`"
        )

    # Get s1 units
    Iunit1 = s1.units[var]
    wunit1 = s1.get_waveunit()

    # Resample s2 on s1
    s2 = s2.resample(s1, inplace=False)

    # Add, change output unit if needed.
    w1, I1 = s1.get(var=var, Iunit=Iunit1, wunit=wunit1)
    w2, I2 = s2.get(var=var, Iunit=Iunit1, wunit=wunit1)

    name = s1.get_name() + "+" + s2.get_name()

    sub = Spectrum.from_array(w1, I1 + I2, var, waveunit=wunit1, unit=Iunit1, name=name)
    #    warn("Conditions of the left spectrum were copied in the substraction.", Warning)
    return sub
Beispiel #6
0
def substract_spectra(s1, s2, var=None, wunit='nm'):
    '''Return a new spectrum with ``s2`` substracted 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
    wunit: str
        'nm'or 'cm-1'
        
    Returns    
    -------
    
    s: Spectrum
        Spectrum object with the same units and waveunits as ``s1``
        
    See Also
    --------
    
    :func:`~radis.spectrum.operations.add_spectra`
    
    '''

    # Get variable
    if var is None:
        try:
            var = _get_unique_var(
                s2, var, inplace=False)  # unique variable of 2nd spectrum
        except KeyError:
            var = _get_unique_var(
                s1, var, inplace=False
            )  # if doesnt exist, unique variable of 1st spectrum
            # if it fails, let it fail
    # Make sure it is in both Spectra
    if var not in s1.get_vars():
        raise KeyError('Variable {0} not in Spectrum {1}'.format(
            var, s1.get_name()))
    if var not in s2.get_vars():
        raise KeyError('Variable {0} not in Spectrum {1}'.format(
            var, s1.get_name()))

    # Use same units
    Iunit = s1.units[var]
    wunit = s1.get_waveunit()
    medium = s1.get_medium()

    # Resample
    if wunit == 'nm':
        s2 = s2.resample(s1.get_wavelength(), 'nm', inplace=False)
    elif wunit == 'cm-1':
        s2 = s2.resample(s1.get_wavenumber(), 'cm-1', inplace=False)
    else:
        raise ValueError('Unexpected wunit: {0}'.format(wunit))

    # Substract
    w1, I1 = s1.get(var=var, Iunit=Iunit, wunit=wunit, medium=medium)
    w2, I2 = s2.get(var=var, Iunit=Iunit, wunit=wunit, medium=medium)

    name = s1.get_name() + '-' + s2.get_name()

    sub = Spectrum.from_array(w1,
                              I1 - I2,
                              var,
                              waveunit=wunit,
                              unit=Iunit,
                              conditions={'medium': medium},
                              name=name)
    #    warn("Conditions of the left spectrum were copied in the substraction.", Warning)
    return sub
Beispiel #7
0
def add_spectra(s1, s2, var=None, wunit='nm', force=False):
    '''Return a new spectrum with ``s2`` added to ``s1``. 
    Equivalent to::
        
        s1 + s2
    
    .. warning::
        we are just algebrically adding the quantities. If you want to merge
        spectra while preserving the radiative transfer equation, see 
        :func:`~radis.los.slabs.MergeSlabs` and :func:`~radis.los.slabs.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
    wunit: str
        'nm'or 'cm-1'
        
    Returns    
    -------
    
    s: Spectrum
        Spectrum object with the same units and waveunits as ``s1``
        
    See Also
    --------
    
    :func:`~radis.los.slabs.MergeSlabs`,
    :func:`~radis.spectrum.operations.substract_spectra`
    
    '''

    # Get variable
    if var is None:
        try:
            var = _get_unique_var(
                s2, var, inplace=False)  # unique variable of 2nd spectrum
        except KeyError:
            var = _get_unique_var(
                s1, var, inplace=False
            )  # if doesnt exist, unique variable of 1st spectrum
            # if it fails, let it fail
    # Make sure it is in both Spectra
    if var not in s1.get_vars():
        raise KeyError('Variable {0} not in Spectrum {1}'.format(
            var, s1.get_name()))
    if var not in s2.get_vars():
        raise KeyError('Variable {0} not in Spectrum {1}'.format(
            var, s1.get_name()))

    if var in ['transmittance_noslit', 'transmittance'] and not force:
        raise ValueError('It does not make much physical sense to sum transmittances. Are '+\
                         'you sure of what you are doing? See also `//` (MergeSlabs), `>` '+\
                         "(SerialSlabs) and `concat_spectra`. If you're sure, use `force=True`")

    # Use same units
    Iunit = s1.units[var]
    wunit = s1.get_waveunit()
    medium = s1.get_medium()

    # Resample
    if wunit == 'nm':
        s2 = s2.resample(s1.get_wavelength(), 'nm', inplace=False)
    elif wunit == 'cm-1':
        s2 = s2.resample(s1.get_wavenumber(), 'cm-1', inplace=False)
    else:
        raise ValueError('Unexpected wunit: {0}'.format(wunit))

    # Add
    w1, I1 = s1.get(var=var, Iunit=Iunit, wunit=wunit, medium=medium)
    w2, I2 = s2.get(var=var, Iunit=Iunit, wunit=wunit, medium=medium)

    name = s1.get_name() + '+' + s2.get_name()

    sub = Spectrum.from_array(w1,
                              I1 + I2,
                              var,
                              waveunit=wunit,
                              unit=Iunit,
                              conditions={'medium': medium},
                              name=name)
    #    warn("Conditions of the left spectrum were copied in the substraction.", Warning)
    return sub