IRAF supports I/O redirection using the same syntax as in Unix,
e.g.
listpix x.fits[1][371:375,290:281] wcs="physical" > x.txt
.
Python does not have this option,
but PyRAF has a workaround,
making use of ``special'' task parameters
Stdin
, Stdout
and Stderr
.
They are special in the sense that
they are not in the parameter file for any task,
but you can specify them when calling a task in PyRAF.
Stdout
and Stderr
can be set to a numerical value (0 or 1),
which will be taken as a boolean flag,
or the value can be a file name or
Python file handle for a file that is open for writing (or appending).
The example at the beginning of this section
redirected the listpixels output to a text file x.txt;
this can be done in PyRAF as follows:
iraf.listpix ("x.fits[1][371:375,290:281]", wcs="physical", Stdout="x.txt")
.
Note that Python syntax is used, rather than IRAF ``command mode'' syntax.
If Stdout
or Stderr
is set to 1,
the task standard output or standard error respectively
will be returned as the value of the task,
rather than being printed to the terminal window or written to a file.
For example,
x_txt = iraf.listpix ("x.fits[1][371:375,290:281]", wcs="physical", Stdout=1)
.
The function value (x_txt, in this example) will be a list of strings,
one string for each line of text in the output
(the newlines will not be included).
If only Stderr
is specified,
then both Stderr
and Stdout
are written to
the specified Stderr
location.
If both Stderr
and Stdout
are specified,
the output for the two streams are redirected separately.
It is possible to specify redirection of only Stderr
,
though the syntax is a bit weird:
task(params, Stderr=filename, Stdout="STDOUT")
"STDOUT"
is a ``magic'' value that causes output
to be redirected to the normal sys.stdout
.
Similarly, "STDERR"
and "STDIN"
are magic
for their corresponding redirection keywords.
This is included for compatibility with the IRAF CL,
which does the same thing.
Here is a simple example of using this list-of-strings output from listpixels. The first two ``words'' in each string are the pixel coordinates in the X and Y directions, and the image pixel value is the third word. This example computes the flux-weighted sums of X and Y pixel values, then divides by the sum of the weights to get the X and Y centroids.
The Stdin
special parameter can be used
to specify the standard input for a task.
The value for Stdin
can be
a Python list of strings containing the text
(i.e. the same format as the variable
returned as standard output when using Stdout=1
),
or it can be a file name or
a Python file handle for a file open for reading.
A pipe may be emulated by using a Python variable
to pass the standard output of one task to another,
without using a temporary file.
Setting Stderr=1
(instead of Stdout=1
)
may be used to capture messages
that were sent explicitly to standard error,
but see the discussion below about errors vs. warnings.
Some tasks write messages about an error condition
to the standard output rather than to standard error;
in that case, using Stderr=1
is not sufficient to
separate the normal text output from an error message.
When an error condition is encountered,
PyRAF raises an exception
rather than writing the error message and traceback to the standard error.
This information can be captured (see the next section),
but not by using Stderr=1
.
Questions or comments? Contact help@stsci.edu Documented updated on |