SUBROUTINE M7_DRYDEP &
!---input
  &( KIDIA , KFDIA, KLON  , KLEV , KTILES, &
  &  LDLAND, & 
  &  PAPH  , PCI  , PDENS , PFRTI, PFWMLT, PLSM , PSNS, &
  &  PTIA  , PTS  , PTSPHY, PWS1 , PWSMX , PXTM1, &
!-- in/output
  &  PFLXM7 &
  & )

!**** *M7_DRYDEP* -  ROUTINE FOR PARAMETRIZATION OF DRY DEPOSITION

!     PURPOSE.
!     --------
!        Dry deposition is (simply) represented by a modification of the 
!        instantaneous surface flux by what comes down from layer just 
!        above the surface

!**** *M7_DRYDEP* -  ROUTINE FOR PARAMETRIZATION OF DRY DEPOSITION

!     METHOD.
!     -------
!       Rewritten and adapted to ECMWF-IFS, JJMorcrette 20100115 
!       Originally from M7 *xt_drydep* and *xt_drydep_presc*
!
!     Authors:
!     --------
!        H. Feicher, MPI    2001/2002
!        P. Stier  , MPI    2001/2002 
 
!**   INTERFACE.
!     ----------
!        CALL *M7_DRYDEP* FROM *M7_EMI*.

! INPUTS:
! -------

! OUTPUTS:
! --------
! PFLXM7(KLON,nm7aer)    : SURFACE FLUX              (xx m-2)

!     EXTERNALS.
!     ----------
!          NONE

!     MODIFICATIONS.
!     -------------

!     SWITCHES.
!     --------

!     MODEL PARAMETERS
!     ----------------

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

USE YO_AERO_M7,ONLY : ndryd, ntrac, nmode, nm7aer, cphas, ctrac

USE YOMCST   , ONLY : RG, RTT
USE YOS_SOIL , ONLY : RTFREEZSICE, RWCAP

IMPLICIT NONE

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

!*       0.1  ARGUMENTS
!             ---------

!---input fields
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA, KFDIA, KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTILES 

LOGICAL           ,INTENT(IN)    :: LDLAND(KLON)

REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH(KLON,0:KLEV), PDENS(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCI(KLON) , PFRTI(KLON,KTILES), PFWMLT(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLSM(KLON), PSNS(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTIA(KLON), PTS(KLON) , PWS1(KLON), PWSMX(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXTM1(KLON,KLEV,nm7aer)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSPHY

!---in/output fields
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFLXM7(KLON,nm7aer) 

!*       0.5  LOCAL VARIABLES
!  	      ---------------

INTEGER(KIND=JPIM) :: JL, JT

!LOGICAL :: LLPRINT

REAL(KIND=JPRB) :: zcsncri, zvdcoarse, zvdaitken, zvd2ice, zvd4ice, zvd2nof, zvd4nof &
  &              , zvwc2, zvw02, zvwc4, zvw04
REAL(KIND=JPRB) :: zdz(KLON), zmaxvdry(KLON), ZRAT(KLON), zvdrd(KLON,2), zvdrydep(KLON) &
  &              , zdrydepflx(klon), zwcap

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

zcsncri = 5.85036E-3_JPRB   ! m water equivalent critical snow depth
zvdcoarse = 0.01_JPRB       ! V-dep coarse mode [m s-1]
zvdaitken = 0.01_JPRB       ! V-dep aitken mode [m s-1]

zvwc2 = (0.8E-02_JPRB - 0.2E-02)/(1._JPRB - 0.9_JPRB)
zvw02 = zvwc2 - 0.8E-2_JPRB
zvwc4 = (0.2E-02_JPRB - 0.025E-02)/(1._JPRB - 0.9_JPRB)
zvw04 = zvwc4 - 0.2E-2_JPRB

zwcap = RWCAP

DO JL=KIDIA,KFDIA
  zmaxvdry(JL)=(paph(JL,KLEV)-paph(JL,KLEV-1)) &
    &         /(RG*PDENS(JL,KLEV)*PTSPHY)
  ZRAT(JL)=PWS1(JL)/PWSMX(JL)
  zdz(JL) =(PAPH(JL,KLEV)-PAPH(JL,KLEV-1))/(RG*PDENS(JL,KLEV))
ENDDO

DO JL=KIDIA,KFDIA

  IF (.NOT.LDLAND(JL)) THEN

!-- over sea
     IF (PTIA(JL) >= (RTFREEZSICE-0.01_JPRB)) THEN
!---- over sea-ice melting
       zvd2ice=0.8E-02_JPRB         ! SO2                 
       zvd4ice=0.2E-02_JPRB         ! others
     ELSE
!---- over sea-ice non melting
       zvd2ice=0.1E-02_JPRB         ! SO2                 
       zvd4ice=0.025E-02_JPRB       ! others
     ENDIF
     zvdrd(JL,1)=(1._JPRB-PCI(JL))*1.0E-02_JPRB + PCI(JL)*zvd2ice
     zvdrd(JL,2)=(1._JPRB-PCI(JL))*0.2E-02_JPRB + PCI(JL)*zvd4ice
  ELSE

!-- over land
!---- over snow
    IF (PSNS(JL) > zcsncri ) THEN
      IF (PFWMLT(JL) > 0._JPRB) THEN
!------ over snow melting
        zvd2nof=0.8E-02_JPRB
        zvd4nof=0.2E-02_JPRB
      ELSE
!------ over snow non melting
        zvd2nof=0.1E-02_JPRB
        zvd4nof=0.025E-02_JPRB
      ENDIF
    ELSE
!-- soil
      IF (PTS(JL) <= RTT) THEN
!---- soil frozen
        zvd2nof=0.2E-02_JPRB
        zvd4nof=0.025E-02_JPRB
      ELSE
        IF (PFRTI(JL,3) > 0.5_JPRB .OR. PWS1(JL) > zwcap ) THEN
!---- soil saturated
          zvd2nof=0.8E-02_JPRB
          zvd4nof=0.2E-02_JPRB
        ELSE
!---- soil dry
          IF ( ZRAT(JL) < 0.9_JPRB) THEN
            zvd2nof=0.2E-02_JPRB
            zvd4nof=0.025E-02_JPRB
          ELSE
!----- soil partly wet
            zvd2nof=zvwc2*ZRAT(JL)-zvw02
            zvd4nof=zvwc4*ZRAT(JL)-zvw04
          ENDIF
        ENDIF
      ENDIF
!-- PDFRTI(..,6) is the fraction covered by snow-free high vegetation
    ENDIF
    zvdrd(JL,1)=PFRTI(JL,6)*0.8E-02_JPRB+(1._JPRB-PFRTI(JL,6))*zvd2nof
    zvdrd(JL,2)=PFRTI(JL,6)*0.2E-02_JPRB+(1._JPRB-PFRTI(JL,6))*zvd4nof
  ENDIF
  zvdrd(JL,1)=min(zvdrd(jl,1),zmaxvdry(jl))
  zvdrd(JL,2)=min(zvdrd(jl,2),zmaxvdry(jl))
ENDDO

DO JT=1,ntrac
  IF (cphas(JT) == 'GAS' .and. (ctrac(JT) == 'SO2' .or. ctrac(JT) == 'SO4')) THEN
    zvdrydep(kidia:kfdia)=zvdrd(kidia:kfdia,1)

  ELSEIF (cphas(JT) == 'AMS' .OR. cphas(JT) == 'ANU') THEN
    IF (nmode(JT) == 1) THEN
      zvdrydep(kidia:kfdia)=0._JPRB
    ELSEIF (nmode(JT) == 2) THEN
      zvdrydep(kidia:kfdia)=zvdaitken
    ELSEIF (nmode(JT) == 4) THEN
      zvdrydep(kidia:kfdia)=zvdrd(kidia:kfdia,2)
    ELSEIF (nmode(JT) == 4) THEN
      zvdrydep(kidia:kfdia)=zvdcoarse
    ELSEIF (nmode(JT) == 5) THEN
      zvdrydep(kidia:kfdia)=zvdaitken
    ELSEIF (nmode(JT) == 6) THEN
      zvdrydep(kidia:kfdia)=zvdrd(kidia:kfdia,2)
    ELSEIF (nmode(JT) == 7) THEN
      zvdrydep(kidia:kfdia)=zvdcoarse
    ENDIF
  ENDIF

  IF (ndryd(JT) > 0) THEN
    DO JL=KIDIA,KFDIA
!-- security check to limit the velocity to twice the vertical grid velocity 
!     of the lowest model layer
      zvdrydep(JL) = MIN(zvdrydep(JL), 2._JPRB*zdz(JL)/PTSPHY)

!-- calculate the tracer flux to the surface equivalent to the deposition velocity
      zdrydepflx(jl)=pxtm1(jl,klev,jt)*pdens(jl,klev)*zvdrydep(jl)

!-- reduce the emission flux correspondingly
      PFLXM7(JL,JT) = PFLXM7(JL,JT) - zdrydepflx(jl)
    ENDDO
  ENDIF
ENDDO
                
!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('M7_DRYDEP',1,ZHOOK_HANDLE)
END SUBROUTINE M7_DRYDEP
