C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MSETWIN     SET MADIS TIME WINDOW
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-14
C
C ABSTRACT: SET TIME WINDOW FOR RETURNING STATION RECORDS
C
C PROGRAM HISTORY LOG:
C   99-09-14  M. BARTH    ORIGINAL VERSION
C   00-03-23  M. BARTH    - MAXIMUM WINDOW CHANGED FROM 1H TO 3H.
C                         - ADDED RECWIN 4 OPTION.
C   01-08-11  M. BARTH    SPECIFIC STATUS CODES FOR EACH ERROR.
C   03-09-22  M. BARTH    CALL MMADISCLOSE ROUTINE INSTEAD OF CALLING
C                         INDIVIDUAL CLOSE ROUTINES.
C
C USAGE:    CALL MSETWIN (BCKMIN,FWDMIN,WINREC,STATUS)
C
C   INPUT ARGUMENT LIST:
C     BCKMIN   - I       NUMBER OF MINUTES RELATIVE TO NOMINAL TIME
C                        AT WHICH TO START TIME WINDOW
C     FWDMIN   - I       NUMBER OF MINUTES RELATIVE TO NOMINAL TIME
C                        AT WHICH TO END TIME WINDOW
C     WINREC   - I       0 - RETURN ALL RECORDS WITHIN *FILE*
C                        1 - RETURN ONE RECORD PER FIXED STATION,
C                            CLOSEST IN TIME TO NOMINAL TIME
C                        2 - RETURN ONE RECORD PER FIXED STATION,
C                            CLOSEST IN TIME TO START OF WINDOW
C                        3 - RETURN ONE RECORD PER FIXED STATION,
C                            CLOSEST IN TIME TO END OF WINDOW
C                        4 - RETURN ALL RECORDS WITHIN *WINDOW*
C
C   OUTPUT ARGUMENT LIST:
C     STATUS   - I       0 - SUCCESS, OR ERROR CODE
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR,MMADISCLOSE
C
C REMARKS:
C   - THE MAXIMUM TIME WINDOW IS 180 MINUTES.
C   - THE WINDOW IS INCLUSIVE (BCKMIN & FWDMIN BOTH INCLUDED).
C   - THE "NOMINAL TIME" IS THE TIME ASKED FOR WHEN READING OBS.
C   - THE DEFAULTS (SET IN MADISCOM) WILL CAUSE ALL RECORDS IN
C     THE FILE CONTAINING THE NOMINAL TIME TO BE OBTAINED:
C
C     BCKMIN 0
C     FWDMIN 0
C     WINREC 0
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MSETWIN(BCKMIN,FWDMIN,WINREC,STATUS)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER BCKMIN,FWDMIN,WINREC,STATUS

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I

C     ------------------------------------------------------------------
C     IF NO SUBSYSTEM HAS BEEN INITIALIZED, RETURN WITH ERROR.
C     ------------------------------------------------------------------
      IF(MADIS_INIT.NE.INIT_P)THEN
         STATUS = SUB_NOTINIT_P
         CALL MERROR('MSETWIN: NO MADIS SUBSYSTEM INITIALIZED')
         RETURN
      ENDIF

C     ------------------------------------------------------------------
C     VALIDATE THE TIME WINDOW.  IF OK, TRANSFER THE ARGUMENTS TO
C     COMMON AND WIPE OUT SOME DATA STRUCTURES SO THAT THE NEW WINDOW
C     WILL TAKE EFFECT WITH THE NEXT READ, AND CLOSE ANY OPEN NETCDF
C     FILES.
C     ------------------------------------------------------------------
      IF(FWDMIN.LT.BCKMIN)THEN

         CALL MERROR('MSETWIN: INVALID TIME WINDOW (MINFWD < MINBCK)')
         STATUS = INV_WIN_P

      ELSE IF(FWDMIN-BCKMIN.GT.180)THEN

         CALL MERROR('MSETWIN: TIME WINDOW TOO LARGE (MORE THAN 3H)')
         STATUS = INV_BIGWIN_P

      ELSE IF(WINREC.LT.0.OR.WINREC.GT.4)THEN

         CALL MERROR('MSETWIN: INVALID WINDOW RECORD TYPE')
         STATUS = INV_RECWIN_P

      ELSE

         MINBCK = BCKMIN
         MINFWD = FWDMIN
         RECWIN = WINREC

         DO 1 I = 0, MAX_DTYPE_P - 1
            LAST_DTYPE_ATIME(I) = '         '
 1       CONTINUE

         CALL MMADISCLOSE

         STATUS = SUCCESS_P

      ENDIF

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