!+ Subroutine to set the conditions for continuum transmissions.
!
! Method:
!       The pressures and temperatures and the amounts of water
!       vapour are set, either interactively or by reading from 
!       a file. If averaging over an archive, the amounts are 
!	determined from the archive. 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_CONTINUUM(IERR
     &   , L_ARCH
     &   , N_BAND, INDEX_WATER
     &   , N_ENTRY, P_ARCH, T_ARCH
     &   , N_PT_PAIR, N_AMOUNT
     &   , I_BAND_FIRST, I_BAND_LAST
     &   , P, T, U
     &   , N_PARTIAL_P
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_arch.ucf'
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision_pp.tcf'
      INCLUDE 'incl/gas_list.pcf'
      INCLUDE 'incl/gas_list.ncf'
      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
     &   , N_PARTIAL_P
!             NUMBER OF PARTIAL PRESSURES
      INTEGER	!, INTENT(IN)
     &     N_BAND
!             NUMBER OF BANDS
     &   , INDEX_WATER
!             INDEX OF WATER
      LOGICAL	!, INTENT(IN)
     &     L_ARCH
!             FLAG FOR USING AN ARCHIVE
      INTEGER	!, INTENT(IN)
     &     N_ENTRY
!             NUMBER OF ENTRIES IN ARCHIVE
      REAL*8	!, INTENT(IN)
     &     P_ARCH(NPD_ARCH_ENTRY)
!             PRESSURES OF ENTRIES IN ARCHIVES
     &   , T_ARCH(NPD_ARCH_ENTRY)
!             TEMPERATURES OF ENTRIES IN ARCHIVES
      REAL*8	!, INTENT(OUT)
     &     P(NPD_PT)
!             PRESSURES FOR TRANSMISSION
     &   , T(NPD_PT)
!             TEMPERATURES FOR TRANSMISSION
     &   , U(NPD_AMOUNT_CONTIN)
!             AMOUNTS OF WATER VAPOUR
!
!     LOCAL VARIABLES.
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     CHAR_IF*1
!             RESPONSE TO REQUEST FOR TYPE OF INPUT
     &   , LINE*80
!             LINE OF INPUT
      INTEGER
     &     IOS
!             I/O ERROR FLAG
     &   , I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
     &   , I_SWAP
!             VARIABLE FOR SWAPPING
      LOGICAL
     &     L_FILE
!             FLAG FOR READING INPUT FROM A FILE
     &   , L_FINISH
!             FLAG FOR ENDING P,T INPUT
     &   , L_FOUND
!             TRUE IF ENTRY MATCHES PREVIOUS DATA
      REAL*8
     &     AMOUNT_MIN
!             MINIMUM AMOUNT
     &   , AMOUNT_MAX
!             MAXIMUM AMOUNT
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     OPEN_FILE_IN
!
!
      INCLUDE 'incl/gas_names.dcf'
!
!
!
!     DETERMINE THE PRESSURES AND TEMPERATURES AT WHICH DATA ARE TO BE
!     GENERATED. IF AVERAGING ACROSS AN ARCHIVE, THE PRESSURES AND
!     TEMPERATURES AND AMOUNTS AT WHICH DATA ARE TO BE GENERATED ARE
!     DETERMINED FROM THE ARCHIVE.
      IF (L_ARCH) THEN
!        BEGIN WITH THE FIRST ENTRY.
         IF (N_ENTRY.GE.1) THEN
            N_PT_PAIR=1
            P(1)=P_ARCH(1)
            T(1)=T_ARCH(1)
         ELSE
            N_PT_PAIR=0
            RETURN
         ENDIF
!
!        CHECK EACH ENTRY AND EXTEND THE LIST OF PRESSURES AND 
!        TEMPERATURES IF NECESSARY.
         DO I=2, N_ENTRY
            J=0
            L_FOUND=.FALSE.
10          J=J+1
               IF ( (ABS(P_ARCH(I)-P(J)).LT.P_ARCH(I)*TOL_PRESS_PP).AND.
     &              (ABS(T_ARCH(I)-T(J)).LT.T_ARCH(I)*TOL_TEMP_PP) )
     &            THEN
                  L_FOUND=.TRUE.
               ENDIF
               IF (.NOT.L_FOUND.AND.(J.LT.N_PT_PAIR) ) GOTO 10
               IF (.NOT.L_FOUND) THEN
                  IF (N_PT_PAIR.LT.NPD_PT) THEN
                     N_PT_PAIR=N_PT_PAIR+1
                     P(N_PT_PAIR)=P_ARCH(I)
                     T(N_PT_PAIR)=T_ARCH(I)
                  ELSE
                     WRITE(IU_ERR, '(/A)')
     &                  '*** ERROR: TOO MANY COMBINATIONS OF P AND  T.'
                     IERR=I_ERR_FATAL
                     RETURN
                  ENDIF
               ENDIF
         ENDDO
      ELSE
         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) THEN
               GOTO 2
            ENDIF
!
            IF (N_PT_PAIR.EQ.0) THEN
               IF (LOCK_CODE(.TRUE.)) THEN
                  INCLUDE 'batch_error.finc'
               ELSE
                  WRITE(IU_ERR, '(A/)')
     &               'AT LEAST ONE PAIR OF VALUES MUST BE SPECIFIED.'
                  GOTO 2
               ENDIF
            ENDIF
