!+ Subroutine to write input data for LOWTRAN7.
!
! Method:
!	The amounts are converted to concentrations as required by
!	LOWTRAN7. A set of cards is written and an entry is made in 
!	the record file for each case for which LOWTRAN is to be run.
!
! 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 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
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/gas_list.pcf'
      INCLUDE 'incl/gas_list.ncf'
      INCLUDE 'incl/lowtran_parameters.ecf'
      INCLUDE 'incl/lowtran_dec.ecf'
      INCLUDE 'incl/continuum.pcf'
      INCLUDE 'incl/continuum.ncf'
      INCLUDE 'incl/physical_constants_0.ccf'
      INCLUDE 'incl/physical_constants_1.ccf'
      INCLUDE 'incl/physical_constants_pp.ccf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/def_lwtrn_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N_BAND
!             NUMBER OF BANDS
     &   , N_ABSORB
!             NUMBER OF ABSORBERS
     &   , I_BAND_FIRST
!             FIRST BAND CONSIDERED
     &   , I_BAND_LAST
!             LAST BAND CONSIDERED
     &   , TYPE_ABSORB(NPD_SPECIES)
!             TYPES OF ABSORBERS
     &   , N_PT_PAIR
!             NUMBER OF PRESSURES
     &   , N_AMOUNT
!             NUMBER OF AMOUNTS
     &   , N_BAND_ABSORB(NPD_BAND)
!             NUMBER OF ABSORBERS IN BANDS
     &   , INDEX_ABSORB(NPD_SPECIES, NPD_BAND)
!             LIST OF ABSORBERS
     &   , N_BAND_CONTINUUM(NPD_BAND)
!             NUMBER OF CONTINUA IN BANDS
     &   , INDEX_CONTINUUM(NPD_BAND, NPD_CONTINUUM)
!             LIST OF CONTINUA
      LOGICAL	!, INTENT(IN)
     &     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	!, INTENT(IN)
     &     WAVE_LENGTH_SHORT(NPD_BAND)
!             SHORTER WAVELENGTH LIMITS
     &   , WAVE_LENGTH_LONG(NPD_BAND)
!             UPPER WAVELENGTH LIMITS
     &   , P(NPD_PT)
!             PRESSURES FOR TRANSMISSION
     &   , T(NPD_PT)
!             TEMPERATURES FOR TRANSMISSION
     &   , U(NPD_GASES, NPD_AMOUNT)
!             AMOUNTS OF GASES
!
!     LOCAL VARIABLES.
      INTEGER
     &     I_CASE
!             CASE NUMBER OF RUN
     &   , I_INDEX
!             INDEX NUMBER OF GAS
     &   , IGAS
!             TYPE NUMBER OF GAS
     &   , IPT
!             PRESSURE, TEMPERATURE INDEX
     &   , IA
!             AMOUNT INDEX
     &   , IB
!             BAND INDEX
     &   , IL
!             INDEX OF LOWTRAN GASES
     &   , J
!             LOOP INDEX
      LOGICAL
     &     L_GENERATE_GAS
!             TRUE IF A LOWTRAN GAS IS REQUIRED
     &   , L_FIRST
!             FLAG FOR FIRST CALL
      REAL*8
     &     WMOL(NPD_GAS_LOW)
!             WMOL ARRAY FOR LOWTRAN
     &   , AMOUNT_N2
!             AMOUNT OF NITROGEN
     &   , RANGE
!             ACTUAL RANGE OF DATA
     &   , RANGE_MIN
!             MINIMUM POSSIBLE RANGE
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/gas_names.dcf'
      INCLUDE 'incl/gas_molar_weight.ccf'
      INCLUDE 'incl/gas_lowtran_type.ecf'
      INCLUDE 'incl/continuum_names.dcf'
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     LOWTRAN_CARDS
!
!
      DATA L_FIRST/.TRUE./
!
!
!
!     CONSTRUCT A SERIES OF SETS OF TRANSMISSION DATA.
      I_CASE=0
!
!     LOOP OVER PRESSURE AND TEMPERATURE POINTS
      DO IPT=1, N_PT_PAIR
!
!        LOOP OVER THE AMOUNTS OF TRANSMISSION.
         DO IA=1, N_AMOUNT
!
!           SET THE RANGE TO BE USED. IDEALLY, THIS IS SMALL TO GIVE
!           N2-CONTINUUM TRANSMITTANCES OF A REASONABLE SIZE; BUT IT
!           CANNOT BE TOO SMALL, FOR THE AMOUNT AND THE RANGE
!           DETERMINE THE DENSITY OF EACH SPECIES, AND HENCE A LOWER
!           BOUND ON THE GAS DENSITY; THE ACTUAL DENSITY IMPLIED BY
!           THE PRESSURE AND THE TEMPERATURE MUST EXCEED THIS OR
!           LOWTRAN WILL GENERATE INFINITE VALUES FOR THE FOREIGN
!           CONTINUUM. THE MINIMUM POSSIBLE VALUE FORCED BY LOWTRAN'S
!           FORMAT FOR THE RANGE IS 1 km.
            RANGE_MIN=0.0D+00
            DO I_INDEX=1, N_ABSORB
               IGAS=TYPE_ABSORB(I_INDEX)
               RANGE_MIN=RANGE_MIN+U(IGAS, IA)/MOLAR_WEIGHT(IGAS)
            ENDDO
            RANGE_MIN=R_GAS*T(IPT)*RANGE_MIN/P(IPT)
