Skip to content

Instantly share code, notes, and snippets.

@chiahaoliu
Forked from CJ-Wright/customized_per_step.py
Last active July 20, 2018 19:58
Show Gist options
  • Save chiahaoliu/f567e4c23a15b816ab490ee136dcd57f to your computer and use it in GitHub Desktop.
Save chiahaoliu/f567e4c23a15b816ab490ee136dcd57f to your computer and use it in GitHub Desktop.
customized per_step to collect light and dark frames at each motor point
# ACQ SIDE
# ======================================================
import bluesky.plans as bp
import bluesky.preprocessors as bpp
import bluesky.plan_stubs as bps
from bluesky.utils import Msg, short_uid as _short_uid
from bluesky.preprocessors import subs_wrapper
from bluesky.simulators import summarize_plan
from bluesky.callbacks import LiveTable
from xpdacq.xpdacq_conf import xpd_configuration
from xpdacq.xpdacq import glbl, CustomizedRunEngine
from xpdacq.beamtime import _configure_area_det
save_kwargs = {}
shutter = xpd_configuration['shutter']
sh_open = glbl['shutter_conf']['open']
sh_close = glbl['shutter_conf']['close']
sample_motor = spinner_goniohead.X
# experimental info
Tstart, Tstop, numT = 50, 500, 30
det_z_pos1, det_z_pos2 = 1227, 1727
# special dict for cryostat experiment
sample1_name, sample1_pos, sample1_expo, sample1_wait = 'Cu2S_disordered', -4.0, 20, 2 # CHANGE as needed!!!!!
sample2_name, sample2_pos, sample2_expo, sample2_wait = 'Ni', -6.0, 5, 10 # CHANGE as needed!!!!!
sample3_name, sample3_pos, sample3_expo, sample3_wait = 'Cu2S_ordered', -8.0, 20, 2 # CHANGE as needed!!!!!
sub_sample_dict = {
sample1_pos: {
'name': sample1_name,
'exposure': sample1_expo,
'wait': sample1_wait},
sample2_pos: {
'name': sample2_name,
'exposure': sample2_expo,
'wait': sample2_wait},
sample3_pos: {'name': sample3_name,
'exposure': sample3_expo,
'wait': sample3_wait}
}
# this is tailored for xpdAn at this stage. Will be changed in the future
cryostat_sample_dict = {'sub_samples': sub_sample_dict}
# heating stage of cryostat
heater_dict = {(4, 30): 1, (30, 80): 2, (80, 600): 3}
# vendor blusky nd_per_step
def light_dark_nd_step(detectors, step, pos_cache):
"""
Inner loop of an N-dimensional step scan
This is the default function for ``per_step`` param`` in ND plans.
Parameters
----------
detectors : iterable
devices to read
step : dict
mapping motors to positions in this step
pos_cache : dict
mapping motors to their last-set positions
"""
def move():
yield Msg('checkpoint')
grp = _short_uid('set')
for motor, pos in step.items():
if pos == pos_cache[motor]:
# This step does not move this motor.
continue
#if motor == cryostat1:
# for (low, hi), heater_pos in heater_dict.items():
# if low < pos <= hi:
# yield Msg('set', cryostat1.heater, heater_pos, group=grp)
yield Msg('set', motor, pos, group=grp)
pos_cache[motor] = pos
yield Msg('wait', None, group=grp)
motors = step.keys()
yield from bps.abs_set(shutter, sh_close, wait=True)
yield from move()
yield from bps.trigger_and_read(list(detectors) + list(motors) +
[shutter], name='dark')
yield from bps.abs_set(shutter, sh_open, wait=True)
yield from bps.trigger_and_read(list(detectors) + list(motors) +
[shutter])
yield from bps.abs_set(shutter, sh_close, wait=True)
inner_dict = sub_sample_dict.get(sample_motor.get().user_readback,
{'exposure': .1, 'wait': 1})
_configure_area_det(inner_dict['exposure'])
yield from bps.sleep(inner_dict['wait'])
plan = bp.grid_scan([pe1c, shutter, Det_1_Z, cryostat1, sample_motor],
cryostat1, Tstart, Tstop, numT,
Det_1_Z, det_z_pos1, det_z_pos2, 2, False,
sample_motor, sample1_pos, sample3_pos, 3, False,
per_step=light_dark_nd_step)
dummy_plan = bp.grid_scan([pe1c, shutter, Det_1_Z, cryostat1, sample_motor],
cryostat1, Tstart, Tstop, numT,
Det_1_Z, det_z_pos1, det_z_pos2, 2, False,
sample_motor, sample1_pos, sample3_pos, 3, False,
per_step=light_dark_nd_step)
plan = bpp.subs_wrapper(plan, LiveTable([pe1c, shutter, Det_1_Z, cryostat1, sample_motor]))
# COPY this to your terminal, DO NOT run in script
"""
# prview the plan
summarize_plan(dummy_plan)
# execute the plan
xrun(cryostat_sample_dict, plan)
"""
# AN SIDE
# =================================================================================
"""
save_kwargs['string'] = (''
'{base_folder}/{folder_prefix}/'
'{analysis_stage}/'
'{human_timestamp}_'
'[temp_{raw_event[data][cryostat_T]:1.2f}'
'{raw_descriptor[data_keys][cryostat_T][units]}]_'
'[dx_{raw_event[data][diff_x]:1.3f}'
'{raw_descriptor[data_keys][diff_x][units]}]_'
'[dy_{raw_event[data][diff_y]:1.3f}'
'{raw_descriptor[data_keys][diff_y][units]}]_'
'[z_{raw_event[data][Det_1_Z]:1.3f}'
'{raw_descriptor[data_keys][Det_1_Z][units]}]_'
'[gx_{raw_event[data][spinner_goniohead_X]:1.3f}'
'{raw_descriptor[data_keys][spinner_goniohead_X][units]}]_'
'{raw_start[uid]:.6}_'
'{raw_event[seq_num]:04d}{ext}')
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment