>  Docs Center  >  Using IDL  >  Reading and Writing FORTRAN Data

IDL

Reading and Writing FORTRAN Data

Reading and Writing FORTRAN Data

The standard FORTRAN unformatted sequential file input/output mechanism performs file input and output by reading and writing blocks of data from (or to) a file as logical records. To read data, the FORTRAN program asks for the next logical record from an open file; the operating system is then responsible for determining how much data should be retrieved from the file. This system works well for operating systems like VMS, which organize files into records and can thus keep track of where logical blocks of data begin and end.

In contrast, the UNIX and Microsoft Windows operating systems supported by IDL treat files as an uninterrupted stream of bytes. In order to reconcile the FORTRAN need for logical records with these stream files, FORTRAN compilers for UNIX and Microsoft Windows provide a mechanism to add a longword integer count of the number of bytes in each logical record. This mechanism allows FORTRAN-generated data files that treat data as a series of logical records to be read on platforms that use stream files.

The F77_UNFORMATTED keyword to the OPEN procedures informs IDL that the file contains unformatted data demarcated by logical record identifiers. When a file is opened with this keyword, IDL interprets the longword counts properly and is able to read the logical records. Similarly, IDL can write data using the logical record format using the F77_UNFORMATTED keyword.

Use the F77_UNFORMATTED keyword if your IDL program is reading data that contain embedded longword logical record separators, or if your program is writing data that will be read by a FORTRAN program that reads unformatted sequential files.

Note: On 64-bit machines, some Fortran compilers will insert record markers that are 64-bit integers instead of the standard 32-bit integers. When reading FORTRAN data, IDL will attempt to recognize the presence of 64-bit record markers and switch to the appropriate format. When writing unformatted Fortran files, IDL will continue to use 32-bit record markers.

Note: Direct-access FORTRAN I/O does not write data using logical records, but transfers binary data to or from the file.

Reading Data from a FORTRAN File


The following FORTRAN program, when run on a UNIX or Microsoft Windows system (that is, an operating system that uses stream files), 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 FORTRAN unformatted WRITE statements on an operating system that uses stream files 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
% READUEnd 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 reached 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 its 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



© 2019 Harris Geospatial Solutions, Inc. |  Legal
My Account    |    Store    |    Contact Us