Datatypes

When reading files created by a Fortran program, you need to be aware of the exact datatypes that were used to write the data. It is essential to specify the correct corresponding Julia datatype when using the read function provided by this package. Especially, note that the default Fortran INTEGER datatype on most systems corresponds to Julia's Int32 datatype, which differs from Julia's default Int datatype on 64-bit systems.

Likewise, when using this package to write Julia data into files which should be readable by a Fortran program, you need to define your data with the correct datatypes, or convert them appropriately before using them in the write function.

Type Correspondence

The following table lists the Julia types which correspond to the standard Fortran types:

Fortran typea.k.a.Julia type
INTEGER(KIND=INT8)INTEGER*1Int8
INTEGER(KIND=INT16)INTEGER*2Int16
INTEGER(KIND=INT32)INTEGER*4Int32
INTEGER(KIND=INT64)INTEGER*8Int64
REAL(KIND=REAL32)REAL*4Float32
REAL(KIND=REAL64)REAL*8Float64
COMPLEX(KIND=REAL32)COMPLEX*8ComplexF32
COMPLEX(KIND=REAL64)COMPLEX*16ComplexF64
CHARACTER(LEN=N)CHARACTER*(N)FString{N}

The first column lists the datatypes using the kind parameters according to the Fortran2008 standard. Most Fortran programs will likely use type declarations as in the second column, although these don't conform to the Fortran standard. If the Fortran program doesn't specify the kind, then the exact Fortran datatype also depends on the compiler options (which can influence the default kind of integers and reals).

This package currently only supports one kind of CHARACTER data, namely ASCII characters with one byte of storage per character.

Strings

Fortran character strings possess an inherent length property. To support reading and writing such data, this package defines an FString datatype which takes the length as a type parameter:

FortranFiles.FStringType
FString{L}

Datatype for reading and writing character strings from FortranFiles. The type parameter L signifies the length of the string. This is the equivalent to the Fortran datatype CHARACTER(len=L).

source
FortranFiles.FStringMethod
FString(L, s::String)

Convert the Julia String s to an FString{L}. s must contain only ASCII characters. As in Fortran, the string will be padded with spaces or truncated in order to reach the desired length.

source

There is not much you can do with FStrings, except printing them and writeing them back to a FortranFile. For conversion to a Julia String, use the following:

FortranFiles.trimstringFunction
trimstring(s::FString)

Convert the FString s into a Julia String, where trailing spaces are removed. Use String(s) to keep the spaces.

source

To make it easier to convert Fortran code into Julia, the following functions are provided for convenience:

FortranFiles.trimFunction
trim(s::FString)

Returns a truncated copy of the FString s where all trailing spaces are removed.

source

Logicals

It is currently undecided how best to support I/O of Fortran LOGICAL data, pending some design decisions.

For the moment, such data can be read or written by treating them as integer data, where 0 corresponds to false and 1 or -1 corresponds to true (depending on the Fortran system). According to the Fortran standard, the storage size for the default LOGICAL kind must be the same as for the default INTEGER kind, therefore you probably want to use Int32 data in Julia.