In [1]:
"""
Example 5

- import a data file (magnetic distribution of an undulator)
- check the phase error and compare with an ideal case
"""

import sys
isjupyter = "ipykernel" in sys.modules

import spectra 
if isjupyter:
    spectra.Start(mode="c") 
    import plotly.io as pio
    pio.renderers.default = "notebook"
else:
    spectra.Start(src="l") 

spectra.Open("sample.json") 
spectra.SelectBL("X-ray-BL")
spectra.SelectCalculation("near", "energy", "fdenss")

# calculation with an ideal condition
spectra.StartCalculation(folder="./output", prefix="sample5", serial=1)

# evaluate the effects due to a randome field error
# change the source type
spectra.Set("src", "type", "User Defined")
# set the unit (longitudinal coordinate in m)
spectra.PreProcess.SetUnit("zpos", "m")
# import the undualtor data with a random error
spectra.PreProcess.Import("Field Profile", "uerror_model.dat")

# evaluate the electron orbit and phase error
if isjupyter:
    from IPython.display import display, HTML
    display(HTML("<h2>Pre-Processing the imported undulator field data"))
spectra.PreProcess.Plot("2nd Integral")
if not isjupyter:
    spectra.PreProcess.DuplicatePlot("Electron Orbit")
spectra.PreProcess.Plot("Phase Error")
if not isjupyter:
    spectra.PreProcess.DuplicatePlot("Phase Error")

# calculation with the error condition
if not isjupyter:
    spectra.ShowMain()
spectra.StartCalculation()

# compare the result
if isjupyter:
    from IPython.display import display, HTML
    display(HTML("<h2>Comparison between the ideal and practical conditions"))
    spectra.ConfigurePlot(yscale="Logarithmic")
    spectra.PostProcessCLI.Plot(data=["sample5-1", "sample5-2"])
else:
    spectra.ShowPostProcessor()
    spectra.PostProcess.SelectData("sample5-1")
    spectra.PostProcess.ComparativePlot("sample5-2")
    spectra.PostProcess.PlotScale(y="log")

if not isjupyter:
    input("Completed. Press enter to exit. ")
spectra.Exit()
                                                  

Pre-Processing the imported undulator field data

                                                  

Comparison between the ideal and practical conditions

In [ ]: