Completed
Pull Request — master (#515)
by
unknown
59s
created

mean_wind_pressure_weighted()   B

Complexity

Conditions 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 42
rs 8.8571
1
# Copyright (c) 2008-2017 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
"""Contains calculation of various derived indices."""
5
import numpy as np
6
7
from .thermo import mixing_ratio, saturation_vapor_pressure
8
from .tools import get_layer
9
from ..constants import g, rho_l
10
from ..package_tools import Exporter
11
from ..units import check_units, units
12
13
exporter = Exporter(globals())
14
15
16
@exporter.export
17
@check_units('[temperature]', '[pressure]', '[pressure]')
18
def precipitable_water(dewpt, pressure, top=400 * units('hPa')):
19
    r"""Calculate precipitable water through the depth of a sounding.
20
21
    Default layer depth is sfc-400 hPa. Formula used is:
22
23
    .. math:: \frac{1}{pg} \int\limits_0^d x \,dp
24
25
    from [Tsonis2008]_, p. 170.
26
27
    Parameters
28
    ----------
29
    dewpt : `pint.Quantity`
30
        Atmospheric dewpoint profile
31
    pressure : `pint.Quantity`
32
        Atmospheric pressure profile
33
    top: `pint.Quantity`, optional
34
        The top of the layer, specified in pressure. Defaults to 400 hPa.
35
36
    Returns
37
    -------
38
    `pint.Quantity`
39
        The precipitable water in the layer
40
41
    """
42
    sort_inds = np.argsort(pressure[::-1])
43
    pressure = pressure[sort_inds]
44
    dewpt = dewpt[sort_inds]
45
46
    pres_layer, dewpt_layer = get_layer(pressure, dewpt, depth=pressure[0] - top)
47
48
    w = mixing_ratio(saturation_vapor_pressure(dewpt_layer), pres_layer)
49
    # Since pressure is in decreasing order, pw will be the negative of what we want.
50
    # Thus the *-1
51
    pw = -1. * (np.trapz(w.magnitude, pres_layer.magnitude) * (w.units * pres_layer.units) /
52
                (g * rho_l))
53
    return pw.to('millimeters')
54
55
56
@exporter.export
57
@check_units('[speed]', '[speed]', '[pressure]', '[length]')
58
def mean_wind_pressure_weighted(u, v, p, heights=None, bottom=None, depth=None):
59
    r"""Calculate pressure-weighted mean wind through a layer.
60
61
    Layer top and bottom specified in height or pressure.
62
63
    Parameters
64
    ----------
65
    u : `pint.Quantity`
66
        U-component of wind.
67
    v : `pint.Quantity`
68
        V-component of wind.
69
    p : `pint.Quantity`
70
        Atmospheric pressure profile
71
    heights : `pint.Quantity`, optional
72
        Heights from sounding
73
    bottom: `pint.Quantity`, optional
74
        The bottom of the layer in either the provided height coordinate
75
        or in pressure. Don't provide in meters AGL unless the provided
76
        height coordinate is meters AGL. Default is the first observation,
77
        assumed to be the surface.
78
    depth: `pint.Quantity`, optional
79
        The depth of the layer in meters or hPa.
80
81
    Returns
82
    -------
83
    `pint.Quantity`
84
        u_mean: u-component of layer mean wind.
85
    `pint.Quantity`
86
        v_mean: v-component of layer mean wind.
87
88
    """
89
    layer_p, layer_u, layer_v = get_layer(p, u, v, heights=heights,
90
                                          bottom=bottom, depth=depth)
91
    # Taking the integral of the weights (pressure) to feed into the weighting
92
    # function. Said integral works out to this function:
93
    pres_int = 0.5 * (layer_p[-1].magnitude**2 - layer_p[0].magnitude**2)
94
    u_mean = (np.trapz(layer_u * layer_p, x=layer_p) / pres_int)
95
    v_mean = (np.trapz(layer_v * layer_p, x=layer_p) / pres_int)
96
97
    return u_mean * u.units, v_mean * v.units
98