SUBROUTINE SWTL &
 & ( KIDIA , KFDIA  , KLON , KLEV  , KAER,&
 !- trajectory inputs
 & PSCT5 , PCARDI5, PPSOL5 , PALBD5, PALBP5 , PWV5, PQS5,&
 & PRMU05, PCG5   , PCLDSW5, POMEGA5, POZ5  , PPMB5,&
 & PTAU5 , PTAVE5 , PAER5,&
 !- trajectory outputs
 & PFACT5 , PFDOWN5, PFUP5,&
! & PCDOWN5, PCUP5  ,&
 !- perturbation inputs      
 & PSCT  , PCARDI , PPSOL  , PALBD , PALBP  , PWV , PQS,&
 & PRMU0 , PCG    , PCLDSW , POMEGA , POZ   , PPMB,&
 & PTAU  , PTAVE  , PAER,&
 !- perturbation outputs
 & PFDOWN , PFUP &
! & PCDOWN , PCUP &
 & )  

!**** *SWTL* - COMPUTES THE SHORTWAVE RADIATION FLUXES.
!              -> TANGENT LINEAR <-

!     PURPOSE.
!     --------

!          THIS ROUTINE COMPUTES THE SHORTWAVE RADIATION FLUXES IN TWO
!     SPECTRAL INTERVALS FOLLOWING FOUQUART AND BONNEL (1980).

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

!          *SWTL* IS CALLED FROM *RADLSWTL*

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

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

!     METHOD.
!     -------

!          1. COMPUTES ABSORBER AMOUNTS                 (SWU)
!          2. COMPUTES FLUXES IN U.V./VISIBLE  SPECTRAL INTERVAL (SW1S)
!          3. COMPUTES FLUXES IN NEAR-INFRARED SPECTRAL INTERVAL (SWNI)

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

!          *SWUTL*, *SW1STL*, *SWNITL*

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

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

!     AUTHOR.
!     -------
!        JEAN-JACQUES MORCRETTE  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 96-09-26
!        Modified : 2000-02-24 - M.Janiskova (adopted for revised
!                                             aerosols as in sw)
!                   2002-05-07 - M.Janiskova (adopted for 6 spectral 
!                                             intervals)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!                   2008-03-18 - M.Janiskova: commented diagnostic computation
!     ------------------------------------------------------------------

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

