C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MWISDOMRDSTA READ WISDOM STATION INFO FROM NETCDF FILE
C   PRGMMR: L. BENJAMIN      ORG: FSL        DATE: 08-07-18
C
C ABSTRACT: INPUT WISDOM STATION INFO FROM A SINGLE, OPEN NETCDF FILE.
C
C PROGRAM HISTORY LOG:
C   08-07-18  L. BENJAMIN ORIGINAL VERSION
C
C USAGE:    CALL MWISDOMRDSTA (SOURCE,NCID,RECDIM,STARTR,NRECS,NDSTA,
C                             OBTIME,STAID,PROVDR,LAT,LON,ELEV,
C                             SUBSRC,STATUS)
C
C   INPUT ARGUMENT LIST:
C     SOURCE   - I       DATASET CODE NUMBER
C     NCID     - I       ID OF THE OPEN NETCDF QC FILE
C     RECDIM   - I       DIMENSION ID OF THE FILE'S RECORD DIMENSION
C     STARTR   - I       STARTING RECORD TO READ
C     NRECS    - I       NUMBER OF RECORDS TO READ
C     NDSTA    - I       SIZE TO USE FOR STATION DIMENSION IN ARRAYS
C
C   OUTPUT ARGUMENT LIST:
C     OBTIME   - IA      TIMES FOR THE OB RECORDS
C     STAID    - CA      STATION NAMES FOR THE OB RECORDS
C     PROVDR   - CA      PROVIDER NAMES FOR THE OB RECORDS
C     LAT      - RA      LATITUDES FOR THE OB RECORDS
C     LON      - RA      LONGITUDES FOR THE OB RECORDS
C     ELEV     - RA      STATION ELEVATION FOR THE OB RECORDS
C     SUBSRC   - IA      SUBSRC NUMBERS FOR THE OB RECORDS
C     STATUS   - I       SUCCESS_P OR ERROR CODE
C
C   INPUT FILES:
C     NETCDF FILE OPEN WITH NCID ID
C
C SUBPROGRAMS CALLED:
C     LIBRARY: - NETCDF LIBRARY:
C                NCAGT
C                NCDINQ
C                NCINQ
C                NCVGT
C                NCVGTC
C                NCVID
C                NCVINQ
C     UNIQUE:  - M_IDSUBSRC
C                M_LSTCHR
C                M_UPDSRCIN
C                MERROR
C
C REMARKS:
C   - SOME OF THE RETURNED ARRAYS ONLY HAVE INFO FOR SOME SOURCES.
C   - CHARACTER ARRAYS ARE BLANK-FILLED TO THE RIGHT AND ONLY THE
C     MAXIMUM SIZE THAT CAN HAVE CHARACTERS ARE ACTUALLY RETURNED
C     (E.G., STAID DIMENSION IS 11 CHAR, BUT LAST ONE IS ZERO-
C     TERMINATOR FOR C-CODE --> WE TURN THIS INTO 10 CHAR).
C   - THERE IS CURRENTLY ONLY ONE WISDOM DATASET (LWISDOM_DS_P).  
C     HOWEVER, THE LOGIC TO HANDLE MULTIPLE DATASETS HAS BEEN 
C     RETAINED IN THE PORT FROM THE SFC VERSION OF THIS ROUTINE
C     FOR HELP IN THE FUTURE IF AND WHEN DATASETS ARE ADDED.
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$






      SUBROUTINE MWISDOMRDSTA(SOURCE,NCID,RECDIM,STARTR,NRECS,NDSTA,
     1                       OBTIME,STAID,PROVDR,LAT,LON,ELEV,
     2                       SUBSRC,STATUS)





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


