!+ Subroutine to open files of averaged transmission data for input.
!
! Method:
!	The readable .sum file gives the conditions for which the data
!	were generated and the binary .dat file holding the 
!	transmission data are opened for input.
!
! 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_TRANS_IN(IERR, N_PER_TRANS_RECORD)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/def_pp_io.icf'
      INCLUDE 'incl/record_size_trans.pcf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , N_PER_TRANS_RECORD
!             NUMBER OF VALUES IN A RECORD
!
!     LOCAL VARAIBLES.
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     BASE_IN*80
!             BASE NAME OF OUTPUT FILES
     &   , FILE_IN*80
!             NAME OF OUTPUT FILE
      INTEGER
     &     IOS
!             I/O ERROR FLAG
     &   , LENGTH_NAME
!             LENGTH OF FILE NAME
     &   , J
!             LOOP VARIABLE
      LOGICAL
     &     L_OPEN
!             OPEN FLAG
     &   , L_EXIST
!             EXISTENCE FLAG
!
!
!     CHECK WHETHER THE UNITS FOR THE TRANSMISSION FILES 
!     ARE ALREADY OPEN.
      INQUIRE(UNIT=IU_TRANSMISSION, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A)') 
     &      '*** ERROR: UNIT ', IU_TRANSMISSION
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
      INQUIRE(UNIT=IU_TRANSMISSION_SUMMARY, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A516, I5, A)') 
     &      '*** ERROR: UNIT ', IU_TRANSMISSION_SUMMARY
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     REQUEST THE BASE-NAME OF THE INPUT FILE.
      DO J=1, LEN(BASE_IN)
         BASE_IN(J:J)=' '
      ENDDO
      DO J=1, LEN(FILE_IN)
         FILE_IN(J:J)=' '
      ENDDO
      WRITE(IU_USER, '(/A)')
     &   'ENTER THE BASE NAME OF THE TRANSMISSION INPUT FILES.'
1     READ(IU_STDIN, '(A)') BASE_IN
!
!     DETERMINE THE LENGTH OF THE BASE NAME
      LENGTH_NAME=80
2     IF (BASE_IN(LENGTH_NAME:LENGTH_NAME).EQ.' ') THEN
         LENGTH_NAME=LENGTH_NAME-1
         GOTO 2
      ENDIF
!
!     CHECK WHETHER THE INPUT FILES EXIST.
      FILE_IN(1: LENGTH_NAME+4)=BASE_IN(1: LENGTH_NAME)//'.sum'
100   INQUIRE(FILE=FILE_IN(1: LENGTH_NAME+4), EXIST=L_EXIST)
      IF (.NOT.L_EXIST) THEN
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(/A, /A, /A)')
     &         'THE FILE ', FILE_IN, 'DOES NOT EXIST.'
            WRITE(IU_ERR, '(A)') 'PLEASE ENTER ANOTHER BASE NAME.'
            GOTO 1
         ENDIF
      ENDIF
!
!     FILE MAY NOW SAFELY BE OPENED.
      OPEN(UNIT=IU_TRANSMISSION_SUMMARY, 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
!
!     CONSIDER THE ACTUAL FILE OF DATA.
      FILE_IN(1: LENGTH_NAME+4)=BASE_IN(1: LENGTH_NAME)//'.dat'
      INQUIRE(FILE=FILE_IN(1: LENGTH_NAME+4), EXIST=L_EXIST)
      IF (.NOT.L_EXIST) THEN
         WRITE(IU_ERR, '(/A, /A, /A)')
     &      'THE FILE ', FILE_IN, 'DOES NOT EXIST.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
!     FILE MAY NOW SAFELY BE OPENED.
      OPEN(UNIT=IU_TRANSMISSION, FILE=FILE_IN, IOSTAT=IOS
     &   , ACCESS='DIRECT', FORM='UNFORMATTED'
     &   , STATUS='OLD'
     &   , RECL=TRANS_RECORD_LENGTH)
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/, 3A)') '*** ERROR: FILE', FILE_IN
     &      , ' COULD NOT BE OPENED.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     SET THE NUMBER OF VALUES PER RECORD.
      N_PER_TRANS_RECORD=TRANS_RECORD_LENGTH/24
!
!
      RETURN
      END
