Compaq Fortran
User Manual for
Tru64 UNIX and
Linux Alpha Systems


Previous Contents Index


Chapter 10
Converting Unformatted Numeric Data

This chapter contains the following topics:

10.1 Endian Order of Numeric Formats

Data storage uses a convention of either little endian or big endian storage, depending on the computer. The storage convention generally applies to numeric values that span multiple bytes.

Little endian storage occurs when:

Big endian storage occurs when:

Figure 10-1 shows the difference between the two byte-ordering schemes.

Figure 10-1 Little Endian and Big Endian Storage of an INTEGER Value


Little Endian and Big Endian Storage of an INTEGER Value

Moving data files between little endian and big endian computers requires that the data be converted.

10.2 Little Endian Floating-Point Format

On Compaq Tru64 UNIX and Linux systems, Compaq Fortran supports the following little endian floating-point formats in memory:
Floating-Point Size Format in Memory
KIND=4 IEEE S_float
KIND=8 IEEE T_float
KIND=16 Compaq IEEE style X_float

If your program needs to read or write unformatted data files containing a floating-point format that differs from the format in memory for that data size, you can request that the unformatted data be converted.

Converting unformatted data is generally faster than converting formatted data and is less likely to lose precision for floating-point numbers.

10.3 Native and Supported Nonnative Numeric Formats

Compaq Fortran provides the capability for programs to read and write unformatted data (originally written using unformatted I/O statements) in several nonnative floating-point formats and in big endian INTEGER or floating-point format.

When reading a nonnative unformatted format, the nonnative format on disk must be converted to native format in memory. Similarly, native data in memory can be written to a nonnative unformatted format. If a converted nonnative value is outside the range of the native data type, a run-time message appears (listed in Table 8-3).

Supported native and nonnative floating-point formats include:

The native memory format uses little endian integers and little endian IEEE floating-point formats, as follows:

Table 10-1 lists the keywords for the supported unformatted file data formats. Use the appropriate keyword after the -convert option (such as -convert cray ) or as an environment variable value (see Section 10.5.1 and Section 10.5.2).

Table 10-1 Unformatted Numeric Formats, Keywords, and Supported Data Types
Recognized Keyword1 Description
BIG_ENDIAN Big endian integer data of the appropriate INTEGER size (one, two, four, or eight bytes) and big endian IEEE floating-point formats for REAL and COMPLEX single- and double- and extended-precision numbers. INTEGER (KIND=1) or INTEGER*1 data is the same for little endian and big endian.
CRAY Big endian integer data of the appropriate INTEGER size (one, two, four, or eight bytes) and big endian CRAY proprietary floating-point format for REAL and COMPLEX single- and double-precision numbers.
FDX Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian Compaq proprietary floating-point formats:
  • VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
  • VAX D_float for REAL (KIND=8) and COMPLEX (KIND=8)
  • IEEE style X_float for REAL (KIND=16) and COMPLEX (KIND=16)
FGX Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian Compaq proprietary floating-point formats:
  • VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
  • VAX G_float for REAL (KIND=8) and COMPLEX (KIND=8)
  • IEEE style X_float for REAL (KIND=16) and COMPLEX (KIND=16)
IBM Big endian integer data of the appropriate INTEGER size (one, two, or four bytes) and big endian IBM proprietary (System\370 and similar) floating-point format for REAL and COMPLEX single- and double-precision numbers.
LITTLE_ENDIAN Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following native little endian IEEE floating-point formats:
  • S_float for REAL (KIND=4) and COMPLEX (KIND=4)
  • T_float for REAL (KIND=8) and COMPLEX (KIND=8)
  • IEEE style X_float for REAL (KIND=16) and COMPLEX (KIND=16)
NATIVE No conversion occurs between memory and disk. This is the default for unformatted files.
VAXD Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian VAX Compaq proprietary floating-point formats:
  • VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
  • VAX D_float for REAL (KIND=8) and COMPLEX (KIND=8)
  • VAX H_float for REAL (KIND=16) and COMPLEX (KIND=16)
VAXG Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian VAX Compaq proprietary floating-point formats:
  • VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
  • VAX G_float for REAL (KIND=8) and COMPLEX (KIND=8)
  • VAX H_float for REAL (KIND=16) and COMPLEX (KIND=16)


1When using the data type as a -convert keyword option on the f90 command line, the data type keyword must be in lowercase, such as -convert big_endian.

While this solution is not expected to fulfill all floating-point conversion needs, it provides the capability to read and write various types of unformatted nonnative floating-point data.

