C$$$  SUBPROGRAM DOCUMENTATION BLOCK

C                .      .    .                                       .
C SUBPROGRAM:    MRDDBL1     READ A DOUBLE PRECISION NETCDF VARIABLE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 00-02-09
C
C ABSTRACT: READ A DOUBLE PRECISION NETCDF VARIABLE ON AN OPEN NETCDF
C           FILE.
C
C PROGRAM HISTORY LOG:
C   00-02-09  M. BARTH    ORIGINAL VERSION -- COPIED FROM MRDNUM.F
C   01-07-06  M. BARTH    - ADDED VAR_NOTINDB_P ERROR RETURN.
C                         - CHANGED HANDLING OF VNAME IN MERROR CALLS
C                           TO CONFORM WITH G77 COMPILER
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   05-11-30  L. BENJAMIN CHANGED DIMENSION FROM MXDATA_P TO MXDBL_P
C                         TO SAVE USED SPACE.
C   08-06-13  L. BENJAMIN CHANGED "MADISCON" INCLUDE TO 
C                         "INTQC1CON2".  
C
C USAGE:    CALL MRDDBL1 (VNAME,NCID,RECDIM,STARTR,NRECS,SPVAL,
C                        VDATA,STATUS)
C
C   INPUT ARGUMENT LIST:
C     VNAME    - 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      DOUBLE PRECISION ARRAY WITH DATA AND SPVALS
C                        FILLED IN
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
C     DOUBLE PRECISION.
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 MXDBL_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
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MRDDBL1(VNAME,NCID,RECDIM,STARTR,NRECS,SPVAL,VDATA,
     1     MXDBL,DDATA,RDATA,IDATA,I2DATA,BDATA,STATUS)

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

      

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*(*) VNAME
      INTEGER NCID,RECDIM,STARTR,NRECS,STATUS
      REAL SPVAL
      DOUBLE PRECISION VDATA(*)
      INTEGER MXDBL
      DOUBLE PRECISION DDATA(MXDBL)
      REAL RDATA(MXDBL)
      INTEGER IDATA(MXDBL)
      INTEGER*2 I2DATA(MXDBL)
      BYTE BDATA(MXDBL)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------

      CHARACTER*(MAXNCNAM) VARNAM,DIMNAM,ERRVAR
      INTEGER I,J,K,L,N,RCODE,VARTYP,NVDIMS,VDIMS(MAXVDIMS),NVATTS,LN
      INTEGER DIMSIZ,START(MAXVDIMS),COUNT(MAXVDIMS),ATRTYP,M_LSTCHR
      REAL MISVAL1
      PARAMETER (MISVAL1 = -999.0)
      LOGICAL HAVE_FILL,HAVE_MISS
      DOUBLE PRECISION DFILL
      REAL RFILL
      INTEGER IFILL
      INTEGER*2 I2FILL
      BYTE BFILL
      EQUIVALENCE (RFILL,IFILL,I2FILL,BFILL,DFILL)
      REAL RMISS
      INTEGER IMISS
      INTEGER*2 I2MISS
      BYTE BMISS
      DOUBLE PRECISION DMISS
      EQUIVALENCE (RMISS,IMISS,I2MISS,BMISS,DMISS)

C     ------------------------------------------------------------------
C     TEMPORARY STORAGE DEFINITIONS
C     ------------------------------------------------------------------

      STATUS = NC_READ_P

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(VNAME)
      L = M_LSTCHR(VNAME,J)
      ERRVAR = VNAME(1:L)
      J = NCVID(NCID,VNAME,RCODE)
      IF(RCODE.NE.0)THEN
         IF(RCODE.EQ.NCENOTVR)THEN
            CALL MERROR('MRDDBL1: VARIABLE NOT IN SELECTED DATABASE')
            STATUS = VAR_NOTINDB_P
         ELSE
            CALL MERROR('MRDDBL1: BAD NCVID - '//ERRVAR(1:L))
         ENDIF
      ENDIF
      CALL NCVINQ(NCID,J,VARNAM,VARTYP,NVDIMS,VDIMS,NVATTS,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MRDDBL1: 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.AND.
     2   VARTYP.NE.NCDOUBLE)THEN
         CALL MERROR('MRDDBL1: 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('MRDDBL1: 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.MXDBL)THEN
         STATUS = DIM_TOOSMALL_P
         CALL MERROR('MRDDBL1: EXCEEDED INTERNAL ARRAY SIZE - '//
     1                ERRVAR(1:L))
         GO TO 900
      ENDIF
      CALL NCVGT(NCID,J,START,COUNT,DDATA,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MRDDBL1: BAD NCVGT - '//ERRVAR(1:L))
         GO TO 900
      ENDIF
      CALL NCAGT(NCID,J,'_FillValue',DFILL,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.

         DFILL = 0
      ELSE
         HAVE_FILL = .TRUE.
      ENDIF
      CALL NCAGT(NCID,J,'missing_value',DMISS,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('MRDDBL1: 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.
         DMISS = 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)))THEN
               VDATA(I) = SPVAL
            ELSE
               VDATA(I) = I2DATA(I)
            ENDIF
 101     CONTINUE

      ELSE IF(VARTYP.EQ.NCLONG)THEN

         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)))THEN
               VDATA(I) = SPVAL
            ELSE
               VDATA(I) = IDATA(I)
            ENDIF
 102     CONTINUE

      ELSE IF(VARTYP.EQ.NCFLOAT)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)))THEN
               VDATA(I) = SPVAL
            ELSE
               VDATA(I) = RDATA(I)
            ENDIF

 103     CONTINUE

      ELSE IF(VARTYP.EQ.NCDOUBLE)THEN

         DO 104 I = 1, N
            IF((HAVE_FILL.AND.DDATA(I).EQ.DFILL).OR.
     1         (HAVE_MISS.AND.(DDATA(I).EQ.DMISS.OR.
     2          DDATA(I).EQ.MISVAL1)))THEN
               VDATA(I) = SPVAL
            ELSE
               VDATA(I) = DDATA(I)
            ENDIF

 104     CONTINUE

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