Skip to content

Commit

Permalink
MNT: Actually enable flake8-simplify in ruff
Browse files Browse the repository at this point in the history
'S' rule wasn't actually doing it. Fix up the issues identified.
  • Loading branch information
dopplershift committed Jan 12, 2024
1 parent cc2cb57 commit 70e3b69
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 85 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ filterwarnings = [
[tool.ruff]
line-length = 95
exclude = ["docs", "build", "src/metpy/io/_metar_parser/metar_parser.py"]
select = ["A", "B", "C", "CPY001", "D", "E", "E226", "F", "G", "I", "N", "NPY", "Q", "R", "S", "T", "U", "W"]
select = ["A", "B", "C", "CPY001", "D", "E", "E226", "F", "G", "I", "N", "NPY", "Q", "R", "S", "SIM", "T", "U", "W"]
ignore = ["F405", "I001", "RET504", "RET505", "RET506", "RET507", "RUF100"]
preview = true
explicit-preview-rules = true
Expand Down
5 changes: 1 addition & 4 deletions src/metpy/calc/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,10 +906,7 @@ def _trailing_dims(indexer):
raise ValueError('The shape of the smoothing window must be odd in all dimensions.')

# Optionally normalize the supplied weighting window
if normalize_weights:
weights = window / np.sum(window)
else:
weights = window
weights = window / np.sum(window) if normalize_weights else window

# Set indexes
# Inner index for the centered array elements that are affected by the smoothing
Expand Down
5 changes: 1 addition & 4 deletions src/metpy/calc/kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,7 @@ def geostrophic_wind(height, dx=None, dy=None, latitude=None, x_dim=-1, y_dim=-2
"""
f = coriolis_parameter(latitude)
if height.dimensionality['[length]'] == 2.0:
norm_factor = 1. / f
else:
norm_factor = mpconsts.g / f
norm_factor = 1. / f if height.dimensionality['[length]'] == 2.0 else mpconsts.g / f

dhdx, dhdy = geospatial_gradient(height, dx=dx, dy=dy, x_dim=x_dim, y_dim=y_dim,
parallel_scale=parallel_scale,
Expand Down
5 changes: 1 addition & 4 deletions src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2441,10 +2441,7 @@ def cape_cin(pressure, temperature, dewpoint, parcel_profile, which_lfc='bottom'
parcel_temperature_profile=parcel_profile, which=which_el)

# No EL and we use the top reading of the sounding.
if np.isnan(el_pressure):
el_pressure = pressure[-1].magnitude
else:
el_pressure = el_pressure.magnitude
el_pressure = pressure[-1].magnitude if np.isnan(el_pressure) else el_pressure.magnitude

# Difference between the parcel path and measured temperature profiles
y = (parcel_profile - temperature).to(units.degK)
Expand Down
7 changes: 1 addition & 6 deletions src/metpy/calc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,7 @@ def reduce_point_density(points, radius, priority=None):

# Need to use sorted indices rather than sorting the position
# so that the keep mask matches *original* order.
if priority is not None:
# Need to sort the locations in decreasing priority.
sorted_indices = np.argsort(priority)[::-1]
else:
# Take advantage of iterator nature of range here to avoid making big lists
sorted_indices = range(len(points))
sorted_indices = range(len(points)) if priority is None else np.argsort(priority)[::-1]

# Keep all good points initially
keep = np.logical_and.reduce(good_vals, axis=-1)
Expand Down
59 changes: 19 additions & 40 deletions src/metpy/io/gempak.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,43 +489,25 @@ def _wx_to_wnum(wx1, wx2, wx3, missing=-9999):
Notes
-----
See GEMAPK function PT_WNMT.
"""
metar_codes = [
'BR', 'DS', 'DU', 'DZ', 'FC', 'FG', 'FU', 'GR', 'GS',
'HZ', 'IC', 'PL', 'PO', 'RA', 'SA', 'SG', 'SN', 'SQ',
'SS', 'TS', 'UP', 'VA', '+DS', '-DZ', '+DZ', '+FC',
'-GS', '+GS', '-PL', '+PL', '-RA', '+RA', '-SG',
'+SG', '-SN', '+SN', '+SS', 'BCFG', 'BLDU', 'BLPY',
'BLSA', 'BLSN', 'DRDU', 'DRSA', 'DRSN', 'FZDZ', 'FZFG',
'FZRA', 'MIFG', 'PRFG', 'SHGR', 'SHGS', 'SHPL', 'SHRA',
'SHSN', 'TSRA', '+BLDU', '+BLSA', '+BLSN', '-FZDZ',
'+FZDZ', '+FZFG', '-FZRA', '+FZRA', '-SHGS', '+SHGS',
'-SHPL', '+SHPL', '-SHRA', '+SHRA', '-SHSN', '+SHSN',
'-TSRA', '+TSRA'
]

gempak_wnum = [
9, 33, 8, 2, -2, 9, 7, 4, 25, 6, 36, 23, 40, 1, 35, 24, 3, 10,
35, 5, 41, 11, 68, 17, 18, -1, 61, 62, 57, 58, 13, 14, 59, 60, 20,
21, 69, 9, 33, 34, 35, 32, 33, 35, 32, 19, 30, 15, 31, 9, 27, 67,
63, 16, 22, 66, 68, 69, 70, 53, 54, 30, 49, 50, 67, 67, 75, 76, 51,
52, 55, 56, 77, 78
]

if wx1 in metar_codes:
wn1 = gempak_wnum[metar_codes.index(wx1)]
else:
wn1 = 0

if wx2 in metar_codes:
wn2 = gempak_wnum[metar_codes.index(wx2)]
else:
wn2 = 0
if wx3 in metar_codes:
wn3 = gempak_wnum[metar_codes.index(wx3)]
else:
wn3 = 0
"""
metar_to_gempak_wnum = {'BR': 9, 'DS': 33, 'DU': 8, 'DZ': 2, 'FC': -2, 'FG': 9, 'FU': 7,
'GR': 4, 'GS': 25, 'HZ': 6, 'IC': 36, 'PL': 23, 'PO': 40, 'RA': 1,
'SA': 35, 'SG': 24, 'SN': 3, 'SQ': 10, 'SS': 35, 'TS': 5, 'UP': 41,
'VA': 11, '+DS': 68, '-DZ': 17, '+DZ': 18, '+FC': -1, '-GS': 61,
'+GS': 62, '-PL': 57, '+PL': 58, '-RA': 13, '+RA': 14, '-SG': 59,
'+SG': 60, '-SN': 20, '+SN': 21, '+SS': 69, 'BCFG': 9, 'BLDU': 33,
'BLPY': 34, 'BLSA': 35, 'BLSN': 32, 'DRDU': 33, 'DRSA': 35,
'DRSN': 32, 'FZDZ': 19, 'FZFG': 30, 'FZRA': 15, 'MIFG': 31,
'PRFG': 9, 'SHGR': 27, 'SHGS': 67, 'SHPL': 63, 'SHRA': 16,
'SHSN': 22, 'TSRA': 66, '+BLDU': 68, '+BLSA': 69, '+BLSN': 70,
'-FZDZ': 53, '+FZDZ': 54, '+FZFG': 30, '-FZRA': 49, '+FZRA': 50,
'-SHGS': 67, '+SHGS': 67, '-SHPL': 75, '+SHPL': 76, '-SHRA': 51,
'+SHRA': 52, '-SHSN': 55, '+SHSN': 56, '-TSRA': 77, '+TSRA': 78}

wn1 = metar_to_gempak_wnum.get(wx1, 0)
wn2 = metar_to_gempak_wnum.get(wx2, 0)
wn3 = metar_to_gempak_wnum.get(wx3, 0)

if all(w >= 0 for w in [wn1, wn2, wn3]):
wnum = wn3 * 80 * 80 + wn2 * 80 + wn1
Expand Down Expand Up @@ -2216,10 +2198,7 @@ def snxarray(self, station_id=None, station_number=None,

sndno = [(s.DTNO, s.SNDNO) for s in matched]

if self.merged:
data = self._unpack_merged(sndno)
else:
data = self._unpack_unmerged(sndno)
data = self._unpack_merged(sndno) if self.merged else self._unpack_unmerged(sndno)

soundings = []
for snd in data:
Expand Down
11 changes: 2 additions & 9 deletions src/metpy/io/nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,15 +717,8 @@ def float16(val):
exp = (val >> 10) & 0x1F
sign = val >> 15

if exp:
value = 2 ** (exp - 16) * (1 + float(frac) / 2**10)
else:
value = float(frac) / 2**9

if sign:
value *= -1

return value
value = 2 ** (exp - 16) * (1 + float(frac) / 2**10) if exp else float(frac) / 2**9
return -value if sign else value


def float32(short1, short2):
Expand Down
17 changes: 3 additions & 14 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,13 +1823,8 @@ def _valid_color_list(_, proposal):
"""
color = proposal['value']

if isinstance(color, str):
color = [color]
# `color` must be a collection if it is not a string
else:
color = list(color)

return color
return [color] if isinstance(color, str) else list(color)

@staticmethod
@validate('labels')
Expand Down Expand Up @@ -1877,10 +1872,7 @@ def _position_label(geo_obj, label):
geo_obj = geo_obj.geoms[label_hash % len(geo_obj.geoms)]

# Get the list of coordinates of the polygon/line/point
if isinstance(geo_obj, Polygon):
coords = geo_obj.exterior.coords
else:
coords = geo_obj.coords
coords = geo_obj.exterior.coords if isinstance(geo_obj, Polygon) else geo_obj.coords

return coords[label_hash % len(coords)]

Expand Down Expand Up @@ -1990,10 +1982,7 @@ def _build(self):

# If polygon, put label directly on edge of polygon. If line or point, put
# label slightly below line/point.
if isinstance(geo_obj, (MultiPolygon, Polygon)):
offset = (0, 0)
else:
offset = (0, -12)
offset = (0, 0) if isinstance(geo_obj, (MultiPolygon, Polygon)) else (0, -12)

# Finally, draw the label
self._draw_label(label, lon, lat, fontcolor, fontoutline, offset)
6 changes: 3 additions & 3 deletions tests/calc/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,9 +1936,9 @@ def test_dewpoint_specific_humidity_old_signature():
p = 1013.25 * units.mbar
temperature = 20. * units.degC
q = 0.012 * units.dimensionless
with pytest.deprecated_call(match='Temperature argument'):
with pytest.raises(ValueError, match='changed in version'):
dewpoint_from_specific_humidity(q, temperature, p)
with (pytest.deprecated_call(match='Temperature argument'),
pytest.raises(ValueError, match='changed in version')):
dewpoint_from_specific_humidity(q, temperature, p)


def test_dewpoint_specific_humidity_kwargs():
Expand Down

0 comments on commit 70e3b69

Please sign in to comment.