!+ Subroutine to open files for input to LOWTRAN.
!
! Method:
!	A name is obtained from the file to contain input to LOWTRAN.
!	A record file with the suffix ".rec" is also opened.
!
! 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(IERR)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/def_lwtrn_io.icf'
      INCLUDE 'incl/file_type.pcf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
!
!     LOCAL VARAIBLES.
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     CHAR_YN*1
!             TERMINAL RESPONSE
     &   , FILE_LOWTRAN*80
!             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
      LOGICAL
     &     L_OPEN
!             OPEN FLAG
     &   , L_EXIST
!             EXISTENCE FLAG
!
!
!     CHECK WHETHER THE UNIT FOR THE LOWTRAN FILE IS ALREADY OPEN.
      INQUIRE(UNIT= IU_LOWTRAN, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/A)') 
     &      '*** ERROR: THE UNIT IS ALREADY OPEN ELSEWHERE.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
!     REQUEST THE NAME OF THE LOWTRAN DATA FILE.
      WRITE(IU_USER, '(/A)')
     &   'ENTER NAME FOR THE LOWTRAN DATA FILE.'
      READ(IU_STDIN, '(A)') FILE_LOWTRAN
!
!     CHECK WHETHER THE FILE EXISTS.
100   INQUIRE(FILE=FILE_LOWTRAN, EXIST=L_EXIST)
      IF (L_EXIST) THEN
         INQUIRE(FILE=FILE_LOWTRAN, OPENED=L_OPEN)
         WRITE(IU_ERR, '(/A)') 'THIS FILE ALREADY EXISTS.'
         IF (L_OPEN) THEN
            WRITE(IU_ERR, '(A)') 'THIS FILE IS ALREADY OPEN.'
         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 NAME OR QUIT (?Q).'
               READ(IU_STDIN, '(A)') FILE_LOWTRAN
               IF ( (FILE_LOWTRAN(1:2).EQ.'?Q').OR.
     &              (FILE_LOWTRAN(1:2).EQ.'?q') ) THEN
                  WRITE(IU_ERR, '(/A/)') '*** PROGRAM TERMINATED.'
                  IERR=I_ERR_FATAL
                  RETURN
               ELSE
                  GOTO 100
               ENDIF
            ENDIF
         ENDIF
      ENDIF
!
!     FILE MAY NOW SAFELY BE OPENED.
      OPEN(UNIT=IU_LOWTRAN, FILE=FILE_LOWTRAN, IOSTAT=IOS
     &   , STATUS='UNKNOWN'
     &   )
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/, 3A)') '*** ERROR: FILE', FILE_LOWTRAN
     &      , ' COULD NOT BE OPENED.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     PRINT A MESSAGE SAYING THAT THE FILE HAS BEEN OPENED SUCCESSFULLY.
      WRITE(IU_STDOUT, '(/A)')
     &   'THE FILE FOR LOWTRAN DATA HAS BEEN OPENED, NAMED:'
      WRITE(IU_STDOUT, '(A/)') FILE_LOWTRAN
!
!     OPEN THE RECORD FILE. NO OVERWRITING CHECKS SHOULD BE MADE
!     HERE SINCE THIS MUST AGREE WITH THE CURRENT VERSION OF
!     FILE_LOWTRAN.
!
!     DETERMINE THE LENGTH OF THE 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
      FILE_LOWTRAN_RECORD(1: LENGTH_NAME+4)
     &   =FILE_LOWTRAN(1: LENGTH_NAME)//'.rec'
      OPEN(UNIT=IU_LOWTRAN_RECORD
     &   , FILE=FILE_LOWTRAN_RECORD(1:LENGTH_NAME+4)
     &   , IOSTAT=IOS, STATUS='UNKNOWN'
     &   )
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/, 3A)') 
     &      '*** ERROR: FILE', FILE_LOWTRAN_RECORD
     &      , ' COULD NOT BE OPENED.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     NOTE THAT THE RECORD FILE HAS BEEN OPENED SUCCESSFULLY.
      WRITE(IU_STDOUT, '(A)') 'A SUMMARY OF THE LOWTRAN DATA IS '
     &   //'STORED IN THE FILE:'
      WRITE(IU_STDOUT, '(A)') FILE_LOWTRAN_RECORD(1: LENGTH_NAME+4)
!
!     WRITE THE HEADER TO THE RECORD FILE.
      WRITE(IU_LOWTRAN_RECORD, '(A13, I5, //, 27X, A/)')
     &   '*FILE TYPE = ', IT_FILE_LOWTRAN_RECORD
     &   , 'RECORD OF RUNS OF LOWTRAN'
!
!
      RETURN
      END
