def alt2temp(H, alt_units=default_alt_units, temp_units=default_temp_units):
    """Return the standard temperature for the specified altitude.  Altitude
    units may be feet ('ft'), metres ('m'), statute miles, ('sm') or 
    nautical miles ('nm').  Temperature units may be degrees C, F, K or R 
    ('C', 'F', 'K' or 'R')  
    
    If the units are not specified, the units in default_units.py are used.
    
    Examples:
    
    Calculate the standard temperature (in default temperature units) at 
    5,000 (default altitude units):
    >>> alt2temp(5000)
    5.0939999999999941
    
    Calculate the standard temperature in deg F at sea level:
    >>> alt2temp(0, temp_units = 'F')
    59.0
    
    Calculate the standard temperature in deg K at 11,000 m:
    >>> alt2temp(11000, alt_units = 'm', temp_units = 'K')
    216.64999999999998
    
    Calculate the standard temperature at 11 statute miles in deg R:
    >>> alt2temp(11, alt_units = 'sm', temp_units = 'R')
    389.96999999999997
    
    The input value may be an expression:
    >>> alt2temp(11 * 5280, temp_units = 'R')
    389.96999999999997
    
    """

    # Validated to 84000 m
    # uses meters and degrees K for the internal calculations

    # function tested in tests/test_std_atm.py

    H = U.len_conv(H, from_units=alt_units, to_units='km')

    if H <= 11:
        temp = T0 + H * L0
    elif H <= 20:
        temp = T11
    elif H <= 32:
        temp = T20 + (H - 20) * L20
    elif H <= 47:
        temp = T32 + (H - 32) * L32
    elif H <= 51:
        temp = T47
    elif H <= 71:
        temp = T51 + (H - 51) * L51
    elif H <= 84.852:
        temp = T71 + (H - 71) * L71
    else:
        raise ValueError(
            'This function is only implemented for altitudes of 84.852 km and below.'
        )

    return U.temp_conv(temp, to_units=temp_units, from_units='K')
def press2alt(P, press_units=default_press_units, alt_units=default_alt_units):
    """
    Return the altitude corresponding to the specified pressure, with
    pressure in inches of HG, mm of HG, psi, psf (lb per sq. ft), pa, hpa or
    mb. 
    
    The altitude is in units of feet ('ft'), metres ('m'), statute miles, 
    ('sm') or nautical miles ('nm')
    
    If the units are not specified, the units in default_units.py are used.
    
    Examples:
    
    Calculate the pressure altitude in feet for a pressure of 31.0185 inches
    of HG:
    >>> press2alt(31.0185)
    -999.98992888235091
    
    Calculate the pressure altitude in feet for a pressure of 
    1455.33 lb sq. ft:
    >>> press2alt(1455.33, press_units = 'psf')
    10000.002466564831
    
    Calculate the pressure altitude in metres for a pressure of 
    90.3415 mm HG:
    >>> press2alt(90.3415, press_units = 'mm HG', alt_units = 'm')
    15000.025465320754
    
    Calculate the pressure altitude in metres for a pressure of 
    1171.86 pascal:
    >>> press2alt(1171.86, press_units = 'pa', alt_units = 'm')
    30000.029510365184
    """

    # function tested in tests/test_std_atm.py

    P = U.press_conv(P, from_units=press_units, to_units='in HG')

    if P > P11:
        H = _press2alt_gradient(P, P0, 0, T0, L0)
    elif P > P20:
        H = _press2alt_isothermal(P, P11, 11, T11)
    elif P > P32:
        H = _press2alt_gradient(P, P20, 20, T20, L20)
    elif P > P47:
        H = _press2alt_gradient(P, P32, 32, T32, L32)
    elif P > P51:
        H = _press2alt_isothermal(P, P47, 47, T47)
    elif P > P71:
        H = _press2alt_gradient(P, P51, 51, T51, L51)
    else:
        H = _press2alt_gradient(P, P71, 71, T71, L71)

    if H > 84.852:
        raise ValueError(
            'This function is only implemented for altitudes of 84.852 km and below.'
        )

    return U.len_conv(H, from_units='km', to_units=alt_units)
