A Hooked Scan

Hooks allows users to inject extra code at certain positions of the scanning procedure. The following example shows how this can be done using Macro classes.

#!/usr/bin/env python
# file name: /home/pXXuser/sardanaMacros/hooked_scan_class.py 

"""the demo for a hooked scan"""

from __future__ import print_function

__all__ = ["hooked_scan_class"]

import PyTango
from sardana.macroserver.macro import *
from sardana.macroserver.macro import macro

class hooked_scan_class(Macro):
    """An example on how to attach hooks to the various hook points of a scan."""
    
    param_def = [
       ['motor',      Type.Motor,   None, 'Motor to move'],
       ['start_pos',  Type.Float,   None, 'Scan start position'],
       ['final_pos',  Type.Float,   None, 'Scan final position'],
       ['nr_interv',  Type.Integer, None, 'Number of scan intervals'],
       ['integ_time', Type.Float,   None, 'Integration time']
    ]
    def hook_pre_scan(self):
        self.info("\t pre-scan hook")
    
    def hook_pre_acq(self):
        self.info("\t pre-acq hook")
    
    def hook_post_acq(self):
        self.info("\t post-acq hook")
    
    def hook_pre_move(self):
        self.info("\t pre-move hook")
    
    def hook_post_move(self):
        self.info("\t post-move hook")
    
    def hook_post_scan(self):
        self.info("\t post-scan hook")
    
    def run(self, motor, start_pos, final_pos, nr_interv, integ_time):
        ascan, pars = self.createMacro( "ascan", motor, start_pos, final_pos, nr_interv, integ_time)

        ascan.pre_scan_hooks = [self.hook_pre_scan]
        ascan.post_scan_hooks = [self.hook_post_scan]

        ascan.hooks = [ 
            (self.hook_pre_acq, ["pre-acq"]),
            (self.hook_post_acq, ["post-acq"]),
            (self.hook_pre_move, ["pre-move"]),
            (self.hook_post_move, ["post-move"]),
            ]
        self.runMacro(ascan)



2019-11-13