C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MRDDEP      READ QC DEPARTURE NETCDF VARIABLE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 02-05-31
C
C ABSTRACT: READ A QC DEPARTURE VARIABLE ON AN OPEN NETCDF FILE.
C
C PROGRAM HISTORY LOG:
C   02-05-31  M. BARTH    ORIGINAL VERSION
C   05-04-18  L. BENJAMIN REMOVED WRITING ERROR MESSAGE FOR
C                         QCR AND QCA "VAR_NOTINDB_P" CASES.
C   08-06-19  L. BENJAMIN PASSED IN MEMORY SIZE
C   
C
C USAGE:    CALL MRDDEP (VRNAM,NCID,RECDIM,STARTR,NRECS,SPVAL,CHKNUM,
C                        VDATA,STATUS,MXDATA)
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     CHKNUM   - I       DESIRED CHECK NUMBER (0 = ALL CHECKS)
C     MXDATA   - I       SIZE OF VDATA ARRAY
C
C   OUTPUT ARGUMENT LIST:
C     VDATA    - RA      REAL ARRAY WITH DATA AND SPVALS 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
C
C REMARKS:
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
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MRDDEP(VRNAM,NCID,RECDIM,STARTR,NRECS,SPVAL,CHKNUM,
     1                  VDATA,STATUS,MXDATA)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*(*) VRNAM
      INTEGER NCID,RECDIM,STARTR,NRECS,CHKNUM,STATUS,MXDATA
      REAL SPVAL,VDATA(*)

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),M_LSTCHR
      LOGICAL HAVE_QCCHKDIM
      REAL RFILL

      STATUS = NC_READ_P
      HAVE_QCCHKDIM = .FALSE.

C     ------------------------------------------------------------------
C     GET THE VARIABLE ID FROM ITS NAME.  INQUIRE ABOUT ITS DIMENSIONS.
C     FILL IN THE DIMENSIONS OF THE HYPERSLAB TO BE READ.  READ IT
C     DIRECTLY INTO THE CALLER'S ARRAY.  IF SUCCESSFUL, GET ITS FILL 
C     ATTRIBUTE, AND REPLACE FILL VALUES WITH SPVALS.
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(VRNAM(L-2:L).NE.'QCA'.AND.VRNAM(L-2:L).NE.'QCR')THEN
               CALL MERROR('MRDDEP: VARIABLE NOT IN SELECTED DATABASE')
            ENDIF
         ELSE
            CALL MERROR('MRDDEP: 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('MRDDEP: BAD NCVINQ - '//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('MRDDEP: BAD NCDINQ - '//ERRVAR(1:L))
            GO TO 900
         ENDIF
         IF(DIMNAM(1:10).EQ.'QCcheckNum')THEN
            HAVE_QCCHKDIM = .TRUE.
            IF(CHKNUM.EQ.0)THEN
               START(K) = 1
               COUNT(K) = DIMSIZ
               LN = LN * DIMSIZ
            ELSE IF(CHKNUM.LE.DIMSIZ)THEN
               START(K) = CHKNUM
               COUNT(K) = 1
            ELSE
               CALL MERROR('MRDDEP: INVALID QC CHECK NUMBER - '//
     1                     ERRVAR(1:L))
               GO TO 900
            ENDIF
         ELSE IF(K.LT.NVDIMS.OR.(NVDIMS.EQ.K.AND.
     1           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 WE DIDN'T FIND THE QC CHECK DIMENSION, OUR CALLER SCREWED UP...
C     ------------------------------------------------------------------
      IF(.NOT.HAVE_QCCHKDIM)THEN
         CALL MERROR('MRDDEP: QC CHECK DIMENSION NOT FOUND - '//
     1               ERRVAR(1:L))
         GO TO 900

C     ------------------------------------------------------------------
C     IF TOO BIG FOR OUR LOCAL ARRAYS, RETURN WITH ERROR.
C     ------------------------------------------------------------------
      ELSE IF(LN.GT.MXDATA)THEN
         STATUS = DIM_TOOSMALL_P
         CALL MERROR('MRDDEP: EXCEEDED INTERNAL ARRAY SIZE - '//
     1               ERRVAR(1:L))
         GO TO 900
      ENDIF

      CALL NCVGT(NCID,J,START,COUNT,VDATA,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MRDDEP: BAD NCVGT - '//ERRVAR(1:L))
         GO TO 900
      ENDIF
      CALL NCAGT(NCID,J,'_FillValue',RFILL,RCODE)
      IF(RCODE.NE.SUCCESS_P)THEN
         CALL MERROR('MRDDEP: BAD NCAGT - '//ERRVAR(1:L))
         GO TO 900
      ENDIF

      N = LN

      DO 103 I = 1, N
         IF(VDATA(I).EQ.RFILL)VDATA(I) = SPVAL
 103  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**---------------------------------------------------------------------
