C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MMAPWIND    GET MAP WIND VARIABLES
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 02-05-13
C
C ABSTRACT: GET MAP WIND SPEED, WIND DIR, U COMPONENT, OR V COMPONENT 
C   FROM ONE FILE FOR ONE TIME.
C
C PROGRAM HISTORY LOG:
C   02-05-13  M. BARTH    ORIGINAL VERSION
C   04-10-16  L. BENJAMIN ADDED NO SUCH VARIABLE CHECK FOR QCA CHECKS
C
C USAGE:    CALL MMAPWIND (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_DDFF, M_GETUV, M_ROTFAC
C
C REMARKS:
C   - THE MAP ROUTINE DIFFERS FROM THE GENERAL-CASE UAPT ROUTINE
C     BECAUSE IT'S POSSIBLE (AT LEAST WITH THE AWIPS DATABASE)
C     FOR U/V AND SPD/DIR TO BOTH EXIST IN THE FILE AND BE
C     DIFFERENT MEASUREMENTS.  
C   - IF VARIABLE CONVERSION (SPD/DIR <--> U/V) 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 MMAPWIND (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(MXMAPLEV_P,MXMAP_P),QCAPP(MXMAPLEV_P,MXMAP_P)
      REAL QCRES(MXMAPLEV_P,MXMAP_P)
      CHARACTER*1 QCDESC(MXMAPLEV_P,MXMAP_P)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      REAL SINDL,COSDL,UTEMP,VTEMP
      REAL LON(MXMAP_P)
      INTEGER I,J,K,LEN1REC,CD
      REAL OBTMP(MXMAPLEV_P,MXMAP_P,4),QCATMP(MXMAPLEV_P,MXMAP_P,4)
      REAL QCRTMP(MXMAPLEV_P,MXMAP_P,4)
      CHARACTER*1 QCDTMP(MXMAPLEV_P,MXMAP_P,4)
      INTEGER LVCN(4)

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)

C     ------------------------------------------------------------------
C     DATA STATEMENTS (KEEP AT BOTTOM OF DECLARATIONS)
C     ------------------------------------------------------------------
      DATA LVCN/UAPTDD_VN_P,UAPTFF_VN_P,UAPTU_VN_P,UAPTV_VN_P/

C     ------------------------------------------------------------------
C     READ IN BOTH FORMS OF THE WINDS & THEIR QC STRUCTURES.
C     ------------------------------------------------------------------
      CD = DSN - MAX_SFCDSET_P

      DO I = 1, 4

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

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

         IF(VQC(LVCN(I),DSN).EQ.'Q')THEN

            CALL MGTNUM(VNAME(LVCN(I),DSN)(1:VLEN(LVCN(I),DSN))//'QCA',
     1                  UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2                  UAPT_NRECS(FNUM,CD),MISS_P,QCATMP(1,1,I),STATUS)

            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               QCSTATUS = EQCFAILED_P
               STATUS = SUCCESS_P
               DO J = 1, UAPT_NRECS(FNUM,CD)
                  DO K = 1, MXMAPLEV_P
                     QCATMP(K,J,I) = 0
                     QCRTMP(K,J,I) = 0
                     QCDTMP(K,J,I) = 'Z'
                  ENDDO
               ENDDO
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               RETURN
            ELSE
               CALL MGTNUM(VNAME(LVCN(I),DSN)(1:VLEN(LVCN(I),DSN))//
     1              'QCR',UAPT_NCID(FNUM,CD),UAPT_RECDIM(FNUM,CD),1,
     2              UAPT_NRECS(FNUM,CD),MISS_P,QCRTMP(1,1,I),STATUS)
               IF(STATUS.NE.SUCCESS_P)RETURN

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

            DO J = 1, UAPT_NRECS(FNUM,CD)
               DO K = 1, MXMAPLEV_P
                  QCATMP(K,J,I) = 0
                  QCRTMP(K,J,I) = 0
                  QCDTMP(K,J,I) = 'Z'
               ENDDO
            ENDDO
         ENDIF

      ENDDO

C     ------------------------------------------------------------------
C     NOW FOR ANY STATION THAT HAS ONLY DD/FF OR U/V, FILL IN THE
C     OTHER FORM OF THE WINDS -- AND COMBINE THE QC STRUCTURES FOR THEM.
C     ALSO NOTE THAT OUR POLICY IS TO ONLY USE DATA THAT HAVEN'T
C     FLUNKED QC WHEN DOING CONVERSIONS.
C     ------------------------------------------------------------------
      DO I = 2, 4, 2

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

            DO K = 1, MXMAPLEV_P
               IF(OBTMP(K,J,I).NE.MISS_P.OR.OBTMP(K,J,I-1).NE.MISS_P)
     1            GO TO 100
            ENDDO

            IF(I.EQ.4)THEN

C     ------------------------------------------------------------------
C     DD/FF --> U/V FOR STATION J
C     ------------------------------------------------------------------
               DO K = 1, MXMAPLEV_P

                  IF(.NOT.(OBTMP(K,J,1).EQ.MISS_P.OR.
     1               OBTMP(K,J,2).EQ.MISS_P.OR.QCDTMP(K,J,1).EQ.'B'.OR.
     2               QCDTMP(K,J,1).EQ.'X'.OR.QCDTMP(K,J,1).EQ.'Q'.OR.
     3               QCDTMP(K,J,2).EQ.'B'.OR.QCDTMP(K,J,2).EQ.'X'.OR.
     4               QCDTMP(K,J,2).EQ.'Q'))THEN

                     CALL M_GETUV(OBTMP(K,J,1),OBTMP(K,J,2),MISS_P,
     1                            OBTMP(K,J,3),OBTMP(K,J,4))

                     CALL M_COMBQC(QCATMP(K,J,1),QCATMP(K,J,2),
     1                             QCRTMP(K,J,1),QCRTMP(K,J,2),
     2                             QCDTMP(K,J,1),QCDTMP(K,J,2),
     3                             QCATMP(K,J,3),QCRTMP(K,J,3),
     4                             QCDTMP(K,J,3))
                     QCATMP(K,J,4) = QCATMP(K,J,3)
                     QCRTMP(K,J,4) = QCRTMP(K,J,3)
                     QCDTMP(K,J,4) = QCDTMP(K,J,3)

                  ENDIF

               ENDDO

            ELSE IF(I.EQ.2)THEN

C     ------------------------------------------------------------------
C     U/V --> DD/FF FOR STATION J
C     ------------------------------------------------------------------
               DO K = 1, MXMAPLEV_P

                  IF(.NOT.(OBTMP(K,J,3).EQ.MISS_P.OR.
     1               OBTMP(K,J,4).EQ.MISS_P.OR.QCDTMP(K,J,3).EQ.'B'.OR.
     2               QCDTMP(K,J,3).EQ.'X'.OR.QCDTMP(K,J,3).EQ.'Q'.OR.
     3               QCDTMP(K,J,4).EQ.'B'.OR.QCDTMP(K,J,4).EQ.'X'.OR.
     4               QCDTMP(K,J,4).EQ.'Q'))THEN

                     CALL M_DDFF(OBTMP(K,J,3),OBTMP(K,J,4),MISS_P,
     1                           OBTMP(K,J,1),OBTMP(K,J,2))

                     CALL M_COMBQC(QCATMP(K,J,3),QCATMP(K,J,4),
     1                             QCRTMP(K,J,3),QCRTMP(K,J,4),
     2                             QCDTMP(K,J,3),QCDTMP(K,J,4),
     3                             QCATMP(K,J,1),QCRTMP(K,J,1),
     4                             QCDTMP(K,J,1))
                     QCATMP(K,J,2) = QCATMP(K,J,1)
                     QCRTMP(K,J,2) = QCRTMP(K,J,1)
                     QCDTMP(K,J,2) = QCDTMP(K,J,1)

                  ENDIF

               ENDDO

            ENDIF

 100     CONTINUE

      ENDDO

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
C     ALSO NOTE THAT OUR RULE IS DON'T DO ANY CONVERSIONS USING DATA
C     THAT HAVE FLUNKED QC.  THEREFORE, THE ACT OF ROTATION WILL KNOCK
C     OUT ALL BAD WINDS, REGARDLESS OF WHETHER OR NOT THE STORED
C     VARIABLE IS WHAT'S BEING RETURNED.
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,LON,STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

         DO J = 1, UAPT_NRECS(FNUM,CD)

            DO K = 1, MXMAPLEV_P

               IF(.NOT.(OBTMP(K,J,3).EQ.MISS_P.OR.
     1            OBTMP(K,J,4).EQ.MISS_P.OR.QCDTMP(K,J,3).EQ.'B'.OR.
     2            QCDTMP(K,J,3).EQ.'X'.OR.QCDTMP(K,J,3).EQ.'Q'.OR.
     3            QCDTMP(K,J,4).EQ.'B'.OR.QCDTMP(K,J,4).EQ.'X'.OR.
     4            QCDTMP(K,J,4).EQ.'Q'))THEN

                  CALL M_ROTFAC(LON(J),LON_XX_U,ROTCON_U,SINDL,COSDL)
                  UTEMP = OBTMP(K,J,3)
                  VTEMP = OBTMP(K,J,4)
                  OBTMP(K,J,3) = COSDL * UTEMP + SINDL * VTEMP
                  OBTMP(K,J,4) =  -SINDL * UTEMP + COSDL * VTEMP
                  CALL M_DDFF(OBTMP(K,J,3),OBTMP(K,J,4),MISS_P,
     1                        OBTMP(K,J,1),OBTMP(K,J,2))

               ELSE

                  DO I = 1, 4
                     OBTMP(K,J,I) = MISS_P
                     QCATMP(K,J,I) = 0
                     QCRTMP(K,J,I) = 0
                     QCDTMP(K,J,I) = 'Z'
                  ENDDO
                  
               ENDIF

            ENDDO

         ENDDO

      ENDIF

C     ------------------------------------------------------------------
C     RETURN THE VARIABLE AND QC ASKED FOR BY THE CALLER.
C     ------------------------------------------------------------------
      DO I = 1, 4
         
         IF(VCN.EQ.LVCN(I))THEN

            DO J = 1, UAPT_NRECS(FNUM,CD)

               DO K = 1, MXMAPLEV_P

                  OB(K,J) = OBTMP(K,J,I)
                  QCAPP(K,J) = QCATMP(K,J,I)
                  QCRES(K,J) = QCRTMP(K,J,I)
                  QCDESC(K,J) = QCDTMP(K,J,I) 

               ENDDO

            ENDDO

            RETURN

         ENDIF

      ENDDO

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