Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'Unknown normalization type: `norm_by` = {0}'.format(norm_by))
# Plot in correct unit (plot_slit deals with the conversion if needed)
fig, ax = plot_slit(wslit0, Islit0, waveunit=waveunit, plot_unit=wunit,
Iunit=Iunit)
# Plot other slit functions if dispersion was applied:
if 'slit_dispersion' in self.conditions:
slit_dispersion = self.conditions['slit_dispersion']
if slit_dispersion is not None:
waveunit = self.get_waveunit()
if waveunit == 'nm':
wslit0_nm = wslit0
else:
wslit0_nm = cm2nm(wslit0)
w_nm = self.get_wavelength(medium='default', which='non_convoluted')
wings = len(wslit0) # note: hardcoded. Make sure it's the same as in apply_slit
wings *= max(1, abs(int(np.diff(wslit0_nm).mean() / np.diff(w_nm).mean())))
slice_windows, wings_min, wings_max = _cut_slices(w_nm, slit_dispersion, wings=wings)
# Loop over all waverange slices (needed if slit changes over the spectral range)
for islice, slice_window in enumerate(slice_windows):
w_nm_sliced = w_nm[slice_window]
w_min = w_nm_sliced.min()
w_max = w_nm_sliced.max()
# apply spectrometer linear dispersion function.
# dont forget it has to be added in nm and not cm-1
wslit, Islit = offset_dilate_slit_function(wslit0_nm, Islit0,
w_nm[slice_window],
'A (top, base) tuple must be used with a trapezoidal slit')
# ... first get FWHM in our wavespace unit
if return_unit == 'cm-1' and unit == 'nm':
# center_wavespace ~ nm, FWHM ~ nm
top = dnm2dcm(top, center_wavespace) # wavelength > wavenumber
base = dnm2dcm(base, center_wavespace) # wavelength > wavenumber
center_wavespace = nm2cm(center_wavespace)
if norm_by == 'max':
scale_slit = sum(slit_function) / \
(top+base) # [unit/return_unit]
elif return_unit == 'nm' and unit == 'cm-1':
# center_wavespace ~ cm-1, FWHM ~ cm-1
top = dcm2dnm(top, center_wavespace) # wavenumber > wavelength
base = dcm2dnm(base, center_wavespace) # wavenumber > wavelength
center_wavespace = cm2nm(center_wavespace)
if norm_by == 'max':
scale_slit = sum(slit_function) / \
(top+base) # [unit/return_unit]
else:
pass # correct unit already
FWHM = (top+base)/2
# ... now, build it (in our wavespace)
if __debug__:
printdbg('get_slit_function: {0}, FWHM {1:.2f}{2}, center {3:.2f}{2}, norm_by {4}'.format(
shape, FWHM, return_unit, center_wavespace, norm_by))
wslit, Islit = trapezoidal_slit(top, base, wstep,
center=center_wavespace, bplot=plot,
norm_by=norm_by,
# TODO @dev: rewrite with wunit='cm-1', 'nm_air', 'nm_vac'
waveunit = s.get_waveunit()
wmin0, wmax0 = wmin, wmax
if wunit == 'nm' and waveunit == 'cm-1':
if medium == 'air':
if wmax0: wmin = nm_air2cm(wmax0) # reverted
if wmin0: wmax = nm_air2cm(wmin0) # reverted
else:
if wmax0: wmin = nm2cm(wmax0) # reverted
if wmin0: wmax = nm2cm(wmin0) # reverted
elif wunit == 'cm-1' and waveunit == 'nm':
if s.get_medium() == 'air':
if wmax0: wmin = cm2nm_air(wmax0) # nm in air
if wmin0: wmax = cm2nm_air(wmin0) # nm in air
else:
if wmax0: wmin = cm2nm(wmax0) # get nm in vacuum
if wmin0: wmax = cm2nm(wmin0) # get nm in vacuum
elif wunit == 'nm' and waveunit == 'nm':
if s.get_medium() == 'air' and medium == 'vacuum':
# convert from given medium ('vacuum') to spectrum medium ('air')
if wmin0: wmin = vacuum2air(wmin0)
if wmax0: wmax = vacuum2air(wmax0)
elif s.get_medium() == 'vacuum' and medium == 'air':
# the other way around
if wmin0: wmin = air2vacuum(wmin0)
if wmax0: wmax = air2vacuum(wmax0)
else:
assert wunit == waveunit # correct wmin, wmax
# Crop non convoluted
if len(s._q)>0:
b = ones_like(s._q['wavespace'], dtype=bool)
# Final checks
# ... error in Pandas? Sometimes _metadata is preserved over several runs.
# ... Clean it here.
if __debug__ and len(df._metadata)>0:
printdbg('df._metadata was not []. Cleaning')
df._metadata = []
# ... check database is not empty
if len(df) == 0:
msg = (("Reference databank " +
"has 0 lines in range {0:.2f}-{1:.2f}cm-1".format(
wavenum_min, wavenum_max)) +
' ({0:.2f}-{1:.2f}nm) Check your range !'.format(
cm2nm(wavenum_min), cm2nm(wavenum_max)))
raise ValueError(msg)
maxwavdb = df.wav.max()
minwavdb = df.wav.min()
# ... Explicitely write molecule if not given
if self.input.molecule in [None, '']:
id_set = df.id.unique()
if len(id_set) > 1:
raise NotImplementedError('RADIS expects one molecule per run for the '+\
"moment. Got {0}. Use different runs ".format(id_set)+\
"and use MergeSlabs(out='transparent' afterwards")
self.input.molecule = get_molecule(id_set[0])
# ... explicitely write all isotopes based on isotopes found in the database
if self.input.isotope == 'all':
from radis.phys.convert import nm2cm, cm2nm
import matplotlib.pyplot as plt
from numpy import loadtxt, linspace
# Test even resampling
w_nm, I_nm = loadtxt(getTestFile('spectrum.txt')).T
w_cm, I_cm = resample_even(nm2cm(w_nm), I_nm, resfactor=2, energy_threshold=1e-3,
print_conservation=verbose)
if plot:
plt.figure()
plt.xlabel('Wavelength (nm)')
plt.ylabel('Intensity')
plt.plot(w_nm, I_nm, '-ok', label='original')
plt.plot(cm2nm(w_cm), I_cm, '-or', label='resampled')
plt.legend()
# Test resampling
w_crop = linspace(376, 381, 100)
I_crop = resample(w_nm, I_nm, w_crop, energy_threshold=0.01)
if plot:
plt.figure()
plt.xlabel('Wavelength (nm)')
plt.ylabel('Intensity')
plt.plot(w_nm, I_nm, '-ok', label='original')
plt.plot(w_crop, I_crop, '-or', label='resampled')
plt.legend()
if debug:
def calc_radiance(wavenumber, emissivity, Tgas, unit='mW/sr/cm2/nm'):
''' Derive radiance (mW/cm2/sr/nm) from the emissivity
Returns
-------
radiance: mW/sr/cm2/nm
'''
radiance = emissivity * planck(cm2nm(wavenumber), Tgas, unit=unit)
return radiance
# to the Spectrum `waveunit` if wavespaces are different)
# -------
wslit0, Islit0 = get_slit_function(slit_function, unit=unit, norm_by=norm_by,
shape=shape, center_wavespace=center_wavespace,
return_unit=waveunit, wstep=wstep,
auto_recenter_crop=auto_recenter_crop,
verbose=verbose,
plot=plot_slit, *args, **kwargs)
# Check if dispersion is too large
# ----
if waveunit == 'nm':
w_nm = w
wslit0_nm = wslit0
else:
w_nm = cm2nm(w)
wslit0_nm = cm2nm(wslit0)
if slit_dispersion is not None:
# add space (wings) on the side of each slice. This space is cut
# after convolution, removing side effects. Too much space and we'll
# overlap too much and loose performance. Not enough and we'll have
# artifacts on the jonction. We use the slit width to be conservative.
wings = len(wslit0)
# correct if the slit is to be interpolated (because wstep is different):
wings *= max(1, abs(int(np.diff(wslit0).mean() / wstep)))
slice_windows, wings_min, wings_max = _cut_slices(w_nm, slit_dispersion, wings=wings)
else:
slice_windows = [np.ones_like(w, dtype=np.bool)]
# Create dictionary to store convolved
I_conv_slices = {}
for qns in varlist: