      SUBROUTINE SCWATM(QRC,CEWMASS,CIWMASS,CEN0,CEPSI,CEWETRB,CEMOM2, &
                        CEMOM3,CEMOM4,CEMOM5,CEMOM6,CEMOM7,CEMOM8, &
                        CIN0,CIPSI,CIWETRB,CIMOM2,CIMOM3,CIMOM4,CIMOM5, &
                        CIMOM6,CIMOM7,CIMOM8,ILGA,LEVA)
!-----------------------------------------------------------------------
!     PURPOSE:
!     --------
!     CALCULATION OF THE AMOUNT OF WATER IN THE AEROSOL OR CLOUD
!     DROPLET. THE NECESSARY MOMENTS FROM THE INPUT ARE CALCULATED
!     IN SCMOM. MAKE SURE THAT THE MOMENTS ARE CONSISTENT WITH THE
!     WET AEROSOL SIZES THAT ARE USED IN THIS IN THIS SUBROUTINE.
!
!     HISTORY:
!     --------
!     * APR 12/2006 - K.VONSALZEN   NEW.
!
!-----------------------------------------------------------------------
!
      USE SCPARM
      USE SDPARM
      USE SDPHYS
      USE FPDEF
!
      IMPLICIT REAL (A-H,O-Z), INTEGER (I-N)
!
      INTEGER, INTENT(IN) :: ILGA,LEVA
      REAL, INTENT(IN), DIMENSION(ILGA,LEVA,ISEXTB) :: CEWETRB
      REAL, INTENT(IN), DIMENSION(ILGA,LEVA,ISINTB) :: CIWETRB
      REAL, INTENT(IN), DIMENSION(ILGA,LEVA,ISEXT) :: CEN0,CEPSI
      REAL(R8), INTENT(IN), DIMENSION(ILGA,LEVA,ISEXT) :: &
                                          CEMOM2,CEMOM3,CEMOM4,CEMOM5, &
                                          CEMOM6,CEMOM7,CEMOM8
      REAL, INTENT(IN), DIMENSION(ILGA,LEVA,ISINT) :: CIN0,CIPSI
      REAL(R8), INTENT(IN), DIMENSION(ILGA,LEVA,ISINT) :: &
                                          CIMOM2,CIMOM3,CIMOM4,CIMOM5, &
                                          CIMOM6,CIMOM7,CIMOM8
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA) :: QRC
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA,ISEXT) :: CEWMASS
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA,ISINT) :: CIWMASS
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: CETMPB
      REAL(R8), ALLOCATABLE, DIMENSION(:,:,:) :: CEA1,CEA2,CEAA,CEAB, &
                                                 CEAC,CEAD,CEWT,CEWLOW, &
                                                 CEWHIG
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: CITMPB
      REAL(R8), ALLOCATABLE, DIMENSION(:,:,:) :: CIA1,CIA2,CIAA,CIAB, &
                                                 CIAC,CIAD,CIWT,CIWLOW, &
                                                 CIWHIG
!
!-----------------------------------------------------------------------
!     * ALLOCATE WORK ARRAYS.
!
      IF ( ISEXT > 0 ) THEN
        ALLOCATE(CEA1  (ILGA,LEVA,ISEXT))
        ALLOCATE(CEA2  (ILGA,LEVA,ISEXT))
        ALLOCATE(CEAA  (ILGA,LEVA,ISEXT))
        ALLOCATE(CEAB  (ILGA,LEVA,ISEXT))
        ALLOCATE(CEAC  (ILGA,LEVA,ISEXT))
        ALLOCATE(CEAD  (ILGA,LEVA,ISEXT))
        ALLOCATE(CEWLOW(ILGA,LEVA,ISEXT))
        ALLOCATE(CEWHIG(ILGA,LEVA,ISEXT))
        ALLOCATE(CEWT  (ILGA,LEVA,ISEXT))
      ENDIF
      IF ( ISEXTB > 0 ) THEN
        ALLOCATE(CETMPB(ILGA,LEVA,ISEXTB))
      ENDIF
      IF ( ISINT > 0 ) THEN
        ALLOCATE(CIA1  (ILGA,LEVA,ISINT))
        ALLOCATE(CIA2  (ILGA,LEVA,ISINT))
        ALLOCATE(CIAA  (ILGA,LEVA,ISINT))
        ALLOCATE(CIAB  (ILGA,LEVA,ISINT))
        ALLOCATE(CIAC  (ILGA,LEVA,ISINT))
        ALLOCATE(CIAD  (ILGA,LEVA,ISINT))
        ALLOCATE(CIWLOW(ILGA,LEVA,ISINT))
        ALLOCATE(CIWHIG(ILGA,LEVA,ISINT))
        ALLOCATE(CIWT  (ILGA,LEVA,ISINT))
      ENDIF
      IF ( ISINTB > 0 ) THEN
        ALLOCATE(CITMPB(ILGA,LEVA,ISINTB))
      ENDIF
