Completed
Pull Request — master (#515)
by
unknown
01:48
created

mean_wind_pressure_weighted()   A

Complexity

Conditions 3

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 59
rs 9.597

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
@exporter.export
58
@check_units('[speed]', '[speed]', '[pressure]', '[length]', '[length]', '[length]')
59
def mean_wind_pressure_weighted(u, v, p, hgt, top, bottom=None, interp=False):
60
    r"""Calculate pressure-weighted mean wind through a layer.
61
62
    Layer top and bottom specified in meters AGL. Options are included to use only
63
    observed winds or interpolate through the layer to match NSHARP/SharpPy's output.
64
65
    Parameters
66
    ----------
67
    u : array-like
68
        U-component of wind.
69
    v : array-like
70
        V-component of wind.
71
    p : array-like
72
        Atmospheric pressure profile
73
    hgt : array-like
74
        Heights from sounding
75
    top: `pint.Quantity`
76
        The top of the layer in meters AGL
77
    bottom: `pint.Quantity`, optional
78
        The bottom of the layer in meters AGL.
79
        Default is the surface.
80
    interp: boolean, optional
81
        Determines whether to use only observations or interpolate.
82
        Default is only observations.
83
84
    Returns
85
    -------
86
    `pint.Quantity`
87
        u_mean: u-component of layer mean wind, in m/s
88
    `pint.Quantity`
89
        v_mean: v-component of layer mean wind, in m/s
90
91
    """
92
    if bottom:
93
        depth_s = top - bottom
94
        bottom = bottom + hgt[0]
95
    else:
96
        depth_s = top
97
98
    if interp:
99
        dp = -1
100
        pressure_top = np.interp(top.magnitude, hgt.magnitude - hgt[0].magnitude,
101
                                 np.log(p.magnitude))
102
        pressure_top = np.exp(pressure_top)
103
        interp_levels = (np.arange(p[0].magnitude, pressure_top + dp, dp)) * units('hPa')
104
        u_int = log_interp(interp_levels, p, u)
105
        v_int = log_interp(interp_levels, p, v)
106
        h_int = log_interp(interp_levels, p, hgt)
107
        w_int = get_layer(interp_levels, u_int, v_int, heights=h_int,
108
                          bottom=bottom, depth=depth_s)
109
    else:
110
        w_int = get_layer(p, u, v, heights=hgt, bottom=bottom, depth=depth_s)
111
112
    u_mean = ma.average(w_int[1], weights=w_int[0]) * u.units
113
    v_mean = ma.average(w_int[2], weights=w_int[0]) * v.units
114
115
    return u_mean, v_mean
116