      SUBROUTINE NEWAVR(NSPOTS,NLINES,NDATEL,HIRDAT,SPOT,LINE,NIGHT,
     1                  SNWFRE,ICEFRE,OVRLND,PRCOMP,RESULT,ERSTAT)
C*********************************************************************
C
C   NAME- NEWAVR
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- 3.0    DATE- 05/29/96   PROGRAMMER- LEAH WILSON (HSTX)
C      SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1    DATE- 09/18/97   PROGRAMMER- LEAH WILSON (HSTX)
C      RTOVS UPGRADE TO MODIFY N1AVHR CALL TO ALLOW USE OF HEIGHT IN
C      CALCULATION OF TEST VALUE.  REMOVE HIRDAT AND ASSOCIATED
C      PARAMETERS FROM CALLS TO N1AVHR, N2AVHR, N5AVHR AND N6AVHR.
C   VERSION- 3.2    DATE- 11/19/97   PROGRAMMER- LEAH WILSON (HSTX)
C      CALL N6AVHR IN CASE OF SNOW OR ICE, WITH A MODIFIED TEST
C      LIMIT.
C   VERSION- 3.3    DATE- 12/10/98   PROGRAMMER- MP FERGUSON (RITSS)
C      DO NOT CALL N1AVHR IN THE CASE OF PRESENCE OF ICE.
C   VERSION- 3.4    DATE- 05/01/00   PROGRAMMER- MP FERGUSON (RITSS).
C      REMOVE USE OF AVAUSN IN DETERMINING EXECUTION OF AVHRR 3B TESTS,
C      IT IS ERRONEOUSLY CAUSING DISUSE OF THESE TESTS IN THE FIRST 4
C      LINES OF EACH FRAME.
C   VERSION- 3.5    DATE- 06/05/00   PROGRAMMER- MP FERGUSON (RITSS)
C      ADD USE OF 4 DIFFERENT LIMITS FOR CALL TO SUBROUTINE N6AVHR,
C      DEPENDING ON TERRAIN TYPE (SEA,ICE,LAND/COAST, OR SNOW).
C   VERSION- 1.2    DATE- 02/08/02   PROGRAMMER- R ROLLINS (CSC)
C      PORTED TO IBM SP DEFINED PRCOMP A L*4
C
C   FUNCTION- THIS SUBROUTINE PERFORMS THE NEW AVHRR TESTS ON A
C             HIRS SPOT TO DETERMINE IF CLOUDS ARE PRESENT.
C
C   CALLING SEQUENCE PARAMETERS-
C
C     NAME      TYPE  I/O   DESCRIPTION
C     ----      ----  ---   -----------
C     NSPOTS    INT    I    TOTAL NUMBER OF SPOTS IN A HIRS SCAN LINE.
C     NLINES    INT    I    TOTAL NUMBER OF SCAN LINES IN THE HIRS DATA
C                           BUFFER.
C     NDATEL    INT    I    TOTAL NUMBER OF DATA ELEMENTS IN THE HIRS
C                           DATA BUFFER.
C     HIRDAT    REAL   I    3D ARRAY (NSPOTS X NLINES X NDATEL)
C                           CONTAINING THE HIRS DATA BUFFER FOR AN
C                           EXTENDED DATA FRAME.
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     NIGHT   LOGICAL  I    LOGICAL FLAG INDICATING WHETHER HIRS SPOT
C                           IS IN NIGHTTIME (TRUE = NIGHT, FALSE = DAY).
C     SNWFRE  LOGICAL  I    LOGICAL FLAG INDICATING WHETHER HIRS SPOT
C                           IS SNOW-FREE (TRUE = SNOW-FREE, FALSE =
C                           SNOW-COVERED).
C     ICEFRE  LOGICAL  I    LOGICAL FLAG INDICATING WHETHER HIRS SPOT
C                           IS ICE-FREE (TRUE = ICE-FREE, FALSE = ICE-
C                           COVERED).
C     OVRLND  LOGICAL  I    LOGICAL FLAG INDICATING WHETHER HIRS SPOT
C                           IS OVER LAND OR WATER (TRUE = OVER LAND,
C                           FALSE = OVER WATER).
C     PRCOMP  LOGICAL  I    LOGICAL ARRAY (DIM 6) CONTAINING THE
C                           PROCESSING FLAGS FOR THE COMPONENTS OF THE
C                           NEW AVHRR TEST.  VALUE OF TRUE INDICATES
C                           THAT THE TEST COMPONENT SHOULD BE EXECUTED,
C                           FALSE MEANS THAT THE TEST COMPONENT SHOULD
C                           BE SKIPPED.
C     RESULT    INT   I/O   ARRAY (DIM 6) CONTAINING THE RESULTS OF EACH
C                           OF THE COMPONENTS OF THE NEW AVHRR TEST.
C                           A VALUE OF 0 MEANS THE SPOT WAS DETERMINED
C                           TO BE CLOUDY FROM THE TEST COMPONENT, 1
C                           MEANS THE SPOT WAS NOT DETERMINED TO BE
C                           CLOUDY, 2 MEANS THAT A DETERMINATION COULD
C                           NOT BE MADE.  IT IS ASSUMED THAT ALL
C                           ELEMENTS ARE INITIALIZED TO 2 BEFORE THIS
C                           ROUTINE IS CALLED.
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     STATUS    INT      STATUS CODE RETURNED FROM A CALLED SUBROUTINE.
C
C   SUBPROGRAMS CALLED- N1AVHR, N2AVHR, N4AVHR, N5AVHR, N6AVHR
C
C
C   EXIT STATES-
C     ERSTAT = 10 - A COMPONENT OF THE NEW AVHRR TEST COULD NOT BE
C                   EXECUTED.
C     ERSTAT = 99 - A FATAL ERROR WAS RETURNED FROM A CALLED SUBROUTINE.
C
C   COMMENTS-
C
C    IT IS ASSUMED THAT THE ELEMENTS OF THE RESULT ARRAY HAVE BEEN
C    INITIALIZED TO 2 BEFORE THIS 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 RESULT(6), NSPOTS, NLINES, NDATEL, SPOT, LINE, ERSTAT
      REAL HIRDAT(NSPOTS,NLINES,NDATEL)
