!+ Subroutine to find a set matching reference conditions.
!
! Method:
!	The sets of transmissiona available are examined to see which
!	matches the reference conditions most closely. The set must
!	refer to the same gas and band: the pressure and temperature
!	are selected as those which have the smallest relative
!	difference from the reference values.
!
! 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 FIND_SET(IERR, I_BAND, I_ABSORB, I_TYPE_ABSORB
     &   , P_REFERENCE, T_REFERENCE
     &   , N_SET, BAND_SET, INDEX_SET, T_SET, P_SET
     &   , I_SET, N_OTHER, LIST_OTHER)
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(OUT)
     &     I_SET
!             NUMBER OF SELECTED SET
     &   , N_OTHER
!             NUMBER OF OTHER SETS WITH SAME BAND AND GAS
     &   , LIST_OTHER(NPD_SET_SCALE)
!             LIST OF OTHER SETS
      INTEGER	!, INTENT(IN)
     &     I_BAND
!             NUMBER OF BAND
     &   , I_ABSORB
!             INDEX OF ABSORBER
     &   , I_TYPE_ABSORB
!             ACTUAL TYPE OF ABSORBER
     &   , N_SET
!             NUMBER OF SETS
     &   , BAND_SET(NPD_SET)
!             NUMBER OF BANDS
     &   , INDEX_SET(NPD_SET)
!             ABSORBER INDICES OF SETS
      REAL*8	!, INTENT(IN)
     &     P_REFERENCE
!             REFERENCE PRESSURE
     &   , T_REFERENCE
!             REFERENCE TEMPERATURE
     &   , T_SET(NPD_SET)
!             TEMPERATURES OF SETS
     &   , P_SET(NPD_SET)
!             PRESSURES OF SETS
!
!     LOCAL VARIABLES
      INTEGER
     &     I
!             LOOP VARIABLE
      REAL*8
     &     DELTA_MIN
!             MINIMUM L2 NORM SO FAR
     &   , DELTA_MIN_NEW
!             NEW L2 NORM
!
!
!     LOOP THROUGH THE SETS FINDING THAT WHICH MATCHES THE REQUIREMENTS
!     MOST CLOSELY.
      I_SET=0
      DELTA_MIN=1.0D+00
      DO I=1, N_SET
         IF (BAND_SET(I).EQ.I_BAND) THEN
            IF (INDEX_SET(I).EQ.I_ABSORB) THEN
               DELTA_MIN_NEW=SQRT(0.5D+00*
     &            ((P_SET(I)/P_REFERENCE-1.0D+00)**2
     &            +(T_SET(I)/T_REFERENCE-1.0D+00)**2))
               IF ( (DELTA_MIN_NEW.LT.DELTA_MIN)
     &              .OR.(I_SET.EQ.0) ) THEN
!                 THIS SET APPROXIMATES THE REFERENCE DATA BEST.
!                 ANY SET IN THE RIGHT BAND FOR THE RIGHT GAS WILL
!                 BE BETTER THAN A NULL SET.
                  I_SET=I
                  DELTA_MIN=DELTA_MIN_NEW
               ENDIF
            ENDIF
         ENDIF
      ENDDO
!
!     IF THERE IS NO DATA FOR THIS BAND AND GAS NO FIT IS PERFORMED.
      IF (I_SET.EQ.0) RETURN
!
!     NOW ASSEMBLE THE LIST OF OTHER SETS OF DATA IN THE SAME BAND FOR
!     THIS SAME GAS.
      N_OTHER=0
      DO I=1, N_SET
         IF ( (BAND_SET(I).EQ.I_BAND).AND.
     &       (INDEX_SET(I).EQ.I_ABSORB).AND.
     &       (I.NE.I_SET) ) THEN
            IF (N_OTHER.LT.NPD_SET_SCALE) THEN
               N_OTHER=N_OTHER+1
               LIST_OTHER(N_OTHER)=I
            ELSE
               WRITE(IU_ERR, '(/A, I5, A, I5)')
     &            '*** ERROR: THERE ARE TOO MANY SETS OF DATA FOR '
     &            //'SPECIES ', I_TYPE_ABSORB, ' IN BAND ', I_BAND
               IERR=I_ERR_FATAL
               RETURN
            ENDIF
         ENDIF
      ENDDO
!
!
      RETURN
      END
