C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MNPNLT      GET LEVEL TYPES FOR NPN INTEGRATED PROFILES
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 00-03-31
C
C ABSTRACT: GET THE LEVEL TYPES USED FOR EACH NPN INTEGRATED PROFILE
C   GIVEN THE CURRENT DATA
C
C PROGRAM HISTORY LOG:
C   00-03-31  M. BARTH    ORIGINAL VERSION
C   01-05-03  M. BARTH    CHANGED MRDNUM/MRDCHR TO MGTNUM/MGTCHR
C   01-07-10  M. BARTH    CHANGES FOR AWIPS DATABASE
C   02-05-29  M. BARTH    RESTRUCTURED TEMPORARY MEMORY ARRAY USAGE
C
C USAGE:    CALL MNPNLT (VCN,DSN,FNUM,NV,OB,QCDESC,QCAPP,QCRES,STATUS)
C
C   INPUT ARGUMENT LIST:
C     VCN      - I       VARIABLE CODE NUMBER
C     DSN      - I       DATASET CODE NUMBER
C     FNUM     - I       FILE NUMBER (INDEX IN COMMON) OF NETCDF FILE
C     NV       - I       NUMBER OF LEVELS
C
C   OUTPUT ARGUMENT LIST:
C     OB       - RA      LEVEL TYPE CODES (1-LOW, 2-HIGH)
C     QCDESC   - CA      QC DATA DESCRIPTORS  -- SET TO NO QC
C     QCAPP    - RA      QC APPLIED WORD      -- SET TO NO QC
C     QCRES    - RA      QC RESULTS WORD      -- SET TO NO QC
C     STATUS   - I       SUCCESS_P OR ERROR CODE
C
C   INPUT FILES:
C     NETCDF FILE OPENED USING DATA STRUCTURES FOR FNUM IN STRCCOM
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MGTNUM
C
C REMARKS:
C   - THIS ROUTINE MAKES THE DECISIONS ON WHICH MODE IS TO BE USED
C     IN THE OVERLAP REGION.  ALL THINGS BEING EQUAL, HIGH-MODE
C     DATA IS CONSIDERED OF HIGHER QUALITY BECAUSE IT HAS STRONGER
C     RETURNED POWER THAN DOES THE TOP OF THE LOW MODE.
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   - 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 MNPNLT (VCN,DSN,FNUM,NV,OB,QCDESC,QCAPP,QCRES,STATUS)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER VCN,DSN,FNUM,NV,STATUS
      REAL OB(NV,*),QCAPP(NV,*),QCRES(NV,*)
      CHARACTER*1 QCDESC(NV,*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I,J,K,CD

      REAL QCATMP(MXNPNLEV_P,MXNPN_P),QCRTMP(MXNPNLEV_P,MXNPN_P)
      REAL MODETMP(MXANPNLEV_P,MXNPN_P)

C     ------------------------------------------------------------------
C     TEMPORARY STORAGE DEFINITIONS
C     ------------------------------------------------------------------
      EQUIVALENCE(QCATMP,TMP_L2_N2)
      EQUIVALENCE(QCRTMP,TMP_L2_N3)
      EQUIVALENCE(MODETMP,TMP_L2_N1)

C     ------------------------------------------------------------------
C     IF THIS ROUTINE HASN'T YET BEEN CALLED FOR THIS TIME, READ IN
C     THE APPROPRIATE INFO FOR THE PARTICULAR DATABASE AND USE THAT TO
C     FILL IN THE LEVEL TYPES IN MADISCOM.  (FSL DATABASE:  QC WORDS FOR
C     THE U COMPONENT, AWIPS DATABASE:  THE LEVEL MODE VARIABLE ITSELF.)
C
C     WITH THE FSL DATABASE, HERE'S THE LOGIC FOR THE OVERLAP REGION
C     BETWEEN THE MODES:
C
C     - THE HIGH MODE IS PREFERABLE, GIVEN THAT IT DOESN'T HAVE WORSE
C       QC THAN THE LOW MODE.
C     - IF ONLY ONE MODE'S LEVEL EXISTS, IT'S USED REGARDLESS OF QC.
C     - IF BOTH MODES'S LEVELS EXIST, AND BOTH ARE BAD, THE HIGH-MODE
C       IS RETURNED.
C     ------------------------------------------------------------------
      STATUS = SUCCESS_P

      IF(.NOT.HAVE_NPN_LT)THEN

         CD = DSN - MAX_SFCDSET_P

         IF(CDBTYPE.EQ.FSL_DB_P)THEN

            CALL MGTNUM('uComponentQCA',UAPT_NCID(FNUM,CD),
     1                  UAPT_RECDIM(FNUM,CD),1,UAPT_NRECS(FNUM,CD),
     2                  MISS_P,QCATMP,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN

            CALL MGTNUM('uComponentQCR',UAPT_NCID(FNUM,CD),
     1                  UAPT_RECDIM(FNUM,CD),1,UAPT_NRECS(FNUM,CD),
     2                  MISS_P,QCRTMP,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN

         ELSE IF(CDBTYPE.EQ.AWIPS_DB_P)THEN

            CALL MGTNUM('levelMode',UAPT_NCID(FNUM,CD),
     1                  UAPT_RECDIM(FNUM,CD),1,UAPT_NRECS(FNUM,CD),
     2                  MISS_P,MODETMP,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN

         ENDIF

         HAVE_NPN_LT = .TRUE.

         IF(CDBTYPE.EQ.FSL_DB_P)THEN

            DO 1 J = 1, UAPT_NRECS(FNUM,CD)

               DO 2 I = 1, 28

                  IF(QCATMP(I,J).EQ.MISS_P)THEN
                     NPN_LT(I,J) = MISS_P
                  ELSE
                     NPN_LT(I,J) = 1
                  ENDIF

 2             CONTINUE

               DO 3 I = 1, 8

                  IF(QCATMP(I+36,J).NE.MISS_P.AND.QCATMP(I+36,J).NE.0
     1                 .AND.QCRTMP(I+36,J).EQ.0)THEN
                     NPN_LT(I+28,J) = 2
                  ELSE IF(QCATMP(I+28,J).NE.MISS_P.AND.
     1                    QCATMP(I+28,J).NE.0.AND.
     2                    QCRTMP(I+28,J).EQ.0)THEN
                     NPN_LT(I+28,J) = 1
                  ELSE IF(QCATMP(I+36,J).NE.MISS_P.AND.
     1                    QCATMP(I+36,J).NE.0)THEN
                     NPN_LT(I+28,J) = 2
                  ELSE IF(QCATMP(I+28,J).NE.MISS_P.AND.
     1                    QCATMP(I+28,J).NE.0)THEN
                     NPN_LT(I+28,J) = 1
                  ELSE
                     NPN_LT(I+28,J) = MISS_P
                  ENDIF

 3             CONTINUE

               DO 4 I = 37, 64

                  IF(QCATMP(I+8,J).EQ.MISS_P)THEN
                     NPN_LT(I+8,J) = MISS_P
                  ELSE
                     NPN_LT(I+8,J) = 1
                  ENDIF

 4             CONTINUE

 1          CONTINUE

         ELSE IF(CDBTYPE.EQ.AWIPS_DB_P)THEN

            DO 11 J = 1, UAPT_NRECS(FNUM,CD)

               DO 22 I = 1, 43

                  NPN_LT(I,J) = MODETMP(I,J)

 22            CONTINUE

 11         CONTINUE

         ENDIF

      ENDIF

C     ------------------------------------------------------------------
C     IF WE'VE BEEN CALLED TO RETURN THE LEVEL TYPES TO THE USER, DO SO.
C     NOTE THAT WE JUST HARD-WIRE IN ALL 72(FSL)/43(AWIPS) GATES, IT'S
C     UP TO OUR CALLER TO SORT THINGS OUT USING NPN_LT WHEN TRANSFERRING
C     INFO TO THE USER'S ARRAYS.
C     ------------------------------------------------------------------
      IF(VCN.EQ.NPNLT_VN_P)THEN

         CD = DSN - MAX_SFCDSET_P

         DO 5 J = 1, UAPT_NRECS(FNUM,CD)

            DO 6 I = 1, 36

               OB(I,J) = 1
               QCDESC(I,J) = 'Z'
               QCAPP(I,J) = 0
               QCRES(I,J) = 0

 6          CONTINUE

            IF(CDBTYPE.EQ.FSL_DB_P)THEN
               K = 72
            ELSE IF(CDBTYPE.EQ.AWIPS_DB_P)THEN
               K = 43
            ENDIF

            DO 7 I = 37, K

               OB(I,J) = 2
               QCDESC(I,J) = 'Z'
               QCAPP(I,J) = 0
               QCRES(I,J) = 0

 7          CONTINUE

 5       CONTINUE

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