For More Information:

Note

1 IEEE floating-point formats are defined in the IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985, Institute of Electrical and Electronics Engineers, August 1985.

10.4 Limitations of Numeric Conversion

The Compaq Fortran floating-point conversion solution is not expected to fulfill all floating-point conversion needs.

Data (variables) contained in derived types and record structures (specified in a STRUCTURE statement) are not converted. When the variables are later examined as separate fields by the program, they will remain in the binary format they were stored in on disk, unless the program is modified.

If a program reads an I/O record containing multiple floating-point fields into an integer array (instead of their respective variables), the fields will not be converted. When the fields are later examined as separate fields by the program, they will remain in the binary format they were stored in on disk, unless the program is modified.

With EQUIVALENCE statements, the data type of the variable named in the I/O statement is used.

10.5 Methods of Specifying the Unformatted Numeric Format

The five methods you can use to specify the type of nonnative (or native) format are as follows:

If you specify more than one method, the order of precedence when you open a file with unformatted data is:

  1. Check for a FORT_CONVERTn environment variable
  2. Check for a FORT_CONVERT.ext environment variable
  3. Check the OPEN statement CONVERT specifier
  4. Check whether an OPTIONS statement with a /CONVERT=keyword qualifier was present when the program was compiled
  5. Check whether the f90 -convert keyword option was used when the program was compiled

If none of these methods are specified, no conversion occurs between disk and memory. Data should therefore be in the native memory format (little endian integer and little endian IEEE format) or otherwise translated by the application program.

Any keyword listed in Table 10-1 can be used with any of these methods.

If you are uncertain about the format, you can do the following:

10.5.1 Environment Variable FORT_CONVERTn Method

You can use the environment variable method to specify multiple formats in a single program, usually one format for each unit number. You specify the numeric format at run time by setting the appropriate environment variable before you open that unit number. For example, to specify the numeric format for unit 9, set environment variable FORT_CONVERT9 to the appropriate value (such as BIG_ENDIAN) before you run the program.

When you open the file, the environment variable is always used, since this method takes precedence over the f90 command-line option methods. For instance, you might use this method to specify that different unformatted numeric formats for different unit numbers (perhaps in a script file that sets the environment variable before running the program).

For example, assume you have a previously compiled program that reads numeric data from unit 28 and writes it to unit 29 using unformatted I/O statements. You want the program to read nonnative big endian (IEEE floating-point) format from unit 28 and write that data in native little endian format to unit 29.

In this case, the data is converted from big endian IEEE format to native little endian IEEE memory format (S_float, T_float, X_float) when read from unit 28, and then written without conversion in native little endian IEEE format to unit 29.

Without requiring source code modification or recompilation of this program, the following C shell command sequence sets the appropriate environment variables before running the program ( /usr/userc/conv_ieee.out ):


% setenv FORT_CONVERT28 BIG_ENDIAN
% setenv FORT_CONVERT29 NATIVE
% /usr/userc/conv_ieee.out

Figure 10-2 shows the data formats used on disk and in memory when the example file /usr/userc/conv_ieee.out is run after the environment variables are set with shell commands.

Figure 10-2 Sample Unformatted File Conversion


Sample Unformatted File Conversion

For More Information:

10.5.2 Environment Variable FORT_CONVERT.ext Method

You can use this method to specify formats in a single program, usually one format for each specified file name extension (suffix). You specify the numeric format at run time by setting the appropriate environment variable before an implicit or explicit OPEN statement to one or more unformatted files.

For example, assume you have a previously compiled program that reads floating-point numeric data from one file and writes to another file using unformatted I/O statements. You want the program to read nonnative big endian (IEEE floating-point) format from a file with a .dat file extension suffix and write that data in native little endian format to a file with a suffix of .data . In this case, the data is converted from big endian IEEE format to native little endian IEEE memory format when read from file.dat , and then written without conversion in native little endian IEEE format to the file with a suffix of .data , assuming that environment variables FORT_CONVERT.DATA and FORT_CONVERTn (for that unit number) are not defined.

Without requiring source code modification or recompilation of this program, the following command sequence sets the appropriate environment variables before running the program ( /usr/userc/proj2/cvbigend.exe ):


% setenv FORT_CONVERT.DAT BIG_ENDIAN
% /usr/userc/proj2/cvbigend.exe

