SUBROUTINE VERTDISC (PWL, PTRAY550_VERTIN , PTAER_VERTIN , PHASEL_IN, PIZ_IN,      &
&                                              PTAER_VERT, PTRAY_VERT, PHASEL, PIZ)

!****VERTDISC*** ROUTINE THAT DISCRETICES THE VERTICAL INTO ISO-OPTICAL
!                DEPTH LAYERS. THIS IS DONE WITH RESPECT TO THE TOTAL 
!                OPTICAL DEPTH.

! The difference of this version with respect to the original one is that this one keeps
! the same values for the first layer (TOA) as the original profiles. It uses the rest 
! of the layers for the discretisation.

!    N.HUNEEUS ,  LOA 

!     INTERFACE
!     ---------
!           VERTDISC IS CALLED IN THE MAIN ROUTINE 6S_GEMS

!     INPUTS
!     ------

! PWL(KWL)                     : Wavelengths at which the calculations are done (m).
! PTRAY550_VERTIN(KLON,KLEV)   : Molecular optical thickness profile at 0.55e-6 m.
! PTAER_VERTIN(KLON,KLEV,KWL)  : Aerosol optical thickness profile.
! PHASEL_IN(KLON,KLEV,KWL,KMU) : Aerosol phase function profile.
! PIZ_IN(KLON,KLEV,KWL)        : Aerosol single scattering albedo profile.

!     OUTPUTS
!     -------
! PTAER_VERT(KLON,KNT,KWL)     : Aerosol optical thickness profile.
! PTRAY_VERT(KLON,KNT,KWL)     : Molecular optical thickness profile
! PHASEL(KLON,KLEV,KWL,KMU)    : Aerosol phase function profile.
! PIZ(KLON,KLEV,KWL)           : Aerosol single scattering albedo profile.


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

!     MODIFICATIONS.
!     -------------
!      A.BENEDETTI - ECMWF 06.08.2008

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

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

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

USE RT6SVALUES

IMPLICIT NONE

REAL(KIND=JPRB),INTENT(IN)  :: PWL(KWL)                       
REAL(KIND=JPRB),INTENT(IN)  :: PTRAY550_VERTIN(KLON,0:KLEV), PTAER_VERTIN(KLON,0:KLEV,KWL)        
REAL(KIND=JPRB),INTENT(IN)  :: PHASEL_IN(KLON,0:KLEV,KWL,KNBMU), PIZ_IN(KLON,0:KLEV,KWL)           
REAL(KIND=JPRB),INTENT(OUT) :: PTAER_VERT(KLON,0:KNT,KWL), PTRAY_VERT(KLON,0:KNT,KWL)
REAL(KIND=JPRB),INTENT(OUT) :: PHASEL(KLON,0:KNT,KWL,KNBMU), PIZ(KLON,0:KNT,KWL)


! LOCAL VARIABLES
! ---------------


!INTEGER(KIND=JPIM),PARAMETER  :: KMU=25, KMU2=48, KNP=49, KNANG=80, KNBMU=83, KNT=20, KWL=1 
INTEGER(KIND=JPIM) :: JLEV, JDIV, JLON, JWL, JAUX, JCONT, JMU
!INTEGER(KIND=JPIM),PARAMETER :: ICTE=5 !TOGETHER WITH KNT THEY DETERMINE THE NUMBER OF LAYERS EACH
                            !ORIGINAL LAYER WILL DIVIDED
REAL(KIND=JPRB) :: ZTRAY(0:(KLEV+1)*IDIV-1), ZTAER(0:(KLEV+1)*IDIV-1), ZTRAY_VERTIN(0:KLEV)
REAL(KIND=JPRB) :: ZPHA(0:(KLEV+1)*IDIV-1,KNBMU), ZPIZ(0:(KLEV+1)*IDIV-1)
REAL(KIND=JPRB) :: ZOD_AXIS, ZISO_OD, ZODNEW_AER, ZODNEW_RAY, ZODNEW
REAL(KIND=JPRB) :: ZOMEGA, ZPHASE(KNBMU)

REAL(KIND=JPRB) :: ZHOOK_HANDLE


IF (LHOOK) CALL DR_HOOK('VERTDISC',0,ZHOOK_HANDLE)

! INITIALIZATION OF VARIABLES
! ---------------------------
PTAER_VERT(:,:,:)=0.0_JPRB
PTRAY_VERT(:,:,:)=0.0_JPRB
PHASEL(:,:,:,:)=0.0_JPRB
PIZ(:,:,:)=0.0_JPRB

DO JWL=1,KWL
  DO JLON=1,KLON
! JLEV=0 CORRESPONDS TO THE TOP OF THE ATMOPSHERE. THE VARIABLES WITH THE NEW VERTICAL DISCRETIZATION

!  1. MOLECULAR OPTICAL THICKNESS AT DIFFERENT WAVELENGTHS 
!     ----------------------------------------------------

