C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MRDNUM1     READ A NUMERIC NETCDF VARIABLE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-16
C
C ABSTRACT: READ A NUMERIC NETCDF VARIABLE ON AN OPEN NETCDF FILE.
C
C PROGRAM HISTORY LOG:
C   02-04-24  M. BARTH    ORIGINAL VERSION -- COPIED FROM MADISLIB'S
C                         MRDNUM SUBROUTINE.  THE ONLY DIFFERENCE
C                         IS THIS ROUTINE RETURNS THE NUMBER OF
C                         ELEMENTS READ IN.  
C   02-05-07  M. BARTH    CHECK FOR -9999 UNDOCUMENTED MISSING
C                         VALUES IN AWIPS MAP DATA.
C   02-05-29  M. BARTH    RESTRUCTURED TEMPORARY MEMORY ARRAY USAGE
C   02-07-29  M. BARTH    KEEP NAN'S OUT OF VARIABLES FOR FILLVALS.
C   03-10-10  M. BARTH    - CHECK FOR -99999 IN ALL AWIPS DATABASE
C                           FILES.
C                         - CHECK FOR 99999 UNDOCUMENTED MISSING
C                           VALUE IN ALL FSL AWIPS VARIABLES...
C   05-04-18  L. BENJAMIN REMOVED WRITING ERROR MESSAGE FOR
C                         QCR AND QCA "VAR_NOTINDB_P" CASES.
C   08-06-13  L. BENJAMIN ADDED MRDNUM LOGIC INTO ROUTINE AND MOVED
C                         IT BACKED INTO API.  REMOVED NAMED 
C                         DATASETS (*DS_P). ADDED MISS_AWIPS_VAL2 
C                         AND MISS_ACARS_VAL2 GLOBAL VARIABLES
C                         TO HANDLE ADDITIONAL MISSING VARIABLES
C                         VALUES.  MOVED THE RESCALING OF AWIPS
C                         SATS TO MRDNUM AND MGTNUM ROUTINES.  
C  
C USAGE:    CALL MRDNUM1 (VRNAM,NCID,RECDIM,STARTR,NRECS,SPVAL,
C                        VDATA,STATUS)
C
C   INPUT ARGUMENT LIST:
C     VRNAM    - C       NETCDF VARIABLE NAME
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     SPVAL    - R       DESIRED MISSING DATA FLAG
C
C   OUTPUT ARGUMENT LIST:
C     VDATA    - RA      REAL ARRAY WITH DATA AND SPVALS FILLED IN
C     N        - I       NUMBER OF ELEMENTS READ INTO VDATA
C     STATUS   - I       SUCCESS_P OR ERROR CODE
C
C   INPUT FILES:
C     WHATEVER NETCDF FILE'S OPEN ON NCID.
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR, M_LSTCHR
C     NETCDF:    NCVID, NCVINQ, NCDINQ, NCVGT, NCAGT, NCAINQ
C
C REMARKS:
C   - THE EXTENDED FORTRAN BYTE DATA TYPE IS USED.  IF THIS DOESN'T
C     COMPILE ON YOUR MACHINE, SEE IF INTEGER*1 IS AVAILABLE
C     AND CHANGE THIS ROUTINE TO USE THAT INSTEAD (THE PROBLEM
C     EXISTS WITH IRIX FORTRAN 90, AND THIS WILL SOLVE IT.)
C   - THIS ROUTINE IS ASSUMED TO BE CALLED BY A HIGHER-LEVEL MADIS
C     ROUTINE, SO NO ERROR CHECKING IS DONE ON THE INPUT VARIABLES.
C   - WHATEVER THE DATA TYPE OF THE VARIABLE, IT'S CONVERTED TO REAL.
C     THEREFORE, THIS ROUTINE SHOULDN'T BE USED FOR DOUBLE PRECISION
C     VARIABLES.
C   - THE TEST FOR -999 AS A MISSING VALUE IS NEEDED BECAUSE SOME
C     MESONET VARIABLES MISTAKENLY HAVE THIS VALUE (INSTEAD OF
C     THE LEGITIMATE MISSING_VALUE ATTRIBUTE).  ALSO NOTE THAT
C     AS THERE ARE NO BYTE DATA TYPES IN MESONET FILES WHERE THIS
C     BUG MAY BE EXTANT, WE DON'T BOTHER WITH THIS LOGIC IN THE
C     BYTE HANDLING BELOW.
C   - YET ANOTHER MESONET BUG CAUSES THE FUNKY USE OF DMISS BELOW.
C     THE MISSING_VALUE ATTRIBUTE MENTIONED IN THE PREVIOUS
C     REMARK ISN'T QUITE AS "LEGITIMATE" AS IT OUGHT TO BE...
C     INSTEAD, FOR FLOAT TYPES IT HAS BEEN SPECIFIED WITH A DOUBLE
C     PRECISION CONSTANT WHEN IT SHOULD BE A FLOAT TYPE.  THE
C     CODE IN THIS ROUTINE SHOULD HANDLE EITHER VERSION.
C   - LOCAL ARRAYS ARE DIMENSIONED TO MXDATA_P.  IF THIS NUMBER
C     PROVES TOO LARGE FOR A GIVEN COMPUTER, THEN PORTIONS OF THE
C     MADIS API (E.G., RAOB) THAT RELY ON THIS VOLUME OF DATA
C     BEING HANDLED IN A SINGLE NETCDF CALL MAY NEED TO BE
C     RESTRUCTURED!
C   - SEE madistmp.f FOR AN EXPLANATION OF HOW TEMPORARY STORAGE
C     IS HANDLED (ALL ARRAYS NAMED *TMP*).
C   - THE TEST FOR -99999 IN ALL AWIPS DATABASE FILES IS TO
C     COMPENSATE FOR THE COMMONLY-USED, UNDOCUMENTED MISSING DATA
C     FLAG THAT SHOWS UP ALL THE TIME IN AWIPS FILES...
C   - SOME DATA SETS HAVE ANOTHER AWIPS MISSING VARIABLE.  THE CALLER
C     IS RESPONSIBLE FOR SETTING THE GLOBAL MISS_AWIPS_VAL2 VARIABLE
C     FOR THESE CASES.  ALWAYS SET THE MISS_AWIPS_VAL2 IF IT USED
C     OR NOT.  MINIT SETS ITS VALUE.
C   - ACARS ALSO CAN HAVE ANOTHER MISSING VARIABLE.  THE CALLER
C     IS RESPONSIBLE FOR SETTING THE GLOBAL MISS_ACARS_VAL2 VARIABLE
C     FOR THESE CASES.  ALWAYS SET THE MISS_ACARS_VAL2 IF IT USED
C     OR NOT.  MINIT SETS ITS VALUE.
C   - FOR AWIPS'S API SATELLITE SOUNDING USERS, DO NOT CALL THIS 
C     ROUTINE DIRECTLY, CALL MRDNUM OR MGTNUM, AND INITIALIZE SETTING 
C     WITH MINIT.  THE LOGIC TO CONDENSE THE LEVELS TO 40 IS IN THOSE 
C     ROUTINES. 
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MRDNUM1(VRNAM,NCID,RECDIM,STARTR,NRECS,SPVAL,VDATA,
     1     MXDATA,RDATA,IDATA,I2DATA,BDATA,N,STATUS)

      


C     ------------------------------------------------------------------
C     INCLUDES AND COMMONS
C     ------------------------------------------------------------------
      INCLUDE 'IMPLICIT'
      INCLUDE 'INTQC1COM1'
      INCLUDE 'INTQC1CON2'
      INCLUDE 'NETCDFI'

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*(*) VRNAM
      INTEGER NCID,RECDIM,STARTR,NRECS,STATUS,N
      REAL SPVAL,VDATA(*)
      INTEGER MXDATA
      REAL RDATA(MXDATA)
      INTEGER IDATA(MXDATA)
      INTEGER*2 I2DATA(MXDATA)
      BYTE BDATA(MXDATA)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      CHARACTER*(MAXNCNAM) VARNAM,DIMNAM,ERRVAR
      INTEGER I,J,K,L,RCODE,VARTYP,NVDIMS,VDIMS(MAXVDIMS),NVATTS,LN
      INTEGER DIMSIZ,START(MAXVDIMS),COUNT(MAXVDIMS),ATRTYP,M_LSTCHR
      REAL MISVAL1
      PARAMETER (MISVAL1 = -999.0)
      REAL AWIPSMISS
      PARAMETER (AWIPSMISS = -99999.0)
      LOGICAL HAVE_FILL,HAVE_MISS
      REAL RFILL
      INTEGER IFILL
      INTEGER*2 I2FILL
      BYTE BFILL
      EQUIVALENCE (RFILL,IFILL,I2FILL,BFILL)
      REAL RMISS
      INTEGER IMISS,I40,I50
      INTEGER*2 I2MISS
      BYTE BMISS
      DOUBLE PRECISION DMISS
      EQUIVALENCE (RMISS,IMISS,I2MISS,BMISS,DMISS)
      INTEGER AWIPSMISVAL2
      REAL ACARSMISVAL2
      
