!+ Subroutine to set the conditions for generating transmissions.
!
! Method:
!	The pressures and temperatures and the amounts of gases
!	are set, either interactively or by reading from a file.
!	The ranges of bands are also set.
!
! 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 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
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/gas_list.pcf'
      INCLUDE 'incl/lowtran_parameters.ecf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , 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	!, INTENT(IN)
     &     N_ABSORB
!             NUMBER OF ABSORBERS
     &   , N_BAND
!             NUMBER OF BANDS
     &   , TYPE_ABSORB(NPD_SPECIES)
!             TYPE NUMBERS OF ABSORBERS
      LOGICAL	!, INTENT(IN)
     &     L_GEN_LOWTRAN(NPD_GASES)
!             FLAG TO GENERATE LOWTRAN DATA
      LOGICAL	!, INTENT(OUT)
     &     L_GAS_REQUIRED(NPD_SPECIES)
!             GASES FOR WHICH DATA ARE REQUIRED
     &   , L_CONTINUUM_REQUIRED
!             CONTINUUM DATA ARE REQUIRED
      REAL*8	!, INTENT(OUT)
     &     P(NPD_PT)
!             PRESSURES FOR TRANSMISSION
     &   , T(NPD_PT)
!             TEMPERATURES FOR TRANSMISSION
     &   , U(NPD_GASES, NPD_AMOUNT)
!             AMOUNTS OF GASES
!
!
!
!     LOCAL VARIABLES.
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     CHAR_YN*1
!             INTERACTIVE RESPONSE
     &   , CHAR_IF*1
!             INTERACTIVE RESPONSE
     &   , LINE*80
!             LINE OF DATA
      INTEGER
     &     IOS
!             I/O ERROR FLAG
     &   , I_SWAP
!             SWAPPING VARIABLE
     &   , I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
      LOGICAL
     &     L_FINISH
!             FLAG FOR ENDING P,T INPUT
     &   , L_FILE
!             FLAG FOR DATA FROM A FILE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     OPEN_FILE_IN, SET_AMOUNT_FILE, SET_AMOUNT_INTERACTIVE
!
!
!
!     DETERMINE THE PRESSURES AND TEMPERATURES AT WHICH DATA ARE TO BE
!     GENERATED.
      WRITE(IU_USER, '(/A)')
     &   'SETTING OF PRESSURES AND TEMPERATURES:'
      WRITE(IU_USER, '(/A)')
     &   'ENTER "F" TO READ FROM A FILE OR "I" TO SET '
     &   //'VALUES INTERACTIVELY.'
1     READ(IU_STDIN, *, IOSTAT=IOS) CHAR_IF
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/A)') '+++ UNRECOGNISED REPONSE.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(/A)') 'PLEASE RE-ENTER.'
            GOTO 1
         ENDIF
      ENDIF
!
      IF ( (CHAR_IF.EQ."F").OR.(CHAR_IF.EQ."f") ) THEN
         L_FILE=.TRUE.
         CALL OPEN_FILE_IN(IERR, IU_FILE_IN
     &      , 'ENTER NAME OF FILE CONTAINING P/T-VALUES.'
     &      )
!        READ THE LINES OF THE FILE UNTIL THE STARTING DIRECTIVE 
!        "*PTVAL" IS ENCOUNTERED.
         READ(IU_FILE_IN, '(A)') LINE
         DO WHILE (LINE(1:6).NE.'*PTVAL')
            READ(IU_FILE_IN, '(A)') LINE
         ENDDO
      ELSE IF ( (CHAR_IF.EQ."I").OR.(CHAR_IF.EQ."i") ) THEN
         L_FILE=.FALSE.
      ELSE
         WRITE(IU_ERR, '(/A)') '+++ ILLEGAL INPUT.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(/A)') 'PLEASE RESPECIFY.'
            GOTO 1
         ENDIF
      ENDIF
!
      N_PT_PAIR=0
      L_FINISH=.FALSE.
2        CALL READ_PT_LINE(IERR, L_FILE, N_PT_PAIR, P, T, L_FINISH)
         IF (IERR.NE.I_NORMAL) RETURN
         IF (.NOT.L_FINISH) GOTO 2
