MODULE YOMCOSP_UTIL
  ! IMPORT IFS SPECIFIC MODULES
  USE PARKIND1, ONLY : JPIM, JPRB, JPIB
  USE YOMMP,    ONLY : MYPROC
  USE YOMCT0,   ONLY : NSTART, NSTOP, NPROC
  USE YOMCT3,   ONLY : NSTEP
  USE YOMGEM,   ONLY : NGPTOT, NGPTOTG
  USE YOMDIM,   ONLY : NFLEVG, NPROMA, NGPBLKS
  USE YOMLUN,   ONLY : NULOUT, NULNAM, NULERR
  USE YOMCST,   ONLY : RG, RLVTT, RLSTT, RTT, RV, RCPD, &
       RDAY, RD, RETV, RA, REA, RSIGMA, RCPV
  USE YOETHF,   ONLY : R2ES, R3LES, R3IES, R4LES, R4IES, &
       R5LES, R5IES, R5ALVCP, R5ALSCP, &
       RALVDCP, RALSDCP, RTWAT, RTICE, RTICECU, &
       RTWAT_RTICE_R, RTWAT_RTICECU_R, RVTMP2
  USE YOECLDP,  ONLY : NCLV, NCLDQL, NCLDQR, NCLDQI, NCLDQS
  USE YOMHOOK, ONLY : LHOOK, DR_HOOK
  USE YOMDYN, ONLY : TSTEP
  USE PHY_DIAG_MOD, ONLY: NEW_DIAG_SET, NEW_DIAGNOSTIC
  USE YOM_GRIB_CODES, ONLY: NGRB_CALIPSO_LOW_CLD_FRAC, NGRB_CALIPSO_MID_CLD_FRAC, & 
       NGRB_CALIPSO_HIGH_CLD_FRAC,  NGRB_CALIPSO_TOTAL_CLD_FRAC, & 
       NGRB_ISCCP_TOTAL_CLD_FRAC,   NGRB_ISCCP_CLD_ALBEDO, &
       NGRB_ISCCP_CLD_TOP_PRESSURE, NGRB_MODIS_TOTAL_CLD_FRAC, &
       NGRB_MODIS_WATER_CLD_FRAC,   NGRB_MODIS_ICE_CLD_FRAC,  NGRB_COSP_3D
  ! COSP-SPECIFIC MODULES
  USE MOD_COSP_CONSTANTS
  USE MOD_COSP_TYPES
  USE MOD_COSP_MODIS_SIMULATOR
  USE MOD_COSP
  USE YOMCOSP_VARIABLES
  
#include "abor1.intfb.h"
#include "posnam.intfb.h"
#include "namcospinput.h"
#include "namcospoutput.h"
  
