C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MUAPTSTA    GET UPPER-AIR-POINT STATIONS FOR ONE TIME
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 00-03-23
C
C ABSTRACT: READ STATION INFO FOR AN UPPER-AIR-POINT DATASET FOR ONE
C   TIME.
C
C PROGRAM HISTORY LOG:
C   00-03-23  M. BARTH    ORIGINAL VERSION
C   00-06-13  M. BARTH    ADDED ACARS
C   01-04-27  M. BARTH    CHANGES FOR AWIPS-FORMATTED FILES
C   01-07-02  M. BARTH    - ADDED CDBTYPE
C                         - CHANGED MSFILES CALL & INCR USAGE
C                         - CHANGED ACTION WHEN, AFTER FILTERING,
C                           THERE WOULD BE TOO MANY RECORDS TO RETURN
C                           TO THE USER.  BEFORE:  SUBTRACTED THE
C                           DATASET THAT PUT HIM OVER THE TOP; NOW:
C                           RETURN WITH "TOO MANY RECORDS" ERROR.
C                         - STANAM CHANGED FROM 8 TO 10 CHARACTERS
C   02-04-30  M. BARTH    ADDED MAP DATASET & PROVDR ARGUMENT
C   02-06-01  M. BARTH    RETURN "TOO MANY RECORDS" ERROR IF NUMBER
C                         OF FILES NEEDED FOR THE TIME WINDOW IS MORE
C                         THAN THE NUMBER THAT CAN BE USED -- THIS
C                         CHANGE ALLOWS US TO CHANGE THE AMOUNT OF
C                         TIME (AND THEREFORE VOLUME OF RECORDS)
C                         KEPT IN A SINGLE FILE BY ONLY CHANGING
C                         THE TEMPLATE -- ALLOWS ROOM FOR GROWTH.
C   03-07-01  M. BARTH    ADDED RDMTR DATASET
C   03-10-03  M. BARTH    REPLACED NCOPN CALL WITH MMADISOPEN ROUTINE
C   04-06-16  M. BARTH    ADDED ACARSP DATASET
C   04-08-19  M. BARTH    REPLACED NCCLOS CALL WITH MMADISCLS1 ROUTINE
C                         AND PUT THE YYYYMMDD_HHMM PART OF THE 
C                         FILENAMES INTO STRCCOM (WAS LOCAL)
C   06-02-13  L. BENJAMIN REUSED MEMORY FOR LOCAL VARIABLES
C
C USAGE:    CALL MUAPTSTA (ATIME,NSTA,STANAM,WMOID,LAT,LON,ELEV,TIMEOB,
C                          PROVDR,STATUS)
C
C   INPUT ARGUMENT LIST:
C     ATIME    - C       YYJJJHHMM OF THE DATA TO BE READ
C
C   OUTPUT ARGUMENT LIST:
C     NSTA     - I       NUMBER OF STATIONS RETURNED
C     STANAM   - CA      STATION NAMES
C     WMOID    - IA      WMO ID NUMBERS
C     LAT      - RA      LATITUDES (N)
C     LON      - RA      LONGITUDES (E)
C     ELEV     - RA      STATION ELEVATIONS (M)
C     TIMEOB   - CA      YYJJJHHMM OBSERVATION TIMES
C     PROVDR   - CA      DATA PROVIDER NAMES (MAP ONLY)
C     STATUS   - I       0 - SUCCESS, OR ERROR CODE
C
C   INPUT FILES:
C     MADIS UPPER-AIR-POINT NETCDF FILES FOR ATIME
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR, M_MKI4TM, MUAPTCLS, MSFILES, M_MKFNAM,
C                M_MKWFNM, MUAPTRDSTA, MUAPTFLTR, MMADISOPEN,
C                MMADISCLS1
C     NETCDF:    NCINQ, NCDINQ
C
C REMARKS:
C   - THE CALLER MUST CALL MINIT BEFORE CALLING THIS ROUTINE.
C   - THE CALLER SHOULD DIMENSION ARRAYS TO MATCH THE MAXIMUM NUMBER
C     OF LEVELS AND STATIONS POSSIBLE FOR THIS DATATYPE (madiscon.f).
C     IF AFTER TIME & DOMAIN FILTERING THERE ARE MORE THAN THAT
C     MANY RECORDS THIS ROUTINE WILL RETURN WITH THE
C     "TOO MANY RECORDS" ERROR (TOO_MANY_REC_P).  THIS SHOULD ALERT
C     THE USER TO RESTRUCTURE HIS/HER CODE TO CHANGE HOW DATA
C     ARE BEING RETRIEVED:  E.G., USE A SMALLER TIME WINDOW AND
C     CALL THIS ROUTINE TWICE -- GET THE DESIRED RESULT BUT WITHOUT
C     EXCEEDING THE MAXIMUM NUMBER OF RECORDS LEGALLY HANDLED BY
C     THE MADIS CODE.
C   - THIS ROUTINE SHOULD BE CALLED WHENEVER ACCESS TO A NEW TIME
C     IS DESIRED.
C   - FOLLOWING THIS CALL, MGETUAPT OR MGETUAPTC SHOULD BE CALLED TO
C     READ IN WHATEVER OBSERVATIONS ARE DESIRED FOR THIS TIME.  THE
C     INFO STORED IN THE STRCCOM COMMON BY THIS ROUTINE IS USED BY
C     THE OTHER ROUTINES TO FIGURE OUT WHICH RECORDS IT NEEDS ON
C     NETCDF FILES OPENED IN THIS ROUTINE.
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MUAPTSTA (ATIME,NSTA,STANAM,WMOID,LAT,LON,ELEV,TIMEOB,
     1                     PROVDR,STATUS)


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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*9 ATIME,TIMEOB(*)
      CHARACTER*(*) STANAM(*),PROVDR(*)
      INTEGER NSTA,WMOID(*),STATUS
      REAL LAT(*),LON(*),ELEV(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I,J,K,L,I4TIME,NFILES,START_TIME,RCODE
      INTEGER NDIMS,NVARS,NGATTS
      LOGICAL KEEP_GOING
      CHARACTER*9 FTIME
      CHARACTER*31 DUMMY

      INTEGER OBTIME(MXUAPT_P*UAPT_MXSTRF)
      INTEGER FNUML(MXUAPT_P*UAPT_MXSTRF),RNUML(MXUAPT_P*UAPT_MXSTRF)
      CHARACTER*10 STANAML(MXUAPT_P*UAPT_MXSTRF)
      CHARACTER*(LDPNAM_P) PROVDRL(MXUAPT_P*UAPT_MXSTRF)
      INTEGER NSTANEW,WMOIDL(MXUAPT_P*UAPT_MXSTRF)
      INTEGER NLEVELSL(MXUAPT_P*UAPT_MXSTRF)
      INTEGER SUBSRC(MXUAPT_P*UAPT_MXSTRF)
      REAL LATL(MXUAPT_P*UAPT_MXSTRF),LONL(MXUAPT_P*UAPT_MXSTRF)
      REAL ELEVL(MXUAPT_P*UAPT_MXSTRF)
      EQUIVALENCE(STANAML,TMP_L1_C1)
      EQUIVALENCE(PROVDRL,TMP_L1_C1(10*MXUAPT_P*UAPT_MXSTRF+1))
      EQUIVALENCE(LATL,TMP_L1_N1)
      EQUIVALENCE(LONL,TMP_L1_N1(MXUAPT_P*UAPT_MXSTRF+1))
      EQUIVALENCE(ELEVL,TMP_L1_N2)
      EQUIVALENCE(OBTIME,TMP_L1_N2(MXUAPT_P*UAPT_MXSTRF+1))
      EQUIVALENCE(FNUML,TMP_L1_N3)
      EQUIVALENCE(RNUML,TMP_L1_N3(MXUAPT_P*UAPT_MXSTRF+1))

C     ------------------------------------------------------------------
C     IF THE DESIRED SUBSYSTEM HASN'T BEEN INITIALIZED, RETURN WITH
C     ERROR.  OTHERWISE, CLOSE ANY OPEN NETCDF FILES AND INITIALIZE
C     OTHER DATA STRUCTURES.
C     ------------------------------------------------------------------
      IF(CDTYPE.LT.1.OR.CDTYPE.GT.MAX_UAPTDSET_P)THEN
         STATUS = SELSUB_NOTINIT_P
         CALL MERROR('MUAPTSTA: SELECTED SUBSYSTEM NOT INITIALIZED')
         RETURN
      ELSE IF(UAPT_INIT(CDTYPE).NE.INIT_P)THEN
         STATUS = SELSUB_NOTINIT_P
         CALL MERROR('MUAPTSTA: SELECTED SUBSYSTEM NOT INITIALIZED'//
     1               ' - '//UAPTDSET_NAME(CDTYPE))
         RETURN
      ENDIF

      CALL M_MKI4TM(ATIME,I4TIME,STATUS)
      IF(STATUS.NE.SUCCESS_P)THEN
         CALL MERROR('MUAPTSTA: INVALID TIME'//
     1               ' - '//UAPTDSET_NAME(CDTYPE))
         RETURN
      ENDIF
      I = CDTYPE
      CALL MUAPTCLS(I)
      LAST_UAPT_ATIME(I) = '         '
      CDBTYPE = UAPT_DBTYPE(I)
      IF(I.EQ.NPN_DS_P)THEN
         HAVE_NPN_LT = .FALSE.
      ELSE IF(I.EQ.MAP_DS_P)THEN
         HAVE_MAP_LT = .FALSE.
      ENDIF
      UAPT_NFILES(I) = 0
      UAPT_LNUM(I) = 0
      KEEP_GOING = .TRUE.

C     ------------------------------------------------------------------
C     DETERMINE HOW MANY FILES NEED TO BE READ IN TO SATISFY THE TIME
C     WINDOW.
C     ------------------------------------------------------------------
      CALL MSFILES(MAX_SFCDSET_P+I,I4TIME,START_TIME,NFILES)

      IF((I.NE.RAOB_DS_P.AND.NFILES.GT.UAPT_MXSTRF).OR.
     1   (I.EQ.RAOB_DS_P.AND.NFILES.GT.RAOB_MXSTRF))THEN
         NSTA = 0
         STATUS = TOO_MANY_REC_P
         CALL MERROR('MUAPTSTA: TOO MANY RECORDS REQUESTED')
         RETURN
      ENDIF

      K = 1

C     ------------------------------------------------------------------
C     LOOP FOR ALL FILES IN THE DATASET:
C     - CREATE THE FILENAME.
C     - OPEN THE FILE AND GET THE NO. OF RECORDS, STORE NCID & NREC.
C       IN COMMON FOR SUBSEQUENT READS.
C     - READ IN THE STATION INFO INTO LOCAL ARRAYS.
C     ------------------------------------------------------------------
      DO 5 J = 1, NFILES

         UAPT_NFILES(I) = UAPT_NFILES(I) + 1
         CALL M_MKFNAM(START_TIME,FTIME,STATUS)
         START_TIME = START_TIME + UAPTDSET_INCR(I)

         CALL M_MKWFNM(FTIME,UAPT_FNAME(UAPT_NFILES(I),I),STATUS)
         CALL MMADISOPEN(UAPTDIR(I),UAPTDIRLEN(I),
     1                   UAPT_FNAME(UAPT_NFILES(I),I),
     2                   UAPT_NCID(UAPT_NFILES(I),I),RCODE)
         IF(RCODE.NE.0)THEN
            UAPT_NCID(UAPT_NFILES(I),I) = -1
            GO TO 10
         ENDIF

         CALL NCINQ(UAPT_NCID(UAPT_NFILES(I),I),NDIMS,NVARS,NGATTS,
     1              UAPT_RECDIM(UAPT_NFILES(I),I),RCODE)
         IF(RCODE.NE.0)GO TO 10
         CALL NCDINQ(UAPT_NCID(UAPT_NFILES(I),I),
     1               UAPT_RECDIM(UAPT_NFILES(I),I),DUMMY,
     2               UAPT_NRECS(UAPT_NFILES(I),I),RCODE)
         IF(RCODE.NE.0.OR.UAPT_NRECS(UAPT_NFILES(I),I).EQ.0)GO TO 10

C     ------------------------------------------------------------------
C     IF WE'VE SOMEHOW EXCEEDED THE DIMENSION OF OUR ARRAYS, DON'T
C     TRY TO READ ANYMORE, BUT DO RETURN WITH WHAT WE'VE ALREADY GOT.
C     ------------------------------------------------------------------
         IF(K-1+UAPT_NRECS(UAPT_NFILES(I),I).GT.
     1      MXUAPT_P*UAPT_MXSTRF)THEN

            KEEP_GOING = .FALSE.
            CALL MMADISCLS1(UAPT_NCID(UAPT_NFILES(I),I),UAPTDIR(I),
     1                      UAPTDIRLEN(I),UAPT_FNAME(UAPT_NFILES(I),I))
            UAPT_NCID(UAPT_NFILES(I),I) = -1
            UAPT_NFILES(I) = UAPT_NFILES(I) - 1
            GO TO 100
         ENDIF

         CALL MUAPTRDSTA(I+MAX_SFCDSET_P,UAPT_NCID(UAPT_NFILES(I),I),
     1                   UAPT_NFILES(I),UAPT_RECDIM(UAPT_NFILES(I),I),1,
     2                   UAPT_NRECS(UAPT_NFILES(I),I),
     3                   MXUAPT_P*UAPT_MXSTRF,OBTIME(K),STANAML(K),
     4                   WMOIDL(K),PROVDRL(K),LATL(K),LONL(K),ELEVL(K),
     5                   SUBSRC(K),NLEVELSL(K),STATUS)
         IF(STATUS.EQ.SUCCESS_P)THEN

C     ------------------------------------------------------------------
C     PUT THE FILE AND RECORD NUMBERS INTO COMMON AND UPDATE K FOR NEXT
C     FILE IN THIS DATASET.
C     ------------------------------------------------------------------
            DO 4 L = K, K + UAPT_NRECS(UAPT_NFILES(I),I) - 1
               FNUML(L) = UAPT_NFILES(I)
               RNUML(L) = L - K + 1
 4          CONTINUE
            K = K + UAPT_NRECS(UAPT_NFILES(I),I)
            GO TO 5
         ENDIF

C     ------------------------------------------------------------------
C     ERROR WITH THE NETCDF FILE.  CLOSE IT (IF OPEN) AND RELEASE ITS
C     SLOT IN THE COMMON.
C     ------------------------------------------------------------------
 10      IF(UAPT_NCID(UAPT_NFILES(I),I).GE.0)
     1      CALL MMADISCLS1(UAPT_NCID(UAPT_NFILES(I),I),UAPTDIR(I),
     2                      UAPTDIRLEN(I),UAPT_FNAME(UAPT_NFILES(I),I))
         UAPT_NCID(UAPT_NFILES(I),I) = -1
         UAPT_NFILES(I) = UAPT_NFILES(I) - 1

 5    CONTINUE

C     ------------------------------------------------------------------
C     IF WE HAVE ANYTHING FOR THIS DATASET, FILTER RECS WE DON'T WANT:
C     - MISSING LAT, LON OR ELEV (EXCEPT ACARS WHICH DEAL WITH THESE
C       ITEMS AS DATA VARIABLES)
C     - OUTSIDE THE TIME WINDOW
C     - MULTIPLES WITHIN THE TIME WINDOW (IF RECWIN SO INDICATES)
C     ------------------------------------------------------------------
 100  K = K - 1
      IF(K.EQ.0)GO TO 900

      IF(I.EQ.MAP_DS_P)THEN
         J = 3
      ELSE 
         J = 4
      ENDIF
      CALL MUAPTFLTR(ATIME,MINBCK,MINFWD,RECWIN,I,MISS_P,
     1               PROVDR_ENBL(1,J),K,OBTIME,STANAML,WMOIDL,
     2               PROVDRL,SUBSRC,LATL,LONL,ELEVL,NLEVELSL,FNUML,
     3               RNUML,NSTANEW)

C     ------------------------------------------------------------------
C     THE CALLER IS EXPECTED TO HAVE THE MAXIMUM NUMBER OF RECORDS 
C     POSSIBLE FOR THE PARTICULAR DATASET AVAILABLE.  IF THIS WOULD PUT 
C     HIM OVER THE TOP, RETURN WITH AN ERROR (SEE REMARKS SECTION 
C     ABOVE).
C     ------------------------------------------------------------------
      IF(I.EQ.RAOB_DS_P)THEN
         J = MXRAOB_P
      ELSE IF(I.EQ.ACARS_DS_P)THEN
         J = MXACARS_P
      ELSE IF(I.EQ.NPN_DS_P)THEN
         J = MXNPN_P
      ELSE IF(I.EQ.MAP_DS_P)THEN
         J = MXMAP_P
      ELSE IF(I.EQ.RDMTR_DS_P)THEN
         J = MXRDMTR_P
      ELSE IF(I.EQ.ACARSP_DS_P)THEN
         J = MXACARSP_P
      ENDIF
      IF(NSTANEW+UAPT_LNUM(I).GT.J)THEN
         NSTA = 0
         LAST_UAPT_ATIME(I) = '         '
         IF(I.EQ.NPN_DS_P)THEN
            HAVE_NPN_LT = .FALSE.
         ELSE IF(I.EQ.MAP_DS_P)THEN
            HAVE_MAP_LT = .FALSE.
         ENDIF
         STATUS = TOO_MANY_REC_P
         CALL MERROR('MUAPTSTA: TOO MANY RECORDS REQUESTED')
         RETURN
      ENDIF

C     ------------------------------------------------------------------
C     TRANSFER REMAINING RECORDS TO CALLER'S ARRAYS (AND NLEVELS TO
C     COMMON), AND UPDATE INFO IN COMMON.
C     ------------------------------------------------------------------
      DO 200 J = 1, NSTANEW
         K = UAPT_LNUM(I) + J
         STANAM(K) = STANAML(J)
         IF(I.NE.ACARS_DS_P)THEN
            WMOID(K) = WMOIDL(J)
            ELEV(K) = ELEVL(J)
         ENDIF
         LAT(K) = LATL(J)
         LON(K) = LONL(J)
         IF(I.EQ.MAP_DS_P.OR.I.EQ.RDMTR_DS_P)PROVDR(K) = PROVDRL(J)
         CALL M_MKFNAM(OBTIME(J),TIMEOB(K),STATUS)

         UAPT_FNUM(K,I) = FNUML(J)
         UAPT_RNUM(K,I) = RNUML(J)

 200  CONTINUE

      UAPT_LNUM(I) = UAPT_LNUM(I) + NSTANEW

C     ------------------------------------------------------------------
C     IF WE GOT ANYTHING AT ALL, TELL CALLER HOW MANY STATIONS AND
C     UPDATE THE ATIME KEPT IN COMMON SO SUBSEQUENT READS FOR THE
C     SAME TIME WILL USE THE FILE/RECORD INFO WE JUST WORKED SO HARD
C     TO CREATE...
C     ------------------------------------------------------------------
 900  IF(UAPT_LNUM(I).GT.0)THEN
         NSTA = UAPT_LNUM(I)
         LAST_UAPT_ATIME(I) = ATIME
         STATUS = SUCCESS_P
      ELSE
         NSTA = 0
         LAST_UAPT_ATIME(I) = '         '
         IF(I.EQ.NPN_DS_P)THEN
            HAVE_NPN_LT = .FALSE.
         ELSE IF(I.EQ.MAP_DS_P)THEN
            HAVE_MAP_LT = .FALSE.
         ENDIF
         STATUS = NO_DATA_P
         CALL MERROR('MUAPTSTA: NO DATA FOR '//ATIME//
     1               ' - '//UAPTDSET_NAME(CDTYPE))
      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**---------------------------------------------------------------------
