C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MWISDOMFLTR FILTER WISDOM OBS BASED ON USER CRITERIA
C   PRGMMR: L. BENJAMIN       ORG: FSL        DATE: 08-07-20
C
C ABSTRACT: FILTER OUT WISDOM RECORDS BASED ON USER CRITERIA
C
C PROGRAM HISTORY LOG:
C   08-07-20  L. BENJAMIN    ORIGINAL VERSION
C   09-09-09  L. BENJAMIN    FIXED FILTERING OPTIONS 1,2, AND 3
C
C USAGE:    CALL MWISDOMFLTR(ATIME,MINBCK,MINFWD,RECWIN,SPVAL,
C                            PROVDR_ENBL,NUMSTA,OBTIME,STANAMES,
C                            PROVDR,SUBSRC,
C                            LATS,LONS,P,FNUM,RNUM,NUMSTA_NEW)
C
C   INPUT ARGUMENT LIST:
C     ATIME    - CHARACTER*9   YYJJJHHMM NOMINAL OB TIME
C     MINBCK   - INTEGER       NUMBER OF MINUTES RELATIVE TO NOMINAL
C                              TIME AT WHICH TO START TIME WINDOW
C     MINFWD   - INTEGER       NUMBER OF MINUTES RELATIVE TO NOMINAL
C                              TIME AT WHICH TO END TIME WINDOW
C     RECWIN   - INTEGER       0 - RETURN ALL RECORDS WITHIN *FILE*
C                              4 - RETURN ALL RECORDS WITHIN *WINDOW*
C     SPVAL    - REAL          MISSING DATA FLAG
C     PROVDR_ENBL - LOGICAL    ARRAY OF PROVIDER-ENABLE FLAGS
C     NUMSTA   - INTEGER       NO. OF RECORDS IN OB ARRAYS
C
C   INPUT/OUTPUT ARGUMENT LIST:
C     OBTIME   - INTEGER OBTIME(NUMSTA)
C                INPUT I4TIMES FOR EACH OB "RECORD"
C     STANAMES - CHARACTER*(*) ARRAY STANAMES(NUMSTA)
C                STATION NAMES: ONLY THE FIRST 10 CHARACTERS ARE USED.
C     PROVDR   - CHARACTER*(*) ARRAY PROVDR(NUMSTA) PART OF STA RECORD
C     SUBSRC   - INTEGER ARRAY SUBSRC(NUMSTA) PART OF STA RECORD
C                USED IN PROVIDER-FILTERING
C     LATS     - REAL ARRAY LATS(NUMSTA)     PART OF STA RECORD
C     LONS     - REAL ARRAY LONS(NUMSTA)     PART OF STA RECORD
C     P        - REAL ARRAY P(NUMSTA)PART OF STA RECORD
C     FNUM     - INTEGER ARRAY FNUM(NUMSTA)  PART OF STA RECORD
C     RNUM     - INTEGER ARRAY RNUM(NUMSTA)  PART OF STA RECORD
C
C   OUTPUT ARGUMENT LIST:
C     NUMSTA_NEW - INTEGER
C                NO. OF STA RECORDS REMAINING AFTER BEING CULLED
C
C SUBPROGRAMS CALLED:
C   M_MKI4TM
C
C REMARKS:
C
C   NOTE THAT TO KEEP THINGS SIMPLE THE TIME WINDOW IS INCLUSIVE.
C   THUS, GIVEN MINBCK = -15, MINFWD = 4 AND A NOMINAL TIME AT THE
C   TOP OF THE HOUR THIS ROUTINE WILL ACCEPT OBS TIME-STAMPED
C   BETWEEN MINUTE 45 OF THE PREVIOUS HOUR AND MINUTE 4 OF THE
C   CURRENT HOUR.  TO RUN 3 CYCLES IN AN HOUR WITHOUT OVERLAPPING
C   OBS, HERE'S AN EXAMPLE:
C
C   ATIME                                             971751200
C   1.  RUN TIME, MINBCK, MINFWD           1220          -15, 4
C   2.  RUN TIME, MINBCK, MINFWD           1240           5, 24
C   3.  RUN TIME, MINBCK, MINFWD           1300          25, 44
C
C   IN ADDITION TO THE ABOVE PARAGRAPH, STATIONS WITHOUT LAT, LON OR
C   P WILL BE FILTERED.  IF DOMAIN FILTERING HAS BEEN SELECTED,
C   THAT WILL ALSO BE PERFORMED.
C
C   USERS STUPID ENOUGH TO SET RECWIN EQUAL TO SOMETHING OTHER THAN
C   0 OR 4 WILL GET RECWIN 0 BEHAVIOR BY DEFAULT...
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN 77 + EXTENSIONS
C   MACHINE: UNIX
C
C$$$




      SUBROUTINE MWISDOMFLTR(ATIME,MINBCK,MINFWD,RECWIN,SPVAL,
     1     PROVDR_ENBL,NUMSTA,OBTIME,STANAMES,PROVDR,SUBSRC,
     2     LATS,LONS,P,FNUM,RNUM,NUMSTA_NEW)


C     ------------------------------------------------------------------
C     CONSTANTS
C     ------------------------------------------------------------------
      INCLUDE 'IMPLICIT'
      INCLUDE 'MADISCON'
      INCLUDE 'MADISTMP'

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*9 ATIME
      INTEGER MINBCK
      INTEGER MINFWD
      INTEGER RECWIN
      REAL SPVAL
      LOGICAL PROVDR_ENBL(*)
      INTEGER NUMSTA
      INTEGER OBTIME(NUMSTA)
      CHARACTER*(*) STANAMES(NUMSTA)
      CHARACTER*(*) PROVDR(NUMSTA)
      INTEGER SUBSRC(NUMSTA)
      REAL LATS(NUMSTA)
      REAL LONS(NUMSTA)
      REAL P(NUMSTA)
      INTEGER FNUM(NUMSTA)
      INTEGER RNUM(NUMSTA)
      INTEGER NUMSTA_NEW

C     ------------------------------------------------------------------
C     LOCAL VARIABLES
C     ------------------------------------------------------------------
      INTEGER I,LOW,NOM,HIGH,STATUS,OBMIN,RECWINL,J
      INTEGER RECORD(MXWISDOM_P*4)
      LOGICAL HAVE_STATION
c$$$      CHARACTER*8 LOCAL_STANAMES(MXSNOW_P*4)
      INTEGER LOCAL_OBTIME(MXWISDOM_P*4)
      CHARACTER*10 LOCAL_STANAMES(MXWISDOM_P*4)
      CHARACTER*10 LOCAL_DPNAME(MXWISDOM_P*4)

      LOGICAL MDOMFLTR
      EXTERNAL MDOMFLTR

      EQUIVALENCE(LOCAL_STANAMES,TMP_L2_C1)
      EQUIVALENCE(LOCAL_DPNAME,TMP_L2_C2)
      EQUIVALENCE(RECORD,TMP_L2_N3)

C
      NUMSTA_NEW = 0
      IF(NUMSTA.EQ.0)GO TO 900
      RECWINL = RECWIN
      IF(RECWINL.LE.0.OR.RECWINL.GT.4)RECWINL = 0

      IF(RECWINL.NE.0)THEN
C
C     CREATE THE TIME WINDOW IN I4TIME.
C
         CALL M_MKI4TM(ATIME, NOM, STATUS)
         HIGH = NOM + MINFWD * 60
         LOW = NOM + MINBCK * 60

      ENDIF
C
      DO 3 I = 1, NUMSTA
C
         HAVE_STATION = .FALSE.
C
C     FILTER OUT STATIONS MISSING LAT, LON, P OR TIME.
C
         IF(LATS(I).EQ.SPVAL.OR.LONS(I).EQ.SPVAL
     1      .OR.P(I).EQ.SPVAL.OR.OBTIME(I).EQ.SPVAL)GO TO 3
C
C     REMOVE THE SECONDS FROM THE OBTIME FOR USE IN COMPARING AGAINST
C     THE HIGH SIDE OF THE WINDOW:  THIS WAS DISCOVERED WHEN ADDING
C     ACARS DATA -- IF A TIME IS, SAY, 23:59:59, WE WON'T GET IT IN
C     A WINDOW SPECIFIED TO BE FROM MINUTE 0 TO MINUTE 59.  BY
C     REDUCING THE 23:59:59 TO 23:59:00 WE WILL GET IT.
C
         IF(OBTIME(I).NE.SPVAL)THEN
            OBMIN = OBTIME(I)/60*60
            IF(OBTIME(I).LT.0)OBMIN = OBMIN - 60
         ENDIF
C
C     FILTER OUT DISABLED PROVIDERS.
C
         IF(SUBSRC(I).NE.0.AND.SUBSRC(I).NE.999)THEN
            IF(.NOT.PROVDR_ENBL(SUBSRC(I)))GO TO 3
         ENDIF
C
C     IF WE'RE RETURNING ALL RECORDS (RECWINL 0 - IN THE FILE, 4 - IN 
C     THE WINDOW), SKIP THE TIME WINDOW FILTERING.
C
         IF(RECWINL.EQ.0.OR.RECWINL.EQ.4)GO TO 40
C
         DO 4 J = 1, NUMSTA_NEW
C
C     PROVIDER FILTERING MEANS THAT WE CAN'T COUNT ON THE STATION NAMES
C     BEING UNIQUE.  WHAT WE CAN COUNT ON IS THE COMBINATION OF
C     STATION NAME AND DATA PROVIDER NAME WILL BE UNIQUE.
C
            IF(LOCAL_STANAMES(J).EQ.STANAMES(I)(1:10).AND.
     4          LOCAL_DPNAME(J).EQ.PROVDR(I)(1:10))THEN
C
               HAVE_STATION = .TRUE.

               IF(RECWIN.EQ.3)THEN
                  IF(OBTIME(I).GE.LOCAL_OBTIME(J).AND.OBMIN.LE.HIGH.AND.
     1                 OBTIME(I).NE.SPVAL)THEN
                     LOCAL_OBTIME(J) = OBTIME(I)
                     RECORD(J) = I
                  ENDIF
               ELSE IF(RECWIN.EQ.2)THEN
                  IF(OBTIME(I).LE.LOCAL_OBTIME(J).AND.OBMIN.LE.HIGH.AND.
     1                 OBMIN.GE.LOW.AND.OBTIME(I).NE.SPVAL)THEN
                     LOCAL_OBTIME(J) = OBTIME(I)
                     RECORD(J) = I
                  ENDIF
               ELSE IF(RECWIN.EQ.1)THEN
                  IF(ABS(OBTIME(I)-NOM).LE.ABS(LOCAL_OBTIME(J)-NOM).AND.
     1                 OBMIN.LE.HIGH.AND.OBTIME(I).NE.SPVAL)THEN
                     LOCAL_OBTIME(J) = OBTIME(I)
                     RECORD(J) = I
                  ENDIF
               ENDIF
            ENDIF

 4       CONTINUE
C
C     IF WE DIDN'T ALREADY HAVE ONE FOR THIS STATION, ADD IT IN IF
C     WITHIN THE TIME WINDOW.
C
 40      IF((.NOT.HAVE_STATION.AND.OBTIME(I).GE.LOW.AND.
     1        OBMIN.LE.HIGH.AND.OBTIME(I).NE.SPVAL).OR.RECWIN.EQ.0)THEN
C
C     WHOOPS, NOT SO FAST.  FIRST TOSS STUFF OUTSIDE THE USER'S
C     DOMAIN, IF SO DESIRED.
C
            IF(.NOT.MDOMFLTR(LATS(I),LONS(I)))THEN

               NUMSTA_NEW = NUMSTA_NEW + 1
               LOCAL_OBTIME(NUMSTA_NEW) = OBTIME(I)
               LOCAL_STANAMES(NUMSTA_NEW) = STANAMES(I)(1:10)

               LOCAL_DPNAME(NUMSTA_NEW) = PROVDR(I)(1:10)

               RECORD(NUMSTA_NEW) = I

            ENDIF

         ENDIF
C
 3    CONTINUE
C
 900  IF(NUMSTA_NEW.NE.0)THEN
C
C     FILL IN THE ARRAYS IN PLACE.
C
         DO 6 I = 1, NUMSTA_NEW
            OBTIME(I) = OBTIME(RECORD(I))
            PROVDR(I) = PROVDR(RECORD(I))
            LATS(I) = LATS(RECORD(I))
            LONS(I) = LONS(RECORD(I))
            P(I) = P(RECORD(I))
            FNUM(I) = FNUM(RECORD(I))
            RNUM(I) = RNUM(RECORD(I))
 6       CONTINUE
C
      ENDIF
C
      RETURN
      END
C**---------------------------------------------------------------------
C**REQUIRED STANDARD FSL DISCLAIMER (NOVEMBER 2000)
C**---------------------------------------------------------------------
C**  OPEN SOURCE LICENSE/DISCLAIMER,  FORECAST SYSTEMS LABORATORY
C**  NOAA/OAR/FSL, 325 BROADWAY BOULDER, CO 80305
C**
C**  THIS SOFTWARE IS DISTRIBUTED UNDER THE OPEN SOURCE DEFINITION, 
C**  WHICH MAY BE FOUND AT http://www.opensource.org/osd.html.
C**
C**  IN PARTICULAR, REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS,
C**  WITH OR WITHOUT MODIFICATION, ARE PERMITTED PROVIDED THAT THE 
C**  FOLLOWING CONDITIONS ARE MET:
C**
C**  - REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THIS NOTICE, THIS LIST
C**    OF CONDITIONS AND THE FOLLOWING DISCLAIMER.
C**
C**  - REDISTRIBUTIONS IN BINARY FORM MUST PROVIDE ACCESS TO THIS 
C**    NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER, AND
C**    THE UNDERLYING SOURCE CODE.
C**
C**  - ALL MODIFICATIONS TO THIS SOFTWARE MUST BE CLEARLY DOCUMENTED, 
C**    AND ARE SOLELY THE RESPONSIBILITY OF THE AGENT MAKING THE 
C**    MODIFICATIONS.
C**
C**  - IF SIGNIFICANT MODIFICATIONS OR ENHANCEMENTS ARE MADE TO THIS
C**    SOFTWARE, THE FSL SOFTWARE POLICY MANAGER 
C**    (softwaremgr.fsl@noaa.gov) BE NOTIFIED.
C**
C**  THIS SOFTWARE AND ITS DOCUMENTATION ARE IN THE PUBLIC DOMAIN AND 
C**  ARE FURNISHED "AS IS."  THE AUTHORS, THE UNITED STATES GOVERNMENT,
C**  ITS INSTRUMENTALITIES, OFFICERS, EMPLOYEES, AND AGENTS MAKE NO 
C**  WARRANTY, EXPRESS OR IMPLIED, AS TO THE USEFULNESS OF THE SOFTWARE
C**  AND DOCUMENTATION FOR ANY PURPOSE.  THEY ASSUME NO RESPONSIBILITY 
C**  (1) FOR THE USE OF THE SOFTWARE AND DOCUMENTATION; OR (2) TO 
C**  PROVIDE TECHNICAL SUPPORT TO USERS.
C**---------------------------------------------------------------------
