Skip to content
Snippets Groups Projects
call_rttov.f90 63 KiB
Newer Older
  • Learn to ignore specific revisions
  • !MNH_LIC Copyright 1994-2014 CNRS, Meteo-France and Universite Paul Sabatier
    
    !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
    
    !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt  
    
    !MNH_LIC for details. version 1.
    
    !-----------------------------------------------------------------
    !--------------- special set of characters for RCS information
    !-----------------------------------------------------------------
    ! $Source$ $Revision$
    !-----------------------------------------------------------------
    !    ######################
         MODULE MODI_CALL_RTTOV
    !    ######################
    INTERFACE
    !
         SUBROUTINE CALL_RTTOV(KDLON, KFLEV, KSTATM, PEMIS, PTSRAD, PSTATM,     &
                    PTHT, PRT, PPABST, PZZ, PMFCONV, PCLDFR, PULVLKB, PVLVLKB,  &
                    OUSERI, KRTTOVINFO, HFMFILE    )
    !
    INTEGER, INTENT(IN)   :: KDLON !number of columns where the
                                   !radiation calculations are performed
    INTEGER, INTENT(IN)   :: KFLEV !number of vertical levels where the
                                   !radiation calculations are performed
    INTEGER, INTENT(IN)   :: KSTATM  !index of the standard atmosphere level
                                     !just above the model top
    !
    !
    REAL, DIMENSION(:,:),     INTENT(IN) :: PEMIS  !Surface IR EMISsivity
    REAL, DIMENSION(:,:),     INTENT(IN) :: PTSRAD !RADiative Surface Temperature
    REAL, DIMENSION(:,:),     INTENT(IN) :: PSTATM !selected standard atmosphere
    !
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PTHT   !THeta at t
    REAL, DIMENSION(:,:,:,:), INTENT(IN) :: PRT    !moist variables at t
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PPABST !pressure at t
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PZZ    !Model level heights
    !
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PMFCONV! convective mass flux (kg /s m^2)
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PCLDFR  ! cloud fraction
    REAL, DIMENSION(:,:),     INTENT(IN) :: PULVLKB ! U-wind at KB level
    REAL, DIMENSION(:,:),     INTENT(IN) :: PVLVLKB ! V-wind at KB level
    !
    LOGICAL, INTENT(IN)                  :: OUSERI ! logical switch to compute both
                                                   ! liquid and solid condensate (OUSERI=.TRUE.)
                                                   ! or only liquid condensate (OUSERI=.FALSE.)
    !
    INTEGER, DIMENSION(:,:), INTENT(IN) :: KRTTOVINFO ! platform, satelit, sensor,
                                                      ! and selection calculations
    CHARACTER(LEN=28), INTENT(IN) :: HFMFILE      ! Name of FM-file to write
    !
    END SUBROUTINE CALL_RTTOV
    END INTERFACE
    END MODULE MODI_CALL_RTTOV
    !    #####################################################################
    SUBROUTINE CALL_RTTOV(KDLON, KFLEV, KSTATM, PEMIS, PTSRAD, PSTATM,     &
               PTHT, PRT, PPABST, PZZ, PMFCONV, PCLDFR, PULVLKB, PVLVLKB,  &
               OUSERI, KRTTOVINFO, HFMFILE    )
    !    #####################################################################
    !!
    !!****  *CALL_RTTOV* - 
    !!
    !!    PURPOSE
    !!    -------
    !!
    !!**  METHOD
    !!    ------
    !!
    !!    EXTERNAL
    !!    --------
    !!
    !!    IMPLICIT ARGUMENTS
    !!    ------------------
    !!
    !!    REFERENCE
    !!    ---------
    !!    See Chaboureau and Pinty, 2006
    !!    Validation of a cirrus parameterization with Meteosat Second Generation
    !!    observations. Geophys. Res. Let., doi:10.1029/2005GL024725
    !!
    !!    AUTHOR
    !!    ------
    !!      J.-P. Chaboureau       *L.A.*
    !!
    !!    MODIFICATIONS
    !!    -------------
    !!      Original    11/12/03
    !!      JP Chaboureau 27/03/2008 Vectorization
    !!      JP Chaboureau 02/11/2009 move GANGL deallocation outside the sensor loop
    
    !!      J.Escobar : 15/09/2015 : WENO5 & JPHEXT <> 1 
    
    !!----------------------------------------------------------------------------
    !!
    !!*       0.    DECLARATIONS
    !!              ------------
    !!
    
    94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    USE MODD_CST
    USE MODD_PARAMETERS
    USE MODD_GRID_n
    USE MODD_LUNIT_n
    USE MODD_DEEP_CONVECTION_n
    USE MODD_REF_n
    USE MODD_RADIATIONS_n,  ONLY : XSEA
    !
    USE MODN_CONF
    !                                
    USE MODD_RAD_TRANSF
    !                               
    USE MODI_DETER_ANGLE
    USE MODI_PINTER
    !
    USE MODE_FMWRIT
    USE MODE_FMREAD
    USE MODE_ll
    USE MODE_FM
    USE MODE_IO_ll
    USE MODE_POS
    !
    USE rttov_const, ONLY :  &
         &  gas_id_watervapour  ,&
         & errorstatus_success,&
         & errorstatus_warning,&
         & errorstatus_fatal,&
         & sensor_id_mw,        &
         & npolar_return,       &
         & npolar_compute
    
    
    USE rttov_types, ONLY : &
         & geometry_type  ,&
         & rttov_coef     ,&
         & rttov_scatt_coef     ,&
         & profile_type   ,&
         & profile_cloud_type   ,&
         & transmission_type    ,&
         & radiance_cloud_type
    
    USE MOD_CPARAM, ONLY : jppf   ! Max no. profiles
    
    USE parkind1, ONLY : jpim     ,jprb
    !
    IMPLICIT NONE
    !
    ! -----------------------------------------------------------------------------
    INTERFACE
    
    !!! #include "rttov_setupindex.interface
    SUBROUTINE rttov_setupindex (&
         & mchan,           & ! in
         & nprofiles,       & ! in
         & nfrequencies,    & ! in
         & nchannels,       & ! in
         & nbtout,          & ! in
         & coef,            & ! in
         & surfem,          & ! in
         & lprofiles,       & ! out
         & channels,        & ! out
         & polarisations,   & ! out
         & emissivity)        ! out
    ! Imported Type Definitions:
    USE rttov_types, ONLY : &
         rttov_coef
    USE rttov_const, ONLY :   &
         sensor_id_mw,     &
         npolar_return,    &
         npolar_compute
    
    USE parkind1, ONLY : jpim     ,jprb
    IMPLICIT NONE
    ! Subroutine arguments
    INTEGER(Kind=jpim),  INTENT(in)    :: nprofiles          ! Number of profiles
    INTEGER(Kind=jpim),  INTENT(in)    :: mchan(nprofiles)   ! nfrequencies/nprofiles
    INTEGER(Kind=jpim),  INTENT(in)    :: nchannels          ! Number of radiances computed
    INTEGER(Kind=jpim),  INTENT(in)    :: nfrequencies       ! Number of frequencies
    !  (= channels used * profiles)
    INTEGER(Kind=jpim),  INTENT(in)    :: nbtout             ! Number of BTs returned
    INTEGER(Kind=jpim),  INTENT(out)   :: channels(nfrequencies)      ! Channel indices
    INTEGER(Kind=jpim),  INTENT(out)   :: polarisations(nchannels,3)  ! Channel indices
    INTEGER(Kind=jpim),  INTENT(out)   :: lprofiles(nfrequencies)     ! Profiles indices
    REAL(Kind=jprb),     INTENT(in)    :: surfem(nchannels)           ! Input surface emissivity
    REAL(Kind=jprb),     INTENT(out)   :: emissivity(nchannels)       ! Surface emissivity array for RTTOV
    TYPE( rttov_coef ),  INTENT (in)   :: coef               ! coefficients
    END SUBROUTINE rttov_setupindex
    !
    !!! #include "rttov_setupchan.interface"
    SUBROUTINE rttov_setupchan (&
         & nprofiles,       & ! in
         & nchan,           & ! in
         & coef,            & ! in
         & nfrequencies,    & ! out
         & nchannels,       & ! out
         & nbtout)            ! out
      ! Imported Type Definitions:
    USE rttov_types, ONLY : &
         rttov_coef
    USE rttov_const, ONLY :   &
         sensor_id_mw,     &
         npolar_return,    &
         npolar_compute
    USE parkind1, ONLY : jpim
    IMPLICIT NONE
    ! Subroutine arguments
    INTEGER(Kind=jpim),  INTENT(in)    :: nprofiles        ! Number of profiles
    INTEGER(Kind=jpim),  INTENT(in)    :: nchan(nprofiles) ! Number of channels requested
    TYPE( rttov_coef ),  INTENT (in)   :: coef             ! coefficients
    INTEGER(Kind=jpim),  INTENT(out)   :: nchannels        ! Number of radiances computed
    INTEGER(Kind=jpim),  INTENT(out)   :: nfrequencies     ! Number of frequencies
    !  (= channels used * profiles)
    INTEGER(Kind=jpim),  INTENT(out)   :: nbtout           ! Number of BTs returned
    END SUBROUTINE rttov_setupchan
    !
    !!! #include "rttov_scatt_setupindex.interface"
    SUBROUTINE rttov_scatt_setupindex (nprofiles, n_chan, coef, nchannels, &
         & lsprofiles,lsprofiles2, frequencies, nbtout)
    USE parkind1     , ONLY: jpim, jprb
    USE rttov_const, ONLY : npolar_return, npolar_compute, &
         & inst_id_ssmi
    USE rttov_types, ONLY : rttov_coef     
    IMPLICIT NONE
    INTEGER (kind=jpim), INTENT ( in) :: nprofiles
    INTEGER (kind=jpim), INTENT ( in) :: nchannels
    INTEGER (kind=jpim), INTENT ( in) :: nbtout
    INTEGER (kind=jpim), INTENT ( in) :: n_chan (nprofiles)    
    TYPE   (rttov_coef), INTENT ( in) :: coef   
    INTEGER (kind=jpim), INTENT (out), DIMENSION (nchannels)    :: lsprofiles   
    INTEGER (kind=jpim), INTENT (out), DIMENSION (nbtout)       :: lsprofiles2  
    INTEGER (kind=jpim), INTENT (out), DIMENSION (nchannels)    :: frequencies
    END SUBROUTINE rttov_scatt_setupindex
    !
    !!! #include "rttov_cld.interface"
    SUBROUTINE rttov_cld( &
         errorstatus,     & ! out
         nfrequencies,    & ! in
         nchannels,       & ! in
         nbtout,          & ! in
         nprofiles,       & ! in
         channels,        & ! in
         polarisations,   & ! in
         lprofiles,       & ! in
         profiles,        & ! inout  (to invalid clw absorption)
         cld_profiles,    & ! in
         coef,            & ! in
         calcemis,        & ! in
         emissivity,      & ! inout
         cld_radiance )     ! inout
    USE rttov_const, ONLY :   &
         errorstatus_success ,&
         errorstatus_fatal   ,&
         overlap_scheme
    USE rttov_types, ONLY :    &
         rttov_coef           ,&
         geometry_Type        ,&
         profile_Type         ,&
         profile_cloud_Type   ,&
         transmission_Type    ,&
         radiance_Type        ,&
         radiance_cloud_Type
    USE parkind1, ONLY : jpim     ,jprb
    IMPLICIT NONE
    INTEGER(Kind=jpim),        INTENT(in)    :: nbtout  ! Number of output radiances
    INTEGER(Kind=jpim),        INTENT(in)    :: nfrequencies  ! Number of output radiances
    INTEGER(Kind=jpim),        INTENT(in)    :: nchannels
    INTEGER(Kind=jpim),        INTENT(in)    :: nprofiles
    INTEGER(Kind=jpim),        INTENT(in)    :: channels(nfrequencies)
    INTEGER(Kind=jpim),        INTENT(in)    :: polarisations(nchannels,3)      ! Channel indices
    INTEGER(Kind=jpim),        INTENT(in)    :: lprofiles(nfrequencies)
    TYPE(profile_Type),        INTENT(inout) :: profiles(nprofiles) ! Profiles on RTTOV levels
    TYPE(profile_cloud_Type),  INTENT(in)    :: cld_profiles(nprofiles) ! Cloud profiles on NWP levels
    TYPE(rttov_coef),          INTENT(in)    :: coef  ! Coefficients
    LOGICAL,                   INTENT(in)    :: calcemis(nchannels)  ! switch for emmissivity calc.
    REAL(Kind=jprb),           INTENT(inout) :: emissivity(nchannels) ! surface emmissivity
    TYPE(radiance_cloud_Type), INTENT(inout) :: cld_radiance    ! radiances (mw/cm-1/ster/sq.m)
    INTEGER(Kind=jpim),        INTENT(out)   :: errorstatus(nprofiles)  ! return flag
    END SUBROUTINE rttov_cld
    
    !!! #include "rttov_cld_k.interface"
    SUBROUTINE Rttov_cld_k  ( &
         errorstatus,    & ! out
         nfrequencies,   & ! in
         nchannels,      & ! in
         nbtout,         & ! in
         nprofiles,      & ! in
         channels,       & ! in
         polarisations,  & ! in
         lprofiles,      & ! in
         profiles,       & ! in
         cld_profiles,   & ! in
         coef,           & ! in
         switchrad,      & ! in
         calcemis,       & ! in
         emissivity,     & ! inout
         profiles_k ,    & ! inout
         cld_profiles_k ,& ! inout
         emissivity_k ,  & ! inout
         cld_radiance)     ! inout
    USE rttov_const, ONLY :   &
         errorstatus_success ,&
         errorstatus_fatal   ,&
         overlap_scheme
    USE rttov_types, ONLY :    &
         rttov_coef           ,&
         geometry_Type        ,&
         profile_Type         ,&
         profile_cloud_Type   ,&
         radiance_cloud_Type
    USE parkind1, ONLY : jpim     ,jprb
    IMPLICIT NONE
    INTEGER(Kind=jpim),        INTENT(in)    :: nfrequencies
    INTEGER(Kind=jpim),        INTENT(in)    :: nchannels
    INTEGER(Kind=jpim),        INTENT(in)    :: nbtout
    INTEGER(Kind=jpim),        INTENT(in)    :: nprofiles
    INTEGER(Kind=jpim),        INTENT(in)    :: channels(nfrequencies)
    INTEGER(Kind=jpim),        INTENT(in)    :: polarisations(nchannels,3)
    INTEGER(Kind=jpim),        INTENT(in)    :: lprofiles(nfrequencies)
    LOGICAL,                   INTENT(in)    :: switchrad  ! true if input is BT
    TYPE(profile_Type),        INTENT(inout) :: profiles(nprofiles)
    TYPE(profile_cloud_Type),  INTENT(in)    :: cld_profiles(nprofiles)
    TYPE(rttov_coef),          INTENT(in)    :: coef
    LOGICAL,                   INTENT(in)    :: calcemis(nchannels)
    REAL(Kind=jprb),           INTENT(inout) :: emissivity(nchannels)
    TYPE(radiance_cloud_type), INTENT(inout) :: cld_radiance! in because of meme allocation
    TYPE(profile_Type),        INTENT(inout) :: profiles_k(nchannels)
    TYPE(profile_cloud_Type),  INTENT(inout) :: cld_profiles_k(nchannels)
    REAL(Kind=jprb),           INTENT(inout) :: emissivity_k(nchannels)
    INTEGER(Kind=jpim),        INTENT(out)   :: errorstatus(nprofiles)
    END SUBROUTINE Rttov_cld_k
    
    
    !!! #include "rttov_scatt.interface"
    SUBROUTINE rttov_scatt(&
         & errorstatus,&
         & nwp_levels,&
         & nrt_levels,&
         & nfrequencies,&
         & nchannels,&
         & nbtout,&
         & nprofiles,&
         & polarisations,&
         & channels,&
         & frequencies,&
         & lprofiles,&
         & lsprofiles,&
         & profiles,&
         & cld_profiles,&
         & coef_rttov,&
         & coef_scatt,&
         & calcemiss,&
         & emissivity_in,&
         & cld_radiance ) 
    USE rttov_types, ONLY :&
         & rttov_coef ,&
         & rttov_scatt_coef ,&
         & geometry_Type ,&
         & profile_Type ,&
         & profile_cloud_Type ,&
         & profile_scatt_aux ,&
         & transmission_Type ,&
         & radiance_Type ,&
         & radiance_cloud_Type 
    USE parkind1, ONLY : jpim ,jprb
    INTEGER (Kind=jpim), INTENT (in) :: nwp_levels
    INTEGER (Kind=jpim), INTENT (in) :: nrt_levels
    INTEGER (Kind=jpim), INTENT (in) :: nprofiles
    INTEGER (Kind=jpim), INTENT (in) :: nfrequencies
    INTEGER (Kind=jpim), INTENT (in) :: nchannels
    INTEGER (Kind=jpim), INTENT (in) :: nbtout
    INTEGER (Kind=jpim), INTENT (in) :: channels (nfrequencies)
    INTEGER (Kind=jpim), INTENT (in) :: frequencies (nchannels)
    INTEGER (Kind=jpim), INTENT (in) :: polarisations (nchannels,3)
    INTEGER (Kind=jpim), INTENT (in) :: lprofiles (nfrequencies)
    INTEGER (Kind=jpim), INTENT (in) :: lsprofiles (nchannels)
    INTEGER (Kind=jpim), INTENT (out) :: errorstatus (nprofiles)
    LOGICAL, INTENT (in) :: calcemiss (nchannels)
    REAL (Kind=jprb), INTENT (in) :: emissivity_in (nchannels)
    TYPE (profile_Type), INTENT (inout) :: profiles (nprofiles)
    TYPE (rttov_coef), INTENT (in) :: coef_rttov
    TYPE (rttov_scatt_coef), INTENT (in) :: coef_scatt
    TYPE (profile_cloud_Type), INTENT (in) :: cld_profiles (nprofiles)
    TYPE (radiance_cloud_Type), INTENT (inout) :: cld_radiance
    END SUBROUTINE rttov_scatt
    
    !!! #include "rttov_readcoeffs.interface"
    SUBROUTINE rttov_readcoeffs  (&
         & errorstatus,  & ! out
         & coef,         & ! out
         & instrument,   & ! in Optional
         & kmyproc,      & ! in Optional
         & kioproc,      & ! in Optional
         & file_id,      & ! in Optional
         & channels      ) ! in Optional
    USE rttov_const, ONLY :   &
         version             ,&
         release             ,&
         minor_version       ,&
         rttov_magic_string  ,&
         sensor_id_mw        ,&
         sensor_id_ir        ,&
         errorstatus_info    ,&
         errorstatus_success ,&
         errorstatus_fatal   ,&
         gas_id_mixed        ,&
         gas_id_watervapour  ,&
         gas_id_ozone        ,&
         gas_id_wvcont       ,&
         gas_id_co2          ,&
         gas_id_n2o          ,&
         gas_id_co           ,&
         gas_id_ch4          ,&
         gas_unit_specconc   ,&
         gas_unit_ppmv       ,&
         earthradius         ,&
         gas_name            ,&
         pressure_top
    USE rttov_types, ONLY : &
         rttov_coef
    USE parkind1, ONLY : jpim     ,jprb
    IMPLICIT NONE
    INTEGER(Kind=jpim), OPTIONAL, INTENT(in) :: kmyproc  ! logical processor id
    INTEGER(Kind=jpim), OPTIONAL, INTENT(in) :: kioproc  ! processor dedicated for io
    INTEGER(Kind=jpim), OPTIONAL, INTENT (in) :: instrument(3)  ! (platform, satellite identification, instrument) number
    INTEGER(Kind=jpim), OPTIONAL, INTENT (in) :: file_id      ! file logical unit number
    INTEGER(Kind=jpim), OPTIONAL, INTENT (in) :: channels(:)      ! list of channels to extract
    INTEGER(Kind=jpim), INTENT (out) :: errorstatus       ! return code
    TYPE( rttov_coef ), INTENT (out) :: coef   ! coefficients
    END SUBROUTINE rttov_readcoeffs
    
    !!! #include "rttov_initcoeffs.interface"
    SUBROUTINE rttov_initcoeffs  (&
         & errorstatus,   &! out
         & coef,          &! out
         & knproc,        &! in Optional
         & kmyproc,       &! in Optional
         & kioproc        )! in Optional
    USE rttov_const, ONLY :   &
         & sensor_id_mw        ,&
         & errorstatus_info    ,&
         & errorstatus_success ,&
         & errorstatus_fatal   ,&
         & gas_id_mixed        ,&
         & gas_id_watervapour  ,&
         & gas_id_ozone        ,&
         & gas_id_wvcont       ,&
         & gas_id_co2          ,&
         & gas_id_n2o          ,&
         & gas_id_co           ,&
         & gas_id_ch4          ,&
         & gas_unit_specconc   ,&
         & gas_unit_ppmv       ,&
         & earthradius         ,&
         & gas_name            ,&
         & pressure_top
    ! Imported Type Definitions:
    USE rttov_types, ONLY : &
         & rttov_coef
    USE parkind1, ONLY : jpim     ,jprb
    IMPLICIT NONE
    INTEGER(Kind=jpim), OPTIONAL, INTENT(in) :: knproc   ! number of procs
    INTEGER(Kind=jpim), OPTIONAL, INTENT(in) :: kmyproc  ! logical processor id
    INTEGER(Kind=jpim), OPTIONAL, INTENT(in) :: kioproc  ! procs dedicated for io
    ! scalar arguments with intent(out):
    INTEGER(Kind=jpim), INTENT (out) :: errorstatus       ! return code
    TYPE( rttov_coef ), INTENT (out) :: coef   ! coefficients
    END SUBROUTINE rttov_initcoeffs
    
    !!! #include "rttov_readscattcoeffs.interface"
    SUBROUTINE rttov_readscattcoeffs  (&
         & errorstatus,   &! out
         & coef_rttov,    &! in
         & coef_scatt,    &! out
         & file_id       ) ! in Optional
    ! Imported Type Definitions:
    USE rttov_types, ONLY : &
         & rttov_coef, &
         & rttov_scatt_coef
    USE rttov_const, ONLY :   &
         & inst_name           ,&
         & platform_name       ,&
         & errorstatus_info    ,&
         & errorstatus_success ,&
         & errorstatus_fatal
    USE parkind1, ONLY : jpim     ,jprb
    IMPLICIT NONE
    ! subroutine arguments
    ! scalar arguments with intent(out):
    INTEGER(Kind=jpim), INTENT (out) :: errorstatus             ! return code
    ! scalar arguments with optional intent(in):
    INTEGER(Kind=jpim), OPTIONAL, INTENT (in)  :: file_id       ! file logical unit number
    ! array arguments with intent(in):
    TYPE( rttov_coef ), INTENT (in) :: coef_rttov               ! clear-sky coefficients
    ! array arguments with intent(out):
    TYPE( rttov_scatt_coef ), INTENT (out) :: coef_scatt        ! coefficients
    END SUBROUTINE rttov_readscattcoeffs
    
    END INTERFACE
    !!! #include "rttov_opencoeff.interface"
    !!! #include "rttov_errorhandling.interface"
    !!! #include "rttov_dealloc_coef.interface"
    !!! #include "rttov_errorreport.interface"
    
    !!!
    !!!*       0.1   DECLARATIONS OF DUMMY ARGUMENTS :
    !!!
    INTEGER, INTENT(IN)   :: KDLON   !number of columns where the
    ! radiation calculations are performed
    INTEGER, INTENT(IN)   :: KFLEV   !number of vertical levels where the
    ! radiation calculations are performed
    INTEGER, INTENT(IN)   :: KSTATM  !index of the standard atmosphere level
                                    !just above the model top
    !!!
    REAL, DIMENSION(:,:),     INTENT(IN) :: PEMIS  !Surface IR EMISsivity
    REAL, DIMENSION(:,:),     INTENT(IN) :: PTSRAD !RADiative Surface Temperature
    REAL, DIMENSION(:,:),     INTENT(IN) :: PSTATM !selected standard atmosphere
                                    !
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PTHT   !THeta at t
    REAL, DIMENSION(:,:,:,:), INTENT(IN) :: PRT    !moist variables at t
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PPABST !pressure at t
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PZZ    !Model level heights
    !!!
    !!!
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PMFCONV! convective mass flux (kg /s m^2)
    REAL, DIMENSION(:,:,:),   INTENT(IN) :: PCLDFR  ! cloud fraction
    REAL, DIMENSION(:,:),     INTENT(IN) :: PULVLKB ! U-wind at KB level
    REAL, DIMENSION(:,:),     INTENT(IN) :: PVLVLKB ! V-wind at KB level
    !!!
    LOGICAL, INTENT(IN)                  :: OUSERI ! logical switch to compute both
    ! liquid and solid condensate (OUSERI=.TRUE.)
    ! or only liquid condensate (OUSERI=.FALSE.)
    !!!
    INTEGER, DIMENSION(:,:), INTENT(IN) :: KRTTOVINFO ! platform, satelit, sensor,
                                                      ! and selection calculations
    CHARACTER(LEN=28), INTENT(IN) :: HFMFILE      ! Name of FM-file to write
    
    !!!
    !!!*       0.2   DECLARATIONS OF LOCAL VARIABLES
    !!!
    !!!
    INTEGER, PARAMETER :: JPNSAT=3       ! No. of Satellite required
                                    !
    INTEGER :: JI,JJ,JK,JK1,JK2,JKRAD,JKF,JSAT,JC ! loop indexes
                                    !
    INTEGER :: IJSAT        ! number of columns/=NUNDEF which 
                            ! have to be treated in the table KRTTOVINFO(:,:)
    INTEGER :: IIB,IIE        ! I index value of the first/last inner mass point
    INTEGER :: IJB,IJE        ! J index value of the first/last inner mass point
    INTEGER :: IKB,IKE        ! K index value of the first/last inner mass point
    INTEGER :: IIU          ! array size for the first  index
    INTEGER :: IJU          ! array size for the second index
    INTEGER :: IKU          ! array size for the third  index
    INTEGER :: IKR          ! real array size for the third  index
    INTEGER (Kind=jpim) :: iwp_levels ! equal to IKR (call to rttov_scatt)
    INTEGER :: IIJ          ! reformatted array index
    INTEGER :: IKSTAE       ! level number of the STAndard atmosphere array
    INTEGER :: IKUP         ! vertical level above which STAndard atmosphere data
    INTEGER, DIMENSION(SIZE(PZZ,1),SIZE(PZZ,3)) :: IKKOZ ! indice array used to
    ! vertically interpolate the ozone content on the model grid
                                    !
    REAL, DIMENSION(:,:), ALLOCATABLE :: ZPAVE  ! mean-layer pressure
    REAL, DIMENSION(:,:), ALLOCATABLE :: ZTAVE  ! mean-layer temperature
    REAL, DIMENSION(:,:), ALLOCATABLE :: ZQVAVE ! mean-layer specific humidity
    REAL, DIMENSION(:,:), ALLOCATABLE :: ZO3AVE ! mean-layer ozone content
    REAL, DIMENSION(:),   ALLOCATABLE :: ZREMIS ! Reformatted PEMIS array
    REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZEXNT ! Exner function
    REAL, DIMENSION(SIZE(PSTATM,1)) :: ZSTAZZ,ZSTAOZ ! STAndard atmosphere height
    !    and OZone content
    REAL :: ZOZ ! variable used to interpolate the ozone profile
    
    REAL, DIMENSION(:),   ALLOCATABLE   ::  ZULAT
    REAL, DIMENSION(:),   ALLOCATABLE   ::  ZULON
    
    REAL, DIMENSION(:,:,:), ALLOCATABLE   ::  ZTBTMP
    REAL, DIMENSION(:,:), ALLOCATABLE :: ZANTMP, ZUTH
    REAL :: ZZH, zdeg_to_rad, zrad_to_deg, zbeta, zalpha
    
    ! Other arrays for zenithal solar angle 
    ! REAL, DIMENSION(:,:),   ALLOCATABLE :: ZCOSZEN, ZSINZEN, ZAZIMSOL
    
    ! Other arrays for condensation
    REAL, DIMENSION(:,:,:), ALLOCATABLE  :: ZTEMP  ! Temperature
    REAL, DIMENSION(:,:,:), ALLOCATABLE  :: ZNCLD  ! grid scale cloud fraction
    REAL, DIMENSION(:,:,:), ALLOCATABLE  :: ZRC    ! grid scale r_c (kg/kg)
    REAL, DIMENSION(:,:,:), ALLOCATABLE  :: ZRI    ! grid scale r_i (kg/kg)
    REAL, DIMENSION(:,:,:), ALLOCATABLE  :: ZRR    ! grid scale r_r (kg/kg)
    REAL, DIMENSION(:,:,:), ALLOCATABLE  :: ZRS    ! grid scale r_s (kg/kg)
    ! -----------------------------------------------------------------------------
    INTEGER, PARAMETER :: JPLEV=43, JPNAV=3, JPNSAV=5, JPNSSV=6, JPNCVCLD=6  
    
    REAL, DIMENSION(JPLEV) ::  ZPRES !Fixed level pressures used in RTTOV
    REAL, DIMENSION(JPLEV) ::  ZPRES_INV
    REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZAV    !Profile array content
    REAL, DIMENSION(:,:),   ALLOCATABLE :: ZSAV   !Surface array content
    REAL, DIMENSION(:,:),   ALLOCATABLE :: ZSSV   !Surface Skin array content
    REAL, DIMENSION(:,:),   ALLOCATABLE :: ZAP    !Full-level Model Pressure (hPa)
    REAL, DIMENSION(:,:),   ALLOCATABLE :: ZAP_HL !Half-level Model Pressure (hPa)
    REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZCV    !Temperature and cloud variable
                                    !on full-level model
    REAL, DIMENSION(:), ALLOCATABLE :: ZANGL   !Satellite zenith angle (deg)
    REAL, DIMENSION(:), ALLOCATABLE :: ZANGS   !Solar zenith angle (deg)
    ! -----------------------------------------------------------------------------
    ! Jacobian fields
    REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZTEMPK, ZWVAPK
    REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZTEMPKP, ZTEMPKPP, ZWVAPKP, ZWVAPKPP
    ! -----------------------------------------------------------------------------
    ! INDEXES AND TEMPORAL ARRAYS FOR VECTORIZATION
    INTEGER :: JIS, IBEG, IEND, IDIM, ICPT
    INTEGER, DIMENSION(:),   ALLOCATABLE :: IMSURFP
    REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZAVP, ZCVP
    REAL, DIMENSION(:,:),   ALLOCATABLE :: ZSAVP, ZSSVP, ZAPP, ZAP_HLP
    REAL, DIMENSION(:,:),   ALLOCATABLE :: ZZTMP, ZZTMPP
    REAL, DIMENSION(:),   ALLOCATABLE :: ZANGLP, ZREMISP
    LOGICAL, DIMENSION(:), ALLOCATABLE ::  GANGL
    ! -----------------------------------------------------------------------------
    INTEGER :: INRAD = 2 ! INRAD=1 RADIANCE; INRAD=2 BRIGHTNESS TEMPERATURE
    ! -----------------------------------------------------------------------------
    ! Realistic maximum values for hydrometeor content in kg/kg
    REAL :: ZRCMAX = 5.0E-03, ZRRMAX = 5.0E-03, ZRIMAX = 2.0E-03, ZRSMAX = 5.0E-03
    ! -----------------------------------------------------------------------------
    INTEGER, DIMENSION(:), ALLOCATABLE :: IMSURF   !Surface type index
                                    
    INTEGER :: IKFBOT, IKFTOP, INDEX, ISUM, JLEV, JCH, IWATER, ICAN
    REAL, DIMENSION(:), ALLOCATABLE :: ZTEXTR, ZQVEXTR !Array used in interpolation
    REAL, DIMENSION(:), ALLOCATABLE :: ZQVSAT, ZVINT !Array used in interpolation
    REAL, DIMENSION(:), ALLOCATABLE :: ZPSUM, ZTSUM, ZQVSUM, ZO3SUM !Array used in interpolation
    REAL :: zconst, ZPS, ZTGRAD, ZQGRAD, ZOGRAD !variables used in interpolation
    REAL, DIMENSION(:), ALLOCATABLE :: ZPIN, ZFIN, ZOUT
    ! variables for FMWRIT
    INTEGER           :: IRESP          ! IRESP  : return-code if a problem appears
    !  at the open of the file LFI routines 
    INTEGER           :: IGRID          ! IGRID : grid indicator
    INTEGER           :: ILENCH         ! ILENCH : length of comment string 
    
    CHARACTER(LEN=16) :: YRECFM         ! Name of the article to be written
    CHARACTER(LEN=22) :: YCOMMENT       ! Comment string
    CHARACTER(LEN=8)  :: YINST  
    CHARACTER(LEN=4)  :: YBEG, YEND
    CHARACTER(LEN=2)  :: YCHAN, YTWO   
    CHARACTER(LEN=1)  :: YONE   
                                   
    INTEGER, PARAMETER :: JPPLAT=16
    
    CHARACTER(LEN=3), DIMENSION(JPPLAT) :: YPLAT= (/ &
         'N  ','D  ','MET','GO ','GMS','FY2','TRM','ERS', &
         'EOS','MTP','ENV','MSG','FY1','ADS','MTS','CRL' /)
    CHARACTER(LEN=2), DIMENSION(2) :: YLBL_MVIRI = (/ 'WV', 'IR'/)
    CHARACTER(LEN=3), DIMENSION(7) :: YLBL_SSMI = (/ &
         '19V','19H','22V','37V','37H','85V','85H'/)
    CHARACTER(LEN=3), DIMENSION(9) :: YLBL_TMI = (/ &
         '10V','10H','19V','19H','22V','37V','37H','85V','85H'/)
    CHARACTER(LEN=3), DIMENSION(8) :: YLBL_SEVIRI = (/ &
         '039', '062','073','087','097','108','120','134'/)
    CHARACTER(LEN=3), DIMENSION(4) :: YLBL_GOESI = (/ &
         '039', '067','107','120'/)
    
    ! -----------------------------------------------------------------------------
    !*JPC*VECTORIZATION
    !! One profile per run
    !! INTEGER (Kind=jpim) :: nprofiles = 1
    INTEGER (Kind=jpim) :: nprofiles, ntruepro
    !*JPC*VECTORIZATION
    
    ! RTTOV_readcoeffs interface
    ! ====================
    INTEGER(Kind=jpim) :: errorstatus
    INTEGER(Kind=jpim) :: instrument(3)
    TYPE( rttov_coef ) :: coef         ! coefficients
    TYPE( rttov_scatt_coef ) :: coef_scatt
    
    ! RTTOV interface
    ! ====================
    INTEGER(Kind=jpim), ALLOCATABLE :: rttov_errorstatus(:)  ! rttov error return code
    INTEGER(Kind=jpim) :: nfrequencies
    INTEGER(Kind=jpim) :: nchannels
    INTEGER(Kind=jpim) :: nbtout
    INTEGER(Kind=jpim), ALLOCATABLE :: channels   (:), n_chan(:)
    INTEGER(Kind=jpim), ALLOCATABLE :: polarisations   (:,:)
    INTEGER(Kind=jpim), ALLOCATABLE :: frequencies   (:)
    INTEGER(Kind=jpim), ALLOCATABLE :: lprofiles  (:),lsprofiles(:),lsprofiles2(:)
    TYPE(profile_Type), ALLOCATABLE  :: profiles(:)
    TYPE(profile_cloud_type), ALLOCATABLE :: cld_profiles(:)
    TYPE(transmission_type)               :: transmission
    LOGICAL              :: addcloud = .FALSE.
    LOGICAL, ALLOCATABLE         :: calcemis(:)
    REAL(Kind=jprb), ALLOCATABLE :: emissivity (:)
    TYPE(radiance_cloud_type)             :: radiance
    
    REAL(Kind=jprb),    ALLOCATABLE :: input_emissivity (:)
    CHARACTER (len=6)  :: NameOfRoutine = 'tstrad'
    ! RTTOV K/AD interface
    ! ====================
    LOGICAL    :: switchrad  ! true if input is BT
    TYPE(profile_Type), ALLOCATABLE :: profiles_k(:)
    TYPE(profile_cloud_Type), ALLOCATABLE :: cld_profiles_k(:)
    REAL(Kind=jprb),    ALLOCATABLE       :: emissivity_k (:)
    
    ! variables for input
    ! ====================
    ! Parameter for WV conversion used in all tstrad suite
    REAL(Kind=jprb), PARAMETER :: q_mixratio_to_ppmv  = 1.60771704e+6_JPRB
    REAL(Kind=jprb), PARAMETER :: o3_mixratio_to_ppmv = 6.03504e+5_JPRB
    INTEGER(Kind=jpim) :: alloc_status(40)
    
    ! - End of header --------------------------------------------------------
    !!!----------------------------------------------------------------------------
    !!!
    !!!*       1.    INITIALIZATION OF CONSTANTS FOR TRANSFERT CODE
    !!!              ----------------------------------------------
    !!!
    
    ! JPC from refprof.dat
    ZPRES=(/   0.100,    0.290,    0.690,    1.420,    2.611,    4.407, &
         6.950,   10.370,   14.810,   20.400,   27.260,   35.510, &
         45.290,   56.730,   69.970,   85.180,  102.050,  122.040, &
         143.840,  167.950,  194.360,  222.940,  253.710,  286.600, &
         321.500,  358.280,  396.810,  436.950,  478.540,  521.460, &
         565.540,  610.600,  656.430,  702.730,  749.120,  795.090, &
         839.950,  882.800,  922.460,  957.440,  985.880, 1005.430, &
         1013.250 /)
    
    DO JK=1,JPLEV
      JKRAD=JPLEV-JK+1
      ZPRES_INV(JK)=ZPRES(JKRAD)*100. ! Conversion from hPa to Pa
    END DO
    
    errorstatus     = 0
    alloc_status(:) = 0
    
    PRINT *,'NB OF SAT SIZE(KRTTOVINFO,1)=',SIZE(KRTTOVINFO,1)
    PRINT *,'NB OF SAT SIZE(KRTTOVINFO,2)=',SIZE(KRTTOVINFO,2)
    DO JSAT=1,SIZE(KRTTOVINFO,2)
      IF (KRTTOVINFO(1,JSAT) /= NUNDEF) THEN
        IJSAT = JSAT
      END IF
    END DO
    
    JSAT=1
    instrument(1)=KRTTOVINFO(1,JSAT)
    instrument(2)=KRTTOVINFO(2,JSAT)
    instrument(3)=KRTTOVINFO(3,JSAT)
    PRINT *,'range(KRTTOVINFO(3,JSAT)) ',range(KRTTOVINFO(3,JSAT))
    PRINT *,'range(instrument(3)) ',range(instrument(3))
    CALL rttov_readcoeffs (errorstatus, coef, instrument)
    CALL rttov_initcoeffs (errorstatus, coef)
    
    switchrad = INRAD == 2
    PRINT *,' RADIANCE OR TB CALCULATION: INRAD=',INRAD,' switchrad=',switchrad
    
    !!!----------------------------------------------------------------------------
    !!!
    !!!*       2.    COMPUTE DIMENSIONS OF ARRAYS AND OTHER INDICES
    !!!              ----------------------------------------------
                                   
    IIU = SIZE(PTHT,1)
    IJU = SIZE(PTHT,2)
    IKU = SIZE(PTHT,3)
    
    CALL GET_INDICE_ll (IIB,IJB,IIE,IJE)
    
    IKB = 1 + JPVEXT
    IKE = IKU - JPVEXT
    IKR = IKE - IKB +1
                                  
    IKSTAE = SIZE(PSTATM,1)
    IKUP   = IKE-JPVEXT+1
    
    !*JPC*VECTORIZATION
    ! Determine the number of profiles per RTTOV run
    nprofiles = JPPF
    !*JPC*VECTORIZATION
    
      
    !!!----------------------------------------------------------------------------
    !!!
    !!!*       3.    INITIALIZES THE MEAN-LAYER VARIABLES
    !!!              ------------------------------------
                                   
    ALLOCATE(ZEXNT(SIZE(PTHT,1),SIZE(PTHT,2),SIZE(PTHT,3)))
    ZEXNT(:,:,:)= ( PPABST(:,:,:)/XP00 ) ** (XRD/XCPD)
                                  
    ! Pressure
    ALLOCATE(ZPAVE(KDLON,KFLEV))
    DO JK=IKB,IKE
      JKRAD = JK-JPVEXT
      DO JJ=IJB,IJE
        DO JI=IIB,IIE
          IIJ = (JI-JPHEXT) + (IIE-IIB+1)*(JJ-IJB)
          ZPAVE(IIJ,JKRAD)  = PPABST(JI,JJ,JK)*0.01  !Pressure in hPa
        END DO
      END DO
    END DO
                                   
    ! Temperature
    ALLOCATE(ZTEMP(IIU,IJU,IKU))
    ZTEMP=PTHT*ZEXNT
    ALLOCATE(ZTAVE(KDLON,KFLEV))
    DO JK=IKB,IKE
      JKRAD = JK-JPVEXT
      DO JJ=IJB,IJE
        DO JI=IIB,IIE
          IIJ = (JI-JPHEXT) + (IIE-IIB+1)*(JJ-IJB)
          ZTAVE(IIJ,JKRAD)  = ZTEMP(JI,JJ,JK)
        END DO
      END DO
    END DO
    
    ! Water vapor
    ALLOCATE(ZQVAVE(KDLON,KFLEV))
    ZQVAVE(:,:) = 0.0
    IF( SIZE(PRT(:,:,:,:),4) >= 1 ) THEN
      DO JK=IKB,IKE
        JKRAD = JK-JPVEXT
        DO JJ=IJB,IJE
          DO JI=IIB,IIE
            IIJ = (JI-JPHEXT) + (IIE-IIB+1)*(JJ-IJB)
            ZQVAVE(IIJ,JKRAD) = PRT(JI,JJ,JK,1)
          END DO
        END DO
      END DO
    END IF
    
    ! Ozone
    ALLOCATE(ZO3AVE(KDLON,KFLEV))
        
    ZSTAOZ(:) = PSTATM(:,6)/PSTATM(:,4)
    ZSTAZZ(:) = 1000.0*PSTATM(:,1)
    
    DO JJ = IJB,IJE
      DO JK2 = IKB,IKE
        JKRAD = JK2-JPVEXT
        IKKOZ(:,JK2) = IKB-1
        DO JK1 = 1,IKSTAE
          DO JI = IIB,IIE
            IKKOZ(JI,JK2)=IKKOZ(JI,JK2) + NINT(0.5 + SIGN(0.5,    &
                 -ZSTAZZ(JK1)+0.5*(PZZ(JI,JJ,JK2)+PZZ(JI,JJ,JK2+1)) ))
          END DO
        END DO
        DO JI = IIB,IIE
          ZOZ=(0.5*(PZZ(JI,JJ,JK2)+PZZ(JI,JJ,JK2+1))- ZSTAZZ(IKKOZ(JI,JK2))) &
               /( ZSTAZZ(IKKOZ(JI,JK2)+1)           - ZSTAZZ(IKKOZ(JI,JK2)))
          IIJ = (JI-JPHEXT) + (IIE-IIB+1)*(JJ-IJB)
          ZO3AVE(IIJ,JKRAD) =( (1.- ZOZ) * ZSTAOZ(IKKOZ(JI,JK2))    &
               +  ZOZ     * ZSTAOZ(IKKOZ(JI,JK2)+1))
        END DO
      END DO
    END DO
                                    
    !  Standard atmosphere extension                               
    DO JK=IKUP,KFLEV
      JK1 = (KSTATM-1)+(JK-IKUP)
      JK2 = JK1+1
      ZPAVE(:,JK)  = 0.5*( PSTATM(JK1,2)+PSTATM(JK2,2) )
      ZTAVE(:,JK)  = 0.5*( PSTATM(JK1,3)+PSTATM(JK2,3) )
      ZQVAVE(:,JK) = 0.5*( PSTATM(JK1,5)/PSTATM(JK1,4)+PSTATM(JK2,5)/PSTATM(JK2,4))
      JK1 = (KSTATM)+(JK-IKUP)
      ZO3AVE(:,JK) = ZSTAOZ(JK1)
    END DO
    !!!
    !!!----------------------------------------------------------------------------
    !!!
    !!!*       4.    INTERPOLATES THE ATMOSPHERIC VARIABLES ONTO THE RTTOV GRID
    !              ----------------------------------------------------------
    !!!WITH INVERSION OF VERTICAL LEVELS!
                                   
    ALLOCATE(ZAV(JPLEV,JPNAV,KDLON))
    
    ALLOCATE(ZTEXTR(JPLEV))
    ALLOCATE(ZQVEXTR(JPLEV))
    ALLOCATE(ZVINT(JPLEV))
    ISUM=JPLEV+KFLEV
    ALLOCATE(ZPSUM(ISUM))
    ALLOCATE(ZTSUM(ISUM))
    ALLOCATE(ZQVSUM(ISUM))
    ALLOCATE(ZO3SUM(ISUM))
    ALLOCATE(ZQVSAT(ISUM))
    ZPSUM(:)=0.
    ZTSUM(:)=0.
    ZQVSUM(:)=0.
    ZO3SUM(:)=0.
    ZQVSAT(:)=0.
    zconst= 287./1005.
    IWATER = coef % fmv_gas_pos( gas_id_watervapour )
    DO JI=IIB,IIE
      DO JJ=IJB,IJE
        IIJ = (JI-JPHEXT) + (IIE-IIB+1)*(JJ-IJB)
        ZPS=XP00*0.01 * & !Surface Pressure in hPa
             (0.5*(ZEXNT(JI,JJ,IKB)+ZEXNT(JI,JJ,IKB-1)))**(XCPD/XRD)
        DO JK=1,KFLEV
          JKRAD = KFLEV-JK+1 !INVERSION OF VERTICAL LEVELS!
          ZPSUM(JKRAD)=ZPAVE(IIJ,JK)
          ZTSUM(JKRAD)=ZTAVE(IIJ,JK)
          ZQVSUM(JKRAD)=ZQVAVE(IIJ,JK)
          ZO3SUM(JKRAD)=ZO3AVE(IIJ,JK)
        END DO
        ZTSUM(KFLEV+1)=ZTAVE(IIJ,1)
        ZQVSUM(KFLEV+1)=ZQVAVE(IIJ,1)
        IKFBOT=0
        DO JKF=1,JPLEV
          IF (ZPRES(JKF) > ZPS) THEN
            IKFBOT=JKF
            EXIT
          END IF
        END DO
        INDEX = KFLEV
        IF (IKFBOT /= 0) THEN
    !!!-----Extrapolates temperature below surface pressure-------------------
          INDEX=JPLEV-IKFBOT+1
          INDEX=INDEX+KFLEV+1
          ZTSUM((KFLEV+2):INDEX) = PTSRAD(JI,JJ)
          ZPSUM((KFLEV+1))=ZPS
          ZPSUM((KFLEV+2):INDEX)=ZPRES(IKFBOT:JPLEV)
          ZO3SUM((KFLEV+2):INDEX)=ZO3AVE(IIJ,1)
        END IF
    !!!-----Extrapolates profile above highest declared level-----------------
    !!!----- => linear extrapolation -----------------------------------------
        IKFTOP = 1
        DO JLEV=1,INDEX
          IF(ZPRES(JLEV) >= ZPAVE(IIJ,KFLEV) ) EXIT
          IKFTOP = IKFTOP + 1
        END DO
        IF (IKFTOP /= 1) THEN
          ZTGRAD = (ZTSUM(1) - ZTSUM(2)) / (ZPSUM(1)-ZPSUM(2))
          ZQGRAD = (ZQVSUM(1) - ZQVSUM(2)) / (ZPSUM(1)-ZPSUM(2))
          ZOGRAD = (ZO3SUM(1) - ZO3SUM(2)) / (ZPSUM(1)-ZPSUM(2))
          DO JLEV=INDEX, 1, -1
            ZTSUM(JLEV+IKFTOP-1) = ZTSUM(JLEV)
            ZQVSUM(JLEV+IKFTOP-1) = ZQVSUM(JLEV)
            ZO3SUM(JLEV+IKFTOP-1) = ZO3SUM(JLEV)
            ZPSUM(JLEV+IKFTOP-1) = ZPSUM(JLEV)
          END DO
          INDEX = INDEX + IKFTOP-1
          DO JLEV=1,IKFTOP-1
            ZPSUM(JLEV) = ZPRES(JLEV)
            ZTSUM(JLEV) = ZTSUM(IKFTOP)  &
                 + ZTGRAD * (ZPSUM(JLEV) - ZPSUM(IKFTOP))
            ZQVSUM(JLEV) = ZQVSUM(IKFTOP)  &
                 + ZQGRAD * (ZPSUM(JLEV) - ZPSUM(IKFTOP))
            ZO3SUM(JLEV) = ZO3SUM(IKFTOP)  &
                 + ZOGRAD * (ZPSUM(JLEV) - ZPSUM(IKFTOP))
          END DO
        ENDIF
    !!!-----Interpolates to given pressure grid-------------------------------
        ALLOCATE(ZPIN(INDEX))
        ALLOCATE(ZFIN(INDEX))
        ALLOCATE(ZOUT(JPLEV))
        DO JLEV=1,INDEX
          JKRAD=INDEX-JLEV+1
          ZPIN(JKRAD) = ZPSUM(JLEV)*100.
          ZFIN(JKRAD) = ZTSUM(JLEV)
        END DO
        CALL PINTER(ZFIN, ZPIN, ZFIN, ZFIN, ZOUT, ZPRES_INV, &
             1, 1, INDEX, 1, JPLEV, 'LOG', 'RHU.')
        DO JLEV=1,JPLEV
          JKRAD=JPLEV-JLEV+1
          ZVINT(JKRAD) = ZOUT(JLEV)
        END DO
        ZAV(:,1,IIJ)= ZVINT(:) ! temperature K
        DO JLEV=1,INDEX
          JKRAD=INDEX-JLEV+1
          ZFIN(JKRAD) = ZQVSUM(JLEV)
        END DO
        CALL PINTER(ZFIN, ZPIN, ZFIN, ZFIN, ZOUT, ZPRES_INV, &
             1, 1, INDEX, 1, JPLEV, 'LOG', 'RHU.')
        DO JLEV=1,JPLEV
          JKRAD=JPLEV-JLEV+1
          ZVINT(JKRAD) = ZOUT(JLEV)
        END DO
        ZAV(:,2,IIJ)= ZVINT(:)*q_mixratio_to_ppmv ! water vapor mr ppmv
        DO JLEV=1,INDEX
          JKRAD=INDEX-JLEV+1
          ZFIN(JKRAD) = ZO3SUM(JLEV)
        END DO
        CALL PINTER(ZFIN, ZPIN, ZFIN, ZFIN, ZOUT, ZPRES_INV, &
             1, 1, INDEX, 1, JPLEV, 'LOG', 'RHU.')
        DO JLEV=1,JPLEV
          JKRAD=JPLEV-JLEV+1
          ZVINT(JKRAD) = ZOUT(JLEV)
        END DO
        ZAV(:,3,IIJ)= ZVINT(:)*o3_mixratio_to_ppmv ! ozone mixing ratio  ppmv
         DO JLEV=1,JPLEV
           ZAV(JLEV,1,IIJ)= MAX(coef%lim_prfl_tmin(JLEV), &
                MIN(coef%lim_prfl_tmax(JLEV),ZAV(JLEV,1,IIJ)))
           ZAV(JLEV,2,IIJ)= MAX(coef%lim_prfl_gmin(JLEV,IWATER), &
                MIN(coef%lim_prfl_gmax(JLEV,IWATER),ZAV(JLEV,2,IIJ)))
         END DO
        DEALLOCATE(ZPIN,ZFIN,ZOUT)
      END DO
    END DO
    DEALLOCATE(ZVINT)
    DEALLOCATE(ZPAVE,ZTAVE,ZQVAVE,ZO3AVE)
    !
    !--------------------------------------------------------------------------
    !
    !*       6.    CALLS THE RTTOV RADIATION CODE
    !	       ------------------------------
    !
    !*       6.1   INITIALIZES 2D AND SURFACE FIELDS
    !
    !
    ALLOCATE(ZANGS(KDLON))
    ZANGS(:)=0. ! zenithal solar angle not used
    !
    ALLOCATE(IMSURF(KDLON))
    DO JJ=IJB,IJE
      DO JI=IIB,IIE
        IIJ = (JI-JPHEXT) + (IIE-IIB+1)*(JJ-IJB)
        IMSURF(IIJ) = NINT(XSEA(JI,JJ)) ! Surface Mask 0=land, 1=sea, 2=sea-ice