Example #1
0
def lateral_histogram(H,coordinate='Y',show=1, filename="", title=""):

    if coordinate == 'Y':
        direction = "h"
    else:
        direction = 'v'
    # CALCULATE fwhm h
    hh = h["histogram_%s"%direction]
    bin = h["bin_%s_center"%direction]

    tt = numpy.where(hh >= max(hh) * 0.1)

    if hh[tt].size > 1:
        binSize = bin[1] - bin[0]
        fwp1m = binSize * (tt[0][-1] - tt[0][0])
        fwp1m_coordinates = (bin[tt[0][0]], bin[tt[0][-1]])
    print(fwp1m, fwp1m_coordinates)
    xt = numpy.array([1e3*bin.min(),1e3*bin.max()])
    yt = numpy.array([0.1*hh.max(),0.1*hh.max()])
    f = plot(1e3*bin, hh, xt, yt, xtitle="%s [mm]"%coordinate, title=title+"; Size at 0.1*height: %7.1f mm"%(1e3*fwp1m),show=0)
    if filename != "":
        f[0].savefig(filename)
        print("File written to disk: ",filename)
    if show: plot_show()
    return 1e3*fwp1m
    def plot_undul_cdf(undul_cdf_input,do_show=True):
        #
        # plots output of undul_cdf
        #
        try:
            from srxraylib.plot.gol import plot,plot_image,plot_show
        except:
            print("srxraylib not available: No plot")
            return

        if isinstance(undul_cdf_input,str):
            undul_cdf_dict = load_file_undul_cdf(undul_cdf_input)
        else:
            undul_cdf_dict = undul_cdf_input

        TWO = undul_cdf_dict['cdf_EnergyThetaPhi']
        ONE = undul_cdf_dict['cdf_EnergyTheta']
        ZERO = undul_cdf_dict['cdf_Energy']
        # E = undul_cdf_dict['energy']
        # T = undul_cdf_dict['theta']
        # P = undul_cdf_dict['phi']


        NG_E,NG_T,NG_P = ZERO.shape

        plot(numpy.arange(NG_E),TWO,title="cdf(energy) TWO",xtitle="index Energy",ytitle="cdf(E) TWO",show=0)
        plot_image(ONE,numpy.arange(NG_E),numpy.arange(NG_T),aspect='auto',
                   title="cdf(energy,theta) ONE",xtitle="index Energy",ytitle="index Theta",show=0)
        plot_image(ZERO[0,:,:],numpy.arange(NG_T),numpy.arange(NG_P),aspect='auto',
                   title="cdf (theta,phi) ZERO[0]",xtitle="index Theta",ytitle="index Phi",show=0)

        if do_show: plot_show()
    def plot_undul_phot(undul_phot_input,do_plot_intensity=True,do_plot_polarization=True,do_show=True,title=""):
        #
        # plots the output of undul_phot
        #
        try:
            from srxraylib.plot.gol import plot,plot_image,plot_show
        except:
            print("srxraylib not available: No plot")
            return

        if isinstance(undul_phot_input,str):
            undul_phot_dict = load_file_undul_phot(undul_phot_input)
            title += undul_phot_input
        else:
            undul_phot_dict = undul_phot_input


        if do_plot_intensity: plot_image(undul_phot_dict['radiation'][0,:,:],undul_phot_dict['theta']*1e6,undul_phot_dict['phi']*180/numpy.pi,
                   title="INTENS RN0[0] "+title,xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)

        if do_plot_polarization: plot_image(undul_phot_dict['polarization'][0,:,:],undul_phot_dict['theta']*1e6,undul_phot_dict['phi']*180/numpy.pi,
                   title="POL_DEG RN0[0] "+title,xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)


        if do_show: plot_show()