C     ------------------------------------------------------------------
C     TEMPORARY STORAGE DEFINITIONS
C     ------------------------------------------------------------------

C     GET MISSING VALUE FROM COMMON IN INTEGER FORMAT
      AWIPSMISVAL2 = MISS_AWIPS_VAL2
C     GET MISSING VALUE FOR ACARS DATA FROM COMMON IN REAL FORMAT
      ACARSMISVAL2 =  MISS_ACARS_VAL2


      STATUS = NC_READ_P
      N = 0

C     ------------------------------------------------------------------
C     GET THE VARIABLE ID FROM ITS NAME.  INQUIRE ABOUT ITS TYPE AND
C     DIMENSIONS.  FILL IN THE DIMENSIONS OF THE HYPERSLAB TO BE READ.
C     READ IT.  IF SUCCESSFUL, GET ITS FILL & MISSING ATTRIBUTES,
C     AND FILL IN THE CALLER'S ARRAY.
C     ------------------------------------------------------------------
      J = LEN(VRNAM)
      L = M_LSTCHR(VRNAM,J)
      ERRVAR = VRNAM(1:L)
      J = NCVID(NCID,VRNAM,RCODE)
      IF(RCODE.NE.0)THEN
         IF(RCODE.EQ.NCENOTVR)THEN
            STATUS = VAR_NOTINDB_P
            IF(L.LT.4.OR.(ERRVAR(L-2:L).NE.'QCA'.AND.
     1           ERRVAR(L-2:L).NE.'QCR'))THEN
               CALL MERROR('MRDNUM1: VARIABLE NOT IN SELECTED DATABASE')
            ENDIF
         ELSE
            CALL MERROR('MRDNUM1: BAD NCVID - '//ERRVAR(1:L))
         ENDIF
         GO TO 900
      ENDIF
      CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MRDNUM1: BAD NCVINQ - '//ERRVAR(1:L))
         GO TO 900
      ENDIF
      IF(VARTYP.NE.NCBYTE.AND.VARTYP.NE.NCSHORT.AND.
     1   VARTYP.NE.NCLONG.AND.VARTYP.NE.NCFLOAT)THEN
         CALL MERROR('MRDNUM1: BAD VARIABLE TYPE '//ERRVAR(1:L))
         GO TO 900
      ENDIF
      LN = 1
      DO 8551 K = 1, NVDIMS
         CALL NCDINQ(NCID,VDIMS(K),DIMNAM,DIMSIZ,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MRDNUM1: BAD NCDINQ - '//ERRVAR(1:L))
            GO TO 900
         ENDIF

         IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.VDIMS(K).NE.RECDIM))THEN
            START(K) = 1
            COUNT(K) = DIMSIZ
            LN = LN * DIMSIZ
         ELSE
            START(K) = STARTR
            COUNT(K) = NRECS
            LN = LN * NRECS
         ENDIF
 8551 CONTINUE
C     ------------------------------------------------------------------
C     IF TOO BIG FOR OUR LOCAL ARRAYS, RETURN WITH ERROR.
C     ------------------------------------------------------------------
      IF(LN.GT.MXDATA)THEN
         STATUS = DIM_TOOSMALL_P
         CALL MERROR('MRDNUM1: EXCEEDED INTERNAL ARRAY SIZE - '//
     1               ERRVAR(1:L))
         GO TO 900
      ENDIF
      CALL NCVGT(NCID,J,START,COUNT,RDATA,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MRDNUM1: BAD NCVGT - '//ERRVAR(1:L))
         GO TO 900
      ENDIF
      CALL NCAGT(NCID,J,'_FillValue',RFILL,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         HAVE_FILL = .FALSE.

C     NOTE WE HAVE TO PUT A VALID NUMBER INTO THE FILLVAL
C     VARIABLE FOR THE CASE OF IT DOESN'T EVEN EXIST -- GOT A
C     NAN THAT CAUSED PROBLEMS WHEN THIS WAS DONE ON A DEC ALPHA.

         RFILL = 0
      ELSE
         HAVE_FILL = .TRUE.
      ENDIF
      CALL NCAGT(NCID,J,'missing_value',RMISS,RCODE)
      IF(RCODE.EQ.SUCCESS_P)THEN
         HAVE_MISS = .TRUE.

C     ------------------------------------------------------------------
C     LOOK FOR AND FIX THE CASE OF A MISMATCH BETWEEN THE ATTRIBUTE
C     AND THE VARIABLE.  WE KNOW IT HAPPENS WITH MESONET FLOAT & SHORT
C     VARIABLES, AND WE KNOW IT REALLY DOESN'T HAPPEN ANYWHERE ELSE
C     (ESPECIALLY AS NO OTHER DATASET CURRENTLY USES THIS ATTRIBUTE).
C     GIVEN OUR HISTORY (BEST LEFT UNSPOKEN) WITH THE MESONET FILES,
C     IT'S BEST TO MAKE THE LOGIC GENERAL AND NOT STOP WITH THIS ONE
C     MISMATCH.
C     ------------------------------------------------------------------

         CALL NCAINQ(NCID,J,'missing_value',ATRTYP,NVATTS,RCODE)
         IF(RCODE.NE.SUCCESS_P)THEN
            CALL MERROR('MRDNUM1: BAD NCAINQ - '//ERRVAR(1:L))
            GO TO 900
         ELSE IF(ATRTYP.EQ.NCDOUBLE.AND.VARTYP.NE.NCDOUBLE)THEN
            IF(VARTYP.EQ.NCBYTE)THEN
               BMISS = DMISS
            ELSE IF(VARTYP.EQ.NCSHORT)THEN
               I2MISS = DMISS
            ELSE IF(VARTYP.EQ.NCLONG)THEN
               IMISS = DMISS
            ELSE IF(VARTYP.EQ.NCFLOAT)THEN
               RMISS = DMISS
            ENDIF
         ELSE IF(ATRTYP.EQ.NCLONG.AND.VARTYP.NE.NCLONG)THEN
            IF(VARTYP.EQ.NCBYTE)THEN
               BMISS = IMISS
            ELSE IF(VARTYP.EQ.NCSHORT)THEN
               I2MISS = IMISS
            ELSE IF(VARTYP.EQ.NCLONG)THEN
               IMISS = IMISS
            ELSE IF(VARTYP.EQ.NCFLOAT)THEN
               RMISS = IMISS
            ENDIF
         ENDIF
      ELSE
         HAVE_MISS = .FALSE.
         RMISS = 0
      ENDIF

      N = LN

      IF(VARTYP.EQ.NCBYTE)THEN

         DO 100 I = 1, N
            IF((HAVE_FILL.AND.BDATA(I).EQ.BFILL).OR.
     1         (HAVE_MISS.AND.BDATA(I).EQ.BMISS))THEN
               VDATA(I) = SPVAL
            ELSE
               VDATA(I) = BDATA(I)
            ENDIF
 100     CONTINUE

      ELSE IF(VARTYP.EQ.NCSHORT)THEN

         DO 101 I = 1, N
            IF((HAVE_FILL.AND.I2DATA(I).EQ.I2FILL).OR.
     1         (HAVE_MISS.AND.(I2DATA(I).EQ.I2MISS.OR.
     2          I2DATA(I).EQ.MISVAL1)).OR.
     3         (CDBTYPE.EQ.AWIPS_DB_P.AND.I2DATA(I).EQ.AWIPSMISS))THEN
               VDATA(I) = SPVAL
            ELSE
               VDATA(I) = I2DATA(I)
            ENDIF
 101     CONTINUE

      ELSE IF(VARTYP.EQ.NCLONG)THEN

C     ------------------------------------------------------------------
C     TAKE CARE OF THE AWIPS DATABASE UNDOCUMENTED PROFILER MISSING
C     VALUE FLAG...AND THE AWIPS DATABASE UNDOCUMENTED GENERAL MISSING
C     FLAG (SHEESH!)  
C     ------------------------------------------------------------------
         DO 102 I = 1, N
            IF((HAVE_FILL.AND.IDATA(I).EQ.IFILL).OR.
     1           (HAVE_MISS.AND.(IDATA(I).EQ.IMISS.OR.
     2           IDATA(I).EQ.MISVAL1)).OR.
     3           (CDBTYPE.EQ.AWIPS_DB_P.AND.IDATA(I).EQ.AWIPSMISVAL2)
     4           .OR.(CDBTYPE.EQ.AWIPS_DB_P.AND.IDATA(I).EQ.AWIPSMISS))
     5           THEN
               VDATA(I) = SPVAL
            ELSE
               VDATA(I) = IDATA(I)
            ENDIF
 102     CONTINUE

      ELSE IF(VARTYP.EQ.NCFLOAT)THEN

C     ------------------------------------------------------------------
C     TAKE CARE OF THE AWIPS DATABASE UNDOCUMENTED GENERAL MISSING VALUE
C     FLAG...  
C     ------------------------------------------------------------------
         IF(CDBTYPE.EQ.AWIPS_DB_P)THEN

            DO 103 I = 1, N
               IF((HAVE_FILL.AND.RDATA(I).EQ.RFILL).OR.
     1              (HAVE_MISS.AND.(RDATA(I).EQ.RMISS.OR.
     2              RDATA(I).EQ.MISVAL1)).OR.RDATA(I).EQ.AWIPSMISS)THEN
                  VDATA(I) = SPVAL
               ELSE
                  VDATA(I) = RDATA(I)
               ENDIF
 103        CONTINUE

         ELSE
C     ------------------------------------------------------------------
C     ONE MORE IDIOSYNCRACY, SOME FSL ACARS VARIABLES HAVE AN 
C     UNDOCUMENTED MISSING FLAG.  THE ACARS'S MISSING 99999.0 VALUE IS 
C     SET IN MINIT OR IN EXTERNAL PROGRAMS BY THE CALLER.  ALL OTHER 
C     DATESETS USE THE DEFAULT FLOAT MISSING VALUE, SO IT IS A PASS 
C     THROUGH CHECK FOR THEM.
C     ------------------------------------------------------------------

            DO 104 I = 1, N
               IF((HAVE_FILL.AND.RDATA(I).EQ.RFILL).OR.
     1              (HAVE_MISS.AND.(RDATA(I).EQ.RMISS.OR.
     2              RDATA(I).EQ.MISVAL1).OR.(CDBTYPE.EQ.FSL_DB_P.AND.
     3              RDATA(I).EQ.ACARSMISVAL2)))THEN
                  VDATA(I) = SPVAL
               ELSE
                  VDATA(I) = RDATA(I)
               ENDIF

 104        CONTINUE

         ENDIF

      ENDIF

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