!+ Subroutine to open files of averaged transmission data for output.
!
! 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 output.
!
! 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_OUT(IERR, L_CONTINUUM, N_PER_TRANS_RECORD
     &   , IUNIT_SUMMARY, IUNIT_DATA)
!
!
!
      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(IN)
     &     IUNIT_SUMMARY
!             UNIT NUMBER FOR SUMMARY
     &   , IUNIT_DATA
!             UNIT NUMBER FOR DATA
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , N_PER_TRANS_RECORD
!             NUMBER OF VALUES IN A RECORD
      LOGICAL	!, INTENT(IN)
     &     L_CONTINUUM
!             CONTINUUM FLAG
!
!     LOCAL VARAIBLES.
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     CHAR_YN*1
!             TERMINAL RESPONSE
     &   , BASE_OUT*80
!             BASE NAME OF OUTPUT FILES
     &   , FILE_OUT*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=IUNIT_DATA, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A)') 
     &      '*** ERROR: UNIT ', IUNIT_DATA
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
      INQUIRE(UNIT=IUNIT_SUMMARY, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A)') 
     &      '*** ERROR: UNIT ', IUNIT_SUMMARY
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     REQUEST THE NAME OF THE OUTPUT FILE.
!     BUT FIRST SET ALL ELEMENTS TO ' '.
      DO J=1, LEN(BASE_OUT)
         BASE_OUT(J:J)=' '
      ENDDO
      DO J=1, LEN(FILE_OUT)
         FILE_OUT(J:J)=' '
      ENDDO
      IF (L_CONTINUUM) THEN
         WRITE(IU_USER, '(/A)')
     &      'ENTER THE BASE NAMES FOR THE CONTINUUM OUTPUT FILES.'
      ELSE
         WRITE(IU_USER, '(/A)')
     &      'ENTER THE BASE NAMES FOR THE LINE OUTPUT FILES.'
      ENDIF
      READ(IU_STDIN, '(A)') BASE_OUT
!
!     DETERMINE THE LENGTH OF THE BASE NAME
100   LENGTH_NAME=LEN(BASE_OUT)
1     IF (BASE_OUT(LENGTH_NAME:LENGTH_NAME).EQ.' ') THEN
         LENGTH_NAME=LENGTH_NAME-1
         GOTO 1
      ENDIF
!
!     CHECK WHETHER THE OUTPUT FILES EXIST.
      FILE_OUT(1: LENGTH_NAME+4)=BASE_OUT(1: LENGTH_NAME)//'.sum'
      INQUIRE(FILE=FILE_OUT(1: LENGTH_NAME+4), EXIST=L_EXIST)
      IF (L_EXIST) THEN
         INQUIRE(FILE=FILE_OUT, OPENED=L_OPEN)
         WRITE(IU_ERR, '(/A, /A, A)')
     &      'THE FILE ', FILE_OUT, 'ALREADY EXISTS.'
         IF (L_OPEN) THEN
            WRITE(IU_USER, '(A, /A, A)')
     &         'THE FILE', FILE_OUT,  'IS ALREADY OPEN.'
            IERR=I_ERR_IO
            RETURN
         ENDIF
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(/A/)') 'DO YOU WISH TO OVERWRITE? (Y/N).'
            READ(IU_STDIN, '(A)') CHAR_YN
            IF ( (CHAR_YN.NE.'Y').AND.(CHAR_YN.NE.'y') ) THEN
               WRITE(IU_USER, '(/A/)')
     &            'PLEASE SPECIFY ANOTHER BASE NAME OR QUIT (?Q).'
               READ(IU_STDIN, '(A)') BASE_OUT
               IF ( (FILE_OUT(1:2).EQ.'?Q').OR.
     &              (FILE_OUT(1:2).EQ.'?q') ) THEN
                  WRITE(IU_USER, '(/A/)') '*** PROGRAM TERMINATED.'
                  IERR=I_ERR_FATAL
                  RETURN
               ELSE
                  GOTO 100
               ENDIF
            ENDIF
         ENDIF
      ENDIF
!
!     FILE MAY NOW SAFELY BE OPENED.
      OPEN(UNIT=IUNIT_SUMMARY, FILE=FILE_OUT, IOSTAT=IOS
     &   , STATUS='UNKNOWN'
     &   )
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/, 3A)') '*** ERROR: FILE', FILE_OUT
     &      , ' COULD NOT BE OPENED.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     CONSIDER THE ACTUAL FILE OF DATA. THIS IS OPENED ANYWAY SINCE
!     THE USER WAS WARNED OF OVERWRITING ABOVE.
      FILE_OUT(1: LENGTH_NAME+4)=BASE_OUT(1: LENGTH_NAME)//'.dat'
      OPEN(UNIT=IUNIT_DATA, FILE=FILE_OUT, IOSTAT=IOS
     &   , ACCESS='DIRECT', FORM='UNFORMATTED', STATUS='UNKNOWN'
     &   , RECL=TRANS_RECORD_LENGTH)
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/, 3A)') '*** ERROR: FILE', FILE_OUT
     &      , ' 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
