!OPTIONS XOPT(HSFUN)
#ifdef RS6K
@PROCESS NOCHECK
#endif
SUBROUTINE RADCFG(KST,KEND,&
 & KSTGLO ,KFLDS,&
 & LDLAND,&
 & POROG,PB2,PGFL,&
 & PUT9,PVT9,PTT9,PSPT9,&
 & PSP_SB,PSP_SG,PSP_RR,&
 & PSD_VF,PSD_VN,PSD_VD,&
!>>dod
 & PSD_XA, PSD_X2, &
!<<dod
 & PALBD  ,PALBP  ,PCCNL  ,PCCNO,&
 & PPRE9  ,PPRE9F ,PNEB,&
 & PCCC   ,PHCC   ,PLCC   ,PMCC  ,PTCC,&
 & PGELAM,&
 & PDELP9 ,PEMIR  ,PEMIW  ,PEMIT,&
 & PGEOH, PGEMU  ,PMU0M  ,PQSAT,&
 & PQICE  ,PQLI   ,PQRAIN ,PRAINT &
 & )  

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDIM   , ONLY : NFLEVG   ,NPROMA
USE YOMCT0   , ONLY : NSTART, LTWOTL
USE YOMCT3   , ONLY : NSTEP
USE YOMDYN   , ONLY : TDT
USE YOMDYNA  , ONLY : LRWSDLR
USE YOMGEM   , ONLY : VBH      ,VAH      ,VC       ,VDELB
USE YOMRIP   , ONLY : RCODEC   ,RSIDEC   ,RCOVSR   ,RSIVSR
USE YOERIP   , ONLY : RCODECM  ,RSIDECM  ,RCOVSRM  ,RSIVSRM
USE YOERAD   , ONLY : NTSW
USE PTRSLB2  , ONLY : MSLB2VVEL
USE SURFACE_FIELDS_MIX  , ONLY :  GPPOPER,&
 & YSP_SBD, YSP_SB, YSP_SGD, YSP_SG, &
 & YSP_RRD, YSP_RR,   &
 & YSD_VDD, YSD_VD, YSD_VFD, YSD_VF, &
 & YSD_VND, YSD_VN, &
!>>dod 
 & YSD_XAD, YSD_X2D
!<<dod
USE YOM_YGFL , ONLY : YGFL, YQ, YL, YI, YA, YAERO, NAERO

USE YOMORB, ONLY : LCORBMD, SHR_ORB_MU

!**** *RADCFG* - Radiation calculations on fine grid

!     Purpose.
!     --------

!**   Interface.
!     ----------
!        *CALL* *RADCFG*

!        Explicit arguments :
!        --------------------
!        KST,KEND  : FIRST AND LAST GRID POINTS FOR COMPUTATIONS
!        KPROC     : AID TO DEBUG
!        POROG     : surface orography
!        PB2       : semilagrangian buffer 2
!        PGFL      : unified_treatment grid-point fields

!        LDLAND    : LAND MASK (.T. OVER LAND)
!        PALBD     : DIFFUSE ALBEDO IN THE NSW SW INTERVALS
!        PALBP     : PARALLEL ALBEDO IN THE NSW SW INTERVALS
!        PCCNL     : CCN CONCENTRATION OVER LAND
!        PCCNO     : CCN CONCENTRATION OVER OCEAN
!        PPRE9     : HALF LEVEL PRESSURE
!        PPRE9F    : FULL LEVEL PRESSURE
!        PNEB      : FRACTIONAL CLOUDINESS FOR RADIATION
!        PCCC      : CONVECTIVE CLOUD COVER DIAGNOSTIC
!        PHCC      : HIGH CLOUD COVER DIAGNOSTIC
!        PLCC      : LOW CLOUD COVER DIAGNOSTIC
!        PMCC      : MEDIUM CLOUD COVER DIAGNOSTIC
!        PTCC      : TOTAL CLOUD COVER DIAGNOSTIC
!        PDELP9    : PRESSURE DIFFERENCE ACROSS LAYERS
!        PEMIR     : MODEL SURFACE LONGWAVE OUTSIDE WINDOW EMISSIVITY
!        PEMIW     : MODEL SURFACE LONGWAVE WINDOW EMISSIVITY
!        PEMIT     : MODEL SURFACE LONGWAVE EMISSIVITY
!        PGEMU     : SINE OF LATITUDE ON THE REAL EARTH
!        PMU0M     : SOLAR ANGLE (BETWEEN RADIATION STEPS)
!        PQSAT     : SPECIFIC HUMIDITY AT SATURATION
!        PQICE     : SPECIFIC HUMIDITY OF SOLID WATER FOR RADIATION
!        PQLI      : SPECIFIC HUMIDITY OF LIQUID WATER FOR RADIATION
!        PQRAIN    : SPECIFIC HUMIDITY OF RAIN WATER FOR RADIATION
!        PRAINT    : RAIN RATE FOR RADIATION

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        Called by RADDRV.

