SUBROUTINE m7_sedimentation &
  &( KIDIA   , KFDIA , KLON, KLEV, & ! ECMWF indices
  &  pxtm1   , pxtte ,             & !   "   tracers
  &  ptp1    ,                     & !   "   temperature
  &  pqp1    ,                     & !   "   humidity
  &  papp1   , paphp1,             & !   "   pressure (full/half levels)
  &  prhoa   , pdensaer, prwet,    & !
  &  PTSPHY  , PAERODDF            &
  &)
                            
  !     -----------------------------------------------------------------------
  !
  !     **** sedimentation  -  calculation of sedimentation velocities
  !     
  !     Authors:
  !     --------
  !     Michael Schulz, LSCE                           5.2.2001
  !     Philip Stier, MPI    (adaption to ECHAM5/HAM)      2002
  !
  !     JJMorcrette, 20091112, Adaptation to ECMWF IFS
  !
  !     interface
  !     ---------
  !      input 
  !       pressure difference and midpoint pressure
  !       temperature
  !       accumulated tracer mixing ratios 
  !       old sedimentation flux
  !      output 
  !       accumulated tracer mixing ratios 
  !       new sedimentation flux to be found in AEROSOL_PROGNOS fields
  !
  !     method
  !     ------
  !     
  !     returns the changed tracer mixing ratio 
  !     expects that the aerosol tracers are in the tracer fields
  !     in the order explained in the aerosol module
  !
  !     Loop is done per aerosol mode, which shall be affected by sedimentation
  !     Modes treated must be "behind" each other in the tracer field
  !     and are identified by mode names such as cimode,csmode...
  !
  !     References:
  !     ----------=
  !     Seinfeld & Pandis (1998)
  !     -----------------------------------------------------------------------
  !
  !     INPUT ARGUMENTS.
  !     ----- ----------
  !    Variables in routine    
  !  zxtp1     : tracer mixing ratio including tendency

!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM  , JPRB
USE YOMHOOK   ,ONLY : LHOOK , DR_HOOK

!  USE mo_kind,         ONLY: dp
!  USE mo_time_control, ONLY: time_step_len,        delta_time
!  USE mo_tracer,       ONLY: ntrac,                trlist,    &
!                             AEROSOLNUMBER,        AEROSOLMASS
!  USE mo_constants,    ONLY: rd,        vtmpc1,    g, tmelt
!  USE mo_aero_m7,      ONLY: sigma,     sigmaln,   cmedr2mmedr
!  USE mo_aero_mem,     ONLY: sed, vsediment, rwet, densaer

!USE YOMCST    ,ONLY : RD, RG, RTT 
USE YOMCST    ,ONLY : RG, RTT 
!USE YOETHF    ,ONLY : RVTMP2
USE YO_AERO_M7,ONLY : ntrac, nmod, nmode, &
  &                   sigma, sigmaln,     &
  &                   cmedr2mmedr, cphas, &
  &                   lsedi

USE YOM_YGFL  ,ONLY : NACTAERO 
 
  IMPLICIT NONE

!--- Arguments:
  
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA, KFDIA, KLON, KLEV
REAL(KIND=JPRB),INTENT(IN) :: ptp1(klon ,klev)        , pqp1(klon ,klev)      ,&
  &                           papp1(klon ,klev)       , paphp1(klon ,klev+1)  ,&
  &                           pdensaer(klon,klev,nmod), prhoa(klon ,klev)     ,&
  &                           prwet(klon,klev,nmod)
REAL(KIND=JPRB),INTENT(IN) :: PTSPHY

REAL(KIND=JPRB),INTENT(IN) :: pxtm1(klon ,klev,ntrac)
  
REAL(KIND=JPRB),INTENT(INOUT) :: pxtte(klon ,klev,ntrac), PAERODDF(klon,NACTAERO,8)

!--- Diagnostic stream:

!REAL(KIND=JPRB),      POINTER :: rwet_p(:,:,:)
!REAL(KIND=JPRB),      POINTER :: densaer_p(:,:,:)
!REAL(KIND=JPRB),      POINTER :: sed_p(:,:)
!REAL(KIND=JPRB),      POINTER :: vsediment_p(:,:)

!--- Local variables:

INTEGER(KIND=JPIM) :: jk, jt, imod
REAL(KIND=JPRB) :: zslinnfac