def QNH(
    HP,
    H,
    alt_units=default_alt_units,
    alt_setting_units='in HG',
):
    """
    Return the altimeter setting, given the pressure altitude (HP) and the 
    barometric altitude (H).
    """

    HP = U.len_conv(HP, from_units=alt_units, to_units='ft')
    H = U.len_conv(H, from_units=alt_units, to_units='ft')
    QNH = P0 * (1 - (HP - H) / 145442.2)**5.255594
    QNH = U.press_conv(QNH, from_units='in HG', to_units=alt_setting_units)

    return QNH
def pressure_alt(H, alt_setting, alt_units=default_alt_units):
    """
    Return the pressure altitude, given the barometric altitude and the
    altimeter setting. 
    
    Altimeter setting may have units of inches of HG, or hpa or mb.  If the
    altimeter setting value is less than 35, the units are assumed to be
    in HG, otherwise they are assumed to be hpa.  The altimeter setting must
    be in the range of 25 to 35 inches of mercury.
    
    The altitude may have units of feet ('ft'), metres ('m'), statute miles,
    ('sm') or nautical miles ('nm').

    If the units are not specified, the units in default_units.py are used.
    
    Examples:
    
    Calculate the pressure altitude for 1,000 (default altitude units) 
    barometric altitude with altimeter setting of 30.92 in HG:
    >>> pressure_alt(1000, 30.92)
    88.612734282205338
    
    Calculate the pressure altitude for 1,000 (default altitude units) 
    barometric altitude with altimeter setting of 1008 mb:
    >>> pressure_alt(1000, 1008)
    1143.6503495627171
    
    Calculate the pressure altitude in metres for 304.8 m barometric 
    altitude with altimeter setting of 1008 mb:
    >>> pressure_alt(304.8, 1008, alt_units = 'm')
    348.58462654671621
    """

    H = U.len_conv(H, from_units=alt_units, to_units='ft')
    if alt_setting > 35:
        alt_setting = U.press_conv(alt_setting,
                                   from_units='hpa',
                                   to_units='in HG')
    if alt_setting < 25 or alt_setting > 35:
        raise ValueError('Altimeter setting out of range.')
    HP = H + 145442.2 * (1 - (alt_setting / P0)**0.190261)
    HP = U.len_conv(HP, from_units='ft', to_units=alt_units)
    return HP
def alt2press_ratio(H, alt_units=default_alt_units):
    """
    Return the pressure ratio (atmospheric pressure / standard pressure
    for sea level).  The altitude is specified in feet ('ft'), metres ('m'),
    statute miles, ('sm') or nautical miles ('nm').   
    
    If the units are not specified, the units in default_units.py are used.
    
    Examples:
    
    Calculate the pressure ratio at 5000 (default altitude units):
    >>> alt2press_ratio(5000)
    0.8320481158727735
    
    Calculate the pressure ratio at 1000 m:
    >>> alt2press_ratio(1000, alt_units = 'm')
    0.88699304638887044
    
    The functions are only implemented at altitudes of 84.852 km and lower.
    >>> alt2press_ratio(90, alt_units = 'km')
    Traceback (most recent call last):
      File '<stdin>', line 1, in ?
      File './std_atm.py', line 189, in alt2press_ratio
    if H <= 20:
    ValueError: This function is only implemented for altitudes of 84.852 km and below.
    """

    # uses meters and degrees K for the internal calculations

    # function tested in tests/test_std_atm.py

    H = U.len_conv(H, from_units=alt_units, to_units='km')

    if H <= 11:
        return _alt2press_ratio_gradient(H, 0, P0, T0, L0)
    if H <= 20:
        return _alt2press_ratio_isothermal(H, 11, P11, T11)
    if H <= 32:
        return _alt2press_ratio_gradient(H, 20, P20, T20, L20)
    if H <= 47:
        return _alt2press_ratio_gradient(H, 32, P32, T32, L32)
    if H <= 51:
        return _alt2press_ratio_isothermal(H, 47, P47, T47)
    if H <= 71:
        return _alt2press_ratio_gradient(H, 51, P51, T51, L51)
    if H <= 84.852:
        return _alt2press_ratio_gradient(H, 71, P71, T71, L71)
    else:
        raise ValueError(
            'This function is only implemented for altitudes of 84.852 km and below.'
        )