!
!     * GROWTH FACTOR FOR EXTERNALLY MIXED AEROSOL TYPES.
!
      DO IS=1,ISEXTB
        CETMPB(:,:,IS)=MAX(CEWETRB(:,:,IS)/CEDRYRB(IS)-1.,0.)
      ENDDO
!
!     * GROWTH FACTOR FOR INTERNALLY MIXED AEROSOL TYPES.
!
      DO IS=1,ISINTB
        CITMPB(:,:,IS)=MAX(CIWETRB(:,:,IS)/CIDRYRB(IS)-1.,0.)
      ENDDO
!
!     * PARAMETERS FOR LINEAR INTERPOLATION OF GROWTH FACTORS
!     * FOR EXTERNALLY MIXED AEROSOL TYPES.
!
      DO IS=1,ISEXT
      DO L=1,LEVA
      DO IL=1,ILGA
        CEA2(IL,L,IS)=(CETMPB(IL,L,CEISVR(IS))-CETMPB(IL,L,CEISVL(IS))) &
                     /(CEDRYR(IS)%VR          -CEDRYR(IS)%VL          )
        CEA1(IL,L,IS)= &
                 CETMPB(IL,L,CEISVL(IS))-CEA2(IL,L,IS)*CEDRYR(IS)%VL
        AFACT=YCNST*RHOH2O*CEN0(IL,L,IS)*CEMOM3(IL,L,IS)
        AFLOW=AFACT*MAX((MIN(CETMPB(IL,L,CEISVR(IS)), &
                                 CETMPB(IL,L,CEISVL(IS))+1.)**3-1.),0.)
        CEWLOW(IL,L,IS)=AFLOW
        AFHIG=AFACT*MAX((MAX(CETMPB(IL,L,CEISVR(IS)), &
                                 CETMPB(IL,L,CEISVL(IS))+1.)**3-1.),0.)
        CEWHIG(IL,L,IS)=AFHIG
      ENDDO
      ENDDO
      ENDDO
      IF ( ISEXT > 0 ) THEN
        CEAA=3._R8*CEA1+3._R8*CEA1**2+CEA1**3
        CEAB=3._R8*CEA2*(1._R8+CEA1)**2
        CEAC=3._R8*(1._R8+CEA1)*CEA2**2
        CEAD=CEA2**3
      ENDIF
!
!     * PARAMETERS FOR LINEAR INTERPOLATION OF GROWTH FACTORS
!     * FOR INTERNALLY MIXED AEROSOL TYPES.
!
      DO IS=1,ISINT
      DO L=1,LEVA
      DO IL=1,ILGA
        CIA2(IL,L,IS)=(CITMPB(IL,L,CIISVR(IS))-CITMPB(IL,L,CIISVL(IS))) &
                     /(CIDRYR(IS)%VR          -CIDRYR(IS)%VL          )
        CIA1(IL,L,IS)= &
                 CITMPB(IL,L,CIISVL(IS))-CIA2(IL,L,IS)*CIDRYR(IS)%VL
        AFACT=YCNST*RHOH2O*CIN0(IL,L,IS)*CIMOM3(IL,L,IS)
        AFLOW=AFACT*MAX((MIN(CITMPB(IL,L,CIISVR(IS)), &
                                 CITMPB(IL,L,CIISVL(IS))+1.)**3-1.),0.)
        CIWLOW(IL,L,IS)=AFLOW
        AFHIG=AFACT*MAX((MAX(CITMPB(IL,L,CIISVR(IS)), &
                                 CITMPB(IL,L,CIISVL(IS))+1.)**3-1.),0.)
        CIWHIG(IL,L,IS)=AFHIG
      ENDDO
      ENDDO
      ENDDO
      IF ( ISINT > 0 ) THEN
        CIAA=3._R8*CIA1+3._R8*CIA1**2+CIA1**3
        CIAB=3._R8*CIA2*(1._R8+CIA1)**2
        CIAC=3._R8*(1._R8+CIA1)*CIA2**2
        CIAD=CIA2**3
      ENDIF
