!OPTIONS XOPT(NOEVAL)
SUBROUTINE SWR15 ( KIDIA,KFDIA,KLON, KLEV, KAER, KNU,&
 & PALBD , PCG   , PCLD , PDSIG, POMEGA,&
 & PSEC  , PTAU,&
 & PCGAZ , PPIZAZ, PRAY1, PRAY2, PREFZ , PRJ  , PRK , PRMUE,&
 & PTAUAZ, PTRA1 , PTRA2 )  

!**** *SWR15* - CONTINUUM SCATTERING COMPUTATIONS
!****           FROZEN VERSION (CYCLE 15) OF SWR

!     PURPOSE.
!     --------
!           COMPUTES THE REFLECTIVITY AND TRANSMISSIVITY IN CASE OF
!     CONTINUUM SCATTERING

!**   INTERFACE.
!     ----------

!          *SWR15* IS CALLED EITHER FROM *SW1S15*
!                                OR FROM *SW2S15*

!        IMPLICIT ARGUMENTS :
!        --------------------

!     ==== INPUTS ===
!     ==== OUTPUTS ===

!     METHOD.
!     -------

!          1. COMPUTES CONTINUUM FLUXES CORRESPONDING TO AEROSOL
!     OR/AND RAYLEIGH SCATTERING (NO MOLECULAR GAS ABSORPTION)

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

!          *SWDE*

!     REFERENCE.
!     ----------

!        SEE RADIATION'S PART OF THE ECMWF RESEARCH DEPARTMENT
!        DOCUMENTATION, AND FOUQUART AND BONNEL (1980)

