      SUBROUTINE N2AVHR(SPOT,LINE,TSTLIM,RGCOEF,RESULT,ERSTAT)
      IMPLICIT NONE 
C*********************************************************************
C
C   NAME- N2AVHR
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- 05/01/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 SECOND COMPONENT OF THE NEW
C             AVHRR TESTS ON A HIRS SPOT TO DETERMINE IF CLOUDS ARE
C             PRESENT.  THIS TEST COMPONENT PREDICTS THE COLLOCATED
C             AVHRR CHANNEL 3 PIXELS FROM THE COLLOCATED AVHRR CHANNEL
C             4 PIXELS. IF THE DIFFERENCE IS GREATER THAN OR EQUAL
C             TO THE TEST LIMIT FOR ANY OF THE 17 COLLOCATED AVHRR
C             PIXELS, THE HIRS SPOT IS MARKED CLOUDY.
C   VERSION- 3.1 COMMON REGCOE ADDED TO ESCORT THE PREDICTED AVHRR 3
C             VALUE CALCULATED FROM AVHRR 4 MEAN (CALCULATED FROM ALL 17
C             PIXELS) VALUE.  (RTOVS UPGRADE)  ALSO CONVERT N2AVH$ TO USE
C             KIDS FILE.  CHANGE AVHRR CH.4 AVERAGE LOCATION IN HIRDAT
C             BUFFER FROM 117 IN RTOVS TO 112 IN ATOVS.  REMOVE HIRDAT
C             AND ITS DIMENSIONS FROM SUBROUTINE CALL SINCE THEY ARE
C             DEFINED IN /INSCOM/.
C   VERSION- 3.2 CORRECT GAC INDICES IN HIRDAT BUFFER, AND RESTRUCTURE
C            DO 10 LOOP TO BE MORE EFFICIENT WITH INDICES.  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 OF PREDICTED AVHRR 3 SO THAT IT IS
C            CALCULATED FROM AVERAGE VALUE OF AVHRR 4 AND NOT AVHRR 3,
C            FOR WR # 75.
C   VERSION- 4.0 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 THE DIFFERENCE BETWEEN THE
C                           PREDICTED AND ACTUAL VALUES OF AVHRR
C                           CHANNEL 3.
C     RGCOEF    REAL   I    REGRESSION COEFFICIENTS USED TO PREDICT
C                           AVHRR CHANNEL 3 VALUES FROM AVHRR CHANNEL
C                           4.
C     RESULT    INT   I/O   RESULT OF THE SECOND 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     AVHR3P    REAL     PREDICTED AVHRR CHANNEL 3 VALUE.
C     AVRPIX    INT      DO LOOP COUNTER FOR AVHRR PIXELS.
C     STATUS    INT      STATUS CODE RETURNED FROM N2AVH$.
C
C   SUBPROGRAMS CALLED- N2AVH$
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 N2AVH$.
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, ICH3WRD
      REAL TSTLIM, RGCOEF(2)
C
      INTEGER AVRPIX, STATUS
      REAL AVHR3P
      LOGICAL ISR4SPV
C
      INCLUDE 'SPVAL'
      INCLUDE 'REGCOE'
      INCLUDE 'NEWTHR'
      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)) .OR. (ISR4SPV(RGCOEF(1))) .OR.
     1    (ISR4SPV(RGCOEF(2)))) THEN
         ERSTAT = 10
         CALL N2AVH$(101,STATUS)
         IF (STATUS.EQ.99) ERSTAT = 99
         RETURN
      ENDIF
C
C   COMPUTE THE PREDICTED VALUE OF AVHRR CHANNEL 3 FOR EACH OF THE
C   COLLOCATED AVHRR PIXELS (FROM CH. 4) AND CHECK THE PREDICTED VALUE 
C   VS. THE ACTUAL VALUE.  IF THE DIFFERENCE BETWEEN THE PREDICTED AND
C   ACTUAL VALUES IS GREATER THAN THE TEST LIMIT FOR ANY AVHRR PIXEL,
C   THE HIRS SPOT IS CLOUDY.
C
      ICH3WRD = 90
      DO 10 AVRPIX = 108,124
         IF (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,AVRPIX))) THEN
            AVHR3P = RGCOEF(1) +
     1               RGCOEF(2) * HIRDAT(SPOT,LINE,AVRPIX)
            PAVHR3(SPOT,LINE) = AVHR3P
            IF (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,ICH3WRD))) THEN
               IF (((HIRDAT(SPOT,LINE,ICH3WRD) - AVHR3P) .GT.
     1                 TSTLIM).OR.((HIRDAT(SPOT,LINE,ICH3WRD)-AVHR3P)
     2                 .LT. NATLM2B)) THEN
                  RESULT = 0
                  GOTO 20
               ELSE
                  RESULT = 1
               ENDIF
            ENDIF
         ENDIF
         ICH3WRD = ICH3WRD + 1
   10 CONTINUE
C
   20 IF (.NOT. ISR4SPV(HIRDAT(SPOT,LINE,125))) THEN
C
C  SET CH 3 PREDICTED BASED ON AVERAGE OF 17 CH. 4 SPOT
C
         AVHR3P = RGCOEF(1) + RGCOEF(2) * HIRDAT(SPOT,LINE,125)
      ELSE
         AVHR3P = R4SPV
      ENDIF
      PAVHR3(SPOT,LINE) = AVHR3P
      RETURN
      END
C**********************************************************************
      SUBROUTINE N2AVH$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- N2AVH$
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 N2AVHR.
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 2 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

