C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MSATSSTA     GET SATSND STATIONS FOR ONE TIME
C   PRGMMR: L. BENJAMIN      ORG: FSL        DATE: 04-09-27
C
C ABSTRACT: READ STATION INFO FOR SATSND DATASETS FOR ONE TIME.
C
C PROGRAM HISTORY LOG:
C   04-09-27  L. BENJAMIN ORIGINAL VERSION
C   05-10-25  L. BENJAMIN COMMENT CHANGE
C   06-02-13  L. BENJAMIN CHANGED MEMORY MAP LOCATION
C
C USAGE:    CALL MSATSNDSTA (ATIME,NSTA,PROVDR,LAT,LON,ELEV,TIMEOB,
C                            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     PROVDR   - CA      DATA PROVIDER NAMES
C     LAT      - RA      LATITUDES (N)
C     LON      - RA      LONGITUDES (E)
C     ELEV     - RA      ELEVATION (M)
C     TIMEOB   - CA      YYJJJHHMM OBSERVATION TIMES
C     STATUS   - I       0 - SUCCESS, OR ERROR CODE
C
C   INPUT FILES:
C     MADIS SATSND NETCDF FILES FOR ATIME
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR, M_MKI4TM, MSATSNDCLS, MSFILES, M_MKFNAM, 
C                M_MKWFNM, MSATSNDRDSTA, MSATSNDFLTR, MMADISOPEN
C     NETCDF:    NCINQ, NCDINQ, NCCLOS
C
C REMARKS:
C   - THE CALLER MUST CALL MINIT BEFORE CALLING THIS ROUTINE.
C   - THE CALLER SHOULD DIMENSION ARRAYS TO MATCH MXSATS_P IN
C     madiscon.f.  IF AFTER TIME & DOMAIN FILTERING THERE ARE MORE
C     THAN MXSATS_P 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, MGETSATSND SHOULD BE CALLED TO READ
C     IN WHATEVER OBSERVATIONS ARE DESIRED FOR THIS TIME.  THE INFO
C     STORED IN THE STRCCOM COMMON BY THIS ROUTINE IS USED BY THE OTHER
C     ROUTINES TO FIGURE OUT WHICH RECORDS IT NEEDS ON NETCDF FILES
C     OPENED IN THIS ROUTINE.
C   - THE MADISCOM PROVIDER ARRAYS (NPVD, PVDNAME, ETC.) HAVE SATSND
C     PROVIDERS IN ROW 6.
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MSATSNDSTA (ATIME,NSTA,PROVDR,LAT,LON,ELEV,TIMEOB,
     1     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*(*) PROVDR(*)
      INTEGER NSTA,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*13 FNAME
      CHARACTER*31 DUMMY

      INTEGER OBTIME(MXSATS_P*4),FNUML(MXSATS_P*4),RNUML(MXSATS_P*4)
      CHARACTER*(LDPNAM_P) PROVDRL(MXSATS_P*4)
      INTEGER NSTANEW,SUBSRC(MXSATS_P*4)
      REAL LATL(MXSATS_P*4),LONL(MXSATS_P*4),ELEVL(MXSATS_P*4)

      EQUIVALENCE(PROVDRL,TMP_L1_C1)
      EQUIVALENCE(LATL,TMP_L1_N1)
      EQUIVALENCE(LONL,TMP_L1_N2)
      EQUIVALENCE(ELEVL,TMP_L1_N3)

C     -----------------------------------------------------------------
C     IF THE SATSND SUBSYSTEM HASN'T BEEN INITIALIZED, RETURN WITH 
C     ERROR.  OTHERWISE, CLOSE ANY OPEN NETCDF FILES AND INITIALIZE 
C     OTHER DATA STRUCTURES.
C     -----------------------------------------------------------------
      IF(SATS_INIT.NE.INIT_P)THEN
         STATUS = SELSUB_NOTINIT_P
         CALL MERROR('MSATSNDSTA: SELECTED SUBSYSTEM NOT INITIALIZED '
     1        //'- SATSND')
         RETURN
      ENDIF

      CALL M_MKI4TM(ATIME,I4TIME,STATUS)
      IF(STATUS.NE.SUCCESS_P)THEN
         CALL MERROR('MSATSNDSTA: INVALID TIME')
         RETURN
      ENDIF
      CALL MSATSNDCLS
      LAST_SATS_ATIME = '         '
      SATS_NFILES = 0
      SATS_LNUM = 0
      KEEP_GOING = .TRUE.
      CDTYPE = SATS_DS_P
      CDBTYPE = SATS_DBTYPE

C     -----------------------------------------------------------------
C     LOOP THROUGH THE DATASETS.
C     -----------------------------------------------------------------
      DO 1 I = 1, MAX_SATSDSET_P

C     -----------------------------------------------------------------
C     DETERMINE HOW MANY FILES NEED TO BE READ IN TO SATISFY THE TIME
C     WINDOW.
C     -----------------------------------------------------------------
         IF(SATSDSET_ENBL(I))THEN

            CALL MSFILES(MAX_SFCDSET_P+MAX_UAPTDSET_P+MAX_HYDRODSET_P+
     1                   MAX_SATWDSET_P+I,I4TIME,START_TIME,NFILES)

            IF(NFILES.GT.SATS_MXSTRF/MAX_SATSDSET_P)THEN
               NSTA = 0
               LAST_SATS_ATIME = '         '
               STATUS = TOO_MANY_REC_P
               CALL MERROR('MSATSNDSTA: 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

               SATS_NFILES = SATS_NFILES + 1
               CALL M_MKFNAM(START_TIME,FTIME,STATUS)
               START_TIME = START_TIME + SATSDSET_INCR(I)

               CALL M_MKWFNM(FTIME,FNAME,STATUS)

               CALL MMADISOPEN(SATSDIR(I),SATSDIRLEN(I),FNAME,
     1                         SATS_NCID(SATS_NFILES),RCODE)
               IF(RCODE.NE.0)THEN
                  SATS_NCID(SATS_NFILES) = -1
                  GO TO 10
               ENDIF

               CALL NCINQ(SATS_NCID(SATS_NFILES),NDIMS,NVARS,NGATTS,
     1                    SATS_RECDIM(SATS_NFILES),RCODE)
               IF(RCODE.NE.0)GO TO 10
               CALL NCDINQ(SATS_NCID(SATS_NFILES),
     1                     SATS_RECDIM(SATS_NFILES),DUMMY,
     2                     SATS_NRECS(SATS_NFILES),RCODE)
               IF(RCODE.NE.0.OR.SATS_NRECS(SATS_NFILES).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     NOTE THAT WE DON'T DECLARE A FATAL ERROR, AS WE DO WHEN THE
C     POST-FILTERED AMOUNT IS GREATHER THAN MXSATS_P.  THIS IS BECAUSE
C     THIS ERROR WOULD BE A RESULT OF INTERNAL LOGIC PROBLEMS AND
C     THE USER WHO STUMBLES ACROSS IT SHOULDN'T BE EXPECTED TO REWRITE
C     HIS CODE TO COMPENSATE...
C     -----------------------------------------------------------------
               IF(K-1+SATS_NRECS(SATS_NFILES).GT.MXSATS_P*4)THEN
                  KEEP_GOING = .FALSE.
                  CALL NCCLOS(SATS_NCID(SATS_NFILES),RCODE)
                  SATS_NCID(SATS_NFILES) = -1
                  SATS_NFILES = SATS_NFILES - 1
                  GO TO 100
               ENDIF

               CALL MSATSNDRDSTA(I+MAX_SFCDSET_P+MAX_UAPTDSET_P+
     1                           MAX_HYDRODSET_P+MAX_SATWDSET_P,
     1                           SATS_NCID(SATS_NFILES),
     2                           SATS_RECDIM(SATS_NFILES),1,
     3                           SATS_NRECS(SATS_NFILES),MXSATS_P*4,
     4                           OBTIME(K),PROVDRL(K),LATL(K),LONL(K),
     5                           ELEVL(K),SUBSRC(K),STATUS)
               IF(STATUS.EQ.SUCCESS_P)THEN

C     -----------------------------------------------------------------
C     PUT THE FILE AND RECORD NUMBERS, AND THE SOURCE, INTO COMMON AND
C     UPDATE K FOR NEXT FILE IN THIS DATASET.
C     -----------------------------------------------------------------
                  DO 4 L = K, K + SATS_NRECS(SATS_NFILES) - 1
                     FNUML(L) = SATS_NFILES
                     RNUML(L) = L - K + 1
 4                CONTINUE
                  K = K + SATS_NRECS(SATS_NFILES)
                  SATS_SOURCE(SATS_NFILES) = 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(SATS_NCID(SATS_NFILES).GE.0)
     1            CALL NCCLOS(SATS_NCID(SATS_NFILES),RCODE)
               SATS_NCID(SATS_NFILES) = -1
               SATS_NFILES = SATS_NFILES - 1

 5          CONTINUE

C     -----------------------------------------------------------------
C     IF WE HAVE ANYTHING FOR THIS DATASET, FILTER RECS WE DON'T WANT:
C     - MISSING LAT, LON OR P
C     - OUTSIDE THE TIME WINDOW
C     - PROVIDERS THAT HAVE BEEN DISABLED BY MSETPVDR
C     -----------------------------------------------------------------
 100        K = K - 1
            IF(K.EQ.0)THEN
               IF(KEEP_GOING)THEN
                  GO TO 1
               ELSE
                  GO TO 900
               ENDIF
            ENDIF

            CALL MSATSNDFLTR(ATIME,MINBCK,MINFWD,RECWIN,MISS_P,
     1                       PROVDR_ENBL(1,6),K,OBTIME,PROVDRL,SUBSRC,
     2                       LATL,LONL,ELEVL,FNUML,RNUML,NSTANEW)

C     -----------------------------------------------------------------
C     THE CALLER IS EXPECTED TO HAVE MXSATS_P RECORDS AVAILABLE.  IF
C     THIS WOULD PUT HIM OVER THE TOP, RETURN WITH AN ERROR (SEE
C     REMARKS SECTION ABOVE).
C     -----------------------------------------------------------------
            IF(NSTANEW+SATS_LNUM.GT.MX3SATS_P)THEN
               NSTA = 0
               LAST_SATS_ATIME = '         '
               STATUS = TOO_MANY_REC_P
               CALL MERROR('MSATSNDSTA: TOO MANY RECORDS REQUESTED')
               RETURN
            ENDIF

C     -----------------------------------------------------------------
C     TRANSFER REMAINING RECORDS TO CALLER'S ARRAYS, AND UPDATE INFO
C     IN COMMON.
C     -----------------------------------------------------------------
            DO 200 J = 1, NSTANEW
               K = SATS_LNUM + J
               PROVDR(K) = PROVDRL(J)
               LAT(K) = LATL(J)
               LON(K) = LONL(J)
               ELEV(K) = ELEVL(J)
               CALL M_MKFNAM(OBTIME(J),TIMEOB(K),STATUS)
               SATS_FNUM(K) = FNUML(J)
               SATS_RNUM(K) = RNUML(J)

 200        CONTINUE

            SATS_LNUM = SATS_LNUM + NSTANEW

         ENDIF

         IF(.NOT.KEEP_GOING)GO TO 900

 1    CONTINUE

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(SATS_LNUM.GT.0)THEN
         NSTA = SATS_LNUM
         LAST_SATS_ATIME = ATIME
         STATUS = SUCCESS_P
      ELSE
         NSTA = 0
         LAST_SATS_ATIME = '         '
         STATUS = NO_DATA_P
         CALL MERROR('MSATSNDSTA: NO DATA FOR '//ATIME)
      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**--------------------------------------------------------------------
