      SUBROUTINE ALBEDO(NIGHT,SNWFRE,OVRLND,LNDLIM,WTRLIM,HIRS20,
     1                  RESULT,ERSTAT)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME- ALBEDO
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CEMSCS
C
C   VERSION- 1.0    DATE- 2/4/93     PROGRAMMER- CARLA NEIGH (SMSRC)
C   VERSION- 3.0    DATE- 5/29/96    PROGRAMMER- LEAH WILSON (HSTX)
C       SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C   VERSION- 3.2    DATE- 07/14/10   PROGRAMMER- FRANK TILLEY (IMSG)
C        CORRECTED CODE TO PERFORM THE ALBEDO TEST ONLY IF THE
C        HIRS CHANNEL 20 VALUE IS "NOT" MISSING RATHER THAN IF IT
C        "IS" MISSING.
C
C   FUNCTION-  THIS SUBROUTINE PERFORMS THE ALBEDO 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   NIGHT  LOGICAL  I    LOGICAL FLAG SPECIFYING WHETHER HIRS SPOT
C                        IS IN NIGHTTIME (TRUE = NIGHTTIME, FALSE =
C                        DAYTIME).
C   SNWFRE LOGICAL  I    LOGICAL FLAG SPECIFYING WHETHER HIRS SPOT
C                        IS OVER A SNOW-FREE REGION (TRUE = OVER SNOW-
C                        FREE REGION, FALSE = OVER SNOW-COVERED REGION).
C   OVRLND LOGICAL  I    LOGICAL FLAG SPECIFYING WHETHER HIRS SPOT IS
C                        OVER LAND (TRUE = OVER LAND, FALSE = OVER
C                        WATER).
C   LNDLIM   REAL   I    LIMIT FOR ALBEDO TEST OVER LAND AREAS.
C   WTRLIM   REAL   I    LIMIT FOR ALBEDO TEST OVER WATER AREAS.
C   HIRS20   REAL   I    HIRS CHANNEL 20 PERCENT ALBEDO VALUE FOR THE
C                        SPOT.
C   RESULT   INT   I/O   RESULT OF THE ALBEDO TEST.  VALUE IS 0 IF THE
C                        TEST DETERMINED SPOT TO BE CLOUDY, 1 IF THE
C                        TEST DID NOT DETERMINE THE SPOT TO BE CLOUDY,
C                        AND 2 IF A DETERMINATION COULD NOT BE MADE.
C                        IT IS ASSUMED THAT RESULT HAS BEEN INITIALIZED
C                        TO 2 BEFORE THIS ROUTINE IS CALLED.
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 ALBED$.
C   TSTLIM  REAL   TEST LIMIT FOR THE ALBEDO TEST.
C
C   SUBPROGRAMS CALLED- ALBED$
C
C   EXIT STATES-
C    ERSTAT = 10 : NON-FATAL ERROR - ALBEDO TEST LIMIT IS MISSING
C                  VALUE.
C    ERSTAT = 99 : FATAL ERROR RETURNED FROM ALBED$.
C
C   COMMENTS-
C    IT IS ASSUMED THAT RESULT HAS BEEN INITIALIZED TO 2 BEFORE THIS
C    ROUTINE IS CALLED.
C
C    THE REFERENCE FOR THIS CLOUD TEST IS:
C     CHEDIN ET AL, "RECENT PROGRESS IN THE DETERMINATION OF
C     METEOROLOGICAL PARAMETERS FROM THE SATELLITES OF THE TIROS-N
C     SERIES". TECHNICAL PROCEEDINGS THIRD ITOVS STUDY CONFERENCE, 13-19
C     AUGUST 1986, MADISON, WI.
C
C***********************************************************************
C
      LOGICAL NIGHT, SNWFRE, OVRLND
      REAL LNDLIM, WTRLIM, HIRS20
      INTEGER RESULT, ERSTAT
C
      INTEGER STATUS
      REAL TSTLIM

      LOGICAL ISR4SPV
C
      INCLUDE'SPVAL'
C
      ERSTAT = 0
C
C   THE ALBEDO TEST IS ONLY PERFORMED FOR THOSE HIRS SPOTS
C   WHICH ARE IN DAYTIME AND OVER SNOW-FREE AREAS.
C
      IF ((.NOT. NIGHT) .AND. (SNWFRE)) THEN
C
C       CHECK FOR LAND OR WATER TEST.
C
         IF (OVRLND) THEN
C
C          IF THE TEST LIMIT FOR LAND AREAS IS MISSING, WRITE A WARNING
C          MESSAGE TO THE MAINTENANCE MESSAGE LOG FILE AND SKIP THIS
C          TEST.
C
            IF (ISR4SPV(LNDLIM)) THEN
               CALL ALBED$(101,STATUS)
               ERSTAT = 10
               IF (STATUS.EQ.99) ERSTAT = 99
               RETURN
            ELSE
               TSTLIM = LNDLIM
            ENDIF
C
         ELSE
C
C          IF THE TEST LIMIT FOR WATER AREAS MISSING, WRITE A WARNING
C          MESSAGE TO THE MAINTENANCE MESSAGE LOG FILE AND SKIP THIS
C          TEST.
C
            IF (ISR4SPV(WTRLIM)) THEN
               CALL ALBED$(102,STATUS)
               ERSTAT = 10
               IF (STATUS.EQ.99) ERSTAT = 99
               RETURN
            ELSE
               TSTLIM = WTRLIM
            ENDIF
         ENDIF
C
         IF (.NOT.(ISR4SPV(HIRS20))) THEN
C
C          IF THE HIRS CHANNEL 20 VALUE IS GREATER THAN THE
C          TEST LIMIT, THE SPOT IS CLOUDY.
C
             IF (HIRS20 .GT. TSTLIM) THEN
                RESULT = 0
             ELSE
                RESULT = 1
             ENDIF
         ENDIF
      ENDIF
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE ALBED$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- ALBED$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 02/08/93    PROGRAMMER- CARLA NEIGH (SMSRC)
C   VERSION- 3.0    DATE- 09/24/98    PROGRAMMER- LEAH WILSON, RAYTHEON
C
C   FUNCTION- THIS SUBROUTINE WRITES OUT ERROR MESSAGES TO THE
C             MAINTENANCE MESSAGE LOG FILE FOR THE SUBROUTINE ALBEDO.
C   VERSION- 3.0 CONVERT MMLF TO KIDS.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT  FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----  ----------------------
C     KIDS                      O   KUNIT  KRAY I/A DOCUMENTATION 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 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPGUNT'
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 FOR EACH MESSAGE.
C
C   SUBPROGRAMS CALLED-  KIDS 
C
C   EXIT STATES-
C       STAT =  1; INDICATING A NON-FATAL ERROR WAS DETECTED.
C       STAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C***********************************************************************
C
      INTEGER    STAT, I, MSGCOD, COUNT(2)
      CHARACTER*4 C4TEMP
      CHARACTER*100 KMSG
C
      DATA COUNT /0,0/
      SAVE COUNT
C
C   SET THE SUBROUTINE NAME.
C
      SUBNAM = 'SPGDVR'
      STAT = 0
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:50)='* WARNING * The Albedo test could not be performe'
         KMSG(51:100)='d over land due to missing test threshold.'
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSEIF (MSGCOD .EQ. 102) THEN
         COUNT(2) = COUNT(2) + 1
         IF (COUNT(2).GT.1) RETURN
         KMSG(1:50)='* WARNING * The Albedo test could not be performe'
         KMSG(51:100)='d over sea due to missing test threshold.'
         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


