# Export data to files with Suitcase¶

In this notebook you will:

• Export saved data to CSV and TIFF.
• See a preview of how exporting data live during a scan works.

## Configuration¶

Below, we will connect to EPICS IOC(s) controlling simulated hardware in lieu of actual motors, detectors. The IOCs should already be running in the background. Run this command to verify that they are running: it should produce output with RUNNING on each line. In the event of a problem, edit this command to replace status with restart all and run again.

In [ ]:
!supervisorctl -c supervisor/supervisord.conf status

In [ ]:
%run scripts/beamline_configuration.py


## Scalar data to CSV¶

In [ ]:
RE(scan([ph], motor_ph, -1, 1, 5), sample_comp='Ni', barcode='12345')

In [ ]:
import suitcase.csv

In [ ]:
suitcase.csv.export(db[-1].documents(fill=True), 'my_exported_files/')

In [ ]:
suitcase.csv.export(db[-1].documents(fill=True), 'my_exported_files/', file_prefix='{start[sample_comp]}-{start[barcode]}-')

In [ ]:
suitcase.csv.export(db[-1].documents(fill=True), 'my_exported_files/', file_prefix='{start[sample_comp]}/{start[barcode]}-')


## Image data to TIFF¶

In [ ]:
RE(scan([spot], mtr_spotx, -1, 1, 5), sample_comp='Ni', barcode='12345')


### TIFF stack, a 3D block of images in one file¶

In [ ]:
import suitcase.tiff_stack

In [ ]:
suitcase.tiff_stack.export(db[-1].documents(fill=True), 'my_exported_files/', file_prefix='{start[sample_comp]}/{start[barcode]}-')


### TIFF series¶

In [ ]:
import suitcase.tiff_series


Unlike CSV or TIFF stack, this exporter creates separate file per row (Event document). We can include information from a specific Event in the filename. Here, we include the motor position.

In [ ]:
suitcase.tiff_series.export(db[-1].documents(fill=True), 'my_exported_files/', file_prefix='{start[sample_comp]}/{start[barcode]}/{event[data][motor_spotx]}-')


## "Beta" feature: Set up live exporting¶

We plan to streamline this in the future, but here is a demonstration that it is possible.

In [ ]:
# Execute this once to configure RE to send data to an exporter.
# At the beamline, this would normally be done in a script automatically at startup.

from event_model import RunRouter
from databroker.core import discover_handlers

def factory(name, start_doc):

serializer = suitcase.tiff_series.Serializer(
'my_exported_files/', file_prefix='{start[sample_comp]}/{start[barcode]}-{start[uid]:.8}-')

def cb(name, doc):
serializer(name, doc)

return [cb], []

rr = RunRouter([factory], discover_handlers())
RE.subscribe(rr)

In [ ]:
RE(count([spot], num=30, delay=1), sample_comp='Lab6', barcode='12345')

In [ ]: