      SUBROUTINE SFCTMP(OVRLND,NIGHT,TSF,TSE,RESULT,ERSTAT)
      IMPLICIT NONE
C
C********************************************************************
C
C   NAME- SFCTMP
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 03/04/93   PROGRAMMER- JEFF MANNING, SMSRC
C   VERSION- 3.0    DATE- 05/29/96   PROGRAMMER- LEAH WILSON (HSTX)
C         SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1    DATE- 09/24/97   PROGRAMMER- LEAH WILSON (HSTX)
C         RTOVS UPGRADE TO CORRECT CODE SO THAT TEST COULD FAIL ONLY
C         WHEN BOTH THE SURFACE TEMPERATURE ESTIMATE AND FCST SFC.
C         TEMPERATURE ARE PRESENT FOR THE DIFFERENCE CALCULATION.
C         ALSO, UPGRADE TO ADD NEW LIMIT TO MAKE THE SFC. TEMP.
C         TEST DOUBLE-ENDED FOR BOTH DAY AND NIGHT.  SENSE OF TEMPERATURE
C         TEST SWITCHED FROM TSE-TSF TO TSF-TSE.  ALSO, SFCTM$ CONVERTED 
C         TO KIDS CONVENTION.
C   VERSION- 3.2    DATE- 11/19/97   PROGRAMMER- LEAH WILSON (HSTX)
C         COMMENT OUT ABS FOR DIFF.  INCLUDE /REGCOE/ AND REMOVE DUPLICATE
C         VARIABLES FROM SUBROUTINE CALL.
C   VERSION- 3.3    DATE- 03/26/03   PROGRAMMER- Rickey Rollins (CSC)
C         Replaced test of equality with R4SPV to function that checks for
C         closeness.
C   VERSION- 3.4    DATE- 07/21/10   PROGRAMMER- FRANK TILLEY (IMSG)
C         CORRECTED AN ERROR THAT SETS THE CLOUD TEST RESULT TO 'CLEAR'
C         WHEN EITHER THE FORECAST SURFACE TEMPERATURE OR THE SURFACE
C         TEMPERATURE ESTIMATE IS MISSING.
C
C   FUNCTION- THIS ROUTINE PERFORMS THE SURFACE TEMPERATURE TEST.  THE
C             TEST USES THE SURFACE TEMPERATURE FORECAST AND THE
C             TEMPERATURE ESTIMATE FOR THE CURRENT SPOT.
C
C             THE SPOT IS MARKED CLOUDY IF:
C
C                ABS(TSE-TSF) > REGCOE:SFH2OL   FOR WATER AREAS.
C                ABS(TSE=TSF) > REGCOE:SFLNDL   FOR LAND AREAS.
C
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE      TYPE   I/O      DESCRIPTION
C     --------      ----   ---      -----------
C     NIGHT          L*4    I       FLAG INDICATING THAT THE CURRENT
C                                   HIRS SPOT IS FOR DAYTIME DATA OR
C                                   FOR NIGHTTIME DATA.  A VALUE OF
C                                   TRUE INDICATES NIGHT, A VALUE OF
C                                   FALSE INDICATES DAY.
C     OVRLND       LOGICAL  I       FLAG INDICATING THAT THE CURRENT
C                                   HIRS SPOT IS OVER LAND, OR OVER
C                                   WATER.  A VALUE OF TRUE INDICATES
C                                   OVER LAND AND FALSE INDICATES OVER
C                                   WATER.
C     TSF            REAL   I       SURFACE TEMPERATURE FORECAST VALUE.
C     TSE            REAL   I       SURFACE TEMPERATURE ESTIMATE.
C     RESULT         INT    O       CLOUD TEST RESULT FOR THE SURFACE
C                                   TEMPERATURE TEST.  A ONE INDICATES
C                                   THAT THE SPOT WAS DETERMINED NOT TO
C                                   BE CLOUDY, A ZERO INDICATES THAT THE
C                                   SPOT WAS DETERMINED TO BE CLOUDY,
C                                   AND A TWO INDICATES THAT THE TEST
C                                   WAS NOT PERFORMED.
C     ERSTAT         INT    O       ERROR STATUS CODE.
C
C
C   LOCAL PARAMTERS-
C     VARIABLE      TYPE      DEFINITION
C     --------      ----      ----------
C     MSGCOD         INT      MESSAGE CODE FOR SFCTM$ TO DETERMINE
C                             WHAT THE ERROR IS.  MSGCOD EQUALS 101
C                             FOR LAND AREAS AND EQUALS 102 FOR
C                             WATER AREAS.
C     STATUS         INT      STATUS CODE RETURNED FROM SFCTM$.  A
C                             VALUE OF 99 INDICATES A FATAL ERROR AND
C                             A ZERO INDICATES A NORMAL RETURN.
C     DIFF           REAL     HOLDS THE ABSOLUTE DIFFERENCE BETWEEN
C                             TSE MINUS TSF.
C
C   SUBROUTINE USED- SFCTM$
C
C   FUNCTIONS USED- ABS
C
C   EXIT STATES-
C               ERSTAT = 10 : TEST LIMIT IS A MISSING VALUE.
C
C   COMMENTS- IT IS ASSUMED THAT THE VARIABLE RESULT IS SET TO TWO
C             PRIOR TO THE CALL TO THIS ROUTINE.
C
C   REFERENCE-  WAHICHE, SCOTT, AND CHEDIN, "CLOUD DETECTION AND CLOUD
C               PARAMETERS RETRIEVAL FROM THE SATELLITE OF THE TIROS-N
C               SERIES".  ANNALES GEOPHYSICAE, 1986, 4, B, 2, 207-222.
C
C********************************************************************
C
      INTEGER RESULT, ERSTAT, STATUS, MSGCOD
      REAL    TSF, TSE, DIFF, TSTLMH, TSTLML
      LOGICAL OVRLND, NIGHT, ISR4SPV
C
      INCLUDE 'REGCOE'
      INCLUDE 'SPVAL'
C
C     INITIALIZE ERSTAT TO ZERO.
C
      ERSTAT = 0
C
C     CHECK TO SEE WHICH TEST LIMIT IS TO BE USED.
C
      IF(OVRLND) THEN
          TSTLMH = SFLNDL
          TSTLML = -SFLNDL
          MSGCOD = 101
      ELSE
         MSGCOD = 102
         IF(NIGHT) THEN
            TSTLMH = SFHNOL
            TSTLML = SFHNLL
         ELSE
            TSTLMH = SFH2OL
            TSTLML = SFH2LL
         ENDIF
      END IF
C
C     CHECK TO SEE IF THE LIMIT IS A MISSING VALUE.
C
      IF(ISR4SPV(TSTLMH).OR.ISR4SPV(TSTLML)) THEN
C
C         WRITE A MESSAGE TO THE KIDS FILE.
C
          CALL SFCTM$(MSGCOD,STATUS)
C
C         CHECK THE STATUS.
C
          IF(STATUS.EQ.99) THEN
              ERSTAT =  99
          ELSE
              ERSTAT = 10
C
C                 RETURN TO CALLING ROUTINE.
C
              RETURN
          END IF
C
      END IF
C
C     CALCULATE THE DIFFERENCE BETWEEN THE TEMPERATURE ESTIMATE
C     AND THE FORECAST TEMPERATURE.
C
C     IF(TSE.NE.R4SPV.AND.TSF.NE.R4SPV) THEN
      IF(.NOT. ISR4SPV(TSE) .AND. .NOT. ISR4SPV(TSF)) THEN
         DIFF = TSF - TSE
      ELSE
         DIFF=R4SPV
      ENDIF
C
C     CHECK THE ABSOLUTE VALUE OF THIS DIFFERENCE.
C
ccmpf DIFF = ABS(DIFF)
C
C     IF THE DIFFERENCE VALUE IS MISSING, DO NOT SET THE CLOUD
C     TEST RESULT FLAG.  OTHERWISE, PERFORM THE SURFACE TEMPERATURE
C     TEST AND SET THE RESULT FLAG ACCORDINGLY.
C
      IF (.NOT. ISR4SPV(DIFF)) THEN
         IF(DIFF.GT.TSTLMH.OR.DIFF.LT.TSTLML) THEN
C
C         SET THE RESULT TO ZERO FOR CLOUDY.
C
             RESULT = 0
         ELSE
C
C         SET THE RESULT TO ONE FOR NOT CLOUDY.
C
             RESULT = 1
         END IF
      END IF
C
C     RETURN TO CALLING ROUTINE.
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE SFCTM$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- SFCTM$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 03/05/93    PROGRAMMER- JEFF MANNING (SMSRC)
C   VERSION- 3.0    DATE- 09/23/97    PROGRAMMER- LEAH WILSON (HSTX)  
C
C   FUNCTION- THIS SUBROUTINE WRITES OUT ERROR MESSAGES TO THE
C             KIDS MESSAGE LOG FILE FOR THE SUBROUTINE SFCTMP.
C   VERSION- 3.0 CONVERSION FROM MMLF TO KIDS FILE.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT  FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----  ----------------------
C     ....KIDS.....             O   KUNIT  KRAY INTERACTIVE DOCUMENTATION
C                                          SYSTEM. 
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     MSGCOD             INT  I   MESSAGE CODE OF ERROR DETECTED.
C     STAT               INT  O   ERROR STATUS CODE
C                                  = 0 IF NO ERROR OCCURRED,
C                                 <> 0 IF ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     NAME              CH*6  $ ROUTINE NAME.
C     C4TEMP            CH*4  DUMMY FOR WRITING TO KIDS FILE.
C     I                  INT  DO LOOP CONTROL INDEX.
C     COUNT              INT  COUNTER FOR NUMBER OF TIMES $-ROUTINE
C                             HAS BEEN INVOKED FOR EACH MESSAGE.
C     KMSG             CH*100 KIDS MESSAGE BUFFER
C
C   SUBPROGRAMS CALLED-  KIDS 
C
C***********************************************************************
C
      CHARACTER*4 C4TEMP
      CHARACTER*6 NAME
      CHARACTER*100 KMSG
      INTEGER   STAT, I, MSGCOD, COUNT(2)
C
      DATA COUNT /0,0/
      SAVE COUNT
C
C   SET THE MAIN NAME.
C
      SUBNAM = 'SPGDVR'
      STAT = 0
C
C
C     CHECK FOR VALID MESSAGE CODES.
C
      IF (MSGCOD .EQ. 101) THEN
        COUNT(1) = COUNT(1) + 1
        IF(COUNT(1).GT.1) RETURN
        KMSG(1:51)='* WARNING * Surface temperature test could not be '
        KMSG(52:100)='done over land - test limit is a missing value. '
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE IF (MSGCOD .EQ. 102) THEN
        COUNT(2) = COUNT(2) + 1
        IF(COUNT(2).GT.1) RETURN
        KMSG(1:51)='* WARNING * Surface temperature test could not be '
        KMSG(52:100)='done over water - test limit is a missing value.'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
C NOT A VALID MSGCOD
C
      ELSE
        STAT=0
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END