!
!     * WATER CONTENT FOR EXTERNALLY MIXED AEROSOLS.
!
      IF ( ISEXT > 0 ) THEN
        CEWT=YNA
        WHERE ( ABS(CEPSI-YNA) > YTINY )
          CEWT=YCNST*RHOH2O*CEN0 &
                     *(CEMOM3*CEAA+CEMOM4*CEAB+CEMOM5*CEAC+CEMOM6*CEAD)
        ENDWHERE
        WHERE ( ABS(CEWT-YNA) > YTINY )
          WHERE ( CEWLOW > CEWT )
            CEWT=CEWLOW
          ELSEWHERE ( CEWHIG < CEWT )
            CEWT=CEWHIG
          ENDWHERE
        ENDWHERE
        CEWMASS=CEWT
      ENDIF
!
!     * WATER CONTENT FOR INTERNALLY MIXED AEROSOLS.
!
      IF ( ISINT > 0 ) THEN
        CIWT=YNA
        WHERE ( ABS(CIPSI-YNA) > YTINY )
          CIWT=YCNST*RHOH2O*CIN0 &
                     *(CIMOM3*CIAA+CIMOM4*CIAB+CIMOM5*CIAC+CIMOM6*CIAD)
        ENDWHERE
        WHERE ( ABS(CIWT-YNA) > YTINY )
          WHERE ( CIWLOW > CIWT )
            CIWT=CIWLOW
          ELSEWHERE ( CIWHIG < CIWT )
            CIWT=CIWHIG
          ENDWHERE
        ENDWHERE
        CIWMASS=CIWT
      ENDIF
!
!     * SUM UP CONDENSED WATER.
!
      QRC=0.
      IF ( ISEXTB > 0 ) THEN
        DO IS=1,ISEXT
          WHERE ( ABS(CEWMASS(:,:,IS)-YNA) > YTINY )
            QRC=QRC+CEWMASS(:,:,IS)
          ENDWHERE
        ENDDO
      ENDIF
      IF ( ISINTB > 0 ) THEN
        DO IS=1,ISINT
          WHERE ( ABS(CIWMASS(:,:,IS)-YNA) > YTINY )
            QRC=QRC+CIWMASS(:,:,IS)
          ENDWHERE
        ENDDO
      ENDIF
!
!     * DEALLOCATE WORK ARRAYS.
!
      IF ( ISEXT > 0 ) THEN
        DEALLOCATE(CEA1)
        DEALLOCATE(CEA2)
        DEALLOCATE(CEAA)
        DEALLOCATE(CEAB)
        DEALLOCATE(CEAC)
        DEALLOCATE(CEAD)
        DEALLOCATE(CEWT)
      ENDIF
      IF ( ISEXTB > 0 ) THEN
        DEALLOCATE(CETMPB)
        DEALLOCATE(CEWLOW)
        DEALLOCATE(CEWHIG)
      ENDIF
      IF ( ISINT > 0 ) THEN
        DEALLOCATE(CIA1)
        DEALLOCATE(CIA2)
        DEALLOCATE(CIAA)
        DEALLOCATE(CIAB)
        DEALLOCATE(CIAC)
        DEALLOCATE(CIAD)
        DEALLOCATE(CIWT)
      ENDIF
      IF ( ISINTB > 0 ) THEN
        DEALLOCATE(CITMPB)
        DEALLOCATE(CIWLOW)
        DEALLOCATE(CIWHIG)
      ENDIF
!
      END SUBROUTINE SCWATM
