Skip to content

Commit

Permalink
Remove use of kwarg.pop wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
zbruick committed Oct 21, 2019
1 parent e0bd6d9 commit f7cb562
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 81 deletions.
5 changes: 1 addition & 4 deletions src/metpy/calc/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def precipitable_water(dewpt, pressure, bottom=None, top=None):
@exporter.export
@preprocess_xarray
@check_units('[pressure]')
def mean_pressure_weighted(pressure, *args, **kwargs):
def mean_pressure_weighted(pressure, *args, heights=None, bottom=None, depth=None):
r"""Calculate pressure-weighted mean of an arbitrary variable through a layer.
Layer top and bottom specified in height or pressure.
Expand Down Expand Up @@ -99,9 +99,6 @@ def mean_pressure_weighted(pressure, *args, **kwargs):
v_mean: v-component of layer mean wind.
"""
heights = kwargs.pop('heights', None)
bottom = kwargs.pop('bottom', None)
depth = kwargs.pop('depth', None)
ret = [] # Returned variable means in layer
layer_arg = get_layer(pressure, *args, heights=heights,
bottom=bottom, depth=depth)
Expand Down
3 changes: 1 addition & 2 deletions src/metpy/calc/kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ def _check_and_flip(arr):
def ensure_yx_order(func):
"""Wrap a function to ensure all array arguments are y, x ordered, based on kwarg."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
def wrapper(*args, dim_order=None, **kwargs):
# Check what order we're given
dim_order = kwargs.pop('dim_order', None)
x_first = _is_x_first_dim(dim_order)

# If x is the first dimension, flip (transpose) every array within the function args.
Expand Down
57 changes: 19 additions & 38 deletions src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,9 @@ def most_unstable_parcel(pressure, temperature, dewpoint, heights=None,
@exporter.export
@preprocess_xarray
@check_units('[temperature]', '[pressure]', '[temperature]')
def isentropic_interpolation(theta_levels, pressure, temperature, *args, **kwargs):
def isentropic_interpolation(theta_levels, pressure, temperature, *args, axis=0,
temperature_out=False, max_iters=50, eps=1e-6,
bottom_up_search=True, **kwargs):
r"""Interpolate data in isobaric coordinates to isentropic coordinates.
Parameters
Expand All @@ -1597,17 +1599,6 @@ def isentropic_interpolation(theta_levels, pressure, temperature, *args, **kwarg
One-dimensional array of pressure levels
temperature : array
Array of temperature
args : array, optional
Any additional variables will be interpolated to each isentropic level.
Returns
-------
list
List with pressure at each isentropic level, followed by each additional
argument interpolated to isentropic coordinates.
Other Parameters
----------------
axis : int, optional
The axis corresponding to the vertical in the temperature array, defaults to 0.
temperature_out : bool, optional
Expand All @@ -1620,6 +1611,14 @@ def isentropic_interpolation(theta_levels, pressure, temperature, *args, **kwarg
bottom_up_search : bool, optional
Controls whether to search for theta levels bottom-up, or top-down. Defaults to
True, which is bottom-up search.
args : array, optional
Any additional variables will be interpolated to each isentropic level.
Returns
-------
list
List with pressure at each isentropic level, followed by each additional
argument interpolated to isentropic coordinates.
Notes
-----
Expand Down Expand Up @@ -1648,17 +1647,11 @@ def _isen_iter(iter_log_p, isentlevs_nd, ka, a, b, pok):
fp = exner * (ka * t - a)
return iter_log_p - (f / fp)

# Change when Python 2.7 no longer supported
# Pull out keyword arguments
temperature_out = kwargs.get('tmpk_out')
if temperature_out is not None:
# Remove block when tmpk_out is removed in 1.0
if 'tmpk_out' in kwargs:
temperature_out = kwargs.get('tmpk_out')
warnings.warn('The use of "tmpk_out" has been deprecated in favor of'
'"temperature_out",', metpyDeprecation)
temperature_out = kwargs.pop('temperature_out', temperature_out)
max_iters = kwargs.pop('max_iters', 50)
eps = kwargs.pop('eps', 1e-6)
axis = kwargs.pop('axis', 0)
bottom_up_search = kwargs.pop('bottom_up_search', True)

# Get dimensions in temperature
ndim = temperature.ndim
Expand Down Expand Up @@ -1895,7 +1888,7 @@ def mixed_parcel(p, temperature, dewpt, parcel_start_pressure=None,
@exporter.export
@preprocess_xarray
@check_units('[pressure]')
def mixed_layer(p, *args, **kwargs):
def mixed_layer(p, *args, heights=None, bottom=None, depth=100 * units.hPa, interpolate=True):
r"""Mix variable(s) over a layer, yielding a mass-weighted average.
This function will integrate a data variable with respect to pressure and determine the
Expand All @@ -1916,20 +1909,14 @@ def mixed_layer(p, *args, **kwargs):
The thickness of the layer as a pressure or height above the bottom of the layer
(default 100 hPa)
interpolate : bool, optional
Interpolate the top and bottom points if they are not in the given data
Interpolate the top and bottom points if they are not in the given data (default True)
Returns
-------
`pint.Quantity`
The mixed value of the data variable.
"""
# Pull out keyword arguments, remove when we drop Python 2.7
heights = kwargs.pop('heights', None)
bottom = kwargs.pop('bottom', None)
depth = kwargs.pop('depth', 100 * units.hPa)
interpolate = kwargs.pop('interpolate', True)

layer = get_layer(p, *args, heights=heights, bottom=bottom,
depth=depth, interpolate=interpolate)
p_layer = layer[0]
Expand Down Expand Up @@ -2014,7 +2001,8 @@ def moist_static_energy(heights, temperature, specific_humidity):
@exporter.export
@preprocess_xarray
@check_units('[pressure]', '[temperature]')
def thickness_hydrostatic(pressure, temperature, **kwargs):
def thickness_hydrostatic(pressure, temperature, mixing=None,
molecular_weight_ratio=mpconsts.epsilon, bottom=None, depth=None):
r"""Calculate the thickness of a layer via the hypsometric equation.
This thickness calculation uses the pressure and temperature profiles (and optionally
Expand Down Expand Up @@ -2057,11 +2045,6 @@ def thickness_hydrostatic(pressure, temperature, **kwargs):
thickness_hydrostatic_from_relative_humidity, pressure_to_height_std, virtual_temperature
"""
mixing = kwargs.pop('mixing', None)
molecular_weight_ratio = kwargs.pop('molecular_weight_ratio', mpconsts.epsilon)
bottom = kwargs.pop('bottom', None)
depth = kwargs.pop('depth', None)

# Get the data for the layer, conditional upon bottom/depth being specified and mixing
# ratio being given
if bottom is None and depth is None:
Expand All @@ -2088,7 +2071,7 @@ def thickness_hydrostatic(pressure, temperature, **kwargs):
@preprocess_xarray
@check_units('[pressure]', '[temperature]')
def thickness_hydrostatic_from_relative_humidity(pressure, temperature, relative_humidity,
**kwargs):
bottom=None, depth=None):
r"""Calculate the thickness of a layer given pressure, temperature and relative humidity.
Similar to ``thickness_hydrostatic``, this thickness calculation uses the pressure,
Expand Down Expand Up @@ -2131,8 +2114,6 @@ def thickness_hydrostatic_from_relative_humidity(pressure, temperature, relative
mixing_ratio_from_relative_humidity
"""
bottom = kwargs.pop('bottom', None)
depth = kwargs.pop('depth', None)
mixing = mixing_ratio_from_relative_humidity(relative_humidity, temperature, pressure)

return thickness_hydrostatic(pressure, temperature, mixing=mixing, bottom=bottom,
Expand Down
15 changes: 3 additions & 12 deletions src/metpy/calc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def _get_bound_pressure_height(pressure, bound, heights=None, interpolate=True):
@exporter.export
@preprocess_xarray
@check_units('[length]')
def get_layer_heights(heights, depth, *args, **kwargs):
def get_layer_heights(heights, depth, *args, bottom=None, interpolate=True, with_agl=False):
"""Return an atmospheric layer from upper air data with the requested bottom and depth.
This function will subset an upper air dataset to contain only the specified layer using
Expand Down Expand Up @@ -447,10 +447,6 @@ def get_layer_heights(heights, depth, *args, **kwargs):
The height and data variables of the layer
"""
bottom = kwargs.pop('bottom', None)
interpolate = kwargs.pop('interpolate', True)
with_agl = kwargs.pop('with_agl', False)

# Make sure pressure and datavars are the same length
for datavar in args:
if len(heights) != len(datavar):
Expand Down Expand Up @@ -510,7 +506,8 @@ def get_layer_heights(heights, depth, *args, **kwargs):
@exporter.export
@preprocess_xarray
@check_units('[pressure]')
def get_layer(pressure, *args, **kwargs):
def get_layer(pressure, *args, heights=None, bottom=None, depth=100 * units.hPa,
interpolate=True):
r"""Return an atmospheric layer from upper air data with the requested bottom and depth.
This function will subset an upper air dataset to contain only the specified layer. The
Expand Down Expand Up @@ -544,12 +541,6 @@ def get_layer(pressure, *args, **kwargs):
The pressure and data variables of the layer
"""
# Pop off keyword arguments
heights = kwargs.pop('heights', None)
bottom = kwargs.pop('bottom', None)
depth = kwargs.pop('depth', 100 * units.hPa)
interpolate = kwargs.pop('interpolate', True)

# If we get the depth kwarg, but it's None, set it to the default as well
if depth is None:
depth = 100 * units.hPa
Expand Down
9 changes: 1 addition & 8 deletions src/metpy/interpolate/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def interpolate_to_grid(x, y, z, interp_type='linear', hres=50000,

@exporter.export
@preprocess_pandas
def interpolate_to_isosurface(level_var, interp_var, level, **kwargs):
def interpolate_to_isosurface(level_var, interp_var, level, bottom_up_search=True):
r"""Linear interpolation of a variable to a given vertical level from given values.
This function assumes that highest vertical level (lowest pressure) is zeroth index.
Expand All @@ -326,9 +326,6 @@ def interpolate_to_isosurface(level_var, interp_var, level, **kwargs):
given level (e.g., potential temperature on isobaric levels)
level: int or float
Desired interpolated level (e.g., 2 PVU surface)
Other Parameters
----------------
bottom_up_search : bool, optional
Controls whether to search for levels bottom-up, or top-down. Defaults to
True, which is bottom-up search.
Expand All @@ -346,10 +343,6 @@ def interpolate_to_isosurface(level_var, interp_var, level, **kwargs):
tropopause (e.g., 2 PVU surface)
"""
# Change when Python 2.7 no longer supported
# Pull out keyword arguments
bottom_up_search = kwargs.pop('bottom_up_search', True)

# Find index values above and below desired interpolated surface value
above, below, good = metpy.calc.find_bounding_indices(level_var, [level], axis=0,
from_below=bottom_up_search)
Expand Down
15 changes: 3 additions & 12 deletions src/metpy/interpolate/one_dimension.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018 MetPy Developers.
# Copyright (c) 2018,2019 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Interpolate data along a single axis."""
Expand Down Expand Up @@ -50,7 +50,7 @@ def interpolate_nans_1d(x, y, kind='linear'):

@exporter.export
@preprocess_xarray
def interpolate_1d(x, xp, *args, **kwargs):
def interpolate_1d(x, xp, *args, axis=0, fill_value=np.nan, return_list_always=False):
r"""Interpolates data with any shape over a specified axis.
Interpolation over a specified axis for arrays of any shape.
Expand Down Expand Up @@ -96,11 +96,6 @@ def interpolate_1d(x, xp, *args, **kwargs):
xp and args must be the same shape.
"""
# Pull out keyword args
fill_value = kwargs.pop('fill_value', np.nan)
axis = kwargs.pop('axis', 0)
return_list_always = kwargs.pop('return_list_always', False)

# Handle units
x, xp = _strip_matching_units(x, xp)

Expand Down Expand Up @@ -180,7 +175,7 @@ def interpolate_1d(x, xp, *args, **kwargs):

@exporter.export
@preprocess_xarray
def log_interpolate_1d(x, xp, *args, **kwargs):
def log_interpolate_1d(x, xp, *args, axis=0, fill_value=np.nan):
r"""Interpolates data with logarithmic x-scale over a specified axis.
Interpolation on a logarithmic x-scale for interpolation values in pressure coordintates.
Expand Down Expand Up @@ -222,10 +217,6 @@ def log_interpolate_1d(x, xp, *args, **kwargs):
xp and args must be the same shape.
"""
# Pull out kwargs
fill_value = kwargs.pop('fill_value', np.nan)
axis = kwargs.pop('axis', 0)

# Handle units
x, xp = _strip_matching_units(x, xp)

Expand Down
5 changes: 2 additions & 3 deletions src/metpy/plots/skewt.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,9 +909,8 @@ def plot_colormapped(self, u, v, c, intervals=None, colors=None, **kwargs):
This has been deprecated in 0.11 in favor of `intervals`.
"""
bounds = kwargs.pop('bounds', None)
if bounds is not None:
intervals = bounds
if 'bounds' in kwargs:
intervals = kwargs.pop('bounds')
warnings.warn('The use of "bounds" as a parameter has been deprecated in '
'favor of "intervals",', metpyDeprecation)

Expand Down
2 changes: 1 addition & 1 deletion tests/calc/test_calc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def test_get_layer_heights_agl_bottom_no_interp():
heights_init = np.arange(300, 1200, 100) * units.m
data = heights_init.m * 0.1 * units.degC
heights, data = get_layer_heights(heights_init, 500 * units.m, data, with_agl=True,
interpolation=False, bottom=200 * units.m)
interpolate=False, bottom=200 * units.m)
# Regression test for #789
assert_array_equal(heights_init[0], 300 * units.m)
heights_true = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7]) * units.km
Expand Down
2 changes: 1 addition & 1 deletion tests/interpolate/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def test_interpolate_to_isosurface_from_below():
[340., 360., 380., 400., 420.],
[350., 370., 390., 410., 430.]]])

dt_thta = interpolate_to_isosurface(pv, thta, 2, from_below=False)
dt_thta = interpolate_to_isosurface(pv, thta, 2, bottom_up_search=False)

truth = np.array([[330., 350., 370., 385., 400.],
[340., 359., 374., 389., 404.],
Expand Down

0 comments on commit f7cb562

Please sign in to comment.