      SUBROUTINE CBTSIM(TEMP,TAUHRS,SURTMP,LGND,KCHAN,BTSIMU)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- CBTSIM 
C      ADAPTED FROM SIMCBT
C
C   LANGUAGE- FORTRAN     TYPE- SUBROUTINE
C    
C
C   VERSION- 1.0    DATE- 05/01/07   PROGRAMMER- FRANK TILLEY (IMSG)
C
C   FUNCTION- THIS ROUTINE CALCULATES SIMULATED BRIGHTNESS
C        TEMPERATURES FOR CALCULATION OF ATTENUATION ABOVE A GIVEN
C        PRESSURE LEVEL, USUALLY THE CLOUD TOP
C
C   CALLING ARGUMENTS-
C      VARIABLE                TYPE I/O FUNCTION
C      --------                ---- --- --------
C      TEMP                    REAL  I   THE ATMOSPHERIC TEMPERATURE
C                                        PROFILE.
C      TAUHRS                  REAL  I   TRANSMITTANCE PROFILES FOR  
C                                        HIRS CHANNELS 1-11.
C      SURTMP                  REAL  I   TEMPERATURE AT THE SURFACE  
C                                        (OR CLOUD TOP).
C      LGND                    REAL  I   THE BOTTOM MOST LEVEL OF THE
C                                        LAYER FOR WHICH TO COMPUTE THE
C                                        B. TEMPERATURE CONTRIBUTION.
C      KCHAN                    INT  I   THE CHANNEL TO SIMULATE THE
C                                        BRIGHTNESS TEMP. FOR.
C      BTSIMU                  REAL  O   SIMULATED BRIGHTNESS TEMP.
C                                        CONTRIBUTION FOR CHANNEL KCHAN.
C
C   COMMON AREAS- RETPARM
C
C   LOCAL VARIABLES-
C      NAME              TYPE     DESCRIPTION
C      ----              ----     -----------
C      B1                REAL     CHANNEL RADIANCE FROM THE UPPER LEVEL
C      B2                REAL     CHANNEL RADIANCE FROM THE LOWER LEVEL
C      BS                REAL     CHANNEL RADIANCE FOR THE "SURFACE" LEVEL
C      RDN               REAL     CUMMULATIVE RADIANCE VALUE
C      TAU1              REAL     ATTENUATION FOR THE UPPER LEVEL
C      TAU2              REAL     ATTENUATION FOR THE LOWER LEVEL
C
C
C
C**********************************************************************
C
C
      INCLUDE 'RETPARM'
      INCLUDE 'SPVAL'

      REAL   TAUHRS(PRFTLV,11),TEMP(PRFTLV),TEMPX(PRFTLV),IPLKRD,PLKRAD
      REAL   BTSIMU, SURTMP, SKINTK, TAU1, TAU2, B1, B2, BS, RDN 
      INTEGER LGND, LBTM, I, L, KCHAN

      BTSIMU = R4SPV
C
C  USE RETRIEVED TEMPERATURES FROM 0.1 MB TO THE LEVEL ABOVE THE BOTTOM
C  LEVEL SPECIFIED IN THE CALLING ARGUMENTS.
C
      LBTM = LGND - 1
      DO 50 L=1,LBTM
        TEMPX(L) = TEMP(L)
  50  CONTINUE
C
C  SET SURFACE AND SKIN TEMPERATURE TO THE TEMPERATURE PROVIDED IN THE
C  CALLING ARGUMENTS.
C
      TEMPX(LGND) = SURTMP
      SKINTK      = SURTMP
C
C  SIMULATE RADIANCE FROM THE TOP OF THE ATMOSPHERE DOWN TO THE LEVEL
C  CORRESPONDING TO THE TEMPERATURE PROVIDED IN THE CALLING ARGUMENTS.
C
c  Make a radiance for the channel using the retrieved temperature at 
c  level 1 
      B1=PLKRAD(TEMPX(1),KCHAN)
c  Get the HIRS transmittance for level 1 for the specified channel
c  Based on OPTRAN this should always be 1.0.

      TAU1=TAUHRS(1,KCHAN)
      RDN=B1*(1.0-TAU1)

C
C  ADD CONTRIBUTIONS ONLY TO THE LEVEL SPECIFIED IN THE CALLING ARGUMENTS.
C
      DO 120 I=2,LGND
        B2=PLKRAD(TEMPX(I),KCHAN)
        TAU2=TAUHRS(I,KCHAN)
        RDN=RDN+(1.0 - (.5*(B1+B2)*(TAU1-TAU2)))
        B1=B2
        TAU1=TAU2
  120 CONTINUE
C
      BS=PLKRAD(SKINTK,KCHAN)
      RDN=RDN+BS*TAUHRS(LGND,KCHAN)
C
C  CONVERT RADIANCE VALUE TO BRIGHTNESS TEMPERATURE
C
      IF(RDN.GT.0.) BTSIMU = IPLKRD(RDN,KCHAN)
ccft
      if (KCHAN.EQ.6) then
         print *,'CBTSIM: SKINTK = ',SKINTK,' CHAN ',KCHAN,' BTSIMU ',BTSIMU
      end if
      RETURN
      END
