C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MSFILES     SPECIFY INPUT FILES TO SATISFY TIME WINDOW
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-14
C
C ABSTRACT: DETERMINE WHICH INPUT FILES ARE NEEDED TO SATISFY THE
C   CURRENT TIME WINDOW FOR ONE DATASET.
C
C PROGRAM HISTORY LOG:
C   99-09-15  M. BARTH    ORIGINAL VERSION
C   00-03-23  M. BARTH    EXTENDED TO UPPER-AIR-POINT DATASETS.
C   01-07-05  M. BARTH    LOGIC NOW GENERAL, NOT DATASET SPECIFIC.
C   03-09-25  M. BARTH    ADDED SATWND DATASETS.
C   04-10-01  L. BENJAMIN ADDED SATSND DATASETS.
C   05-10-27  L. BENJAMIN ADDED SATRAD DATASETS.
C   05-11-18  L. BENJAMIN ADDED SNOW DATASETS.
C   08-07-12  L. BENJAMIN ADDED WISDOM DATASETS.
C
C USAGE:    CALL MSFILES (SOURCE,I4TIME,START_TIME,NFILES)
C
C   INPUT ARGUMENT LIST:
C
C     SOURCE   - I       DATASET CODE NUMBER
C     I4TIME   - I       NOMINAL I4TIME
C
C   OUTPUT ARGUMENT LIST:
C     START_TIME - I     I4TIME OF FIRST FILE TO READ
C     NFILES   - I       NUMBER OF FILES TO READ
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    M_CNVTTM
C
C REMARKS:
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MSFILES(SOURCE,I4TIME,START_TIME,NFILES)

C     ------------------------------------------------------------------
C     INCLUDES AND COMMONS
C     ------------------------------------------------------------------
      INCLUDE 'IMPLICIT'
      INCLUDE 'MADISCON'
      INCLUDE 'MADISCOM'

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER SOURCE,I4TIME,START_TIME,NFILES

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER END_TIME,MINUTES,NYEAR,NMONTH,NDAY,NHOUR,NSEC,INCR,STATUS
      INTEGER CUTOFF,R,S,START_DAY

C     ------------------------------------------------------------------
C     CREATE THE START & END TIMES, THEN DECIDE WHICH FILES WILL CONTAIN
C     THEM BASED ON THE LENGTH OF TIME PUT INTO EACH FILE (INCR) AND
C     THE LAST MINUTE WITHIN THE HOUR TO BE FOUND IN EACH FILE
C     (CUTOFF), AND USE THAT INFO TO CREATE NFILES.
C     ------------------------------------------------------------------
      START_TIME = I4TIME + MINBCK * 60
      END_TIME = I4TIME + MINFWD * 60

      CALL M_CNVTTM(START_TIME,NYEAR,NMONTH,NDAY,NHOUR,MINUTES,NSEC,
     1              STATUS)

C     ------------------------------------------------------------------
C     NOTE THAT WE DON'T HANDLE SECONDS IN I4TIME SO THEY AREN'T
C     SUBTRACTED OUT IN THE FOLLOWING CALCULATIONS.
C     ------------------------------------------------------------------

      IF(SOURCE.LE.MAX_SFCDSET_P)THEN
         INCR = SFCDSET_INCR(SOURCE)
         CUTOFF = SFCDSET_CUTOFF(SOURCE) + 1
      ELSE IF(SOURCE.LE.MAX_SFCDSET_P+MAX_UAPTDSET_P)THEN
         INCR = UAPTDSET_INCR(SOURCE-MAX_SFCDSET_P)
         CUTOFF = UAPTDSET_CUTOFF(SOURCE-MAX_SFCDSET_P) + 1
      ELSE IF(SOURCE.LE.
     1        MAX_SFCDSET_P+MAX_UAPTDSET_P+MAX_HYDRODSET_P)THEN
         INCR = HYDRODSET_INCR(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P)
         CUTOFF = HYDRODSET_CUTOFF(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P)
     1            + 1
      ELSE IF(SOURCE.LE.
     1        MAX_SFCDSET_P+MAX_UAPTDSET_P+MAX_HYDRODSET_P+
     2        MAX_SATWDSET_P)THEN
         INCR = SATWDSET_INCR(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                        MAX_HYDRODSET_P)
         CUTOFF = SATWDSET_CUTOFF(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                            MAX_HYDRODSET_P) + 1
      ELSE IF(SOURCE.LE.
     1        MAX_SFCDSET_P+MAX_UAPTDSET_P+MAX_HYDRODSET_P+
     2        MAX_SATWDSET_P+MAX_SATSDSET_P)THEN
         INCR = SATSDSET_INCR(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                        MAX_HYDRODSET_P-MAX_SATWDSET_P)
         CUTOFF = SATSDSET_CUTOFF(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                            MAX_HYDRODSET_P-MAX_SATWDSET_P) + 1
      ELSE IF(SOURCE.LE.
     1        MAX_SFCDSET_P+MAX_UAPTDSET_P+MAX_HYDRODSET_P+
     2        MAX_SATWDSET_P+MAX_SATSDSET_P+MAX_SATRDSET_P)THEN
         INCR = SATRDSET_INCR(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                        MAX_HYDRODSET_P-MAX_SATWDSET_P-
     2        MAX_SATSDSET_P)
         CUTOFF = SATRDSET_CUTOFF(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                            MAX_HYDRODSET_P-MAX_SATWDSET_P-
     2        MAX_SATSDSET_P) + 1
      ELSE IF(SOURCE.LE.
     1        MAX_SFCDSET_P+MAX_UAPTDSET_P+MAX_HYDRODSET_P+
     2        MAX_SATWDSET_P+MAX_SATSDSET_P+MAX_SATRDSET_P+
     3        MAX_SNOWDSET_P)THEN
         INCR = SNOWDSET_INCR(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                        MAX_HYDRODSET_P-MAX_SATWDSET_P-
     2        MAX_SATSDSET_P-MAX_SATRDSET_P)
         CUTOFF = SNOWDSET_CUTOFF(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                            MAX_HYDRODSET_P-MAX_SATWDSET_P-
     2        MAX_SATSDSET_P-MAX_SATRDSET_P) + 1
      ELSE IF(SOURCE.LE.
     1        MAX_SFCDSET_P+MAX_UAPTDSET_P+MAX_HYDRODSET_P+
     2        MAX_SATWDSET_P+MAX_SATSDSET_P+MAX_SATRDSET_P+
     3        MAX_SNOWDSET_P+MAX_WISDOMDSET_P)THEN
         INCR = WISDOMDSET_INCR(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1                        MAX_HYDRODSET_P-MAX_SATWDSET_P-
     2        MAX_SATSDSET_P-MAX_SATRDSET_P-MAX_SNOWDSET_P)
         CUTOFF = WISDOMDSET_CUTOFF(SOURCE-MAX_SFCDSET_P-MAX_UAPTDSET_P-
     1        MAX_HYDRODSET_P-MAX_SATWDSET_P-
     2        MAX_SATSDSET_P-MAX_SATRDSET_P-MAX_SNOWDSET_P) + 1
      ENDIF

      START_DAY = START_TIME - NHOUR*3600 - MINUTES*60
      S = START_TIME - START_DAY
      R = MOD(S,INCR)
      S = S/INCR
      IF(R.LT.CUTOFF*60)THEN
         START_TIME = START_DAY + S * INCR
      ELSE
         START_TIME = START_DAY + S * INCR + INCR
      ENDIF

      CALL M_CNVTTM(END_TIME,NYEAR,NMONTH,NDAY,NHOUR,MINUTES,NSEC,
     1              STATUS)

      START_DAY = END_TIME - NHOUR*3600 - MINUTES*60
      S = END_TIME - START_DAY
      R = MOD(S,INCR)
      S = S/INCR
      IF(R.LT.CUTOFF*60)THEN
         END_TIME = START_DAY + S * INCR
      ELSE
         END_TIME = START_DAY + S * INCR + INCR
      ENDIF

      NFILES = (END_TIME - START_TIME)/INCR + 1

      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**---------------------------------------------------------------------
