SUBROUTINE RADTRTL(KSAT,KSENSOR,KDPROF,KPROF,KMXCH,KNCH,PRCH, &
 & PZENANG,PAZIANG,PSOLZENANG,PSOLAZIANG,PLAT,POROG,&
 & PTQOSAT,YDGEMSPROF,PPSSAT,PTSSAT,PT2SAT,PH2SAT,PCTOP,PCAMT ,&
 & PUSSAT,PVSSAT,PEMIS,                                  &
 & PTQOSAT5,YDGEMSPROF5,PPSSAT5,PTSSAT5,PLSSAT5,PT2SAT5,&
 & PH2SAT5 ,PCTOP5,PCAMT5 ,PUSSAT5,PVSSAT5,PEMIS5, PTBCLR)  

!**** *RADTRTL* - INTERFACE TO TL TOVS RAD. TRANSFER CODE

!     AUTHOR: Mike Fisher (based on RADTR)     DATE  : 95-05-23

!     PURPOSE.
!     -------
!     SET UP REASONABLE CHUNKS OF WORK FOR THE TOVS RADIANCE
!     CALCULATION.
!     EXTRAPOLATE PROFILES ABOVE TOP OF MODEL IF REQUIRED.
!     CALL THE RT-CODE.

!**   INTERFACE.
!**   ---------
!**   *CALL* * RADTRTL( )*  (FROM TOVCLR)
!     WHERE PZENANG: Satellite zenith Angle
!           PAZIANG: Satellite azimuth angle
!           PSOLZENANG: Solar zenith angle
!           PSOLAZIANG: Solar azimuth angle
!           PLAT:    Latitude [deg]
!           POROG:   Orography [m^2 s^-2]
!           KSAT   : NOAA-satellite id
!           KSENSOR: Sensor id
!           KMXCH  : Maximum number of channels
!           KNCH   : Number of channels to be computed
!           PRCH   : List of channels to be computed
!           KDPROF : First dimension of arrays
!           KPROF  : Actual number of TOVS points
!           PTQOSAT: Model values at TOVS locations, T/Q/O3  (TL)
!           YDGEMSPROF : GEMS variables  (TL)
!           PPSSAT : Surface pressure  (TL)
!           PTSSAT : Surface temperature  (TL)
!           PT2SAT : 2m temperature  (TL)
!           PH2SAT : 2m humidity  (TL)
!           PUSSAT : Surface wind u  (TL)
!           PVSSAT : Surface wind v  (TL)
!           PEMIS  : Surface emissivity for each channel  (TL)
!           ...5   : Trajectory
!           PLSSAT5 : MODEL SURFACE TYPE
!           PTBCLR : Clear brightness temperatures  (output)  (TL)

!**   EXTERNALS
!     ---------
!        RTTOVTL - RT-CODE

!     MODIFICATIONS
!     -------------
!     Erik Andersson : 970220 - F90, simplify code.
!     Roger Saunders : 970811 - Include ozone, emissivity & cloud as 
!                               variables
!     Rose Munro     : 980108 - Include satellite view angle
!     Rose Munro     : 980620 - Alter tests on max/min regr. prof. for new RT
!     Roger Saunders : 980821 - Changes for RTTOV5 interface
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        V. Guidard    23-Feb-2007 Add arg KSENSOR and add IASI (after A.Collard)
!     Niels Bromann  : 071003 - Updated for RTTOV-9
!     Alan Geer      : 080718   Fix units for orography (IFS uses m^2 s^-2)
!     Richard Engelen  090210 : More flexible interface between tracers and RTTOV input
!     Vincent GUidard: 090224 - Add CO2-slicing for AIRS
!*    ------------------------------------------------------------------


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

USE YOMVAR   , ONLY : LTOVSCV  , LCO2, LCLDSINK 
USE YOMDIMO  , ONLY :     NLSAT    ,NMXTCHU  ,&
 & NPNAV    ,NPTNAV    ,NPNSAV   ,NPNSSV   ,NPNCV   
