      SUBROUTINE N6AVHR(SPOT,LINE,TSTLIM,RGCOEF,RESULT,ERSTAT)
      IMPLICIT NONE
C*********************************************************************
C
C   NAME- N6AVHR
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/18/98   PROGRAMMER- LEAH WILSON,RAYTHEON
C   VERSION- 3.3    DATE- 15/01/00   PROGRAMMER- M.P.FERGUSON,(RITSS)
C   VERSION- 3.4    DATE- 06/07/00  PROGRAMMER- M.P.FERGUSON,(RITSS)
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C
C   FUNCTION- THIS SUBROUTINE PERFORMS THE SIXTH COMPONENT OF THE NEW
C             AVHRR TESTS ON A HIRS SPOT TO DETERMINE IF CLOUDS ARE
C             PRESENT.  THIS TEST COMPONENT PREDICTS THE VALUES FOR
C             EACH OF THE COLLOCATED AVHRR CHANNEL 4 PIXELS FROM THE
C             COLLOCATED AVHRR CHANNEL 5 PIXELS. IF THE DIFFERENCE
C             BETWEEN THE PREDICTED VALUES AND THE ACTUAL VALUES OF
C             AVHRR CHANNEL 4 ARE NOT WITHIN THE TEST LIMIT FOR ANY OF
C             THE AVHRR PIXELS, THE HIRS SPOT IS MARKED CLOUDY.
C   VERSION- 3.1 RTOVS UPGRADES.  SINCE /INSCOM/ WAS ADDED, REMOVE
C                NSPOTS, NLINES, NDATEL, AND HIRDAT FROM SUBR. CALL.
C                CHANGE AVHRR CH. 5 AVERAGE IN HIRDAT BUFFER FROM
C                135 IN RTOVS TO 130 IN ATOVS.
C   VERSION- 3.2 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- 3.3 CORRECT CALCULATION FOR PREDICTED AVHRR 4 SO THAT IT IS 
C                CALCULATED FROM AVERAGE VALUE OF AVHRR 5, NOT AVHRR 4.
C   VERSION- 3.4 CHANGE THIS ROUTINE TO HANDLE MAX AND MIN THRESHOLDS.
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                           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 DIFFERENCE BETWEEN ACTUAL
C                           AND PREDICTED VALUES OF AVHRR CHANNEL 4.
C     RGCOEF    REAL   I    ARRAY (DIM 2) CONTAINING THE REGRESSION
C                           COEFFICIENTS USED TO PREDICT AVHRR CHANNEL
C                           4 FROM AVHRR CHANNEL 5.
C     RESULT    INT   I/O   RESULT OF THE SIXTH COMPONENT OF THE NEW
C                           AVHRR TEST.  A VALUE OF 0 MEANS THE SPOT
C                           WAS DETERMINED TO BE CLOUDY FROM THE TEST
C                           COMPONENT, 1 MEANS THE SPOT WAS NOT
C                           DETERMINED TO BE CLOUDY, 2 MEANS THAT A
C                           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     AVHR4P    REAL     AVHRR CHANNEL 4 PREDICTED VALUE.
C     AVRPIX    INT      DO LOOP COUNTER FOR AVHRR PIXELS.
C     STATUS    INT      STATUS CODE RETURNED FROM N6AVH$.
C
C   SUBPROGRAMS CALLED- N6AVH$
C
C   FUNCTIONS USED- ABS
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 N6AVH$.
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(2), RGCOEF(2), VAL
C
      INTEGER AVRPIX, STATUS, ICH4WRD
      REAL AVHR4P, THIGHT, AVH4AV
      LOGICAL   ISR4SPV
C
      INCLUDE 'SPVAL'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
C
      ERSTAT = 0
C
C   IF THE TEST LIMIT OR ANY OF THE REGRESSION COEFFICIENTS
C   IS A MISSING VALUE, WRITE A WARNING MESSAGE TO THE
C   KIDS MESSAGE LOG FILE AND SKIP THIS COMPONENT
C   OF THE TEST.
C
      IF ((ISR4SPV(TSTLIM(1))) .OR. (ISR4SPV(TSTLIM(2)))
     1 .OR. (ISR4SPV(RGCOEF(1))) .OR.(ISR4SPV(RGCOEF(2)))) THEN
         CALL N6AVH$(101,STATUS)
         ERSTAT = 10
         IF (STATUS.EQ.99) ERSTAT = 99
         RETURN
      ENDIF
C
C   COMPUTE THE PREDICTED VALUES OF AVHRR CHANNEL 4 AND CHECK AGAINST
C   THE ACTUAL VALUES.  IF THE DIFFERENCE BETWEEN THE PREDICTED AND
C   ACTUAL VALUES IS GREATER THAN THE TEST LIMIT, THE HIRS SPOT IS
C   CLOUDY.  DO NOT DO TEST OVER HIGH TERRAIN.
C
      THIGHT = HIRANC(SPOT,LINE,8)
      IF (HIRANC(SPOT,LINE,8) .GT. 1500)GO TO 20
      ICH4WRD = 108
      DO 10 AVRPIX = 126,142
         IF (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,AVRPIX))) THEN
            AVHR4P = RGCOEF(1) + RGCOEF(2)*HIRDAT(SPOT,LINE,AVRPIX)
            IF (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,ICH4WRD))) THEN
               VAL = AVHR4P - HIRDAT(SPOT,LINE,ICH4WRD)
               IF (VAL .GT. TSTLIM(1) .OR. VAL .LT. TSTLIM(2))THEN
                  RESULT = 0
                  GOTO 20
               ELSE
                  RESULT = 1
               ENDIF
            ENDIF
         ENDIF
         ICH4WRD = ICH4WRD + 1
   10 CONTINUE
C
   20 IF (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,143))) THEN
C
C  COMPUTE AVHRR-4 PREDICTED BASED ON AVERAGE OF CH. 5
C
         AVHR4P = RGCOEF(1) + RGCOEF(2) * HIRDAT(SPOT,LINE,143)
      ELSE
         AVHR4P = R4SPV
      ENDIF
      PAVHR4(SPOT,LINE) = AVHR4P
      AVH4AV =  HIRDAT(SPOT,LINE,125) 
      RETURN
      END
C**********************************************************************
      SUBROUTINE N6AVH$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- N6AVH$
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 N6AVHR.
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 6 check of predic'
        KMSG(52:100)='ted ch. 4 cannot be done since 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
      ENDIF
      RETURN
      END

