      SUBROUTINE N4AVHR(SPOT,LINE,TSTLIM,RESULT,ERSTAT)
C*********************************************************************
C
C   NAME- N4AVHR
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE    COMPUTER- CRAY J916
C
C   VERSION- 1.0 DATE- 02/09/93   PROGRAMMER- CARLA NEIGH, SMSRC
C   VERSION- 2.0 DATE- 9/28/94   PROGRAMMER-  SURYA JANDHYALA(SMSRC)
C     XATOV-57 CHANGED THE AVHRR CHANNEL ACCESS LOCATIONS INTO THE
C              CHANGED HIRS DATA BUFFER.C   i
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/22/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.2    DATE- 04/06/98   PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.3    DATE- 11/18/98   PROGRAMMER- LEAH WILSON,RAYTHEON
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C
C   FUNCTION- THIS SUBROUTINE PERFORMS THE FOURTH COMPONENT OF THE NEW
C             AVHRR TESTS ON A HIRS SPOT TO DETERMINE IF CLOUDS ARE
C             PRESENT.  THIS TEST COMPONENT CHECKS THE PERCENTAGE OF
C             AVHRR CHANNEL 4 VALUES GREATER THAN OR EQUAL TO 270.0 K.
C             IF THIS PERCENTAGE IS LOWER THAN THE TEST LIMIT, THE HIRS
C             SPOT IS MARKED CLOUDY.
C   VERSION- 3.1 RTOVS UPGRADE TO USE TERRAIN HEIGHT.  CONVERT N4AVH$ TO
C                USE KIDS FILE.  REMOVE HIRDAT AND ASSOCIATED INFO FROM
C                SUBROUTINE CALL SINCE /INSPARM/ AND /INSCOM/ ARE USED.
C   VERSION- 3.2 RTOVS UPGRADE - LIMIT SOFTCODED TO N4AVHL IN /REGCOE/.
C                ALSO SOFTCODED TERRAIN HEIGHT LIMIT IN /REGCOE/ TO
C                THLIMIT.
C   VERSION- 3.3 CORRECT INDICES INTO HIRDAT BUFFER SO GAC DATA IS
C                ACCESSED CORRECTLY.  ALSO RESTRUCTURE DO 10 LOOP TO BE
C                MORE EFFICIENT.  IN HIRDAT,                                                            
C                CH. 1 = INDICES 36-52, AVG. CH 1 = INDEX 53
C                CH. 2 = INDICES 54-70, AVG. CH 2 = INDEX 71
C                CH. 3A = INDICES 72-88, AVG. CH 3A = INDEX 89
C                CH. 3B = INDICES 90-106, AVG. CH 3B = INDEX 107
C                CH. 4 = INDICES 108-124, AVG. CH 4 = INDEX 125
C                CH. 5 = INDICES 126-142, AVG. CH 5 = INDEX 143
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C
C   CALLING SEQUENCE PARAMETERS-
C
C     NAME      TYPE  I/O   DESCRIPTION
C     ----      ----  ---   -----------
C     SPOT      INT    I    NUMBER OF HIRS SPOT WITHIN THE SCAN LINE
C                           FOR WHICH THE TESTS ARE BEING PERFORMED.
C     LINE      INT    I    NUMBER OF HIRS SCAN LINE WITHIN THE HIRS
C                           DATA BUFFER WHICH CONTAINS THE SPOT.
C     TSTLIM    REAL   I    TEST LIMIT FOR PERCENTAGE OF AVHRR
C                           SPOTS >= N4AVHL.
C     RESULT    INT   I/O   RESULT OF THE FOURTH COMPONENT OF THE
C                           NEW AVHRR TEST.  A VALUE OF 0 MEANS
C                           THE SPOT WAS DETERMINED TO BE CLOUDY FROM
C                           THE TEST COMPONENT, 1 MEANS THE SPOT
C                           WAS NOT DETERMINED TO BE CLOUDY, 2 MEANS
C                           THAT A DETERMINATION COULD NOT BE MADE.
C     ERSTAT    INT    O    ERROR RETURN 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     AVRPIX    INT      DO LOOP COUNTER FOR AVHRR PIXELS.
C     NAVHR4    INT      NUMBER OF AVHRR PIXELS >= N4AVHL.
C     PCNTA4    REAL     PERCENTAGE OF AVHRR PIXELS >= N4AVHL.
C     TAVHR4    INT      TOTAL NUMBER OF AVHRR PIXELS.
C     STATUS    INT      STATUS CODE RETURNED FROM N4AVH$.
C
C   SUBPROGRAMS CALLED- N4AVH$
C
C   FUNCTIONS USED- REAL
C
C   EXIT STATES-
C     ERSTAT = 10 - THE COMPONENT OF THE NEW AVHRR TEST COULD NOT BE
C                   EXECUTED.
C     ERSTAT = 99 - A FATAL ERROR WAS RETURNED FROM N4AVH$.
C
C   COMMENTS-
C
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     COX, KARL W., 1987: "THE NOAA-K,L & M SOUNDING ALGORITHMS
C     SCIENTIFIC BASIS., S M SYSTEMS AND RESEARCH CORPORATION,
C     IIA-7 - IIA-20.
C
C**********************************************************************
C
      INTEGER SPOT, LINE, RESULT, ERSTAT
      REAL TSTLIM
