!+ Subroutine to open input and scratch files for LOWTRAN.
!
! Method:
!	The inpput and record files for LOWTRAN are opened. Since this
!	routine works with LOWTRAN as a subroutine, the output files
!	including the TAPE7 and TAPE8 files are opened as scratch files.
!
! 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_LOWTRAN_INTERNAL(IERR, L_FORM, IU_LOWTRAN_IRD
     &   , IU_LOWTRAN_IPR, IU_LOWTRAN_IPU, IU_LOWTRAN_IPR1)
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/def_lwtrn_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , IU_LOWTRAN_IRD
!             UNIT FOR LOWTRAN'S READING
     &   , IU_LOWTRAN_IPR
!             UNIT FOR LOWTRAN PRINT FILE
     &   , IU_LOWTRAN_IPU
!             UNIT FOR TAPE7 FILE
     &   , IU_LOWTRAN_IPR1
!             UNIT FOR TAPE8 FILE
      LOGICAL	!, INTENT(OUT)
     &     L_FORM
!             FORMAT FLAG
!
!     LOCAL VARAIBLES.
      CHARACTER
     &     FILE_LOWTRAN*80
!             BASE NAME OF LOWTRAN FILE
     &   , FILE_LOWTRAN_RECORD*80
!             NAME OF RECORD FILE
     &   , CHAR_PREVIOUS*1
!             PREVIOUS CHARACTER IN NAME
      INTEGER
     &     IOS
!             ERROR FLAG
     &   , LENGTH_NAME
!             LENGTH OF FILENAME
     &   , I
!             LOOP VARIABLE
      LOGICAL
     &     L_OPEN
!             OPEN FLAG
     &   , L_EXIST
!             EXISTENCE FLAG
!
!
      DO I=1, 80
         FILE_LOWTRAN(I: I)=' '
      ENDDO
!
      WRITE(IU_USER, '(/A/)') 'ENTER THE BASE NAME OF THE'
     &   //' LOWTRAN FILES.'
      READ(IU_STDIN, '(A)') FILE_LOWTRAN
!
!     CHECK WHETHER THE UNITS FOR THE LOWTRAN FILES ARE ALREADY OPEN.
      INQUIRE(UNIT=IU_LOWTRAN_RECORD, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A27)')
     &      '*** ERROR: UNIT ', IU_LOWTRAN_RECORD 
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
      INQUIRE(UNIT= IU_LOWTRAN_INPUT, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A27)')
     &      '*** ERROR: UNIT ', IU_LOWTRAN_INPUT
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
      INQUIRE(UNIT= IU_LOWTRAN_OUTPUT, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A27)')
     &      '*** ERROR: UNIT ', IU_LOWTRAN_OUTPUT
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
      INQUIRE(UNIT= IU_LOWTRAN_TAPE8, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A27)')
     &      '*** ERROR: UNIT ', IU_LOWTRAN_TAPE8
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
      INQUIRE(UNIT= IU_LOWTRAN_TAPE7, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A27)')
     &      '*** ERROR: UNIT ', IU_LOWTRAN_TAPE7
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     DETERMINE THE LENGTH OF THE BASE FILE NAME.
      CHAR_PREVIOUS=' '
      LENGTH_NAME=1
1     IF ( (CHAR_PREVIOUS.EQ.' ').OR.
     &     (FILE_LOWTRAN(LENGTH_NAME: LENGTH_NAME).NE.' ') ) THEN
!        END NOT YET FOUND
         CHAR_PREVIOUS=FILE_LOWTRAN(LENGTH_NAME: LENGTH_NAME)
         LENGTH_NAME=LENGTH_NAME+1
         GOTO 1
      ENDIF
      LENGTH_NAME=LENGTH_NAME-1
!
!
!     ASSEMBLE THE NAME OF THE INPUT FILE AND THE RECORD FILE.
      FILE_LOWTRAN_RECORD(1: LENGTH_NAME+4)
     &   =FILE_LOWTRAN(1: LENGTH_NAME)//'.rec'
!
!     CHECK WHETHER THE FILES EXIST AND OPEN THEM IF THEY DO.
      INQUIRE(FILE=FILE_LOWTRAN(1: LENGTH_NAME)
     &   , EXIST=L_EXIST)
      IF (L_EXIST) THEN
         OPEN(UNIT=IU_LOWTRAN_INPUT
     &      , FILE=FILE_LOWTRAN(1: LENGTH_NAME)
     &      , IOSTAT=IOS, STATUS='OLD'
     &      )
         IF (IOS.NE.0) THEN
            WRITE(IU_ERR, '(/, 3A)')
     &         '*** ERROR: FILE ', FILE_LOWTRAN(1: LENGTH_NAME)
     &         , ' COULD NOT BE OPENED.'
            IERR=I_ERR_IO
            RETURN
         ENDIF
      ELSE
         WRITE(IU_ERR, '(/, 3A)')
     &      '*** ERROR: FILE ', FILE_LOWTRAN(1: LENGTH_NAME)
     &      , ' DOES NOT EXIST.'
         IERR=I_ERR_EXIST
         RETURN
      ENDIF
      INQUIRE(FILE=FILE_LOWTRAN_RECORD(1: LENGTH_NAME+4)
     &   , EXIST=L_EXIST)
      IF (L_EXIST) THEN
         OPEN(UNIT=IU_LOWTRAN_RECORD
     &      , FILE=FILE_LOWTRAN_RECORD(1: LENGTH_NAME+4)
     &      , IOSTAT=IOS, STATUS='OLD'
     &      )
         IF (IOS.NE.0) THEN
            WRITE(IU_ERR, '(/, 3A)')
     &         '*** ERROR: FILE ', FILE_LOWTRAN_RECORD(1: LENGTH_NAME+4)
     &         , ' COULD NOT BE OPENED.'
            IERR=I_ERR_IO
            RETURN
         ENDIF
      ELSE
         WRITE(IU_ERR, '(/, 3A)')
     &      '*** ERROR: FILE ', FILE_LOWTRAN_RECORD(1: LENGTH_NAME+4)
     &      , ' DOES NOT EXIST.'
         IERR=I_ERR_EXIST
         RETURN
      ENDIF
!
!     OPEN THE SCRATCH FILES FOR LOWTRAN.
      OPEN(UNIT=IU_LOWTRAN_TAPE8, FORM='FORMATTED', STATUS='SCRATCH')
      OPEN(UNIT=IU_LOWTRAN_TAPE7, FORM='FORMATTED', STATUS='SCRATCH')
      OPEN(UNIT=IU_LOWTRAN_OUTPUT, FORM='FORMATTED', STATUS='SCRATCH')
!
!     THESE DATA ARE ALWAYS FORMATTED.
      L_FORM=.TRUE.
!
!     SET UNIT NUMBERS FOR LOWTRAN
      IU_LOWTRAN_IRD=IU_LOWTRAN_INPUT
      IU_LOWTRAN_IPR=IU_LOWTRAN_OUTPUT
      IU_LOWTRAN_IPU=IU_LOWTRAN_TAPE7
      IU_LOWTRAN_IPR1=IU_LOWTRAN_TAPE8
!
!
      RETURN
      END
