      SUBROUTINE MDFLTR(SPOT,LINE,ANSPOT,ATOTLN,ANANC,MEDLIM,HGTLIM,
     1                  CNFLIM,TMPAMA,AMAANC,NDIMSQ,CHKPREC,TERRPERC,
     2                  CH4LIM,CH5LIM,AVLLIM,ALTERLIM)
      IMPLICIT NONE
C
C********************************************************************
C
C   NAME- MDFLTR
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- HDS-9000
C
C   VERSION- 1.0  DATE- 04/07/92  PROGRAMMER- JEFF MANNING, SMSRC
C   VERSION- 2.0  DATE- 04/20/94  PROGRAMMER- JEFF MANNING, SMSRC
C            ATOVS-1 CHANGE SPOT CONFIDENCE CHECK (FROM .LE. TO .LT.).
C   VERSION- 2.1  DATE- 10/11/94  PROGRAMMER- SURYA JANDHYALA, SMSRC
C            XATOV-60 MSU CHANNEL VALUES ARE CHANGED TO AMSU-A CHANNEL
C                     VALUES.
C   VERSION- 3.0  DATE- 05/21/96  PROGRAMMER- LEAH W. CASEY (HSTX)
C             INITIALIZE AMAPNT ARRAY TO ALL 0 FOR CRAY BASELINE. 
C              SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1  DATE- 06/13/00  PROGRAMMER- A. ALLEGRINO  (RITSS)
C             MODIFIED PROGRAM TO MAKE THE DIMENSIONS OF THE SQUARE
C             AREA USED FOR THE FILTER TO BE FLEXIBLE AND ADDED CHECKS
C             FOR PRECIPITATION CONTAMINATION AND TERRAIN.
C             ALSO CHANGED THE PRECIP FLAG VALUE FROM 1 TO 4 OR 5,
C             DEPENDING UPON WHETHER AMUSA CHANNEL 4 OR 5 IS USED. (WR#74)
C   VERSION- 3.2  DATE- 08/02/00  PROGRAMMER- A. ALLEGRINO  (RITSS)
C            MODIFIED PROGRAM TO CHOOSE POINTS WHICH ONLY MATCH THE TERRAIN 
C            TYPE OF THE REFERENCE POINT AND CREATED FLEXIABLE AND SEPARATE 
C            MEDIAN FILTER LIMITS FOR CHANNELS 4 AND 5 AND MADE THE LIMIT
C            FOR THE AVAILABLE PRECENTAGE OF SPOTS FLEXIABLE. 
C            ALSO ADDED A NOTEST FLAG TO THE MEDIAN FILTER AND ADDED A TERRAIN
C            HEIGHT CHECK OF THE SPOTS SURROUNDING THE REFERENCE SPOT. (WR#74)
C   VERSION- 3.3  DATE- 08/15/00  PROGRAMMER- A. ALLEGRINO  (RITSS)
C            MODIFIED PROGRAM TO IGNORE TERRAIN TESTING FOR ALL TERRAIN EXCEPT
C            LAND AND SEA. FOR ALL TERRAIN, EXCEPT LAND AND SEA, ALL OF THE 
C            SPOTS WITHING THE N X N DIMENSIONAL BOX ARE CONSIDERED AVAILABLE
C            FOR THE MEDIAN FILTER AND CHANNEL 5 IS USED.
C   VERSION- 3.4  DATE- 08/16/00  PROGRAMMER- A. ALLEGRINO  (RITSS)
C            CORRECTED DIFFERENCE CALCULATION BETWEEN THE MEDIAN AND
C            THE REFERENCE SPOT TO TAKE THE ABSOLUTE VALUE OF THE DIFFERENCE
C   VERSION- 3.5 DATE- 08/17/00 PROGRAMMER- A. ALLEGRINO  (RITSS)
C            ADDED ANOTHER LIMIT TO BE USED ONLY IN CASES WHERE THE TERRAIN IS
C            ICE, COAST, SNOW, ISOLATED LAND OR ISOLATED SEA. BUT STILL USING
C            CHANNEL 5.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function        
C   VERSION- 4.1    DATE- 01/13/06   PROGRAMMER- FRANK TILLEY (ERT)
C            RESTRICTED THE NUMBER OF WARNINGS WRITTEN TO THE KIDS FILE TO
C            FIVE MESSAGES PER ORBIT. ADDED PARAMETER FILE 'ERPARM' AND
C            COMMON MEMBER 'ERINFO' TO PICK UP THE MAIN PROGRAM NAME FOR
C            THE CALL TO THE KIDS ROUTINE (WR #298). EXPLICITLY DECLARED
C            UNDECLARED VARIABLES.
C
C   FUNCTION- THIS ROUTINE PERFORMS THE MEDIAN FILTER TEST ON THE LIMB
C             CORRECTED AMSU-A CHANNEL 5 DATA.   THE MEDIAN, OF THE
C             CENTRAL SPOT AND THE UP TO 8 SURROUNDING SPOTS, IS
C             DETERMINED AND THE DIFFERENCE BETWEEN THIS MEDIAN AND
C             THE MEASURED VALUE IS CALCULATED.  THIS DIFFERENCE IS THEN
C             COMPARED WITH A PREVIOUSLY ESTABLISHED LIMIT TO LOCATE
C             REGIONS OF PRECIPITATION.  A TERRAIN ELEVATION CHECK IS
C             ALSO PERFORMED TO BE SURE THE ELEVATION IS ACCEPTABLE.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE      I/O   TYPE      DEFINITION
C     --------      ---   ----      ----------
C     SPOT           I     INT      CURRENT AMSU-A SPOT NUMBER.
C     LINE           I     INT      CURRENT AMSU-A LINE NUMBER.
C     ANSPOT         I     INT      TOTAL NUMBER OF AMSU-A SPOTS.
C     ATOTLN         I     INT      TOTAL NUMBER OF AMSU-A LINES.
C     ANANC          I     INT      TOTAL NUMBER OF AMSU-A ELEMENTS FOR
C                                   EACH SPOT AND LINE IN THE AMSU-A
C                                   ANCILLARY BUFFER.
C     MEDLIM         I     REAL     MEDIAN FILTER LIMIT.
C     HGTLIM         I     REAL     TERRAIN HEIGHT LIMIT.
C     CNFLIM         I     INT      SPOT CONFIDENCE LIMIT.
C     TMPAMA         I     REAL     TEMPORARY ARRAY OF LIMB CORRECTED
C                                   AMSU-A CHANNEL 4 & 5 VALUES
C                                   DIM(ANSPOT X ATOTLN X 2).
C     AMAANC        I/O    REAL     AMSU-A ANCILLARY BUFFER
C                                   DIM(ANSPOTXATOTLNXANANC).
C     NDIMSQ         I     INT      THE DIMENSIONS OF THE SQUARE AREA OF
C                                   SPOTS USED IN THE MEDIAN FILTER
C     CHKPREC        I     INT      USED TO DETERMINE IF PREVIOUSLY DEFINED
C                                   PRECIPITATION CONTAMINATED SPOTS WILL BE
C                                   USED TO DETERMINE THE MEDIAN FILTER.
C                                   (=1, NO THEY WON'T, =0, YES THEY WILL)
C     TERRPERC       I     INT      MINIMUM PERCENTAGE OF SPOTS NECESSARY
C                                   TO DECLARE AN AREA OPEN OCEAN.
C     CH4LIM         I     REAL     MEDIAN FILTER LIMIT FOR AMSU CH4
C     CH5LIM         I     REAL     MEDIAN FILTER LIMIT FOR AMSU CH5
C     ALTERLIM       I     REAL     MEDIAN FILTER LIMIT FOR COASTS, ICE,
C                                   SNOW, ISOLATED SEA AND ISOLATED LAND
C     AVLLIM         I     REAL     THE MINIMUM PERCENTAGE OF AVAILABLE SPOTS
C                                   NECESSARY FOR THE MEDIAN FILTER 
C
C
C   LOCAL PARAMTERS-
C     VARIABLE      TYPE      DEFINITION
C     --------      ----      ----------
C     I, J, K        INT      DO LOOP COUNTERS.
C     STRSPT         INT      LOOP OVER SPOTS STARTING BOUNDARY.
C     ENDSPT         INT      LOOP OVER SPOTS ENDING BOUNDARY.
C     STRLNE         INT      LOOP OVER LINES STARTING BOUNDARY.
C     ENDLNE         INT      LOOP OVER LINES ENDIING BOUNDARY.
C     AVLSPT         INT      AVAILABLE SPOT COUNTER.
C     AMAPNT         REAL     ARRAY OF THE CURRENT 10 ELEMENTS
C                             CONTAINING THE CURRENT SPOT AND THE
C                             8 SURROUNDING SPOTS, ORDERED FROM
C                             SMALLEST TO LARGEST.  DIMENSION (9)
C     MEDIAN         REAL     THE MEDIAN OF THE AVAILABLE SPOTS.
C     DIFF           REAL     THE DIFFERENCE BETWEEN THE MEDIAN AND
C                             THE MEASURED VALUE.
C     DONE          LOGICAL   THE LOGICAL FLAG INDICATING WHETHER THE
C                             REORDERING HAS BEEN COMPLETED.
C     RHOLD          REAL     HOLDS A VALUE SO THAT IT CAN BE REORDERED.
C     RCNFLM         REAL     HOLDS THE REAL*4 REPRESENTATION OF THE
C                             SPOT CONFIDENCE LIMIT (CNFLIM).
C     PERCSPOT       REAL     PERCENTAGE OF AVAILABLE SPOTS FOR THE FILTER
C     PERCSEA        REAL     PERCENTAGE OF SPOTS DESIGNATED AS OPEN OCEAN
C     ISOLTERR       INT      FLAG INDICATING WHETHER OR NOT THERE IS ISOLATED
C                             SEA OR LAND.
C     KMSG          CH*100    KIDS MESSAGE BUFFER
C     KMSGCT         INT      COUNT OF KIDS MESSAGES PER ORBIT
C     AVLPERC        REAL     PERCENTAGE OF AVAILABLE LAND OR SEA SPOTS.
C     ISPT           INT      LOOP INDEX
C
C   SUBROUTINE USED- KIDS
C
C   FUNCTIONS USED- NONE
C
C   EXIT STATES- NONE
C
C********************************************************************
C
      INTEGER I, J, K, AVLSPT, ANSPOT, ATOTLN, ANANC, SPOT, LINE
      INTEGER CNFLIM, PERCSPOT, STRSPT, ENDSPT, STRLNE, ENDLNE,NDIMSQ,CHKPREC
      INTEGER PERCSEA,SEACNT,NUMSPT,TERRPERC,AVLSPT2 
      INTEGER KMSGCT, ISPT, NDIM, ISOLTERR
      REAL    TMPAMA(ANSPOT,ATOTLN,2), AMAANC(ANSPOT,ATOTLN,ANANC)
      REAL    RHOLD, MEDLIM, MEDIAN, DIFF, AMAPNT(NDIMSQ), HGTLIM, RCNFLM
      REAL    CH5LIM,CH4LIM,AVLLIM,AMAPNT2(NDIMSQ),ALTERLIM
      REAL    AVLPERC
      CHARACTER*100 KMSG
      CHARACTER*3 CPERCNT
      LOGICAL DONE
      LOGICAL ISR4SPV
C
      INCLUDE 'SPVAL'
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
      DATA KMSGCT/0/
      SAVE KMSGCT
C
C     INITIALIZE VARIABLES.
C
      AVLSPT = 0
      AVLSPT2 = 0
      MEDIAN = 0.0
      DIFF = 0.0
      DONE = .FALSE.
      DO 5 I=1,NDIMSQ
         AMAPNT(I) = 0.0
         AMAPNT2(I) = 0.0
    5 CONTINUE
      NDIM= INT(SQRT(FLOAT(NDIMSQ)))
C
C     SET UP THE SPOT AND LINE BOUNDARIES TO BE LOOPED OVER.
C
      IF(SPOT.LT.(NDIM/2+1)) THEN
         STRSPT = 1
         ENDSPT = NDIM
      ELSE IF(SPOT.GT. (ANSPOT - (NDIM/2))) THEN
         STRSPT = ANSPOT - NDIM+1
         ENDSPT = ANSPOT
      ELSE
         STRSPT = SPOT - INT((SQRT(FLOAT(NDIMSQ)))/2)
         ENDSPT = SPOT + INT((SQRT(FLOAT(NDIMSQ)))/2)
      END IF
C
      IF(LINE.LT. (NDIM/2+1)) THEN
         STRLNE = 1
         ENDLNE = NDIM 
      ELSE IF(LINE.GT. (ATOTLN - (NDIM/2))) THEN
         STRLNE = ATOTLN - NDIM +1 
         ENDLNE = ATOTLN
      ELSE
         STRLNE = LINE - INT((SQRT(FLOAT(NDIMSQ)))/2)
         ENDLNE = LINE + INT((SQRT(FLOAT(NDIMSQ)))/2)
      END IF
C
C   LOOP THRU LINE AND SPOT BOUNDARIES TO DETERMINE THE PERCENTAGE OF
C   SPOTS DESIGNATED AS SEA.
C
      SEACNT =0
      DO J = STRLNE, ENDLNE
         DO I = STRSPT, ENDSPT
           IF(NINT(AMAANC(I,J,8)) .EQ. 0) SEACNT =SEACNT + 1
         ENDDO
      ENDDO
      PERCSEA = NINT((FLOAT(SEACNT)/FLOAT(NDIMSQ)) * 100.0)
C
C     LOOP THROUGH THE LINE BOUNDARIES.
C
      DO 20 J = STRLNE, ENDLNE
C
C     LOOP THROUGH THE SPOT BOUNDARIES.
C
           DO 10 I = STRSPT, ENDSPT
C
C     CHECK TO SEE IF THE CURRENT AMSU-A SPOT IS A MISSING VALUE.
C
              IF(ISR4SPV(TMPAMA(I,J,1))) GO TO 10
C
C     CHECK THE SPOT CONFIDENCE.
C
              RCNFLM = REAL(CNFLIM)
              IF(AMAANC(I,J,5).LT.RCNFLM) THEN
                 GO TO 10
              ENDIF
C
C     CHECK FOR PRECIPITATION CONTAMINATION IF FLAG IS SET
C
              IF(CHKPREC .EQ.1 ) THEN
                 IF(AMAANC(I,J,9) .EQ. 1.0) THEN
                    GO TO 10
                 ENDIF
              ENDIF
C
C     CHECK THE TERRAIN ELEVATION.
C
              IF(AMAANC(I,J,7).GE.HGTLIM) GO TO 10
C
C AVOID TERRAIN TESTING IF TERRAIN IS NOT LAND OR SEA BY USING
C ALL THE AVAILABLE SPOTS REGARDLESS OF TERRAIN TYPE AND STORING THE
C AMSU-A CHANNEL 5 DATA.
C
              AVLSPT = AVLSPT +1
              AMAPNT(AVLSPT) = TMPAMA(I,J,2)
C
C THE SPOT WITHIN THE NXN DIMENSIONED BOX MUST HAVE THE SAME TERRAIN TYPE
C AS THE REFERENCE SPOT IN ORDER FOR IT TO BE CONSIDERED AN AVAILABLE SPOT
C FOR THE FILTER. USED FOR JUST LAND AND SEA TERRAIN.
C (ASA, 7/13/00)
C
C     INCREMENT THE AVAILABLE SPOT NUMBER.
C
              IF(NINT(AMAANC(SPOT,LINE,8)).EQ.0) THEN
                 AVLSPT2 = AVLSPT2 + 1
C
C     SET UP ARRAY BASED ON AVAILABLE SPOTS AND ON TERRAIN TYPE.
C     IF >= TERRPERC  SEA, THEN USE AMSU-A CH 4 ELSE USE CH 5.
C     TERRPERC IS INITIALLY SET TO 80% VIA THE SOFTFLAGS DATASET
C     THIS MAY CHANGE.
C
                 IF(PERCSEA .GE. TERRPERC) THEN
                     AMAPNT2(AVLSPT2) = TMPAMA(I,J,1)
                 ELSE
                    AMAPNT2(AVLSPT2) = TMPAMA(I,J,2)
                 ENDIF
              ENDIF
              IF(NINT(AMAANC(SPOT,LINE,8)).EQ.1) THEN
                 AVLSPT2 = AVLSPT2 + 1
                 AMAPNT2(AVLSPT2) = TMPAMA(I,J,2)
              ENDIF
C
   10      CONTINUE
   20 CONTINUE
C
C CHECK REFERENCE SPOT FOR TERRAIN. IF LAND OR SEA SUBSTITUTE AVLSPT2 FOR
C AVLSPT AND AMAPNT2 FOR AMAPNT. ALSO CHECK FOR INCIDENCES OF ISOLATED
C LAND OR SEA SPOTS. THIS IS DONE BY COMPARING THE PERCENTAGE OF
C AVAILABLE LAND OR SEA SPOTS WITH THE MINIMUM AMOUNT. IF GREATER THAN OR
C EQUAL TO, THEN SWAP THE AVAILABLE SPOT NUMBERS AND THE  CHANNEL DATA.
C
      ISOLTERR = 0
      IF(NINT(AMAANC(SPOT,LINE,8)).EQ.1.OR. NINT(AMAANC(SPOT,LINE,8))
     +   .EQ.0) THEN          
         AVLPERC = (FLOAT(AVLSPT2)/FLOAT(NDIMSQ)) *100.0
         IF(AVLPERC .GE. AVLLIM) THEN
            AVLSPT =AVLSPT2
            DO ISPT = 1,AVLSPT
               AMAPNT(ISPT)= AMAPNT2(ISPT)
            ENDDO
         ELSE
            ISOLTERR = 1
         ENDIF
      ENDIF
C
      AVLPERC = (FLOAT(AVLSPT)/FLOAT(NDIMSQ)) *100.0
      PERCSPOT = NINT(AVLPERC)
      IF(AVLPERC .GE. AVLLIM) THEN
C
C     REORDER THE ELEMENTS FROM SMALLEST TO LARGEST.
C
   30    IF(.NOT. DONE) THEN
            DONE =.TRUE.
            DO 40 K = 1,AVLSPT - 1
               IF(AMAPNT(K).LE.AMAPNT(K+1)) GO TO 40
               RHOLD = AMAPNT(K)
               AMAPNT(K) = AMAPNT(K+1)
               AMAPNT(K+1) = RHOLD
               DONE = .FALSE.
   40       CONTINUE
C
            GO TO 30
C
         END IF
C
C     COMPUTE THE MEDIAN VALUE.
C
         DO NUMSPT = NDIMSQ, NDIMSQ/2 +1, -1
            IF(AVLSPT .EQ. NUMSPT) THEN
               IF(MOD(NUMSPT,2) .EQ. 0) THEN
                  MEDIAN = (AMAPNT(NUMSPT/2) + AMAPNT((NUMSPT/2)
     >                     +1))/2.0
               ELSE
                  MEDIAN =  AMAPNT((NUMSPT/2) +1)
               ENDIF
            ENDIF
         ENDDO 
C
C     COMPUTE THE DIFFERENCE BETWEEN THE MEDIAN VALUE AND THE MEASURED
C     VALUE.
C
           IF(ISOLTERR .EQ. 0) THEN
              IF(PERCSEA .GE. TERRPERC) THEN
                 DIFF = ABS(MEDIAN - TMPAMA(SPOT,LINE,1))
                 MEDLIM = CH4LIM
              ELSE
                 DIFF = ABS(MEDIAN - TMPAMA(SPOT,LINE,2))
                 MEDLIM = CH5LIM
                 IF(NINT(AMAANC(SPOT,LINE,8)).NE.0.AND.
     +              NINT(AMAANC(SPOT,LINE,8)).NE.1) MEDLIM = ALTERLIM
              ENDIF
           ELSE
              MEDLIM = ALTERLIM 
           ENDIF
C
C     CHECK TO SEE IF THE DIFFERENCE IS GREATER THAN THE LIMIT.
C
           IF(DIFF.GT.MEDLIM) THEN
C
C     CHECK THE TERRAIN ELEVATION.
C
              IF(AMAANC(SPOT,LINE,7).LT.HGTLIM) THEN
C
C     SET THE PRECIPITATION EDITING FLAG IN THE AMSU-A ANCILLARY
C     BUFFER TO PRECIPITATING.
C     CHANGED THE VALUE OF THE FLAG FROM 1 TO EITHER 4 OR 5 TO
C     REPRESENT WHETHER AMSU-A CHANNEL 4 OR 5 WAS USED IN THE
C     FILTER (ASA, 6/15/00).
C
                 IF(PERCSEA .GE. TERRPERC) THEN
                     AMAANC(SPOT,LINE,9) = 4.0
                 ELSE
                     AMAANC(SPOT,LINE,9) = 5.0
                 ENDIF
C
              ELSE
C
C     IF THE REFERENCE POINT IS AT A HEIGHT WHICH EQUALS OR EXCEEDS
C     THE TERRAIN HEIGHT LIMIT, SET THE MEDIAN FILTER VALUE TO THE NO TEST
C     PERFORMED INDICATOR.
C
                 AMAANC(SPOT,LINE,9) = 1.0
              END IF
           ELSE
C
C     CHECK THE TERRAIN ELEVATION.
C
             IF(AMAANC(SPOT,LINE,7).LT.HGTLIM) THEN
C
C     SET THE PRECIPITATION EDITING FLAG IN THE AMSU-A ANCILLARY
C     BUFFER TO NOT PRECIPITATING.
C
                AMAANC(SPOT,LINE,9) = 0.0
             ELSE
C
C     IF THE REFERENCE POINT IS AT A HEIGHT WHICH EQUALS OR EXCEEDS
C     THE TERRAIN HEIGHT LIMIT, SET THE MEDIAN FILTER VALUE TO THE NO TEST
C     PERFORMED INDICATOR.
C
                AMAANC(SPOT,LINE,9) = 1.0
             END IF
          END IF
C
      ELSE
C
C  SET THE MEDIAN FILTER FLAG TO 1 BECAUSE NO TESTING WAS DONE DUE TO
C  A LACK OF AVAILABLE POINTS.
C
         AMAANC(SPOT,LINE,9) = 1.0
C
C  DO NOT SEND MORE THAN 5 WARNING MESSAGES TO THE KIDS FILE PER ORBIT.
C  USE THE 'SUBNAM' FROM COMMON /ERINFO/ IN THE CALL TO KIDS.
C
         IF (KMSGCT.LT.5) THEN
           KMSGCT = KMSGCT+1
           KMSG(1:60)= '*WARNING* NOT ENOUGH AVAILABLE SPOTS FOR MEDIAN FILTER(ONLY '
           WRITE(CPERCNT,'(I3)') PERCSPOT
           KMSG(61:63)= CPERCNT
           KMSG(64:93)='%). PRECIP FLAG SET TO MISSING' 
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
         END IF
      END IF
C
      RETURN
      END
