C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MUAPTWIND   GET UPPER-AIR-POINT WIND
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 00-03-27
C
C ABSTRACT: GET UPPER-AIR-POINT WIND SPEED, WIND DIR, U COMPONENT, OR
C   V COMPONENT FROM ONE FILE FOR ONE TIME.
C
C PROGRAM HISTORY LOG:
C   00-03-27  M. BARTH    ORIGINAL VERSION
C   01-05-03  M. BARTH    CHANGED MRDNUM/MRDCHR TO MGTNUM/MGTCHR
C   02-04-30  M. BARTH    CHANGED MAX_QCCHK_P TO MAX_UAPT_QCCHK_P
C   02-05-29  M. BARTH    RESTRUCTURED TEMPORARY MEMORY ARRAY USAGE
C   04-10-16  L. BENJAMIN ADDED NO SUCH VARIABLE CHECK FOR QCA CHECKS
C   08-07-25  L. BENJAMIN REMOVED COMMENT
C
C USAGE:    CALL MUAPTWIND (VCN,DSN,FNUM,NV,OB,QCDESC,QCAPP,QCRES,
C                           STATUS)
C
C   INPUT ARGUMENT LIST:
C     VCN      - I       VARIABLE CODE NUMBER FOR FF, DD, U, V
C     DSN      - I       DATASET CODE NUMBER
C     FNUM     - I       FILE NUMBER (INDEX IN COMMON) OF NETCDF FILE
C     NV       - I       NUMBER OF ELEMENTS DIMENSIONED FOR THE VARIABLE
C
C   OUTPUT ARGUMENT LIST:
C     OB       - RA      OBSERVATIONS IN M/K/S UNITS
C     QCDESC   - CA      QC DATA DESCRIPTORS
C     QCAPP    - RA      QC APPLIED WORD (SHOWS CHECKS APPLIED)
C     QCRES    - RA      QC RESULTS WORD (GOOD/BAD FOR EACH CHECK)
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:    MGTCHR, MGTNUM, M_GETUV, M_MKDESC, M_ROTFAC
C
C REMARKS:
C   - IF THE CALLER REQUESTS FF OR DD AND THAT'S WHAT'S STORED IN
C     THE FILE, OR IF THE CALLER REQUESTS U OR V AND THAT'S STORED,
C     THE DATA ARE READ IN.  OTHERWISE, FF/DD OR U/V ARE READ IN AND
C     CONVERTED TO U/V OR FF/DD.  IF CONVERSION IS DONE, THE QC
C     RESULTS STORED IN THE FILE ARE COMBINED.
C   - IF ANY CONVERSIONS OR ROTATIONS ARE NEEDED, BOTH STORED
C     WIND VARIABLES MUST HAVE PASSED ANY QC THAT WAS APPLIED IN
C     ORDER TO BE USED, OTHERWISE MISSING DATA WILL BE RETURNED.
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 MUAPTWIND (VCN,DSN,FNUM,NV,OB,QCDESC,QCAPP,QCRES,
     1                      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(*),QCAPP(*),QCRES(*)
      CHARACTER*1 QCDESC(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER MXTMP
      PARAMETER (MXTMP = MXRAOB_P * MXRAOBLEV_P)
      REAL OBTMP(MXTMP,2),COMPTMP(MXTMP,2),SINDL,COSDL,UTEMP,VTEMP
      REAL WIND(2),LONTMP(MXUAPT_P)
      INTEGER I,J,VAL,VNUM,LEN1REC,CD
      REAL QCATMP(MXTMP,2),QCRTMP(MXTMP,2)
      INTEGER QCA(2),QCR(2),QCARET,QCRRET
      CHARACTER*1 QCDTMP(MXTMP,2)
C     1=FF 2=DD 3=U 4=V
      LOGICAL QCMISS(4) 
      INTEGER QCIDX

C     ------------------------------------------------------------------
C     TEMPORARY STORAGE DEFINITIONS
C     ------------------------------------------------------------------
      EQUIVALENCE(OBTMP,TMP_L2_N1)
      EQUIVALENCE(QCATMP,TMP_L2_N2)
      EQUIVALENCE(QCRTMP,TMP_L2_N3)
      EQUIVALENCE(QCDTMP,TMP_L2_C1)
      EQUIVALENCE(COMPTMP,TMP_L2_N1(MXTMP*2+1))
      EQUIVALENCE(LONTMP,TMP_L2_N2(MXTMP*2+1))

C     ------------------------------------------------------------------
C     IF THE REQUESTED VARIABLE IS THE ONE STORED IN THE DATASET, READ
C     IT'S QC STRUCTURES DIRECTLY.  THEN, IF IT DOESN'T HAVE TO BE
C     ROTATED FOR THE USER'S GRID PROJECTION, READ THE VARIABLE AND
C     WE'RE DONE.
C     ------------------------------------------------------------------
      CD = DSN - MAX_SFCDSET_P

      IF(((VCN.EQ.UAPTFF_VN_P.OR.VCN.EQ.UAPTDD_VN_P).AND.
     1    CD.NE.NPN_DS_P).OR.
     2   ((VCN.EQ.UAPTU_VN_P.OR.VCN.EQ.UAPTV_VN_P).AND.
     3    CD.EQ.NPN_DS_P))THEN

C     ASSUME QC OK, SET FOR DIFFERNET CONDITIONS
         QCSTATUS = EQCOK_P

         IF(VQC(VCN,DSN).EQ.'Q')THEN

            CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'QCA',
     1                  UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2                  UAPT_NRECS(FNUM,CD),MISS_P,QCAPP,STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               QCSTATUS = EQCFAILED_P
               STATUS = SUCCESS_P
               DO I = 1, UAPT_NRECS(FNUM,CD) * NV
                  QCAPP(I) = 0
                  QCRES(I) = 0
                  QCDESC(I) = 'Z'
               ENDDO
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               RETURN
            ELSE
               CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'QCR',
     1                  UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2                  UAPT_NRECS(FNUM,CD),MISS_P,QCRES,STATUS)
               IF(STATUS.NE.SUCCESS_P)RETURN

               CALL MGTCHR(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'DD',
     1                  UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2                  UAPT_NRECS(FNUM,CD),QCDESC,LEN1REC,STATUS)
               IF(STATUS.NE.SUCCESS_P)RETURN
            ENDIF
         ELSE
            QCSTATUS = EQCFAILED_P

            DO 1 I = 1, UAPT_NRECS(FNUM,CD) * NV
               QCAPP(I) = 0
               QCRES(I) = 0
               QCDESC(I) = 'Z'
 1          CONTINUE

         ENDIF

         IF(DMTYPE.NE.0.AND.DMTYPE.NE.1)THEN

            CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN)),
     1                  UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2                  UAPT_NRECS(FNUM,CD),MISS_P,OB,STATUS)
            RETURN

         ENDIF

      ENDIF

