C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MUAPTTV     GET UPPER-AIR-POINT VIRTUAL TEMPERATURES
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 01-07-23
C
C ABSTRACT: GET UPPER-AIR-POINT VIRTUAL TEMPERATURE OBS FOR ALL 
C   STATIONS IN ONE NETCDF FILE FOR ONE TIME.
C
C PROGRAM HISTORY LOG:
C   01-07-23  M. BARTH    ORIGINAL VERSION
C   02-05-29  M. BARTH    RESTRUCTURED TEMPORARY MEMORY ARRAY USAGE
C   03-06-06  M. BARTH    ADDED VALIDITY CHECK BEFORE DOING CONVERSIONS.
C                         (SEE REMARKS BELOW).
C   03-07-03  M. BARTH    - ADDED RDMTR DATASET.
C                         - FIXED BUG THAT WOULD HAVE PROVIDED TOTALLY
C                           WRONG TV FOR AWIPS DATABASE ACARS DATA.
C                           THE BUG WAS THAT PRESSURE OBS WERE READ IN
C                           AND ASSUMED TO BE ALTITUDE, THEN CONVERTED
C                           TO PRESSURE USING M_ZTOPSA...
C   04-10-16  L. BENJAMIN ADDED NO SUCH VARIABLE CHECK FOR QCA CHECKS
C   06-07-20  L. BENJAMIN ADDED NO SUCH VARIABLE CHECK FOR CHAR QC.
C   07-11-21  L. BENJAMIN REMOVED COMMENTED OUT CODE
C   08-08-15  L. BENJAMIN ADDED MISS_P AS MISSING DATA ARGUMENT FOR
C                         M_ZTOPSA.
C
C USAGE:    CALL MUAPTTV (DSN,FNUM,NV,OB,QCDESC,QCAPP,QCRES,STATUS)
C
C   INPUT ARGUMENT LIST:
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:    MUAPTMOI, M_TV, M_ZTOPSA, MQCFLTR, MUAPTHTP
C
C REMARKS:
C   - THE QC FROM THE TEMPERATURE VARIABLE IS USED FOR TV.
C   - NORMALLY, THE MADIS API WILL RETURN A MISSING VALUE IF ANY OF 
C     THE VARIABLES NEEDED IN CONVERSIONS FAILED QC OR ARE MISSING.
C     HOWEVER, PER DATA ASSIMILATION PRACTICE, WE WILL RETURN 
C     TEMPERATURE (IF IT PASSED THE HIGHEST LEVEL OF QC) UNDER 
C     THOSE CIRCUMSTANCES AND SET THE QC DESCRIPTOR TO "T" SO USERS 
C     WHO DON'T WANT THAT CAN DETECT IT.
C   - WHEN CALCULATING Q FOR ACARS & RDMTR, NOTE THAT THE PRESSURE 
C     USED IS CALCULATED USING THE STANDARD ATMOSPHERE CALCULATION.
C   - A VALIDITY CHECK IS PERFORMED BEFORE DOING CERTAIN CONVERSIONS.
C     THIS IS NEEDED TO COVER THE CASE OF *REALLY* BAD DATA FROM THE
C     AWIPS DATABASE, WHICH DOESN'T HAVE ANY QC DONE ON IT, FROM 
C     BLOWING UP A CALCULATION.  THE VALIDITY CHECK LIMITS ARE VERY
C     GROSS, AND ARE DESIGNED SIMPLY TO KEEP CALCULATIONS FROM 
C     BLOWING UP.
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 MUAPTTV (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 DSN,FNUM,NV,STATUS
      REAL OB(*),QCAPP(*),QCRES(*)
      CHARACTER*1 QCDESC(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER MXTMP
      PARAMETER (MXTMP = MXRAOB_P * MXRAOBLEV_P)
      REAL M_TV,M_ZTOPSA
      INTEGER I,VCN,LEN1REC,CD,DUMMY,SAVE_QCTYPE
      REAL OBTMP(MXTMP,2),QCATMP(MXTMP,2),QCRTMP(MXTMP,2)
      CHARACTER*1 QCDTMP(MXTMP,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)

C     ------------------------------------------------------------------
C     GET THE TEMPERATURE AND ITS QC INFO.
C     ------------------------------------------------------------------
      CD = DSN - MAX_SFCDSET_P

      VCN = UAPTT_VN_P
      CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN)),UAPT_NCID(FNUM,CD),
     1            UAPT_RECDIM(FNUM,CD),1,UAPT_NRECS(FNUM,CD),MISS_P,OB,
     2            STATUS)
      IF(STATUS.NE.SUCCESS_P)RETURN

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