!
         IF (N_PT_PAIR.EQ.0) THEN
            WRITE(IU_ERR, '(A/)')
     &         'AT LEAST ONE PAIR OF VALUES MUST BE SPECIFIED.'
            IF (LOCK_CODE(.TRUE.)) THEN
               INCLUDE 'batch_error.finc'
            ELSE
               IF (L_FILE) THEN
                  WRITE(IU_ERR, '(/A)') 'TERMINIATING PROGRAM.'
                  IERR=I_ERR_FATAL
                  RETURN
               ELSE
                  GOTO 2
               ENDIF
            ENDIF
         ENDIF
!
!     CLOSE THE FILE TO ALLOW THE UNIT TO BE RE-USED.
      CLOSE(IU_FILE_IN)
!
!     NOW SET THE AMOUNTS OF ABSORBERS.
      WRITE(IU_USER, '(/A)')
     &   'SELECT THE METHOD OF SETTING AMOUNTS OF ABSORBERS.'
      WRITE(IU_USER, '(/A)')
     &   'ENTER "F" TO READ FROM A FILE OR "I" TO SET '
     &   //'VALUES INTERACTIVELY.'
3     READ(IU_STDIN, *, IOSTAT=IOS) CHAR_IF
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/A)') '+++ UNRECOGNISED REPONSE.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(/A)') 'PLEASE RE-ENTER.'
            GOTO 3
         ENDIF
      ENDIF
!
!     INITIALIZE U TO 0.0.
      DO I=1, NPD_GASES
         DO J=1, NPD_AMOUNT
            U(I, J)=0.0D+00
         ENDDO
      ENDDO
!
      IF ( (CHAR_IF.EQ."F").OR.(CHAR_IF.EQ."f") ) THEN
!
         CALL SET_AMOUNT_FILE(IERR
     &      , N_ABSORB, TYPE_ABSORB, L_GEN_LOWTRAN
     &      , N_AMOUNT, L_GAS_REQUIRED, U
     &      )
!
      ELSE IF ( (CHAR_IF.EQ."I").OR.(CHAR_IF.EQ."i") ) THEN
!
         CALL SET_AMOUNT_INTERACTIVE(IERR
     &      , N_ABSORB, TYPE_ABSORB, L_GEN_LOWTRAN
     &      , N_AMOUNT, L_GAS_REQUIRED, U
     &      )
!
      ENDIF
      IF (IERR.NE.I_NORMAL) RETURN
!
!     GENERATION OF CONTINUUM DATA.
      WRITE(IU_USER, '(/A, /A)')
     &   'DO YOU WISH TO GENERATE CONTINUUM DATA ? (Y/N)'
     &   , '(THIS MAY ONLY BE DONE IF LINE DATA FOR WATER '
     &   //'VAPOUR ARE GENERATED)'
8     READ(IU_STDIN, *) CHAR_YN
      IF ( (CHAR_YN.EQ.'Y').OR.(CHAR_YN.EQ.'y') ) THEN
         L_CONTINUUM_REQUIRED=.TRUE.
      ELSE IF ( (CHAR_YN.EQ.'N').OR.(CHAR_YN.EQ.'n') ) THEN
         L_CONTINUUM_REQUIRED=.FALSE.
      ELSE
         WRITE(IU_ERR, '(A)') '+++ ERRONEOUS RESPONSE.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(A)') 'PLEASE RE-ENTER.'
            GOTO 8
         ENDIF
      ENDIF
!
!     OBTAIN LIMITS ON BANDS.
      WRITE(IU_USER, '(/A)') 'ENTER FIRST AND LAST BANDS TO BE '
     &   //'CONSIDERED.'
10    READ(IU_STDIN, *, IOSTAT=IOS) I_BAND_FIRST, I_BAND_LAST
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(A)') '+++ ERRONEOUS RESPONSE.'
         IF(LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(A)') 'PLEASE RE-ENTER.'
            GOTO 10
         ENDIF
      ENDIF
!     CHECK LIMITS.
      IF (I_BAND_FIRST.GT.I_BAND_LAST) THEN
         I_SWAP=I_BAND_FIRST
         I_BAND_FIRST=I_BAND_LAST
         I_BAND_LAST=I_SWAP
      ENDIF
      IF ( (I_BAND_FIRST.LT.1).OR.(I_BAND_LAST.GT.N_BAND) ) THEN
         WRITE(IU_ERR, '(A)') '+++ RESPONSE OUT OF RANGE.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(A)') 'PLEASE RE-ENTER.'
            GOTO 10
         ENDIF
      ENDIF
!
!
      RETURN
      END