C     LOGICAL NIGHT, SNWFRE, ICEFRE, OVRLND, PRCOMP(6)
      LOGICAL NIGHT, SNWFRE, ICEFRE, OVRLND
      LOGICAL*4 PRCOMP(6)
C
      INTEGER STATUS
C
      INCLUDE 'REGCOE'
      INCLUDE 'SPVAL'
      INCLUDE 'AVRCON'
C
      ERSTAT = 0
C
C   THERE ARE SIX COMPONENTS OF THE NEW AVHRR TEST.  THE FIRST
C   TWO COMPONENTS ARE EXECUTED ONLY FOR SPOTS IN NIGHTTIME.
C
      IF (NIGHT) THEN
C
C   CHECK TO SEE IF COMPONENT 1 IS TO BE EXECUTED.
C
CCMPF   IF(.NOT.AVAUSN(LINE))THEN
CCMPF      IF((LINE.GE.2).AND..NOT.AVAUSN(LINE-1))THEN
CCMPF         IF((LINE.LE.49).AND..NOT.AVAUSN(LINE+1))THEN
CCMPF            CONTINUE
CCMPF         ELSE
CCMPF            GOTO 909
CCMPF         ENDIF
CCMPF      ELSE
CCMPF         GOTO 909
CCMPF      ENDIF
CCMPF   ELSE
CCMPF      GOTO 909
CCMPF   ENDIF
        IF (PRCOMP(1).AND. ICEFRE) THEN
           CALL N1AVHR(SPOT,LINE,NATL1A,NATL1B,RESULT(1),STATUS)
           IF (STATUS .NE. 0) THEN
              ERSTAT = STATUS
              IF (ERSTAT .EQ. 99) RETURN
           ENDIF
        ENDIF
C
C   CHECK TO SEE IF COMPONENT 2 IS TO BE EXECUTED.  COMPONENT 2 IS
C   NOT EXECUTED OVER AREAS OF SNOW OR ICE.
C
        IF (PRCOMP(2)) THEN
           IF (SNWFRE .AND. ICEFRE) THEN
              CALL N2AVHR(SPOT,LINE,NATLM2,COEFK,RESULT(2),STATUS)
              IF (STATUS .NE. 0) THEN
                 ERSTAT = STATUS
                 IF (ERSTAT .EQ. 99) RETURN
              ENDIF
           ENDIF
        ENDIF
C
      ENDIF
909   CONTINUE
C
C   THE THIRD COMPONENT HAS BEEN ELIMINATED.  FOR NOW THIS SPOT IS
C   RESERVED FOR ANY FUTURE AVHRR TESTS.
C
C
C   THE REMAINING TEST COMPONENTS ARE EXECUTED IN DAYTIME OR NIGHTTIME.
C
C
C   CHECK TO SEE IF COMPONENT 4 IS TO BE EXECUTED.  COMPONENT 4 IS ONLY
C   EXECUTED OVER ICE-FREE WATER.
C
      IF (PRCOMP(4)) THEN
         IF (.NOT. (OVRLND) .AND. ICEFRE) THEN
            CALL N4AVHR(SPOT,LINE,NATLM4,RESULT(4),STATUS)
            IF (STATUS .NE. 0) THEN
               ERSTAT = STATUS
               IF (ERSTAT .EQ. 99) RETURN
            ENDIF
         ENDIF
      ENDIF
C
C   CHECK TO SEE IF COMPONENT 5 IS TO BE EXECUTED.  COMPONENT 5 IS NOT
C   EXECUTED IN THE DAY OVER AREAS OF SNOW OR ICE.
C
      IF (PRCOMP(5)) THEN
         IF (NIGHT .OR. (.NOT.(NIGHT) .AND. SNWFRE .AND. ICEFRE)) THEN
            CALL N5AVHR(SPOT,LINE,NATLM5,RESULT(5),STATUS)
            IF (STATUS .NE. 0) THEN
               ERSTAT = STATUS
               IF (ERSTAT .EQ. 99) RETURN
            ENDIF
         ENDIF
      ENDIF
C
C   CHECK TO SEE IF COMPONENT 6 IS TO BE EXECUTED.  COMPONENT 6 IS
C   NOT EXECUTED OVER SNOW OR ICE.
C
      IF (PRCOMP(6)) THEN
         IF (.NOT. OVRLND .AND. ICEFRE) THEN
            CALL N6AVHR(SPOT,LINE,NATLM6,COEFM,RESULT(6),STATUS)
            IF (STATUS .NE. 0) THEN
               ERSTAT = STATUS
               IF (ERSTAT .EQ. 99) RETURN
            ENDIF
         ELSE IF (.NOT. ICEFRE) THEN
            CALL N6AVHR(SPOT,LINE,NATL6B,COEFM,RESULT(6),STATUS)
            IF (STATUS .NE. 0) THEN
               ERSTAT = STATUS
               IF (ERSTAT .EQ. 99) RETURN
            ENDIF
         ELSE IF (SNWFRE .AND. ICEFRE .AND. OVRLND) THEN
            CALL N6AVHR(SPOT,LINE,NATL6C,COEFM,RESULT(6),STATUS)
            IF (STATUS .NE. 0) THEN
               ERSTAT = STATUS
               IF (ERSTAT .EQ. 99) RETURN
            ENDIF
         ELSE IF (.NOT. SNWFRE) THEN
            CALL N6AVHR(SPOT,LINE,NATL6D,COEFM,RESULT(6),STATUS)
            IF (STATUS .NE. 0) THEN
               ERSTAT = STATUS
               IF (ERSTAT .EQ. 99) RETURN
            ENDIF
         ENDIF
      ENDIF
C
      RETURN
      END
