C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MRAOBEXT    EXTRACT DESIRED LEVELS FOR RAOB SOUNDINGS
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 00-03-31
C
C ABSTRACT: EXTRACT THE DESIRED LEVEL TYPES FOR RAOB SOUNDINGS
C
C PROGRAM HISTORY LOG:
C   00-03-31  M. BARTH    ORIGINAL VERSION
C   01-05-04  M. BARTH    ADDED FNUM ARG FOR USE IN RAOB STRCCOM VARS
C   01-07-09  M. BARTH    ADDED MISSING VALUE CHECK TO NUMBER OF
C                         LEVELS BEING RETURNED -- IF MISS, SET TO 0
C
C USAGE:    CALL MRAOBEXT (SPT,NRECS,FNUM,NLEVS,OB,QCDESC,QCAPP,QCRES,
C                          NLEVELS)
C
C   INPUT ARGUMENT LIST:
C     SPT      - I       SOUNDING TYPE CODE NUMBER
C     NRECS    - I       NUMBER OF RECORDS (STATIONS)
C     FNUM     - I       INDEX OF THE FILE BEING DEALT WITH
C     NLEVS    - I       NUMBER OF LEVELS IN DESIRED SOUNDING TYPE
C
C   INPUT/OUTPUT ARGUMENT LIST:
C     OB       - RA      OBS
C     QCDESC   - CA      QC DATA DESCRIPTORS
C     QCAPP    - RA      QC APPLIED WORD
C     QCRES    - RA      QC RESULTS WORD
C
C   OUTPUT ARGUMENT LIST:
C     NLEVELS  - IA      ACTUAL NUMBER OF LEVELS IN DESIRED SOUNDING
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 MRAOBEXT (SPT,NRECS,FNUM,NLEVS,OB,QCDESC,QCAPP,QCRES,
     1                     NLEVELS)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER SPT,NRECS,FNUM,NLEVS
      REAL OB(*),QCAPP(*),QCRES(*)
      CHARACTER*1 QCDESC(*)
      INTEGER NLEVELS(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I,J,NIN,NOUT

C     ------------------------------------------------------------------
C     WE HAVE TO DO THIS IN PLACE BY KEEPING ONLY THE DESIRED LEVELS,
C     AND CHANGE THE ACTUAL 2-D ARRAY FROM (MXRAOBLEV_P,NRECS) TO
C     (NLEVS,NRECS).
C     ------------------------------------------------------------------

      DO 1 J = 1, NRECS

         IF(RAOB_LT_COUNT(J,SPT,FNUM).NE.MISS_P)THEN
            NLEVELS(J) = RAOB_LT_COUNT(J,SPT,FNUM)
         ELSE
            NLEVELS(J) = 0
         ENDIF
         NOUT = (J - 1) * NLEVS
         NIN = (J - 1) * MXRAOBLEV_P

         DO 2 I = 1, MXRAOBLEV_P

            NIN = NIN + 1

            IF(RAOB_LT(I,J,FNUM).EQ.SPT.OR.
     1         (RAOB_LT(I,J,FNUM).EQ.SFC_RAOB_P.AND.
     2          SPT.EQ.MAN_RAOB_P))THEN

               NOUT = NOUT + 1
               OB(NOUT) = OB(NIN)
               QCAPP(NOUT) = QCAPP(NIN)
               QCRES(NOUT) = QCRES(NIN)
               QCDESC(NOUT) = QCDESC(NIN)

            ELSE IF(RAOB_LT(I,J,FNUM).EQ.MISS_P)THEN

               GO TO 3

            ENDIF

 2       CONTINUE

C     ------------------------------------------------------------------
C     FILL IN ANY UNUSED SLOTS WITH MISSING FLAGS UP THROUGH NLEVS FOR
C     THIS STATION.
C     ------------------------------------------------------------------
 3       DO 4 I = NOUT + 1, J * NLEVS

            OB(I) = MISS_P
            QCAPP(I) = 0
            QCRES(I) = 0
            QCDESC(I) = 'Z'

 4       CONTINUE

 1    CONTINUE

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