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 def run(self):
43 self.macros.wa()
44
45
46class call_wm(Macro):
47 param_def = [
48 [
49 "motor_list",
50 [["motor", Type.Motor, None, "Motor to move"]],
51 None,
52 "List of motor to show",
53 ],
54 ]
55
56 def run(self, m):
57 self.macros.wm(m)
58
59
60class subsubm(Macro):
61 """this macro just calls the 'subm' macro
62 This macro is part of the examples package. It was written for demonstration purposes"""
63
64 def run(self):
65 self.output("Starting %s" % self.getName())
66 m = self.macros
67 motors = self.getObjs(".*", type_class=Type.Motor)
68 m.ascan(motors[0], 0, 100, 10, 0.2)
69 self.output("Finished %s" % self.getName())
70
71
72class subm(Macro):
73 """this macro just calls the 'subsubm' macro
74 This macro is part of the examples package. It was written for demonstration purposes"""
75
76 def run(self):
77 self.output("Starting %s" % self.getName())
78 self.macros.subsubm()
79 self.output("Finished %s" % self.getName())
80
81
82class mainmacro(Macro):
83 """this macro just calls the 'subm' macro
84 This macro is part of the examples package. It was written for demonstration purposes"""
85
86 def run(self):
87 self.output("Starting %s" % self.getName())
88 self.macros.subm()
89 self.output("Finished %s" % self.getName())
90
91
92# -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
93# Second example:
94# a 'runsubs' macro that shows the different ways to call a macro from inside
95# another macro
96# -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~--~-~-
97
98
99class runsubs(Macro):
100 """A macro that calls a ascan macro using the motor given as first parameter.
101
102 This macro is part of the examples package. It was written for demonstration purposes
103
104 Call type will allow to choose to format in which the ascan macro is called
105 from this macro:
106 1 - m.ascan(motor.getName(), '0', '10', '4', '0.2')
107 2 - m.ascan(motor, 0, 10, 4, 0.2)
108 3 - self.execMacro('ascan', motor.getName(), '0', '10', '4', '0.2')
109 4 - self.execMacro(['ascan', motor, 0, 10, 4, 0.2])
110 5 - params = 'ascan', motor, 0, 10, 4, 0.2
111 self.execMacro(params)
112 6 - self.execMacro("ascan %s 0 10 4 0.2" % motor.getName())
113 7 - macro, prep = self.createMacro("ascan %s 0 10 4 0.2" % motor.getName())
114 macro.hooks = [ self.hook ]
115 self.runMacro(macro)
116 8 - macro, prep = self.createMacro('ascan', motor, 0, 10, 4, 0.2)
117 macro.hooks = [ self.hook ]
118 self.runMacro(macro)
119 9 - params = 'ascan', motor, 0, 10, 4, 0.2
120 macro, prep = self.createMacro(params)
121 macro.hooks = [ self.hook ]
122 self.runMacro(macro)
123
124 Options 7,8 and 9 use the lower level macro API in order to be able to
125 attach hooks to the ascan macro."""
126
127 param_def = [
128 ["motor", Type.Motor, None, "Motor to move"],
129 ["call_type", Type.Integer, 2, "type of run to execute internally"],
130 ]
131
132 def hook(self):
133 self.info("executing hook in a step of a scan...")
134
135 def run(self, motor, call_type):
136 m = self.macros
137 self.output("Using type %d" % call_type)
138 if call_type == 1:
139 m.ascan(motor.getName(), "0", "10", "4", "0.2")
140 elif call_type == 2:
141 m.ascan(motor, 0, 10, 4, 0.2)
142 elif call_type == 3:
143 self.execMacro("ascan", motor.getName(), "0", "10", "4", "0.2")
144 elif call_type == 4:
145 self.execMacro("ascan", motor, 0, 10, 4, 0.2)
146 elif call_type == 5:
147 params = "ascan", motor, 0, 10, 4, 0.2
148 self.execMacro(params)
149 elif call_type == 6:
150 self.execMacro("ascan %s 0 10 4 0.2" % motor.getName())
151 elif call_type == 7:
152 macro, prep = self.createMacro("ascan %s 0 10 4 0.2" % motor.getName())
153 macro.hooks = [self.hook]
154 self.runMacro(macro)
155 elif call_type == 8:
156 macro, prep = self.createMacro("ascan", motor, 0, 10, 4, 0.2)
157 macro.hooks = [self.hook]
158 self.runMacro(macro)
159 elif call_type == 9:
160 params = "ascan", motor, 0, 10, 4, 0.2
161 macro, prep = self.createMacro(params)
162 macro.hooks = [self.hook]
163 self.runMacro(macro)
164
165
166class get_data(Macro):
167 """A macro that executes another macro from within it, get its data,
168 and calculates a result using this data.
169
170 This macro is part of the examples package. It was written for
171 demonstration purposes"""
172
173 param_def = [["mot", Type.Moveable, None, "moveable to be moved"]]
174 result_def = [["middle", Type.Float, None, "the middle motor position"]]
175
176 def run(self, mot):
177 start = 0
178 end = 2
179 intervals = 2
180 integtime = 0.1
181 positions = []
182 dscan, _ = self.createMacro("dscan", mot, start, end, intervals, integtime)
183 self.runMacro(dscan)
184
185 data = dscan.data
186 len_data = len(data)
187 for point_nb in range(len_data):
188 position = data[point_nb].data[mot.getName()]
189 positions.append(position)
190
191 middle_pos = max(positions) - min(positions) / len_data
192 return middle_pos