C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MSATWNDFLTR FILTER SATWND OBS BASED ON USER CRITERIA
C   PRGMMR: M. BARTH          ORG: FSL        DATE: 03-09-25
C
C ABSTRACT: FILTER OUT SATWND RECORDS BASED ON USER CRITERIA
C
C PROGRAM HISTORY LOG:
C   03-09-25  M. BARTH      ORIGINAL VERSION
C
C USAGE:    CALL MSATWNDFLTR(ATIME,MINBCK,MINFWD,RECWIN,SPVAL,
C                            PROVDR_ENBL,NUMSTA,OBTIME,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     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 MSATWNDFLTR(ATIME,MINBCK,MINFWD,RECWIN,SPVAL,
     1                       PROVDR_ENBL,NUMSTA,OBTIME,PROVDR,SUBSRC,
     2                       LATS,LONS,P,FNUM,RNUM,NUMSTA_NEW)


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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*9 ATIME
      INTEGER MINBCK
      INTEGER MINFWD
      INTEGER RECWIN
      REAL SPVAL
      LOGICAL PROVDR_ENBL(*)
      INTEGER NUMSTA
      INTEGER OBTIME(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
      INTEGER RECORD(MXSATW_P*4)
      LOGICAL MDOMFLTR
      EXTERNAL MDOMFLTR
C
      NUMSTA_NEW = 0
      IF(NUMSTA.EQ.0)GO TO 900
      RECWINL = RECWIN
      IF(RECWINL.NE.0.AND.RECWINL.NE.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
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     ADD IT IN IF WITHIN THE TIME WINDOW.
C
 40      IF((OBTIME(I).GE.LOW.AND.OBMIN.LE.HIGH.AND.OBTIME(I).NE.SPVAL)
     1      .OR.RECWINL.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
               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**---------------------------------------------------------------------
