Spice_icon SPICE
Spectrometer Instrument Control Environment


How to write a macro:

A macro is essentially a series of commands which can be stored in a file.  In this guide, we will describe the process of writing and executing macros.

Writing macros:

The simplest macros simply contain a list of commands, for example:

drive m2 40
count preset time 10 repeat 3
drive m2 60
count preset time 10 repeat 3

To save the macro to a file which can be executed from SPICE, the file should be saved in the User Macro directory the location of which can be found on the GUI by selecting the Default Directories tab from the Setup top-level tab.  The User Macro directory location is stored on this tab in the Experiment Macro directory text box.  In addition, the default extension for SPICE macros is .macro and the complete filename does not need to be specified when this extension is used.  To make the process of writing macros simpler, we have included a graphical interface which
can be accessed by selecting the Edit Macro tab from the Macro/Stack top-level tab.  If the desired macro is typed into the top textbox on this tab, the green Write to file button will bring up a file dialog box which has the correct path and extension included.

runfile GUI interface

The example shown in the above macro shows a useful feature of the macro processing in SPICE - the ability to use loops.  The loop syntax in macro is the following:

loop i=start,stop,step
.
.
.
endloop

if step is not included, a step size of 1 is assumed.  In addition, the loop variable can be used in the commands contained in the loop by using the syntax %name.  For example, in the image above, the first command in the loop is drive s2 50+%i where i is the loop variable. From the GUI, loops can be expanded by clicking of the yellow Expand button and the results are printed in the lower text box.  The results of this expansion are shown in the above image and the various drives corresponding to different i values can be clearly seen.  There are no limitations in this loop structure and embedded loops are also allowed.

Another feature which can be used in writing macros is the ability to pass on command line arguments.  As an example of this, consider the following macro:

drive s2 40+%1
count preset time %3
drive s2 50+%2
count preset time %3

If this macro is then saved (let's assume the filename is testvariable.macro) then the command run testvariable 10 -5 100 will execute the following set of commands:

drive s2 40+10
count preset time 100
drive s2 50-5
count preset time100

This structure is useful when there is some set of commands which need to be repeated regularly but with slightly different parameters.
NOTE: there is no way of passing command line variables to a macro using the included graphical user interface.  Therefor the run command will need to be used.



Executing Macros:


Macros can be executed using either the graphical user interface or the run command.  

1. From the graphical user interface, as shown in the image above, the macro can be executed directly by hitting the blue Execute Macro button.  As mentioned above, command line arguments cannot be passed to a macro using this method.  

2. The second method of executing a macro is with the run command.  Detailed information about this command can be found in the run help document.