def test_units(verbose=True, *args, **kwargs): # Test unit-ware arrays # RADIS pint-aware array # b = (print(a.to('V * cm^2'))==print(res)) # Test conversion convtable = [ (500, "nm", 0.5, "µm"), (1, "erg/s", 1e-7, "W"), (1, "m**2", 10000, "cm**2"), ] for a, f, r, t in convtable: cr = conv2(a, f, t) if verbose: print(("{0} {1} = {2} {3}".format(a, f, cr, t))) assert isclose(cr, r) # Ensures that an error is raised if units with angles are converted # (mathematically correct because angles are dimensionless, but prone # to user errors) assert not is_homogeneous("mW/cm2/sr/nm", "mW/cm2/nm") with pytest.raises(TypeError): conv2(1, "mW/cm**2/sr/nm", "mW/cm**2/nm") return True
def test_convoluted_quantities_units(*args, **kwargs): """ Test that units are correctly convoluted after convolution """ from radis.test.utils import getTestFile s = load_spec(getTestFile("CO_Tgas1500K_mole_fraction0.5.spec"), binary=True) s.update(verbose=False) assert s.units["radiance_noslit"] == "mW/cm2/sr/nm" assert s.units["transmittance_noslit"] == "" s.apply_slit(0.5, norm_by="area", verbose=False) assert s.units["radiance"] == "mW/cm2/sr/nm" assert s.units["transmittance"] == "" s.apply_slit(0.5, norm_by="max", verbose=False) assert is_homogeneous(s.units["radiance"], "mW/cm2/sr") assert s.units["transmittance"] == "nm" # whatever that means
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_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