!    print *,'1. MOLECULAR OPTICAL THICKNESS AT DIFFERENT WAVELENGTHS'
    DO JLEV=0,KLEV
      ZTRAY_VERTIN(JLEV)=PTRAY550_VERTIN(JLON,JLEV)*(PWL(JWL)/PWL550)**(-4)
    ENDDO

! 2. ASIGNING VALUES OF THE FIRST LAYER TO THE NEW VARIABLES
!    -------------------------------------------------------

    PTAER_VERT(JLON,0,JWL)=PTAER_VERTIN(JLON,0,JWL)
    PTRAY_VERT(JLON,0,JWL)=ZTRAY_VERTIN(0)
    PIZ(JLON,0,JWL)=PIZ_IN(JLON,0,JWL)
    DO JMU=1,KNBMU
        PHASEL(JLON,0,JWL,JMU)=PHASEL_IN(JLON,0,JWL,JMU)
    ENDDO 
  

! JLEV=0 CORRESPONDS TO THE TOP OF THE ATMOPSHERE. THE VARIABLES WITH THE NEW VERTICAL DISCRETIZATION

!  2. CALCULATING ISO-OPTICAL THICKNESS
!     ---------------------------------

    ZOD_AXIS=0.0_JPRB
    DO JLEV=1,KLEV
      ZOD_AXIS=ZOD_AXIS+PTAER_VERTIN(JLON,JLEV,JWL)+ZTRAY_VERTIN(JLEV)
    ENDDO
!    ZISO_OD=ZOD_AXIS/FLOAT(KNT+1)
    ZISO_OD=ZOD_AXIS/FLOAT(KNT)


!  3. SPLITTING OF ORIGINAL LAYERS INTO IDIV LAYERS
!     ---------------------------------------------
!    print *,'2. SPLITTING OF ORIGINAL LAYERS INTO IDIV LAYERS'
!    ZTAER(:) = 0.0_JPRB
! BEN - try to initialize with a different value
    ZTAER(:) = 0.000000001_JPRB
    ZTRAY(:) = 0.0_JPRB
    ZPIZ(:) = 0.000000001_JPRB
    ZPHA(:,:)=0.000000001_JPRB
    DO JLEV=1,KLEV
      DO JDIV=1,IDIV
        JAUX=(JLEV)*IDIV+JDIV
        ZTAER(JAUX-1)=PTAER_VERTIN(JLON,JLEV,JWL)/IDIV
        ZTRAY(JAUX-1)=ZTRAY_VERTIN(JLEV)/IDIV
        ZPIZ(JAUX-1)=PIZ_IN(JLON,JLEV,JWL)
        DO JMU=1,KNBMU
          ZPHA(JAUX-1,JMU)=PHASEL_IN(JLON,JLEV,JWL,JMU)
        ENDDO
      ENDDO
    ENDDO

!  4. NEW VERTICAL DISCRETIZATION
!     ---------------------------

!    print *,'3. NEW VERTICAL DISCRETIZATION'
    ZODNEW_AER=0.0_JPRB
    ZODNEW_RAY=0.0_JPRB
    ZOMEGA=0.0_JPRB
    ZPHASE(:)=0.0_JPRB
    JCONT=1
    DO JLEV=1,IDIV*(KLEV+1)-1
      ZODNEW=ZODNEW_AER+ZODNEW_RAY
      ZODNEW_AER=ZODNEW_AER+ZTAER(JLEV)
      ZODNEW_RAY=ZODNEW_RAY+ZTRAY(JLEV)
      ZOMEGA=ZOMEGA+ZTAER(JLEV)*ZPIZ(JLEV)
      DO JMU=1,KNBMU
        ZPHASE(JMU)=ZPHASE(JMU)+ZPHA(JLEV,JMU)*ZTAER(JLEV)*ZPIZ(JLEV)
      ENDDO 
      PTAER_VERT(JLON,JCONT,JWL)=ZODNEW_AER
      PTRAY_VERT(JLON,JCONT,JWL)=ZODNEW_RAY
      PIZ(JLON,JCONT,JWL)=ZOMEGA/ZODNEW_AER

      DO JMU=1,KNBMU
       PHASEL(JLON,JCONT,JWL,JMU)=ZPHASE(JMU)/ZOMEGA
      ENDDO 
      IF (((ZODNEW+ZTAER(JLEV)+ZTRAY(JLEV)) >  ZISO_OD))THEN  
! Starting new layer   
        ZODNEW_AER=0.0_JPRB
        ZODNEW_RAY=0.0_JPRB
        ZOMEGA=0.0_JPRB
        ZPHASE(:)=0.0_JPRB
        JCONT=JCONT+1
      ENDIF
    ENDDO
  ENDDO   ! LOOP ON KLON
ENDDO     ! LOOP ON WAVELENGTHS

IF (LHOOK) CALL DR_HOOK('VERTDISC',1,ZHOOK_HANDLE)
END SUBROUTINE VERTDISC
