from time import sleep
from bluesky.callbacks.broker import LiveTiffExporter
from databroker import process
from bluesky import Msg
from bluesky.plans import DeltaScanPlan, DeltaListScanPlan


def take_dark():
    print('closing shutter...')
    shctl1.put(0)  # close shutter
    sleep(2)
    print('taking dark frame....')
    uid, = RE(Count([pe1c]))
    print('opening shutter...')
    shctl1.put(1)
    sleep(2)
    return uid


def run(motor, x, start, stop, num_steps, loops, *, exposure=1,  **metadata):
    print('moving %s to initial position' % motor.name)
    subs = [LiveTable(['pe1_stats1_total', motor.name]),
            LivePlot('pe1_stats1_total', motor.name)]
    motor.move(x)
    pe1c.images_per_set.put(exposure // 0.1)
    dark_uid = take_dark()
    steps = loops * list(np.linspace(start, stop, num=num_steps, endpoint=True))
    plan = DeltaListScanPlan([pe1c], motor, steps)
    uid = RE(plan, subs, dark_frame=dark_uid, **metadata)
    sleep(3)
    process(db[uid], exporter)


class SubtractedTiffExporter(LiveTiffExporter):
    "Intercept images before saving and subtract dark image"

    def start(self, doc):
        # The metadata refers to the scan uid of the dark scan.
        if 'dark_frame' not in doc:
            raise ValueError("No dark_frame was recorded.")
        uid = doc['dark_frame']
        dark_header = db[uid]
        self.dark_img, = get_images(db[uid], 'pe1_image')
        super().start(doc)

    def event(self, doc):
        img = doc['data'][self.field]
        subtracted_img = img - self.dark_img
        doc['data'][self.field] = subtracted_img
        super().event(doc)

template = "/home/xf28id1/xpdUser/tiff_base/LaB6_EPICS/{start.sa_name}_{start.scan_id}_step{event.seq_num}.tif"
exporter = SubtractedTiffExporter('pe1_image', template)

@@ -1,27 +1,4 @@
-from time import sleep%0A
 from
@@ -51,934 +51,8 @@
 ter%0A
-from databroker import process%0Afrom bluesky import Msg%0Afrom bluesky.plans import DeltaScanPlan, DeltaListScanPlan%0A%0A%0Adef take_dark():%0A    print('closing shutter...')%0A    shctl1.put(0)  # close shutter%0A    sleep(2)%0A    print('taking dark frame....')%0A    uid, = RE(Count(%5Bpe1c%5D))%0A    print('opening shutter...')%0A    shctl1.put(1)%0A    sleep(2)%0A    return uid%0A%0A%0Adef run(motor, x, start, stop, num_steps, loops, *, exposure=1,  **metadata):%0A    print('moving %25s to initial position' %25 motor.name)%0A    subs = %5BLiveTable(%5B'pe1_stats1_total', motor.name%5D),%0A            LivePlot('pe1_stats1_total', motor.name)%5D%0A    motor.move(x)%0A    pe1c.images_per_set.put(exposure // 0.1)%0A    dark_uid = take_dark()%0A    steps = loops * list(np.linspace(start, stop, num=num_steps, endpoint=True))%0A    plan = DeltaListScanPlan(%5Bpe1c%5D, motor, steps)%0A    uid = RE(plan, subs, dark_frame=dark_uid, **metadata)%0A    sleep(3)%0A    process(db%5Buid%5D, exporter)%0A
 %0A%0Acl

remove function that (I think) are no longer used