Skip to content

Commit

Permalink
Merge pull request #2156 from kgoebber/add_declarative_smooth
Browse files Browse the repository at this point in the history
add smoothing for declarative
  • Loading branch information
dcamron authored Nov 10, 2021
2 parents 806324e + edae5ad commit cdecf1c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ._mpl import TextCollection
from .cartopy_utils import import_cartopy
from .station_plot import StationPlot
from ..calc import reduce_point_density
from ..calc import reduce_point_density, smooth_n_point
from ..package_tools import Exporter
from ..units import units

Expand Down Expand Up @@ -1062,6 +1062,19 @@ class PlotScalar(Plots2D):
`list(ds)`
"""

smooth_field = Int(allow_none=True, default_value=None)
smooth_field.__doc__ = """Number of smoothing passes using 9-pt smoother.
By setting this parameter with an integer value it will call the MetPy 9-pt smoother and
provide a smoothed field for plotting. It is best to use this smoothing for data with
finer resolutions (e.g., smaller grid spacings with a lot of grid points).
See Also
--------
metpy.calc.smooth_n_point
"""

@observe('field')
def _update_data(self, _=None):
"""Handle updating the internal cache of data.
Expand Down Expand Up @@ -1099,6 +1112,10 @@ def griddata(self):
'for plotting'
)

# Handle smoothing of data
if self.smooth_field is not None:
data_subset = smooth_n_point(data_subset, 9, self.smooth_field)

# Handle unit conversion (both direct unit specification and scaling)
if self.plot_units is not None:
data_subset = data_subset.metpy.convert_units(self.plot_units)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions tests/plots/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ def test_declarative_figsize():
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True,
tolerance={'3.0': 0.216}.get(MPL_VERSION, 0.022))
@needs_cartopy
def test_declarative_smooth_field():
"""Test the smoothing of the field with smooth_field trait."""
data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))

contour = ContourPlot()
contour.data = data
contour.field = 'Geopotential_height'
contour.level = 700 * units.hPa
contour.contours = list(range(0, 4000, 30))
contour.linewidth = 1
contour.linecolor = 'black'
contour.smooth_field = 3

panel = MapPanel()
panel.area = 'us'
panel.projection = 'lcc'
panel.layers = ['coastline', 'borders', 'usstates']
panel.plots = [contour]

pc = PanelContainer()
pc.size = (10.5, 10.5)
pc.panels = [panel]
pc.draw()

return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True,
tolerance={'3.0': 0.328}.get(MPL_VERSION, 0.022))
@needs_cartopy
Expand Down

0 comments on commit cdecf1c

Please sign in to comment.