! Copyright 2008-2021 Earth Sciences Department, BSC-CNS
!
! This file is part of MONARCH.
!
! MONARCH is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! MONARCH is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with MONARCH. If not, see <http://www.gnu.org/licenses/>.
!-----------------------------------------------------------------------
!
       MODULE MODULE_BSC_DEPOSITION
!
!-----------------------------------------------------------------------
!***  THIS MODULE CALCULATES THE DRY AND WET DEPOSITION OF DUST
!***  SEDIMENTAION IS INCLUDED FOLLOWING THE METHODS OF WRF_CHEM
!-----------------------------------------------------------------------
!
      USE MODULE_KINDS
!
      USE MODULE_DM_PARALLEL,ONLY : IDS,IDE,JDS,JDE                     &
                                   ,IMS,IME,JMS,JME                     &
                                   ,ITS,ITE,JTS,JTE                     &
                                   ,MPI_COMM_COMP,MYPE_SHARE,NUM_TILES
!
      USE MODULE_BSC_CONSTANTS_CHEM 
      USE MODULE_MP_ETANEW, ONLY : FPVS
      USE MODULE_CONSTANTS
!
      USE, INTRINSIC :: IEEE_ARITHMETIC
      USE, INTRINSIC :: IEEE_FEATURES
!
!-----------------------------------------------------------------------
      IMPLICIT NONE
!-----------------------------------------------------------------------
!
      PRIVATE
      PUBLIC :: DRY_DEPOSITION
!
!-----------------------------------------------------------------------
!
      CONTAINS
!
!-----------------------------------------------------------------------
!
!      SUBROUTINE DEPOSITION_DRIVER
!
!      CALL DRY_DEPOSITION
!
!      CALL WET_DEPOSITION
!
!      CALL SEDIMENTATION
!
!      END SUBROUTINE DEPOSITION_DRIVER
!
!-----------------------------------------------------------------------
!#######################################################################
!-----------------------------------------------------------------------
!
       SUBROUTINE DRY_DEPOSITION(LOWLYR                                   &
                                ,NUM_TRACERS_CHEM                         &
                                ,NUM_DUST                                 &
                                ,NUM_ASH                                  &
                                ,NUM_SALT                                 &
                                ,NUM_BC                                   &
                                ,NUM_OM                                   &
                                ,NUM_SO4                                  &
                                ,NUM_NO3,NUM_NH4                          &
                                ,NUM_UNSPC                                &
                                ,NUM_AERO                                 &
                                ,DUST_START,DUST_END                      &
                                ,NUM_VEG                                  &
                                ,USTAR,U10,V10                            &
                                ,SM,IVGTYP                                &
                                ,Z0DUST                                   &
                                ,AA,PRADI,Z0,PDENS                        &
                                ,CD,CV,RSQCD0V,ASM                        &
                                ,CHEM                                     &
                                ,APESTS,T,Q                               &
                                ,NTSD,NPHS,DT                             &
                                ,AKHS                                     &
                                ,HT,DZ                                    &
                                ,DEPDRY                                   &
                                ,VDRY                                     &
                                ,VEGDUST                                  &
                                ,IDS,IDE,JDS                              &
                                ,JDE,KDS,KDE                              &
                                ,IMS,IME,JMS                              &
                                ,JME,KMS,KME                              &
                                ,ITS,ITE,JTS                              &
                                ,JTE,KTS,KTE)
!
!-----------------------------------------------------------------------
!
      IMPLICIT NONE
!
!-----------------------------------------------------------------------
!***  VARIABLES
!-----------------------------------------------------------------------
!
      INTEGER(KIND=KINT),INTENT(IN) :: IDS,IDE,JDS,JDE,KDS,KDE                       &
                           ,IMS,IME,JMS,JME,KMS,KME                       &
                           ,ITS,ITE,JTS,JTE,KTS,KTE
!
      INTEGER(KIND=KINT),INTENT(IN) :: NTSD,NPHS
      INTEGER(KIND=KINT),INTENT(IN) :: NUM_DUST,NUM_TRACERS_CHEM,NUM_VEG
      INTEGER(KIND=KINT),INTENT(IN) :: NUM_AERO,DUST_START,DUST_END,NUM_ASH
      INTEGER(KIND=KINT),INTENT(IN) :: NUM_SALT,NUM_BC,NUM_OM,NUM_SO4,NUM_NO3,NUM_NH4,NUM_UNSPC
!
      INTEGER(KIND=KINT),DIMENSION(IMS:IME,JMS:JME),INTENT(IN) :: LOWLYR,IVGTYP
      INTEGER(KIND=KINT),DIMENSION(IMS:IME,JMS:JME),INTENT(IN) :: VEGDUST              !<-- NEW VEGETATION TYPES
!
      REAL(KIND=KFPT),INTENT(IN) :: DT
!
      REAL(KIND=KFPT),DIMENSION(1:NUM_DUST),INTENT(IN) :: AA
      REAL(KIND=KFPT),DIMENSION(1:NUM_AERO),INTENT(IN) :: PRADI,PDENS                  !<-- particle radius in (m); particle density in (kg/m³)
!
      REAL(KIND=KFPT),DIMENSION(1:NUM_VEG),INTENT(IN) :: CD,CV,RSQCD0V,ASM
!
      REAL(KIND=KFPT),DIMENSION(IMS:IME,JMS:JME),INTENT(IN) :: HT,Z0DUST,Z0
      REAL(KIND=KFPT),DIMENSION(IMS:IME,JMS:JME),INTENT(IN) :: USTAR,U10,V10,SM,AKHS
!
      REAL(KIND=KFPT),DIMENSION(IMS:IME,JMS:JME,KTS:KTE),INTENT(IN) :: DZ,APESTS,T,Q
!
      REAL(KIND=KFPT),DIMENSION(IMS:IME,JMS:JME,KTS:KTE,1:NUM_TRACERS_CHEM),INTENT(INOUT) :: CHEM
!
      REAL(KIND=KFPT),DIMENSION(IMS:IME,JMS:JME,1:NUM_AERO),INTENT(INOUT) :: DEPDRY
!
      REAL(KIND=KFPT),INTENT(IN) :: VDRY
!
!-----------------------------------------------------------------------
!***  LOCAL VARIABLES
!-----------------------------------------------------------------------
!
      INTEGER(KIND=KINT) :: I,J,K,KFLIP,N,LMH,LBT,M
      INTEGER(KIND=KINT) :: DELTIM
!
      REAL(KIND=KFPT) :: DTMAX
      INTEGER(KIND=KINT), DIMENSION(1:NUM_AERO) :: NDT_DP 
      REAL(KIND=KFPT), DIMENSION(1:NUM_AERO) :: DELTIM_DP
      REAL(KIND=KFPT) :: WV,ESAT,QSAT,RHUM,D2WF
!
      REAL(KIND=KFPT) :: UVH,CDR,REYN,VDSL
      REAL(KIND=KFPT) :: RSQCD0,SQCD0,RSQCD0VG,SQCD0VG
      REAL(KIND=KFPT) :: DEC,DEZ,AIRDENS
      REAL(KIND=KFPT) :: PTOP,PSFC
!
      REAL(KIND=KFPT),DIMENSION(1:NUM_AERO) :: STKN,STKN2,GG,VDIL,RR,STKNA
      REAL(KIND=KFPT),DIMENSION(1:NUM_AERO) :: FBO,RSM,CEFF,FSTK,VTERM,CC
!
      REAL(KIND=KFPT),DIMENSION(KTS:KTE,1:NUM_AERO) :: DUSTLOW  !NOW EXTENDED FOR ALL THE AEROSOLS
      REAL(KIND=KFPT),DIMENSION(1:NUM_AERO) :: AERO_TOT_BEFORE,AERO_TOT_AFTER
      REAL(KIND=KFPT) :: FLUX
!
      REAL(KIND=KFPT),DIMENSION(ITS:ITE,JTS:JTE,1:NUM_AERO) :: VDDP, EB, EIM, EIN
!
      REAL(KIND=KFPT),DIMENSION(ITS:ITE,JTS:JTE) :: RA
!
      REAL(KIND=KFPT),DIMENSION(ITS:ITE,JTS:JTE,1:NUM_AERO) :: SCHD
!
      REAL(KIND=KFPT),DIMENSION(KTS:KTE+1) :: DZINT
      REAL(KIND=KFPT),DIMENSION(KTS:KTE) :: TK, QK
!
      REAL(KIND=KFPT) :: DDEPDT
!
      REAL(KIND=KFPT),DIMENSION(ITS:ITE,JTS:JTE,KTS:KTE+1) :: ZINT
!
      REAL,PARAMETER :: PI=3.14159265                                       !<-- pi
      REAL,PARAMETER :: G=9.808                                             !<-- gravitational constant (m/s2)
      REAL,PARAMETER :: VISCKIN=1.46E-5                                     !<-- kinematic viscosity (m2/s)
      REAL,PARAMETER :: VISCDYN=1.7E-5                                     !<-- kinematic viscosity (m2/s)
      REAL,PARAMETER :: FREEMP=0.065E-6                                     !<-- free mean path length (m)
      REAL,PARAMETER :: SCHN=7.1                                            !<-- const schmidt number
      REAL,PARAMETER :: AAS=10.E-6                                          !<-- Alpha_s (m)
      REAL,PARAMETER :: BCONS=1.3806504*1.E-23                              !<-- boltzmann (J/K = kg*m²/s²/K)
      REAL,PARAMETER :: RC=287.04                                           !<-- gas constant (J/kg/K)
!
      REAL(KIND=KFPT),DIMENSION(IMS:IME,JMS:JME) :: DEPDRYN,DUSTDEP
      REAL(KIND=KFPT),DIMENSION(1:NUM_AERO) :: PRADIW                                  !<-- wet radius (after water-uptake)
      REAL(KIND=KFPT),DIMENSION(1:NUM_AERO) :: PDENSW                                  !<-- wet density (...)
!
!-----------------------------------------------------------------------
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!-----------------------------------------------------------------------
!
        DO N=1,NUM_AERO
          DO J=JTS,JTE
            DO I=ITS,ITE
              VDDP(I,J,N)=0.
              IF(N.EQ.1)THEN
                 DEPDRYN(I,J)= 0.
                 DUSTDEP(I,J)= 0.
              ENDIF
            ENDDO
          ENDDO
        ENDDO
!
!-----------------------------------------------------------------------
!
      DELTIM=DT*NPHS                                                        !<-- DELTIM=DTTURBL IN MYJPBL (DTTURBL=DT*STEPBL)
!
!-----------------------------------------------------------------------
!***  PREPARATIONS NEEDED FOR FLIPPING DIRECTION
!***  MONARCH ALL FLIPPING REMOVED
!-----------------------------------------------------------------------
!
      DO J=JTS,JTE
        DO K=KTS,KTE+1
          DO I=ITS,ITE
            ZINT(I,J,K)=0.
          ENDDO
        ENDDO
      ENDDO
!
      DO J=JTS,JTE
        DO I=ITS,ITE
          ZINT(I,J,KTE+1)=HT(I,J)
        ENDDO
      ENDDO
!
      DO J=JTS,JTE
        DO K=KTE,KTS,-1
          DO I=ITS,ITE
            ZINT(I,J,K)=ZINT(I,J,K+1)+DZ(I,J,K)
          ENDDO
        ENDDO
      ENDDO
!

!!MOVED LATER         
!!-----------------------------------------------------------------------
!!***  CALCULATION OF TERMINAL VELOCITY WITH CUNNINGHAN CORRECTION
!!-----------------------------------------------------------------------
!      DO N=1,NUM_DUST
!        M=NUM_ASH+N
!!
!        CC(N)=1.+((FREEMP/PRADI(M))                                     &
!                *(1.257+0.4*EXP(-1.1*PRADI(M)/FREEMP)))
!!        VTERM(N)=(PRADI(M)*2.)**2*PDENS(M)*G*CC(N)                      &
!        VTERM(N)=(PRADI(M)*2.)**2*PDENS(M)*G                            &
!                /(18.*VISCDYN)
!!
!      ENDDO
!
!-----------------------------------------------------------------------
      SETUP_INTEGRATION:  DO J=JTS,JTE
                          DO I=ITS,ITE
!-----------------------------------------------------------------------
!
      LMH=LOWLYR(I,J)
      LBT=LMH
        PSFC=APESTS(I,J,LMH)
        PTOP=APESTS(I,J,1)
!
      DO K=KTE,KTS,-1
        DZINT(K)=ZINT(I,J,K)
        TK(K)=T(I,J,K)
        QK(K)=Q(I,J,K)
        DO N=1,NUM_AERO
          M=NUM_ASH+N 
          DUSTLOW(K,N)=CHEM(I,J,K,M)
          IF(DUSTLOW(K,N).LT.0.) DUSTLOW(K,N)=0.
        ENDDO
      ENDDO
      DZINT(KTE+1)=HT(I,J)
!!!---------------------------------------------------------------------
!!!   calculating RH...
!!!
      WV=QK(LBT)/(1.-QK(LBT))
      ESAT=1000.*FPVS(TK(LBT))
      QSAT=(EP_2*ESAT)/(PSFC-ESAT)
      RHUM=WV/QSAT
!!!
!!!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!!!   applying dry-to-wet radius growth factors (r_e,wet/r_e,dry), 
!!!   following Chin et al. (2002) (GOCART) 
!!!   which use the GADS database by D'Almeida et al. (1991) 
!!!

! these lines are moved below...
!      D2WF=1.
!      IF (RHUM.GE.0.99)THEN
!        D2WF=4.8
!      ELSEIF (RHUM.GE.0.95)THEN
!        D2WF=2.9
!      ELSEIF (RHUM.GE.0.90)THEN
!        D2WF=2.4
!      ELSEIF (RHUM.GE.0.80)THEN
!        D2WF=2.0
!      ELSEIF (RHUM.GE.0.70)THEN
!        D2WF=1.8
!      ELSEIF (RHUM.GE.0.50)THEN
!        D2WF=1.6
!      ENDIF

      DO N=1,NUM_AERO
        PRADIW(N)=PRADI(N)
        PDENSW(N)=PDENS(N)
        IF (N.GT.NUM_ASH+NUM_DUST.AND.N.LE.NUM_ASH+NUM_DUST+NUM_SALT)THEN
!!!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++       
!!! r_wet = r_dry * D2WF
!!! rho_wet = f_d * rho_dry + (1-f_d) * rho_water
!!! where f_d is the volume fraction of dry sea-salt, i.e.:
!!! f_d = (r_dry / r_wet)**3 = D2WF**(-3)
!!!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
           D2WF=1.
           IF (RHUM.GE.0.99)THEN
             D2WF=4.8
           ELSEIF (RHUM.GE.0.95)THEN
             D2WF=2.9
           ELSEIF (RHUM.GE.0.90)THEN
             D2WF=2.4
           ELSEIF (RHUM.GE.0.80)THEN
             D2WF=2.0
           ELSEIF (RHUM.GE.0.70)THEN
             D2WF=1.8
           ELSEIF (RHUM.GE.0.50)THEN
             D2WF=1.6
           ENDIF
          PRADIW(N)=PRADI(N)*D2WF
          PDENSW(N)=D2WF**(-3)*PDENS(N)+(1.-D2WF**(-3))*rhowater
        ENDIF

       !BC
       IF (N.GT.NUM_ASH+NUM_DUST+NUM_SALT+NUM_OM.AND.N.LE.NUM_ASH+NUM_DUST+NUM_SALT+NUM_OM+NUM_BC)THEN
           D2WF=1.
          IF (N-(NUM_ASH+NUM_DUST+NUM_SALT+NUM_OM).EQ.2)THEN !philic
           IF (RHUM.GE.0.99)THEN
             D2WF=1.9
           ELSEIF (RHUM.GE.0.95)THEN
             D2WF=1.5
           ELSEIF (RHUM.GE.0.90)THEN
             D2WF=1.4
           ELSEIF (RHUM.GE.0.80)THEN
             D2WF=1.2
           ELSEIF (RHUM.GE.0.70)THEN
             D2WF=1.0
           ELSEIF (RHUM.GE.0.50)THEN
             D2WF=1.0
           ENDIF
          ENDIF
          PRADIW(N)=PRADI(N)*D2WF
          PDENSW(N)=D2WF**(-3)*PDENS(N)+(1.-D2WF**(-3))*rhowater
        ENDIF !philic

       !OM
       IF (N.GT.NUM_ASH+NUM_DUST+NUM_SALT.AND.N.LE.NUM_ASH+NUM_DUST+NUM_SALT+NUM_OM)THEN
           D2WF=1.
          IF (N-(NUM_ASH+NUM_DUST+NUM_SALT).EQ.2.OR.N-(NUM_ASH+NUM_DUST+NUM_SALT).GE.4)THEN !philic
           IF (RHUM.GE.0.99)THEN
             D2WF=2.2
           ELSEIF (RHUM.GE.0.95)THEN
             D2WF=1.8
           ELSEIF (RHUM.GE.0.90)THEN
             D2WF=1.6
           ELSEIF (RHUM.GE.0.80)THEN
             D2WF=1.5
           ELSEIF (RHUM.GE.0.70)THEN
             D2WF=1.4
           ELSEIF (RHUM.GE.0.50)THEN
             D2WF=1.2
           ENDIF
          ENDIF !philic
          PRADIW(N)=PRADI(N)*D2WF
          PDENSW(N)=D2WF**(-3)*PDENS(N)+(1.-D2WF**(-3))*rhowater
        ENDIF

       !SO4
       IF (N.GT.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM.AND.N.LE.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM+NUM_SO4)THEN
           D2WF=1.
           IF (RHUM.GE.0.99)THEN
             D2WF=2.2
           ELSEIF (RHUM.GE.0.95)THEN
             D2WF=1.9
           ELSEIF (RHUM.GE.0.90)THEN
             D2WF=1.8
           ELSEIF (RHUM.GE.0.80)THEN
             D2WF=1.6
           ELSEIF (RHUM.GE.0.70)THEN
             D2WF=1.5
           ELSEIF (RHUM.GE.0.50)THEN
             D2WF=1.4
           ENDIF
          PRADIW(N)=PRADI(N)*D2WF
          PDENSW(N)=D2WF**(-3)*PDENS(N)+(1.-D2WF**(-3))*rhowater
        ENDIF

       !NO3
       IF (N.GT.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM+NUM_SO4.AND.N.LE.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM+NUM_SO4+NUM_NO3)THEN
           D2WF=1.
!           IF (RHUM.GE.0.99)THEN
!             D2WF=2.2
!           ELSEIF (RHUM.GE.0.95)THEN
!             D2WF=1.9
!           ELSEIF (RHUM.GE.0.90)THEN
!             D2WF=1.8
!           ELSEIF (RHUM.GE.0.80)THEN
!             D2WF=1.6
!           ELSEIF (RHUM.GE.0.70)THEN
!             D2WF=1.5
!           ELSEIF (RHUM.GE.0.50)THEN
!             D2WF=1.4
!           ENDIF
          PRADIW(N)=PRADI(N)*D2WF
          PDENSW(N)=D2WF**(-3)*PDENS(N)+(1.-D2WF**(-3))*rhowater
        ENDIF
       !NH4
       IF (N.GT.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM+NUM_SO4+NUM_NO3.AND.N.LE.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM+NUM_SO4+NUM_NO3+NUM_NH4)THEN
           D2WF=1.
!           IF (RHUM.GE.0.99)THEN
!             D2WF=2.2
!           ELSEIF (RHUM.GE.0.95)THEN
!             D2WF=1.9
!           ELSEIF (RHUM.GE.0.90)THEN
!             D2WF=1.8
!           ELSEIF (RHUM.GE.0.80)THEN
!             D2WF=1.6
!           ELSEIF (RHUM.GE.0.70)THEN
!             D2WF=1.5
!           ELSEIF (RHUM.GE.0.50)THEN
!             D2WF=1.4
!           ENDIF
          PRADIW(N)=PRADI(N)*D2WF
          PDENSW(N)=D2WF**(-3)*PDENS(N)+(1.-D2WF**(-3))*rhowater
        ENDIF
       !UNSPC
       IF (N.GT.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM+NUM_SO4+NUM_NO3+NUM_NH4.AND.N.LE.NUM_ASH+NUM_DUST+NUM_SALT+NUM_BC+NUM_OM+NUM_SO4+NUM_NO3+NUM_NH4+NUM_UNSPC)THEN
           D2WF=1.
!           IF (RHUM.GE.0.99)THEN
!             D2WF=2.2
!           ELSEIF (RHUM.GE.0.95)THEN
!             D2WF=1.9
!           ELSEIF (RHUM.GE.0.90)THEN
!             D2WF=1.8
!           ELSEIF (RHUM.GE.0.80)THEN
!             D2WF=1.6
!           ELSEIF (RHUM.GE.0.70)THEN
!             D2WF=1.5
!           ELSEIF (RHUM.GE.0.50)THEN
!             D2WF=1.4
!           ENDIF
          PRADIW(N)=PRADI(N)*D2WF
          PDENSW(N)=D2WF**(-3)*PDENS(N)+(1.-D2WF**(-3))*rhowater
        ENDIF

!!!---------------------------------------------------------------------
!!***  CALCULATION OF TERMINAL VELOCITY WITH CUNNINGHAN CORRECTION
!!!
!! to do
!        VISC= 1.458E-6*TK(LBT)**1.5/(TK(LBT)+110.4)          !Dynamic viscosity
!        FREEMP = 1.1E-3/P_MIDL_MB(K)/SQRT(TK(LBT))         !Free path


        CC(N)=1.+((FREEMP/PRADIW(N))                                     &
              *(1.257+0.4*EXP(-1.1*PRADIW(N)/FREEMP)))
        VTERM(N)=(PRADIW(N)*2.)**2*PDENSW(N)*G                            &
              /(18.*VISCDYN)
      ENDDO
!
!-----------------------------------------------------------------------
!-----------------------------------------------------------------------
!
      AIRDENS=PSFC/(R_D*TK(LBT)*(P608*QK(LBT)+1.))
!
!AERODYNAMIC RESISTENCE
      RA(I,J)=1./(AKHS(I,J)*VDRY)    !VDRY tunning of aerodynamics resistance
!
!
!-----------------------------------------------------------------------
      DO N=1,NUM_AERO
        M=NUM_ASH+N
!-----------------------------------------------------------------------
!*** CALCULATION OF SCHMIDT NUMBER AS FUNCTION OF PARTICLE SIZE
!-----------------------------------------------------------------------
!
      SCHD(I,J,N)=VISCKIN/(CC(N)*BCONS*TK(LBT)                          &
                   /(6.*PI*VISCKIN*PRADIW(M)*AIRDENS))
!
!COLLECTION EFFICIENCY FROM BROWNIAN DIFFUSION
      EB(I,J,N)=SCHD(I,J,N)**(-GAMMAN(IVGTYP(I,J)))
      IF(SM(I,J).GT.0.5) EB(I,J,N)=SCHD(I,J,N)**(-GAMMAN(16))
!
!COLLECTION EFIFICIENCY FROM IMPACTION
!-----------------------------------------------------------------------
      IF(SM(I,J)  .EQ.1..OR.                                            &
      IVGTYP(I,J).EQ.16.OR.                                             &
      (IVGTYP(I,J).GE.19.AND.                                           &
      IVGTYP(I,J).LE.27))THEN                                           
!-----------------------------------------------------------------------
!
      STKN(N)=VTERM(N)*CC(N)*USTAR(I,J)*USTAR(I,J)/VISCKIN
      EIM(I,J,N)=(STKN(N)/(ALPHA(IVGTYP(I,J))+STKN(N)))**2
      IF(SM(I,J).EQ.1.) EIM(I,J,N)=(STKN(N)/(ALPHA(16)+STKN(N)))**2

      ELSE

      STKN(N)=VTERM(N)*CC(N)*USTAR(I,J)/(AAA(IVGTYP(I,J))*1E-3*G)
      EIM(I,J,N)=(STKN(N)/(ALPHA(IVGTYP(I,J))+STKN(N)))**2

      ENDIF
!
!COLLECTION EFFICIENCY BY INTERCEPTION
!-----------------------------------------------------------------------
      IF(SM(I,J)  .EQ.1..OR.                                            &
      IVGTYP(I,J).EQ.16.OR.                                             &
      (IVGTYP(I,J).GE.19.AND.                                           &
      IVGTYP(I,J).LE.27))THEN                                           
!-----------------------------------------------------------------------
!
      EIN(I,J,N)=0.0
!
      ELSE
!
      EIN(I,J,N)=0.5*(2*PRADIW(N)/(AAA(IVGTYP(I,J))*1.E-3))**2
!
      ENDIF
!
!DRY DEPOSITION VELOCITY
!
!gravitational settling
      VDDP(I,J,N)=0. !We now do the settling in the sedimentation routine
      VDDP(I,J,N)=VDDP(I,J,N)+1/(RA(I,J)+(1/(3*USTAR(I,J)*(EB(I,J,N)+EIM(I,J,N)+EIN(I,J,N)))))
!
!-----------------------------------------------------------------------
      ENDDO
!-----------------------------------------------------------------------
!
      DEZ=DZINT(LBT)-DZINT(LBT+1)     
!
      DO N=1,NUM_AERO
        M=NUM_ASH+N
         FLUX = DELTIM*VDDP(I,J,N)*DUSTLOW(LBT,N)              ! kg/m2 during DELTIM       
         FLUX = MIN(FLUX, DUSTLOW(LBT,N)*DEZ)                 ! do not remove more mass than the existing
               !
         IF(FLUX.GT.0.) then
           DUSTLOW(LBT,N)= DUSTLOW(LBT,N)-FLUX/DEZ
         ENDIF

        DEPDRY(I,J,M)=MAX(0.,DEPDRY(I,J,M)+FLUX)
        DEPDRYN(I,J)=MAX(0.,DEPDRYN(I,J)+FLUX)

        ENDDO
!
!-----------------------------------------------------------------------
!***  UPDATE SURFACE DUST IN THE MAIN DUST VARIABLE
!-----------------------------------------------------------------------
!
      DO K=KTS,KTE
        DO N=1,NUM_AERO
          M=NUM_ASH+N
          CHEM(I,J,K,M)=DUSTLOW(K,N)
        ENDDO
      ENDDO
!
!-----------------------------------------------------------------------
      ENDDO
      ENDDO SETUP_INTEGRATION
!-----------------------------------------------------------------------
!
!-----------------------------------------------------------------------
!
       END SUBROUTINE DRY_DEPOSITION
!
!-----------------------------------------------------------------------
!#######################################################################
!-----------------------------------------------------------------------
!
       END MODULE MODULE_BSC_DEPOSITION
!
!-----------------------------------------------------------------------
