!+ Subroutine to aggregate clouds into regions.
!
! Method:
!	The clouds in a layer are combined in groups to form regions
!       which will be considered as bulk entities in the solution of the 
!       equation of transfer. The extents of these regions are also
!       determined.
!
! 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 AGGREGATE_CLOUD(IERR
     &   , N_PROFILE, N_LAYER, N_CLOUD_TOP
     &   , I_CLOUD, I_CLOUD_REPRESENTATION, N_CLOUD_TYPE
     &   , FRAC_CLOUD
     &   , I_REGION_CLOUD, FRAC_REGION
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUSION OF HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/dimensions_fixed.pcf'
      INCLUDE 'incl/cloud_representation.pcf'
      INCLUDE 'incl/cloud_type.pcf'
      INCLUDE 'incl/cloud_region.pcf'
      INCLUDE 'incl/cloud_scheme.pcf'
      INCLUDE 'incl/phase.pcf'
!
!     DUMMY VARIABLES.
      INTEGER   !, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER   !, INTENT(IN)
     &     N_PROFILE
!             NUMBER OF PROFILES
     &   , N_LAYER
!             NUMBER OF LAYERS
     &   , N_CLOUD_TOP
!             TOPMOST CLOUDY LAYER
      INTEGER	!, INTENT(IN)
     &     I_CLOUD
!             CLOUD SCHEME USED
     &   , I_CLOUD_REPRESENTATION
!             REPRESENTATION OF CLOUDS USED
     &   , N_CLOUD_TYPE
!             NUMBER OF TYPES OF CLOUD
!
      REAL*8	!, INTENT(OUT)
     &     FRAC_CLOUD(NPD_PROFILE, NPD_LAYER, NPD_CLOUD_TYPE)
!             FRACTIONS OF EACH TYPE OF CLOUD
!
      INTEGER	!, INTENT(OUT)
     &     I_REGION_CLOUD(NPD_CLOUD_TYPE)
!             REGIONS IN WHICH PARTICULAR TYPES OF CLOUD FALL
      REAL*8	!, INTENT(OUT)
     &     FRAC_REGION(NPD_PROFILE, NPD_LAYER, NPD_REGION)
!             FRACTIONS OF TOTAL CLOUD OCCUPIED BY EACH REGION
!
!
!     LOCAL VARIABLES
      INTEGER
     &     I
!            LOOP VARIABLE 
     &   , L
!            LOOP VARIABLE 
     &   , K
!            LOOP VARIABLE 
!
!
!
      IF (I_CLOUD.EQ.IP_CLOUD_TRIPLE) THEN
!
         IF (I_CLOUD_REPRESENTATION.EQ.IP_CLOUD_CSIW) THEN
!
            DO K=1, N_CLOUD_TYPE
               IF (K.EQ.IP_CLOUD_TYPE_SW) THEN
                  I_REGION_CLOUD(K)=IP_REGION_STRAT
               ELSE IF (K.EQ.IP_CLOUD_TYPE_SI) THEN
                  I_REGION_CLOUD(K)=IP_REGION_STRAT
               ELSE IF (K.EQ.IP_CLOUD_TYPE_CW) THEN
                  I_REGION_CLOUD(K)=IP_REGION_CONV
               ELSE IF (K.EQ.IP_CLOUD_TYPE_CI) THEN
                  I_REGION_CLOUD(K)=IP_REGION_CONV
               ENDIF
            ENDDO
!
            DO I=N_CLOUD_TOP, N_LAYER
               DO L=1, N_PROFILE
                  FRAC_REGION(L, I, IP_REGION_STRAT)
     &               =FRAC_CLOUD(L, I, IP_CLOUD_TYPE_SW)
     &               +FRAC_CLOUD(L, I, IP_CLOUD_TYPE_SI)
                  FRAC_REGION(L, I, IP_REGION_CONV)
     &               =FRAC_CLOUD(L, I, IP_CLOUD_TYPE_CW)
     &               +FRAC_CLOUD(L, I, IP_CLOUD_TYPE_CI)
               ENDDO
            ENDDO
!
         ELSE IF (I_CLOUD_REPRESENTATION.EQ.IP_CLOUD_CONV_STRAT) THEN
!
            DO K=1, N_CLOUD_TYPE
               IF (K.EQ.IP_CLOUD_TYPE_STRAT) THEN
                  I_REGION_CLOUD(K)=IP_REGION_STRAT
               ELSE IF (K.EQ.IP_CLOUD_TYPE_CONV) THEN
                  I_REGION_CLOUD(K)=IP_REGION_CONV
               ENDIF
            ENDDO
!
            DO I=N_CLOUD_TOP, N_LAYER
               DO L=1, N_PROFILE
                  FRAC_REGION(L, I, IP_REGION_STRAT)
     &               =FRAC_CLOUD(L, I, IP_CLOUD_TYPE_STRAT)
                  FRAC_REGION(L, I, IP_REGION_CONV)
     &               =FRAC_CLOUD(L, I, IP_CLOUD_TYPE_CONV)
               ENDDO
            ENDDO
!
!
         ELSE
            WRITE(IU_ERR, '(/A)') 
     &         '*** ERROR: THIS REPRESENTATION OF CLOUDS IS NOT '
     &         //'COMPATIBLE WITH THE TRIPLE OVERLAP.'
            IERR=I_ERR_FATAL
            RETURN
         ENDIF
!
      ENDIF
!
!
!
      RETURN
      END   
