Here is a very simple example
to show the essential components of a Python script
that calls an IRAF task, in this case
The file imstat_example.py contains the following:
imstatisticson a list of images. This can be run either from the shell or from a Python or PyRAF session. To run it from the shell, type
imstat_example.py(which must have execute permission) followed by one or more image names. To run it from Python or PyRAF, first type
import imstat_example; then it can be run as follows (for example):
imstat_example.run_imstat(["file.fits", "file.fits", "file.fits"]). The argument is a list, and one or more images may be specified in the list.
from pyraf import iraf
makes IRAF tasks in general available.
iraf.images() loads the
images is already loaded,
iraf.images() does nothing;
a package may be loaded any number of times.
run_imstat() function accepts a list of images
and runs the
imstatistics task (abbreviated as
on each of them.
Like many IRAF tasks,
imstatistics accepts a ``file name template'' as input,
i.e. a string containing one or more image names,
separated by commas, and possibly making use of wildcard characters.
An alternative to running
imstat separately on each image
is to construct such a string containing all the image names,
and to call
imstat just once, with that string as input.
Here is another version of
that concatenates the list elements to a comma-separated string.
Other differences with this version are described below.
As in the previous version,
iraf.images() is used to load the
_doprint=0 argument has been included to
disable printing of the tasks in the package.
The default for
_doprint (a boolean flag) is 1;
_doprint=0 is explicitly specified,
when a package is loaded for the first time
the names of the tasks in that package will be printed to the screen.
Showing the tasks is OK for interactive use,
but when loading a package in a script,
the user wouldn't normally want to see such output.
If the package is already loaded (and images may well be),
then the tasks will not be shown anyway,
but in general it's preferable to specify
when loading any package in a script.
A bit of error handling was also added to this version of
imstat itself just prints a warning and continues
if one or more of the input images do not exist.
Much of the time, that may be all the error handling that is needed.
Additional checking may be useful in cases where
the functions being called take a lot of time
or write intermediate files that would need to be cleaned up
if one of the input files did not exist.
imaccess() function used in this example
is actually of limited use for error checking, however.
It does not catch an invalid image section, for example,
and for a FITS file, the extension number is ignored.
See the section on error handling for further discussion.
Frequently, one wants something even simpler than this, such as a file that just invokes one or more IRAF tasks without defining a function. For example, suppose the following is in the file simple.py:
execfile("simple.py"). Commands in this file could also be executed (once) by
import simple, but in order to do that the file would need to begin with the statement
from pyraf import iraf. Using execfile is simpler, and it is also much easier to repeat; import can only be done once, after which you must use reload.
Here is another example, using slightly different style, and with comments that explain what is being done.
|Questions or comments? Contact email@example.com|
Documented updated on