Esempio n. 1
0
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
Esempio n. 2
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
Esempio 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