bm01macros
Python macros for the BM01 beamline:
operator helpers for switching the monochromator between saved states,
the LaB6 test grid, and mesh scans for operando battery mapping,
one cell or a whole holder of them.
The macros are imported from the
pylatus Python scripting environment, where the auxygen modules
(auxygen_log, auxygen_motors, auxygen_icat,
pylatus_diffractometer) are available.
Requirements
- Python 3.8 or newer
- The pylatus scripting environment (auxygen-based)
Install
Use an editable install so that a git pull is live immediately, without any
reinstall, and so the checkout stays outside the Python installation:
git clone <this repository> <stable path>
python3 -m pip install -e <stable path>
Add --user when not running as root; on PEP 668 (externally managed)
systems also pass --break-system-packages. After a Python version upgrade,
re-run the pip install -e line with the new interpreter; the checkout
itself is untouched. Do not clone the repository into site-packages: the
repo root is build metadata, the package lives in bm01macros/.
Usage
Switch the monochromator to a saved state and set the detector energy to match:
from bm01macros import move_mono, PHE
move_mono(PHE)
The argument is one of the eight state constants exported by the package:
PHE, FHE, PLE, FLE and their harmonic-rejection variants PHEHR,
FHEHR, PLEHR, FLEHR. move_mono reads the state file for that
constant, moves every motor named in it (in parallel), sets the PILATUS
detector energy from the file's energy line, and waits for all motors to
settle.
Acquire the lab6 test-pattern grid (distances 0-500 mm, vertical positions -80 and 0 mm, serpentine order, omega 0-100 degrees, 10 s, 1 frame):
from bm01macros import lab6
lab6(folder='lab6')
Images are saved as lab6_<distance:03d>_<height:02d> in the given folder.
Mesh scans
Raster a battery cell for operando mapping, naming every point after the BM01
mesh contract, <cell>_<series>_<slow>_<fast>.<ext>, all counters five digits
wide and starting at zero:
from bm01macros import Mesh
m = Mesh(cell='IFE-135',
slow=('prhor', -1.0, 0.1, 11),
fast=('battz', -0.5, 0.1, 11),
repeats=200,
exposure=1.0,
extension='xye',
integrated_dir='xye')
for series in m.scan():
for row in m.rows():
row.move()
for point in row.points():
point.move()
point.measure()
The slow axis is the outer one of the raster, the fast axis the inner one; each
is (motor, begin, step, count), positions in millimetres. repeats is how
many times the raster is replayed while the cell charges and discharges,
exposure is seconds per point. A zero step is refused even for a single
point: the axis would collapse into a line of zero width on the map.
measure() passes nop=1, nperiods=1, nframes=1, dOmega=0 on its own. Without
nop pylatus appends the period suffix to every name and the contract breaks;
without dOmega the sample rotates during the point. Nothing is moved with
now=True: the motor moves and the scans go into the same pylatus queue, so
they keep their order.
Before the first point the constructor writes the mesh.json marker into the
data folder. That marker is the only thing medved recognises a mesh by, and it
describes the geometry: the cell name, the three counters, both axes with their
motor names and steps, the file extension and the integrated subdirectory
(integrated_dir='' when bubble writes the integrated patterns next to the
frames, 'xye' when it writes them into a subdirectory). A folder without the
marker is opened by medved as an ordinary series of patterns.
The remaining parameters are folder (defaults to the cell name, whitespace
squeezed into underscores the way pylatus does it), unit (default mm),
omega (taken from the pylatus window when left out), wavelength (read from
the wavelength pseudomotor when left out), base (a data directory for
offline runs instead of the one from ICAT), resume, and dry_run=True, which
prints the moves and the names instead of measuring anything.
The cell name may contain underscores but no spaces or slashes, and its length is checked against the camserver limit of 48 characters. The counters take 18 of them, so 30 are left for the cell; above the limit the camserver truncates the name silently and every point of the mesh overwrites the previous one.
Several cells in one holder
A holder with several cells is measured cell by cell: one raster of the first
cell, one of the second, and so on, then round again. Every cell is a mesh of
its own, with its own folder, marker and geometry -- the holder is asymmetric,
so both axes differ from cell to cell -- and rounds() plays them in turn:
from bm01macros import Mesh, rounds
cells = [Mesh(cell=name, slow=('prhor', x, 0.1, nx), fast=('battz', z, 0.1, nz),
repeats=40, exposure=1.0, extension='xye', integrated_dir='xye')
for name, x, nx, z, nz in (('IFE-135', -1.00, 21, -0.50, 11),
('IFE-136', 4.20, 17, -0.35, 13),
('IFE-137', 9.05, 21, -0.50, 11))]
for m in rounds(*cells):
for row in m.rows():
row.move()
for point in row.points():
point.move()
point.measure()
The body of the loop is the same as for a single cell; rounds() only replaces
the loop over the series, handing out the cell whose turn it is with its series
already set. rounds(m) on one mesh does what m.scan() does. Every cell keeps
its own repeats, and a cell that has played all of its rasters drops out while
the others go on. The rasters need not be the same size, so the rounds differ in
length.
Two meshes of one holder may not share a folder, or they would overwrite each other point by point; a repeated cell name is refused before the first move.
Nothing else changes per cell: bubble walks the data directory recursively, so a single watch covers all the folders, and medved opens each of them as an ordinary mesh.
Restarting into the same folder
A mesh interrupted by a broken cable is continued with resume=True:
m = Mesh(cell='IFE-135', ..., resume=True)
The declared geometry is compared with the marker already on disk. If they
agree, the scan continues from the first free series, max(series) + 1 over the
raw cbf files, because the integrated patterns lag behind; the marker is not
rewritten, and the series cut short by the restart stays a hole, which medved
handles. If the geometry differs, or the marker cannot be read, the constructor
refuses with a line-by-line diff and asks for a new folder: the old points would
be laid out by the new geometry and every map would silently lie. A changed
exposure is a warning rather than a refusal, since only the summed maps step
between series.
An existing marker without resume=True is an error too, so rerunning the
script cannot quietly append to a mesh somebody else has started.
In a holder the cells are restarted together, and each of them finds its own
first free series: a cell interrupted in the middle of its raster is one round
ahead of a cell the restart never reached. rounds() lifts them all to the
highest of those series, so that a series index keeps meaning the same round in
every cell; the series skipped by the cells that were behind stay holes.
After each restart, retromesh check <folder> from the medved tools compares
the disk with the marker and reports the holes.
State files
Each state is a plain-text file in bm01macros/states/ (phe.dat,
fhe.dat, fle.dat, fhehr.dat, flehr.dat): a required # energy
directive, then one line per motor:
# energy <eV>
<motor> <position>
The # energy directive is the PILATUS detector energy for that state; a
state file without it is an error, not a silent default. The directive is
hash-prefixed on purpose: the beamline has a motor named energy, and every
bare line in the file is a <motor> <position> move. Any other # line is
ignored as a comment. A motor section is a full snapshot, not a delta:
move_mono moves every motor it lists and leaves all others where they are. The files live in the repository on purpose: edit
them here, commit, and git pull on the beamline machine; they are the
single source of truth. The repository currently carries five of the eight
referenced states; for ple, phehr and plehr there is no state file,
and move_mono raises RuntimeError for a missing state file, a file
without an energy line, or an unknown state argument.
Tests
Everything in mesh.py that touches neither the motors nor the detector, that
is the names, the marker, the geometry diff and the resume arithmetic, is
covered by tests on the stdlib unittest, no pytest needed:
python3 -m unittest discover -s tests
Layout
bm01macros/- the importable package (move_monoinmono.py,lab6inlab6.py,Meshandroundsinmesh.py)tests/- offline tests for the mesh helperpyproject.toml- package metadata, setuptools backend, no dependencies