!     Reference.
!     ----------

!     Author.
!     -------
!        George Mozdzynski  *ECMWF*

!     Modifications.
!     --------------
!      Original : 94-12-5
!      Modified : 99-03-26 P Viterbo: Tiling of the land-surface
!      Modified : 01-10-10 JJMorcrette: CCNs
!        R. El Khatib : 01-08-07 Pruning options
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      10-Jan-2004 CY28R1 Cleaning
!        M.Hamrud      15-Jan-2005 Revised GPRCP
!        M.Hamrud      01-Jul-2006 Revised surface fields
!        JJMorcrette   20060511 MODIS albedo
!        G. Balsamo    20070115 Soil type 
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!        S. Boussetta/G.Balsamo   May 2009   (Add variable LAI)
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFLDS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTGLO 
LOGICAL           ,INTENT(OUT)   :: LDLAND(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PB2(NPROMA,KFLDS) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSPT9(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSP_SB(NPROMA,YSP_SBD%NLEVS,YSP_SBD%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSP_SG(NPROMA,YSP_SGD%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSP_RR(NPROMA,YSP_RRD%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSD_VF(NPROMA,YSD_VFD%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSD_VN(NPROMA,YSD_VND%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSD_VD(NPROMA,YSD_VDD%NDIM)
!>>dod
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSD_XA(NPROMA,YSD_XAD%NLEVS,YSD_XAD%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSD_X2(NPROMA,YSD_X2D%NDIM)
!<<dod
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PALBD(NPROMA,NTSW) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PALBP(NPROMA,NTSW) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCCNL(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCCNO(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PPRE9(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PPRE9F(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PNEB(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCCC(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PHCC(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLCC(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMCC(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTCC(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGELAM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDELP9(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PEMIR(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PEMIW(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PEMIT(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEMU(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMU0M(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSAT(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQICE(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQLI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQRAIN(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRAINT(NPROMA,NFLEVG)

! PGEOH  : (NPROMA,0:NFLEVG)     ; HALF LEVEL GEOPOTENTIAL
! ZGEO : (NPROMA,  NFLEVG)     ; FULL LEVEL GEOPOTENTIAL
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PGEOH (NPROMA,0:NFLEVG)
REAL(KIND=JPRB)   :: ZGEO(NPROMA,  NFLEVG)

!     POINTER BLOCKS

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

! ZRDELP9: (NPROMA,  NFLEVG  )   ; THEIR INVERSE
! ZLNPR9 : (NPROMA,  NFLEVG  )   ; LOGARITHM OF RATIO OF PRESSURE
! ZALPH9 : (NPROMA,  NFLEVG  )   ; COEFFICIENTS OF THE HYDROSTATICS

REAL(KIND=JPRB) :: ZRDELP9(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZLNPR9 (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZALPH9 (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZDUM   (NPROMA,NFLEVG)

! ZR9    : (NPROMA,NFLEVG)       ; R
! ZCP9   : (NPROMA,NFLEVG)       ; CP

REAL(KIND=JPRB) :: ZR9(NPROMA,NFLEVG),ZCP9(NPROMA,NFLEVG)

! ZMU0  : (NPROMA)            ; SOLAR ANGLE

REAL(KIND=JPRB) :: ZMU0(NPROMA)

REAL(KIND=JPRB),ALLOCATABLE :: ZSPD9(:,:)

INTEGER(KIND=JPIM) ::  IEND, IENDC,  IST, ISTC, JAER, JK, JL, JROF
INTEGER(KIND=JPIM) ::  IBL
LOGICAL :: LLFSTEP

REAL(KIND=JPRB) :: ZDTPHY
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "gnhdlrb.intfb.h"
#include "gppref.intfb.h"
#include "gppreh.intfb.h"
#include "gprcp.intfb.h"
#include "gpxyb.intfb.h"
#include "gpgeo.intfb.h"
#include "radpar.intfb.h"

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

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

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

!*       1.    Interface to global arrays.
!              ---------------------------

!*       1.1   INITIAL COMPUTATIONS

IF(NSTEP > 0) THEN
  LLFSTEP=.FALSE.
ELSE
  LLFSTEP=.TRUE.
ENDIF

IST =KST
IEND=KEND
ISTC =KST
IENDC=KEND
IBL=(KSTGLO-1)/NPROMA+1

!*       1.5   SURFACE PRESSURE VARIABLES

DO JROF=IST,IEND
  PPRE9 (JROF,NFLEVG)=EXP(PSPT9(JROF))
ENDDO

!*       1.6   SURFACE FIELDS FOR PHYSICS

IF(LLFSTEP .AND. .NOT. LTWOTL) THEN
  CALL GPPOPER('SET9TO0', PSP_SB=PSP_SB,PSP_SG=PSP_SG,PSP_RR=PSP_RR)
ENDIF

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

!*       3.   PREPARE FOR PHYSICS  COMPUTATIONS
!             ---------------------------------

!*       3.1   Compute pressure
CALL GPPREH(NPROMA,IST,IEND,NFLEVG,VAH,VBH,PPRE9)

!*       3.2   Compute auxiliary arrays related to the vertical
CALL GPXYB(NPROMA,IST,IEND,NFLEVG,VDELB,VC,&
 & PPRE9,PDELP9(1,1),ZRDELP9,ZLNPR9,ZALPH9,ZDUM,&
 & ZDUM,ZDUM)  
CALL GPPREF(NPROMA,IST,IEND,NFLEVG,ZALPH9,PPRE9,PPRE9F)

!*       3.3   Compute R, Cp 
CALL GPRCP(NPROMA,IST,IEND,NFLEVG,PGFL=PGFL,KGFLTYP=9,PCP=ZCP9,PR=ZR9)

!*       3.4   Compute Geopotential height

DO JL=IST,IEND
  PGEOH(JL,NFLEVG) = POROG(JL)
ENDDO
CALL GPGEO(NPROMA,IST,IEND,NFLEVG,PGEOH,ZGEO,PTT9,ZR9,ZLNPR9,ZALPH9) 

!*       3.5   Transform "presim" pressures into "prehyd" pressures
!              for input to the radiation scheme
!              (deep-layer NH equations in the dynamics, but physics keeps
!              the thin layer assumption and should see "prehyd" pressures
!              only). 

IF (LRWSDLR) THEN
  ALLOCATE(ZSPD9(NPROMA,NFLEVG))
  ! Ignore (provisionally) the NH effects in this calculation,
  ! because GMV attribute YPH9%MSPD is not ready for the time being.
  ! Normally, ZSPD9 should be <=> PGMV(.,.,YPH9%MSPD,IBLOCK) in RADDRV.
  ZSPD9(IST:IEND,1:NFLEVG)=0.0_JPRB
  ! Transform "presim" quantities into "prehyd" quantities.
  CALL GNHDLRB(NPROMA,IST,IEND,.TRUE.,POROG,PTT9,ZR9,ZSPD9, &
   & PPRE9F,PPRE9,PDELP9(1,1),ZRDELP9,ZLNPR9,ZALPH9)
  DEALLOCATE(ZSPD9)
  ! PSPT9 should not be used from now on.
ENDIF

!*       3.6   Astronomy.
DO JROF=IST,IEND
  ZMU0(JROF)=MAX( RSIDEC*PGEMU(JROF)&
   & -RCODEC*RCOVSR*SQRT(1.0_JPRB-PGEMU(JROF)**2)*COS(PGELAM(JROF))&
   & +RCODEC*RSIVSR*SQRT(1.0_JPRB-PGEMU(JROF)**2)*SIN(PGELAM(JROF))&
   & ,0.0_JPRB)  
ENDDO

DO JROF=IST,IEND
  PMU0M(JROF)=MAX( RSIDECM*PGEMU(JROF)&
   & -RCODECM*RCOVSRM*SQRT(1.0_JPRB-PGEMU(JROF)**2)*COS(PGELAM(JROF))&
   & +RCODECM*RSIVSRM*SQRT(1.0_JPRB-PGEMU(JROF)**2)*SIN(PGELAM(JROF))&
   & ,0.0_JPRB)  
ENDDO

IF(LCORBMD) THEN
    CALL SHR_ORB_MU(NPROMA,IST,IEND,PGEMU,PGELAM,ZMU0,PMU0M)
ENDIF

!*       3.7  ADIABATIC TENDENCIES AS INPUT FOR PHYSICS
!             (SLB2T1 ETC. TEMPORALY USED FOR TENDENCIES)
ZDTPHY=TDT

!*       3.8  PROGNOSTIC AEROSOLS

!>>dod unused code, removed
!<<dod

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

!*       4.   CALL PHYSICS
!             -------------

!*     4.1  CALL PHYSICS NECESSARY FOR RADINT

CALL RADPAR (IST,IEND,NPROMA,YSP_SBD%NLEVS,KSTGLO,&
 & 1,NFLEVG,NSTART,NSTEP,&
 ! - INPUT
 & PPRE9   ,PPRE9F,&
 & PTT9 ,PGFL(1,1,YQ%MP9_PH),&
 & PUT9 ,PVT9,&
 & PGFL(1,1,YL%MP9_PH) ,PGFL(1,1,YI%MP9_PH) ,PGFL(1,1,YA%MP9_PH),&
 & PSP_SG(1,YSP_SG%YF%MP9 )     ,PSP_SG(1,YSP_SG%YR%MP9),&
 & PSP_RR(1,YSP_RR%YT%MP9 ),&
 & PSP_RR(1,YSP_RR%YW%MP9  )    ,PSP_SB(1,1,YSP_SB%YQ%MP9 ),PB2(1,MSLB2VVEL),&
 & PSD_VF(1,YSD_VF%YALBF%MP)    ,&
 & PSD_VF(1,YSD_VF%YALUVP%MP),PSD_VF(1,YSD_VF%YALUVD%MP),&
 & PSD_VF(1,YSD_VF%YALNIP%MP),PSD_VF(1,YSD_VF%YALNID%MP),&
 & PSP_SG(1,YSP_SG%YA%MP9)   ,PSD_VF(1,YSD_VF%YEMISF%MP),&
 & PSD_VF(1,YSD_VF%YLSM%MP)     ,PMU0M              ,PGEMU,&
 & PSD_VF(1,YSD_VF%YZ0F%MP),&
 & PSD_VF(1,YSD_VF%YTVL%MP)     ,PSD_VF(1,YSD_VF%YTVH%MP),&
 & PSD_VF(1,YSD_VF%YCVL%MP)     ,PSD_VF(1,YSD_VF%YCVH%MP),&
 & PSD_VF(1,YSD_VF%YLAIL%MP)    ,PSD_VF(1,YSD_VF%YLAIH%MP),&
 & PSD_VF(1,YSD_VF%YSOTY%MP)    ,&
 & PSD_VF(1,YSD_VF%YSOALUVP%MP) , PSD_VF(1,YSD_VF%YSOALUVD%MP) ,&
 & PSD_VF(1,YSD_VF%YSOALNIP%MP) , PSD_VF(1,YSD_VF%YSOALNID%MP) , &
 & PSD_VF(1,YSD_VF%YGETRL%MP),&
 & PSD_VF(1,YSD_VF%YCI%MP),PSD_VF(1,YSD_VF%YLIT%MP),&
! & PGFL(1,1,YAERO%MP9_PH),&
 & YSD_XAD%NLEVS, YSD_XAD%NUMFLDS, YSD_X2D%NUMFLDS, &
!  & PSD_XA(1,1,1),  &
 & PSD_XA,  PSD_X2,  &
 ! - UPDATED
 & PSD_VN(1,YSD_VN%YTOP%MP)     ,PSD_VN(1,YSD_VN%YBAS%MP) ,PSD_VN(1,YSD_VN%YACPR%MP),&
 ! - OUTPUT
 & LDLAND  ,PALBD  ,PALBP  ,PCCNL  ,PCCNO,&
 & PNEB    ,PQICE  ,PQLI   ,PQSAT,&
 & PCCC    ,PHCC   ,PLCC   ,PMCC   ,PTCC,&
 & PEMIR   ,PEMIW  ,PEMIT,&
 & PSD_VD(1,YSD_VD%YZ0F%MP) &
! &,PAERO   )  
 & )

! -- temporary
DO JK=1,NFLEVG
  DO JL=IST,IEND
    PQRAIN(JL,JK)=0.0_JPRB
    PRAINT(JL,JK)=0.0_JPRB
  ENDDO
ENDDO

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

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