Read MAT and LUD Files Using Fortran

Read the .mat file that contains the elements of the MoM matrix A and the .lud file that contains the LU decomposition of the method of moments matrix A.

The .mat and .lud files are binary files, written in the native platform coding (for example, little-endian coding on the INTEL / AMD CPUs), and have a Fortran block structure using the COMPLEX data type (in either single or double precision).

Reading can be done again from Fortran using the following code fragment:
   CHARACTER MD5_CHECK*32
    INTEGER VERSION
    LOGICAL FILE_SINGLE_PRECISION
    INTEGER ROWS, COLUMNS
    INTEGER I, J

    OPEN (19, FILE="filename", FORM='UNFORMATTED')

    READ (19) VERSION
    READ (19) MD5_CHECK
    IF (VERSION.GE.2) THEN
      READ (19) FILE_SINGLE_PRECISION
    ELSE
      FILE_SINGLE_PRECISION = .FALSE.
    END IF
    READ (19) ROWS
    READ (19) COLUMNS

    DO J=1, COLUMNS
      IF (FILE_SINGLE_PRECISION) THEN
        READ (19) (MATRIX_S(I,J), I=1, ROWS)
      ELSE
        READ (19) (MATRIX(I,J), I=1, ROWS)
      END IF
    END DO

    CLOSE (19)
with these additional variables:
MATRIX
a two dimensional array at least ROWS*COLUMNS in size to store the data in double precision (declared as DOUBLE COMPLEX).
MATRIX_S
a two dimensional array at least ROWS*COLUMNS in size to store the data in single precision (declared as COMPLEX).
The command above,
        READ (19) (MATRIX_S(I,J), I=1, ROWS)
reads a complete column of the matrix at once.

File Structure

The structure of the .mat and .lud files are as follows:
    | length=4       | VERSION (4 bytes)                | length=4       |
    | length=32      | MD5_CHECK (32 bytes)             | length=32      |
   (| length=4       | FILE_SINGLE_PRECISION (4 bytes)  | length=4       |) -- Only present if VERSION >= 2
    | length=4       | ROWS (4 bytes)                   | length=4       |
    | length=4       | COLUMNS (4 bytes)                | length=4       |
    | length=ROWS*es | MATRIX(:,1) (ROW*es bytes)       | length=ROWS*es |  -- es is 8 or 16 bytes depending on precision.
    | length=ROWS*es | MATRIX(:,2) (ROW*es bytes)       | length=ROWS*es |
    ...
    | length=ROWS*es | MATRIX(:,COLUMNS) (ROW*es bytes) | length=ROWS*es |
Here each record of interest is preceded by a length field that indicates the size (in bytes) of the record.
Note: The size of the length field is 4 bytes.

When reading these files using an external utility, such as one written in C or MATLAB, these length fields must also be considered. They can either be ignored or can be used to detect errors in the reading process.