def test_auto_correct_dispersion(f=750, phi=-6, gr=2400, verbose=True, plot=True, close_plots=True, *args, **kwargs): ''' A test case to show the effect of wavelength dispersion (cf spectrometer reciprocal function) on the slit function Parameters ---------- f: focal length (mm) default 750 (SpectraPro 2750i) phi: angle in degrees (°) default -6 gr: grooves spacing (gr/mm) default 2400 ''' from radis.test.utils import getTestFile from publib import set_style, fix_style from radis.misc.warning import SlitDispersionWarning if plot: plt.ion() # dont get stuck with Matplotlib if executing through pytest if close_plots: plt.close('all') w_slit_632, I_slit_632 = import_experimental_slit(getTestFile('slitfunction.txt')) slit_measured_632nm = getTestFile('slitfunction.txt') w, I = np.loadtxt(getTestFile('calc_N2C_spectrum_Trot1200_Tvib3000.txt')).T s = calculated_spectrum(w, I, conditions={'Tvib': 3000, 'Trot': 1200}, Iunit='mW/cm2/sr/µm') slit_dispersion = lambda w: linear_dispersion(w, f=f, phi=phi, m=1, gr=gr) s.apply_slit(slit_measured_632nm) if plot: w_full_range = np.linspace(w.min(), w_slit_632.max()) set_style('origin') plt.figure('Spectrometer Dispersion (f={0}mm, phi={1}°, gr={2}'.format( f, phi, gr)) plt.plot(w_full_range, slit_dispersion(w_full_range)) plt.xlabel('Wavelength (nm)') plt.ylabel('Reciprocal Linear Dispersion') # Compare 2 spectra s.plot(nfig='Linear dispersion effect', color='r', label='not corrected') with pytest.warns(SlitDispersionWarning): # expect a "large slit dispersion" warning s.apply_slit(slit_measured_632nm, slit_dispersion=slit_dispersion) if plot: s.plot(nfig='same', color='k', label='corrected') plt.legend() # Plot different slits: s.plot_slit() # plt.plot(w_slit_632, I_slit_632, color='r', label='Not corrected') # plt.legend() return True # nothing defined yet
def test_slit_energy_conservation(verbose=True, plot=True, close_plots=True, *args, **kwargs): """ Convoluted and non convoluted quantities should have the same area (difference arises from side effects if the initial spectrum is not 0 on the sides """ from radis.test.utils import getTestFile if plot: import matplotlib.pyplot as plt plt.ion() # dont get stuck with Matplotlib if executing through pytest if close_plots: plt.close("all") if verbose: print("\n>>> _test_slit_energy_conservation\n") s = calculated_spectrum(*np.loadtxt( getTestFile("calc_N2C_spectrum_Trot1200_Tvib3000_slit0.1.txt")).T, wunit="nm", Iunit="mW/cm2/sr/nm") # arbitrary) P = s.get_power(unit="mW/cm2/sr") s.apply_slit(0.5, norm_by="area") w, I = s.get("radiance", wunit="nm", Iunit="mW/cm2/sr/nm") Pc = abs(np.trapz(I, x=w)) # mW/cm2/sr b = np.isclose(P, Pc, 3e-2) if plot: fig = plt.figure(fig_prefix + "energy conservation during resampling") s.plot(nfig=fig.number, label="{0:.1f} mW/cm2/sr".format(P)) s.plot("radiance_noslit", nfig=fig.number, label="{0:.1f} mW/cm2/sr".format(Pc)) plt.title("Energy conservation: {0}".format(b)) plt.legend() plt.tight_layout() assert np.isclose(P, Pc, 3e-2) return True
def test_normalisation_mode(plot=True, close_plots=True, verbose=True, *args, **kwargs): """ Test norm_by = 'area' vs norm_by = 'max' """ from radis.test.utils import getTestFile if plot: plt.ion() # dont get stuck with Matplotlib if executing through pytest if close_plots: plt.close("all") from publib import set_style set_style("origin") # %% Compare spectra convolved with area=1 and max=1 # Slit in nm # Spectrum in nm # Specair units: mW/cm2/sr/µm w, I = np.loadtxt(getTestFile("calc_N2C_spectrum_Trot1200_Tvib3000.txt")).T s = calculated_spectrum(w, I, conditions={ "Tvib": 3000, "Trot": 1200 }, Iunit="mW/cm2/sr/µm") FWHM = 2 s.apply_slit(FWHM, norm_by="area") # spectrum convolved with area=1 w_area, I_area = s.get("radiance") if plot: fig = plt.figure(fig_prefix + "Spectrum in nm + slit in nm") fig.clear() ax = fig.gca() s.plot(nfig=fig.number, wunit="nm", label="norm_by: area", lw=3) s.apply_slit(FWHM, norm_by="max") # spectrum convolved with max=1 w_max, I_max = s.get("radiance", wunit="nm") if plot: ax.plot(w_max, I_max / FWHM, "r", label="(norm_by:max)/FWHM") ax.legend(loc="best") assert np.allclose(I_area, I_max / FWHM) if verbose: print("equivalence of normalisation mode for spectrum in 'nm': OK") # %% Compare spectra convolved with area=1 and max=1 # Slit in nm # Spectrum in cm-1 s = load_spec(getTestFile("CO_Tgas1500K_mole_fraction0.01.spec"), binary=True) s.update() # spectrum convolved with area=1 s.apply_slit(FWHM, norm_by="area", plot_slit=plot) w_area, I_area = s.get("radiance") if plot: fig = plt.figure(fig_prefix + "Spectrum in cm-1 + slit in nm") fig.clear() ax = fig.gca() s.plot(nfig=fig.number, wunit="nm", label="norm_by: area", lw=3) # spectrum convolved with max=1 s.apply_slit(FWHM, norm_by="max", plot_slit=plot) w_max, I_max = s.get("radiance", wunit="nm") if plot: ax.plot(w_max, I_max / FWHM, "r", label="(norm_by:max)/FWHM") ax.legend(loc="best") assert np.allclose(I_area, I_max / FWHM) if verbose: print( "equivalence of normalisation mode for spectrum in 'cm-1': {0}: OK" ) assert is_homogeneous(s.units["radiance"], "mW/cm2/sr") if verbose: print(("radiance unit ({0}) is homogeneous to 'mW/cm2/sr': OK".format( s.units["radiance"]))) return True
def test_against_specair_convolution(plot=True, close_plots=True, verbose=True, debug=False, *args, **kwargs): if plot: plt.ion() # dont get stuck with Matplotlib if executing through pytest if close_plots: plt.close("all") # Test from radis.test.utils import getTestFile # Plot calculated vs convolved with slit # Specair units: mW/cm2/sr/µm w, I = np.loadtxt(getTestFile("calc_N2C_spectrum_Trot1200_Tvib3000.txt")).T s = calculated_spectrum(w, I, conditions={ "Tvib": 3000, "Trot": 1200 }, Iunit="mW/cm2/sr/µm") if plot: fig = plt.figure(fig_prefix + "SPECAIR convoluted vs SPECAIR non convoluted") s.plot("radiance_noslit", nfig=fig.number, label="calc") slit_nm = 0.1 s.apply_slit(slit_nm, shape="triangular", norm_by="area") if plot: s.plot( "radiance", nfig=fig.number, label="slit {0}nm".format(slit_nm), color="r", lw=2, ) plt.legend() # Compare with Specair slit function s.apply_slit(slit_nm, norm_by="max") # Note unit conversion from mW/cm2/sr/um*nm is properly done! if plot: fig = plt.figure(fig_prefix + "convoluted RADIS vs convoluted SPECAIR") s.plot( "radiance", nfig=fig.number, label="slit {0}nm, norm with max".format(slit_nm), color="r", lw=2, Iunit="mW/cm2/sr", ) # ... Get initial spectrum convoluted with Specair ws, Is = np.loadtxt( getTestFile("calc_N2C_spectrum_Trot1200_Tvib3000_slit0.1.txt") ).T # Specair units: mW/cm2/sr if plot: plt.plot(ws, Is, "k", label="normalized in Specair (sides not cropped)") # ... Test output is the same wu, Iu = s.get("radiance", Iunit="mW/cm2/sr") As = np.trapz(Is, ws) # Todo one day: replace with get_power() function Au = np.trapz(Iu, wu) if verbose: print(("Integrals should match: {0:.2f} vs {1:.2f} ({2:.2f}% error)". format(As, Au, 100 * abs(As - Au) / As))) assert np.isclose(As, Au, rtol=1e-2) # Test resampling s.resample(linspace(376, 380.6, 3000), unit="nm", if_conflict_drop="convoluted") s.apply_slit(slit_nm, norm_by="max") if plot: s.plot( "radiance", Iunit="mW/cm2/sr", lw=3, nfig=fig.number, color="b", zorder=-3, label="Resampled", ) plt.legend() if verbose: print("\n>>>Testing spectrum slit matches Specair: OK") return True
def test_normalisation_mode(plot=True, close_plots=True, verbose=True, *args, **kwargs): ''' Test norm_by = 'area' vs norm_by = 'max' ''' from radis.test.utils import getTestFile if plot: plt.ion() # dont get stuck with Matplotlib if executing through pytest if close_plots: plt.close('all') from publib import set_style set_style('origin') # %% Compare spectra convolved with area=1 and max=1 # Slit in nm # Spectrum in nm # Specair units: mW/cm2/sr/µm w, I = np.loadtxt(getTestFile('calc_N2C_spectrum_Trot1200_Tvib3000.txt')).T s = calculated_spectrum(w, I, conditions={ 'Tvib': 3000, 'Trot': 1200 }, Iunit='mW/cm2/sr/µm') FWHM = 2 s.apply_slit(FWHM, norm_by='area') # spectrum convolved with area=1 w_area, I_area = s.get('radiance') if plot: fig = plt.figure(fig_prefix + 'Spectrum in nm + slit in nm') fig.clear() ax = fig.gca() s.plot(nfig=fig.number, wunit='nm', label='norm_by: area', lw=3) s.apply_slit(FWHM, norm_by='max') # spectrum convolved with max=1 w_max, I_max = s.get('radiance', wunit='nm') if plot: ax.plot(w_max, I_max / FWHM, 'r', label='(norm_by:max)/FWHM') ax.legend(loc='best') assert np.allclose(I_area, I_max / FWHM) if verbose: print("equivalence of normalisation mode for spectrum in 'nm': OK") # %% Compare spectra convolved with area=1 and max=1 # Slit in nm # Spectrum in cm-1 s = load_spec(getTestFile('CO_Tgas1500K_mole_fraction0.01.spec'), binary=True) s.update() # spectrum convolved with area=1 s.apply_slit(FWHM, norm_by='area', plot_slit=plot) w_area, I_area = s.get('radiance') if plot: fig = plt.figure(fig_prefix + 'Spectrum in cm-1 + slit in nm') fig.clear() ax = fig.gca() s.plot(nfig=fig.number, wunit='nm', label='norm_by: area', lw=3) # spectrum convolved with max=1 s.apply_slit(FWHM, norm_by='max', plot_slit=plot) w_max, I_max = s.get('radiance', wunit='nm') if plot: ax.plot(w_max, I_max / FWHM, 'r', label='(norm_by:max)/FWHM') ax.legend(loc='best') assert np.allclose(I_area, I_max / FWHM) if verbose: print( "equivalence of normalisation mode for spectrum in 'cm-1': {0}: OK" ) assert is_homogeneous(s.units['radiance'], 'mW/cm2/sr') if verbose: print(("radiance unit ({0}) is homogeneous to 'mW/cm2/sr': OK".format( s.units['radiance']))) return True
def test_against_specair_convolution(plot=True, close_plots=True, verbose=True, debug=False, *args, **kwargs): if plot: plt.ion() # dont get stuck with Matplotlib if executing through pytest if close_plots: plt.close('all') # Test from radis.test.utils import getTestFile # Plot calculated vs convolved with slit # Specair units: mW/cm2/sr/µm w, I = np.loadtxt(getTestFile('calc_N2C_spectrum_Trot1200_Tvib3000.txt')).T s = calculated_spectrum(w, I, conditions={ 'Tvib': 3000, 'Trot': 1200, 'medium': 'air' }, Iunit='mW/cm2/sr/µm') if plot: fig = plt.figure(fig_prefix + 'SPECAIR convoluted vs SPECAIR non convoluted') s.plot('radiance_noslit', nfig=fig.number, label='calc') slit_nm = 0.1 s.apply_slit(slit_nm, shape='triangular', norm_by='area') if plot: s.plot('radiance', nfig=fig.number, label='slit {0}nm'.format(slit_nm), color='r', lw=2) plt.legend() # Compare with Specair slit function s.apply_slit(slit_nm, norm_by='max') # Note unit conversion from mW/cm2/sr/um*nm is properly done! if plot: fig = plt.figure(fig_prefix + 'convoluted RADIS vs convoluted SPECAIR') s.plot('radiance', nfig=fig.number, label='slit {0}nm, norm with max'.format(slit_nm), color='r', lw=2, Iunit='mW/cm2/sr') # ... Get initial spectrum convoluted with Specair ws, Is = np.loadtxt( getTestFile('calc_N2C_spectrum_Trot1200_Tvib3000_slit0.1.txt') ).T # Specair units: mW/cm2/sr if plot: plt.plot(ws, Is, 'k', label='normalized in Specair (sides not cropped)') # ... Test output is the same wu, Iu = s.get('radiance', Iunit='mW/cm2/sr') As = np.trapz(Is, ws) # Todo one day: replace with get_power() function Au = np.trapz(Iu, wu) if verbose: print(('Integrals should match: {0:.2f} vs {1:.2f} ({2:.2f}% error)'. format(As, Au, 100 * abs(As - Au) / As))) assert np.isclose(As, Au, rtol=1e-2) # Test resampling s.resample(linspace(376, 380.6, 3000), unit='nm', if_conflict_drop='convoluted') s.apply_slit(slit_nm, norm_by='max') if plot: s.plot('radiance', Iunit='mW/cm2/sr', lw=3, nfig=fig.number, color='b', zorder=-3, label='Resampled') plt.legend() if verbose: print('\n>>>Testing spectrum slit matches Specair: OK') return True