USE YOMTVRAD , ONLY : NJPPF    ,RADPRE    , MSENSOR_AIRS, MSENSOR_IASI, &
 & AIRSCHAN_BACK, IASICHAN_BACK, LCO2_DIAG_AIRS
USE YOMLUN   , ONLY : NULOUT
USE YOMANCS  , ONLY : RMDI, NMDI
USE YOM_YGFL , ONLY : NGHG
USE YOMCST   , ONLY : RG
USE TYPE_GEMS_PROFILES

IMPLICIT NONE

!*    0.1  Global arrays

REAL(KIND=JPRB)   ,INTENT(IN)    :: PZENANG(KDPROF)
INTEGER(KIND=JPIM),INTENT(IN)    :: KDPROF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF
INTEGER(KIND=JPIM),INTENT(IN)    :: KSAT
INTEGER(KIND=JPIM),INTENT(IN)    :: KSENSOR
INTEGER(KIND=JPIM),INTENT(IN)    :: KMXCH 
INTEGER(KIND=JPIM),INTENT(IN)    :: KNCH(KDPROF)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAZIANG(KDPROF)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSOLZENANG(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSOLAZIANG(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAT(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRCH(KDPROF,KMXCH) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTQOSAT(KDPROF,NLSAT,NPNAV-1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPSSAT(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSSAT(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT2SAT(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PH2SAT(KDPROF),PCTOP(KDPROF),PCAMT(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUSSAT(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVSSAT(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEMIS(KDPROF,KMXCH) 
TYPE (TYPE_GEMSPROF)   ,INTENT(IN) :: YDGEMSPROF(KDPROF,NGEMS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTQOSAT5(KDPROF,NLSAT,NPNAV-1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPSSAT5(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSSAT5(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLSSAT5(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT2SAT5(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PH2SAT5(KDPROF),PCTOP5(KDPROF),PCAMT5(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUSSAT5(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVSSAT5(KDPROF) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEMIS5(KDPROF,KMXCH) 
TYPE (TYPE_GEMSPROF)   ,INTENT(IN) :: YDGEMSPROF5(KDPROF,NGEMS)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTBCLR(KDPROF,KMXCH) 

INTEGER(KIND=JPIM), DIMENSION(NJPPF*NMXTCHU) :: ICHID,IPFID
INTEGER(KIND=JPIM), DIMENSION(NJPPF)         :: ISURF
INTEGER(KIND=JPIM), DIMENSION(NJPPF)         :: INCH
INTEGER(KIND=JPIM), DIMENSION(NJPPF)         :: IFAIL

REAL(KIND=JPRB), DIMENSION(NJPPF*NMXTCHU):: ZTBCLR ! Clear brightness temperatures (TL)
REAL(KIND=JPRB), DIMENSION(NJPPF*NMXTCHU):: ZTBCLR5 ! Clear brightness temperatures
REAL(KIND=JPRB), DIMENSION(NJPPF*NMXTCHU):: ZTBTOT ! Total brightness temperatures (TL)
REAL(KIND=JPRB), DIMENSION(NJPPF*NMXTCHU):: ZTBTOT5 ! Total brightness temperatures

REAL(KIND=JPRB), DIMENSION(NJPPF*NMXTCHU)    :: ZEM
REAL(KIND=JPRB), DIMENSION(NLSAT,NJPPF)      :: ZPPRES
REAL(KIND=JPRB), DIMENSION(NLSAT,NPTNAV+2,NJPPF):: ZPROF
REAL(KIND=JPRB), DIMENSION(NPNSAV,NJPPF)     :: ZPROFS
REAL(KIND=JPRB), DIMENSION(NPNSSV,NJPPF)     :: ZPROFSS
REAL(KIND=JPRB), DIMENSION(NPNCV,NJPPF)      :: ZPROFCV
REAL(KIND=JPRB), DIMENSION(NJPPF*NMXTCHU)    :: ZEM5
REAL(KIND=JPRB), DIMENSION(NLSAT,NJPPF)      :: ZPPRES5
REAL(KIND=JPRB), DIMENSION(NLSAT,NPTNAV+2,NJPPF):: ZPROF5
REAL(KIND=JPRB), DIMENSION(NPNSAV,NJPPF)     :: ZPROFS5
REAL(KIND=JPRB), DIMENSION(NPNSSV,NJPPF)     :: ZPROFSS5
REAL(KIND=JPRB), DIMENSION(NPNCV,NJPPF)      :: ZPROFCV5
REAL(KIND=JPRB), DIMENSION(NJPPF)            :: ZLAT,ZOROG
REAL(KIND=JPRB), DIMENSION(NJPPF)            :: ZZENANG,ZAZIANG
REAL(KIND=JPRB), DIMENSION(NJPPF)            :: ZSOLZENANG,ZSOLAZIANG

INTEGER(KIND=JPIM) :: IPACK,IFIN,IEND,IDONE,ICHPF,IMXCHPF
INTEGER(KIND=JPIM) :: JJPACK,JJPF,JCH,JLEV,JF, JP
LOGICAL :: LL_LCLOUD, LL_LINTERP
LOGICAL, DIMENSION(4) :: LLTRACER
CHARACTER(LEN=80) :: CLERROR

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

!========================================================================
IF (LHOOK) CALL DR_HOOK('RADTRTL',0,ZHOOK_HANDLE)

LL_LCLOUD  = .FALSE.
LL_LINTERP = .FALSE.

!*    1.0  Number of packets
!*    ------------------------------------------------------------------

IPACK=KPROF/NJPPF+1
IFIN=MOD(KPROF,NJPPF)
IF(IFIN == 0) THEN
  IPACK=IPACK-1
  IFIN=NJPPF
ENDIF

!*    1.1  Pressure Pa--> hPa

DO JJPF=1,NJPPF
  ZPPRES5(:,JJPF)=RADPRE(:)/100._JPRB
  ZPPRES(:,JJPF) =0._JPRB
ENDDO

!*    1.2  Allocate RTTOV temporary arrays

IMXCHPF=KMXCH*NJPPF
CALL RTTOV_EC_ALLOC_TL(IFAIL, KSAT, IMXCHPF, NJPPF, NLSAT, &
 &    LL_LCLOUD, LL_LINTERP,                               &
 &    1_JPIM)


!*    2.0  Make packets, Call RT-code.
!*    ------------------------------------------------------------------

IDONE=0
PACKET_LOOP: DO JJPACK=1,IPACK
  IFAIL(:)=0

  IF(JJPACK == IPACK) THEN
    IEND=IFIN
  ELSE
    IEND=NJPPF
  ENDIF

!*    2.1  Profile variables, surface, T2m, q2m, Ts and Ps.
!*         Ozon, cloud pressure and amount, emissivity
!*         Surface indicators and angles

  DO JJPF=1,IEND
    ZPROF5(1:NLSAT,1,JJPF)=PTQOSAT5(JJPF+IDONE,1:NLSAT,1)  ! T
    ZPROF5(1:NLSAT,2,JJPF)=PTQOSAT5(JJPF+IDONE,1:NLSAT,2)  ! Q 
    ZPROF5(1:NLSAT,3,JJPF)=PTQOSAT5(JJPF+IDONE,1:NLSAT,3)  ! O3
    ZPROF(1:NLSAT,1,JJPF)=PTQOSAT(JJPF+IDONE,1:NLSAT,1)  ! T
    ZPROF(1:NLSAT,2,JJPF)=PTQOSAT(JJPF+IDONE,1:NLSAT,2)  ! Q 
    ZPROF(1:NLSAT,3,JJPF)=PTQOSAT(JJPF+IDONE,1:NLSAT,3)  ! O3

    ZPROFS5(1,JJPF)=PT2SAT5(JJPF+IDONE)
    ZPROFS5(2,JJPF)=PH2SAT5(JJPF+IDONE)
    ZPROFS5(3,JJPF)=PPSSAT5(JJPF+IDONE)/100._JPRB
    ZPROFS5(4,JJPF)=PUSSAT5(JJPF+IDONE)
    ZPROFS5(5,JJPF)=PVSSAT5(JJPF+IDONE)
    ZPROFSS5(1,JJPF)=PTSSAT5(JJPF+IDONE)
    ZPROFCV5(1,JJPF)=500._JPRB     ! cloud top pressure
    ZPROFCV5(2,JJPF)=0.0_JPRB      ! cloud amount
    ZPROFS(1,JJPF)=PT2SAT(JJPF+IDONE)
    ZPROFS(2,JJPF)=PH2SAT(JJPF+IDONE)
    ZPROFS(3,JJPF)=PPSSAT(JJPF+IDONE)/100._JPRB
    ZPROFS(4,JJPF)=PUSSAT(JJPF+IDONE)
    ZPROFS(5,JJPF)=PVSSAT(JJPF+IDONE)
    ZPROFSS(1,JJPF)=PTSSAT(JJPF+IDONE)
    ZPROFCV(1,JJPF)=0._JPRB       ! cloud top pressure
    ZPROFCV(2,JJPF)=0.0_JPRB      ! cloud amount

    ZZENANG(JJPF)=PZENANG(JJPF+IDONE)
    ZAZIANG(JJPF)=PAZIANG(JJPF+IDONE)
    ZSOLZENANG(JJPF)=PSOLZENANG(JJPF+IDONE)
    ZSOLAZIANG(JJPF)=PSOLAZIANG(JJPF+IDONE)
    ZLAT(JJPF)  =PLAT(JJPF+IDONE)
    ZOROG(JJPF) =POROG(JJPF+IDONE)/RG ! convert to m
    IF(PLSSAT5(JJPF+IDONE)  <=  0.5_JPRB) THEN
      ISURF(JJPF)=1
    ELSE
      ISURF(JJPF)=0
    ENDIF
  ENDDO
  ZPROF5(1:NLSAT,4:6,1:IEND)=0.0_JPRB
  ZPROF(1:NLSAT,4:6,1:IEND)=0.0_JPRB

!*    2.2  Additional gases

  LLTRACER(:)=.FALSE.
  IF(NGHG > 0) THEN
    DO JF=1,NGEMS
      DO JJPF=1,IEND
        IF(YDGEMSPROF5(JJPF+IDONE,JF)%LRTTOV) THEN
          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210061) JP=7 !CO2
          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210063) JP=8 !N2O
          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210062) JP=9 !CH4
          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210061) LLTRACER(1)=.TRUE. !CO2
          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210063) LLTRACER(2)=.TRUE. !N2O
          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210062) LLTRACER(3)=.TRUE. !CH4
          ZPROF(:,JP,JJPF)=YDGEMSPROF(JJPF+IDONE,JF)%RTTOVPROF(:)
          ZPROF5(:,JP,JJPF)=YDGEMSPROF5(JJPF+IDONE,JF)%RTTOVPROF(:)
        ENDIF
      ENDDO
    ENDDO
  ENDIF
! The following bit has not been tested yet
!  IF(NGRG > 0) THEN
!    DO JF=1,NGEMS
!      DO JJPF=1,IEND
!        IF(YDGEMSPROF5(JJPF+IDONE,JF)%LRTTOV) THEN
!          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210123) JP=10 !CO
!          IF(YDGEMSPROF5(JJPF+IDONE,JF)%IGRIB == 210123) LLTRACER(4)=.TRUE. !CO
!          ZPROF(:,JP,JJPF)=YDGEMSPROF(JJPF+IDONE,JF)%RTTOVPROF(:)
!          ZPROF5(:,JP,JJPF)=YDGEMSPROF5(JJPF+IDONE,JF)%RTTOVPROF(:)
!        ENDIF
!      ENDDO
!    ENDDO
!  ENDIF

!*   2.3 CO2

  IF(LCO2) THEN
    DO JJPF=1,IEND
      ZPROF(JLEV,7,JJPF)=PTQOSAT(JJPF+IDONE,JLEV,4)
      ZPROF5(JLEV,7,JJPF)=PTQOSAT5(JJPF+IDONE,JLEV,4)
    ENDDO
  ENDIF


  ZPROF5(1:NLSAT,4,1:IEND)=0.0_JPRB
  ZPROF (1:NLSAT,4,1:IEND)=0.0_JPRB

!*    2.5  Surface, T2m, q2m, Ts and Ps.
!*         cloud pressure and amount, emissivity
!*         Surface indicators and angles

  PF_LOOP_S: DO JJPF=1,IEND

    IF(PPSSAT5(JJPF+IDONE)/100._JPRB > 1080.0) PRINT*,'RADTRTL:',JJPF,IEND,IDONE,PPSSAT5(JJPF+IDONE)/100._JPRB, &
      &PT2SAT5(JJPF+IDONE),minval(ZPROF5(:,5,JJPF)),maxval(ZPROF5(:,5,JJPF))
    ZPROFS5 (1,JJPF)=PT2SAT5(JJPF+IDONE)
    ZPROFS  (1,JJPF)=PT2SAT (JJPF+IDONE)
    ZPROFS5 (2,JJPF)=PH2SAT5(JJPF+IDONE)
    ZPROFS  (2,JJPF)=PH2SAT (JJPF+IDONE)
    ZPROFS5 (3,JJPF)=PPSSAT5(JJPF+IDONE)/100._JPRB
    ZPROFS  (3,JJPF)=PPSSAT (JJPF+IDONE)/100._JPRB
    ZPROFS5 (4,JJPF)=PUSSAT5(JJPF+IDONE)
    ZPROFS  (4,JJPF)=PUSSAT (JJPF+IDONE)
    ZPROFS5 (5,JJPF)=PVSSAT5(JJPF+IDONE)
    ZPROFS  (5,JJPF)=PVSSAT (JJPF+IDONE)
    ZPROFSS5(1,JJPF)=PTSSAT5(JJPF+IDONE)
    ZPROFSS (1,JJPF)=PTSSAT (JJPF+IDONE)
    
   IF(LTOVSCV .AND. LCLDSINK) THEN
    ZPROFCV5(1,JJPF)=MIN(MAX(PCTOP5(JJPF+IDONE),51.0_JPRB),1012.0_JPRB)       ! cloud top pressure
    ZPROFCV(1,JJPF)=PCTOP(JJPF+IDONE)       ! cloud top pressure
    ZPROFCV5(2,JJPF)=MIN(MAX(PCAMT5(JJPF+IDONE),0.0_JPRB),1.0_JPRB) ! cloud amount
    ZPROFCV(2,JJPF)=PCAMT(JJPF+IDONE)      ! cloud amount
   ELSEIF ((KSENSOR == MSENSOR_AIRS).AND.LCO2_DIAG_AIRS) THEN
    ZPROFCV5(1,JJPF)=PCTOP5(JJPF+IDONE)      ! cloud top pressure
    ZPROFCV(1,JJPF)=0.0_JPRB
    ZPROFCV5(2,JJPF)=PCAMT5(JJPF+IDONE)      ! cloud amount
    ZPROFCV(2,JJPF)=0.0_JPRB
   ELSE
    ZPROFCV5(1,JJPF)=555.0_JPRB      ! cloud top pressure
    ZPROFCV(1,JJPF)=0.0_JPRB                                          
    ZPROFCV5(2,JJPF)=0.0_JPRB        ! cloud amount
    ZPROFCV(2,JJPF)=0.0_JPRB                                          
   ENDIF
  
    ZZENANG(JJPF)=PZENANG(JJPF+IDONE)
    ZAZIANG(JJPF)=PAZIANG(JJPF+IDONE)
    ZSOLZENANG(JJPF)=PSOLZENANG(JJPF+IDONE)
    ZSOLAZIANG(JJPF)=PSOLAZIANG(JJPF+IDONE) 
    ZLAT(JJPF)  =PLAT(JJPF+IDONE)
    ZOROG(JJPF) =POROG(JJPF+IDONE)
    IF (PLSSAT5(JJPF+IDONE)  <=  0.5_JPRB)THEN
      ISURF(JJPF)=1
    ELSE
      ISURF(JJPF)=0
    ENDIF
  ENDDO PF_LOOP_S

!*    2.4  Set indices of TOVS channels
!*         Perform radtiative transfer calculations

  ICHPF=0
  PF_LOOP_RI: DO JJPF=1,IEND
    INCH(JJPF)=KNCH(JJPF+IDONE)
    DO JCH=1,KNCH(JJPF+IDONE)
      ICHPF=ICHPF+1
      ICHID(ICHPF)=NINT(PRCH(JJPF+IDONE,JCH))
      IPFID(ICHPF)=JJPF
      ZEM5(ICHPF)=PEMIS5(JJPF+IDONE,JCH)
      ZEM(ICHPF)=PEMIS(JJPF+IDONE,JCH)
    ENDDO
  ENDDO PF_LOOP_RI
    
  ! Reset channel numbers to the values in the reduced set initialised in 
  ! RTTVI
  SELECT CASE (KSENSOR)
     CASE (MSENSOR_AIRS)
         ICHID(1:ICHPF) = AIRSCHAN_BACK(ICHID(1:ICHPF))
     CASE (MSENSOR_IASI)
         ICHID(1:ICHPF) = IASICHAN_BACK(ICHID(1:ICHPF))
  END SELECT
  IF (ANY(ICHID(1:ICHPF) == -NMDI)) &
       & CALL ABOR1('Error in channel subsetting for RTTOV')

  CALL RTTOV_EC_TL(IFAIL, KSAT, ICHPF, IEND, NLSAT,           &
   &    ICHID, IPFID,                                         &
   &    ZPPRES5,ZPROF5,ZPROFS5,ZPROFSS5,ISURF,ZEM5,ZPROFCV5,  &
   &    ZPPRES, ZPROF, ZPROFS, ZPROFSS,       ZEM, ZPROFCV,   &
   &    ZZENANG, ZAZIANG, ZSOLZENANG, ZSOLAZIANG, ZLAT, ZOROG,&
   &    LL_LCLOUD, LL_LINTERP,LLTRACER,                        &
   &    ZTBCLR5,ZTBCLR,ZTBTOT5,ZTBTOT)

!*    2.6  Construct output array containing model radiances

  ICHPF=0
  IF(ANY(IFAIL(:) == 2))THEN
    WRITE(CLERROR, *) 'Problems in RTTOV call for satellite ',ksat
    CALL ABOR1(CLERROR)
  ENDIF
  PF_LOOP_RO: DO JJPF=1,IEND
    IF(IFAIL(JJPF) /= 0)THEN
      DO JCH=1,KNCH(JJPF+IDONE)
        ICHPF=ICHPF+1
        PTBCLR(JJPF+IDONE,JCH)=RMDI
      ENDDO
    ELSE
      DO JCH=1,KNCH(JJPF+IDONE)
        ICHPF=ICHPF+1
        PTBCLR(JJPF+IDONE,JCH)=ZTBCLR(ICHPF)
      ENDDO
    ENDIF
  ENDDO PF_LOOP_RO

  IDONE=IDONE+IEND

ENDDO PACKET_LOOP

!*    3.0  Deallocate RTTOV temporary arrays

CALL RTTOV_EC_ALLOC_TL(IFAIL, KSAT, IMXCHPF, NJPPF, NLSAT, &
 &    LL_LCLOUD, LL_LINTERP,                               &
 &    0_JPIM)

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