Substituting String Variables into the Command Line

       

To substitute the value of a string into a command line, enclose the name of the string in braces. As an example, the command RD reads a file from disk. Its syntax is

which reads a disk file 'filename' into buffer number 'buf'. Suppose the string FNAME has been defined to be MYDIR/MYFILE. Thenwill execute the command To print string variables:
1.
Use the command PRINT STRINGS to show all defined strings.
2.
Use PRINTF and substitute the string as the format string. Example:
PRINTF 'STRING'
The and do the string substitution. The quotes are needed if the string has more than one word in it.

3.
Use PRINTF with the %A format specification. (As in FORTRAN), this may be followed by the number of characters to print. Examples:
PRINTF 'Writing file %A' 'FNAME'
PRINTF 'Writing file %A20 ' 'FNAME'

The substitution mechanism can be used to copy the value of a FITS card into a string. The syntax for this is:

where BUFFER is an arithmetic expression which evaluates to a buffer number, and CARDNAME is the name of a FITS card.

The value of the FITS card is substituted into the command line where indicated by the {?BUFFER:CARDNAME} construction. If the buffer number is incorrect, there is nothing in the listed buffer, or if the named card does not exist, an error message is printed and a blank string is loaded. Leading blanks and comments are stripped off. Use the STRING command to load a string with a FITS character card. Use a direct assignment to load a numerical FITS card into a VISTA variable. For example:

STRING OBJ '?23:OBJECT'
Loads the name of the object in buffer 23 into string OBJ.
A=?1:FOCUS
Gets the value of the FOCUS card (a number) and loads the numerical value into A.
ADVANCED EXAMPLES:

In the examples below, the string substitution is used as part of procedures. See the section on procedures for more information.

1.
In this procedure, the user is asked to give a filename. The file contains an image which is to be processed in some standard way. The processed image is written out with the same name.
    STRING FILE '?Enter the file to process. >> '  ! Get filename
    RD 1 {FILE}                                    ! Read image
    CALL PROCESS                                   ! Process it
    WD 1 {FILE}                                    ! Write out
    END
As the procedure is run, the prompt 'Enter ... ' appears user's screen. The reply is loaded into the string variable FILE. Suppose the reply was ./mydir/hd183143. The next command, which reads an image from the disk, uses the string substitution mechanism to insert the string FILE into the command. The actual command executed is Similarly, the last command in the procedure is

2.
The following loop defines the string NAME. The value of NAME is successively FILE001, FILE002, FILE003, FILE004, ... FILE100
    DO INDEX=1,100
       STRING NAME 'FILE%I3.3' INDEX
    END_DO
    END

3.
The following procedure reads an image from tape, processes it in some way, then writes it out to disk in a directory specified by the user. The filename is FILEnnn, where nnn is the number of the image on tape. Program stops when the user gives a number less than or zero.
     STRING DIR '?Which directory will hold the images? $>>$ ' 
     LOOP:
        PRINTF 'Enter a number <=0 to stop.'
        ASK NUM 'Process which number on tape? >> '
        IF NUM<=0
           GOTO FINISH
        END_IF
        RT 1 $NUM
        CALL PROCESS
        STRING NAME 'FILE%I3.3' NUM
        WD 1 {DIR}{NAME}
     GOTO LOOP
     FINISH:
        PRINTF 'Done!'
     END
The string substitution mechanism, and , can also be used to substitute text from an OPEN'ed file. See the discussion for the OPEN and READ commands for more details on using text files.