C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MWISDOMWIND GET WISDOM WIND
C   PRGMMR: L. BENJAMIN      ORG: FSL        DATE: 08-07-20
C
C ABSTRACT: GET WISDOM WIND SPEED, WIND DIR, U COMPONENT, OR V 
C   COMPONENT FROM ONE FILE FOR ONE TIME.
C
C PROGRAM HISTORY LOG:
C   08-07-12  L. BENJAMIN ORIGINAL VERSION
C
C USAGE:    CALL MWISDOMWIND (VCN,DSN,FNUM,OB,QCDESC,QCAPP,QCRES,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
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   - FF & DD ARE STORED IN THE FILES.  IF THE CALLER REQUESTS ONE OF
C     THOSE, IT'S READ IN AND THAT'S ALL.
C   - IF U OR V IS REQUESTED, FF & DD ARE READ IN AND CONVERTED TO U/V.
C   - IF U OR V IS REQUESTED, THE QC RESULTS FROM FF & DD ARE
C     COMBINED.
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 MWISDOMWIND (VCN,DSN,FNUM,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,STATUS
      REAL OB(*),QCAPP(*),QCRES(*)
      CHARACTER*1 QCDESC(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      REAL OBTMP(MXWISDOM_P,2),COMPTMP(MXWISDOM_P,2),SINDL,COSDL,UTEMP
      REAL WIND(2),LONTMP(MXWISDOM_P),VTEMP
      INTEGER I,J,VAL,VNUM,LEN1REC
      REAL QCATMP(MXWISDOM_P,2),QCRTMP(MXWISDOM_P,2)
      INTEGER QCA(2),QCR(2),QCARET,QCRRET
      CHARACTER*1 QCDTMP(MXWISDOM_P,2)

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(MXWISDOM_P*2+1))
      EQUIVALENCE(LONTMP,TMP_L2_N2(MXWISDOM_P*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     ------------------------------------------------------------------
      IF(VCN.EQ.WISDOMFF_VN_P.OR.VCN.EQ.WISDOMDD_VN_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                  WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,
     2                  WISDOM_NRECS(FNUM),MISS_P,QCAPP,STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               QCSTATUS = EQCFAILED_P
               STATUS = SUCCESS_P
               DO I = 1, WISDOM_NRECS(FNUM)
                  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                  WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,
     2                  WISDOM_NRECS(FNUM),MISS_P,QCRES,STATUS)
               IF(STATUS.NE.SUCCESS_P)RETURN

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

            DO 1 I = 1, WISDOM_NRECS(FNUM)
               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           WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,
     2           WISDOM_NRECS(FNUM),MISS_P,OB,STATUS)
            RETURN

         ENDIF

      ENDIF

C     ------------------------------------------------------------------
C     READ IN THE QC FLAGS FROM FF, DD SO WE CAN USE THEM IN DECIDING
C     WHETHER OR NOT EACH WIND HAS BEEN FOUND BAD.  IF SO, WE WON'T
C     DO ANY CONVERSIONS FOR IT AND WILL RETURN MISSING DATA.
C     ------------------------------------------------------------------
      IF(VQC(WISDOMFF_VN_P,DSN).EQ.'Q')THEN

         CALL MGTNUM(VNAME(WISDOMFF_VN_P,DSN)(1:VLEN(WISDOMFF_VN_P,DSN))
     1        //'QCA',WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,
     2        WISDOM_NRECS(FNUM),MISS_P,QCATMP(1,1),STATUS)
         IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            QCSTATUS = EQCFAILED_P
            STATUS = SUCCESS_P
            DO I = 1, WISDOM_NRECS(FNUM)
               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(WISDOMFF_VN_P,DSN)
     1           (1:VLEN(WISDOMFF_VN_P,DSN))//'QCR',WISDOM_NCID(FNUM),
     2           WISDOM_RECDIM(FNUM),1,WISDOM_NRECS(FNUM),MISS_P,
     3           QCRTMP(1,1),STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN

            CALL MGTCHR(VNAME(WISDOMFF_VN_P,DSN)
     1           (1:VLEN(WISDOMFF_VN_P,DSN))//'DD',WISDOM_NCID(FNUM),
     2           WISDOM_RECDIM(FNUM),1,WISDOM_NRECS(FNUM),QCDTMP(1,1),
     3           LEN1REC,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
         ENDIF
      ELSE
         QCSTATUS = EQCFAILED_P
         DO 11 I = 1, WISDOM_NRECS(FNUM)
            QCATMP(I,1) = 0
            QCRTMP(I,1) = 0
            QCDTMP(I,1) = 'Z'
 11      CONTINUE

      ENDIF

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

         CALL MGTNUM(VNAME(WISDOMDD_VN_P,DSN)(1:VLEN(WISDOMDD_VN_P,DSN))
     1        //'QCA',WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,
     2               WISDOM_NRECS(FNUM),MISS_P,QCATMP(1,2),STATUS)
         IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            QCSTATUS = EQCFAILED_P
            STATUS = SUCCESS_P
            DO I = 1, WISDOM_NRECS(FNUM)
               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(WISDOMDD_VN_P,DSN)
     1           (1:VLEN(WISDOMDD_VN_P,DSN))//'QCR',WISDOM_NCID(FNUM),
     2           WISDOM_RECDIM(FNUM),1,WISDOM_NRECS(FNUM),MISS_P,
     3           QCRTMP(1,2),STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN


            CALL MGTCHR(VNAME(WISDOMDD_VN_P,DSN)
     1           (1:VLEN(WISDOMDD_VN_P,DSN))//'DD',WISDOM_NCID(FNUM),
     2           WISDOM_RECDIM(FNUM),1,WISDOM_NRECS(FNUM),QCDTMP(1,2),
     3           LEN1REC,STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN

         ENDIF
      ELSE
         QCSTATUS = EQCFAILED_P

         DO 21 I = 1, WISDOM_NRECS(FNUM)
            QCATMP(I,2) = 0
            QCRTMP(I,2) = 0
            QCDTMP(I,2) = 'Z'
 21      CONTINUE

      ENDIF

C     ------------------------------------------------------------------
C     READ FF & DD.  CONVERT TO U/V.
C     ------------------------------------------------------------------
      CALL MGTNUM(VNAME(WISDOMFF_VN_P,DSN)(1:VLEN(WISDOMFF_VN_P,DSN)),
     1     WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,WISDOM_NRECS(FNUM),
     2     MISS_P,OBTMP(1,1),STATUS)
      IF(STATUS.NE.SUCCESS_P)RETURN

      CALL MGTNUM(VNAME(WISDOMDD_VN_P,DSN)(1:VLEN(WISDOMDD_VN_P,DSN)),
     1     WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,WISDOM_NRECS(FNUM),
     2     MISS_P,OBTMP(1,2),STATUS)
      IF(STATUS.NE.SUCCESS_P)RETURN

      DO 13 I = 1, WISDOM_NRECS(FNUM)
         CALL M_GETUV(OBTMP(I,2),OBTMP(I,1),MISS_P,COMPTMP(I,1),
     1                COMPTMP(I,2))
 13   CONTINUE

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               WISDOM_NCID(FNUM),WISDOM_RECDIM(FNUM),1,
     2               WISDOM_NRECS(FNUM),MISS_P,LONTMP,STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

         DO 14 I = 1, WISDOM_NRECS(FNUM)

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

               CALL M_ROTFAC(LONTMP(I),LON_XX_U,ROTCON_U,SINDL,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     IF FF OR DD IS WHAT THE USER WANTS, WE ALREADY HAVE THE QC.  NOW
C     CONVERT BACK TO FF/DD AND RETURN THE CHOSEN VARIABLE TO THE USER,
C     AND RETURN TO CALLER.
C     ------------------------------------------------------------------
      IF(VCN.EQ.WISDOMFF_VN_P.OR.VCN.EQ.WISDOMDD_VN_P)THEN

         IF(VCN.EQ.WISDOMDD_VN_P)THEN
            VNUM = 1
         ELSE
            VNUM = 2
         ENDIF

         DO 15 I = 1, WISDOM_NRECS(FNUM)

            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)

            ENDIF

 15      CONTINUE

         RETURN

      ENDIF

C     ------------------------------------------------------------------
C     COMBINE THE QC FOR FF & DD INTO ONE FLAG.  WHILE WE'RE AT IT,
C     STUFF IN THE DESIRED COMPONENT (THAT'S NOW BEEN ROTATED, IF
C     NEEDED) INTO THE CALLER'S OB ARRAY.
C     ------------------------------------------------------------------
      IF(VCN.EQ.WISDOMU_VN_P)THEN
         VNUM = 1
      ELSE
         VNUM = 2
      ENDIF

      DO 31 I = 1, WISDOM_NRECS(FNUM)

         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

            OB(I) = COMPTMP(I,VNUM)

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_WISDOM_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 THE SPEED OR DIR DESCRIPTOR SHOWS SUBJECTIVE QC, USE
C     THAT VALUE.  OTHERWISE, TRANSLATE THE NEW QCAPP/QCRES INTO THE
C     DESCRIPTOR TO 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

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