CONTAINS

  !------------------------------------
  ! INITIAL SETUP FOR COSP SIMULATORS -
  !------------------------------------
  SUBROUTINE SUCOSP

    IMPLICIT NONE
    INTEGER(KIND=JPIM) :: I, ISET
    
    !*       1.1 SET DEFAULT AND INITIAL VALUES.
    WRITE(NULOUT,*) '************** COSP SETUP INFORMATION ***************'
    WRITE(NULOUT,*) 'SETTING UP DEFAULTS FOR COSP CONFIG INFO...'
    COSP_DEBUG=.TRUE.
    COSP_RUN=.FALSE.
    COSP_TSTEP=1_JPIM
    COSP_IBL=1_JPIM
    COSP_NPOINTS=-1_JPIM
    COSP_NCOLUMNS=10_JPIM
    COSP_NPOINTS_IT=100_JPIM
    COSP_USE_VGRID=.TRUE.
    COSP_NLVGRID=91_JPIM
    COSP_CSAT_VGRID=.TRUE.
    COSP_RADAR_FREQ=94.0_JPRB
    COSP_SURFACE_RADAR=0_JPIM
    COSP_USE_GAS_ABS=1_JPIM
    COSP_DO_RAY=0_JPIM
    COSP_K2=-1_JPIM
    COSP_USE_MIE_TABLES=0
    COSP_USE_REFF=.TRUE.
    COSP_MELT_LAY=0
    COSP_USE_PRECIPITATION_FLUXES=.TRUE.
    COSP_NPRMTS_MAX_HYDRO=12_JPIM
    COSP_NUMAERO=1_JPIM
    COSP_NPRMTS_MAX_AERO=1
    COSP_LIDAR_ICE_TYPE=0
    COSP_OVERLAP=3_JPIM
    COSP_ISCCP_TOPHEIGHT=1_JPIM
    COSP_ISCCP_TOPHEIGHT_DIRECTION=2
    COSP_OUTPUT_FREQUENCY=-1_JPIM
    COSP_EMSFC_LW=0.98_JPRB
    ! COSP OUTPUT LOGICALS
    ! ISCCP
    COSP_LPCTISCCP=.FALSE.         ! ISCCP MEAN CLOUD TOP PRESSURE
    COSP_LCLISCCP=.FALSE.          ! ISCCP CLOUD AREA FRACTION
    COSP_LBOXPTOPISCCP=.FALSE.     ! ISCCP CTP IN EACH COLUMN
    COSP_LBOXTAUISCCP=.FALSE.      ! ISCCP OPTICAL EPTH IN EACH COLUMN
    COSP_LTAUISCCP=.FALSE.         ! ISCCP MEAN OPTICAL DEPTH
    COSP_LCLTISCCP=.FALSE.         ! ISCCP TOTAL CLOUD FRACTION
    COSP_LMEANTBISCCP=.FALSE.      ! ISCCP MEAN ALL-SKY 10.5MICRON BRIGHTNESS TEMP
    COSP_LMEANTBCLRISCCP=.FALSE.   ! ISCCP MEAN CLEAR-SKY 10.5MICRON BRIGHTNESS TEMP
    COSP_LALBISCCP=.FALSE.         ! ISCCP MEAN CLOUD ALBEDO
    ! MISR
    COSP_LCLMISR=.FALSE.           ! MISR CLOUD FRACTION
    ! MODIS
    COSP_LCLTMODIS=.FALSE.         ! MODIS TOTAL CLOUD FRACTION
    COSP_LCLWMODIS=.FALSE.         ! MODIS LIQUID CLOUD FRACTION
    COSP_LCLIMODIS=.FALSE.         ! MODIS ICE CLOUD FRACTION
    COSP_LCLHMODIS=.FALSE.         ! MODIS HIGH-LEVEL CLOUD FRACTION
    COSP_LCLMMODIS=.FALSE.         ! MODIS MID-LEVEL CLOUD FRACTION
    COSP_LCLLMODIS=.FALSE.         ! MODIS LOW-LEVEL CLOUD FRACTION
    COSP_LTAUTMODIS=.FALSE.        ! MODIS TOTAL CLOUD OPTICAL THICKNES
    COSP_LTAUWMODIS=.FALSE.        ! MODIS LIQUID OPTICAL THICKNESS
    COSP_LTAUIMODIS=.FALSE.        ! MODIS ICE OPTICAL THICKNESS
    COSP_LTAUTLOGMODIS=.FALSE.     ! MODIS TOTAL CLOUD OPTICAL THICKNESS (LOG10 MEAN)
    COSP_LTAUWLOGMODIS=.FALSE.     ! MODIS LIQUID OPTICAL THICKNESS (LOG10 MEAN)
    COSP_LTAUILOGMODIS=.FALSE.     ! MODIS ICE OPTICAL THICKNESS (LOG10 MEAN)
    COSP_LREFFCLWMODIS=.FALSE.     ! MODIS LIQUID CLOUD PARTICLE SIZE
    COSP_LREFFCLIMODIS=.FALSE.     ! MODIS ICE PARTICLE SIZE
    COSP_LPCTMODIS=.FALSE.         ! MODIS CLOUD TOP PRESSURE
    COSP_LLWPMODIS=.FALSE.         ! MODIS CLOUD LIQUID WATER PATH
    COSP_LIWPMODIS=.FALSE.         ! MODIS CLOUD ICE WATER PATH
    COSP_LCLMODIS=.FALSE.          ! MODIS CLOUD AREA FRACTION
    ! CALIPSO
    COSP_LATB532=.FALSE.           ! CALIPSO ATTENUATED TOTAL BACKSCATTER (532NM)
    COSP_LLIDARBETAMOL532=.FALSE.  ! CALIPSO MOLECULAR BACKSCATTER (532NM)
    COSP_LCFADLIDARSR532=.FALSE.   ! CALIPSO SCATTERING RATIO CFAD
    COSP_LCLCALIPSO2=.FALSE.       ! CALIPSO CLOUD FRACTION UNDETECTED BY CLOUDSAT
    COSP_LCLCALIPSO=.FALSE.        ! CALIPSO CLOUD AREA FRACTION
    COSP_LCLHCALIPSO=.FALSE.       ! CALIPSO HIGH-LEVEL CLOUD FRACTION
    COSP_LCLLCALIPSO=.FALSE.       ! CALIPSO LOW-LEVEL CLOUD FRACTION
    COSP_LCLMCALIPSO=.FALSE.       ! CALIPSO MID-LEVEL CLOUD FRACTION
    COSP_LCLTCALIPSO=.FALSE.       ! CALIPSO TOTAL CLOUD FRACTION
    COSP_LCLTLIDARRADAR=.FALSE.    ! CALIPSO-CLOUDSAT TOTAL CLOUD FRACTION
    COSP_LCLCALIPSOLIQ=.FALSE.     ! CALIPSO LIQUID CLOUD AREA FRACTION
    COSP_LCLCALIPSOICE=.FALSE.     ! CALIPSO ICE CLOUD AREA FRACTION
    COSP_LCLCALIPSOUN=.FALSE.      ! CALIPSO UNDETECTED CLOUD AREA FRACTION
    COSP_LCLCALIPSOTMP=.FALSE.     ! CALIPSO UNDETECTED CLOUD AREA FRACTION
    COSP_LCLCALIPSOTMPLIQ=.FALSE.  ! CALIPSO LIQUID CLOUD AREA FRACTION
    COSP_LCLCALIPSOTMPICE=.FALSE.  ! CALIPSO ICE CLOUD AREA FRACTION
    COSP_LCLCALIPSOTMPUN=.FALSE.   ! CALIPSO UNDETECTED CLOUD AREA FRACTION
    COSP_LCLTCALIPSOLIQ=.FALSE.    ! CALIPSO LIQUID TOTAL CLOUD FRACTION
    COSP_LCLTCALIPSOICE=.FALSE.    ! CALIPSO ICE TOTAL CLOUD FRACTION
    COSP_LCLTCALIPSOUN=.FALSE.     ! CALIPSO UNDETECTED TOTAL CLOUD FRACTION
    COSP_LCLHCALIPSOLIQ=.FALSE.    ! CALIPSO HIGH-LEVEL LIQUID CLOUD FRACTION
    COSP_LCLHCALIPSOICE=.FALSE.    ! CALIPSO HIGH-LEVEL ICE CLOUD FRACTION
    COSP_LCLHCALIPSOUN=.FALSE.     ! CALIPSO HIGH-LEVEL UNDETECTED CLOUD FRACTION
    COSP_LCLMCALIPSOLIQ=.FALSE.    ! CALIPSO MID-LEVEL LIQUID CLOUD FRACTION
    COSP_LCLMCALIPSOICE=.FALSE.    ! CALIPSO MID-LEVEL ICE CLOUD FRACTION
    COSP_LCLMCALIPSOUN=.FALSE.     ! CALIPSO MID-LEVEL UNDETECTED CLOUD FRACTION
    COSP_LCLLCALIPSOLIQ=.FALSE.    ! CALIPSO LOW-LEVEL LIQUID CLOUD FRACTION
    COSP_LCLLCALIPSOICE=.FALSE.    ! CALIPSO LOW-LEVEL ICE CLOUD FRACTION
    COSP_LCLLCALIPSOUN=.FALSE.     ! CALIPSO LOW-LEVEL UNDETECTED CLOUD FRACTION
    ! CLOUDSAT
    COSP_LCFADDBZE94=.FALSE.       ! CLOUDSAT RADAR REFLECTIVITY CFAD
    COSP_LDBZE94=.FALSE.           ! CLOUDSAT RADAR REFLECTIVITY
    ! PARASOL
    COSP_LPARASOLREFL=.FALSE.      ! PARASOL REFLECTANCE
    ! RTTOV
    COSP_LTBRTTOV=.FALSE.          ! RTTOV MEAN BRIGHTNESS TEMPERATURE
    ! SIMULATORS
    COSP_LISCCP_SIM =.FALSE.
    COSP_LMODIS_SIM =.FALSE.
    COSP_LMISR_SIM  =.FALSE.
    COSP_LRADAR_SIM =.FALSE.
    COSP_LLIDAR_SIM =.FALSE.
    COSP_LRTTOV_SIM =.FALSE.

    CALL POSNAM(NULNAM,'NAMCOSPINPUT')
    READ(NULNAM,NAMCOSPINPUT)
    WRITE(NULOUT,*) 'COSP INPUT DATA READ!'
    
    CALL POSNAM(NULNAM,'NAMCOSPOUTPUT')
    READ(NULNAM,NAMCOSPOUTPUT)
    WRITE(NULOUT,*) 'COSP OUTPUT DATA READ!'
    
    IF(MYPROC == 1 .AND. COSP_IBL == 1) THEN 
       WRITE(NULOUT,*) 'COSP_NCOLUMNS = ',COSP_NCOLUMNS
       WRITE(NULOUT,*) 'COSP_USE_VGRID =',COSP_USE_VGRID
       WRITE(NULOUT,*) 'COSP_NLVGRID =',COSP_NLVGRID
       WRITE(NULOUT,*) 'COSP_CSAT_VGRID =',COSP_CSAT_VGRID
       WRITE(NULOUT,*) 'COSP_RADAR_FREQ =',COSP_RADAR_FREQ
       WRITE(NULOUT,*) 'COSP_SURFACE_RADAR =',COSP_SURFACE_RADAR
       WRITE(NULOUT,*) 'COSP_USE_GAS_ABS =',COSP_USE_GAS_ABS
       WRITE(NULOUT,*) 'COSP_DO_RAY = ',COSP_DO_RAY
       WRITE(NULOUT,*) 'COSP_K2 =',COSP_K2
       WRITE(NULOUT,*) 'COSP_MELT_LAY =',COSP_MELT_LAY
       WRITE(NULOUT,*) 'COSP_USE_REFF=.TRUE. =',COSP_USE_REFF
       WRITE(NULOUT,*) 'COSP_USE_PRECIPITATION_FLUXES =',COSP_USE_PRECIPITATION_FLUXES
       WRITE(NULOUT,*) 'COSP_NPRMTS_MAX_HYDRO =',COSP_NPRMTS_MAX_HYDRO
       WRITE(NULOUT,*) 'COSP_NUMAERO =',COSP_NUMAERO
       WRITE(NULOUT,*) 'COSP_NPRMTS_MAX_AERO =',COSP_NPRMTS_MAX_AERO
       WRITE(NULOUT,*) 'COSP_LIDAR_ICE_TYPE =',COSP_LIDAR_ICE_TYPE
       WRITE(NULOUT,*) 'COSP_OVERLAP =',COSP_LIDAR_ICE_TYPE
       WRITE(NULOUT,*) 'COSP_ISCCP_TOPHEIGHT =',COSP_ISCCP_TOPHEIGHT
       WRITE(NULOUT,*) 'COSP_ISCCP_TOPHEIGHT_DIRECTION =',COSP_ISCCP_TOPHEIGHT_DIRECTION
       WRITE(NULOUT,*) 'COSP_OUTPUT_FREQUENCY =',COSP_OUTPUT_FREQUENCY
       WRITE(NULOUT,*) 'COSP_EMSFC_LW =',COSP_EMSFC_LW
       WRITE(NULOUT,FMT='(A)') TRIM(COSP_CLOUDSAT_MICRO_SCHEME)
    ENDIF

    ! DEAL WITH DEPENDENCIES
    IF (.NOT.COSP_LRADAR_SIM) THEN
       COSP_LCFADDBZE94   = .FALSE.
       COSP_LCLCALIPSO2    = .FALSE.
       COSP_LCLTLIDARRADAR = .FALSE. ! NEEDS RADAR & LIDAR
       COSP_LDBZE94        = .FALSE.
       COSP_LCLCALIPSO2    = .FALSE. ! NEEDS RADAR & LIDAR
    ENDIF
    IF (.NOT.COSP_LLIDAR_SIM) THEN
       COSP_LATB532 = .FALSE.
       COSP_LCFADLIDARSR532 = .FALSE.
       COSP_LCLCALIPSO2      = .FALSE. ! NEEDS RADAR & LIDAR
       COSP_LCLCALIPSO       = .FALSE.
       COSP_LCLHCALIPSO      = .FALSE.
       COSP_LCLLCALIPSO      = .FALSE.
       COSP_LCLMCALIPSO      = .FALSE.
       COSP_LCLTCALIPSO      = .FALSE.
       COSP_LCLTLIDARRADAR   = .FALSE.
       COSP_LPARASOLREFL    = .FALSE.
       COSP_LLIDARBETAMOL532 = .FALSE.
       COSP_LCLTLIDARRADAR = .FALSE. ! NEEDS RADAR & LIDAR
       COSP_LCLCALIPSOLIQ    = .FALSE.
       COSP_LCLCALIPSOICE    = .FALSE.
       COSP_LCLCALIPSOUN    = .FALSE.
       COSP_LCLCALIPSOTMP    = .FALSE.
       COSP_LCLCALIPSOTMPUN    = .FALSE.
       COSP_LCLCALIPSOTMPLIQ    = .FALSE.
       COSP_LCLCALIPSOTMPICE    = .FALSE.
       COSP_LCLHCALIPSOLIQ      = .FALSE.
       COSP_LCLLCALIPSOLIQ      = .FALSE.
       COSP_LCLMCALIPSOLIQ     = .FALSE.
       COSP_LCLTCALIPSOLIQ      = .FALSE.
       COSP_LCLHCALIPSOICE      = .FALSE.
       COSP_LCLLCALIPSOICE      = .FALSE.
       COSP_LCLMCALIPSOICE      = .FALSE.
       COSP_LCLTCALIPSOICE      = .FALSE.
       COSP_LCLHCALIPSOUN      = .FALSE.
       COSP_LCLLCALIPSOUN      = .FALSE.
       COSP_LCLMCALIPSOUN      = .FALSE.
       COSP_LCLTCALIPSOUN      = .FALSE.
    ENDIF
    IF (.NOT.COSP_LISCCP_SIM) THEN
       COSP_LALBISCCP       = .FALSE.
       COSP_LBOXPTOPISCCP   = .FALSE.
       COSP_LBOXTAUISCCP    = .FALSE.
       COSP_LCLISCCP        = .FALSE.
       COSP_LPCTISCCP       = .FALSE.
       COSP_LTAUISCCP       = .FALSE.
       COSP_LCLTISCCP       = .FALSE.
       COSP_LMEANTBISCCP    = .FALSE.
       COSP_LMEANTBCLRISCCP = .FALSE.
    ENDIF
    IF (.NOT.COSP_LMISR_SIM) THEN
       COSP_LCLMISR = .FALSE.
    ENDIF
    IF (.NOT.COSP_LRTTOV_SIM) THEN
       COSP_LTBRTTOV = .FALSE.
    ENDIF
    IF ((.NOT.COSP_LRADAR_SIM).AND.(.NOT.COSP_LLIDAR_SIM).AND. &
         (.NOT.COSP_LISCCP_SIM).AND.(.NOT.COSP_LMISR_SIM)) THEN
       COSP_LFRACOUT = .FALSE.
    ENDIF
    IF (.NOT.COSP_LMODIS_SIM) THEN
       COSP_LCLTMODIS=.FALSE.
       COSP_LCLWMODIS=.FALSE.
       COSP_LCLIMODIS=.FALSE.
       COSP_LCLHMODIS=.FALSE.
       COSP_LCLMMODIS=.FALSE.
       COSP_LCLLMODIS=.FALSE.
       COSP_LTAUTMODIS=.FALSE.
       COSP_LTAUWMODIS=.FALSE.
       COSP_LTAUIMODIS=.FALSE.
       COSP_LTAUTLOGMODIS=.FALSE.
       COSP_LTAUWLOGMODIS=.FALSE.
       COSP_LTAUILOGMODIS=.FALSE.
       COSP_LREFFCLWMODIS=.FALSE.
       COSP_LREFFCLIMODIS=.FALSE.
       COSP_LPCTMODIS=.FALSE.
       COSP_LLWPMODIS=.FALSE.
       COSP_LIWPMODIS=.FALSE.
       COSP_LCLMODIS=.FALSE.
       COSP_LCRIMODIS=.FALSE.
       COSP_LCRLMODIS=.FALSE.
    ENDIF
    IF (COSP_LMODIS_SIM) COSP_LISCCP_SIM = .TRUE.

    CFG%LSTATS = .FALSE.
    IF ((COSP_LRADAR_SIM).OR.(COSP_LLIDAR_SIM).OR.(COSP_LISCCP_SIM)) CFG%LSTATS = .TRUE.

    ! COPY INSTRUMENT FLAGS TO CFG STRUCTURE
    CFG%LRADAR_SIM = COSP_LRADAR_SIM
    CFG%LLIDAR_SIM = COSP_LLIDAR_SIM
    CFG%LISCCP_SIM = COSP_LISCCP_SIM
    CFG%LMODIS_SIM = COSP_LMODIS_SIM
    CFG%LMISR_SIM  = COSP_LMISR_SIM
    CFG%LRTTOV_SIM = COSP_LRTTOV_SIM

    ! OUTPUT DIAGNOSTICS
    I = 1
    IF (COSP_LALBISCCP)        CFG%OUT_LIST(I) = 'ALBISCCP'
    I = I+1
    IF (COSP_LATB532)          CFG%OUT_LIST(I) = 'ATB532'
    I = I+1
    IF (COSP_LBOXPTOPISCCP)    CFG%OUT_LIST(I) = 'BOXPTOPISCCP'
    I = I+1
    IF (COSP_LBOXTAUISCCP)     CFG%OUT_LIST(I) = 'BOXTAUISCCP'
    I = I+1
    IF (COSP_LCFADDBZE94)      CFG%OUT_LIST(I) = 'CFADDBZE94'
    I = I+1
    IF (COSP_LCFADLIDARSR532)  CFG%OUT_LIST(I) = 'CFADLIDARSR532'
    I = I+1
    IF (COSP_LCLCALIPSO2)      CFG%OUT_LIST(I) = 'CLCALIPSO2'
    I = I+1
    IF (COSP_LCLCALIPSO)       CFG%OUT_LIST(I) = 'CLCALIPSO'
    I = I+1
    IF (COSP_LCLHCALIPSO)      CFG%OUT_LIST(I) = 'CLHCALIPSO'
    I = I+1
    IF (COSP_LCLISCCP)         CFG%OUT_LIST(I) = 'CLISCCP'
    I = I+1
    IF (COSP_LCLLCALIPSO)      CFG%OUT_LIST(I) = 'CLLCALIPSO'
    I = I+1
    IF (COSP_LCLMCALIPSO)      CFG%OUT_LIST(I) = 'CLMCALIPSO'
    I = I+1
    IF (COSP_LCLTCALIPSO)      CFG%OUT_LIST(I) = 'CLTCALIPSO'
    I = I+1

    IF (COSP_LCLLCALIPSOICE)      CFG%OUT_LIST(I) = 'CLLCALIPSOICE'
    I = I+1
    IF (COSP_LCLMCALIPSOICE)      CFG%OUT_LIST(I) = 'CLMCALIPSOICE'
    I = I+1
    IF (COSP_LCLHCALIPSOICE)      CFG%OUT_LIST(I) = 'CLHCALIPSOICE'
    I = I+1
    IF (COSP_LCLTCALIPSOICE)      CFG%OUT_LIST(I) = 'CLTCALIPSOICE'
    I = I+1
    IF (COSP_LCLLCALIPSOLIQ)      CFG%OUT_LIST(I) = 'CLLCALIPSOLIQ'
    I = I+1
    IF (COSP_LCLMCALIPSOLIQ)      CFG%OUT_LIST(I) = 'CLMCALIPSOLIQ'
    I = I+1
    IF (COSP_LCLHCALIPSOLIQ)      CFG%OUT_LIST(I) = 'CLHCALIPSOLIQ'
    I = I+1
    IF (COSP_LCLTCALIPSOLIQ)      CFG%OUT_LIST(I) = 'CLTCALIPSOLIQ'
    I = I+1
    IF (COSP_LCLLCALIPSOUN)      CFG%OUT_LIST(I) = 'CLLCALIPSOUN'
    I = I+1
    IF (COSP_LCLMCALIPSOUN)      CFG%OUT_LIST(I) = 'CLMCALIPSOUN'
    I = I+1
    IF (COSP_LCLHCALIPSOUN)      CFG%OUT_LIST(I) = 'CLHCALIPSOUN'
    I = I+1
    IF (COSP_LCLTCALIPSOUN)      CFG%OUT_LIST(I) = 'CLTCALIPSOUN'
    I = I+1

    IF (COSP_LCLCALIPSOICE)       CFG%OUT_LIST(I) = 'CLCALIPSOICE'
    I = I+1
    IF (COSP_LCLCALIPSOLIQ)       CFG%OUT_LIST(I) = 'CLCALIPSOLIQ'
    I = I+1
    IF (COSP_LCLCALIPSOUN)       CFG%OUT_LIST(I) = 'CLCALIPSOUN'
    I = I+1

    IF (COSP_LCLCALIPSOTMP)       CFG%OUT_LIST(I) = 'CLCALIPSOTMP'
    I = I+1
    IF (COSP_LCLCALIPSOTMPICE)       CFG%OUT_LIST(I) = 'CLCALIPSOTMPICE'
    I = I+1
    IF (COSP_LCLCALIPSOTMPLIQ)       CFG%OUT_LIST(I) = 'CLCALIPSOTMPLIQ'
    I = I+1
    IF (COSP_LCLCALIPSOTMPUN)       CFG%OUT_LIST(I) = 'CLCALIPSOTMPUN'
    I = I+1
    IF (COSP_LCLTLIDARRADAR)   CFG%OUT_LIST(I) = 'CLTLIDARRADAR'
    I = I+1
    IF (COSP_LPCTISCCP)        CFG%OUT_LIST(I) = 'PCTISCCP'
    I = I+1
    IF (COSP_LDBZE94)          CFG%OUT_LIST(I) = 'DBZE94'
    I = I+1
    IF (COSP_LTAUISCCP)        CFG%OUT_LIST(I) = 'TAUISCCP'
    I = I+1
    IF (COSP_LCLTISCCP)        CFG%OUT_LIST(I) = 'CLTISCCP'
    I = I+1
    IF (COSP_LTOFFSET)         CFG%OUT_LIST(I) = 'TOFFSET'
    I = I+1
    IF (COSP_LPARASOLREFL)     CFG%OUT_LIST(I) = 'PARASOLREFL'
    I = I+1
    IF (COSP_LCLMISR)          CFG%OUT_LIST(I) = 'CLMISR'
    I = I+1
    IF (COSP_LMEANTBISCCP)     CFG%OUT_LIST(I) = 'MEANTBISCCP'
    I = I+1
    IF (COSP_LMEANTBCLRISCCP)  CFG%OUT_LIST(I) = 'MEANTBCLRISCCP'
    I = I+1
    IF (COSP_LFRACOUT)         CFG%OUT_LIST(I) = 'FRACOUT'
    I = I+1
    IF (COSP_LLIDARBETAMOL532) CFG%OUT_LIST(I) = 'LIDARBETAMOL532'
    I = I+1
    IF (COSP_LTBRTTOV)         CFG%OUT_LIST(I) = 'TBRTTOV'
    I = I+1
    IF (COSP_LCLTMODIS)        CFG%OUT_LIST(I) = 'CLTMODIS'
    I = I+1
    IF (COSP_LCLWMODIS)        CFG%OUT_LIST(I) = 'CLWMODIS'
    I = I+1
    IF (COSP_LCLIMODIS)        CFG%OUT_LIST(I) = 'CLIMODIS'
    I = I+1
    IF (COSP_LCLHMODIS)        CFG%OUT_LIST(I) = 'CLHMODIS'
    I = I+1
    IF (COSP_LCLMMODIS)        CFG%OUT_LIST(I) = 'CLMMODIS'
    I = I+1
    IF (COSP_LCLLMODIS)        CFG%OUT_LIST(I) = 'CLLMODIS'
    I = I+1
    IF (COSP_LTAUTMODIS)       CFG%OUT_LIST(I) = 'TAUTMODIS'
    I = I+1
    IF (COSP_LTAUWMODIS)       CFG%OUT_LIST(I) = 'TAUWMODIS'
    I = I+1
    IF (COSP_LTAUIMODIS)       CFG%OUT_LIST(I) = 'TAUIMODIS'
    I = I+1
    IF (COSP_LTAUTLOGMODIS)    CFG%OUT_LIST(I) = 'TAUTLOGMODIS'
    I = I+1
    IF (COSP_LTAUWLOGMODIS)    CFG%OUT_LIST(I) = 'TAUWLOGMODIS'
    I = I+1
    IF (COSP_LTAUILOGMODIS)    CFG%OUT_LIST(I) = 'TAUILOGMODIS'
    I = I+1
    IF (COSP_LREFFCLWMODIS)    CFG%OUT_LIST(I) = 'REFFCLWMODIS'
    I = I+1
    IF (COSP_LREFFCLIMODIS)    CFG%OUT_LIST(I) = 'REFFCLIMODIS'
    I = I+1
    IF (COSP_LPCTMODIS)        CFG%OUT_LIST(I) = 'PCTMODIS'
    I = I+1
    IF (COSP_LLWPMODIS)        CFG%OUT_LIST(I) = 'LWPMODIS'
    I = I+1
    IF (COSP_LIWPMODIS)        CFG%OUT_LIST(I) = 'IWPMODIS'
    I = I+1
    IF (COSP_LCLMODIS)         CFG%OUT_LIST(I) = 'CLMODIS'
    I = I+1
    IF (COSP_LCRIMODIS)        CFG%OUT_LIST(I) = 'CRIMODIS'
    I = I+1
    IF (COSP_LCRLMODIS)        CFG%OUT_LIST(I) = 'CRLMODIS'

    IF (I /= N_OUT_LIST) THEN
       WRITE(*,*) 'COSP_UTIL: WRONG NUMBER OF OUTPUT DIAGNOSTICS'
       WRITE(*,*) I,N_OUT_LIST
       CALL ABOR1('COSP_UTIL: WRONG NUMBER OF OUTPUT DIAGNOSTICS')
    ENDIF

    ! COPY DIAGNOSTIC FLAGS TO CFG STRUCTURE
    ! ISCCP SIMULATOR
    CFG%LALBISCCP = COSP_LALBISCCP
    CFG%LATB532 = COSP_LATB532
    CFG%LBOXPTOPISCCP = COSP_LBOXPTOPISCCP
    CFG%LBOXTAUISCCP = COSP_LBOXTAUISCCP
    CFG%LMEANTBISCCP = COSP_LMEANTBISCCP
    CFG%LMEANTBCLRISCCP = COSP_LMEANTBCLRISCCP
    CFG%LCLISCCP = COSP_LCLISCCP
    CFG%LPCTISCCP = COSP_LPCTISCCP
    CFG%LTAUISCCP = COSP_LTAUISCCP
    CFG%LCLTISCCP = COSP_LCLTISCCP
    ! CLOUDSAT SIMULATOR
    CFG%LDBZE94 = COSP_LDBZE94
    CFG%LCFADDBZE94 = COSP_LCFADDBZE94
    ! CALIPSO/PARASOL SIMULATOR
    CFG%LCFADLIDARSR532 = COSP_LCFADLIDARSR532
    CFG%LCLCALIPSO2 = COSP_LCLCALIPSO2
    CFG%LCLCALIPSO = COSP_LCLCALIPSO
    CFG%LCLHCALIPSO = COSP_LCLHCALIPSO
    CFG%LCLLCALIPSO = COSP_LCLLCALIPSO
    CFG%LCLMCALIPSO = COSP_LCLMCALIPSO
    CFG%LCLTCALIPSO = COSP_LCLTCALIPSO
    CFG%LCLHCALIPSOICE = COSP_LCLHCALIPSOICE
    CFG%LCLLCALIPSOICE = COSP_LCLLCALIPSOICE
    CFG%LCLMCALIPSOICE = COSP_LCLMCALIPSOICE
    CFG%LCLTCALIPSOICE = COSP_LCLTCALIPSOICE
    CFG%LCLHCALIPSOLIQ = COSP_LCLHCALIPSOLIQ
    CFG%LCLLCALIPSOLIQ = COSP_LCLLCALIPSOLIQ
    CFG%LCLMCALIPSOLIQ = COSP_LCLMCALIPSOLIQ
    CFG%LCLTCALIPSOLIQ = COSP_LCLTCALIPSOLIQ
    CFG%LCLHCALIPSOUN = COSP_LCLHCALIPSOUN
    CFG%LCLLCALIPSOUN = COSP_LCLLCALIPSOUN
    CFG%LCLMCALIPSOUN = COSP_LCLMCALIPSOUN
    CFG%LCLTCALIPSOUN = COSP_LCLTCALIPSOUN
    CFG%LCLCALIPSOICE = COSP_LCLCALIPSOICE
    CFG%LCLCALIPSOLIQ = COSP_LCLCALIPSOLIQ
    CFG%LCLCALIPSOUN = COSP_LCLCALIPSOUN
    CFG%LCLCALIPSOTMP = COSP_LCLCALIPSOTMP
    CFG%LCLCALIPSOTMPICE = COSP_LCLCALIPSOTMPICE
    CFG%LCLCALIPSOTMPLIQ = COSP_LCLCALIPSOTMPLIQ
    CFG%LCLCALIPSOTMPUN = COSP_LCLCALIPSOTMPUN
    CFG%LCLTLIDARRADAR = COSP_LCLTLIDARRADAR
    CFG%LPARASOLREFL = COSP_LPARASOLREFL
    ! MISR SIMULATOR
    CFG%LCLMISR = COSP_LCLMISR
    ! OTHER
    CFG%LTOFFSET = COSP_LTOFFSET
    CFG%LFRACOUT = COSP_LFRACOUT
    CFG%LLIDARBETAMOL532 = COSP_LLIDARBETAMOL532
    ! RTTOV
    CFG%LTBRTTOV = COSP_LTBRTTOV
    ! MODIS SIMULATOR
    CFG%LCLTMODIS=COSP_LCLTMODIS
    CFG%LCLWMODIS=COSP_LCLWMODIS
    CFG%LCLIMODIS=COSP_LCLIMODIS
    CFG%LCLHMODIS=COSP_LCLHMODIS
    CFG%LCLMMODIS=COSP_LCLMMODIS
    CFG%LCLLMODIS=COSP_LCLLMODIS
    CFG%LTAUTMODIS=COSP_LTAUTMODIS
    CFG%LTAUWMODIS=COSP_LTAUWMODIS
    CFG%LTAUIMODIS=COSP_LTAUIMODIS
    CFG%LTAUTLOGMODIS=COSP_LTAUTLOGMODIS
    CFG%LTAUWLOGMODIS=COSP_LTAUWLOGMODIS
    CFG%LTAUILOGMODIS=COSP_LTAUILOGMODIS
    CFG%LREFFCLWMODIS=COSP_LREFFCLWMODIS
    CFG%LREFFCLIMODIS=COSP_LREFFCLIMODIS
    CFG%LPCTMODIS=COSP_LPCTMODIS
    CFG%LLWPMODIS=COSP_LLWPMODIS
    CFG%LIWPMODIS=COSP_LIWPMODIS
    CFG%LCLMODIS=COSP_LCLMODIS

    ! COSP Diagnostics
    IF (COSP_LLIDAR_SIM .OR. COSP_LISCCP_SIM .OR. COSP_LMODIS_SIM) & 
       & CALL NEW_DIAG_SET('COSP', ISET)

    IF (COSP_LLIDAR_SIM) THEN
       CALL NEW_DIAGNOSTIC(ISET, 'CALIPSO_LOW_CF', 2, NGRB_CALIPSO_LOW_CLD_FRAC,  &
                         & .FALSE., .FALSE., D_CALIPSO_LCC)
 
       CALL NEW_DIAGNOSTIC(ISET, 'CALIPSO_MID_CF', 2, NGRB_CALIPSO_MID_CLD_FRAC,  &
                         & .FALSE., .FALSE., D_CALIPSO_MCC)

       CALL NEW_DIAGNOSTIC(ISET, 'CALIPSO_HIGH_CF', 2, NGRB_CALIPSO_HIGH_CLD_FRAC,  &
                         & .FALSE., .FALSE., D_CALIPSO_HCC)

       CALL NEW_DIAGNOSTIC(ISET, 'CALIPSO_TOTAL_CF', 2, NGRB_CALIPSO_TOTAL_CLD_FRAC,  &
                         & .FALSE., .FALSE., D_CALIPSO_TCC)
    END IF

    IF (COSP_LMODIS_SIM) THEN
       CALL NEW_DIAGNOSTIC(ISET, 'MODIS_TOTAL_CF', 2, NGRB_MODIS_TOTAL_CLD_FRAC, &
                         & .FALSE., .FALSE., D_MODIS_TOTAL)

       CALL NEW_DIAGNOSTIC(ISET, 'MODIS_WATER_CF', 2, NGRB_MODIS_WATER_CLD_FRAC, &
                         & .FALSE., .FALSE., D_MODIS_LIQ)

       CALL NEW_DIAGNOSTIC(ISET, 'MODIS_ICE_CF', 2, NGRB_MODIS_ICE_CLD_FRAC,  &
                         & .FALSE., .FALSE., D_MODIS_ICE)
    END IF

    IF (COSP_LISCCP_SIM) THEN
       CALL NEW_DIAGNOSTIC(ISET, 'ISCCP_TOTAL_CF', 2, NGRB_ISCCP_TOTAL_CLD_FRAC,  &
                         & .FALSE., .FALSE., D_ISCCP_TOTAL_CF)

       CALL NEW_DIAGNOSTIC(ISET, 'ISCCP_CLD_ALBEDO', 2, NGRB_ISCCP_CLD_ALBEDO,  &
                         & .FALSE., .FALSE., D_ISCCP_CLDALB)

       CALL NEW_DIAGNOSTIC(ISET, 'ISCCP_CLD_TOP_PRES', 2, NGRB_ISCCP_CLD_TOP_PRESSURE,  &
                         & .FALSE., .FALSE., D_ISCCP_CTP)
    END IF
    
    IF(ANY((/COSP_LISCCP_SIM,COSP_LMODIS_SIM,COSP_LMISR_SIM,COSP_LRADAR_SIM, &
         COSP_LLIDAR_SIM,COSP_LRTTOV_SIM/))) THEN
       COSP_RUN = .TRUE.
       IF(MYPROC == 1 .AND. COSP_IBL == 1) THEN
          WRITE(*,*) 'COSP SIMULATOR WILL BE CALLED AT OUTPUT FREQUENCY =',&
               COSP_OUTPUT_FREQUENCY
          WRITE(*,'(A20,2X,L1)') 'COSP_LISCCP_SIM = ',COSP_LISCCP_SIM
          WRITE(*,'(A20,2X,L1)') 'COSP_LMODIS_SIM = ',COSP_LMODIS_SIM
          WRITE(*,'(A20,2X,L1)') 'COSP_LMISR_SIM = ', COSP_LMISR_SIM
          WRITE(*,'(A20,2X,L1)') 'COSP_LRADAR_SIM = ',COSP_LRADAR_SIM
          WRITE(*,'(A20,2X,L1)') 'COSP_LLIDAR_SIM = ',COSP_LLIDAR_SIM
          WRITE(*,'(A20,2X,L1)') 'COSP_LRTTOV_SIM = ',COSP_LRTTOV_SIM
       ENDIF
    ELSE
       COSP_RUN = .FALSE.
       IF(MYPROC==1 .AND. COSP_IBL == 1) WRITE(*,*) 'COSP SIMULATOR NOT REQUESTED'
    ENDIF

    WRITE(NULOUT,*) '*********** END COSP SETUP INFORMATION *************'
  END SUBROUTINE SUCOSP
    
END MODULE YOMCOSP_UTIL
