def add_constant(s, cst, unit=None, var=None, inplace=False): """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 Spectrum object where cst is added to intensity of s['var'] If ``inplace=True``, ``s`` has been modified directly. Notes ----- Use only for rough work. If you want to work properly with spectrum objects, see :py:meth:`~radis.los.slabs.MergeSlabs`. """ # Check input var = _get_unique_var(s, var, inplace) # Note: using dimensioned value for `cst` will make it an array, # processed by add_array # Convert to Spectrum unit if unit is not None: Iunit = s.units[var] if unit != Iunit: from radis.phys.convert import conv2 cst = conv2(cst, unit, Iunit) if not inplace: s = s.copy(quantity=var) # Add inplace ( @dev: we have copied already if needed ) w, I = s.get(var, wunit=s.get_waveunit(), copy=False) I += cst # @dev: updates the Spectrum directly because of copy=False # s.name = '{0}+{1}'.format(s.get_name(), cst) return s
def sub_baseline(s, left, right, unit=None, var=None, inplace=False): """Return a new spectrum with a baseline substracted 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 Spectrum object where the baseline was substracted to intensity of s['var'] If ``inplace=True``, ``s`` has been modified directly. Notes ----- Use only for rough work. See Also -------- :py:func:`~radis.spectrum.operations.get_baseline` """ import numpy as np # Check input var = _get_unique_var(s, var, inplace) # Case where left, right are dimensioned if isinstance(left, u.quantity.Quantity) or isinstance( right, u.quantity.Quantity): if unit is not None: raise ValueError( "Cannot use unit= when giving a dimensioned array ({0})". format(left.unit)) if not isinstance(left, u.quantity.Quantity) or not isinstance( right, u.quantity.Quantity): raise ValueError( "Both left and right arguments must be dimensioned with the same unit" ) if left.unit != right.unit: raise ValueError( "Both left and right arguments must be dimensioned with the same unit" ) unit = left.unit left = left.value right = right.value # Convert to Spectrum unit if unit is not None: Iunit = s.units[var] if unit != Iunit: from radis.phys.convert import conv2 left = conv2(left, unit, Iunit) right = conv2(right, unit, Iunit) if not inplace: s = s.copy(quantity=var) # @EP: # Substract inplace ( @dev: we have copied already if needed ) w, I = s.get(var, wunit=s.get_waveunit(), copy=False) I -= np.linspace(left, right, num=np.size(I)) # @dev: updates the Spectrum directly because of copy=False return s
def add_array(s, a, unit=None, var=None, inplace=False): """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 `~astropy.units.quantity.Quantity` array to add. Must have the same length as variable ``var`` in Spectrum ``s``. Can be dimensioned with :py:mod:`~astropy.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 Spectrum object where array ``a`` is added to intensity of s['var'] If ``inplace=True``, ``s`` has been modified directly. 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())) """ # Check input var = _get_unique_var(s, var, inplace) # Case where a is dimensioned if isinstance(a, u.quantity.Quantity): if unit is not None: raise ValueError( "Cannot use unit= when giving a dimensioned array ({0})". format(a.unit)) unit = a.unit a = a.value # Convert to Spectrum unit if unit is not None: Iunit = s.units[var] if unit != Iunit: from radis.phys.convert import conv2 a = conv2(a, unit, Iunit) if not inplace: s = s.copy(quantity=var) # Add inplace ( @dev: we have copied already if needed ) w, I = s.get(var, wunit=s.get_waveunit(), copy=False) I += a # @dev: updates the Spectrum directly because of copy=False return s
def sub_baseline(s, left, right, unit=None, var=None, wunit='nm', inplace=False): '''Return a new spectrum with a baseline substracted 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 wunit: str 'nm'or 'cm-1' inplace: bool if ``True``, modifies ``s`` directly. Else, returns a copy. Default ``False`` Returns ------- s: Spectrum Spectrum object where the baseline was substracted to intensity of s['var'] If ``inplace=True``, ``s`` has been modified directly. Notes ----- Use only for rough work. See Also -------- :py:func:`~radis.spectrum.operations.get_baseline` ''' import numpy as np # Check input var = _get_unique_var(s, var, inplace) # Convert to Spectrum unit if unit is not None: Iunit = s.units[var] if unit != Iunit: from radis.phys.convert import conv2 left = conv2(left, unit, Iunit) right = conv2(right, unit, Iunit) if not inplace: s = s.copy(quantity=var) # @EP: # Substract inplace ( @dev: we have copied already if needed ) w, I = s.get(var, wunit=s.get_waveunit(), copy=False) I -= np.linspace(left, right, num=np.size(I)) # @dev: updates the Spectrum directly because of copy=False return s