<jupyter_start><jupyter_text>Run Sirepo/SRW simulations with BlueskyThis section is based on the **Young's Double Slit Experiment** Sirepo examplethat can be found in the wavefront propagation folder on the SRW simulationssection.**Hint**: See the [List of predefined simulations in Sirepo](../simulations.rst) for examples of identifiers for different beamlines.  Run "Source" SRW simulationsIn this example, we run a simulation using the "Single-Electron Spectrum" report on the "Source" page of Sirepo/SRW app.<jupyter_code>%run -i ../../../examples/prepare_det_env.py

import sirepo_bluesky.srw_detector as sd
import bluesky.plans as bp

srw_det = sd.SirepoSRWDetector(sim_id="00000000", sim_type="srw",
                               source_simulation=True,
                               sirepo_server="http://localhost:8000",
                               root_dir="/tmp/sirepo-bluesky-data")

srw_det.configuration_attrs = ["photon_energy", "shape"]

uid, = RE(bp.count([srw_det]))<jupyter_output><empty_output><jupyter_text>We can observe the produced spectrum as:<jupyter_code>hdr = db[uid]
cfg = hdr.config_data(srw_det.name)["primary"][0]
energies = cfg[f"{srw_det.name}_photon_energy"]
spectrum, = hdr.data(f"{srw_det.name}_image")
plt.plot(energies, spectrum)<jupyter_output><empty_output><jupyter_text>Run "Beamline" SRW simulations sequentiallyIn this example, we scan the horizontal size of the aperture and collect the resulting averaged intensities and corresponding 2D intensity distributions from the "Watchpoint" report on the "Beamline" page of Sirepo/SRW app. Start ``ipython`` and run the code in the following cell. Note that ``sim_id`` may be replaced by the identifier for any supported Sirepo simulation.<jupyter_code>%run -i ../../../examples/prepare_det_env.py

import sirepo_bluesky.srw_detector as sd
import bluesky.plans as bp

srw_det = sd.SirepoSRWDetector(sim_id="00000000", sim_type="srw",
                               sirepo_server="http://localhost:8000",
                               root_dir="/tmp/sirepo-bluesky-data")

srw_det.select_optic("Aperture")
param1 = srw_det.create_parameter("horizontalSize")
param2 = srw_det.create_parameter("verticalSize")
srw_det.configuration_attrs = ["horizontal_extent", "vertical_extent", "shape"]
uid, = RE(bp.scan([srw_det], param1, 0.0, 0.25, 11))<jupyter_output><empty_output><jupyter_text>We can access the data as the last item in the database (`db[-1]`) or directly via the returned `uid`, which when plotted as an image will look like:<jupyter_code>hdr = db[uid]
hdr.table()
imgs = list(hdr.data(f"{srw_det.name}_image"))
cfg = hdr.config_data(srw_det.name)["primary"][0]
hor_ext = cfg[f"{srw_det.name}_horizontal_extent"]
vert_ext = cfg[f"{srw_det.name}_vertical_extent"]
plt.imshow(imgs[-2], aspect="equal", extent=(*hor_ext, *vert_ext))<jupyter_output><empty_output><jupyter_text>Run "Beamline" SRW simulations in parallelIn this example, we are using [Bluesky's fly scans](https://blueskyproject.io/bluesky/async.htmlflying).<jupyter_code>%run -i ../../../examples/prepare_flyer_env.py

import bluesky.plans as bp
import sirepo_bluesky.sirepo_flyer as sf<jupyter_output><empty_output><jupyter_text>We can create 3 different simulations that change 4 parameters at a time:<jupyter_code>params_to_change = []
key1 = "Aperture"
key2 = "Lens"
key3 = "Obstacle"
for i in range(1, 3 + 1):
    parameters_update1 = {"horizontalSize": i * 0.1, "verticalSize": (16 - i) * 0.1}
    parameters_update2 = {"horizontalFocalLength": i + 7}
    parameters_update3 = {"horizontalSize": 6 - i}
    params_to_change.append({key1: parameters_update1,
                             key2: parameters_update2,
                             key3: parameters_update3})<jupyter_output><empty_output><jupyter_text>To create the flyer and run a fly scan, where ``sim_id`` is the UID of this simulation, we run:<jupyter_code>sirepo_flyer = sf.SirepoFlyer(sim_id="00000000",
                              server_name="http://localhost:8000",
                              root_dir="/tmp/sirepo-bluesky-data",
                              params_to_change=params_to_change,
                              watch_name="W60")

uid, = RE(bp.fly([sirepo_flyer]))<jupyter_output><empty_output><jupyter_text>We access the data in the same manner as before:<jupyter_code>hdr = db[uid]
hdr.table(stream_name="sirepo_flyer")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11748695849398956, 'fraction_numerical': 0.01769108641415287, 'mean_word_length': 4.212765957446808, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '318414', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1449, 'n_words': 337, 'path': 'docs/source/notebooks/srw.ipynb', 'repo_name': 'mrakitin/sirepo-bluesky', 'size': '4409'}