Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]

UNIX-Specific Information


UNIX offers only a single type of file. All files are considered to be an uninterpreted stream of bytes, and there is no such thing as record structure at the operating system level. (By convention, records of text are simply terminated by the linefeed character, which is referred to as "newline.") It is possible to move the current file pointer to any arbitrary position in the file and to begin reading or writing data at that point. This simplicity and generality form a system in which any type of file can be manipulated easily using a small set of file operations.

Reading FORTRAN-Generated Unformatted Data with IDL

The UNIX file system considers all files to be an uninterpreted stream of bytes. Standard FORTRAN I/O considers all input/output to be done in terms of logical records.

In order to reconcile the FORTRAN need for logical records with UNIX files, UNIX FORTRAN programs add a longword count before and after each logical record of data. These longwords contain an integer count giving the number of bytes in that record. Note that direct-access FORTRAN I/O does not write data in this format, but simply transfers binary data to or from the file.

The use of the F77_UNFORMATTED keyword with the OPENR statement informs IDL that the file contains unformatted data produced by a UNIX FORTRAN program. When a file is opened with this keyword, IDL interprets the longword counts properly and is able to read and write files that are compatible with FORTRAN.

Reading data from a FORTRAN file

The following UNIX FORTRAN program produces a file containing a five-column by three-row array of floating-point values with each element set to its one-dimensional subscript:

    PROGRAM ftn2idl 
 
    INTEGER i, j 
    REAL data(5, 3) 
 
    OPEN(1, FILE="ftn2idl.dat", FORM="unformatted") 
      DO 100 j = 1, 3 
        DO 100 i = 1, 5 
          data(i,j) = ((j - 1) * 5) + (i - 1) 
          print *, data(i,j) 
100 CONTINUE 
    WRITE(1) data 
    END 

Running this program creates the file ftn2idl.dat containing the unformatted array. The following IDL statements can be used to read this file and print out its contents:

;Create an array to contain the fortran array. 
data = FLTARR(5,3) 
 
;Open the fortran-generated file. The F77_UNFORMATTED keyword is  
;necessary so that IDL will know that the file contains unformatted  
;data produced by a UNIX FORTRAN program. 
OPENR, lun, 'ftn2idl.dat', /GET_LUN, /F77_UNFORMATTED 
 
;Read the data in a single input operation. 
READU, lun, data 
 
;Release the logical unit number and close the fortran file. 
FREE_LUN, lun 
 
;Print the result. 
PRINT, data 

Executing these IDL statements produces the following output:

0.00000      1.00000      2.00000      3.00000      4.00000 
5.00000      6.00000      7.00000      8.00000      9.00000 
10.0000      11.0000      12.0000      13.0000      14.0000 

Because unformatted data produced by UNIX FORTRAN unformatted WRITE statements are interspersed with extra information before and after each logical record, it is important that the IDL program read the data in the same way that the FORTRAN program wrote it. For example, consider the following attempt to read the above data file one row at a time:

;Create an array to contain one row of the FORTRAN array. 
data = FLTARR(5, /NOZERO) 
 
OPENR, lun, 'ftn2idl.dat', /GET_LUN, /F77_UNFORMATTED 
 
;One row at a time. 
FOR I = 0, 4 DO BEGIN 
 
   ;Read a row of data. 
   READU, lun, data 
 
   ;Print the row. 
   PRINT, data 
ENDFOR 
 
;Close the file. 
FREE_LUN, lun 

Executing these IDL statements produces the output:

0.00000      1.00000      2.00000      3.00000      4.00000 
% READU: End of file encountered. Unit: 100 
         File: ftn2idl.dat6 
% Execution halted at $MAIN$(0). 

Here, IDL attempted to read the single logical record written by the FORTRAN program as if it were written in five separate records. IDL hit the end of the file after reading the first five values of the first record.

Writing data to a FORTRAN file

The following IDL statements create a five-column by three-row array of floating-point values with each element set to it's one-dimensional subscript, and writes the array to a data file suitable for reading by a FORTRAN program:

;Create the array. 
data = FINDGEN(5,3) 
 
;Open a file for writing. Note that the F77_UNFORMATTED keyword is  
;necessary to tell IDL to write the data in a format readable by a  
;FORTRAN program. 
OPENW, lun, 'idl2ftn.dat', /GET_LUN, /F77_UNFORMATTED 
 
;Write the data. 
WRITEU, lun, data 
 
;Close the file. 
FREE_LUN, lun 

The following FORTRAN program reads the data file created by IDL:

    PROGRAM idl2ftn 
 
    INTEGER i, j 
    REAL data(5, 3) 
 
    OPEN(1, FILE="idl2ftn.dat", FORM="unformatted") 
    READ(1) data 
      DO 100 j = 1, 3 
        DO 100 i = 1, 5 
          PRINT *, data(i,j) 
100 CONTINUE 
    END 

Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]