def control_valve_choke_P_g(xT, gamma, P1=None, P2=None):
    """Calculates either the upstream or downstream pressure at which choked
    flow though a gas control valve occurs, given either a set upstream or 
    downstream pressure. Implements an analytical solution of 
    the needed equations from the full function
    :py:func:`~.size_control_valve_g`. A singularity arises as `xT` goes to 1
    and `gamma` goes to 1.4.
    
    .. math::
        P_1 = - \\frac{7 P_{2}}{5 \\gamma x_T - 7}
        
    .. math::
        P_2 = \\frac{P_{1}}{7} \\left(- 5 \\gamma x_T + 7\\right)
    
    Parameters
    ----------
    xT : float, optional
        Pressure difference ratio factor of a valve without fittings at choked
        flow [-]
    gamma : float
        Specific heat capacity ratio [-]
    P1 : float, optional
        Absolute pressure upstream of the valve [Pa]
    P2 : float, optional
        Absolute pressure downstream of the valve [Pa]

    Returns
    -------
    P_choke : float
        Pressure at which a choke occurs in the gas valve [Pa]

    Notes
    -----
    Extremely cheap to compute.
    
    Examples
    --------
    >>> control_valve_choke_P_g(1, 1.3, 1E5)
    7142.857142857143
    >>> control_valve_choke_P_g(1, 1.3, P2=7142.857142857143)
    100000.0
    """
    if P2 is None:
        ans = P2 = P1 * (-5.0 * gamma * xT + 7.0) / 7.0
    elif P1 is None:
        ans = P1 = -7.0 * P2 / (5.0 * gamma * xT - 7.0)
    else:
        raise Exception('Either P1 or P2 needs to be specified')
    return ans