!           INCLUDE A SAFTEY FACTOR OF 2.
            RANGE=MAX(RANGE_MIN*2.0D+00, RANGE_DEFAULT)
!
!           INITIALIZE WMOL FOR THIS SET OF ABSORBER AMOUNTS.
            DO IGAS=1, NPD_GAS_LOW
               WMOL(IGAS)=0.0D+00
            ENDDO
            DO I_INDEX=1, N_ABSORB
               IGAS=TYPE_ABSORB(I_INDEX)
               IF (L_GEN_LOWTRAN(I_INDEX)) THEN
                  WMOL(IP_LOWTRAN_NUMBER(IGAS))=U(IGAS, IA)/RANGE
               ENDIF
            ENDDO
!
!           LOOP OVER SPECTRAL BANDS.
            DO IB=I_BAND_FIRST, I_BAND_LAST
!
!              GENERATE DATA ONLY IF THERE IS GASEOUS ABSORPTION BY ONE
!              OF LOWTRAN'S GASES OR CONTINUUM ABSORPTION IN THE BAND.
               L_GENERATE_GAS=.FALSE.
               DO J=1, N_BAND_ABSORB(IB)
                  I_INDEX=INDEX_ABSORB(J, IB)
                  IF (IP_LOWTRAN_NUMBER(TYPE_ABSORB(I_INDEX)).GT.0)
     &               L_GENERATE_GAS=L_GENERATE_GAS
     &                  .OR.L_GAS_REQUIRED(I_INDEX)
               ENDDO
               IF ( (L_GENERATE_GAS)
     &            .OR.( (N_BAND_CONTINUUM(IB).GT.0)
     &            .AND.(L_CONTINUUM_REQUIRED) ) ) THEN
!
!                 A NEW BLOCK OF THE LOWTRAN FILE BEGINS.
                  I_CASE=I_CASE+1
                  IF (I_CASE.GT.NPD_CASE) THEN
                     WRITE(IU_ERR, '(/A)') '*** ERROR: TOO MANY CASES: '
                     WRITE(IU_ERR, '(A)') 'NPD_CASE MUST EXCEED '
     &                  //'THE PRODUCT OF THE NUMBER OF BANDS, '
     &                  //'AMOUNTS, PRESSURES AND TEMPERATURES. '
                     WRITE(IU_ERR, '(A)') 'INCREASE NPD_CASE '
     &                  //'AND RE-COMPILE.'
                     IERR=I_ERR_FATAL
                     RETURN
                  ENDIF
!
!                 WRITE OUT THE RECORDS FOR EACH GAS.
!
                  WRITE(IU_LOWTRAN_RECORD, '(//A4, 1X, I5)')
     &               'CASE', I_CASE
                  WRITE(IU_LOWTRAN_RECORD, '(/5X, A6 ,1X ,I5
     &               , 2X, A10, 1X, 1PE11.4, 1X, A2, 2X, A13, 1X
     &               , 1PE11.4, 1X, A1)') 'BAND =', IB
     &               , 'PRESSURE =', P(IPT), 'Pa', 'TEMPERATURE ='
     &               , T(IPT), 'K'
                  WRITE(IU_LOWTRAN_RECORD, '(5X, A7, 1X, 1PE11.4
     &               , 1X, A1)')
     &               'RANGE =', RANGE, 'm'
                  WRITE(IU_LOWTRAN_RECORD, '(/5X, A35)')
     &               'AMOUNTS OF LOWTRAN''S GASES (kg/m2):'
                  WRITE(IU_LOWTRAN_RECORD, '((5X, 2(3X, A20, A1
     &               , 1X, 1PE12.5)))')
     &               (NAME_ABSORB(IP_TYPE_LOWTRAN(IL))
     &               , ':', U(IP_TYPE_LOWTRAN(IL), IA)
     &               , IL=1, NPD_GAS_LOW)
                  AMOUNT_N2=N2_MASS_FRAC
     &               *(P(IPT)/(T(IPT)*R_GAS_DRY))*RANGE
                  WRITE(IU_LOWTRAN_RECORD, '(/5X, A27)')
     &               'AMOUNT OF NITROGEN (kg/m2):'
                  WRITE(IU_LOWTRAN_RECORD, '(5X, 3X, A20, A1, 1X
     &               , 1PE12.5)') NAME_ABSORB(IP_N2), ':', AMOUNT_N2
!
!                 WRITE THE LOWTRAN CARDS TO CALCULATE THE TRANSMISSION.
                  CALL LOWTRAN_CARDS(P(IPT), T(IPT), RANGE, WMOL
     &               , WAVE_LENGTH_SHORT(IB)
     &               , WAVE_LENGTH_LONG(IB)
     &               , L_FIRST
     &               )
!
!                 RESET L_FIRST FOR SUBSEQUENT CALLS.
                  L_FIRST=.FALSE.
!
               ENDIF
!
            ENDDO
         ENDDO
      ENDDO
!
!
      RETURN
      END