C     ------------------------------------------------------------------
C     GET TD, P AND THEIR QC.  NOTE THAT WE HAVE TO CALL MUAPTMOI TO GET
C     THE TD BECAUSE IT MIGHT CREATE THIS VARIABLES FROM OTHERS.  
C     ------------------------------------------------------------------
      CALL MUAPTMOI(UAPTTD_VN_P,DSN,FNUM,NV,OBTMP(1,1),QCDTMP(1,1),
     1              QCATMP(1,1),QCRTMP(1,1),STATUS)
      IF(STATUS.NE.SUCCESS_P)RETURN

      IF(CD.EQ.RAOB_DS_P.OR.CD.EQ.ACARS_DS_P.OR.CD.EQ.ACARSP_DS_P)THEN

         IF(CD.EQ.RAOB_DS_P)THEN
            VCN = RAOBP_VN_P
         ELSE IF(CD.EQ.ACARS_DS_P)THEN
            VCN = ACARSP_VN_P
         ELSE IF(CD.EQ.ACARSP_DS_P)THEN
            VCN = ACARSPP_VN_P
         ENDIF

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

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

            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),QCDTMP(1,2),LEN1REC,STATUS)

            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               QCSTATUS = EQCFAILED_P
               STATUS = SUCCESS_P
               DO 21 I = 1, UAPT_NRECS(FNUM,CD) * NV
                  QCDTMP(I,2) = 'Z'
 21            CONTINUE
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               RETURN
            ENDIF

         ELSE

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

         ENDIF

      ELSE IF(CD.EQ.RDMTR_DS_P)THEN

         CALL MUAPTHTP(RDMTRP_VN_P,DSN,FNUM,.FALSE.,NV,OBTMP(1,2),
     1                 QCDTMP(1,2),DUMMY,DUMMY,STATUS)
         IF(STATUS.NE.SUCCESS_P)RETURN

      ENDIF

      IF((CD.EQ.ACARS_DS_P.AND.CDBTYPE.EQ.FSL_DB_P).OR.
     1   CD.EQ.ACARSP_DS_P)THEN

C     ------------------------------------------------------------------
C     THE ACARS "PRESSURES" NEED TO BE CONVERTED FROM ALT TO PRESSURE.
C     (NOTE THAT M_ZTOPSA RETURNS P IN MB.)  ALSO NOTE THAT THE AWIPS
C     DATABASE ACARS DATA HAVE PRESSURE IN THE FILES, BUT THE AWIPS
C     ACARSP DATA DON'T HAVE IT.
C     ------------------------------------------------------------------
         DO 3 I = 1, UAPT_NRECS(FNUM,CD) * NV

            IF(OBTMP(I,2).NE.MISS_P.AND.OBTMP(I,2).GT.-99999.0)THEN
               OBTMP(I,2) = M_ZTOPSA(OBTMP(I,2),MISS_P)
            ELSE
               OBTMP(I,2) = MISS_P
            ENDIF

 3       CONTINUE

      ELSE 

C     ------------------------------------------------------------------
C     P'S RETURNED IN PASCALS NEED TO BE CONVERTED TO MB.
C     ------------------------------------------------------------------
         DO 33 I = 1, UAPT_NRECS(FNUM,CD) * NV

            IF(OBTMP(I,2).NE.MISS_P.AND.OBTMP(I,2).GT.0.0.AND.
     1         OBTMP(I,2).LE.110000.0)THEN
               OBTMP(I,2) = OBTMP(I,2)/100.
            ELSE
               OBTMP(I,2) = MISS_P
            ENDIF

 33      CONTINUE

      ENDIF

C     ------------------------------------------------------------------
C     SAVE THE USER'S CURRENT QC FILTERING SELECTION AND THEN SET IT TO
C     "HIGHEST POSSIBLE".  WE'LL USE THE MQCFLTR ROUTINE WHEN DECIDING 
C     IF WE SHOULD RETURN T WHEN WE CAN'T RETURN TV.  THEN WHEN WE'RE
C     DONE, WE'LL RESTORE THE USER'S VALUE.
C     ------------------------------------------------------------------
      SAVE_QCTYPE = QCTYPE
      QCTYPE = 99
      VCN = UAPTT_VN_P

C     ------------------------------------------------------------------
C     IF ALL THE VARIABLES NEEDED FOR THE CONVERSION EXIST AND PASSED
C     QC, RETURN THE VIRTUAL TEMP IN OB(I), OTHERWISE SEE IF THE 
C     TEMPERATURE PASSED THE HIGHEST LEVEL OF QC FOR THIS DATA TYPE.
C     IF SO, WE'LL RETURN THE TEMPERATURE AND ITS QC, AND CHANGE THE
C     QC DESCRIPTOR TO "T".  IF NOT, MISSING.
C     (NOTE THAT M_TV EXPECTS P IN MB.)
C     ------------------------------------------------------------------
      DO 4 I = 1, UAPT_NRECS(FNUM,CD) * NV
         
         IF(.NOT.(OB(I).EQ.MISS_P.OR.QCDESC(I).EQ.'B'.OR.
     1      QCDESC(I).EQ.'X'.OR.QCDESC(I).EQ.'Q'.OR.
     2      OBTMP(I,1).EQ.MISS_P.OR.QCDTMP(I,1).EQ.'B'.OR.
     3      QCDTMP(I,1).EQ.'X'.OR.QCDTMP(I,1).EQ.'Q'.OR.
     4      OBTMP(I,2).EQ.MISS_P.OR.QCDTMP(I,2).EQ.'B'.OR.
     5      QCDTMP(I,2).EQ.'X'.OR.QCDTMP(I,2).EQ.'Q'.OR.
     6      OB(I).LT.89.0.OR.OB(I).GT.422.0.OR.
     7      OBTMP(I,1).LT.89.0.OR.OBTMP(I,1).GT.422.0.OR.
     8      OBTMP(I,2).LE.0.0.OR.OBTMP(I,2).GT.1100.0))THEN

            OB(I) = M_TV(OB(I),OBTMP(I,1),OBTMP(I,2),MISS_P)

         ELSE IF(OB(I).NE.MISS_P)THEN

            CALL MQCFLTR(VCN,DSN,1,1,QCDESC(I),DUMMY,DUMMY,OB(I))

            IF(OB(I).NE.MISS_P)THEN
               
               QCDESC(I) = 'T'

            ELSE

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

            ENDIF

         ENDIF

 4    CONTINUE

      QCTYPE = SAVE_QCTYPE

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