C     ------------------------------------------------------------------
C     MAKE SURE WE HAVE DATA IN U,V IN COMPTMP ARRAY.
C     ------------------------------------------------------------------
      IF(CD.NE.NPN_DS_P)THEN

         CALL MGTNUM(VNAME(UAPTFF_VN_P,DSN)(1:VLEN(UAPTFF_VN_P,DSN)),
     1               UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     1               UAPT_NRECS(FNUM,CD),MISS_P,OBTMP(1,1),STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

         CALL MGTNUM(VNAME(UAPTDD_VN_P,DSN)(1:VLEN(UAPTDD_VN_P,DSN)),
     1               UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,OBTMP(1,2),STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

         DO 13 I = 1, UAPT_NRECS(FNUM,CD) * NV
            CALL M_GETUV(OBTMP(I,2),OBTMP(I,1),MISS_P,COMPTMP(I,1),
     1                   COMPTMP(I,2))
 13      CONTINUE

      ELSE

         CALL MGTNUM(VNAME(UAPTU_VN_P,DSN)(1:VLEN(UAPTU_VN_P,DSN)),
     1               UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     1               UAPT_NRECS(FNUM,CD),MISS_P,COMPTMP(1,1),STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

         CALL MGTNUM(VNAME(UAPTV_VN_P,DSN)(1:VLEN(UAPTV_VN_P,DSN)),
     1               UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,COMPTMP(1,2),STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

      ENDIF

C     ------------------------------------------------------------------
C     IF NEEDED, ROTATE THE WIND COMPONENTS FOR THE USER'S PROJECTION.
C     NOTE THAT THIS REQUIRES GETTING THE LONGITUDE (WHICH, ALONG WITH
C     THE WINDS, ARE READ IN A FEW EXTRA TIMES IN ORDER TO KEEP THE
C     WIND LOGIC LOOK THE SAME AS ANYTHING ELSE AT THE CALLER'S LEVEL.
C     IF NEEDED TO SPEED THINGS UP, THESE DATA COULD BE KEPT IN COMMON,
C     BUT THAT WOULD TAKE UP MEMORY...)
C     ------------------------------------------------------------------
      IF(DMTYPE.EQ.0.OR.DMTYPE.EQ.1)THEN

         CALL MGTNUM(VNAME(LON_VN_P,DSN)(1:VLEN(LON_VN_P,DSN)),
     1               UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,LONTMP,STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

         DO 14 I = 1, UAPT_NRECS(FNUM,CD) * NV

            IF(COMPTMP(I,1).NE.MISS_P.AND.COMPTMP(I,2).NE.MISS_P)THEN

               CALL M_ROTFAC(LONTMP((I-1)/NV+1),LON_XX_U,ROTCON_U,SINDL,
     1                       COSDL)
               UTEMP = COMPTMP(I,1)
               VTEMP = COMPTMP(I,2)
               COMPTMP(I,1) = COSDL * UTEMP + SINDL * VTEMP
               COMPTMP(I,2) = -SINDL * UTEMP + COSDL * VTEMP

            ENDIF

 14      CONTINUE

      ENDIF

C     ------------------------------------------------------------------
C     READ IN THE QC FLAGS FROM THE STORED VARIABLES.
C     ------------------------------------------------------------------
      QCMISS(1) = .FALSE. 
      QCMISS(3) = .FALSE. 
      IF(CD.NE.NPN_DS_P.AND.VQC(UAPTFF_VN_P,DSN).EQ.'Q')THEN

         CALL MGTNUM(VNAME(UAPTFF_VN_P,DSN)(1:VLEN(UAPTFF_VN_P,DSN))//
     1               'QCA',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,QCATMP(1,1),STATUS)
         IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            QCMISS(1) = .TRUE.
            QCSTATUS = EQCFAILED_P
            STATUS = SUCCESS_P
            DO I = 1, UAPT_NRECS(FNUM,CD) * NV
               QCATMP(I,1) = 0
               QCRTMP(I,1) = 0
               QCDTMP(I,1) = 'Z'
            ENDDO
         ELSE IF(STATUS.NE.SUCCESS_P)THEN
            RETURN
         ELSE
            CALL MGTNUM(VNAME(UAPTFF_VN_P,DSN)(1:VLEN(UAPTFF_VN_P,DSN))
     1           //'QCR',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2           UAPT_NRECS(FNUM,CD),MISS_P,QCRTMP(1,1),STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
            CALL MGTCHR(VNAME(UAPTFF_VN_P,DSN)(1:VLEN(UAPTFF_VN_P,DSN))
     1           //'DD',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2           UAPT_NRECS(FNUM,CD),QCDTMP(1,1),LEN1REC,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
         ENDIF
      ELSE IF(CD.EQ.NPN_DS_P.AND.VQC(UAPTU_VN_P,DSN).EQ.'Q')THEN

         CALL MGTNUM(VNAME(UAPTU_VN_P,DSN)(1:VLEN(UAPTU_VN_P,DSN))//
     1               'QCA',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,QCATMP(1,1),STATUS)
         IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            QCMISS(3) = .TRUE. 
            QCSTATUS = EQCFAILED_P
            STATUS = SUCCESS_P
            DO I = 1, UAPT_NRECS(FNUM,CD) * NV
               QCATMP(I,1) = 0
               QCRTMP(I,1) = 0
               QCDTMP(I,1) = 'Z'
            ENDDO
         ELSE IF(STATUS.NE.SUCCESS_P)THEN
            RETURN
         ELSE

            CALL MGTNUM(VNAME(UAPTU_VN_P,DSN)(1:VLEN(UAPTU_VN_P,DSN))//
     1               'QCR',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,QCRTMP(1,1),STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
            CALL MGTCHR(VNAME(UAPTU_VN_P,DSN)(1:VLEN(UAPTU_VN_P,DSN))//
     1               'DD',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),QCDTMP(1,1),LEN1REC,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
         ENDIF
      ELSE
         QCSTATUS = EQCFAILED_P

         DO 11 I = 1, UAPT_NRECS(FNUM,CD) * NV
            QCATMP(I,1) = 0
            QCRTMP(I,1) = 0
            QCDTMP(I,1) = 'Z'
 11      CONTINUE

      ENDIF

      QCMISS(2) = .FALSE. 
      QCMISS(4) = .FALSE. 
      IF(CD.NE.NPN_DS_P.AND.VQC(UAPTDD_VN_P,DSN).EQ.'Q')THEN

         CALL MGTNUM(VNAME(UAPTDD_VN_P,DSN)(1:VLEN(UAPTDD_VN_P,DSN))//
     1               'QCA',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,QCATMP(1,2),STATUS)
         IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            QCMISS(2) = .TRUE. 
            QCSTATUS = EQCFAILED_P
            STATUS = SUCCESS_P
            DO I = 1, UAPT_NRECS(FNUM,CD) * NV
               QCATMP(I,2) = 0
               QCRTMP(I,2) = 0
               QCDTMP(I,2) = 'Z'
            ENDDO
         ELSE IF(STATUS.NE.SUCCESS_P)THEN
            RETURN
         ELSE

            CALL MGTNUM(VNAME(UAPTDD_VN_P,DSN)(1:VLEN(UAPTDD_VN_P,DSN))
     1           //'QCR',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2           UAPT_NRECS(FNUM,CD),MISS_P,QCRTMP(1,2),STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
            CALL MGTCHR(VNAME(UAPTDD_VN_P,DSN)(1:VLEN(UAPTDD_VN_P,DSN))
     1           //'DD',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2           UAPT_NRECS(FNUM,CD),QCDTMP(1,2),LEN1REC,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
         ENDIF
      ELSE IF(CD.EQ.NPN_DS_P.AND.VQC(UAPTV_VN_P,DSN).EQ.'Q')THEN

         CALL MGTNUM(VNAME(UAPTV_VN_P,DSN)(1:VLEN(UAPTV_VN_P,DSN))//
     1               'QCA',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,QCATMP(1,2),STATUS)
         IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            QCMISS(4) = .TRUE. 
            QCSTATUS = EQCFAILED_P
            STATUS = SUCCESS_P
            DO I = 1, UAPT_NRECS(FNUM,CD) * NV
               QCATMP(I,2) = 0
               QCRTMP(I,2) = 0
               QCDTMP(I,2) = 'Z'
            ENDDO
         ELSE IF(STATUS.NE.SUCCESS_P)THEN
            RETURN
         ELSE

            CALL MGTNUM(VNAME(UAPTV_VN_P,DSN)(1:VLEN(UAPTV_VN_P,DSN))//
     1               'QCR',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),MISS_P,QCRTMP(1,2),STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
            CALL MGTCHR(VNAME(UAPTV_VN_P,DSN)(1:VLEN(UAPTV_VN_P,DSN))//
     1               'DD',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2               UAPT_NRECS(FNUM,CD),QCDTMP(1,2),LEN1REC,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
         ENDIF
      ELSE
         QCSTATUS = EQCFAILED_P

         DO 21 I = 1, UAPT_NRECS(FNUM,CD) * NV
            QCATMP(I,2) = 0
            QCRTMP(I,2) = 0
            QCDTMP(I,2) = 'Z'
 21      CONTINUE

      ENDIF

C     ------------------------------------------------------------------
C     IF THE STORED VARIABLE MATCHES THE REQUESTED VARIABLE WE ALREADY
C     HAVE THE QC.  PUT WHAT THE CALLER WANTS INTO HIS ARRAY AND RETURN.
C     ------------------------------------------------------------------
      IF((VCN.EQ.UAPTFF_VN_P.OR.VCN.EQ.UAPTDD_VN_P).AND.
     1    CD.NE.NPN_DS_P)THEN

         IF(VCN.EQ.UAPTDD_VN_P)THEN
            VNUM = 1
            QCIDX = 2
         ELSE
            VNUM = 2
            QCIDX = 1
         ENDIF
         IF(QCMISS(QCIDX))THEN
            QCSTATUS = EQCFAILED_P
         ELSE
            QCSTATUS = EQCOK_P
         ENDIF

         DO 15 I = 1, UAPT_NRECS(FNUM,CD) * NV

            IF(COMPTMP(I,1).EQ.MISS_P.OR.COMPTMP(I,2).EQ.MISS_P.OR.
     1         QCDTMP(I,1).EQ.'B'.OR.QCDTMP(I,1).EQ.'X'.OR.
     2         QCDTMP(I,1).EQ.'Q'.OR.QCDTMP(I,2).EQ.'B'.OR.
     3         QCDTMP(I,2).EQ.'X'.OR.QCDTMP(I,2).EQ.'Q')THEN

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

            ELSE

               CALL M_DDFF(COMPTMP(I,1),COMPTMP(I,2),MISS_P,WIND(1),
     1                     WIND(2))
               OB(I) = WIND(VNUM)

               IF(QCMISS(QCIDX))THEN
                  QCAPP(I) = 0
                  QCRES(I) = 0
                  QCDESC(I) = 'Z'
               ENDIF


            ENDIF

 15      CONTINUE

         RETURN

      ELSE IF((VCN.EQ.UAPTU_VN_P.OR.VCN.EQ.UAPTV_VN_P).AND.
     1        CD.EQ.NPN_DS_P)THEN

         IF(VCN.EQ.UAPTU_VN_P)THEN
            VNUM = 1
            QCIDX = 3
         ELSE
            VNUM = 2
            QCIDX = 4
         ENDIF
         IF(QCMISS(QCIDX))THEN
            QCSTATUS = EQCFAILED_P
         ELSE
            QCSTATUS = EQCOK_P
         ENDIF

         DO 16 I = 1, UAPT_NRECS(FNUM,CD) * NV

            IF(COMPTMP(I,1).EQ.MISS_P.OR.COMPTMP(I,2).EQ.MISS_P.OR.
     1         QCDTMP(I,1).EQ.'B'.OR.QCDTMP(I,1).EQ.'X'.OR.
     2         QCDTMP(I,1).EQ.'Q'.OR.QCDTMP(I,2).EQ.'B'.OR.
     3         QCDTMP(I,2).EQ.'X'.OR.QCDTMP(I,2).EQ.'Q')THEN

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

            ELSE

               OB(I) = COMPTMP(I,VNUM)

               IF(QCMISS(QCIDX))THEN
                  QCAPP(I) = 0
                  QCRES(I) = 0
                  QCDESC(I) = 'Z'
               ENDIF

            ENDIF

 16      CONTINUE

         RETURN

      ENDIF

C     ------------------------------------------------------------------
C     COMBINE THE QC INTO ONE FLAG.  WHILE WE'RE AT IT, STUFF IN THE
C     DESIRED VARIABLE (THAT'S NOW BEEN ROTATED, IF NEEDED) INTO THE
C     CALLER'S OB ARRAY.
C     ------------------------------------------------------------------
      IF(VCN.EQ.UAPTU_VN_P)THEN
         VNUM = 1
         QCIDX = 3
      ELSE IF(VCN.EQ.UAPTDD_VN_P)THEN
         VNUM = 1
         QCIDX = 2
      ELSE IF(VCN.EQ.UAPTFF_VN_P)THEN
         VNUM = 2
         QCIDX = 1
      ELSE
         VNUM = 2
         QCIDX = 4
      ENDIF
      IF(QCMISS(QCIDX))THEN
         QCSTATUS = EQCFAILED_P
      ELSE
         QCSTATUS = EQCOK_P
      ENDIF

      DO 31 I = 1, UAPT_NRECS(FNUM,CD) * NV

         IF(COMPTMP(I,1).EQ.MISS_P.OR.COMPTMP(I,2).EQ.MISS_P.OR.
     1      QCDTMP(I,1).EQ.'B'.OR.QCDTMP(I,1).EQ.'X'.OR.
     2      QCDTMP(I,1).EQ.'Q'.OR.QCDTMP(I,2).EQ.'B'.OR.
     3      QCDTMP(I,2).EQ.'X'.OR.QCDTMP(I,2).EQ.'Q')THEN

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

         ELSE

            QCAPP(I) = 0
            QCRES(I) = 0

            IF(CD.NE.NPN_DS_P)THEN

               OB(I) = COMPTMP(I,VNUM)

            ELSE

               CALL M_DDFF(COMPTMP(I,1),COMPTMP(I,2),MISS_P,WIND(1),
     1                     WIND(2))
               OB(I) = WIND(VNUM)

            ENDIF
C     ------------------------------------------------------------------
C     CHECK FOR MISSING QC, OTHERWISE ASSIGN AS DEFINED
C     ------------------------------------------------------------------
            IF(QCMISS(QCIDX))THEN
               QCAPP(I) = 0
               QCRES(I) = 0
               QCDESC(I) = 'Z'
            ELSE

C     ------------------------------------------------------------------
C     PUT THE QC APPLIED/RESULTS WORDS INTO INTEGER FORMAT (THEIR
C     TRUE NATURE) SO WE CAN EXAMINE THE BITS, AND COMBINE THEM.
C     ------------------------------------------------------------------
               QCA(1) = QCATMP(I,1)
               QCA(2) = QCATMP(I,2)
               QCR(1) = QCRTMP(I,1)
               QCR(2) = QCRTMP(I,2)
               QCARET = 0
               QCRRET = 0

               DO 32 J = 1, MAX_UAPT_QCCHK_P

                  CALL M_CHCKQC(QCA(1),J,VAL,STATUS)
                  IF(VAL.EQ.1)CALL M_SETQC(QCARET,J,QCARET,STATUS)
                  CALL M_CHCKQC(QCA(2),J,VAL,STATUS)
                  IF(VAL.EQ.1)CALL M_SETQC(QCARET,J,QCARET,STATUS)
                  CALL M_CHCKQC(QCR(1),J,VAL,STATUS)
                  IF(VAL.EQ.1)CALL M_SETQC(QCRRET,J,QCRRET,STATUS)
                  CALL M_CHCKQC(QCR(2),J,VAL,STATUS)
                  IF(VAL.EQ.1)CALL M_SETQC(QCRRET,J,QCRRET,STATUS)

 32            CONTINUE

C     ------------------------------------------------------------------
C     IF EITHER DESCRIPTOR SHOWS SUBJECTIVE QC, USE THAT VALUE.
C     OTHERWISE, TRANSLATE THE NEW QCAPP/QCRES INTO THE DESCRIPTOR TO
C     BE RETURNED.
C     ------------------------------------------------------------------
               IF(QCDTMP(I,1).EQ.'G'.OR.QCDTMP(I,1).EQ.'B')THEN

                  QCDESC(I) = QCDTMP(I,1)

               ELSE IF(QCDTMP(I,2).EQ.'G'.OR.QCDTMP(I,2).EQ.'B')THEN

                  QCDESC(I) = QCDTMP(I,2)

               ELSE

                  CALL M_MKDESC(QCARET,QCRRET,QCDESC(I))

               ENDIF

               QCAPP(I) = QCARET
               QCRES(I) = QCRRET
            ENDIF
         ENDIF

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