Completed
Pull Request — master (#515)
by
unknown
01:01
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, log_interp
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
def mean_wind_pressure_weighted(u, v, p, hgt, depth, bottom=None, obs_only=False):
58
    r"""Calculate pressure-weighted mean wind through a layer.
59
60
    Layer bottom and depth specified in meters AGL.
61
62
    Parameters
63
    ----------
64
    u : array-like
65
        U-component of wind.
66
    v : array-like
67
        V-component of wind.
68
    p : array-like
69
        Atmospheric pressure profile
70
    hgt : array-like
71
        Heights from sounding
72
    depth: `pint.Quantity`
73
        The depth of the layer in meters.
74
    bottom: `pint.Quantity`
75
        The bottom of the layer in meters AGL.
76
        Default is the first observation, assumed to be the surface.
77
78
    Returns
79
    -------
80
    `pint.Quantity`
81
        u_mean: u-component of layer mean wind, in m/s
82
    `pint.Quantity`
83
        v_mean: v-component of layer mean wind, in m/s
84
85
    """
86
    u = u.to('meters/second')
87
    v = v.to('meters/second')
88
    if bottom:
89
        bottom = bottom + hgt[0]
90
    w_int = get_layer(p, u, v, heights=hgt, bottom=bottom, depth=depth)
91
92
    if obs_only:
93
        u_mean = ma.average(w_int[1], weights = w_int[0]) * units('m/s')
94
        v_mean = ma.average(w_int[2], weights = w_int[0]) * units('m/s')
95
96
    else:
97
        u_mean = np.trapz(w_int[1] * w_int[0], x=w_int[0]) / np.trapz(w_int[0],
98
                          x=w_int[0]) * units('m/s')
99
        v_mean = np.trapz(w_int[2] * w_int[0], x=w_int[0]) / np.trapz(w_int[0],
100
                          x=w_int[0]) * units('m/s')
101
102
    return u_mean, v_mean
103
    
104