REAL(KIND=JPRB) :: zmd(klon ,klev),      zvt(klon ,klev),     &
  &                ztp1(klon ,klev),     ztempc(klon ,klev),  &
  &                zvis(klon ,klev),     zlair(klon ,klev),   &
  &                zdensair(klon ,klev), zqp1(klon ,klev),    &
  &                zdpg(klon ,klev),     zdz(klon ,klev),     &
  &                zsedtend(klon ,klev), zsedflux(klon ,klev)
             

REAL(KIND=JPRB) :: zxtp1(klon ,klev,ntrac)


REAL(KIND=JPRB) :: ZHOOK_HANDLE
!     ----------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('M7_SEDIMENTATION',0,ZHOOK_HANDLE)

  !--- 0) Initialisations:

  !--- Calculate tracer mixing ratios at t=t+1:

  zxtp1(kidia:kfdia,:,:)=pxtm1(kidia:kfdia,:,:)+pxtte(kidia:kfdia,:,:)*PTSPHY


  !--- 1) Calculate meteorological/thermodynamical parameters:
  
  !--- Temperature in °C at t=t+1:

  ztp1(kidia:kfdia,:)= ptp1(kidia:kfdia,:)
  ztempc(kidia:kfdia,:) = ztp1(kidia:kfdia,:) - RTT

  !---  Dynamic viscosity of air after Prup.Klett: zvis [Pa s]

  WHERE(ztempc(kidia:kfdia,:) >= 0._JPRB)
     zvis(kidia:kfdia,:)=(1.718_JPRB + 0.0049_JPRB*ztempc(kidia:kfdia,:))*1.E-5_JPRB
  ELSEWHERE
     zvis(kidia:kfdia,:)=(1.718_JPRB + 0.0049_JPRB*ztempc(kidia:kfdia,:) - 1.2E-05_JPRB*(ztempc(kidia:kfdia,:)**2))*1.E-5_JPRB
  ENDWHERE

  !--- Mean free path of air after Prupp. Klett: zlair [10^-6 m]

  zlair(kidia:kfdia,:) = 0.066_JPRB *(1.01325E+5_JPRB/papp1(kidia:kfdia,:))*(ztp1(kidia:kfdia,:)/293.15_JPRB)*1.E-06_JPRB

  !--- Density of air:

  zqp1(kidia:kfdia,:)=pqp1(kidia:kfdia,:)

  zdensair(kidia:kfdia,:)=prhoa(kidia:kfdia,:)

  !--- Air mass auxiliary variable:

  !    Uppermost level:

  zdpg(kidia:kfdia,1)=2._JPRB*(paphp1(kidia:kfdia,2)-papp1(kidia:kfdia,1))/RG

  !    Other levels:

  DO jk=2, klev
     
     zdpg(kidia:kfdia,jk)=(paphp1(kidia:kfdia,jk+1)-paphp1(kidia:kfdia,jk))/RG

  ENDDO

  !--- Layer thickness:

  zdz(kidia:kfdia,:)=zdpg(kidia:kfdia,:)/zdensair(kidia:kfdia,:)


  !--- 2) Calculate sedimentation:

  DO jt = 1, ntrac

     zsedflux(kidia:kfdia,1:klev)=0._JPRB


     !--- Lookup mode of the tracer:
        
!     imod=trlist%ti(jt)%mode
     imod = nmode(jt)

!     IF (trlist%ti(jt)%nsedi/=0) THEN 
     IF (lsedi(jt)) THEN

!        rwet_p      => rwet(imod)%ptr
!        densaer_p   => densaer(imod)%ptr
!        sed_p       => sed(jt)%ptr
!        vsediment_p => vsediment(jt)%ptr

        !--- Select diameter and limit it to maximal 50 um:
        !
        !    If tracer is:
        !
        !       aerosol number mixing ratio: use number median radius
        !       aerosol mass   mixing ratio: use mass   median radius

!        IF (trlist%ti(jt)%nphase==AEROSOLNUMBER) THEN 
        IF (cphas(jt) == 'ANU') THEN

           zmd(kidia:kfdia,:)=MIN( prwet(kidia:kfdia,:,imod)*2._JPRB ,  50.E-6_JPRB)

!        ELSE IF (trlist%ti(jt)%nphase==AEROSOLMASS) THEN
        ELSEIF (cphas(jt) == 'AMS') THEN

           zmd(kidia:kfdia,:)=MIN( prwet(kidia:kfdia,:,imod)*cmedr2mmedr(imod)*2._JPRB , 50.E-6_JPRB)

        ENDIF

        !--- Slinn correction for sedimentation velocity of a 
        !    size distribution with a given sigma:

        zslinnfac=sigma(imod)**(2._JPRB*sigmaln(imod))


        WHERE (zmd(kidia:kfdia,:) > 0._JPRB) 

           !--- Stokes-velocity (S&P, Equation 8.42):

           zvt(kidia:kfdia,:)=2._JPRB/9._JPRB*(pdensaer(kidia:kfdia,:,imod)-zdensair(kidia:kfdia,:)) * &
             &                RG/zvis(kidia:kfdia,:)*(zmd(kidia:kfdia,:)/2._JPRB)**2._JPRB

           !--- With cunnigham slip- flow correction (S&P, Equation 8.34):

           zvt(kidia:kfdia,:) = zvt(kidia:kfdia,:) * zslinnfac       *                        &
             &                  (1._JPRB+ 1.257_JPRB*zlair(kidia:kfdia,:)/zmd(kidia:kfdia,:)*2._JPRB +  &
             &                  0.4_JPRB*zlair(kidia:kfdia,:)/zmd(kidia:kfdia,:)*2._JPRB *        &
             &                  EXP(-1.1_JPRB/(zlair(kidia:kfdia,:)/zmd(kidia:kfdia,:)*2._JPRB)) )

           !--- Calculate sedimentation in terms of mixing ratio tendencies:

           !--- Limit loss to the content of box:
           !    Multilayer crossing is not realised here as sedimentation 
           !    velocity is in effect limited to dz/dt (grid velocity).
           !    
           !    zvt=dz(dep)/dt

           !--- Limit zvt to the grid velocity dz(grid)/dt (no multilayer crossing):

           zvt(kidia:kfdia,:)=MIN( zvt(kidia:kfdia,:) , zdz(kidia:kfdia,:)/PTSPHY )

           !--- Loss in terms of mixing ratio tendency:
           
           zsedtend(kidia:kfdia,:)=MAX(0._JPRB,MIN(zxtp1(kidia:kfdia,:,jt)/PTSPHY, zxtp1(kidia:kfdia,:,jt) * &
             &                                   zvt(kidia:kfdia,:)/zdz(kidia:kfdia,:) ))

           !--- Apply loss throughout the column of mixing ratio tendencies:

           pxtte(kidia:kfdia,:,jt)= pxtte(kidia:kfdia,:,jt)-zsedtend(kidia:kfdia,:)

           !--- Transfer loss from mixing ratio tendency [kg/kg] 
           !    to sedimentation flux [kg m-2 s-1]:

           zsedflux(kidia:kfdia,:) = zsedtend(kidia:kfdia,:) *zdpg(kidia:kfdia,:)

        ELSEWHERE

           zsedflux(kidia:kfdia,:) = 0._JPRB 
           zvt(kidia:kfdia,:) =0._JPRB

        ENDWHERE


        !--- Re-convert sedimentation flux and add it to the mixing ratio tendency
        !    of the box below (conversion with zdpg of the box below): 
        
        pxtte(kidia:kfdia,2:klev,jt) = pxtte(kidia:kfdia,2:klev,jt) &
          & + (zsedflux(kidia:kfdia,1:(klev-1))/zdpg(kidia:kfdia,2:klev))

        !--- Store sedimentation in [kg/m2] for diagnostics:
        !    Note: diagnostics has to be integrated over
        !          delta_time not PTSPHY

!        sed_p(kidia:kfdia,krow)=sed_p(kidia:kfdia,krow)+zsedflux(kidia:kfdia,klev)*delta_time

        !--- Store sedimentation velocity for diagnostics:

!        vsediment_p(kidia:kfdia,krow)=zvt(kidia:kfdia,klev)

     ENDIF ! nsedi /=0

     PAERODDF(KIDIA:KFDIA,JT,3) = zsedflux(kidia:kfdia,klev)

  ENDDO !jt=1, ntrac

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('M7_SEDIMENTATION',1,ZHOOK_HANDLE)
END SUBROUTINE m7_sedimentation
