CL Tips & Tricks
[Tips Main Page] |
[Submit a Tip]
Output Redirection
To redirect the output of a command to a file, you can use either an
explicit filename or a temporary one in a script by doing something like:
string dummy
dummy = mktemp ("cen")
imcent (image, dummy, ...., >> dummy)
Temporary Uparm Directories
When writing a script you may sometimes want to set parameters for some
tasks, but not have those parameters persist in your uparm directory
once the task is completed (e.g. when setting pset parameters that are only
needed for the particular script). As long as you set all other needed
parameters and accept defaults for unspecified parameters, simply reset
the value of your uparm directory within the script so a temporary uparm
file is created. For example,
set uparm = "tmp$"
rimexam.banner = no
imexam (......)
delete ("tmp$*.par", ver-)
The rimexam parameter file will be updated to one created in /tmp, but
once the script exits your environment still uses the old uparm directory
which has the original parameters (that's why you use 'set' and not
'reset').
Formatted String Variables
(Submitted by Greg Wirth, wirth@keck.hawaii.edu)
Although IRAF lacks an equivalent to sprintf, you can get similar
behavior by scanning the output of the printf statement. For example,
to define image names using zero-filled fields you could use:
cl> printf("lris%04d.fits\n", i) | scan( s1)
where i is set to an integer value, and s1 contains the
resulting image name (i=1 gives s1="lris0001.fits", etc.) Note that the
newline character "\n" in the format statement is required to complete the
scan operation.
Note that if the formatted string is to contain embedded spaces, you'll
need to scan the output of the printf into a variable of type "struct",
such as the builtin variable "line". For example:
cl> printf("Data section is [%d:%d,%d:%d]\n",
x1, x2, y1, y2) | scan( line)
cl> hedit( image, "DATASEC", line, add+)
Parsing String Variables
(Submitted by Greg Wirth, wirth@keck.hawaii.edu)
You can use the translit task as a stream editor to help in parsing
comma-delimited strings. For example, to parse this header card:
cc> imhead lris0106 l+ | match WINDOW
WINDOW = '1,0,0,2048,2048' /
You can use this set of commands in your script:
hselect (image, "window", "yes") | \
translit ("STDIN", ",", " ") | \
scan (winchip, winxstart, winystart, winxlen, winylen)
Using CL Variables
There are 9 variables
for your use pre-defined in the cl: x,y,z for reals, i,j,k
for integers, and s1,s2,s3 for strings. These can be used for
calculating parameter values, or for generating file names, for example.
cl> x = 3
cl> y = x**2
cl> print (y)
9.
cl> i = 1
cl> s1 = "file" + i
cl> print (s1)
file1
cl> i = i + 1
cl> print ("file"+i)
file2
Scan From Pipe
Output from a command may be read into a local script variable directly
using the scan() function. For example,
cl> hselect("dev$pix","otime",yes) | scan(x)
For more information on commands that are built into the CL, see help
language.
Formatted Script I/O
The scanf and printf functions, which work similarly to the
equivalent C functions, can be used to read or write formatted input.
For example,
cl> printf("pi = %g\n", 3.14159)
pi = 3.14159
or
cl> s1 = "12:34:56.7"
cl> print(s1) | scanf("%d:%d:%g",hr,min,sec)
Editing of Parameter Strings in EPAR
It is possible to edit the value of a parameter in the epar menu.
Move to the line on which the parameter is shown as if you are going to
re-type its value. Then type Ctrl-U, Ctrl-L to enter the
edit mode (for vi users, emacs users should type
ESC-Ctrl-K). Use the left/right arrow keys to move within the
parameter string and delete to erase unwanted characters. Type
the new characters and then RETURN to complete the editing process.
Script Debugging
In V2.10.4p2 and later the "d_trace" command will print a detailed
execution of a CL script allowing you to more easily find where a script
crashes. This command is a toggle, typing it again returns you to normal
operation.
Editing a PSET in EPAR
In epar a pset parameter can be edited from the current session using
the :e command.
Menu Mode
By editing a task mode parameter and setting it to 'm', the task
will execute in menu mode meaning that whenever it is executed the
task will first go into epar automatically and can be run using
:go or by exiting.
Parameter Min/Max Values
It's possible to change a task parameter min/max value by setting the
parameter p_minimum or p_maximum attribute. For example,
rv> fxcor.minwidth.p_min=1
Changing a Parameter to Ask for a Value
It is possible to change a task parameter mode from "hidden" to "auto"
so that you are asked for a value each time. Or, you can change a parameter
to stop asking you for values. For example,
cl> display.z1.p_mode = "a"
cl> display.frame.p_mode = "h"
Setting Image Keyword Types
In IRAF sexigesimal numbers are recognized as numbers and not strings so a
real value keyword is created and the input number is converted to decimal.
To add or modify the keyword so it is entered in the image header as a
string value, use a dummy value to first force a string type keyword to
be created and then change the value. For example,
cl> hedit pix newtime dummy add+ verify- show- upate+
cl> hedit pix newtime "1:20:30" verify- show- update+
IRAF
NOAO