      SUBROUTINE CLDSEA(OVRLND,ICEFRE,TSFEST,TSFLIM,RESULT,ERSTAT)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME- CLDSEA
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CEMSCS
C
C   VERSION- 1.0    DATE- 2/01/93  PROGRAMMER- CARLA NEIGH (SMSRC)
C   VERSION- 2.0    DATE- 9/26/94  PROGRAMMER- SURYA JANDHYALA(SMSRC)
C     XATOV-54  CHANGED COMMENT TO REMOVE REFERENCE TO THE MSU
C               INSTRUMENT.
C   VERSION- 3.0    DATE- 5/29/96  PROGRAMMER- LEAH WILSON(HSTX)
C     SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VRESION- 3.1    DATE 03/26/03  PROGRAMMER- Rickey Rollins (CSC)
C     Replaced test for equivalence with R4SPV to ISR4SPV function 
C
C   FUNCTION-  THIS SUBROUTINE PERFORMS THE COLD SEA TEST ON A HIRS
C              SPOT TO DETERMINE IF CLOUDS ARE PRESENT.
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME    TYPE   I/O   DESCRIPTION
C   ----    ----   ---   -----------
C   OVRLND  LOGICAL I    LOGICAL FLAG SPECIFYING WHETHER HIRS SPOT
C                        IS OVER LAND (TRUE = OVER LAND, FALSE =
C                        OVER WATER).
C   ICEFRE  LOGICAL I    LOGICAL FLAG SPECIFYING WHETHER HIRS SPOT
C                        IS ICE-FREE (TRUE = ICE-FREE, FALSE = ICE-
C                        COVERED).
C   TSFEST   REAL   I    SURFACE TEMPERATURE ESTIMATE FOR HIRS SPOT (K)
C   TSFLIM   REAL   I    SURFACE TEMPERATURE ESTIMATE LIMIT (K).
C   RESULT   INT   I/O   VALUE INDICATING RESULT OF CLOUD TEST.  0 =
C                        TEST DETERMINED SPOT TO BE CLOUDY, 1 = TEST
C                        DID NOT DETERMINE SPOT TO BE CLOUDY, 2 =
C                        DETERMINATION COULD NOT BE MADE.
C   ERSTAT   INT    O    ERROR STATUS CODE
C                         = 0 IF NO ERROR OCCURRED,
C                         = 10 IF NON-FATAL ERROR OCCURRED,
C                         = 99 IF FATAL ERROR OCCURRED.
C
C   LOCAL PARAMETERS-
C   NAME    TYPE   DESCRIPTION
C   ----    ----   -----------
C   STATUS  INT    STATUS CODE RETURNED FROM CLDSE$.
C
C   SUBPROGRAMS CALLED- CLDSE$
C
C   EXIT STATES-
C    ERSTAT = 10 : NON-FATAL ERROR - COLD SEA TEST LIMIT IS MISSING
C                  VALUE.
C    ERSTAT = 99 : FATAL ERROR RETURNED FROM CLDSE$.
C
C   COMMENTS-
C
C    IT IS ASSUMED THAT RESULT HAS BEEN INITIALIZED TO 2 BEFORE THIS
C    ROUTINE IS CALLED.
C
C    THIS CLOUD TEST IS BASED ON THE FOLLOWING REFERENCE:
C     WAHICHE, SCOTT, AND CHEDIN, "CLOUD DETECTION AND CLOUD PARAMETERS
C     RETRIEVAL FROM THE SATELLITES OF THE TIROS-N SERIES". ANNALES
C     GEOPHYSICAE, 1986, 4, B, 2, 207-222.
C
C***********************************************************************
C
C
      LOGICAL OVRLND, ICEFRE, ISR4SPV
      REAL TSFEST, TSFLIM
      INTEGER RESULT, ERSTAT
C
      INTEGER STATUS
C
      INCLUDE'SPVAL'
C
      ERSTAT = 0
C
C   IF THE TEST LIMIT IS MISSING, WRITE A WARNING MESSAGE TO THE
C   MAINTENANCE MESSAGE LOG FILE AND SKIP THIS TEST.
C
C     IF (TSFLIM.EQ.R4SPV) THEN
      IF (ISR4SPV(TSFLIM)) THEN
          CALL CLDSE$(101,STATUS)
          ERSTAT = 10
          IF (STATUS.EQ.99) ERSTAT = 99
          RETURN
      ENDIF
C
C   THE COLD SEA TEST IS ONLY PERFORMED FOR THOSE HIRS SPOTS
C   WHICH ARE LOCATED OVER WATER AND IN NON-ICE-COVERED AREAS.
C
      IF ((.NOT. ISR4SPV(TSFEST)).AND.(.NOT.(OVRLND)).AND.ICEFRE) THEN
C
C        IF THE SURFACE TEMPERATURE ESTIMATE IS LESS THAN THE
C        TEST LIMIT, THE SPOT IS CLOUDY.
C
          IF (TSFEST .LT. TSFLIM) THEN
             RESULT = 0
          ELSE
             RESULT = 1
          ENDIF
      ENDIF
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE CLDSE$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- CLDSE$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 02/01/93  PROGRAMMER- CARLA NEIGH (SMSRC)
C   VERSION- 3.0    DATE- 10/09/97  PROGRAMMER- LEAH WILSON (HSTX)
C
C   FUNCTION- THIS SUBROUTINE WRITES OUT ERROR MESSAGES TO THE
C             KIDS MESSAGE LOG FILE FOR THE SUBROUTINE CLDSEA.
C   VERSION- 3.0 CONVERT FROM MMLF TO KIDS.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT  FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----  ----------------------
C     ...KIDS                   O    KUNIT KIDS MESSAGE LOG FILE.
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-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     I                  INT  DO LOOP CONTROL INDEX.
C     COUNT              INT  COUNTER FOR NUMBER OF TIMES $-ROUTINE
C                             HAS BEEN INVOKED.
C     KMSG            CH*100  KIDS MESSAGE BUFFER
C     C4TEMP            CH*4   HOLDING BUFFER FOR WRITING TO KIDS
C
C   SUBPROGRAMS CALLED-  KIDS
C
C   EXIT STATES-
C       STAT =  0; INDICATING NO OR A NON-FATAL ERROR WAS DETECTED.
C       STAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C***********************************************************************
C
      CHARACTER*4   C4TEMP
      CHARACTER*100 KMSG
      INTEGER   STAT, I, MSGCOD, COUNT
C
      DATA COUNT /0/
      SAVE COUNT
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
      STAT = 0
C
      COUNT = COUNT + 1
C
      IF (COUNT .GT. 1) RETURN
C
C     CHECK FOR VALID MESSAGE CODES.
C
      IF (MSGCOD .EQ. 101) THEN
        KMSG(1:50)=' * WARNING * Cold sea test for cloud detection can'
        KMSG(51:100)='not be performed since test limit is missing.'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
        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
