c----------------------------------------------c
C  Subroutine FLASH                            !
C                                              !
C FLASH sends a message to the program canary  !
C which alerts the user of a serious or po-    !
C tential problem during program execution.    !
C                                              !
c ---------------------------------------------c
      SUBROUTINE flash(severity_level,message,action)
      INTEGER severity_level, action, message_count
      LOGICAL end_of_file
      CHARACTER * 60 message
      DATA message_count / 0 /
C  If ACTION = 1, store the message; otherwise, retrieve the canary
C  file and list all messages.
      IF (action .EQ. 1) THEN
        WRITE( 99,'(1x,i3,a60)' ) severity_level,message
        WRITE( 6,'(1x,"Canary alerted.")' ) 
        message_count = message_count + 1
        IF (message_count .GT. 50) THEN
          PRINT *, ' FLASH: Dead canary.'
          STOP 'Fatal.'
        ENDIF
        IF (severity_level .GT. 4) THEN
          WRITE( 6,'(///)' ) 
          PRINT *, ' ABNORMAL TERMINATION in FLASH.'
          WRITE( 6,'(a60)' ) message
          REWIND 99
          WRITE( 99,'(" severity = ",i3)' ) severity_level
          STOP 'Abort.'
        ENDIF
        RETURN
      ELSE
        REWIND 99
        iworst = 0
        IF (message_count .NE. 0) THEN
          WRITE( 6,'(///," Problems were encountered during this run.", 
     &          /," Please note:")' ) 
          DO 7000 i = 1 , message_count
            READ( 99,'(1x,i3,a60)' ) severity_level,message
            IF (severity_level .GT. iworst) THEN
              iworst = severity_level
            ENDIF
            WRITE( 6,'(a60)' ) message
 7000     CONTINUE
        ENDIF
        REWIND 99
        WRITE( 99,'(" severity = ",i3)' ) iworst
      ENDIF
      RETURN
      END
