!+ Subroutine to open an archive of LbL data 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_ARCHIVE_IN(IERR, IUNIT, RECORD_LENGTH, PROMPT
     &   , VERSION, N_ENTRY)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/def_arch_io.icf'
      INCLUDE 'incl/error.pcf'
!
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , IUNIT
!             UNIT FOR FILE
      CHARACTER	!, INTENT(IN)
     &     PROMPT*(*)
!             PROMPT FOR USER
      INTEGER	!, INTENT(OUT)
     &     VERSION
!             VERSION OF FORMAT USED FOR THE ARCHIVE
     &   , N_ENTRY
!             NUMBER OF ENTRIES IN ARCHIVE
!
!
!     LOCAL ARGUMENTS.
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     NAME_ARCHIVE*80
!             NAME OF ARCHIVE FILE
      INTEGER
     &     IOS
!             I/O ERROR FLAG
     &   , RECORD_LENGTH
!             LENGTH OF I/O RECORD
      LOGICAL
     &     L_OPEN
!             LOGICAL FLAG FOR OPEN FILE
     &   , L_EXIST
!             LOGICAL FLAG FOR EXISTING FILE
!
!
!
      WRITE(IU_USER, '(/A)') PROMPT
1     READ(IU_STDIN, '(A)', IOSTAT=IOS) NAME_ARCHIVE
!
      IF (IOS.NE.0) THEN
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(A)')
     &         '+++ UNEXPECTED RESPONSE: PLEASE RE-TYPE.'
            GOTO 1
         ENDIF
      ENDIF
!
!     "?Q" IS THE RESPONSE TO QUIT.
      IF ( (NAME_ARCHIVE(1:2).EQ.'?Q')
     &     .OR.(NAME_ARCHIVE(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=NAME_ARCHIVE, EXIST=L_EXIST)
      IF (.NOT.L_EXIST) THEN
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(A)') 'THIS FILE DOES NOT EXIST.'
            WRITE(IU_ERR, '(A)')
     &         'PLEASE GIVE ANOTHER NAME OR TYPE "?Q" TO QUIT.'
                  GOTO 1
         ENDIF
      ENDIF
!
!     NOW OPEN THE FILE.
      OPEN(UNIT=IUNIT, ACCESS='DIRECT', RECL=RECORD_LENGTH
     &   , FORM='UNFORMATTED', FILE=NAME_ARCHIVE, IOSTAT=IOS
     &   , STATUS='OLD'
     &   )
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(4A)') '*** ERROR: '
     &      , 'FILE ', NAME_ARCHIVE, 'COULD NOT BE OPENED.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     THE VERSION IS READ HERE TO ENABLE THE APPROPRIATE ROUTINE TO
!     BE CALLED LATER.
      READ(IUNIT, REC=1, IOSTAT=IOS) VERSION, N_ENTRY
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(2A)') '*** ERROR: '
     &      , 'VERSION AND NUMBER OF ENTRIES COULD NOT BE READ.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     CHECK THAT THE VERSION OF THE ARCHIVE IS ALLOWED.
      IF ( (VERSION.NE.0).AND.(VERSION.NE.1) ) THEN
         WRITE(IU_ERR, '(/A)')
     &      '*** ERROR: THE VERSION OF THE ARCHIVE IS NOT 0 OR 1.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
!
      RETURN
      END
