Skip to main content

Come analyze HEASARC, IRSA, and MAST data in the cloud! The Fornax Initiative is now welcoming all interested beta users.

Xspec Home Page

Writing Custom XSPEC commands

XSPEC commands can be written by users as tcl procedures, which have similarities with fortran or C/C++ subroutines. Within XSPEC, tcl procedures can take arguments and execute XSPEC and tcl commands. The syntax for specifying arguments to a tcl procedure is as follows:

proc my_proc {arg1 arg2}{
...

data 1:1 ${arg1}_s0_20
data 2:2 ${arg2}_s1_20
...
}
Here, <arg1>, <arg2> are values supplied by the user (here, part of a filename) from the command line, and substituted wherever <${arg1}>, <${arg2}> appear within the script. One may also give an argument a default value, so that the command so created may be invoked even without needing to specify the argument:
proc my_proc {arg1    {arg2 file2} }  {
...
}
Note that the braces enclosing both <arg2> and <file2> in this expression distinguish this from the case where 3 arguments are required for my_proc. Once this file is created, it needs to be source'd once, which compiles the script into an internal bytecode representation (this is similar to the way Java operates). Alternatively, one may place it in the user script directory and create an index in that directory, after which case it will be found automatically and compiled the first time it is invoked.

The user script directory is given by the line

USER_SCRIPT_DIRECTORY:
in the Xspec.init file that is copied into $HOME/.xspec when the user starts XSPEC for the first time (the supplied default value for this directory is the $HOME/.xspec directory itself). After the script is placed there, perform the following command
%xspec
XSPEC>cd <USER_SCRIPT_DIRECTORY>
XSPEC>auto_mkindex .
XSPEC>exit
This will instruct XSPEC to build an index of scripts to be loaded on xspec startup. On the next invocation of XSPEC, the script will be sourced on startup and will appear in the list of commands XSPEC understands.

The my_proc procedure is then defined such that one may type:

XSPEC>my_proc eso103 eso104
and the data statement in the above example will be executed as if the following had been entered:
data 1:1 eso103_s0_20
data 2:2 eso104_s1_20
The tcl info command can be used to show which procedures have been defined:
XSPEC>info commands <procedure name>
This will return <procedure name> if that procedure has been compiled (source'd) already or is a built-in command, or nothing if it has not (yet) been invoked or defined.