Code Duplication    Length = 48-48 lines in 2 locations

metpy/calc/thermo.py 1 location

@@ 1371-1418 (lines=48) @@
1368
            mean_dewpoint.to(dewpt.units))
1369
1370
1371
@exporter.export
1372
@check_units('[pressure]')
1373
def mixed_layer(p, *args, **kwargs):
1374
    r"""Mix variable(s) over a layer, yielding a mass-weighted average.
1375
1376
    This function will integrate a data variable with respect to pressure and determine the
1377
    average value using the mean value theorem.
1378
1379
    Parameters
1380
    ----------
1381
    p : array-like
1382
        Atmospheric pressure profile
1383
    datavar : array-like
1384
        Atmospheric variable measured at the given pressures
1385
    heights: array-like, optional
1386
        Atmospheric heights corresponding to the given pressures (default None)
1387
    bottom : `pint.Quantity`, optional
1388
        The bottom of the layer as a pressure or height above the surface pressure
1389
        (default None)
1390
    depth : `pint.Quantity`, optional
1391
        The thickness of the layer as a pressure or height above the bottom of the layer
1392
        (default 100 hPa)
1393
    interpolate : bool, optional
1394
        Interpolate the top and bottom points if they are not in the given data
1395
1396
    Returns
1397
    -------
1398
    `pint.Quantity`
1399
        The mixed value of the data variable.
1400
1401
    """
1402
    # Pull out keyword arguments, remove when we drop Python 2.7
1403
    heights = kwargs.pop('heights', None)
1404
    bottom = kwargs.pop('bottom', None)
1405
    depth = kwargs.pop('depth', 100 * units.hPa)
1406
    interpolate = kwargs.pop('interpolate', True)
1407
1408
    layer = get_layer(p, *args, heights=heights, bottom=bottom,
1409
                      depth=depth, interpolate=interpolate)
1410
    p_layer = layer[0]
1411
    datavars_layer = layer[1:]
1412
1413
    ret = []
1414
    for datavar_layer in datavars_layer:
1415
        actual_depth = abs(p_layer[0] - p_layer[-1])
1416
        ret.append((-1. / actual_depth.m) * np.trapz(datavar_layer, p_layer) *
1417
                   datavar_layer.units)
1418
    return ret
1419

metpy/calc/indices.py 1 location

@@ 56-103 (lines=48) @@
53
        bottom = np.nanmax(pressure) * pressure.units
54
55
    pres_layer, dewpt_layer = get_layer(pressure, dewpt, bottom=bottom, depth=bottom - top)
56
57
    w = mixing_ratio(saturation_vapor_pressure(dewpt_layer), pres_layer)
58
59
    # Since pressure is in decreasing order, pw will be the opposite sign of that expected.
60
    pw = -1. * (np.trapz(w.magnitude, pres_layer.magnitude) * (w.units * pres_layer.units) /
61
                (g * rho_l))
62
    return pw.to('millimeters')
63
64
65
@exporter.export
66
@check_units('[pressure]')
67
def mean_pressure_weighted(pressure, *args, **kwargs):
68
    r"""Calculate pressure-weighted mean of an arbitrary variable through a layer.
69
70
    Layer top and bottom specified in height or pressure.
71
72
    Parameters
73
    ----------
74
    pressure : `pint.Quantity`
75
        Atmospheric pressure profile
76
    *args : `pint.Quantity`
77
        Parameters for which the pressure-weighted mean is to be calculated.
78
    heights : `pint.Quantity`, optional
79
        Heights from sounding. Standard atmosphere heights assumed (if needed)
80
        if no heights are given.
81
    bottom: `pint.Quantity`, optional
82
        The bottom of the layer in either the provided height coordinate
83
        or in pressure. Don't provide in meters AGL unless the provided
84
        height coordinate is meters AGL. Default is the first observation,
85
        assumed to be the surface.
86
    depth: `pint.Quantity`, optional
87
        The depth of the layer in meters or hPa.
88
89
    Returns
90
    -------
91
    `pint.Quantity`
92
        u_mean: u-component of layer mean wind.
93
    `pint.Quantity`
94
        v_mean: v-component of layer mean wind.
95
96
    """
97
    heights = kwargs.pop('heights', None)
98
    bottom = kwargs.pop('bottom', None)
99
    depth = kwargs.pop('depth', None)
100
    ret = []  # Returned variable means in layer
101
    layer_arg = get_layer(pressure, *args, heights=heights,
102
                          bottom=bottom, depth=depth)
103
    layer_p = layer_arg[0]
104
    layer_arg = layer_arg[1:]
105
    # Taking the integral of the weights (pressure) to feed into the weighting
106
    # function. Said integral works out to this function: