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

mean_wind_pressure_weighted()   B

Complexity

Conditions 3

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 46
rs 8.9411
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 indicies."""
5
import numpy as np
6
import numpy.ma as ma
7
8
from .thermo import mixing_ratio, saturation_vapor_pressure
9
from .tools import get_layer
10
from ..constants import g, rho_l
11
from ..package_tools import Exporter
12
from ..units import check_units, units
13
14
exporter = Exporter(globals())
15
16
17
@exporter.export
18
@check_units('[temperature]', '[pressure]', '[pressure]')
19
def precipitable_water(dewpt, p, top=400 * units('hPa')):
20
    r"""Calculate precipitable water through the depth of a sounding.
21
22
    Default layer depth is sfc-400 hPa. Formula used is:
23
24
    .. math:: \frac{1}{pg} \int\limits_0^d x \,dp
25
26
    from [Tsonis2008]_, p. 170.
27
28
    Parameters
29
    ----------
30
    dewpt : array-like
31
        Atmospheric dewpoint profile
32
    p : array-like
33
        Atmospheric pressure profile
34
    top: `pint.Quantity`
35
        The top of the layer, specified in pressure.
36
37
    Returns
38
    -------
39
    `pint.Quantity`
40
        The precipitable water in the layer, in inches
41
42
    """
43
    sort_inds = np.argsort(p[::-1])
44
    p = p[sort_inds]
45
    dewpt = dewpt[sort_inds]
46
47
    pres_layer, dewpt_layer = get_layer(p, dewpt, depth=p[0] - top)
48
49
    w = mixing_ratio(saturation_vapor_pressure(dewpt_layer), pres_layer)
50
    # Since pressure is in decreasing order, pw will be the negative of what we want.
51
    # Thus the *-1
52
    pw = -1. * (np.trapz(w.magnitude, pres_layer.magnitude) * (w.units * pres_layer.units) /
53
                (g * rho_l))
54
    return pw.to('millimeters')
55
56
57
@exporter.export
58
@check_units('[speed]', '[speed]', '[pressure]', '[length]')
59
def mean_wind_pressure_weighted(u, v, p, hgt, depth, bottom=None, obs_only=False):
60
    r"""Calculate pressure-weighted mean wind through a layer.
61
62
    Layer bottom and depth specified in meters AGL.
63
64
    Parameters
65
    ----------
66
    u : array-like
67
        U-component of wind.
68
    v : array-like
69
        V-component of wind.
70
    p : array-like
71
        Atmospheric pressure profile
72
    hgt : array-like
73
        Heights from sounding
74
    depth: `pint.Quantity`
75
        The depth of the layer in meters.
76
    bottom: `pint.Quantity`
77
        The bottom of the layer in meters AGL.
78
        Default is the first observation, assumed to be the surface.
79
80
    Returns
81
    -------
82
    `pint.Quantity`
83
        u_mean: u-component of layer mean wind, in m/s
84
    `pint.Quantity`
85
        v_mean: v-component of layer mean wind, in m/s
86
87
    """
88
    u = u.to('meters/second')
89
    v = v.to('meters/second')
90
    if bottom:
91
        bottom = bottom + hgt[0]
92
    w_int = get_layer(p, u, v, heights=hgt, bottom=bottom, depth=depth)
93
94
    if obs_only:
95
        u_mean = ma.average(w_int[1], weights=w_int[0])
96
        v_mean = ma.average(w_int[2], weights=w_int[0])
97
98
    else:
99
        u_mean = np.trapz(w_int[1] * w_int[0], x=w_int[0]) / np.trapz(w_int[0], x=w_int[0])
100
        v_mean = np.trapz(w_int[2] * w_int[0], x=w_int[0]) / np.trapz(w_int[0], x=w_int[0])
101
102
    return u_mean * units('m/s'), v_mean * units('m/s')
103