!+ Subroutine to open a non-specific file for input.
!
! Method:
!       Straightforward.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!       Version         Date                    Comment
!       1.0             12-04-95                First Version under RCS
!                                               (J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77  with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      SUBROUTINE OPEN_FILE_IN(IERR, IUNIT, REQUEST)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY VARIABLES
      CHARACTER	!, INTENT(IN)
     &     REQUEST*(*)
!             PROMPT TO USER
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(OUT)
     &     IUNIT
!             ERROR FLAG
!
!     LOCAL VARIABLES
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     FILE_IN*80
!             NAME OF TRANSMISSION FILE
      LOGICAL
     &     L_EXIST
!             EXISTENCE FLAG FOR FILE
     &   , L_OPEN
!             OPEN FLAG FOR FILE
      INTEGER
     &     IOS
!             READING ERROR FLAG
     &   , J
!             LOOP VARIABLE
!
!
!     OBTAIN THE NAME OF THE FILE OF DATA TO BE READ IN.
      WRITE(IU_USER, '(/A)') REQUEST
!
1     DO J=1, 80
!        CLEAR THE INPUT FILENAME.
         FILE_IN(J:J)=' '
      ENDDO

      READ(IU_STDIN, '(A)', IOSTAT=IOS) FILE_IN
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(A)') '+++ UNEXPECTED RESPONSE:'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(A)') 'PLEASE RE-TYPE.'
            GOTO 1
         ENDIF
      ENDIF
!
!     "?Q" IS THE RESPONSE TO QUIT.
      IF ( (FILE_IN(1:2).EQ.'?Q').OR.(FILE_IN(1:2).EQ.'?q') ) THEN
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
!     CHECK THAT THE UNIT FOR INPUT IS FREE.
      INQUIRE(UNIT=IUNIT, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/A)') '***ERROR: UNIT FOR READING THE FILE '
     &      //'IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     CHECK FOR THE EXISTENCE OF THE FILE.
      INQUIRE(FILE=FILE_IN, EXIST=L_EXIST)
      IF (.NOT.L_EXIST) THEN
         WRITE(IU_ERR, '(A)') 'THIS FILE DOES NOT EXIST.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(A)')
     &         'PLEASE GIVE ANOTHER NAME OR TYPE "?Q" TO QUIT.'
            GOTO 1
         ENDIF
      ENDIF
!
!     NOW OPEN THE FILE.
      OPEN(UNIT=IUNIT, FILE=FILE_IN, IOSTAT=IOS
     &   , STATUS='OLD'
     &   )
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/, 3A)')
     &      '*** ERROR: FILE ', FILE_IN, 'COULD NOT BE OPENED.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!
      RETURN
      END
