# Presentation: 108873

## Python Applications for Beamline Control

- Mark Rivers

## Replace IDL applications (e.g. MCA GUI) with Python so that other beamlines don’t need to buy IDL
Send users home with data and display/analysis programs that are free. 
 They don’t want to buy IDL.

- Send users home with data and display/analysis programs that are free.
-  They don’t want to buy IDL.

- Motivation

## Building Blocks

- CaChannel from Geoff Savage for EPICS interface
- Tkinter and Pmw for GUIs
- Pmw.Blt for plots
- Numeric for arrays

## BltPlot: Enhancements to the Pmw.Blt.Graph widget

- A standalone plotting widget, BltPlot.BltPlot. This widget has menus to:
- Configure all of the plot characteristics
- Save and restore the plot settings and data
- Print the plot to a file or printer
- Methods (BltPlot.plot and BltPlot.oplot) to create a new plot, to overplot more data, etc.
- Designed to provide a rough emulation of the command line plotting capabilities of IDL.

## BltPlot

## BltPlot: Enhancements to the Pmw.Blt.Graph widget

- GUI routines to configure all of the plot characteristics, such as axes, markers, legends, etc.
- These routines work with any Pmw.Blt.Graph instance so they can be used from the standalone plotting widget in this package (BltPlot.BltPlot) or from any application that uses the Pmw.Blt.Graph widget
-  Routines to save and restore plot settings and data.
- Used in the mcaDisplay described later.

## BltPlot Dialogs

## epicsPV: Subclasses Geoff Savage's CaChannel class   

- If a PV name is given then the class constructor will do a searchw() by default.
- setMonitor() sets a generic callback routine for value change events.  Subsequent getw(), getValue() or array_get() calls will return the value from the most recent callback, and hence do not result in any network activity or latency.  This can greatly improve performance.
- checkMonitor() returns a flag to indicate if a callback has occurred since the last call to checkMonitor(), getw(), getValue() or array_get().  It can be used to increase efficiency in polling applications.

## epicsPV

- getControl() reads the "control“ and other information from an EPICS PV without having to use callbacks. In addition to the PV value, this will return the graphic, control and alarm limits, etc.
- putWait() calls array_put_callback() and waits for the callback to occur before it returns.  This allows programs to use array_put_callback() synchronously and without user-written callbacks.

## epicsMotor

- Class library for EPICS motor record
- Methods:
- move(), stop(), wait(), get_position(), set_position()
- Virtual attributes:
- slew_speed, base_speed, high_limit, low_limit, done_moving, backlash, resolution, etc.
- Example use:
- from epicsMotor import *
- m = epicsMotor(’13LAB:m5’)
- m.move(10.)
- m.wait()
- m.get_position(dial=1, readback=1)
- 9.9609375

## epicsScaler

- Class library for EPICS scaler record
- Methods:
- start(), stop(), read(), wait(), get_label(), set_label()
- Example use:
- >>> from epicsScaler import *
- >>> s = epicsScaler('13LAB:scaler1')
- >>> s.get_counts()
- >>> s.read()
- [0, 0, 0, 0, 0, 0, 0, 0]
- >>> s.start(1.)
- >>> s.wait()
- >>> s.read()
- [10000000, 0, 0, 0, 0, 0, 0, 0]

## epicsLoggerGUI appplication for logging EPICS PVs to the screen and to a disk file

## mcaDisplay

## mcaDisplay()

- Replacement for my IDL MCA display program
- Much nicer in many respects, since the Blt plot widget has many more east-to-use features than IDL’s direct graphics
- Python object with callable methods, so it can be remotely controlled
- Device independent.  It reads files and controls the “hardware_mca” class.  “hardware_mca” can be subclassed for any hardware.  Presently the EPICS MCA record is supported

## Mca:  Device-independent MCA class

- Support classes:  mcaROI, mcaCalibration, mcaElapsed, mcaPresets, mcaPeak, etc.
- Many methods: add_roi(), fit_background(), fit_peaks(), get_calibration(), set_calibration(), write_file(), read_file(), etc.
- Used as base class of epicsMca.

## epicsMca:Subclass of hardwareMca, which is subclass of Mca

- All methods of Mca, plus start(), stop(), erase(), wait(), etc.
- Re-implements base class routines for set_calibration(), set_rois(), etc. to communicate with fields in the EPICS MCA record
- Example use:
- from epicsMca import *
- mca = epicsMca(’13BMD:aim_adc1’)
- mca.erase()
- mca.start()
- mca.wait()
- mca.write_file(‘test.001’)

## mcaPeakFit

## mcaPeakFit Parameters

## mcaPeakFit Output

## Mpfit

- Generalized non-linear least squares data fitting
- Based on LMFIT from Minpack
- Originally translated to IDL by Craig Markwardt, I translated to Python
- Much faster and more accurate than the version provided in the Scientific Python package in Scientific.Functions.LeastSquares.
- Contraints, fixed parameters, analytic or numerical derivatives, etc.
- Used in mcaPeakFit

## Med: Device-independent multi-element detector class

- Collection of Mca objects.  Methods operate on all contained Mca objects.  Example:
- add_roi(), set_presets(), get_calibration(), etc.
- Used as base class of epicsMed.

## epicsMed

- Subclass of Mca and Med
- All methods of Mca and Med, plus start(), stop(), erase(), wait(), etc.
- Re-implements base class routines for set_calibration(), set_rois(), etc. to communicate with fields in the EPICS MCA record
- Example use:
- from epicsMed import *
- med = epicsMed(’13GE1:med:’, 16)
- med.erase()
- med.start()
- med.wait()
- med.write_file(‘test.001’)

## medDisplay