!! Provides error message subroutine

MODULE FatalError
!! Provides `ERROUT` error message

  IMPLICIT NONE
  PUBLIC

  INTEGER, PRIVATE :: PRTFile = 6

CONTAINS

  SUBROUTINE ERROUT( WHERE, ErrMsg )
  !! Outputs error message and terminates program

    ! Arguments
    CHARACTER (LEN=*), INTENT( IN ) :: WHERE
        !! Program or subroutine the error is being called from
    CHARACTER (LEN=*), INTENT( IN ) :: ErrMsg
        !! The error message itself

    INTEGER   :: Last

    WRITE( PRTFile, * )
    WRITE( PRTFile, * ) '*** FATAL ERROR ***'

    Last = LEN( WHERE )
    WRITE( PRTFile, * ) 'Generated by program or subroutine: ', WHERE( 1 : Last )

    Last = LEN( ErrMsg )
    WRITE( PRTFile, * ) ErrMsg( 1 : Last )
    WRITE( PRTFile, * )
    CLOSE( PRTFile )

    ! Intel Fortran can't handle the following
    ! ERROR STOP 'Fatal Error in ' // WHERE( 1 : Last1 ) // ': ' // ErrMsg( 1 : Last2 )
    ERROR STOP 'Fatal Error: Check the print file for details'

  END SUBROUTINE ERROUT

END MODULE FatalError