def density2alt(Rho,
                density_units=default_density_units,
                alt_units=default_alt_units):
    """
    Return the altitude corresponding to the specified density, with
    density in 'lb/ft**3', 'slug/ft**3' or 'kg/m**3'.  
    
    The altitude is specified in feet ('ft'), metres ('m'), statute miles, 
    ('sm') or nautical miles ('nm').
    
    If the units are not specified, the units in default_units.py are used.
    
    Examples:
    
    Calculate the altitude in default altitude units where the density is 
    0.056475 in default density units:
    >>> density2alt(.056475)
    9999.8040934937271
    
    Calculate the altitude in metres where the density is 0.018012 kg / m 
    cubed:
    >>> density2alt(.018012, alt_units = 'm', density_units = 'kg/m**3')
    29999.978688508152
    """

    # function tested in tests/test_std_atm.py

    Rho = U.density_conv(Rho, from_units=density_units, to_units='kg/m**3')

    if Rho > Rho11:
        H = _density2alt_gradient(Rho, Rho0, 0, T0, L0)
    elif Rho > Rho20:
        H = _density2alt_isothermal(Rho, Rho11, 11, T11)
    elif Rho > Rho32:
        H = _density2alt_gradient(Rho, Rho20, 20, T20, L20)
    elif Rho > Rho47:
        H = _density2alt_gradient(Rho, Rho32, 32, T32, L32)
    elif Rho > Rho51:
        H = _density2alt_isothermal(Rho, Rho47, 47, T47)
    elif Rho > Rho71:
        H = _density2alt_gradient(Rho, Rho51, 51, T51, L51)
    else:
        H = _density2alt_gradient(Rho, Rho71, 71, T71, L71)

    if H > 84.852:
        raise ValueError(
            'This function is only implemented for altitudes of 84.852 km and below.'
        )

    return U.len_conv(H, from_units='km', to_units=alt_units)
def alt2press(H, alt_units=default_alt_units, press_units=default_press_units):
    """
    Return the atmospheric pressure for a given altitude, with the 
    altitude in feet ('ft'), metres ('m'), statute miles, ('sm') or nautical
    miles ('nm'), and the pressure in inches of HG ('in HG'), mm of HG 
    ('mm HG'), psi, lb per sq. ft ('psf'), pa, hpa or mb.  
    
    If the units are not specified, the units in default_units.py are used.
    
    Examples:
    
    Calculate the pressure in inches of mercury at 5,000 (default altitude 
    units):
    >>> alt2press(5000)
    24.895961289464015
    
    Calculate the pressure in pounds per square foot at 10,000 (default 
    altitude units):
    >>> alt2press(10000, press_units = 'psf')
    1455.3301392981359
    
    Calculate the pressure in pascal at 20 km:
    >>> alt2press(20, press_units = 'pa', alt_units = 'km')
    5474.8827144576408
    """

    # uses meters, inches of HG and degrees K for the internal calculations

    # function tested in tests/test_std_atm.py

    H = U.len_conv(H, from_units=alt_units, to_units='m')

    press = P0 * alt2press_ratio(H, alt_units='m')
    press = U.press_conv(press, from_units='in HG', to_units=press_units)

    return press