diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f99ea94c..5ec6841d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ ci: autoupdate_schedule: quarterly repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.8" + rev: "v0.6.9" hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/CHANGELOG.md b/CHANGELOG.md index 492f2c85..c850e576 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to this library are documented in this file. - Discontinue raw SPS product text within the database. - Ensure raw text products into `WMOProduct` end with a line feed. +- Stop postprocessing the `MapPlot` figure into 8bit colorspace. ### New Features @@ -21,6 +22,7 @@ generating plots of HRRR ptype. - Accomodate ancient LSRs using `TRACE` as the magnitude field. - Ensure geometries going into masking helper are CCW, to mask outside of. +- Properly check USDM service response prior to parsing it. ## **1.21.0** (6 Sep 2024) diff --git a/src/pyiem/plot/geoplot.py b/src/pyiem/plot/geoplot.py index faacae95..4c5e734a 100644 --- a/src/pyiem/plot/geoplot.py +++ b/src/pyiem/plot/geoplot.py @@ -296,8 +296,9 @@ def draw_usdm(self, valid=None, filled=True, hatched=False, **kwargs): valid = valid.strftime("%Y-%m-%d") url = f"http://mesonet.agron.iastate.edu/geojson/usdm.py?date={valid}" try: - req = requests.get(url, timeout=30) - df = gpd.GeoDataFrame().from_features(req.json()) + resp = requests.get(url, timeout=30) + resp.raise_for_status() + df = gpd.GeoDataFrame().from_features(resp.json()) except Exception as exp: warnings.warn( f"draw_usdm IEM USDM Webservice failed: {exp}", stacklevel=1 @@ -1357,31 +1358,19 @@ def postprocess(self, **kwargs): """ with BytesIO() as ram: self.fig.savefig(ram, format="png") - ram.seek(0) - im = Image.open(ram) - im2 = im.convert("RGB").convert( - "P", palette=Image.Palette.ADAPTIVE + imgdata = ram.getvalue() + if kwargs.get("memcache") and kwargs.get("memcachekey"): + kwargs["memcache"].set( + kwargs["memcachekey"], + imgdata, + time=kwargs.get("memcacheexpire", 300), ) - im.close() - if kwargs.get("memcache") and kwargs.get("memcachekey"): - ram = BytesIO() - im2.save(ram, format="png") - ram.seek(0) - r = ram.read() - kwargs["memcache"].set( - kwargs["memcachekey"], - r, - time=kwargs.get("memcacheexpire", 300), - ) if kwargs.get("web", False): ssw("Content-Type: image/png\n\n") - im2.save(getattr(sys.stdout, "buffer", sys.stdout), format="png") - im2.close() + sys.stdout.buffer.write(imgdata) return - tmpfd = tempfile.NamedTemporaryFile(delete=False) - im2.save(tmpfd, format="PNG") - im2.close() - tmpfd.close() + with tempfile.NamedTemporaryFile(delete=False) as tmpfd: + tmpfd.write(imgdata) if kwargs.get("pqstr") is not None: subprocess.call(["pqinsert", "-p", kwargs["pqstr"], tmpfd.name]) if kwargs.get("filename") is not None: diff --git a/tests/plot/test_geoplot.py b/tests/plot/test_geoplot.py index 79919175..e2a1fd2b 100644 --- a/tests/plot/test_geoplot.py +++ b/tests/plot/test_geoplot.py @@ -34,7 +34,8 @@ def test_exercise_usdm(): """Test the various checks in the usdm method.""" mp = MapPlot() - mp.draw_usdm() + with pytest.warns(UserWarning) as _: + mp.draw_usdm() mp.draw_usdm(utc()) with pytest.warns(UserWarning) as _: assert mp.draw_usdm("qqq") is None