!+ Program to generate input data for LOWTRAN7.
!
! Method:
!	A spectral file is read to see which gases and ranges of
!	wavenumber should be considered. The conditions of 
!	pressure and temperature and the amounts of absorber are
!	set. Input cards for LOWTRAN are then produced.
!
! 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.
!
!- ---------------------------------------------------------------------
      PROGRAM GEN_LOWTRAN_DATA
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/def_lwtrn_io.icf'
      INCLUDE 'incl/gas_list.pcf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/lowtran_parameters.ecf'
      INCLUDE 'incl/continuum.pcf'
      INCLUDE 'incl/error.pcf'
!
!     VARIABLES.
      CHARACTER
     &     FILE_SPECTRAL*80
!             NAME OF SPECTRAL FILE
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , IGAS
!             ORDINAL OF GASEOUS SPECIES
      INTEGER
     &     N_PT_PAIR
!             NUMBER OF P,T PAIRS
     &   , N_AMOUNT
!             NUMBER OF AMOUNTS
     &   , I_BAND_FIRST
!             FIRST BAND CONSIDERED
     &   , I_BAND_LAST
!             LAST BAND CONSIDERED
      INTEGER
     &     I
!             LOOP VARIABLE
      LOGICAL
     &     L_GEN_LOWTRAN(NPD_GASES)
!             GENERATE LOWTRAN INPUT FOR GAS
     &   , L_GAS_REQUIRED(NPD_SPECIES)
!             GASES FOR WHICH DATA ARE REQUIRED
     &   , L_CONTINUUM_REQUIRED
!             CONTINUUM DATA ARE REQUIRED
      REAL*8
     &     P(NPD_PT)
!             PRESSURES FOR TRANSMISSION
     &   , T(NPD_PT)
!             TEMPERATURES FOR TRANSMISSION
     &   , U(NPD_GASES, NPD_AMOUNT)
!             AMOUNTS OF GASES
!
!     SET VARIABLES FOR SPECTRAL FILE.
      INCLUDE 'dec_spec.finc'
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     IN_SPECTRUM, OPEN_LOWTRAN, SET_CONDITION, GEN_INPUT
!
!
!     INITIALIZE THE ARRAY FOR GENERATION OF DATA.
      DATA IERR/I_NORMAL/
      DATA L_GEN_LOWTRAN/NPD_GASES*.FALSE./
!
!
!
!
!     REQUEST THE NAME OF THE FILE CONTAINING SPECTRAL DATA.
100   WRITE(IU_USER, '(/A/)')
     &   'GIVE THE NAME OF THE FILE CONTAINING THE SPECTRAL DATA.'
      READ(IU_STDIN, '(A)') FILE_SPECTRAL
!
!     READ IN THE SPECTRAL DATA.
      INCLUDE 'read_spec.finc'
!
      IF (IERR.NE.I_NORMAL) THEN
         IF (IERR.EQ.I_ERR_EXIST) THEN
            IERR=I_NORMAL
            GOTO 100
         ELSE
            STOP
         ENDIF
      ENDIF
!
!     READ THROUGH THE LIST OF GASEOUS ABSORBERS TO
!     DETERMINE THE GASES FOR WHICH TRANSMISSION DATA ARE TO BE
!     GENERATED. THE NUMBERS ARE CHECKED TO ENSURE THAT THE DATA
!     CAN BE GENERATED BY LOWTRAN.
      DO I=1, N_ABSORB
!        ASSEMBLE THE LOCAL NUMBER OF AN ACTUAL ABSORBER.
         IGAS=TYPE_ABSORB(I)
!        CHECK THAT THE NUMBER IS WITHIN THE ALLOWED RANGE.
         IF ( (IGAS.NE.IP_H2O).AND.
     &        (IGAS.NE.IP_CO2).AND.
     &        (IGAS.NE.IP_O3).AND.
     &        (IGAS.NE.IP_N2O).AND.
     &        (IGAS.NE.IP_CO).AND.
     &        (IGAS.NE.IP_CH4).AND.
     &        (IGAS.NE.IP_O2).AND.
     &        (IGAS.NE.IP_NO).AND.
     &        (IGAS.NE.IP_SO2).AND.
     &        (IGAS.NE.IP_NO2).AND.
     &        (IGAS.NE.IP_NH3).AND.
     &        (IGAS.NE.IP_HNO3) ) THEN
            WRITE(IU_ERR, '(/A, I4, A)')
     &         '+++ WARNING A TYPE NUMBER OF ', IGAS
     &         , ' HAS BEEN GIVEN.'
            WRITE(IU_ERR, '(A)')
     &         'THIS DOES NOT CORRESPOND TO A GAS RECOGNISED BY '
     &         //'LOWTRAN.'
            WRITE(IU_ERR, '(A)')
     &         'NO TRANSMISSION DATA FOR THIS GAS WILL BE GENERATED.'
         ELSE
            L_GEN_LOWTRAN(I)=.TRUE.
         ENDIF
      ENDDO
!
!     OPEN THE FILE TO BE USED TO CONTAIN THE INPUT DATA
!     FOR LOWTRAN AND THE RECORD FILE.
      CALL OPEN_LOWTRAN(IERR)
      IF (IERR.NE.I_NORMAL) STOP
!
!     OBTAIN THE CONDITIONS AT WHICH DATA WILL BE GENERATED.
      CALL SET_CONDITION(IERR
     &   , N_BAND, N_ABSORB, TYPE_ABSORB
     &   , L_GEN_LOWTRAN
     &   , N_PT_PAIR, N_AMOUNT
     &   , L_GAS_REQUIRED, L_CONTINUUM_REQUIRED
     &   , I_BAND_FIRST, I_BAND_LAST
     &   , P, T, U
     &   )
      IF (IERR.NE.I_NORMAL) STOP
!
      CALL GEN_INPUT(IERR, TYPE_ABSORB, L_GEN_LOWTRAN
     &   , L_GAS_REQUIRED, L_CONTINUUM_REQUIRED
     &   , N_BAND, I_BAND_FIRST, I_BAND_LAST
     &   , N_ABSORB, N_BAND_ABSORB, INDEX_ABSORB
     &   , N_BAND_CONTINUUM, INDEX_CONTINUUM
     &   , WAVE_LENGTH_SHORT, WAVE_LENGTH_LONG
     &   , N_PT_PAIR, N_AMOUNT
     &   , P, T, U
     &   )
      IF (IERR.NE.I_NORMAL) STOP
!
!     WRITE THE CLOSING CARDS.
      WRITE(IU_LOWTRAN, '(A5)') '    0'
!     CLOSE THE LOWTRAN DATA FILE AND THE ACCOMPANYING RECORD FILE.
      CLOSE(IU_LOWTRAN)
      CLOSE(IU_LOWTRAN_RECORD)
!
!
      STOP
      END
