C$$$   SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    M_TV2TQ     DEVIRTUALIZE TEMPERATURE
C
C   PRGMMR:  BENJAMIN, STAN ORG: ERL/PROFS      DATE: 93-01-12
C
C ABSTRACT:  CALCULATES TEMPERATURE AND SPECIFIC HUMIDITY FROM
C            VIRTUAL TEMPERATURE, PRESSURE, AND RELATIVE HUMIDITY
C
C PROGRAM HISTORY LOG:
C   92       S. BENJAMIN     ORIGINAL VERSION
C   94-05-16 S. BENJAMIN     IMPROVE ACCURACY BY USING SPECIFIC
C                             HUMIDITY INSTEAD OF MIXING RATIO.
C                             ALSO HAS FASTER CONVERGENCE.
C                             ADD ORDER OF ACCURACY TO 0.6078
C                             CONSTANT.
C   94-05-23 S. BENJAMIN     LIMIT ALGORITHM TO 3 ITERATIONS
C                             FOR MORE SPEED.
C   95-01-02 S. BENJAMIN     NEW VERSION
C   95-07-28 S. Benjamin     Limit number of iterations
C
C USAGE:           CALL M_TV2TQ(TV, RH, P, T, Q)
C
C   INPUT ARGUMENT LIST:
C   TV          - REAL           VIRTUAL TEMPERATURE IN KELVIN
C   RH          - REAL           RELATIVE HUMIDITY
C   P           - REAL           PRESSURE IN MILIBARS
C
C   OUTPUT ARGUMENT LIST:
C   T           - REAL           TEMPERATURE IN KELVIN
C   Q           - REAL           SPECIFIC HUMIDITY (G/G)
C
C REMARKS: NONE
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE: DEC - VAX, VMS
C

C$$$
        SUBROUTINE M_TV2TQ(TV,RH,P,T,Q)
C
C   THIS ROUTINE RETURNS TEMPERATURE (KELVIN) AND SPECIFIC HUMIDITY
C   (G/G) GIVEN VIRTUAL TEMPERATURE (KELVIN), PRESSURE (MB), AND
C   RELATIVE HUMIDITY (RANGE 0.0-1.0).

C   IT USES AN ITERATIVE NEWTON-RAPHSON TECHNIQUE.  FOUR ITERATIONS ARE
C   GENERALLY ADEQUATE TO PROVIDE CONVERGENCE TO 5 DECIMAL PLACES.
C   THE WOBUS FUNCTION FOR SATURATION VAPOR PRESSURE OVER LIQUID WATER
C   IS USED

C        S.G. BENJAMIN  13-DEC-1990        Original version
C        S.G. BENJAMIN  02-JAN-1995        New version

        INCLUDE 'IMPLICIT'
        REAL TV,RH,P,T,Q
        REAL TG,QG,FAC,FAC1,TVG,DTVDT,TGNEW
        REAL M_ESW
        INTEGER IT

        IT = 0
C -- TG - guess at non-virtual temperature
        TG = TV/(1.+0.6078*0.62197*RH*M_ESW(TV)/P)
10      CONTINUE
        IT = IT + 1
C -- QG - guess at specific humidity
        QG = 0.62197*RH*M_ESW(TG)/P
        FAC = (1.+0.6078*QG)
C -- TVG - guess at virtual temperature
        TVG = TG*FAC
        FAC1= (1.+0.6078*0.62197*RH*M_ESW(TG+0.1)/P)
C -- DTVDT - d(Tv)/dT estimated over 0.1 K interval
C       from 1st term Taylor series expansion
        DTVDT = FAC + TG*(FAC1-FAC)/0.1
        TGNEW = TG + (TV-TVG)/DTVDT
C        PRINT *,',TV,TVG,TGNEW,QG,FAC,FAC1,DTVDT',TV,TVG,TGNEW,
C    1  QG,FAC,FAC1,DTVDT
        IF (ABS(TV-TVG).LT.0.01) GO TO 20
        IF (IT.GT.6) GO TO 20
        TG = TGNEW
        GO TO 10
20      CONTINUE
        T = TGNEW
        Q = 0.62197*RH*M_ESW(T)/P

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