!+ Subroutine to sort cases into groups.
!
! Method:
!	The conditions of each case for which LOWTRAN was run are
!	examined and lists of those for the same pressure, temperature
!	and band are assembled.
!
! 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 SORT_GROUP(IERR
     &   , N_CASE, BAND_CASE, P_CASE, T_CASE
     &   , N_GROUP
     &   , BAND_GROUP, P_GROUP, T_GROUP
     &   , GROUP_CASE
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/lowtran_parameters.ecf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision_pp.tcf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
     &   , N_GROUP
!             NUMBER OF GROUPS FOUND
     &   , GROUP_CASE(NPD_CASE)
!             GROUPS TO WHICH CASES BELONG
      INTEGER	!, INTENT(IN)
     &     N_CASE
!             NUMBER OF CASES
     &   , BAND_CASE(NPD_CASE)
!             ARRAY OF BANDS
      REAL*8	!, INTENT(IN)
     &     P_CASE(NPD_CASE)
!             PRESSURES FOR CASES
     &   , T_CASE(NPD_CASE)
!             TEMPERATURES
      REAL*8	!, INTENT(OUT)
     &     P_GROUP(NPD_GROUP)
!             PRESSURES FOR EACH GROUP
     &   , T_GROUP(NPD_GROUP)
!             TEMPERATURES FOR EACH GROUP
!
!     LOCAL VARIABLES.
      INTEGER
     &     BAND_GROUP(NPD_GROUP)
!             NUMBERS OF BANDS
     &   , I_P
!             PRESSURE INDEX
     &   , I_T
!             TEMPERATURE INDEX
     &   , I_B
!             BAND INDEX
     &   , I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
!
!
!     ENTER THE FIRST ELEMENTS OF THE PRESSURE AND TEMPERATURE ARRAYS.
      N_GROUP=1
      P_GROUP(1)=P_CASE(1)
      T_GROUP(1)=T_CASE(1)
      BAND_GROUP(1)=BAND_CASE(1)
      GROUP_CASE(1)=1
!
!     LOOP THROUGH THE CASES GROUPING SIMILAR SETS OF DATA.
      DO I=2, N_CASE
!        CHECK WHETHER THESE DATA FALL IN A GROUP ALREADY FOUND.
         J=1
         I_P=0
         I_T=0
         I_B=0
1           IF (ABS(P_CASE(I)/P_GROUP(J)-1.0D+00).LT.TOL_PRESS_PP) THEN
               I_P=J
            ENDIF
            IF (ABS(T_CASE(I)/T_GROUP(J)-1.0D+00).LT.TOL_TEMP_PP) THEN
               I_T=J
            ENDIF
            IF (BAND_CASE(I).EQ.BAND_GROUP(J)) THEN
               I_B=J
            ENDIF
!
!        IF ALL THE DATA ARE FOUND IN THE SAME EXISTING GROUP WE ADD THE
!        CASE NUMBER TO THE LIST FOR THAT GROUP.
         IF ( (I_P.NE.0).AND.(I_P.EQ.I_T).AND.(I_P.EQ.I_B) ) THEN
            GROUP_CASE(I)=I_P
         ELSE IF (J.LT.N_GROUP) THEN
!           CHECK AGAINST NEXT CASE.
            J=J+1
            GOTO 1
         ELSE
!           THIS CASE DOES NOT BELONG TO AN EXISTING GROUP: INCREASE THE
!           NUMBER OF GROUP CONSIDERED.
            N_GROUP=N_GROUP+1
!           TEST AGAINST THE NUMBER OF GROUPS ALLOWED.
            IF (N_GROUP.GT.NPD_GROUP) THEN
               WRITE(IU_ERR, '(/A)')
     &            '***ERROR: TOO MANY GROUPS OF DATA ARE REQUIRED.'
               IERR=I_ERR_FATAL
               RETURN
            ENDIF
            GROUP_CASE(I)=N_GROUP
            P_GROUP(N_GROUP)=P_CASE(I)
            T_GROUP(N_GROUP)=T_CASE(I)
            BAND_GROUP(N_GROUP)=BAND_CASE(I)
         ENDIF
!
      ENDDO
!
!
      RETURN
      END