USE YOERAD   , ONLY : NSW
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),INTENT(IN)    :: KAER 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSCT5 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCARDI5 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPSOL5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALBD5(KLON,NSW) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALBP5(KLON,NSW) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWV5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRMU05(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCG5(KLON,NSW,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDSW5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POMEGA5(KLON,NSW,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POZ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPMB5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAU5(KLON,NSW,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAVE5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAER5(KLON,6,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFACT5(KLON)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFDOWN5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFUP5(KLON,KLEV+1) 
!REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCDOWN5(KLON,KLEV+1)
!REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCUP5(KLON,KLEV+1)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSCT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCARDI 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPSOL(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALBD(KLON,NSW) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALBP(KLON,NSW) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRMU0(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCG(KLON,NSW,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDSW(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POMEGA(KLON,NSW,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POZ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPMB(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAU(KLON,NSW,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAVE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAER(KLON,6,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFDOWN(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFUP(KLON,KLEV+1) 
!REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCDOWN(KLON,KLEV+1)
!REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCUP(KLON,KLEV+1)

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

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

!- Inputs Trajectory

!- Outputs Trajectory

!- Inputs Perturbations

!- Outputs Perturbations

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

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

!- Trajectory

REAL(KIND=JPRB) :: ZAKI5(KLON,2,NSW)&
 & ,  ZCLD5(KLON,KLEV)    , ZCLEAR5(KLON) &
 & ,  ZDSIG5(KLON,KLEV)   &
 & ,  ZFD5(KLON,KLEV+1)   &
 & ,  ZCDNIR5(KLON,KLEV+1), ZCDUVS5(KLON,KLEV+1) &
 & ,  ZFDOWN5(KLON,KLEV+1), ZFDNIR5(KLON,KLEV+1), ZFDUVS5(KLON,KLEV+1)&
 & ,  ZFU5(KLON,KLEV+1)   &
 & ,  ZCUNIR5(KLON,KLEV+1), ZCUUVS5(KLON,KLEV+1)&
 & ,  ZFUP5(KLON,KLEV+1)  , ZFUNIR5(KLON,KLEV+1), ZFUUVS5(KLON,KLEV+1)&
 & ,  ZRMU5(KLON)         , ZSEC5(KLON)         &
 & ,  ZUD5(KLON,5,KLEV+1) 
! diagnostic clear sky quantities
!REAL(KIND=JPRB) :: ZCDOWN5(KLON,KLEV+1), ZCUP5(KLON,KLEV+1) &
! & ,  ZCU5(KLON,KLEV+1), ZCD5(KLON,KLEV+1)

!- Perturbations

REAL(KIND=JPRB) :: ZAKI(KLON,2,NSW)&
 & ,  ZCLD(KLON,KLEV)    , ZCLEAR(KLON) &
 & ,  ZDSIG(KLON,KLEV)   &
 & ,  ZFD(KLON,KLEV+1)   &
 & ,  ZCDNIR(KLON,KLEV+1), ZCDUVS(KLON,KLEV+1)&
 & ,  ZFDOWN(KLON,KLEV+1), ZFDNIR(KLON,KLEV+1), ZFDUVS(KLON,KLEV+1)&
 & ,  ZFU(KLON,KLEV+1)   &
 & ,  ZCUNIR(KLON,KLEV+1), ZCUUVS(KLON,KLEV+1)&
 & ,  ZFUP(KLON,KLEV+1)  , ZFUNIR(KLON,KLEV+1), ZFUUVS(KLON,KLEV+1)&
 & ,  ZRMU(KLON)         , ZSEC(KLON)         &
 & ,  ZUD(KLON,5,KLEV+1) 
REAL(KIND=JPRB) :: ZFACT(KLON)
! diagnostic clear sky quantitie
!REAL(KIND=JPRB) :: ZCDOWN(KLON,KLEV+1), ZCUP(KLON,KLEV+1)&
! & ,  ZCU(KLON,KLEV+1), ZCD(KLON,KLEV+1)

INTEGER(KIND=JPIM) ::  JK, JL, JNU, INUVS, INUIR
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "sw1stl.intfb.h"
#include "swnitl.intfb.h"
#include "swutl.intfb.h"

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

!*         1.     ABSORBER AMOUNTS AND OTHER USEFUL QUANTITIES
!                 --------------------------------------------

! 100 CONTINUE

IF (LHOOK) CALL DR_HOOK('SWTL',0,ZHOOK_HANDLE)
CALL SWUTL &
 & ( KIDIA , KFDIA  , KLON   , KLEV,&
 & PSCT5 , PCARDI5, PCLDSW5, PPMB5 , PPSOL5,&
 & PRMU05, PTAVE5 , PWV5,&
 & ZAKI5 , ZCLD5  , ZCLEAR5, ZDSIG5, PFACT5, ZRMU5 , ZSEC5, ZUD5,&
 & PSCT  , PCARDI , PCLDSW , PPMB  , PPSOL,&
 & PRMU0 , PTAVE  , PWV,&
 & ZAKI  , ZCLD   , ZCLEAR , ZDSIG , ZFACT , ZRMU  , ZSEC , ZUD &
 & )  

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

!*         2.     INTERVAL (0.25-0.68 MICRON): U.V. AND VISIBLE
!                 ---------------------------------------------

! 200 CONTINUE

IF (NSW <= 4) THEN
  INUVS=1
  INUIR=2
ELSEIF (NSW == 6) THEN
  INUVS=1
  INUIR=4
ENDIF

DO JK = 1 , KLEV+1
  DO JL = KIDIA,KFDIA
    ZFD(JL,JK) = 0.0_JPRB
    ZFU(JL,JK) = 0.0_JPRB
    ZFD5(JL,JK) = 0.0_JPRB
    ZFU5(JL,JK) = 0.0_JPRB

! diagnostic clear sky quantities
!    ZCD(JL,JK) = 0.0_JPRB
!    ZCU(JL,JK) = 0.0_JPRB
!    ZCD5(JL,JK) = 0.0_JPRB
!    ZCU5(JL,JK) = 0.0_JPRB
  ENDDO
ENDDO

DO JNU = INUVS , INUIR-1

  CALL SW1STL &
   & ( KIDIA , KFDIA  , KLON  , KLEV  , KAER  , JNU,&
   & PAER5 , PALBD5 , PALBP5, PCG5  , ZCLD5 , ZCLEAR5,&
   & ZDSIG5, POMEGA5, POZ5  , ZRMU5 , ZSEC5 , PTAU5  , ZUD5,&
   & ZFDUVS5, ZFUUVS5, ZCDUVS5, ZCUUVS5,&
   & PAER  , PALBD  , PALBP , PCG   , ZCLD  , ZCLEAR,&
   & ZDSIG , POMEGA , POZ   , ZRMU  , ZSEC  , PTAU   , ZUD,&
   & ZFDUVS, ZFUUVS , ZCDUVS, ZCUUVS &
   & )  

  DO JK = 1 , KLEV+1
    DO JL = KIDIA,KFDIA
      ZFD(JL,JK) = ZFD(JL,JK)+ZFDUVS(JL,JK)
      ZFD5(JL,JK)= ZFD5(JL,JK)+ZFDUVS5(JL,JK)
      ZFU(JL,JK) = ZFU(JL,JK)+ZFUUVS(JL,JK)
      ZFU5(JL,JK)= ZFU5(JL,JK)+ZFUUVS5(JL,JK)

! diagnostic clear sky quantities
!      ZCD(JL,JK) = ZCD(JL,JK)+ZCDUVS(JL,JK)
!      ZCD5(JL,JK)= ZCD5(JL,JK)+ZCDUVS5(JL,JK)
!      ZCU(JL,JK) = ZCU(JL,JK)+ZCUUVS(JL,JK)
!      ZCU5(JL,JK)= ZCU5(JL,JK)+ZCUUVS5(JL,JK)
    ENDDO
  ENDDO
ENDDO

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

!*         3.     INTERVAL (0.68-4.00 MICRON): NEAR-INFRARED
!                 ------------------------------------------

! 300 CONTINUE

DO JK = 1 , KLEV+1
  DO JL = KIDIA,KFDIA
    ZFDOWN (JL,JK)=0.0_JPRB
    ZFUP   (JL,JK)=0.0_JPRB
    ZFDOWN5(JL,JK)=0.0_JPRB
    ZFUP5  (JL,JK)=0.0_JPRB

! diagnostic clear sky quantities
!    ZCDOWN (JL,JK)=0.0_JPRB
!    ZCUP   (JL,JK)=0.0_JPRB
!    ZCDOWN5(JL,JK)=0.0_JPRB
!    ZCUP5  (JL,JK)=0.0_JPRB
  ENDDO
ENDDO

DO JNU = INUIR , NSW

  CALL SWNITL &
   & ( KIDIA  , KFDIA , KLON   , KLEV   , KAER  , JNU,&
   & PAER5  , ZAKI5 , PALBD5 , PALBP5,&
   & PCG5   , ZCLD5 , ZCLEAR5,&
   & ZDSIG5 ,POMEGA5, POZ5   , ZRMU5  , ZSEC5 , PTAU5, ZUD5,&
   & PWV5   , PQS5,&
   & ZFDNIR5,ZFUNIR5,&
! ZCDNIR5, ZCUNIR5,&
   & PAER   , ZAKI  , PALBD  , PALBP,&
   & PCG    , ZCLD  , ZCLEAR,&
   & ZDSIG  , POMEGA, POZ    , ZRMU   , ZSEC  , PTAU , ZUD,&
   & PWV    , PQS,&
   & ZFDNIR , ZFUNIR &
!, ZCDNIR , ZCUNIR &
   & )  

  DO JK = 1 , KLEV+1
    DO JL = KIDIA,KFDIA
      ZFDOWN (JL,JK)=ZFDOWN (JL,JK)+ZFDNIR (JL,JK)
      ZFUP   (JL,JK)=ZFUP   (JL,JK)+ZFUNIR (JL,JK)
      ZFDOWN5(JL,JK)=ZFDOWN5(JL,JK)+ZFDNIR5(JL,JK)
      ZFUP5  (JL,JK)=ZFUP5  (JL,JK)+ZFUNIR5(JL,JK)

! diagnostic clear sky quantities
!      ZCDOWN (JL,JK)=ZCDOWN (JL,JK)+ZCDNIR (JL,JK)
!      ZCUP   (JL,JK)=ZCUP   (JL,JK)+ZCUNIR (JL,JK)
!      ZCDOWN5(JL,JK)=ZCDOWN5(JL,JK)+ZCDNIR5(JL,JK)
!      ZCUP5  (JL,JK)=ZCUP5  (JL,JK)+ZCUNIR5(JL,JK)
    ENDDO
  ENDDO
ENDDO

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

!*         4.     FILL THE ARRAYS
!                 ---------------

! 400 CONTINUE

DO JK = 1 , KLEV+1
  DO JL = KIDIA,KFDIA
    PFUP (JL,JK)  = ZFUP (JL,JK)   + ZFU (JL,JK)
    PFDOWN (JL,JK)= ZFDOWN (JL,JK) + ZFD (JL,JK)
    PFUP5(JL,JK)   = ZFUP5(JL,JK)   + ZFU5(JL,JK)
    PFDOWN5(JL,JK) = ZFDOWN5(JL,JK) + ZFD5(JL,JK)

! diagnostic clear sky quantities
!    PCUP(JL,JK)   = ZCUP (JL,JK)   + ZCU (JL,JK)
!    PCDOWN (JL,JK)= ZCDOWN (JL,JK) + ZCD (JL,JK)
!    PCUP5(JL,JK)   = ZCUP5(JL,JK)   + ZCU5(JL,JK)
!    PCDOWN5(JL,JK) = ZCDOWN5(JL,JK) + ZCD5(JL,JK)
  ENDDO
ENDDO

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

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