|
Application Programming: Files and Input/Output |
|
Use of formatted data is most appropriate when the data must be in human readable form, such as when it is to be prepared or modified with a text editor. Formatted data also are highly portable between various computers and operating systems.
In addition to the PRINT, PRINTF, READ, and READF routines already discussed, the STRING function can be used to generate formatted output that is sent to a string variable instead of a file. The READS procedure can be used to read formatted input from a string variable.
The exact format of the character data may be specified to these routines by providing a format string via the FORMAT keyword. If no format string is given, default formats for each type of data are applied. This method of formatted input/output is called free format. Free format input/output is suitable for most applications involving formatted data. It is designed to provide input/output abilities with a minimum of programming.
IDL structures present a special problem for default formatted input and output. The default format for displaying structure data is to surround the structure with curly braces ({}). For example, if you define an anonymous structure:
struct = { A:2, B:3, C:'A String' }
and then use default formatted output via the PRINT command:
PRINT, struct
IDL prints:
{ 2 3 A String}
You might suppose that default formatted input would recognize that the curly braces are part of the formatting and ignore them. This is not the case, however. By default, to read the third field in the structure (the string field) IDL will read from the "A" to the end of the line, including the closing brace.
This behavior, while unsymmetric, seems to be the best choice for default behavior—displaying the result of the PRINT statement on the computer screen. We recommend that you use explicitly formatted input/output when reading and writing structures to disk files, so as not to have to explicitly code around the possibility that your structure may include strings.
The following rules are used by IDL to perform free format input:
A = INTARR(5) READ, A
causes IDL to read five separate values to fill each element of the variable A.
;Create a complex variable. A = COMPLEX(0) ;IDL prompts for input with a colon: READ, A ;The user enters "(3,4)" and A is set to COMPLEX(3, 4). :(3, 4) ;IDL prompts for input with a colon: READ, A ;The user enters "50" and A is set to COMPLEX(50, 0). :50
The following rules are used by IDL to perform free format output:
IDL free format input/output is extremely easy to use. The following IDL statements demonstrate how to read into a complicated structure variable and then print the results:
;Create a structure named "types" that contains seven of the basic
;IDL data types, as well as a floating-point array.
A = {TYPES, A:0B, B:0, C:0L, D:1.0, E:1D, $
F:COMPLEX(0), G: 'string', E:FLTARR(5)}
;Read free-formatted data from input
READ, A
;IDL prompts for input with a colon. We enter values for the first
;six numeric fields of A and the string.
: 1 2 3 4 5 (6,7) EIGHT
Notice that the complex value was specified as (6, 7). If the parentheses had been omitted, the complex field of A would have received the value COMPLEX(6, 0), and the 7 would have been input for the next field. When reading into a string variable, IDL starts from the current point in the input and continues to the end of the line. Thus, we do not enter values intended for the rest of the structure on this line.
;There are still fields of A that have not received data, so IDL ;prompts for another line of input. : 9 10 11 12 13 ;Show the result. PRINT, A
Executing these statements results in the following output:
{ 1 2 3 4.00000 5.0000000
( 6.00000, 7.00000) eight
9.00000 10.0000 11.0000 12.0000 13.0000
}
When producing the output, IDL uses default rules for formatting the values and attempts to place as many items as possible onto each line. Because the variable A is a structure, braces {} are placed around the output. As noted above, when IDL reads strings it continues to the end of the line. For this reason, it is usually convenient to place string variables at the end of the list of variables to be input.
For example, if S is a string variable and I is an integer:
;Read into the string first. READ, S, I ;IDL prompts for input. We enter a string value followed by an ;integer. : Hello World 34 ;The entire previous line was placed into the string variable S, ;and I still requires input. IDL prompts for another line. : 34
IDL Online Help (March 06, 2007)