Example #4
0
    def compare_undul_phot_files(self,
                                 file1,
                                 file2,
                                 do_plot=DO_PLOT,
                                 do_assert=True):
        print("Comparing undul_phot output files: %s %s" % (file1, file2))

        dict1 = SourceUndulatorInputOutput.load_file_undul_phot(file_in=file1)
        dict2 = SourceUndulatorInputOutput.load_file_undul_phot(file_in=file2)

        rad1 = dict1["radiation"]
        # Do not compare polarizartion, I believe the preprocessor one is wrong
        # pol1 = dict1["polarization"]
        e1 = dict1["photon_energy"]
        t1 = dict1["theta"]
        p1 = dict1["phi"]

        rad2 = dict2["radiation"]
        # pol2 = dict2["polarization"]
        e2 = dict2["photon_energy"]
        t2 = dict2["theta"]
        p2 = dict2["phi"]

        print(r"---> Max diff E array %f " % ((e2 - e1).max()))
        print(r"---> Max diff T array %f " % ((t2 - t1).max()))
        print(r"---> Max diff P array %f " % ((p2 - p1).max()))

        rad_max = numpy.max((rad1, rad2))
        diff_max = numpy.max((rad1 - rad2))

        print(r"---> diff_rad_max/rad_max = %f %%" %
              (100 * diff_max / rad_max))

        if do_plot:
            plot_image(dict1['radiation'][0, :, :],
                       dict1['theta'] * 1e6,
                       dict1['phi'] * 180 / numpy.pi,
                       title="INTENS UNDUL_PHOT_PREPROCESSOR: RN0[0]" + file1,
                       xtitle="Theta [urad]",
                       ytitle="Phi [deg]",
                       aspect='auto',
                       show=False)

            plot_image(dict2['radiation'][0, :, :],
                       dict1['theta'] * 1e6,
                       dict1['phi'] * 180 / numpy.pi,
                       title="INTENS UNDUL_PHOT_PREPROCESSOR: RN0[0]" + file2,
                       xtitle="Theta [urad]",
                       ytitle="Phi [deg]",
                       aspect='auto',
                       show=False)

            plot_show()

        if do_assert:
            assert_almost_equal(e1, e2)
            assert_almost_equal(t1, t2)
            assert_almost_equal(p1, p2)
            # compare only points with appreciable intensity
            # accept if differences are less that 15%
            for ie, e in enumerate(e1):
                for it, t in enumerate(t1):
                    for ip, p in enumerate(p1):
                        if rad1[ie, it, ip] > 0.1 * rad_max:
                            mydiff = 100 * numpy.abs(rad1[ie, it, ip] - rad2[
                                ie, it, ip]) / rad1[ie, it, ip]
                            print(
                                r"--> intensity first:%g second:%g  diff:%g %%"
                                % (rad1[ie, it, ip], rad2[ie, it, ip], mydiff))
                            self.assertLess(mydiff, 15.)
    plot( 1e6*wf_prop.get_coordinate_x(),     horizontal_intensity_profile,
          1e6*wf_prop_srw.get_coordinate_x(), horizontal_intensity_profile_srw,
          show=0,
          legend=["in-python","SRW"],color=["red","black"],
          title="Horizontal profile of diffracted intensity",xtitle='X [um]',ytitle='Diffracted intensity [a.u.]')


    plot( 1e6*wf_prop.get_coordinate_y(),     vertical_intensity_profile,
          1e6*wf_prop_srw.get_coordinate_y(), vertical_intensity_profile_srw,
          show=0,
          legend=["in-python","SRW"],color=["red","black"],
          title="Vertical profile of diffracted intensity",xtitle='X [um]',ytitle='Diffracted intensity [a.u.]')


    plot_show()

    #
    # sample rays fro SHADOW
    #

    do_shadow = 0

    if do_shadow:

        import Shadow
        npoints = 10000
        xs, ys = sample_rays(wf.get_coordinate_x(),wf.get_coordinate_y(),wf.get_intensity(),number_of_points=npoints)
        xps, yps = sample_rays(wf_prop_srw.get_coordinate_x(),wf_prop_srw.get_coordinate_y(),wf_prop_srw.get_intensity(),
                               number_of_points=npoints)
        #
Example #6
0
    if False:
        CASES = [
            "diaboloid_bl1222_detrended",
            # "paraboliccone_bl1222_detrended",
            "ellipticalcylinder_bl1222_detrended",
            "linearized-parabolic-cone_bl1222_detrended",
        ]

        SHAPES = [
            'diaboloid',
            # 'parabolic-cone',
            'elliptical-cylinder',
            'linearized-parabolic-cone'
        ]

        for i in range(len(CASES)):
            Z, X, Y = create_surface(
                p=18.8,
                M=8.0750 / 18.8000,
                theta=2.0e-3,
                shape=SHAPES[i],
            )
            do_plot(Z, X, Y, filename_root=CASES[i], do_show=False)

    # Z, X, Y = create_surface(p=18.8, M=8.0750 / 18.8000, theta=2.0e-3, shape='linearized-parabolic-cone' )
    # title = r"$\theta$=%d mrad    M=q:p=1:%d" % (int(2e-3 * 1e3), int(1 / (18.0/8.075)))
    # do_plot(Z, X, Y, filename_root="linearizedparaboliccone_bl1222_detrended", title=title, do_show=False)

    plot_show()