!
!        CLOSE THE FILE TO ALLOW THE UNIT TO BE RE-USED.
         CLOSE(IU_FILE_IN)
!
!        NOW SET THE AMOUNTS OF WATER VAPOUR.
!
!        INITIALIZE U TO 0.0.
         DO J=1, NPD_AMOUNT_CONTIN
            U(J)=0.0D+00
         ENDDO
         WRITE(IU_USER, '(/A)') 'ENTER THE NUMBER OF VALUES '
     &      //'OF AMOUNT AT WHICH DATA WILL BE GENERATED.'
4        READ(IU_STDIN, *, IOSTAT=IOS) N_AMOUNT
         IF (IOS.NE.0) THEN
            WRITE(IU_ERR, '(/A)') 'INVALID NUMBER OF AMOUNTS.'
            IF (LOCK_CODE(.TRUE.)) THEN
               INCLUDE 'batch_error.finc'
            ELSE
               WRITE(IU_ERR, '(A)') 'PLEASE RE-ENTER.'
               GOTO 4
            ENDIF
         ENDIF
         IF (N_AMOUNT.GT.NPD_AMOUNT_CONTIN) THEN
            IF (LOCK_CODE(.TRUE.)) THEN
               INCLUDE 'batch_error.finc'
            ELSE
               WRITE(IU_ERR, '(A)')
     &            '+++ NUMBER SPECIFIED IS TOO LARGE'
               WRITE(IU_ERR, '(A21, I5)')
     &            'MAXIMUM PERMITTED IS ', NPD_AMOUNT_CONTIN
               WRITE(IU_USER, '(A)')
     &            'PLEASE RE-SPECIFY.'
                  GOTO 4
            ENDIF
         ENDIF
         IF (N_AMOUNT.GT.1) THEN
            WRITE(IU_USER, '(/A, A)') 'ENTER THE MAXIMUM AND '
     &         //'MINIMUM AMOUNTS FOR ', NAME_ABSORB(IP_H2O)
5           READ(IU_STDIN, *, IOSTAT=IOS) AMOUNT_MIN, AMOUNT_MAX
            IF (IOS.NE.0) THEN
               IF (LOCK_CODE(.TRUE.)) THEN
                  INCLUDE 'batch_error.finc'
               ELSE
                  WRITE(IU_ERR, '(A)')
     &               '+++ ERRONEOUS RESPONSE PLEASE RE-ENTER.'
                  GOTO 5
               ENDIF
            ENDIF
            DO J=1, N_AMOUNT
               U(J)=EXP((1.0D+00/DBLE(N_AMOUNT-1))
     &            *(DBLE(J-1)*LOG(AMOUNT_MAX)
     &            +DBLE(N_AMOUNT-J)*LOG(AMOUNT_MIN)))
            ENDDO
         ELSE
            WRITE(IU_USER, '(/A, A)') 'ENTER THE '
     &         //'AMOUNT FOR ', NAME_ABSORB(IP_H2O)
6           READ(IU_STDIN, *, IOSTAT=IOS) U(1)
            IF (IOS.NE.0) THEN
               IF (LOCK_CODE(.TRUE.)) THEN
                  INCLUDE 'batch_error.finc'
               ELSE
                  WRITE(IU_ERR, '(A)')
     &               '+++ ERRONEOUS RESPONSE PLEASE RE-ENTER.'
                  GOTO 6
               ENDIF
            ENDIF
         ENDIF
!
      ENDIF
!
!
!     OBTAIN LIMITS ON BANDS.
      WRITE(IU_USER, '(/A)') 'ENTER FIRST AND LAST BANDS TO BE '
     &   //'CONSIDERED.'
7     READ(IU_STDIN, *, IOSTAT=IOS) I_BAND_FIRST, I_BAND_LAST
      IF (IOS.NE.0) THEN
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(A)')
     &         '+++ ERRONEOUS RESPONSE PLEASE RE-ENTER.'
            GOTO 7
         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
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(A)')
     &         '+++ RESPONSE OUT OF RANGE: PLEASE RE-ENTER.'
            GOTO 7
         ENDIF
      ENDIF
!
!     SET THE NUMBERS OF PARTIAL PRESSURES AT WHICH DATA 
!     WILL BE GENERATED.
      WRITE(IU_USER, '(/A)') 'ENTER THE NUMBER OF VALUES '
     &   //'OF PARTIAL PRESSURE AT WHICH DATA WILL BE GENERATED.'
8     READ(IU_STDIN, *, IOSTAT=IOS) N_PARTIAL_P
      IF (IOS.NE.0) THEN
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(A)')
     &         '+++ ERRONEOUS RESPONSE PLEASE RE-ENTER.'
            GOTO 8
         ENDIF
      ENDIF
      IF (N_PARTIAL_P.GT.NPD_PARTIAL_P) THEN
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_ERR, '(A)')
     &         '+++ RESPONSE IS TOO LARGE: PLEASE RE-ENTER.'
            WRITE(IU_ERR, '(A, I5)')
     &         'MAXIMUM PERMITTED IS ', NPD_PARTIAL_P
            GOTO 8
         ENDIF
      ENDIF
!
!
      RETURN
      END