C
      INTEGER AVRPIX, NAVHR4, TAVHR4, STATUS
      REAL PCNTA4
      LOGICAL   ISR4SPV
C
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'REGCOE'
      INCLUDE 'SPVAL'
C
      ERSTAT = 0
C
C    GET TERRAIN HEIGHT FROM HIRANC
C
      THIGHT=HIRANC(SPOT,LINE,8)
C
C    IF TEST LIMIT IS A MISSING VALUE, WRITE A WARNING MESSAGE
C    TO THE KIDS MESSAGE LOG FILE AND SKIP THIS COMPONENT
C    OF THE TEST.
C
      IF (ISR4SPV(TSTLIM)) THEN
         CALL N4AVH$(101,STATUS)
         ERSTAT = 10
         IF (STATUS.EQ.99) ERSTAT = 99
         RETURN
      ENDIF
C
C    DETERMINE THE PERCENTAGE OF AVHRR CHANNEL 4 VALUES >= N4AVHL (K).
C    IF THIS PERCENTAGE IS LESS THAN THE TEST LIMIT, THE HIRS SPOT
C    IS CLOUDY.
C
      NAVHR4 = 0
      TAVHR4 = 0
      DO 10 AVRPIX = 108,124
         IF (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,AVRPIX))) THEN
            TAVHR4 = TAVHR4 + 1
            IF (HIRDAT(SPOT,LINE,AVRPIX) .GE. N4AVHL) THEN
               NAVHR4 = NAVHR4 + 1
            ENDIF
         ENDIF
   10 CONTINUE
C
      IF (TAVHR4 .NE. 0) THEN
         PCNTA4 = (REAL(NAVHR4)/REAL(TAVHR4)) * 100.0
C
C  DO NOT FAIL SPOT UNLESS TERRAIN HEIGHT LESS THAN LIMIIT
C
         IF (PCNTA4 .LT. TSTLIM .AND. THIGHT .LT. THLIMIT) THEN
            RESULT = 0
         ELSE IF (THIGHT .LT. THLIMIT) THEN
            RESULT = 1
         ENDIF
      ENDIF
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE N4AVH$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- N4AVH$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 2/09/93    PROGRAMMER- CARLA NEIGH (SMSRC)
C   VERSION- 3.0    DATE- 9/22/97    PROGRAMMER- LEAH WILSON (HSTX)
C
C   FUNCTION- THIS SUBROUTINE WRITES OUT ERROR MESSAGES TO THE
C             KIDS MESSAGE LOG FILE FOR THE SUBROUTINE N4AVHR.
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  MAIN 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.
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
C
      DATA COUNT /0/
      SAVE COUNT
C
C   SET THE SUBROUTINE NAME.
C
      SUBNAM = 'SPGDVR'
      STAT = 0
C
      COUNT = COUNT + 1
C
      IF (COUNT .LE. 1) THEN
C
C     CHECK FOR VALID MESSAGE CODES.
C
        IF (MSGCOD .EQ. 101) THEN
        KMSG(1:51)='* WARNING * AVHRR test component 4 check for % of '
        KMSG(52:100)='pixels >= 268.16 cannot be performed since the t'
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
           KMSG='est limit is a missing value. '
           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
      ENDIF
      RETURN
      END

