      SUBROUTINE N1AVHR(SPOT,LINE,HILIM,LOLIM,RESULT,ERSTAT)
C*********************************************************************
C
C   NAME- N1AVHR
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   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- 11/19/97   PROGRAMMER- LEAH WILSON (HSTX)
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 FIRST COMPONENT OF THE NEW
C             AVHRR TESTS ON A HIRS SPOT TO DETERMINE IF CLOUDS ARE
C             PRESENT.  THIS COMPONENT CHECKS THE MAXIMUM DIFFERENCE
C             BETWEEN THE AVHRR CHANNEL 4 AND 3 VALUES AGAINST TWO
C             TEST LIMITS.  IF THE MAXIMUM DIFFERENCE IS LESS THAN THE
C             LOWER TEST LIMIT OR GREATER THAN THE HIGHER TEST LIMIT,
C             THE HIRS SPOT IS MARKED CLOUDY.
C   VERSION- 3.1 COMMON BUFFER INCLUDED TO SUPPLY ARGUMENT HEIGHT FOR
C            USE IN DETERMINING APPLICABILITY OF TEST.  USE COMMON
C            CLDDAT TO SUPPLY ARGUMENT ICEFRE FOR USE IN DETERMINING
C            APPLICABILITY OF TEST.  BOTH ARE RTOVS UPGRADES. ALSO,
C            CONVERT N1AVH$ TO KIDS CONVENTION.  SINCE INSPARM IS INCLUDED,
C            REMOVE NSPOTS,NLINES,NDATEL, AND HIRDAT FROM SUBROUTINE CALL.
C   VERSION- 3.2 REMOVE ICEFRE REQUIREMENT.
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     HILIM     REAL   I    HIGHER TEST LIMIT FOR MAXIMUM DIFFERENCE
C                           BETWEEN AVHRR CHANNELS 4 AND 3.
C     LOLIM     REAL   I    LOWER TEST LIMIT FOR MAXIMUM DIFFERENCE
C                           BETWEEN AVHRR CHANNELS 4 AND 3.
C     RESULT    INT   I/O   RESULT OF THE FIRST 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 THE AVHRR CH 4 PIXELS.
C     ICH3WRD   INT      COUNTER FOR AVHRR CH. 3 PIXELS.
C     DIFRNC    REAL     DIFFERENCE BETWEEN AVHRR CHANNELS 4 AND
C                        3 VALUES FOR A PIXEL.
C     MAXDIF    REAL     MAXIMUM DIFFERENCE BETWEEN AVHRR CHANNELS
C                        4 AND 3.
C     STATUS    INT      STATUS CODE RETURNED FROM N1AVH$.
C
C   SUBPROGRAMS CALLED- N1AVH$
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 N1AVH$.
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 LOLIM, HILIM
C
      INTEGER AVRPIX, STATUS, ICH3WRD
      REAL DIFRNC, MAXDIF, THIGHT, MINDIF
      LOGICAL   ISR4SPV
C
      INCLUDE 'SPVAL'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'CLDDAT'
C
      ERSTAT = 0
C
C   IF A 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(LOLIM)) .OR. (ISR4SPV(HILIM))) THEN
         CALL N1AVH$(101,STATUS)
         ERSTAT = 10
         IF (STATUS.EQ.99) ERSTAT = 99
         RETURN
      ENDIF
C
C   CHECK THE DIFFERENCE BETWEEN AVHRR CHANNELS 4 AND 3 FOR EACH OF THE
C   COLLOCATED AVHRR PIXELS TO FIND THE MAXIMUM.  IF THE MAXIMUM
C   DIFFERENCE IS OUTSIDE THE TEST LIMIT BOUNDS, THE HIRS SPOT IS MARKED
C
      MAXDIF = -9999.0
      MINDIF = 9999.0
C
cclw  DO 10 AVRPIX = 1, 17
cclw     IF ((HIRDAT(SPOT,LINE,AVRPIX+112) .NE. R4SPV) .AND.
cclw 1       (HIRDAT(SPOT,LINE,AVRPIX+94) .NE. R4SPV)) THEN
cclw        DIFRNC = HIRDAT(SPOT,LINE,AVRPIX+112) -
cclw 1                     HIRDAT(SPOT,LINE,AVRPIX+94)
      ICH3WRD = 90
      DO 10 AVRPIX = 108,124
         IF ((.NOT. ISR4SPV(HIRDAT(SPOT,LINE,AVRPIX))) .AND.
     1       (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,ICH3WRD)))) THEN
            DIFRNC = HIRDAT(SPOT,LINE,AVRPIX) -
     1                     HIRDAT(SPOT,LINE,ICH3WRD)
            IF (DIFRNC .GT. MAXDIF) MAXDIF = DIFRNC
            IF (DIFRNC .LT. MINDIF) MINDIF = DIFRNC
         ENDIF
         ICH3WRD = ICH3WRD + 1
   10 CONTINUE
C
C
C   GET TERRAIN HEIGHT FROM ANCILLARY BUFFER.
C
      THIGHT = HIRANC(SPOT, LINE, 8)
      IF ((MAXDIF .NE. -9999.0) .AND.(THIGHT .LT. 500.) .AND.
     1    (MINDIF .NE. 9999.))THEN
        IF ((MAXDIF .GT. HILIM) .OR. (MINDIF .LT. LOLIM)) THEN
           RESULT = 0
        ELSE
           RESULT = 1
        ENDIF
      ENDIF
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE N1AVH$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- N1AVH$
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 N1AVHR.
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 1 check for diffe'
        KMSG(52:100)='rence btw ch.4 & 3 values cannot be performed.  '
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
           KMSG='The test 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

