      SUBROUTINE PRECIP(CLOUDY,CNFLIM,MEDLIM,HGTLIM,AMASPT,AMASCN,  
     >CLWTHR,LPITHR,LATLMT,NDIM,CHKPREC,TERRPERC,CH4LIM,CH5LIM,
     >AVLLIM,ALTERLIM)
      IMPLICIT NONE
C
C********************************************************************
C
C   NAME- PRECIP
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 04/07/92   PROGRAMMER- JEFF MANNING, 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- 11/02/94   PROGRAMMER- ALEX POZDNYAK, SMSRC
C                   XATOV-63 ARGUMENTS MSUSPT AND MSUSCN WERE CHANGED
C                   TO AMASPT AND AMASCN. ALL REFERENCES TO MSU
C                   CHANNEL 2 WERE CHANGED TO AMSU-A CHANNEL 5.
C                   ARGUMENT ANANC WAS ADDED IN CALL TO MDFLTR.
C                   VARIABLE TMPMSU WAS CHANGED TO TMPAMA. VARIABLE
C                   MSUTMP WAS CHANGED TO AMATMP. CALLING ARGUMENT
C                   ASSOCM WAS REMOVED. CALL TO LCMSU WAS REPLACED
C                   WITH CALL TO LIMCOR FOR AMSU-A. INCLUDE (LCCOEF)
C                   WAS ADDED.
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- 05/04/98   PROGRAMMER- MIKE FERGUSON, RSTX
C         LIMB CORRECT ACUTAL AMADAT BUFFER INSTEAD OF TEMPORARY BUFFER.
C   VERSION- 3.2    DATE- 09/28/98   PROGRAMMER- A. ALLEGRINO, RITSS
C         MOVED THE PLACEMENT OF AMSU-A CHANNEL 5 DATA INTO THE TEMPORARY
C         BUFFER FROM BEFORE LIMB CORRECTION TO AFTER LIMB CORRECTION 
C         BECAUSE MDFLTR REQUIRES LIMB CORRECTED AMSU-A CHANNEL 5 DATA.
C   VERSION- 3.3    DATE- 10/08/98   PROGRAMMER- A. ALLEGRINO, RITSS
C         ADDED THE LAND/SEA FLAG TO THE CALLING STATEMENT AND IT'S USED 
C         TO LIMB CORRECT THE AMSU-A TEMPERATURES.
C   VERSION- 3.4    DATE-  4/14/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         REMOVED THE LAND/SEA/FLAG FROM THE CALLING STATEMENT AND
C         REMOVED THE CALLS TO LIMCOR SINCE LIMB CORRECTION IS NO 
C         LONGER DONE HERE. THE LIMB CORRECTED DATA IS PASSED IN.
C   VERSION- 3.5    DATE-  6/17/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         INTRODUCED A CLOUD LIQUID WATER TEST TO IDENTIFY PRECIPITATION
C         AREAS OVER OCEAN
C   VERSION- 3.6    DATE-  6/21/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         INTRODUCED A LAND PRECIPITATION INDEX TO IDENTIFY PRECIPITATION
C         AREAS OVER LAND
C   VERSION- 3.7    DATE-  6/22/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         INTRODUCED A NEW FLAG INTO THE AMAANC BUFFER AT ELEMENT 13
C         CALLED THE CLW/LPI/PRECIP FLAG. THIS WILL BE SEPARATE FROM THE
C         MEDIAN FILTER PRECIP FLAG LOCATED IN ELEMENT 9.
C   VERSION- 3.8    DATE-  7/13/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         PLACED THE PRECIPITATION FLAG DERIVED FROM THE CLOUD LIQUID
C         WATER AND THE LAND PRECIPITATION INDEX INTO THE AMADAT BUFFER
C         SO THAT IT CAN BE INTERPOLATED TO THE HIRS FOV.
C   VERSION- 3.9    DATE-  8/03/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         INITIALIZED THE PRECIPITATION FLAG IN AMAANC AND AMADAT TO
C         ZERO TO CORRECT PROBLEM OF AN OVER ABUNDANCE OF THE PRECIPTATION
C         FLAG BEING SET IN THE RODF.
C   VERSION- 3.10   DATE- 10/25/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         CHANGED PRECIP FILTERING TO INCLUDE THE MEDIAN FILTER,
C         CLOUD LIQUID WATER (CLW) AND LAND PRECIPITATION INDEX (LPI).
C         THE METHODS ARE APPLIED ACCORDING TO LATITUDE AND TERRAIN
C         TYPE IN THE FOLLOWING MANNER:
C
C         EQUATORWARD OF 35, OVER OCEAN : CLW 
C         POLEWARD OF 35, OVER OCEAN    : CLW & MEDIAN FILTER
C         OVER LAND                     : LPI & MEDIAN FILTER
C
C   VERSION- 3.11   DATE- 12/08/99   PROGRAMMER- A. ALLEGRINO, RITSS
C         CHANGED PRECIP MEDIAN FILTER TO USE AMSU-A CHANNEL 4 INSTEAD 
C         OF AMSU-A CHANNEL 5.
C
C   VERSION- 3.12   DATE- 02/15/00   PROGRAMMER- A. ALLEGRINO, RITSS
C         CHANGED PRECIP MEDIAN FILTER TO USE WHATEVER AMSU-A CHANNEL   
C         IS GIVEN  BY THE SOFTFLAGS.A2 FILE.
C   VERSION- 3.13   DATE- 06/13/00   PROGRAMMER- A. ALLEGRINO, RITSS
C         CHANGED THE  PROGRAM IN ORDER TO INCREASE THE FLEXIBILITY
C         IN THE DIMENSION OF THE AREA SURROUNDING THE AMSU-A POINT
C         WITHIN THE MEDIAN FILTER, PASS TO THE MEDIAN FILTER BOTH
C         AMSU-A CHANNELS 4 AND 5,  PASS TO THE MEDIAN FILTER A FLAG
C         TO DETERMINE WHETHER OR NOT TO INCLUDE POINTS WHICH WERE DEEMED
C         PRECIPITATION CONTAMINATED AND, PASS TO THE MEDIAN FILTER
C         THE PERCENTAGE OF SPOTS NECESSARY TO DECLARE THE AREA SEA
C         OR NON-SEA.
C         ALSO MADE CHANGES TO ACCOUNT FOR THE MEDIAN FILTER RESULT BEING
C         GREATER THAN 1. (WR#74)
C   VERSION- 3.14 DATE- 08/02/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C         ADDED SEVERAL MEDIAN FILTER LIMITS TO PASS THRU TO THE SUBROUTINE
C         MDFLTR.  ALSO ADDED A NOTEST FLAG TO THE MEDIAN FILTER FLAG. HENCE
C         THE IF STATEMENTS FOR THE PRECIP FLAG HAD TO BE ADJUSTED.(WR#74)
C   VERSION- 3.15 DATE- 08/17/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C         ADDED ANOTHER MEDIAN FILTER LIMIT TO PASS TO MDFLTR AND MODIFIED
C         CODE TO USE MEDIAN FILTER IF CLW  OR LPI IS NOT AVAILABLE.
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        INCLUDED COMMON 'ERINFO' TO PICK UP VARIABLE 'SUBNAM' FOR THE
C        CALL TO KIDS. CONSTRAINED THE NUMBER OF WARNINGS SENT TO THE
C        KIDS FILE TO FIVE PER ORBIT. (WR #298)
C
C   FUNCTION- THIS ROUTINE IS THE DRIVER FOR THE PRECIPITATION EDITING
C             FLAG.  CURRENTLY THE ONLY TEST BEING PERFORMED IS THE
C             MEDIAN FILTER TEST.  THE MEDIAN FILTER TEST IS RUN
C             AGAINST LIMB CORRECTED DATA.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE      I/O   TYPE      DEFINITION
C     --------      ---   ----      ----------
C     CLOUDY        I/O   LOGICAL   LOGICAL VARIABLE INDICATING WHETHER
C                                   THE HIRS SPOT IS CLOUDY OR CLEAR.
C                                   DIM (HNSPOT X HTOTLN)
C     CNFLIM         I     INT      THE SPOT CONFIDENCE LIMIT.
C     MEDLIM         I     REAL     THE MEDIAN FILTER LIMIT.
C     HGTLIM         I     REAL     THE TERRAIN HEIGHT LIMIT.
C     AMASPT         I     INT      THE COLLOCATED AMSU-A SPOT NUMBER.
C                                   DIM (HNSPOT X HTOTLN)
C     AMASCN         I     INT      THE COLLOCATED AMSU-A LINE NUMBER.
C                                   DIM (HNSPOT X HTOTLN)
C     CLWTHR         I     REAL     CLOUD LIQUID WATER THRESHOLD READ IN 
C                                   FROM SOFTFLAGS DATASET.
C     LPITHR         I     REAL     LAND PRECIPITATION THRESHOLD READ IN 
C                                   FROM SOFTFLAGS DATASET.
C     LATLMT         I     REAL     LATITUDE TO DETERMINE WHICH PRECIP  
C                                   FILTER TO USE OVER OCEAN.
C     NDIM           I     INT      DETERMINES THE DIMENSION OF THE SQUARE
C                                   BOX SUROUNDING THE AMSU-A SPOT WITHIN 
C                                   THE MEDIAN FILTER.
C     CHKPREC        I     INT      FLAG TO DETERMINE WHETHER OR NOT TO 
C                                   CHECK FOR PRECIPITATION CONTAMINATION
C                                   WITHIN THE MEDIAN FILTER BOX. (=1,YES;
C                                   =0,NO)
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     AVLLIM         I     REAL     THE MINIMUM PERCENTAGE OF AVAILABLE SPOTS
C                                   NECESSARY FOR THE MEDIAN FILTER
C     ALTERLIM       I     REAL     MEDIAN FILTER LIMIT FOR COASTS, ICE,
C                                   SNOW, ISOLATED SEA AND ISOLATED LAND
C
C   COMMON AREAS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPVAL'
C
C   LOCAL PARAMTERS-
C     VARIABLE      TYPE      DEFINITION
C     --------      ----      ----------
C     I, J, K        INT      DO LOOP COUNTERS.
C     AMATMP         REAL     TEMPORARY AMSU-A DATA BUFFER
C                             DIMENSION (ANSPOT X ATOTLN X ADATEL).
C     TMPAMA         REAL     CHANNEL 4  AND 5 ONLY AMSU-A BUFFER.
C     NDIMSQ         INT      NDIM SQUARED
C     KMSG          CH*100    KIDS MESSAGE BUFFER
C     KMSGCT         INT      COUNT OF WARNING MESSAGES SENT TO THE KIDS FILE.
C
C   SUBROUTINE USED- MDFLTR, KIDS
C
C   FUNCTIONS USED-  NONE
C
C   EXIT STATES-     NONE
C
C********************************************************************
C
      LOGICAL CLOUDY(HNSPOT,HTOTLN)
      CHARACTER*100 KMSG
      INTEGER CNFLIM,TERRPERC,CHKPREC,NDIM,NDIMSQ
      REAL    MEDLIM, HGTLIM, CLWTHR, LPITHR, LATLMT
      REAL    CH4LIM,CH5LIM,ALTERLIM, AVLLIM
      INTEGER AMASPT(HNSPOT,HTOTLN), AMASCN(HNSPOT,HTOTLN)
      LOGICAL ISR4SPV
C
      INTEGER I, J, K, KMSGCT
      REAL    AMATMP(ANSPOT,ATOTLN,ADATEL), TMPAMA(ANSPOT,ATOTLN,2)
C
      DATA KMSGCT/0/
      SAVE KMSGCT
C
C  SQUARE THE DIMENSION OF THE MEDIAN FILTER AREA TO GET THE NUMBER 
C  OF POINTS IN THE AREA USED BY THE MEDIAN FILTER.
C 
      NDIMSQ= NDIM**2
C
C  SET-UP THE TEMPORARY AMSU-A ARRAY FOR CHANNELS 4 AND 5.
C
      DO 50, I = 1,ANSPOT
           DO 40, J = 1,ATOTLN
             TMPAMA(I,J,1) = AMADAT(I,J,4)
             TMPAMA(I,J,2) = AMADAT(I,J,5)
   40      CONTINUE
   50 CONTINUE
C
C     LOOP OVER THE AMSU-A LINES.
C
      DO 90, J = 1,ATOTLN
C
C     LOOP OVER THE AMSU-A SPOTS.
C
           DO 80, I = 1,ANSPOT
C
C     SKIP SPOTS WITHOUT EARTH LOCATION INFORMATION
C
              IF (ISR4SPV(AMAANC(I,J,1))) GO TO 80
C
C     CHECK TO SEE IF THE SPOT CONTAINS A MISSING VALUE.
C
              IF(.NOT. ISR4SPV(TMPAMA(I,J,1)))  THEN
C
C     CHECK THE TEMPORARY AMSU-A DATA SET (CHANNEL 4 OR 5) WITH THE MEDIAN
C     FILTER TEST.
C
                CALL MDFLTR(I,J,ANSPOT,ATOTLN,ANANC,MEDLIM,HGTLIM,
     +                      CNFLIM,TMPAMA,AMAANC,NDIMSQ,CHKPREC,
     >                      TERRPERC,CH4LIM,CH5LIM,AVLLIM,ALTERLIM)
C
C     CHECK THE CLOUD LIQUID WATER VALUE IN AMAAMC(I,J,10) IF GREATER
C     THAN THE THRESHOLD SET THE CLW/LPI/PRECIP EDITING FLAG TO 1.0
C     IN BOTH THE AMSUA ANCILLARY BUFFER AND DATA BUFFER.
C
                AMAANC(I,J,13) =0.0
                AMADAT(I,J,28) =0.0
                IF(AMAANC(I,J,10) .GT. CLWTHR ) THEN
                 AMAANC(I,J,13) =1.0
                 AMADAT(I,J,28) =1.0
                ENDIF
C
C     CHECK THE LAND PRECIPITATION VALUE IN AMAAMC(I,J,12) IF GREATER
C     THAN THE THRESHOLD SET THE CLW/LPI/PRECIP EDITING FLAG TO 1.0
C     IN BOTH THE AMSUA ANCILLARY BUFFER AND DATA BUFFER.
C
                IF(AMAANC(I,J,12) .GT. LPITHR ) THEN
                 AMAANC(I,J,13) =1.0
                 AMADAT(I,J,28) =1.0
                ENDIF
C
              ELSE
C
C     SEND NO MORE THAN FIVE WARNING MESSAGES PER ORBIT TO THE KIDS FILE.
C     USE THE VALUE OF 'SUBNAM' FROM COMMON MEMBER /ERINFO/.
C
                IF (KMSGCT.LT.5) THEN
                   KMSGCT = KMSGCT+1
                   KMSG( 1:43)= '*WARNING* AMSU-A CH4 IS MISSING. NO PRECIP '
                   KMSG(44:57)= 'FLAGGING DONE.'
                   CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
                END IF
              ENDIF

   80      CONTINUE
   90 CONTINUE
C
C     LOOP OVER THE HIRS LINES.
C
      DO 110, J = 1,HTOTLN
C
C     LOOP OVER THE HIRS SPOTS.
C
           DO 100, I = 1,HNSPOT
C
C     IF THE AMSU-A SPOT IS PRECIPITATING THEN MARK THE CLOUDY FLAG TO
C     TRUE.
C
C     CHECK TO SEE IF AMASCN IS A MISSING VALUE.
C
             IF(AMASCN(I,J).EQ.I4SPV) GO TO 100
C
C     CHECK TO SEE IF TERRAIN TYPE IS SEA OR LAND.
C     IF LAND USE THE LAND PRECIP INDEX IF IT IS NOT MISSING.
C
             IF(AMAANC(AMASPT(I,J),AMASCN(I,J),8).EQ.1.0) THEN
C
               IF(.NOT. ISR4SPV(AMAANC(AMASPT(I,J),AMASCN(I,J),12))) THEN
                IF(AMAANC(AMASPT(I,J),AMASCN(I,J),13).EQ.1.0 .AND.
     >             AMAANC(AMASPT(I,J),AMASCN(I,J),9) .GT. 1.0) THEN
C
                     CLOUDY(I,J) = .TRUE.
                END IF
               ELSE
C
C     IF THE LAND PRECIPITATION INDEX IS MISSING, CHECK THE PRECIP
C     EDITING FLAG.
C
                IF(AMAANC(AMASPT(I,J),AMASCN(I,J),9) .GT. 1.0) THEN
                     CLOUDY(I,J) = .TRUE.
                END IF
               ENDIF
             ENDIF
C
C    IF SEA, CHECK THE LATITUDE AND CHECK FOR MISSING CLW
C
             IF(AMAANC(AMASPT(I,J),AMASCN(I,J),8).EQ.0) THEN
              IF(ABS(AMAANC(AMASPT(I,J),AMASCN(I,J),1)).GT. LATLMT) THEN
C
               IF(.NOT. ISR4SPV(AMAANC(AMASPT(I,J),AMASCN(I,J),10))) THEN
                IF(AMAANC(AMASPT(I,J),AMASCN(I,J),13).EQ.1.0 .AND.
     >             AMAANC(AMASPT(I,J),AMASCN(I,J),9) .GT. 1.0) THEN
C
                     CLOUDY(I,J) = .TRUE.
                END IF
               ELSE
                 IF(AMAANC(AMASPT(I,J),AMASCN(I,J),9) .GT. 1.0) THEN
                  CLOUDY(I,J) = .TRUE.
                 ENDIF
               ENDIF
              ELSE
                IF(AMAANC(AMASPT(I,J),AMASCN(I,J),13).EQ.1.0) THEN
C
                     CLOUDY(I,J) = .TRUE.
                END IF
              ENDIF
             ENDIF
C
  100      CONTINUE
  110 CONTINUE
C
      RETURN
      END
