toPyspMonitor(), simulate a scan, send MCA spectra and counter readings

The following piece of code simulates a scan using toPyspMonitor(). 7.2.3 shows the final plot. The counter readings ar scalars, the MCA data are spectra.

#!/usr/bin/env python3
#
# this script simulates a scan using am MCA and a counter
#
import random, time
import PySpectra, HasyUtils
import numpy as np

def main():

    (status, wasLaunched) = PySpectra.assertPyspMonitorRunning()

    ret = PySpectra.toPyspMonitor( { 'command': ["cls", "delete"]}) 
    if ret[ 'result'].upper() != 'DONE':
        print( "error from pyspMonitor %s" % ret[ 'result'])
        return

    ret = PySpectra.toPyspMonitor( { 'command': ["setTitle \"An important title\"", 
                                                 "setComment \"An interesting comment\""]}) 

    name = "TestScan"
    max = 101
    MAX = 1024
    ret = PySpectra.toPyspMonitor( {'Scan': { 'name': name,
                                     'xMin': 0., 'xMax': 100., 
                                     'yMin': 0., 'yMax': 1.,
                                     'symbol': '+','symbolColor': 'red',
                                     'symbolSize': 7, 'lineColor': 'blue',
                                     'nPts': max,
                                     'autoscaleX': False, 'autoscaleY': True}})

    x = np.linspace(0, 1023, MAX)
    y = np.random.random_sample( (len( x), ))*1000.
    ret = PySpectra.toPyspMonitor( {'Scan': { 'name': "MCA",
                                              'flagMCA': True, 
                                              'lineColor': 'blue', 
                                              'x': x, 
                                              'y': y}}) 
         
    for i in range( max): 
        #
        # send a new MCA spectrum for every scan point
        #
        y = np.random.random_sample( (len( x), ))*1000.
        ret = PySpectra.toPyspMonitor( {'Scan': { 'name': "MCA",
                                                  'flagMCA': True, 
                                                  'lineColor': 'blue', 
                                                  'x': x, 
                                                  'y': y}}) 
        pos = float(i)
        posY = random.random()*10
        PySpectra.toPyspMonitor( { 'command': ['setXY %s %d %s %s' % (name, i, repr(pos), repr(posY))]})
        PySpectra.toPyspMonitor( { 'command': ["display"]}) 
        time.sleep( 0.1)

    print( "Press <space> to continue")
    while 1:
        key = HasyUtils.inkey()
        if key == 32:
            break
        time.sleep( 0.1)
    #
    # if the pyspMonitor was launched by this script, kill it.
    #
    if wasLaunched:
        hshRet = PySpectra.toPyspMonitor( { 'command': [ 'exit']})

    return 

if __name__ == "__main__":
    main()

Figure 7.8: toPyspMonitor(), simulate a scan, send MCA spectra and couner readings
Image examplePysp6