Skip to content
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
23 changes: 20 additions & 3 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,13 @@ class Plots2D(HasTraits):
from the Python standard library.
"""

plot_units = Unicode(allow_none=True, default_value=None)
plot_units.__doc__ = """The desired units to plot the field in.

Setting this attribute will convert the units of the field variable to the given units for
plotting using the MetPy Units module.
"""

@property
def _cmap_obj(self):
"""Return the colormap object.
Expand Down Expand Up @@ -918,7 +925,11 @@ def griddata(self):

if self.time is not None:
subset[data.metpy.time.name] = self.time
self._griddata = data.metpy.sel(**subset).squeeze()
data_subset = data.metpy.sel(**subset).squeeze()

if self.plot_units is not None:
data_subset.metpy.convert_units(self.plot_units)
self._griddata = data_subset

return self._griddata

Expand Down Expand Up @@ -1191,8 +1202,14 @@ def griddata(self):

if self.time is not None:
subset[u.metpy.time.name] = self.time
self._griddata_u = u.metpy.sel(**subset).squeeze()
self._griddata_v = v.metpy.sel(**subset).squeeze()
data_subset_u = u.metpy.sel(**subset).squeeze()
data_subset_v = v.metpy.sel(**subset).squeeze()

if self.plot_units is not None:
data_subset_u.metpy.convert_units(self.plot_units)
data_subset_v.metpy.convert_units(self.plot_units)
self._griddata_u = data_subset_u
self._griddata_v = data_subset_v

return (self._griddata_u, self._griddata_v)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 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_contour_options():
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.035)
def test_declarative_contour_convert_units():
"""Test making a contour plot."""
data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))

contour = ContourPlot()
contour.data = data
contour.field = 'Temperature'
contour.level = 700 * units.hPa
contour.contours = 30
contour.linewidth = 1
contour.linecolor = 'red'
contour.linestyle = 'dashed'
contour.clabels = True
contour.plot_units = 'degC'

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

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

return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.016)
def test_declarative_events():
"""Test that resetting traitlets properly propagates."""
Expand Down Expand Up @@ -417,6 +447,33 @@ def test_declarative_barb_gfs():
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.346)
def test_declarative_barb_gfs_knots():
"""Test making a contour plot."""
data = xr.open_dataset(get_test_data('GFS_test.nc', as_file_obj=False))

barb = BarbPlot()
barb.data = data
barb.level = 300 * units.hPa
barb.field = ['u-component_of_wind_isobaric', 'v-component_of_wind_isobaric']
barb.skip = (3, 3)
barb.earth_relative = False
barb.plot_units = 'knot'

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

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

return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.022)
def test_declarative_sfc_obs():
"""Test making a surface observation plot."""
Expand Down