!     AUTHOR.
!     -------
!        96-11: Ph. Dandin. Meteo-France
!        ORIGINAL : 89-07-14 BY JEAN-JACQUES MORCRETTE  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOMRAD15 , ONLY : NOVLP15
USE YOMRDU15 , ONLY : REPSEC15

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM)               :: KAER ! Argument NOT used
INTEGER(KIND=JPIM),INTENT(IN)    :: KNU 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALBD(KLON,2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCG(KLON,2,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLD(KLON,KLEV) 
REAL(KIND=JPRB)                  :: PDSIG(KLON,KLEV) ! Argument NOT used
REAL(KIND=JPRB)   ,INTENT(IN)    :: POMEGA(KLON,2,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSEC(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAU(KLON,2,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCGAZ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPIZAZ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRAY1(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRAY2(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PREFZ(KLON,2,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRJ(KLON,6,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRK(KLON,6,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRMUE(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAUAZ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTRA1(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTRA2(KLON,KLEV+1) 
!     ------------------------------------------------------------------

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

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

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

REAL(KIND=JPRB) :: ZC1I(KLON,KLEV+1), ZCLEQ(KLON,KLEV)&
 & ,  ZCLEAR(KLON), ZCLOUD(KLON) &
 & ,  ZGG(KLON), ZREF(KLON)&
 & ,  ZRE1(KLON), ZRE2(KLON)&
 & ,  ZRMUZ(KLON), ZRNEB(KLON), ZR21(KLON), ZR22(KLON)&
 & ,  ZR23(KLON),  ZSS1(KLON), ZTO1(KLON), ZTR(KLON,2,KLEV+1)&
 & ,  ZTR1(KLON), ZTR2(KLON), ZW(KLON)  

INTEGER(KIND=JPIM) :: JA, JAJ, JK, JKL, JKLP1, JKM1, JL

REAL(KIND=JPRB) :: ZBMU0, ZBMU1, ZCORAE, ZCORCD, ZDEN, ZDEN1,&
 & ZFACOA, ZFACOC, ZGAP, ZMU1, ZMUE, ZRE11, &
 & ZTO, ZWW  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "swde15.intfb.h"

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

!*         1.    INITIALIZATION
!                --------------

IF (LHOOK) CALL DR_HOOK('SWR15',0,ZHOOK_HANDLE)
DO JK = 1 , KLEV+1
  DO JA = 1 , 6
    DO JL = KIDIA,KFDIA
      PRJ(JL,JA,JK) = 0.0_JPRB
      PRK(JL,JA,JK) = 0.0_JPRB
    ENDDO
  ENDDO
ENDDO

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

!*         2.    TOTAL EFFECTIVE CLOUDINESS ABOVE A GIVEN LEVEL
!                ----------------------------------------------

DO JL = KIDIA,KFDIA
  ZR23(JL) = 0.0_JPRB
  ZC1I(JL,KLEV+1) = 0.0_JPRB
  ZCLEAR(JL) = 1.0_JPRB
  ZCLOUD(JL) = 0.0_JPRB
ENDDO

JK = 1
JKL = KLEV+1 - JK
JKLP1 = JKL + 1
DO JL = KIDIA,KFDIA
  ZFACOA = 1.0_JPRB - PPIZAZ(JL,JKL)*PCGAZ(JL,JKL)*PCGAZ(JL,JKL)
  ZFACOC = 1.0_JPRB - POMEGA(JL,KNU,JKL) * PCG(JL,KNU,JKL)* PCG(JL,KNU,JKL)
  ZCORAE = ZFACOA * PTAUAZ(JL,JKL) * PSEC(JL)
  ZCORCD = ZFACOC * PTAU(JL,KNU,JKL) * PSEC(JL)
  ZR21(JL) = EXP(-ZCORAE   )
  ZR22(JL) = EXP(-ZCORCD   )
  ZSS1(JL) = PCLD(JL,JKL)*(1.0_JPRB-ZR21(JL)*ZR22(JL))&
   & + (1.0_JPRB-PCLD(JL,JKL))*(1.0_JPRB-ZR21(JL))  
  ZCLEQ(JL,JKL) = ZSS1(JL)

  IF (NOVLP15 == 1) THEN
!* maximum-random      
    ZCLEAR(JL) = ZCLEAR(JL)&
     & *(1.0_JPRB-MAX(ZSS1(JL),ZCLOUD(JL)))&
     & /(1.0_JPRB-MIN(ZCLOUD(JL),1.0_JPRB-REPSEC15))  
    ZC1I(JL,JKL) = 1.0_JPRB - ZCLEAR(JL)
    ZCLOUD(JL) = ZSS1(JL)
  ELSEIF (NOVLP15 == 2) THEN
!* maximum
    ZCLOUD(JL) = MAX( ZSS1(JL) , ZCLOUD(JL) )
    ZC1I(JL,JKL) = ZCLOUD(JL)
  ELSEIF (NOVLP15 == 3) THEN
!* random
    ZCLEAR(JL) = ZCLEAR(JL)*(1.0_JPRB - ZSS1(JL))
    ZCLOUD(JL) = 1.0_JPRB - ZCLEAR(JL)
    ZC1I(JL,JKL) = ZCLOUD(JL)
  ENDIF
ENDDO

DO JK = 2 , KLEV
  JKL = KLEV+1 - JK
  JKLP1 = JKL + 1
  DO JL = KIDIA,KFDIA
    ZFACOA = 1.0_JPRB - PPIZAZ(JL,JKL)*PCGAZ(JL,JKL)*PCGAZ(JL,JKL)
    ZFACOC = 1.0_JPRB - POMEGA(JL,KNU,JKL) * PCG(JL,KNU,JKL)* PCG(JL,KNU,JKL)
    ZCORAE = ZFACOA * PTAUAZ(JL,JKL) * PSEC(JL)
    ZCORCD = ZFACOC * PTAU(JL,KNU,JKL) * PSEC(JL)
    ZR21(JL) = EXP(-ZCORAE   )
    ZR22(JL) = EXP(-ZCORCD   )
    ZSS1(JL) = PCLD(JL,JKL)*(1.0_JPRB-ZR21(JL)*ZR22(JL))&
     & + (1.0_JPRB-PCLD(JL,JKL))*(1.0_JPRB-ZR21(JL))  
    ZCLEQ(JL,JKL) = ZSS1(JL)

    IF (NOVLP15 == 1) THEN
!* maximum-random      
      ZCLEAR(JL) = ZCLEAR(JL)&
       & *(1.0_JPRB-MAX(ZSS1(JL),ZCLOUD(JL)))&
       & /(1.0_JPRB-MIN(ZCLOUD(JL),1.0_JPRB-REPSEC15))  
      ZC1I(JL,JKL) = 1.0_JPRB - ZCLEAR(JL)
      ZCLOUD(JL) = ZSS1(JL)
    ELSEIF (NOVLP15 == 2) THEN
!* maximum
      ZCLOUD(JL) = MAX( ZSS1(JL) , ZCLOUD(JL) )
      ZC1I(JL,JKL) = ZCLOUD(JL)
    ELSEIF (NOVLP15 == 3) THEN
!* random
      ZCLEAR(JL) = ZCLEAR(JL)*(1.0_JPRB - ZSS1(JL))
      ZCLOUD(JL) = 1.0_JPRB - ZCLEAR(JL)
      ZC1I(JL,JKL) = ZCLOUD(JL)
    ENDIF
  ENDDO
ENDDO

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

!*         3.    REFLECTIVITY/TRANSMISSIVITY FOR PURE SCATTERING
!                -----------------------------------------------

DO JL = KIDIA,KFDIA
  PRAY1(JL,KLEV+1) = 0.0_JPRB
  PRAY2(JL,KLEV+1) = 0.0_JPRB
  PREFZ(JL,2,1) = PALBD(JL,KNU)
  PREFZ(JL,1,1) = PALBD(JL,KNU)
  PTRA1(JL,KLEV+1) = 1.0_JPRB
  PTRA2(JL,KLEV+1) = 1.0_JPRB
ENDDO

DO JK = 2 , KLEV+1
  JKM1 = JK-1
  DO JL = KIDIA,KFDIA
    ZRNEB(JL)= PCLD(JL,JKM1)
    ZRE1(JL)=0.0_JPRB
    ZTR1(JL)=0.0_JPRB
    ZRE2(JL)=0.0_JPRB
    ZTR2(JL)=0.0_JPRB

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

!*         3.1  EQUIVALENT ZENITH ANGLE
!               -----------------------

    ZMUE = (1.0_JPRB-ZC1I(JL,JK)) * PSEC(JL)+ ZC1I(JL,JK) * 1.66_JPRB
    PRMUE(JL,JK) = 1.0_JPRB/ZMUE

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

!*         3.2  REFLECT./TRANSMISSIVITY DUE TO RAYLEIGH AND AEROSOLS
!               ----------------------------------------------------

    ZGAP = PCGAZ(JL,JKM1)
    ZBMU0 = 0.5_JPRB - 0.75_JPRB * ZGAP / ZMUE
    ZWW = PPIZAZ(JL,JKM1)
    ZTO = PTAUAZ(JL,JKM1)
    ZDEN = 1.0_JPRB + (1.0_JPRB - ZWW + ZBMU0 * ZWW) * ZTO * ZMUE &
     & + (1-ZWW) * (1.0_JPRB - ZWW +2.0_JPRB*ZBMU0*ZWW)*ZTO*ZTO*ZMUE*ZMUE  
    PRAY1(JL,JKM1) = ZBMU0 * ZWW * ZTO * ZMUE / ZDEN
    PTRA1(JL,JKM1) = 1.0_JPRB / ZDEN
!      PRINT *,' LOOP 342 ** 3 ** JL=',JL,PRAY1(JL,JKM1),PTRA1(JL,JKM1)

    ZMU1 = 0.5_JPRB
    ZBMU1 = 0.5_JPRB - 0.75_JPRB * ZGAP * ZMU1
    ZDEN1= 1.0_JPRB + (1.0_JPRB - ZWW + ZBMU1 * ZWW) * ZTO / ZMU1 &
     & + (1-ZWW) * (1.0_JPRB - ZWW +2.0_JPRB*ZBMU1*ZWW)*ZTO*ZTO/ZMU1/ZMU1  
    PRAY2(JL,JKM1) = ZBMU1 * ZWW * ZTO / ZMU1 / ZDEN1
    PTRA2(JL,JKM1) = 1.0_JPRB / ZDEN1

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

!*         3.3  EFFECT OF CLOUD LAYER
!               ---------------------

    ZW(JL) = POMEGA(JL,KNU,JKM1)
    ZTO1(JL) = PTAU(JL,KNU,JKM1)/ZW(JL)+ PTAUAZ(JL,JKM1)/PPIZAZ(JL,JKM1)
    ZR21(JL) = PTAU(JL,KNU,JKM1) + PTAUAZ(JL,JKM1)
    ZR22(JL) = PTAU(JL,KNU,JKM1) / ZR21(JL)
    ZGG(JL) = ZR22(JL) * PCG(JL,KNU,JKM1)&
     & + (1.0_JPRB - ZR22(JL)) * PCGAZ(JL,JKM1)  
    IF (ZW(JL) == 1.0_JPRB .AND. PPIZAZ(JL,JKM1) == 1.0_JPRB) THEN
      ZW(JL)=1.0_JPRB
    ELSE
      ZW(JL) = ZR21(JL) / ZTO1(JL)
    ENDIF
    ZREF(JL) = PREFZ(JL,1,JKM1)
    ZRMUZ(JL) = PRMUE(JL,JK)
  ENDDO

  CALL SWDE15 ( KIDIA, KFDIA , KLON,&
   & ZGG  , ZREF  , ZRMUZ , ZTO1 , ZW,&
   & ZRE1 , ZRE2  , ZTR1  , ZTR2      )  

  DO JL = KIDIA,KFDIA

    PREFZ(JL,1,JK) = (1.0_JPRB-ZRNEB(JL)) * (PRAY1(JL,JKM1)&
     & + PREFZ(JL,1,JKM1) * PTRA1(JL,JKM1)&
     & * PTRA2(JL,JKM1)&
     & / (1.0_JPRB-PRAY2(JL,JKM1)*PREFZ(JL,1,JKM1)))&
     & + ZRNEB(JL) * ZRE2(JL)  

    ZTR(JL,1,JKM1) = ZRNEB(JL) * ZTR2(JL) + (PTRA1(JL,JKM1)&
     & / (1.0_JPRB-PRAY2(JL,JKM1)*PREFZ(JL,1,JKM1)))&
     & * (1.0_JPRB-ZRNEB(JL))  

    PREFZ(JL,2,JK) = (1.0_JPRB-ZRNEB(JL)) * (PRAY1(JL,JKM1)&
     & + PREFZ(JL,2,JKM1) * PTRA1(JL,JKM1)&
     & * PTRA2(JL,JKM1) )&
     & + ZRNEB(JL) * ZRE1(JL)  

    ZTR(JL,2,JKM1) = ZRNEB(JL) * ZTR1(JL)+ PTRA1(JL,JKM1) * (1.0_JPRB-ZRNEB(JL))

  ENDDO
ENDDO
DO JL = KIDIA,KFDIA
  ZMUE = (1.0_JPRB-ZC1I(JL,1))*PSEC(JL)+ZC1I(JL,1)*1.66_JPRB
  PRMUE(JL,1)=1.0_JPRB/ZMUE
ENDDO

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

!*         3.5    REFLECT./TRANSMISSIVITY BETWEEN SURFACE AND LEVEL
!                 -------------------------------------------------

IF (KNU == 1) THEN
  JAJ = 2
  DO JL = KIDIA,KFDIA
    PRJ(JL,JAJ,KLEV+1) = 1.0_JPRB
    PRK(JL,JAJ,KLEV+1) = PREFZ(JL, 1,KLEV+1)
  ENDDO

  DO JK = 1 , KLEV
    JKL = KLEV+1 - JK
    JKLP1 = JKL + 1
    DO JL = KIDIA,KFDIA
      ZRE11= PRJ(JL,JAJ,JKLP1) * ZTR(JL,  1,JKL)
      PRJ(JL,JAJ,JKL) = ZRE11
      PRK(JL,JAJ,JKL) = ZRE11 * PREFZ(JL,  1,JKL)
    ENDDO
  ENDDO

ELSE

  DO JAJ = 1 , 2
    DO JL = KIDIA,KFDIA
      PRJ(JL,JAJ,KLEV+1) = 1.0_JPRB
      PRK(JL,JAJ,KLEV+1) = PREFZ(JL,JAJ,KLEV+1)
    ENDDO

    DO JK = 1 , KLEV
      JKL = KLEV+1 - JK
      JKLP1 = JKL + 1
      DO JL = KIDIA,KFDIA
        ZRE11= PRJ(JL,JAJ,JKLP1) * ZTR(JL,JAJ,JKL)
        PRJ(JL,JAJ,JKL) = ZRE11
        PRK(JL,JAJ,JKL) = ZRE11 * PREFZ(JL,JAJ,JKL)
      ENDDO
    ENDDO
  ENDDO

ENDIF

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

IF (LHOOK) CALL DR_HOOK('SWR15',1,ZHOOK_HANDLE)
899 FORMAT(1X,' SWR LOOP ',I4,' EXECUTED')
9401 FORMAT (1X,I3,8E12.5)
9402 FORMAT (1X,I3,4E15.6)
9403 FORMAT (1X,I3,4E16.8)
END SUBROUTINE SWR15
