Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

METAR/Station Plot fixes #3297

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/metpy/io/metar.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def _metars_to_dataframe(metar_iter, *, year=None, month=None):
# Set the units for the dataframe--filter out warning from Pandas
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
df.units = col_units
df.units = col_units.copy()

return df

Expand Down
12 changes: 10 additions & 2 deletions src/metpy/plots/station_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def plot_symbols(mapper, name, nwrap=10, figsize=(10, 1.4)):
kwargs['fontproperties'] = wx_symbol_font.copy()
return self.plot_parameter(location, codes, symbol_mapper, **kwargs)

def plot_parameter(self, location, parameter, formatter='.0f', **kwargs):
def plot_parameter(self, location, parameter, formatter=None, **kwargs):
"""At the specified location in the station model plot a set of values.

This specifies that at the offset `location`, the data in `parameter` should be
Expand All @@ -186,7 +186,7 @@ def plot_parameter(self, location, parameter, formatter='.0f', **kwargs):
formatter : str or Callable, optional
How to format the data as a string for plotting. If a string, it should be
compatible with the :func:`format` builtin. If a callable, this should take a
value and return a string. Defaults to '0.f'.
value and return a string. Defaults to 'z0.f'.
plot_units: `pint.unit`
Units to plot in (performing conversion if necessary). Defaults to given units.
kwargs
Expand Down Expand Up @@ -372,6 +372,14 @@ def _make_kwargs(self, kwargs):
@staticmethod
def _to_string_list(vals, fmt):
"""Convert a sequence of values to a list of strings."""
if fmt is None:
import sys
if sys.version_info >= (3, 11):
fmt = 'z.0f'
else:
def fmt(s):
"""Perform default formatting with no decimal places and no negative 0."""
return format(round(s, 0) + 0., '.0f')
if not callable(fmt):
def formatter(s):
"""Turn a format string into a callable."""
Expand Down
9 changes: 9 additions & 0 deletions tests/io/test_metar.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,12 @@ def test_repr():
"dewp=TreeNode(text='M10', offset=38)), "
"altim=TreeNode(text=' A3026', offset=41), "
"remarks=TreeNode(text='', offset=47), end=TreeNode(text='', offset=47))")


def test_metar_units_in_place():
"""Test that parsing a METAR yields units that can be changed safely in-place."""
df = parse_metar_to_dataframe('KDEN 012153Z 09010KT 10SM FEW060 BKN110 BKN220 27/13 A3010')
df.units['air_temperature'] = 'degF'

df = parse_metar_to_dataframe('KDEN 012153Z 09010KT 10SM FEW060 BKN110 BKN220 27/13 A3010')
assert df.units['air_temperature'] == 'degC'
Binary file modified tests/plots/baseline/test_declarative_sfc_obs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plots/baseline/test_declarative_sfc_obs_args.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plots/baseline/test_declarative_sfc_obs_changes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plots/baseline/test_declarative_sfc_obs_full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/plots/test_station_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def test_station_plot_locations():
return fig


def test_station_plot_negative_zero():
"""Test that we avoid formatting a negative 0 by default."""
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
sp = StationPlot(ax, [0], [0])
text = sp.plot_parameter('C', [-0.04])

assert text.text[0] == '0'

plt.close(fig)


@pytest.mark.mpl_image_compare(tolerance=0.00413, savefig_kwargs={'dpi': 300},
remove_text=True)
def test_stationlayout_api():
Expand Down