SUBROUTINE m7_logtail ( kidia, kfdia , klon   , klev , &
                      & prdry, pr    , pfracn , pfracm )

    ! *aero_logtail* calculates mass- and number-fraction larger than
    !                the radius pr for each mode of a superposition 
    !                of nmod log-normal aerosol distributions
    !
    ! Author:
    ! -------
    ! Philip Stier, MPI-MET                       2004
    !
    ! Method:
    ! -------

    !
    ! The calculation of the activated number fraction and mass fraction
    ! from the radius of activation is done by a transformation of the 
    ! log-normal distribution to the error function which is then computed
    ! using the routine m7_cumnor:
    !
    !                        / x                              _
    !                 N      |       1           1   ln(R)-ln(R)  2
    !    N(0,x) = ---------  |   --------  exp(- - ( ----------- )   ) d ln(R) 
    !             ln(sigma)  |   sqrt(2PI)       2    ln(sigma)
    !                        / 0 
    !                         
    !                         /tx                   2
    !                        |        1            t
    !           =     N      |     --------  exp(- - ) d t 
    !                        |     sqrt(2PI)       2 
    !                        /-inf
    ! 
    !    where:                   
    !
    !                        _
    !               ln(R)-ln(R)
    !    t      =   -----------
    !                ln(sigma)
    !
    !    and:
    !                        _
    !               ln(x)-ln(R)
    !    tx     =   -----------
    !                ln(sigma)

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

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

USE YO_AERO_M7 ,ONLY : nmod, cmedr2mmedr, sigmaln

!    USE mo_aero_mem, ONLY: rdry


    IMPLICIT NONE


    !--- Subroutine parameters:

INTEGER(KIND=JPIM),INTENT(IN) :: kidia, kfdia, klon, klev

REAL(KIND=JPRB),INTENT(IN)  :: pr(klon ,klev,nmod), prdry(klon,klev,nmod)

REAL(KIND=JPRB),INTENT(OUT) :: pfracn(klon ,klev,nmod), pfracm(klon ,klev,nmod)

    !--- Local variables:

INTEGER(KIND=JPIM) :: jl, jk, jmod

REAL(KIND=JPRB)    :: ztn, ztm, zdummy, zeps


#include "m7_cumnor.intfb.h"

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

    !--- 0) 

!!    zeps=EPSILON(1.0_JPRB)
!    zeps=TINY(1.0_JPRB)
    zeps=1.E-32_JPRB

    !--- 1) 

    DO jmod=1, nmod

!       rdry_p => rdry(jmod)%ptr
! rdry_p(krow) was changed top prdry(jmod)

       DO jk=1, klev
          DO jl=kidia,kfdia

             IF (pr(jl,jk,jmod) > zeps .AND. prdry(jl,jk,jmod) > zeps) THEN

                !--- Transform number distribution to error function:

                ztn=(LOG(pr(jl,jk,jmod))-LOG(prdry(jl,jk,jmod)))/sigmaln(jmod)

                !--- Transform mass distribution to error function:

                ztm=(LOG(pr(jl,jk,jmod))-LOG(prdry(jl,jk,jmod)*cmedr2mmedr(jmod)))/sigmaln(jmod)

                !--- Calculate the cumulative of the log-normal number distribution:

                CALL m7_cumnor(ztn,zdummy,pfracn(jl,jk,jmod))

                !--- Calculate the cumulative of the log-normal mass distribution:

                CALL m7_cumnor(ztm,zdummy,pfracm(jl,jk,jmod))

             ELSEIF (pr(jl,jk,jmod) < zeps .AND. prdry(jl,jk,jmod) > zeps) THEN

                pfracn(jl,jk,jmod)=1.0_JPRB
                pfracm(jl,jk,jmod)=1.0_JPRB

             ELSE

                pfracn(jl,jk,jmod)=0.0_JPRB
                pfracm(jl,jk,jmod)=0.0_JPRB

             ENDIF

          ENDDO !kproma
       ENDDO !klev
    ENDDO !nmod

!--------------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('M7_LOGTAIL',1,ZHOOK_HANDLE)
END SUBROUTINE m7_logtail
