def test_slit_unit_conversions_spectrum_in_nm(verbose=True, plot=True, close_plots=True, *args, **kwargs): ''' Test that slit is consistently applied for different units Assert that: - calculated FWHM is the one that was applied ''' from radis.test.utils import getTestFile from radis.spectrum.spectrum import Spectrum if plot: # dont get stuck with Matplotlib if executing through pytest plt.ion() if close_plots: plt.close('all') # %% Get a Spectrum (stored in nm) s_nm = Spectrum.from_txt( getTestFile('calc_N2C_spectrum_Trot1200_Tvib3000.txt'), quantity='radiance_noslit', waveunit='nm', unit='mW/cm2/sr/µm', conditions={ 'medium': 'air', 'self_absorption': False }) with catch_warnings(): filterwarnings( 'ignore', 'Condition missing to know if spectrum is at equilibrium:') # just because it makes better units s_nm.rescale_path_length(1, 0.001) wstep = np.diff(s_nm.get_wavelength())[0] assert s_nm.get_waveunit() == 'nm' # ensures it's stored in cm-1 for shape in ['gaussian', 'triangular']: # Apply slit in nm slit_nm = 0.5 s_nm.name = 'Spec in nm, slit {0:.2f} nm'.format(slit_nm) s_nm.apply_slit(slit_nm, unit='nm', shape=shape, mode='same') # ... mode=same to keep same output length. It helps compare both Spectra afterwards # in cm-1 as that's s.get_waveunit() fwhm = get_FWHM(*s_nm.get_slit()) assert np.isclose(slit_nm, fwhm, atol=2 * wstep) # Apply slit in nm this time s_cm = s_nm.copy() w_nm = s_nm.get_wavelength(which='non_convoluted') slit_cm = dnm2dcm(slit_nm, w_nm[len(w_nm) // 2]) s_cm.name = 'Spec in nm, slit {0:.2f} cm-1'.format(slit_cm) s_cm.apply_slit(slit_cm, unit='cm-1', shape=shape, mode='same') plotargs = {} if plot: plotargs[ 'title'] = 'test_slit_unit_conversions: {0} ({1} nm)'.format( shape, slit_nm) s_nm.compare_with(s_cm, spectra_only='radiance', rtol=1e-3, verbose=verbose, plot=plot, **plotargs)
def test_slit_unit_conversions_spectrum_in_nm(verbose=True, plot=True, close_plots=True, *args, **kwargs): """Test that slit is consistently applied for different units Assert that: - calculated FWHM is the one that was applied """ from radis.test.utils import getTestFile from radis.spectrum.spectrum import Spectrum if plot: # dont get stuck with Matplotlib if executing through pytest plt.ion() if close_plots: plt.close("all") # %% Get a Spectrum (stored in nm) s_nm = Spectrum.from_txt( getTestFile("calc_N2C_spectrum_Trot1200_Tvib3000.txt"), quantity="radiance_noslit", waveunit="nm", unit="mW/cm2/sr/µm", conditions={"self_absorption": False}, ) with catch_warnings(): filterwarnings( "ignore", "Condition missing to know if spectrum is at equilibrium:") # just because it makes better units s_nm.rescale_path_length(1, 0.001) wstep = np.diff(s_nm.get_wavelength())[0] assert s_nm.get_waveunit() == "nm" # ensures it's stored in cm-1 for shape in ["gaussian", "triangular"]: # Apply slit in nm slit_nm = 0.5 s_nm.name = "Spec in nm, slit {0:.2f} nm".format(slit_nm) s_nm.apply_slit(slit_nm, unit="nm", shape=shape, mode="same") # ... mode=same to keep same output length. It helps compare both Spectra afterwards # in cm-1 as that's s.get_waveunit() fwhm = get_FWHM(*s_nm.get_slit()) assert np.isclose(slit_nm, fwhm, atol=2 * wstep) # Apply slit in nm this time s_cm = s_nm.copy() w_nm = s_nm.get_wavelength(which="non_convoluted") slit_cm = dnm2dcm(slit_nm, w_nm[len(w_nm) // 2]) s_cm.name = "Spec in nm, slit {0:.2f} cm-1".format(slit_cm) s_cm.apply_slit(slit_cm, unit="cm-1", shape=shape, mode="same") plotargs = {} if plot: plotargs[ "title"] = "test_slit_unit_conversions: {0} ({1} nm)".format( shape, slit_nm) s_nm.compare_with(s_cm, spectra_only="radiance", rtol=1e-3, verbose=verbose, plot=plot, **plotargs)