Example #7
0
    def test_undul_cdf_NG_E_one(self, do_plot=DO_PLOT):

        print(
            "\n#                                                            ")
        print("# test_undul_cdf_NG_E_one  ")
        print(
            "#                                                              ")

        case = 0  # 2 = vui file

        if case == 0:

            tmp = \
                """
                {
                "LAMBDAU":     0.0320000015,
                "K":      0.250000000,
                "E_ENERGY":       6.03999996,
                "E_ENERGY_SPREAD":    0.00100000005,
                "NPERIODS": 50,
                "_EMIN":       10498.0000,
                "_EMAX":       10499.0000,
                "INTENSITY":      0.200000003,
                "_MAXANGLE":      0.000100,
                "_NG_E": 1,
                "_NG_T": 51,
                "_NG_P": 11,
                "NG_PLOT(1)":"0",
                "NG_PLOT(2)":"No",
                "NG_PLOT(3)":"Yes",
                "UNDUL_PHOT_FLAG(1)":"0",
                "UNDUL_PHOT_FLAG(2)":"Shadow code",
                "UNDUL_PHOT_FLAG(3)":"Urgent code",
                "UNDUL_PHOT_FLAG(4)":"SRW code",
                "UNDUL_PHOT_FLAG(5)":"Gaussian Approximation",
                "UNDUL_PHOT_FLAG(6)":"ESRF python code",
                "SEED": 36255,
                "SX":     0.0399999991,
                "SZ":    0.00100000005,
                "EX":   4.00000005E-07,
                "EZ":   3.99999989E-09,
                "_FLAG_EMITTANCE(1)":"0",
                "_FLAG_EMITTANCE(2)":"No",
                "_FLAG_EMITTANCE(3)":"Yes",
                "NRAYS": 15000,
                "F_BOUND_SOUR": 0,
                "FILE_BOUND":"NONESPECIFIED",
                "SLIT_DISTANCE":       1000.00000,
                "SLIT_XMIN":      -1.00000000,
                "SLIT_XMAX":       1.00000000,
                "SLIT_ZMIN":      -1.00000000,
                "SLIT_ZMAX":       1.00000000,
                "NTOTALPOINT": 10000000,
                "JUNK4JSON":0
                }
                """

            h = json.loads(tmp)

            # u = SourceUndulator()
            # u.load_json_shadowvui_dictionary(h)
            _calculate_shadow3_beam_using_preprocessors(h)

        elif case == 1:

            # some inputs
            E_ENERGY = 6.04
            INTENSITY = 0.2
            SX = 0.04
            SZ = 0.001
            SXP = 10e-6
            SZP = 4e-6
            FLAG_EMITTANCE = 1
            LAMBDAU = 0.032
            NPERIODS = 50
            K = 0.25
            EMIN = 10498.0000
            EMAX = 10499.0000
            NG_E = 101
            MAXANGLE = 100e-6
            NG_T = 51
            NG_P = 11
            N_J = 20
            SEED = 36255
            NRAYS = 15000

            u = SourceUndulator()

            u.set_from_keywords(
                E_ENERGY=E_ENERGY,
                INTENSITY=INTENSITY,
                SX=SX,
                SZ=SZ,
                SXP=SXP,
                SZP=SZP,
                FLAG_EMITTANCE=FLAG_EMITTANCE,
                LAMBDAU=LAMBDAU,
                NPERIODS=NPERIODS,
                K=K,
                EMIN=EMIN,
                EMAX=EMAX,
                NG_E=NG_E,
                MAXANGLE=MAXANGLE,
                NG_T=NG_T,
                NG_P=NG_P,
                N_J=N_J,
                SEED=SEED,
                NRAYS=NRAYS,
            )

            u.set_energy_monochromatic_at_resonance(harmonic_number=1)
        elif case == 2:

            # u = SourceUndulator()
            # u.load_json_shadowvui_file("xshundul.json")
            # calculate_shadow3_beam_using_preprocessors(json.loads(tmp))

            pass
        else:
            raise Exception("Undefined")

        #
        #
        #
        radiation = SourceUndulatorInputOutput.load_file_undul_phot(
            file_in="uphot.dat")

        cdf2 = SourceUndulatorFactory.undul_cdf(radiation, method='sum')
        SourceUndulatorInputOutput.write_file_undul_cdf(
            cdf2, file_out="xshundul2.sha")

        cdf3 = SourceUndulatorFactory.undul_cdf(radiation, method='trapz')
        SourceUndulatorInputOutput.write_file_undul_cdf(
            cdf3, file_out="xshundul3.sha")

        cdf1 = SourceUndulatorInputOutput.load_file_undul_cdf(
            file_in="xshundul.sha", )
        cdf2 = SourceUndulatorInputOutput.load_file_undul_cdf(
            file_in="xshundul2.sha")
        cdf3 = SourceUndulatorInputOutput.load_file_undul_cdf(
            file_in="xshundul3.sha")

        ZERO1 = cdf1['cdf_Energy']
        ONE1 = cdf1['cdf_EnergyTheta']
        TWO1 = cdf1['cdf_EnergyThetaPhi']

        ZERO2 = cdf2['cdf_Energy']
        ONE2 = cdf2['cdf_EnergyTheta']
        TWO2 = cdf2['cdf_EnergyThetaPhi']

        ZERO3 = cdf3['cdf_Energy']
        ONE3 = cdf3['cdf_EnergyTheta']
        TWO3 = cdf3['cdf_EnergyThetaPhi']

        #
        #
        NG_E = (radiation["photon_energy"]).size
        NG_T = (radiation["theta"]).size
        NG_P = (radiation["phi"]).size
        #
        print("-----> shape comparison", NG_E, NG_T, NG_P, ZERO1.shape,
              ZERO2.shape, ZERO3.shape)
        print("-----> shape comparison", NG_E, NG_T, NG_P, ONE1.shape,
              ONE2.shape, ONE3.shape)
        print("-----> shape comparison", NG_E, NG_T, NG_P, TWO1.shape,
              TWO2.shape, TWO3.shape)
        for ie in range(NG_E):
            for it in range(NG_T):
                for ip in range(NG_P):
                    # print(">>>>",TWO1[ie],TWO2[ie],TWO3[ie])
                    print(">>>>[%d %d %d]>>" % (ie, it, ip), ONE1[ie, it],
                          ONE2[ie, it], ONE3[ie, it])
                    # print(">>>>[%d %d %d]>>"%(ie,it,ip),ZERO1[ie,it,ip],ZERO2[ie,it,ip],ZERO3[ie,it,ip])

        tmp = numpy.where(ZERO1 > 0.1 * ZERO1.max())
        print("test_undul_cdf: ZERO:   sum/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ZERO2[tmp] - ZERO1[tmp]) / ZERO1[tmp]))))
        print("test_undul_cdf: ZERO: trapz/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ZERO3[tmp] - ZERO1[tmp]) / ZERO1[tmp]))))

        tmp = numpy.where(ONE1 > 0.1 * ONE1.max())
        print(r"test_undul_cdf: ONE:   sum/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ONE2[tmp] - ONE1[tmp]) / ONE1[tmp]))))
        print(r"test_undul_cdf: ONE: trapz/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ONE3[tmp] - ONE1[tmp]) / ONE1[tmp]))))

        if do_plot:

            plot_image(cdf1['cdf_Energy'][0, :, :],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="PREPROCESSORS cdf_Energy[0]",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)
            plot_image(cdf3['cdf_Energy'][0, :, :],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="internal-trapezoidal cdf_Energy[0]",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)

            plot_image(cdf1['cdf_EnergyTheta'],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="PREPROCESSORS cdf_EnergyTheta",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)
            plot_image(cdf3['cdf_EnergyTheta'],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="internal-trapezoidal cdf_EnergyTheta",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)
            plot_show()
Example #8
0
    def test_undul_cdf(self, do_plot=DO_PLOT):

        print(
            "\n#                                                            ")
        print("# test_undul_cdf  ")
        print(
            "#                                                              ")
        tmp = \
            """
            {
            "LAMBDAU":     0.0320000015,
            "K":      0.250000000,
            "E_ENERGY":       6.03999996,
            "E_ENERGY_SPREAD":    0.00100000005,
            "NPERIODS": 50,
            "_EMIN":       10200.0000,
            "_EMAX":       10650.0000,
            "INTENSITY":      0.2,
            "_MAXANGLE":     0.000015,
            "_NG_E": 11,
            "_NG_T": 51,
            "_NG_P": 11,
            "NG_PLOT(1)":"1",
            "NG_PLOT(2)":"No",
            "NG_PLOT(3)":"Yes",
            "UNDUL_PHOT_FLAG(1)":"4",
            "UNDUL_PHOT_FLAG(2)":"Shadow code",
            "UNDUL_PHOT_FLAG(3)":"Urgent code",
            "UNDUL_PHOT_FLAG(4)":"SRW code",
            "UNDUL_PHOT_FLAG(5)":"Gaussian Approx",
            "UNDUL_PHOT_FLAG(6)":"python code by Sophie",
            "SEED": 36255,
            "SX":     0.0399999991,
            "SZ":    0.00100000005,
            "EX":   4.00000005E-07,
            "EZ":   3.99999989E-09,
            "_FLAG_EMITTANCE(1)":"1",
            "_FLAG_EMITTANCE(2)":"No",
            "_FLAG_EMITTANCE(3)":"Yes",
            "NRAYS": 15000,
            "F_BOUND_SOUR": 0,
            "FILE_BOUND":"NONESPECIFIED",
            "SLIT_DISTANCE":       1000.00000,
            "SLIT_XMIN":      -1.00000000,
            "SLIT_XMAX":       1.00000000,
            "SLIT_ZMIN":      -1.00000000,
            "SLIT_ZMAX":       1.00000000,
            "NTOTALPOINT": 10000000,
            "JUNK4JSON":0
            }
            """

        h = json.loads(tmp)
        #
        # run_shadow3_using_preprocessors(h) # uphot.dat must exist

        # u = SourceUndulator()
        # u.load_json_shadowvui_dictionary(h)
        _calculate_shadow3_beam_using_preprocessors(h)

        #
        #
        #
        radiation = SourceUndulatorInputOutput.load_file_undul_phot(
            file_in="uphot.dat")

        cdf2 = SourceUndulatorFactory.undul_cdf(radiation, method='sum')
        SourceUndulatorInputOutput.write_file_undul_cdf(
            cdf2, file_out="xshundul2.sha")

        cdf3 = SourceUndulatorFactory.undul_cdf(radiation, method='trapz')
        SourceUndulatorInputOutput.write_file_undul_cdf(
            cdf3, file_out="xshundul3.sha")

        cdf1 = SourceUndulatorInputOutput.load_file_undul_cdf(
            file_in="xshundul.sha")
        cdf2 = SourceUndulatorInputOutput.load_file_undul_cdf(
            file_in="xshundul2.sha")
        cdf3 = SourceUndulatorInputOutput.load_file_undul_cdf(
            file_in="xshundul3.sha")

        ZERO1 = cdf1['cdf_Energy']
        ONE1 = cdf1['cdf_EnergyTheta']
        TWO1 = cdf1['cdf_EnergyThetaPhi']

        ZERO2 = cdf2['cdf_Energy']
        ONE2 = cdf2['cdf_EnergyTheta']
        TWO2 = cdf2['cdf_EnergyThetaPhi']

        ZERO3 = cdf3['cdf_Energy']
        ONE3 = cdf3['cdf_EnergyTheta']
        TWO3 = cdf3['cdf_EnergyThetaPhi']

        tmp = numpy.where(ZERO1 > 0.1 * ZERO1.max())
        print("test_undul_cdf: ZERO:   sum/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ZERO2[tmp] - ZERO1[tmp]) / ZERO1[tmp]))))
        print("test_undul_cdf: ZERO: trapz/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ZERO3[tmp] - ZERO1[tmp]) / ZERO1[tmp]))))

        tmp = numpy.where(ONE1 > 0.1 * ONE1.max())
        print(r"test_undul_cdf: ONE:   sum/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ONE2[tmp] - ONE1[tmp]) / ONE1[tmp]))))
        print(r"test_undul_cdf: ONE: trapz/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (ONE3[tmp] - ONE1[tmp]) / ONE1[tmp]))))

        tmp = numpy.where(TWO1 > 0.1 * TWO1.max())
        print("test_undul_cdf: TWO:   sum/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (TWO2[tmp] - TWO1[tmp]) / TWO1[tmp]))))
        print("test_undul_cdf: TWO: trapz/shadow3 %4.2f %%: " %
              (numpy.average(100 * numpy.abs(
                  (TWO3[tmp] - TWO1[tmp]) / TWO1[tmp]))))

        if do_plot:

            plot(cdf1["energy"],
                 cdf1["cdf_EnergyThetaPhi"],
                 cdf2["energy"],
                 cdf2["cdf_EnergyThetaPhi"],
                 cdf3["energy"],
                 cdf3["cdf_EnergyThetaPhi"],
                 title="cdf vs energy ",
                 xtitle="photon energy [eV]",
                 ytitle="cdf (integrated in theta,phi)",
                 legend=[
                     "preprocessor", "internal by sumation",
                     "internal by trapezoidal integration"
                 ],
                 show=False)

            plot_image(cdf1['cdf_Energy'][0, :, :],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="PREPROCESSORS cdf_Energy[0]",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)
            plot_image(cdf3['cdf_Energy'][0, :, :],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="internal-trapezoidal cdf_Energy[0]",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)

            plot_image(cdf1['cdf_EnergyTheta'],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="PREPROCESSORS cdf_EnergyTheta",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)
            plot_image(cdf3['cdf_EnergyTheta'],
                       1e6 * radiation['theta'],
                       radiation['phi'],
                       title="internal-trapezoidal cdf_EnergyTheta",
                       xtitle="Theta [urad]",
                       ytitle="Phi",
                       aspect='auto',
                       show=False)
            plot_show()
Example #9
0
    def test_comparison_undul_phot(self,
                                   do_plot_intensity=DO_PLOT,
                                   do_plot_polarization=DO_PLOT,
                                   do_plot_trajectory=DO_PLOT):

        print(
            "\n#                                                            ")
        print("# test_comparison_undul_phot  ")
        print(
            "#                                                              ")

        #
        # test undul_phot (undulator radiation)
        #

        # try:
        #     import pySRU
        #     is_available_pysru = True
        # except:
        #     is_available_pysru = False
        #
        # try:
        #     import srwlib
        #     is_available_srw = True
        # except:
        #     is_available_srw = False

        is_available_pysru = False
        is_available_srw = False

        tmp = \
            """
            {
            "LAMBDAU":     0.0320000015,
            "K":      0.250000000,
            "E_ENERGY":       6.03999996,
            "E_ENERGY_SPREAD":    0.00100000005,
            "NPERIODS": 50,
            "_EMIN":       10200.0000,
            "_EMAX":       10650.0000,
            "INTENSITY":      0.2,
            "_MAXANGLE":     0.000015,
            "_NG_E": 11,
            "_NG_T": 51,
            "_NG_P": 11,
            "NG_PLOT(1)":"1",
            "NG_PLOT(2)":"No",
            "NG_PLOT(3)":"Yes",
            "UNDUL_PHOT_FLAG(1)":"4",
            "UNDUL_PHOT_FLAG(2)":"Shadow code",
            "UNDUL_PHOT_FLAG(3)":"Urgent code",
            "UNDUL_PHOT_FLAG(4)":"SRW code",
            "UNDUL_PHOT_FLAG(5)":"Gaussian Approx",
            "UNDUL_PHOT_FLAG(6)":"python code by Sophie",
            "SEED": 36255,
            "SX":     0.0399999991,
            "SZ":    0.00100000005,
            "EX":   4.00000005E-07,
            "EZ":   3.99999989E-09,
            "_FLAG_EMITTANCE(1)":"1",
            "_FLAG_EMITTANCE(2)":"No",
            "_FLAG_EMITTANCE(3)":"Yes",
            "NRAYS": 15000,
            "F_BOUND_SOUR": 0,
            "FILE_BOUND":"NONESPECIFIED",
            "SLIT_DISTANCE":       1000.00000,
            "SLIT_XMIN":      -1.00000000,
            "SLIT_XMAX":       1.00000000,
            "SLIT_ZMIN":      -1.00000000,
            "SLIT_ZMAX":       1.00000000,
            "NTOTALPOINT": 10000000,
            "JUNK4JSON":0
            }
            """
        h = json.loads(tmp)

        # SHADOW3 preprocessor
        # run_shadow3_using_preprocessors(h)

        # u = SourceUndulator()
        # u.load_json_shadowvui_dictionary(h)
        _calculate_shadow3_beam_using_preprocessors(h)

        undul_phot_preprocessor_dict = SourceUndulatorInputOutput.load_file_undul_phot(
            "uphot.dat")

        # if do_plot_intensity: plot_image(undul_phot_preprocessor_dict['radiation'][0,:,:],undul_phot_preprocessor_dict['theta']*1e6,undul_phot_preprocessor_dict['phi']*180/numpy.pi,
        #            title="INTENS UNDUL_PHOT_PREPROCESSOR: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)
        #
        # if do_plot_polarization: plot_image(undul_phot_preprocessor_dict['polarization'][0,:,:],undul_phot_preprocessor_dict['theta']*1e6,undul_phot_preprocessor_dict['phi']*180/numpy.pi,
        #            title="POL_DEG UNDUL_PHOT_PREPROCESSOR: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)

        SourceUndulatorInputOutput.plot_undul_phot(
            undul_phot_preprocessor_dict,
            title="UNDUL_PHOT_PREPROCESSOR",
            do_plot_intensity=do_plot_intensity,
            do_plot_polarization=do_plot_polarization,
            do_show=False)

        # internal code
        undul_phot_dict = SourceUndulatorFactory.undul_phot(
            E_ENERGY=h["E_ENERGY"],
            INTENSITY=h["INTENSITY"],
            LAMBDAU=h["LAMBDAU"],
            NPERIODS=h["NPERIODS"],
            K=h["K"],
            EMIN=h["_EMIN"],
            EMAX=h["_EMAX"],
            NG_E=h["_NG_E"],
            MAXANGLE=h["_MAXANGLE"],
            NG_T=h["_NG_T"],
            NG_P=h["_NG_P"])

        # if do_plot_intensity: plot_image(undul_phot_dict['radiation'][0,:,:],undul_phot_dict['theta']*1e6,undul_phot_dict['phi']*180/numpy.pi,
        #            title="INTENS UNDUL_PHOT: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)
        # if do_plot_polarization: plot_image(undul_phot_dict['polarization'][0,:,:],undul_phot_dict['theta']*1e6,undul_phot_dict['phi']*180/numpy.pi,
        #            title="POL_DEG UNDUL_PHOT: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)

        SourceUndulatorInputOutput.plot_undul_phot(
            undul_phot_dict,
            title="UNDUL_PHOT",
            do_plot_intensity=do_plot_intensity,
            do_plot_polarization=do_plot_polarization,
            do_show=False)

        # pySRU
        if is_available_pysru:
            undul_phot_pysru_dict = SourceUndulatorFactoryPysru.undul_phot(
                E_ENERGY=h["E_ENERGY"],
                INTENSITY=h["INTENSITY"],
                LAMBDAU=h["LAMBDAU"],
                NPERIODS=h["NPERIODS"],
                K=h["K"],
                EMIN=h["_EMIN"],
                EMAX=h["_EMAX"],
                NG_E=h["_NG_E"],
                MAXANGLE=h["_MAXANGLE"],
                NG_T=h["_NG_T"],
                NG_P=h["_NG_P"])
            # if do_plot_intensity: plot_image(undul_phot_pysru_dict['radiation'][0,:,:],undul_phot_pysru_dict['theta']*1e6,undul_phot_pysru_dict['phi']*180/numpy.pi,
            #            title="INTENS UNDUL_PHOT_PYSRU: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)
            # if do_plot_polarization: plot_image(undul_phot_pysru_dict['polarization'][0,:,:],undul_phot_pysru_dict['theta']*1e6,undul_phot_pysru_dict['phi']*180/numpy.pi,
            #            title="POL_DEG UNDUL_PHOT_PYSRU: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)

            SourceUndulatorInputOutput.plot_undul_phot(
                undul_phot_pysru_dict,
                title="UNDUL_PHOT_PYSRU",
                do_plot_intensity=do_plot_intensity,
                do_plot_polarization=do_plot_polarization,
                do_show=False)

        # srw
        if is_available_srw:
            undul_phot_srw_dict = SourceUndulatorFactorySrw.undul_phot(
                E_ENERGY=h["E_ENERGY"],
                INTENSITY=h["INTENSITY"],
                LAMBDAU=h["LAMBDAU"],
                NPERIODS=h["NPERIODS"],
                K=h["K"],
                EMIN=h["_EMIN"],
                EMAX=h["_EMAX"],
                NG_E=h["_NG_E"],
                MAXANGLE=h["_MAXANGLE"],
                NG_T=h["_NG_T"],
                NG_P=h["_NG_P"])
            # if do_plot_intensity: plot_image(undul_phot_srw_dict['radiation'][0,:,:],undul_phot_srw_dict['theta']*1e6,undul_phot_srw_dict['phi']*180/numpy.pi,
            #            title="INTENS UNDUL_PHOT_SRW: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)
            # if do_plot_polarization: plot_image(undul_phot_srw_dict['polarization'][0,:,:],undul_phot_srw_dict['theta']*1e6,undul_phot_srw_dict['phi']*180/numpy.pi,
            #            title="POL_DEG UNDUL_PHOT_SRW: RN0[0]",xtitle="Theta [urad]",ytitle="Phi [deg]",aspect='auto',show=False)

            SourceUndulatorInputOutput.plot_undul_phot(
                undul_phot_srw_dict,
                title="UNDUL_PHOT_SRW",
                do_plot_intensity=do_plot_intensity,
                do_plot_polarization=do_plot_polarization,
                do_show=False)

        x = undul_phot_dict["photon_energy"]
        y0 = (undul_phot_preprocessor_dict["radiation"]).sum(axis=2).sum(
            axis=1)
        y1 = (undul_phot_dict["radiation"]).sum(axis=2).sum(axis=1)
        if is_available_pysru:
            y2 = (undul_phot_pysru_dict["radiation"]).sum(axis=2).sum(axis=1)
        if is_available_srw:
            y3 = (undul_phot_srw_dict["radiation"]).sum(axis=2).sum(axis=1)

        if do_plot_intensity:
            if is_available_pysru and is_available_srw:
                plot(x,
                     y0,
                     x,
                     y1,
                     x,
                     y2,
                     x,
                     y3,
                     xtitle="Photon energy [eV]",
                     ytitle="Flux[photons/s/eV/rad^2]",
                     legend=["preprocessor", "internal", "pySRU", "SRW"])
            else:
                if is_available_pysru:
                    plot(x,
                         y0,
                         x,
                         y1,
                         x,
                         y2,
                         xtitle="Photon energy [eV]",
                         ytitle="Flux[photons/s/eV/rad^2]",
                         legend=["preprocessor", "internal", "pySRU"])
                if is_available_srw:
                    plot(x,
                         y0,
                         x,
                         y1,
                         x,
                         y3,
                         xtitle="Photon energy [eV]",
                         ytitle="Flux[photons/s/eV/rad^2]",
                         legend=["preprocessor", "internal", "SRW"])
        tmp = numpy.where(y0 > 0.1)

        print("\n")
        print(">>> test_undul_phot: preprocessor/internal: %4.2f %% " %
              (numpy.average(100 * numpy.abs((y0[tmp] - y1[tmp]) / y1[tmp]))))
        self.assertLess(
            numpy.average(100 * numpy.abs((y0[tmp] - y1[tmp]) / y1[tmp])), 5)
        if is_available_pysru:
            print(">>> test_undul_phot:        pySRU/internal: %4.2f %% " %
                  (numpy.average(100 * numpy.abs(
                      (y2[tmp] - y1[tmp]) / y1[tmp]))))
            self.assertLess(
                numpy.average(100 * numpy.abs((y2[tmp] - y1[tmp]) / y1[tmp])),
                1)
        if is_available_srw:
            print(">>> test_undul_phot:          SRW/internal: %4.2f %% " %
                  (numpy.average(100 * numpy.abs(
                      (y3[tmp] - y1[tmp]) / y1[tmp]))))
            self.assertLess(
                numpy.average(100 * numpy.abs((y3[tmp] - y1[tmp]) / y1[tmp])),
                5)

        #
        # trajectory
        #
        if do_plot_trajectory:
            # Trajectory is only in undul_phot (internal) and und_phot_pysru
            # t0 = (undul_phot_preprocessor_dict["trajectory"])
            t1 = (undul_phot_dict["trajectory"])
            if is_available_pysru:
                t2 = (undul_phot_pysru_dict["trajectory"])
                plot(t1[3],
                     1e6 * t1[1],
                     t2[3],
                     1e6 * t2[1],
                     title='Trajectory',
                     xtitle='z [m]',
                     ytitle='x[um]',
                     legend=['internal', 'pysru'],
                     show=False)
            else:
                plot(t1[3],
                     1e6 * t1[1],
                     title='Trajectory',
                     xtitle='z [m]',
                     ytitle='x[um]',
                     legend=['internal'],
                     show=False)

        if do_plot_polarization or do_plot_intensity or do_plot_trajectory:
            plot_show()