Macro call examples

This chapter consists of a series of examples demonstrating how to call macros from inside a macro

  1##############################################################################
  2##
  3# This file is part of Sardana
  4##
  5# http://www.sardana-controls.org/
  6##
  7# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
  8##
  9# Sardana is free software: you can redistribute it and/or modify
 10# it under the terms of the GNU Lesser General Public License as published by
 11# the Free Software Foundation, either version 3 of the License, or
 12# (at your option) any later version.
 13##
 14# Sardana is distributed in the hope that it will be useful,
 15# but WITHOUT ANY WARRANTY; without even the implied warranty of
 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17# GNU Lesser General Public License for more details.
 18##
 19# You should have received a copy of the GNU Lesser General Public License
 20# along with Sardana.  If not, see <http://www.gnu.org/licenses/>.
 21##
 22##############################################################################
 23
 24"""
 25A macro package to show examples on how to run a macro from inside another macro
 26"""
 27
 28__all__ = ["call_wa", "call_wm", "subsubm", "subm", "mainmacro", "runsubs"]
 29
 30__docformat__ = 'restructuredtext'
 31
 32from sardana.macroserver.macro import Macro, Type
 33
 34#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
 35# First example:
 36# A 'mainmacro' that executes a 'subm' that in turn executes a 'subsubm'.
 37# The 'subsubm' macro itself calls a short ascan macro
 38#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~--~-~-
 39
 40
 41class call_wa(Macro):
 42
 43    def run(self):
 44        self.macros.wa()
 45
 46
 47class call_wm(Macro):
 48
 49    param_def = [
 50        ['motor_list', [['motor', Type.Motor, None, 'Motor to move']],
 51         None, 'List of motor to show'],
 52    ]
 53
 54    def run(self, m):
 55        self.macros.wm(m)
 56
 57
 58class subsubm(Macro):
 59    """this macro just calls the 'subm' macro
 60    This macro is part of the examples package. It was written for demonstration purposes"""
 61
 62    def run(self):
 63        self.output("Starting %s" % self.getName())
 64        m = self.macros
 65        motors = self.getObjs('.*', type_class=Type.Motor)
 66        m.ascan(motors[0], 0, 100, 10, 0.2)
 67        self.output("Finished %s" % self.getName())
 68
 69
 70class subm(Macro):
 71    """this macro just calls the 'subsubm' macro
 72    This macro is part of the examples package. It was written for demonstration purposes"""
 73
 74    def run(self):
 75        self.output("Starting %s" % self.getName())
 76        self.macros.subsubm()
 77        self.output("Finished %s" % self.getName())
 78
 79
 80class mainmacro(Macro):
 81    """this macro just calls the 'subm' macro
 82    This macro is part of the examples package. It was written for demonstration purposes"""
 83
 84    def run(self):
 85        self.output("Starting %s" % self.getName())
 86        self.macros.subm()
 87        self.output("Finished %s" % self.getName())
 88
 89#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
 90# Second example:
 91# a 'runsubs' macro that shows the different ways to call a macro from inside
 92# another macro
 93#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~--~-~-
 94
 95
 96class runsubs(Macro):
 97    """ A macro that calls a ascan macro using the motor given as first parameter.
 98
 99    This macro is part of the examples package. It was written for demonstration purposes
100
101    Call type will allow to choose to format in which the ascan macro is called
102    from this macro:
103    1 - m.ascan(motor.getName(), '0', '10', '4', '0.2')
104    2 - m.ascan(motor, 0, 10, 4, 0.2)
105    3 - self.execMacro('ascan', motor.getName(), '0', '10', '4', '0.2')
106    4 - self.execMacro(['ascan', motor, 0, 10, 4, 0.2])
107    5 - params = 'ascan', motor, 0, 10, 4, 0.2
108        self.execMacro(params)
109    6 - self.execMacro("ascan %s 0 10 4 0.2" % motor.getName())
110    7 - macro, prep = self.createMacro("ascan %s 0 10 4 0.2" % motor.getName())
111        macro.hooks = [ self.hook ]
112        self.runMacro(macro)
113    8 - macro, prep = self.createMacro('ascan', motor, 0, 10, 4, 0.2)
114        macro.hooks = [ self.hook ]
115        self.runMacro(macro)
116    9 - params = 'ascan', motor, 0, 10, 4, 0.2
117        macro, prep = self.createMacro(params)
118        macro.hooks = [ self.hook ]
119        self.runMacro(macro)
120
121        Options 7,8 and 9 use the lower level macro API in order to be able to
122        attach hooks to the ascan macro."""
123    param_def = [
124        ['motor',      Type.Motor,   None, 'Motor to move'],
125        ['call_type',  Type.Integer, 2, 'type of run to execute internally'],
126    ]
127
128    def hook(self):
129        self.info("executing hook in a step of a scan...")
130
131    def run(self, motor, call_type):
132        m = self.macros
133        self.output("Using type %d" % call_type)
134        if call_type == 1:
135            m.ascan(motor.getName(), '0', '10', '4', '0.2')
136        elif call_type == 2:
137            m.ascan(motor, 0, 10, 4, 0.2)
138        elif call_type == 3:
139            self.execMacro('ascan', motor.getName(), '0', '10', '4', '0.2')
140        elif call_type == 4:
141            self.execMacro('ascan', motor, 0, 10, 4, 0.2)
142        elif call_type == 5:
143            params = 'ascan', motor, 0, 10, 4, 0.2
144            self.execMacro(params)
145        elif call_type == 6:
146            self.execMacro("ascan %s 0 10 4 0.2" % motor.getName())
147        elif call_type == 7:
148            macro, prep = self.createMacro("ascan %s 0 10 4 0.2" %
149                                           motor.getName())
150            macro.hooks = [self.hook]
151            self.runMacro(macro)
152        elif call_type == 8:
153            macro, prep = self.createMacro('ascan', motor, 0, 10, 4, 0.2)
154            macro.hooks = [self.hook]
155            self.runMacro(macro)
156        elif call_type == 9:
157            params = 'ascan', motor, 0, 10, 4, 0.2
158            macro, prep = self.createMacro(params)
159            macro.hooks = [self.hook]
160            self.runMacro(macro)
161
162
163class get_data(Macro):
164    """A macro that executes another macro from within it, get its data,
165    and calculates a result using this data.
166
167    This macro is part of the examples package. It was written for
168    demonstration purposes"""
169
170    param_def = [["mot", Type.Moveable, None, "moveable to be moved"]]
171    result_def = [["middle", Type.Float, None,
172                   "the middle motor position"]]
173
174    def run(self, mot):
175        start = 0
176        end = 2
177        intervals = 2
178        integtime = 0.1
179        positions = []
180        dscan, _ = self.createMacro('dscan',
181                                    mot, start, end, intervals, integtime)
182        self.runMacro(dscan)
183
184        data = dscan.data
185        len_data = len(data)
186        for point_nb in range(len_data):
187            position = data[point_nb].data[mot.getName()]
188            positions.append(position)
189
190        middle_pos = max(positions) - min(positions) / len_data
191        return middle_pos