!+ Subroutine to split the atmosphere into maximally overlapped columns.
!
! Method:
!	The layers are first ranked in order of increasing cloudiness.
!	This operation cannot be vectorized and is done for one profile
!	at a time. The areal extent of each column and the logical
!	cloud mask are then 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 SPLIT_MAXIMUM(N_PROFILE, N_LAYER
     &   , W_CLOUD
     &   , N_COLUMN, AREA_COLUMN, L_COLUMN
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision.pcf'
!
!     DUMMY ARGUMENTS
      INTEGER 	!, INTENT(IN)
     &     N_PROFILE
!             NUMBER OF PROFILES
     &   , N_LAYER
!             NUMBER OF LAYERS
      INTEGER 	!, INTENT(INOUT)
     &     N_COLUMN(NPD_PROFILE)
!             NUMBER OF COLUMNS
      LOGICAL 	!, INTENT(IN)
     &     L_COLUMN(NPD_PROFILE, NPD_LAYER, NPD_COLUMN)
!             ARRAY OF TYPES
      REAL*8 	!, INTENT(IN)
     &     AREA_COLUMN(NPD_PROFILE, NPD_COLUMN)
!             AREA OF EACH COLUMN
     &   , W_CLOUD(NPD_PROFILE, NPD_LAYER)
!             CLOUD AMOUNTS
!
!     LOCAL ARGUMENTS
      INTEGER
     &     IRANK(NPD_LAYER)
!             ARRAY TO RANK COLUMNS BY W
     &   , I
!             LOOP VARIABLE
     &   , K
!             LOOP VARIBLE
     &   , L
!             LOOP VARIBLE
      REAL*8
     &     W_CLOUD_SINGLE(NPD_LAYER)
!             CLOUD AMOUNTS FOR SINGLE PROFILE
     &   , W
!             SINGLE CLOUD AMOUNT
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     RANK
!
!
!
      DO L=1, N_PROFILE
!        GATHER THE CLOUD AMOUNTS FOR ONE PROFILE
         DO I=1, N_LAYER
            W_CLOUD_SINGLE(I)=W_CLOUD(L, I)
         ENDDO
!
!        FIRST FORM THE VECTOR IRANK, RANKING THE LAYERS IN ORDER OF
!        INCREASING CLOUD CONTENT.
         CALL RANK(N_LAYER
     &      , W_CLOUD_SINGLE, IRANK
     &      )
!
!        PASS THROUGH ALL THE COLUMNS SETTING L_COLUMN EQUAL TO .FALSE.
!        IF THE COLUMN IS ACTUALLY CLEAR ON THAT LEVEL. THE ASSUMPTION 
!        OF MAXIMUM OVERLAP IS USED HERE.
         N_COLUMN(L)=1
         W=0.0D+00
         I=1
30       IF (I.LE.N_LAYER) THEN
            IF ( W_CLOUD_SINGLE(IRANK(I)).LT.(W+TOL_TEST) ) THEN
               I=I+1
               GOTO 30
            ELSE
               DO K=1, I-1
                  L_COLUMN(L, IRANK(K), N_COLUMN(L))=.FALSE.
               ENDDO
               DO K=I, N_LAYER
                  L_COLUMN(L, IRANK(K), N_COLUMN(L))=.TRUE.
               ENDDO
               AREA_COLUMN(L, N_COLUMN(L))
     &            =W_CLOUD_SINGLE(IRANK(I))-W
               W=W_CLOUD_SINGLE(IRANK(I))
            ENDIF
            IF (W.LT.W_CLOUD_SINGLE(IRANK(N_LAYER))-TOL_TEST) THEN
               N_COLUMN(L)=N_COLUMN(L)+1
               GOTO 30
            ENDIF
         ENDIF
!
!        THERE IS A TOTALLY CLEAR COLUMN UNLESS AT LEAST ONE LAYER IS
!        TOTALLY CLOUDY. 
         IF ((1.0D+00-W).GT.TOL_TEST) THEN
!           INCREMENT THE NUMBER OF COLUMNS IF THE FIRST IS NOT BLANK.
            IF (W.GE.TOL_TEST) THEN
               N_COLUMN(L)=N_COLUMN(L)+1
            ENDIF
            DO K=1, N_LAYER
               L_COLUMN(L, IRANK(K), N_COLUMN(L))=.FALSE.
            ENDDO
            AREA_COLUMN(L, N_COLUMN(L))=1.0D+00-W
         ENDIF
!
      ENDDO
!
!
      RETURN
      END