The FORT_CONVERTn method takes precedence over this method. When the appropriate environment variable is set when you open the file, the FORT_CONVERT.ext environment variable is used if a FORT_CONVERTn environment variable is not set for the unit number.

The FORT_CONVERTn and FORT_CONVERT.ext environment variable methods take precedence over the other methods. For instance, you might use this method to specify that a unit number will use a particular format instead of the format specified in the program (perhaps for a one-time file conversion).

The file name extension (suffix) is case-sensitive. The extension must be part of the file name (not the directory path).

10.5.3 OPEN Statement CONVERT='keyword' Method

You can use the OPEN statement method to specify multiple formats in a single program, usually one format for each specified unit number. This method requires an explicit file OPEN statement to specify the numeric format of the file for that unit number.

This method takes precedence over the OPTIONS statement or the -convert keyword method, but has a lower precedence than the environment variable methods.

The following source code shows an OPEN statement coded for unformatted VAXD numeric data (read from unit 15), and an OPEN statement coded for unformatted native little endian format (written to unit 20). The absence of the CONVERT specifier (in the second OPEN statement) or environment variable FORT_CONVERT20 indicates native little endian data for unit 20:


OPEN (CONVERT='VAXD', FILE='graph3.dat', FORM='UNFORMATTED', UNIT=15) 
. 
. 
. 
OPEN (FILE='graph3_ieee.dat', FORM='UNFORMATTED', UNIT=20) 

A hard-coded OPEN statement CONVERT specifier keyword value cannot be changed after compile time. However, to allow selection of a particular format at run time, you can equate the CONVERT specifier to a variable and provide the user with a menu that allows selection of the appropriate format (menu choice sets the variable) before the OPEN occurs.

You can also select a particular format for a unit number at run time by using the environment variable method (see Section 10.5.1), which takes precedence over the OPEN statement CONVERT specifier method.

You can issue an INQUIRE statement (by unit number) to an opened file to obtain the current CONVERT option in use.

10.5.4 OPTIONS Statement /CONVERT=keyword Method

You can only specify one numeric file format for all unit numbers using this method, unless you also use one of the FORT_CONVERT environment variable methods or the OPEN statement CONVERT specifier method.

You specify the numeric format at compile time and must compile all routines under the same OPTIONS statement CONVERT=keyword qualifier. You can use one source program and compile it using different f90 commands to create multiple executable programs that each read a certain format.

The environment variables and OPEN CONVERT specifier methods take precedence over this method. For instance, you might use the environment variables or OPEN CONVERT specifier method to specify each unit number that will use a format other than that specified using the f90 command-line option method. This method takes precedence over the f90 command -convert keyword option method.

You can use OPTIONS statements to specify the appropriate floating-point formats (in memory and in unformatted files) instead of using the corresponding f90 command-line options. For example, to use VAX G_float (along with VAX F_float and VAX H_float) as the unformatted file format, specify the following OPTIONS statement:


OPTIONS /CONVERT=VAXG 

Because this method affects all unit numbers, you cannot read data in one format and write it in another format using the OPTIONS statement method, unless you use it in combination with one of the environment variable methods or the OPEN statement CONVERT keyword method to specify a different format for a particular unit number.

For More Information:

10.5.5 Command-Line -convert keyword Option Method

You can specify only one numeric format for all unit numbers by using the command-line option method, unless you also use the environment variable method or CONVERT specifier method.

You specify the numeric format at compile time and must compile all routines under the same -convert keyword option. You can use one source program and compile it using different f90 commands to create multiple executable programs that each read a certain format.

The other methods take precedence over this method. For instance, you might use the environment variables or OPEN CONVERT specifier method to specify each unit number that will use a format other than that specified using the f90 command-line option method.

For example, the following shell commands compile program file.f90 to use VAX D_float and F_float data. Data is converted between the file format and the little endian memory format (little endian integers and S_float, T_float, and X_float little endian IEEE floating-point format). The created file, vaxd_convert.out , is then run:


% f90 -convert vaxd -o vaxd_convert.out file.f90
% vaxd_convert.out

Because this method affects all unit numbers, you cannot read or write data in different formats if you only use the f90 -convert keyword method. To specify a different format for a particular unit number, use the f90 -convert keyword method in combination with an environment variable method or the OPEN statement CONVERT specifier method.

10.6 Additional Information on Nonnative Data

The following information applies to porting nonnative data:

For More Information:

Note

1 American National Standard Fortran 90, ANSI X3.198-1991, and International Standards Organization standard ISO/IEC 1539:1991


Previous Next Contents Index