readMatrix Interface

public interface readMatrix

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed. The return variable must have ALLOCATABLE attribute, and must not be allocated (the subroutine takes care of allocation but not about deallocation).

Syntax

 call readMatrix(filename, matrix)

Where:

  • filename: string of any length with the path to the file to read. If the file does not exist or it is empty a Fortran runtime error is raised. If the file does not have the proper format the behaviour is undefined.
  • matrix: an allocatable array of rank 2 of any of the integer or real kinds supported. It will be allocated automatically in the subroutine, but the user must deallocate matrix manually after use.

Example

The following example program loads values from a file named matrix.txt:

 PROGRAM readMatrixExample
    USE FU_Files, ONLY: readMatrix
    USE FU_Prec, ONLY: dp
    IMPLICIT NONE
    CHARACTER(LEN=:), ALLOCATABLE :: matrixFileName
    REAL(KIND=dp), DIMENSION(:,:), ALLOCATABLE :: matrix
    matrixFileName = 'matrix.txt'
    CALL readMatrix(matrixFileName, matrix)
    WRITE(*,*) matrix
    DEALLOCATE(matrix)
 END PROGRAM readMatrixExample

File matrix.txt contains the following information:

 2 3 F 
 1.1 1.2 1.3 
 2.1 2.2 2.3

Contents


Module Procedures

private subroutine readMatrix_i8(filename, res)

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename

Filename of the file to read the matrix from.

integer(kind=i8), intent(out), DIMENSION(:,:), ALLOCATABLE :: res

Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information.

private subroutine readMatrix_i16(filename, res)

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename

Filename of the file to read the matrix from.

integer(kind=i16), intent(out), DIMENSION(:,:), ALLOCATABLE :: res

Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information.

private subroutine readMatrix_i32(filename, res)

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename

Filename of the file to read the matrix from.

integer(kind=i32), intent(out), DIMENSION(:,:), ALLOCATABLE :: res

Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information.

private subroutine readMatrix_i64(filename, res)

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename

Filename of the file to read the matrix from.

integer(kind=i64), intent(out), DIMENSION(:,:), ALLOCATABLE :: res

Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information.

private subroutine readMatrix_sp(filename, res)

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename

Filename of the file to read the matrix from.

real(kind=sp), intent(out), DIMENSION(:,:), ALLOCATABLE :: res

Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information.

private subroutine readMatrix_dp(filename, res)

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename

Filename of the file to read the matrix from.

real(kind=dp), intent(out), DIMENSION(:,:), ALLOCATABLE :: res

Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information.

private subroutine readMatrix_qp(filename, res)

Reads a matrix from a file. The file must have the proper format:

First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.

The second row can be a header or not.

Then the matrix comes. The different columns must be separated using blanks.

The file is then opened, read and closed.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename

Filename of the file to read the matrix from.

real(kind=qp), intent(out), DIMENSION(:,:), ALLOCATABLE :: res

Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information.