'A collection of generic calculation functions.'

import numpy as np
from numpy.ma import log, exp
from metpy.cbook import iterable

sat_pressure_0c = 6.112 # mb
def vapor_pressure(temp):
    '''
    Calculate the saturation water vapor (partial) pressure given
    *temperature*.

    temp : scalar or array
        The temperature in degrees Celsius.

    Returns : scalar or array
        The saturation water vapor (partial) presure in millibars, with
        the same shape as *temp*.

    Instead of temperature, dewpoint may be used in order to calculate
    the actual (ambient) water vapor (partial) pressure.
    '''
    return sat_pressure_0c * exp(17.67 * temp / (temp + 243.5))

def dewpoint(temp, rh):
    '''
    Calculate the ambient dewpoint given air temperature and relative
    humidity.

    temp : scalar or array
        The temperature in degrees Celsius.

    rh : scalar or array
        The relative humidity expressed as a ratio in the range [0, 1]

    Returns : scalar or array
        The dew point temperature in degrees Celsius, with the shape
        of the result being determined using numpy's broadcasting rules.
    '''
    es = vapor_pressure(temp)
    val = log(rh * es/sat_pressure_0c)
    return 243.5 * val / (17.67 - val)

def mixing_ratio(part_press, tot_press):
    '''
    Calculates the mixing ratio of gas given its partial pressure
    and the total pressure of the air.

    part_press : scalar or array
        The partial pressure of the constituent gas.

    tot_press : scalar or array
        The total air pressure.

    Returns : scalar or array
        The (mass) mixing ratio, unitless (e.g. Kg/Kg or g/g)

    There are no required units for the input arrays, other than that
    they have the same units.
    '''
    return part_press / (tot_press - part_press)

@@ -91,16 +91,26 @@
 log, exp
+, cos, sin
 %0Afrom me
@@ -1827,8 +1827,610 @@
 _press)%0A
+%0Adef get_wind_components(speed, wdir):%0A    '''%0A    Calculate the U, V wind vector components from the speed and%0A    direction (from which the wind is blowing).%0A%0A    speed : scalar or array%0A        The wind speed (magnitude)%0A%0A    wdir : scalar or array%0A        The wind direction in degrees%0A%0A    Returns : tuple of scalars or arrays%0A        The tuple (U,V) corresponding to the wind components in the%0A        X (East-West) and Y (North-South) directions, respectively.%0A    '''%0A    from scipy.constants import degree%0A    u = -speed * sin(wdir * degree)%0A    v = -speed * cos(wdir * degree)%0A    return u,v%0A

Add a function for calculating wind components from speed and (meteorological) direction.