Skip to content

Instantly share code, notes, and snippets.

@danielballan
Last active September 5, 2018 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielballan/8885c63343b3ad1c5f95706ef9e29a89 to your computer and use it in GitHub Desktop.
Save danielballan/8885c63343b3ad1c5f95706ef9e29a89 to your computer and use it in GitHub Desktop.
toy example for getting sample tracking going at CHX
# Do this part whether you acquiring data or accessing it.
import pymongo
cli = pymongo.MongoClient('xf11id-ca')
samples = cli.get_database('samples').get_collection('samples')
from databroker import Broker
db = Broker.named('temp') # for real applications, 'temp' would be 'chx'
# Do this part when you are entering new samples.
# I'm going to assume that 'name' is unique. We should be more careful about
# ensuring that and maybe come up with a richer system, but this is good enough
# for a toy exmaple.
samples.insert(dict(name='A', composition='Ni'))
samples.insert(dict(name='B', composition='Au'))
# Here I import something things and set up RE to send documents to db, as the
# IPython profile (profile_collection) does, so that data acquisition works.
from bluesky import RunEngine
RE = RunEngine({})
from ophyd.sim import det
from bluesky.plans import count
RE.subscribe(db.insert)
# Take some simulated data.
RE(count([det]), sample='A', purpose='dan is testing for Lutz')
# This is the part you have to add to make data access work.
def fetch_sample(start, stop):
if 'sample' in start:
query = dict(name=start['sample'])
return samples.find_one(query)
# This tells the databroker to addd some custom stuff to the `.ext` attribute
# of each Header.
db.external_fetchers['sample_info'] = fetch_sample
print(db[-1].ext.sample_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment