      SUBROUTINE NSDVR(CLOUDY,CNFLIM,ERSTAT)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME- NSDVR
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 4/15/93    PROGRAMMER- CARLA A. NEIGH (SMSRC)
C   VERSION- 2.0    DATE- 05/06/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C                   XATOV-3 REPLACE COMMON BLOCK /BUFFER/ WITH
C                   PARAMETER FILE INSPARM AND COMMON BLOCK /INSCOM/.
C   VERSION- 2.1    DATE- 08/30/95   PROGRAMMER- ALEX POZDNYAK(SMSRC)
C                   ATOVS-168 THE N* FLAG WAS ADDED AS AN ARGUMENT IN
C                   THE CLRHIR CALL AND THE VALUE NSTAR WAS REMOVED.
C                   IN PLACE OF THE LINE: 
C                   HIRANC(SPOT,LINE,17)=NSTAR
C                   PUT: 
C                   HIRANC(SPOT,LINE,24)=NFLAG  
C                   THE LINE: HIRANC(SPOT,LINE,17)=-1.0 WAS REMOVED. 
C   VERSION- 3.0    DATE- 05/07/96   PROGRAMMER- LEAH WILSON (HSTX)
C                   MODIFICATIONS FOR ATOVS BASELINE ON CRAY.
C
C   FUNCTION-  THIS SUBROUTINE IS THE DRIVER FOR PERFORMING CLOUD-
C              CLEARING ON A DATA FRAME USING THE NSTAR METHOD.  CLOUD-
C              CLEARING IS ATTEMPTED ON ALL HIRS SPOTS IN THE DATA FRAME
C              THAT WERE FOUND TO BE CLOUDY BY THE CLOUD TESTS.  IF
C              CLOUD-CLEARING IS SUCCESSFULLY PERFORMED, CLEARED VALUES
C              ARE PLACED IN THE HIRS DATA BUFFER.
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME    TYPE   I/O   DESCRIPTION
C   ----    ----   ---   -----------
C   CLOUDY  LOGICAL I    ARRAY (DIM INSPARM:HNSPOT X INSPARM:HTOTLN)
C                        CONTAINING LOGICAL FLAGS INDICATING WHETHER A
C                        SPOT IS CLOUDY (TRUE = CLOUDY, FALSE = NOT
C                        CLOUDY).
C   CNFLIM   INT    I    SPOT CONFIDENCE LIMIT.  SPOTS WITH A SPOT
C                        CONFIDENCE LESS THAN THIS LIMIT SHOULD NOT
C                        BE CLOUD-CLEARED.
C   ERSTAT   INT    O    ERROR RETURN CODE-
C                         = 0 IF NO ERROR OCCURRED,
C                         = 99 IF FATAL ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS- BUFFER, FRMINF, MONSTA
C
C   LOCAL PARAMETERS-
C   NAME    TYPE   DESCRIPTION
C   ----    ----   -----------
C   CLEAR  LOGICAL LOGICAL FLAG INDICATING WHETHER CLOUD-CLEARING WAS
C                  SUCCESSFULLY PERFORMED ON A SPOT (TRUE = SUCCESSFUL,
C                  FALSE = NOT SUCCESSFUL)
C   CLRVAL   REAL  ARRAY (DIM HNCHAN-1) CONTAINING CLOUD-CLEARED VALUES
C                  FOR A HIRS SPOT.
C   CURPTR   INT   POINTER TO THE SCAN LINE IN THE RING BUFFER THAT
C                  IS AT THE END OF THE RING.
C   ELEM     INT   DO LOOP COUNTER FOR HIRS DATA ELEMENTS.
C   LINE     INT   DO LOOP COUNTER FOR SCAN LINES.
C   LINPTR   INT   ARRAY (DIM CLPARM:NBRLIN) CONTAINING THE LINE
C                  NUMBERS IN THE HIRS DATA BUFFER FOR EACH OF THE
C                  SCAN LINES IN THE RING BUFFER.
C   NSTAR    REAL  THE COMPUTED NSTAR VALUE USED IN CLOUD-CLEARING.
C   RNGBUF   REAL  3D ARRAY (DIM INSPARM:HNSPOT X CLPARM:NBRLIN X
C                  INPARM:HNCHAN-1) CONTAINING THE RING BUFFER USED TO
C                  HOLD CLOUD-CLEARED DATA UNTIL IT MAY BE WRITTEN TO
C                  THE HIRS DATA BUFFER.
C   SPOT     INT   DO LOOP COUNTER FOR SPOTS.
C   STPTR    INT   POINTER TO THE SCAN LINE IN THE RING BUFFER THAT IS
C                  AT THE START OF THE RING.
C   NSTAR    INT   N * FLAG. 
C   STATUS   INT   ERROR STATUS RETURNED FROM CLRHIR.
C
C   SUBPROGRAMS CALLED- CLRHIR
C
C   EXIT STATES-
C     ERSTAT = 99 : FATAL ERROR WAS RETURNED FROM CLRHIR.
C
C   COMMENTS-
C     THE ARRAY RNGBUF IS A RING BUFFER USED TO HOLD HIRS VALUES THAT
C     HAVE BEEN CLOUD-CLEARED.  WHEN CLOUD-CLEARING IS PERFORMED ON
C     A SPOT A CLDPRM:NBRSPT SPOT BY CLDPRM:NBRLIN SCAN LINE REGION
C     OF DATA WHICH HAS NOT BEEN CLOUD-CLEARED IS NEEDED TO CHOOSE A
C     NEIGHBORING SPOT TO USE IN THE NSTAR CALCULATION.  THUS THE CLOUD-
C     CLEARED DATA IS WRITTEN TO THE RING BUFFER TO PRESERVE THE
C     DATA IN THE HIRS DATA BUFFER FOR THIS REGION.  CLOUD-CLEARED
C     DATA FOR A HIRS SCAN LINE IS WRITTEN TO THE HIRS DATA BUFFER
C     ONLY AFTER THE (CLDPRM:NBRLIN - 1) SCAN LINES FOLLOWING THE
C     SCAN LINE HAVE BEEN PROCESSED.  THE RING BUFFER ONLY CONTAINS
C     VALUES FOR HIRS DATA ELEMENTS 1 - (HNCHAN - 1) BECAUSE THESE
C     ARE THE ONLY CHANNELS THAT MAY BE CLOUD-CLEARED.
C
C***********************************************************************
C
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'FRMINF'
      INCLUDE 'CLDPRM'
      INCLUDE 'INPARM'
      INCLUDE 'MONSTA'
C
      LOGICAL CLOUDY(HNSPOT,HTOTLN)
      INTEGER ERSTAT, CNFLIM
C
      INTEGER STPTR, CURPTR, SPOT, LINE, ELEM, STATUS, LINPTR(NBRLIN)
      INTEGER NFLAG
      REAL RNGBUF(HNSPOT,NBRLIN,HNCHAN-1), CLRVAL(HNCHAN-1)
      LOGICAL CLEAR
C
      ERSTAT = 0
C
C   INITIALIZE THE RING BUFFER POINTERS.
C
      STPTR = 1
      CURPTR = 1
C
C    LOOP OVER LINES IN THE HIRS DATA BUFFER TO PERFORM CLOUD-CLEARING.
C
      DO 100 LINE = 1, HTOTLN
C
C      CHECK FOR A USABLE SCAN LINE.  DO NOT CLOUD-CLEAR IF THE SCAN
C      LINE IS NOT USABLE.
C
        IF (.NOT. HGOODL(LINE)) THEN
          NOTEST(FRAMNO) = NOTEST(FRAMNO) + HNSPOT
          GOTO 100
        ENDIF
C
C      PUT THE SCAN LINE FROM THE HIRS DATA BUFFER INTO THE RING
C      BUFFER.
C
        DO 20 SPOT = 1, HNSPOT
          DO 10 ELEM = 1, HNCHAN - 1
             RNGBUF(SPOT,CURPTR,ELEM) = HIRDAT(SPOT,LINE,ELEM)
   10     CONTINUE
   20   CONTINUE
C
        LINPTR(CURPTR) = LINE
C
C      PROCESS THE SPOTS IN THE SCAN LINE.
C
        DO 50 SPOT = 1, HNSPOT
C
C        SKIP OVER THE SPOT IF THE SPOT CONFIDENCE IS BELOW THE LIMIT.
C
          IF (HIRANC(SPOT,LINE,6) .LT. CNFLIM) THEN
            NOTEST(FRAMNO) = NOTEST(FRAMNO) + 1
            GOTO 50
          ENDIF
C
C        IF THE SPOT IS CLOUDY, ATTEMPT CLOUD-CLEARING.
C
cclw - Since it is commented out in RTOVS, Cloud-clearing is commented out
cclw - in ATOVS.  Note that uncommenting this for ATOVS will cause a 
cclw - significant increase in CPU and wall-clock time.
cclw
          IF (CLOUDY(SPOT,LINE)) THEN
             CLEAR = .FALSE.
cclw         CALL CLRHIR(SPOT,LINE,CLOUDY,CNFLIM,CLEAR,CLRVAL,NFLAG,
cclw 1                   STATUS)
cclw         IF (STATUS .EQ. 99) THEN
cclw            ERSTAT = 99
cclw            RETURN
cclw         ENDIF
C
C          IF CLOUD-CLEARING WAS SUCCESSFUL, WRITE THE CLEARED DATA
C          INTO THE RING BUFFER.
C
             IF (CLEAR) THEN
                DO 30 ELEM = 1, HNCHAN - 1
                   RNGBUF(SPOT,CURPTR,ELEM) = CLRVAL(ELEM)
   30           CONTINUE
                HIRANC(SPOT,LINE,24) = NFLAG
                NUMNS(FRAMNO) = NUMNS(FRAMNO) + 1
             ELSE
                NMCLDY(FRAMNO) = NMCLDY(FRAMNO) + 1
             ENDIF
          ELSE
             NUMCLR(FRAMNO) = NUMCLR(FRAMNO) + 1
          ENDIF
C
   50   CONTINUE
C
C      INCREMENT TO POINTER FOR THE END OF THE RING BUFFER TO
C      PREPARE FOR THE NEXT LINE TO PROCESS.
C
        CURPTR = CURPTR + 1
        IF (CURPTR .EQ. NBRLIN) CURPTR = 1
C
C      IF THE RING BUFFER IS FULL, WRITE THE SCAN LINE AT THE START
C      OF THE RING TO THE HIRS DATA BUFFER.
C
        IF (CURPTR .EQ. STPTR) THEN
C
          DO 70 SPOT = 1, HNSPOT
            DO 60 ELEM = 1, HNCHAN - 1
              HIRDAT(SPOT,LINPTR(STPTR),ELEM) = RNGBUF(SPOT,STPTR,ELEM)
   60        CONTINUE
   70     CONTINUE
C
          STPTR = STPTR + 1
          IF (STPTR .EQ. NBRLIN) STPTR = 1
        ENDIF
C
  100 CONTINUE
C
C   CLOUD-CLEARING OF THE FRAME HAS BEEN COMPLETED.  WRITE THE REMAINDER
C   OF THE RING BUFFER SCAN LINES (FROM STPTR THROUGH CURPTR-1) TO
C   THE HIRS DATA BUFFER.
C
      LINE = STPTR
C
  110 IF (LINE .EQ. CURPTR) GOTO 140
C
      DO 130 SPOT = 1, HNSPOT
        DO 120 ELEM = 1, HNCHAN - 1
          HIRDAT(SPOT,LINPTR(LINE),ELEM) = RNGBUF(SPOT,LINE,ELEM)
  120   CONTINUE
  130 CONTINUE
C
      LINE = LINE + 1
      IF (LINE .EQ. NBRLIN) LINE = 1
C
      GOTO 110
C
  140 RETURN
      END
