Ejemplo n.º 1
0
def test_linear_dispersion_effect(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
    
    Test succeeds if a :py:data:`~radis.misc.warning.SlitDispersionWarning` 
    is correctly triggered
    '''

    from radis.test.utils import getTestFile
    from publib import set_style, fix_style
    
    
    if plot:
        set_style('origin')
        plt.ion()   # dont get stuck with Matplotlib if executing through pytest
        if close_plots:
            plt.close('all')

    w_slit, I_slit = import_experimental_slit(getTestFile('slitfunction.txt'))

    if plot:
        plt.figure(fig_prefix+'Linear dispersion effect')
        plt.plot(w_slit, I_slit, '--k', label='Exp: FWHM @{0}nm: {1:.3f} nm'.format(632.8,
                                                                                    get_effective_FWHM(w_slit, I_slit)))

    from radis.misc.warning import SlitDispersionWarning
    with pytest.warns(SlitDispersionWarning):   # expect a "large slit dispersion" warning
        
        # Test how slit function FWHM scales with linear_dispersion
        for w0, FWHM in zip([380, 1000, 4200, 5500],
                            [0.396, 0.388, 0.282, 0.188]):
            w, I = dirac(w0)
    
            wc, Ic = convolve_with_slit(w, I, w_slit, I_slit, norm_by='area',
                                        slit_dispersion=linear_dispersion,
                                        verbose=False)
            assert np.isclose(FWHM, get_effective_FWHM(wc, Ic), atol=0.001)
    
            if plot:
                plt.plot(wc, Ic, label='FWHM @{0:.2f} nm: {1:.3f} nm'.format(w0,
                                                                             get_effective_FWHM(wc, Ic)))

    if plot:
        plt.xlabel('Wavelength (nm)')
        plt.ylabel('Dirac $x$ slit function')
        plt.legend(loc='best', prop={'size': 15})
        fix_style('article')

    return True
Ejemplo n.º 2
0
def test_all_slit_shapes(FWHM=0.4,
                         verbose=True,
                         plot=True,
                         close_plots=True,
                         *args,
                         **kwargs):
    """ Test all slit generation functions and make sure we get the expected FWHM"""

    if plot:
        plt.ion()  # dont get stuck with Matplotlib if executing through pytest
        if close_plots:
            plt.close("all")

    # get spectrum
    from radis.test.utils import getTestFile
    from radis.spectrum.spectrum import Spectrum

    s = Spectrum.from_txt(
        getTestFile("calc_N2C_spectrum_Trot1200_Tvib3000.txt"),
        quantity="radiance_noslit",
        waveunit="nm",
        unit="mW/cm2/sr/µm",
    )
    wstep = np.diff(s.get_wavelength())[0]

    # Plot all slits
    # ... gaussian
    s.apply_slit(FWHM, unit="nm", shape="gaussian", plot_slit=plot)
    assert np.isclose(get_FWHM(*s.get_slit()), FWHM, atol=2 * wstep)

    # ... triangular
    s.apply_slit(FWHM, unit="nm", shape="triangular", plot_slit=plot)
    assert np.isclose(get_FWHM(*s.get_slit()), FWHM, atol=2 * wstep)

    # ... trapezoidal
    s.apply_slit((FWHM * 0.9, FWHM * 1.1),
                 unit="nm",
                 shape="trapezoidal",
                 plot_slit=plot)
    assert np.isclose(get_FWHM(*s.get_slit()), FWHM, atol=2 * wstep)

    #    # ... trapezoidal
    #    s.apply_slit(FWHM, unit='nm', shape='trapezoidal', plot_slit=plot, norm='max')
    #    assert np.isclose(get_FWHM(*s.get_slit()), FWHM, atol=1.1*wstep)

    # ... experimental
    s.apply_slit(getTestFile("slitfunction.txt"), unit="nm", plot_slit=plot)
    assert np.isclose(get_effective_FWHM(*s.get_slit()), FWHM, atol=0.01)
    # note that we're applying a slit function measured at 632.5 nm to a Spectrum
    # at 4.7 µm. It's just good for testing the functions

    #    # ... experimental, convolve with max
    #    s.apply_slit(getTestFile('slitfunction.txt'), unit='nm', norm_by='max', plot_slit=plot)
    #    assert np.isclose(get_FWHM(*s.get_slit()), FWHM, atol=1.1*wstep)

    if verbose:
        print("\n>>> _test_all_slits yield correct FWHM (+- wstep) : OK\n")

    return True  # nothing defined yet
Ejemplo n.º 3
0
def test_slit_function_effect__fast(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 '''

    from radis.test.utils import getTestFile
    from publib import fix_style
    from numpy import pi, tan, cos

    if plot:
        plt.ion()  # dont get stuck with Matplotlib if executing through pytest
        if close_plots: plt.close('all')

    # Effect of Slit dispersion

    def dirac(w0, width=20, wstep=0.009):
        ''' Return a Dirac in w0. Plus some space on the side '''

        w = np.arange(w0 - width, w0 + width + wstep, wstep)
        I = np.zeros_like(w)
        I[len(I) // 2] = 1 / wstep

        return w, I

    def linear_dispersion(w, f=750, phi=-6, m=1, gr=300):
        ''' dlambda / dx
        Default values correspond to Acton 750i

        Input
        -------
        f: focal length (mm)
             default 750 (SpectraPro 2750i)

        phi: angle in degrees (°)
            default 9

        m: order of dispersion
            default 1

        gr: grooves spacing (gr/mm)
            default 300
        '''
        # correct units:
        phi *= 2 * pi / 360
        d = 1e-3 / gr
        disp = w / (2 * f) * (tan(phi) + sqrt((2 * d / m /
                                               (w * 1e-9) * cos(phi))**2 - 1))
        return disp  # to nm/mm

    w_slit, I_slit = import_experimental_slit(getTestFile('slitfunction.txt'))

    if plot:
        plt.figure(fig_prefix + 'Linear dispersion effect')
        plt.plot(w_slit,
                 I_slit,
                 '--k',
                 label='Exp: FWHM @{0}nm: {1:.3f} nm'.format(
                     632.8, get_effective_FWHM(w_slit, I_slit)))

    # Test how slit function FWHM scales with linear_dispersion
    for w0, FWHM in zip([380, 1000, 4200, 5500], [0.396, 0.388, 0.282, 0.188]):
        w, I = dirac(w0)
        wc, Ic = convolve_with_slit(w,
                                    I,
                                    w_slit,
                                    I_slit,
                                    norm_by='area',
                                    slit_dispersion=linear_dispersion)
        assert np.isclose(FWHM, get_effective_FWHM(wc, Ic), atol=0.001)

        if plot:
            plt.plot(wc,
                     Ic,
                     label='FWHM @{0:.2f} nm: {1:.3f} nm'.format(
                         w0, get_effective_FWHM(wc, Ic)))

    if plot:
        plt.xlabel('Wavelength (nm)')
        plt.ylabel('Dirac $x$ slit function')
        plt.legend(loc='best', prop={'size': 15})
        fix_style('article')

    return True  # nothing defined yet