C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER       SOURCE
      INTEGER       NCID
      INTEGER       RECDIM
      INTEGER       STARTR
      INTEGER       NRECS
      INTEGER       NDSTA
      INTEGER       OBTIME(NDSTA)
      INTEGER       SUBSRC(NDSTA)
      CHARACTER*10  STAID(NDSTA)
      CHARACTER*10  PROVDR(NDSTA)
      REAL          ELEV(NDSTA)
      REAL          LAT(NDSTA)
      REAL          LON(NDSTA)
      INTEGER       STATUS

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER       LSTAID
      PARAMETER     (LSTAID = 10)
      INTEGER       I,J,K,N,NT,CD
      INTEGER       M_LSTCHR
      INTEGER       RCODE
      CHARACTER*(MAXNCNAM) VARNAM,DIMNAM
      INTEGER       VDIMS(MAXVDIMS),DIMSIZ,NVDIMS,LENSTR
      INTEGER       VARTYP,NVATTS,START(MAXVDIMS),COUNT(MAXVDIMS)
      CHARACTER*6   C6(MXWISDOM_P*4)
      CHARACTER*11  C11(MXWISDOM_P*4)
      CHARACTER*15  TIMNAM(3)
      INTEGER       IDLEN,NTIME
      REAL          ELEV_FILL
      REAL          LAT_FILL
      REAL          LON_FILL
      LOGICAL       HAVE_FILL,HAVE_MISS
      DOUBLE PRECISION DTIME(MXWISDOM_P*4,3),DTIME_FILL(3)
      REAL MISVAL
      PARAMETER (MISVAL = -9999.0)

      EQUIVALENCE   (C6,C11,TMP_L2_C1)
      EQUIVALENCE   (DTIME,TMP_L2_N1)

C     *****************************************************************
C     ******************* BEGIN OF MODULE *****************************
C     *****************************************************************

      STATUS = NC_READ_P
      CD = SOURCE - MAX_SFCDSET_P - MAX_UAPTDSET_P - MAX_HYDRODSET_P
     1     - MAX_SATWDSET_P - MAX_SATSDSET_P - MAX_SATRDSET_P
     2     - MAX_SNOWDSET_P

C     ------------------------------------------------------------------
C     FILL IN DATA STRUCTURES NEEDED TO READ MOST OF THE VARIABLES
C     FROM NETCDF FILES, DEPENDING ON THE SOURCE.  ALSO HANDLE ANY
C     SOURCE-SPECIFIC INITIALIZATION.
C     ------------------------------------------------------------------
      TIMNAM(1) = 'observationTime'

      NTIME = 3
      IDLEN = 6
      TIMNAM(2) = 'reportTime'
      TIMNAM(3) = 'receivedTime'

C     ------------------------------------------------------------------
C     GET THE DATA PROVIDER NAMES.
C     ------------------------------------------------------------------
      J = NCVID(NCID,'dataProvider',RCODE)
      IF(RCODE.NE.0)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVID - DPNAME')
         GO TO 900
      ENDIF
      CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVINQ - DPNAME')
         GO TO 900
      ENDIF
      LENSTR = 1
      DO 51 K = 1, NVDIMS
         CALL NCDINQ(NCID,VDIMS(K),DIMNAM,DIMSIZ,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCDINQ - DPNAME')
            GO TO 900
         ENDIF
         IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.VDIMS(K).NE.RECDIM))THEN
            START(K) = 1
            COUNT(K) = DIMSIZ
            LENSTR = LENSTR * DIMSIZ
         ELSE
            START(K) = STARTR
            COUNT(K) = NRECS
            LENSTR = LENSTR * NRECS
         ENDIF
 51   CONTINUE
      CALL NCVGTC(NCID,J,START,COUNT,C11,LENSTR,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVGTC - DPNAME')
         GO TO 900
      ENDIF
      

C     ------------------------------------------------------------------
C     MOVE THE DATA PROVIDER NAMES INTO THE CALLER'S ARRAY, WITH
C     BLANKS AT THE END OF THE STRINGS.
C     ------------------------------------------------------------------
      DO 100 K = 1, NRECS
         N = M_LSTCHR(C11(K),10)
         IF(N.NE.10)THEN
            DO 101 J = N+1, 10
               C11(K)(J:J) = ' '
 101        CONTINUE
         ENDIF
         PROVDR(K) = C11(K)(1:10)
 100  CONTINUE
      

C     ------------------------------------------------------------------
C     READ THE STATION NAMES INTO THE PROPER SIZE CHARACTER ARRAY, PUT
C     BLANKS AT THE END OF THE STRING, THEN TRANSFER TO CALLER'S ARRAY.
C     ------------------------------------------------------------------
      J = NCVID(NCID,
     1          VNAME(STANAM_VN_P,SOURCE)(1:VLEN(STANAM_VN_P,SOURCE)),
     2          RCODE)
      IF(RCODE.NE.0)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVID - STATION NAME')
         GO TO 900
      ENDIF
      CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVINQ - STATION NAME')
         GO TO 900
      ENDIF
      LENSTR = 1
      DO 52 K = 1, NVDIMS
         CALL NCDINQ(NCID,VDIMS(K),DIMNAM,DIMSIZ,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCDINQ - STATION NAME')
            GO TO 900
         ENDIF
         IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.VDIMS(K).NE.RECDIM))THEN
            START(K) = 1
            COUNT(K) = DIMSIZ
            LENSTR = LENSTR * DIMSIZ
         ELSE
            START(K) = STARTR
            COUNT(K) = NRECS
            LENSTR = LENSTR * NRECS
         ENDIF
 52   CONTINUE

      IF(IDLEN .EQ. 6)THEN
         CALL NCVGTC(NCID,J,START,COUNT,C6,LENSTR,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MSFCRDSTA: BAD NCVGTC - STATION NAME')
            GO TO 900
         ENDIF
         DO 63 I = 1, NRECS
            N = M_LSTCHR(C6(I),IDLEN)
            IF(N .NE. IDLEN)THEN
               DO 64 K = N+1, IDLEN
                  C6(I)(K:K) = ' '
 64            CONTINUE
            ENDIF
            STAID(I) = C6(I)(1:6)//'    '
 63      CONTINUE
      ENDIF

C     ------------------------------------------------------------------
C     GET THE LATITUDES AND LONGITUDES.
C     ------------------------------------------------------------------
      J = NCVID(NCID,VNAME(LAT_VN_P,SOURCE)(1:VLEN(LAT_VN_P,SOURCE)),
     1          RCODE)
      IF(RCODE.NE.0)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVID - LATITUDE')
         GO TO 900
      ENDIF
      CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVINQ - LATITUDE')
         GO TO 900
      ENDIF
      DO 8551 K = 1, NVDIMS
         CALL NCDINQ(NCID,VDIMS(K),DIMNAM,DIMSIZ,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCDINQ - LATITUDE')
            GO TO 900
         ENDIF
         IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.VDIMS(K).NE.RECDIM))THEN
            START(K) = 1
            COUNT(K) = DIMSIZ
         ELSE
            START(K) = STARTR
            COUNT(K) = NRECS
         ENDIF
 8551 CONTINUE
      CALL NCVGT(NCID,J,START,COUNT,LAT,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVGT - LATITUDE')
         GO TO 900
      ENDIF
      CALL NCAGT(NCID,J,'_FillValue',LAT_FILL,RCODE)
      IF(RCODE.EQ.SUCCESS_P)THEN
         DO 8552 I = 1, NRECS
            IF(LAT(I).EQ.LAT_FILL)LAT(I) = MISS_P
 8552    CONTINUE
      ENDIF

      J = NCVID(NCID,VNAME(LON_VN_P,SOURCE)(1:VLEN(LON_VN_P,SOURCE)),
     1          RCODE)
      IF(RCODE.NE.0)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVID - LONGITUDE')
         GO TO 900
      ENDIF
      CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVINQ - LONGITUDE')
         GO TO 900
      ENDIF
      DO 9551 K = 1, NVDIMS
         CALL NCDINQ(NCID,VDIMS(K),DIMNAM,DIMSIZ,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCDINQ - LONGITUDE')
            GO TO 900
         ENDIF
         IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.VDIMS(K).NE.RECDIM))THEN
            START(K) = 1
            COUNT(K) = DIMSIZ
         ELSE
            START(K) = STARTR
            COUNT(K) = NRECS
         ENDIF
 9551 CONTINUE
      CALL NCVGT(NCID,J,START,COUNT,LON,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVGT - LONGITUDE')
         GO TO 900
      ENDIF
      CALL NCAGT(NCID,J,'_FillValue',LON_FILL,RCODE)
      IF(RCODE.EQ.SUCCESS_P)THEN
         DO 9552 I = 1, NRECS
            IF(LON(I).EQ.LON_FILL)LON(I) = MISS_P
 9552    CONTINUE
      ENDIF

C     ------------------------------------------------------------------
C     READ IN ELEVATION.
C     ------------------------------------------------------------------
      J = NCVID(NCID,VNAME(ELEV_VN_P,SOURCE)(1:VLEN(ELEV_VN_P,SOURCE)),
     1          RCODE)
      IF(RCODE.NE.0)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVID - ELEVATION')
         GO TO 900
      ENDIF
      CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVINQ - ELEVATION')
         GO TO 900
      ENDIF
      DO 153 K = 1, NVDIMS
         CALL NCDINQ(NCID,VDIMS(K),DIMNAM,DIMSIZ,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCDINQ - ELEVATION')
            GO TO 900
         ENDIF
         IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.VDIMS(K).NE.RECDIM))THEN
            START(K) = 1
            COUNT(K) = DIMSIZ
         ELSE
            START(K) = STARTR
            COUNT(K) = NRECS
         ENDIF
 153  CONTINUE

      CALL NCVGT(NCID,J,START,COUNT,ELEV,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MWISDOMRDSTA: BAD NCVGT - ELEVATION')
         GO TO 900
      ENDIF

      CALL NCAGT(NCID,J,'_FillValue',ELEV_FILL,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         HAVE_FILL = .FALSE.
         ELEV_FILL = 0
      ELSE
         HAVE_FILL = .TRUE.
      ENDIF
      HAVE_MISS = .TRUE.

      DO 154 I = 1, NRECS
         IF((HAVE_FILL.AND.ELEV(I).EQ.ELEV_FILL).OR.
     1      (HAVE_MISS.AND.ELEV(I).EQ.MISVAL))THEN
            ELEV(I) = MISS_P
         ENDIF
 154  CONTINUE

C     ------------------------------------------------------------------
C     UPDATE THE SUBSRCIN TABLE BASED ON THE TABLE CONTAINED IN THE
C     DATA FILE (IF APPLICABLE FOR THE DATASET), THEN TRANSLATE PROVIDER 
C     NAMES INTO SUBSRC NUMBERS.
C     ------------------------------------------------------------------
      CALL M_UPDSRCIN(NCID,RECDIM,STATUS)
      IF(STATUS.NE.SUCCESS_P)GO TO 900

      CALL M_IDSUBSRC(NRECS,PROVDR,SUBSRC,STATUS)

C     ------------------------------------------------------------------
C     READ THE DOUBLE PRECISION TIME & ITS FILL VALUE, IF THE TIME ISN'T
C     MISSING, TRANSLATE TO I4TIME (UNIX TIME + 10 YEARS IN SECONDS),
C     AND TRANSFER TO CALLER'S INTEGER ARRAY.
C     - FOR NON-LDAD-WISDOM DATA WE'LL ALSO FILL IN THE PROVIDER NAME &
C       SUBSRC NUMBER.
C     - FOR LDAD WISDOM DATA WE HAVE TO READ TWO TIME ARRAYS, THEN SORT 
C       OUT THE PROPER VALUE GIVEN THE STATION TYPE.
C     ------------------------------------------------------------------
      DO 500 N = 1, NTIME

         J = NCVID(NCID,TIMNAM(N),RCODE)
         IF(RCODE.NE.0)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCVID - OBTIME')
            GO TO 900
         ENDIF
         CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCVINQ - OBTIME')
            GO TO 900
         ENDIF
         DO 152 K = 1, NVDIMS
            CALL NCDINQ(NCID,VDIMS(K),DIMNAM,DIMSIZ,RCODE)
            IF(RCODE.NE.SUCCESS_P)THEN
               CALL MERROR('MWISDOMRDSTA: BAD NCDINQ - OBTIME')
               GO TO 900
            ENDIF
            IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.VDIMS(K).NE.RECDIM))THEN
               START(K) = 1
               COUNT(K) = DIMSIZ
            ELSE
               START(K) = STARTR
               COUNT(K) = NRECS
            ENDIF
 152     CONTINUE

         CALL NCVGT(NCID,J,START,COUNT,DTIME(1,N),RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCVGT - OBTIME')
            GO TO 900
         ENDIF
         CALL NCAGT(NCID,J,'_FillValue',DTIME_FILL(N),RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MWISDOMRDSTA: BAD NCAGT - OBTIME')
            GO TO 900
         ENDIF

 500  CONTINUE

      DO 600 I = 1, NRECS
         IF(DTIME(I,1) .NE. MISVAL .AND.
     1        DTIME(I,1) .LT. DTIME_FILL(1))THEN
            OBTIME(I) = DTIME(I,1)
            OBTIME(I) = OBTIME(I) - 1325376000
         ELSE
            OBTIME(I) = MISS_P
         ENDIF
 600  CONTINUE


      STATUS = SUCCESS_P

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