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

Add HRRR ptype reflectivity helpers. #961

Merged
merged 2 commits into from
Sep 23, 2024
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 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autoupdate_schedule: quarterly
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.6.5"
rev: "v0.6.7"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ All notable changes to this library are documented in this file.

- Add `allowed_as_list` option to `iemapp()` helper to stop lists.
- Add support for plotting by FEMA Regions.
- Introduce `radar_ptype` color ramp and `draw_radar_ptype_legend` for
generating plots of HRRR ptype.

### Bug Fixes

Expand Down
32 changes: 32 additions & 0 deletions src/pyiem/plot/colormaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,35 @@ def maue():
cmap3 = mpcolors.ListedColormap(cpool, "maue")
_register_cmap(cmap=cmap3)
return cmap3


def radar_ptype() -> dict[str, list]:
"""Generate a dictionary of colors for HRRR Ptype."""
return {
"rain": (
# 0-40 by 2.5, Green
"#eef8ea #e5f5e0 #d6efd0 #c7e9c0 #b4e1ad #a0d99b #8ace88 #73c476 "
"#5ab769 #40aa5d #319a50 #228a44 #117b38 #006c2c #005723 #00441b "
# 40-55 by 2.5, Wistia
"#ffe81a #ffd710 #ffc505 #ffb700 #ffab00 #ffa000"
).split(),
"snow": (
# 0-40 by 2.5, 16 colors from ocean_r
"#b4dae6 #99ccdd #81c0d5 #66b3cc #4ea6c4 #3399bb #1b8db3 #0080aa "
"#0073a2 #006699 #005a91 #004d88 #003f7f #003377 #00266e #001a66 "
# 40-55 by 2.5, 6 colors from PuRd_r
"#8d003b #b80b4e #d81b6a #e53592 #df66b0 #cd8bc2"
).split(),
"frzr": (
# 0-55 by 2.5, 22 colors Reds
"#ffeee6 #fee6da #fedecf #fdd0bc #fcc2aa #fcb499 #fca588 #fc9576 "
"#fc8767 #fb7858 #fb694a #f7593f #f24734 #ec382b #de2b25 #d11e1f "
"#c4161c #b61319 #a81016 #940b13 #7c0510 #67000d"
).split(),
"icep": (
# 0-55 by 2.5, 22 colors Purples
"#f8f6fa #f3f1f7 #eeecf4 #e6e5f1 #dedded #d5d5e9 #cacae3 #bebfdd "
"#b4b4d7 #a9a7cf #9e9ac8 #9390c3 #8885be #7e79b8 #7669af #6e58a7 "
"#66499f #5e3a98 #552a90 #4e1c8a #460d83 #3f007d"
).split(),
}
43 changes: 42 additions & 1 deletion src/pyiem/plot/geoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from scipy.signal import convolve2d
from shapely.geometry import shape

from pyiem.plot.colormaps import stretch_cmap
from pyiem.plot.colormaps import radar_ptype, stretch_cmap

# local
from pyiem.plot.use_agg import plt
Expand Down Expand Up @@ -409,6 +409,47 @@ def draw_usdm(self, valid=None, filled=True, hatched=False, **kwargs):
return datetime.datetime.strptime(usdm_valid, "%Y-%m-%d")
return None

def draw_radar_ptype_legend(self):
"""Draw a legend for radar precipitation type."""
ramps = radar_ptype()
pos = self.cax.get_position()
w75 = pos.width * 0.75
x0 = pos.x0 + 0.015
axes = [
self.fig.add_axes((x0, pos.y0, w75, pos.height / 5)),
self.fig.add_axes(
(x0, pos.y0 + pos.height / 4, w75, pos.height / 5)
),
self.fig.add_axes(
(x0, pos.y0 + pos.height / 2, w75, pos.height / 5)
),
self.fig.add_axes(
(x0, pos.y0 + pos.height * 0.75, w75, pos.height / 5)
),
]
levels = np.arange(0, 55.1, 2.5)
norm = mpcolors.BoundaryNorm(levels, 22)
for i, (name, colors) in enumerate(ramps.items()):
cmap = mpcolors.ListedColormap(colors, name=name)
cb = plt.colorbar(
mpcm.ScalarMappable(norm=norm, cmap=cmap),
cax=axes[i],
boundaries=levels,
ticks=range(0, 51, 10),
spacing="uniform",
orientation="vertical",
)
# Remove minor ticks
cb.ax.yaxis.set_tick_params(which="minor", width=0)
cb.ax.text(
-0.03,
0.5,
name if name != "icep" else "sleet",
va="center",
ha="right",
rotation=90,
)

def draw_colorbar(self, clevs, cmap, norm, **kwargs):
"""Draw the colorbar on the structed plot using `self.cax`.

Expand Down
Binary file added tests/plot/baseline/test_radar_ptype_legend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/plot/test_geoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def test_invalid_file():
assert load_bounds("this shall not work") is None


@pytest.mark.mpl_image_compare(tolerance=PAIN)
def test_radar_ptype_legend():
"""Test the plotting of the ptype reflectivity legend."""
mp = MapPlot(nocaption=True, sector="iowa")
mp.draw_radar_ptype_legend()
return mp.fig


@pytest.mark.mpl_image_compare(tolerance=PAIN)
def test_nws_with_axes_position_set():
"""Test that the inset axes work out."""
Expand Down
Loading