From 53d44b411807593274eacb6b9aec9cb25dd50ec3 Mon Sep 17 00:00:00 2001 From: Clarmy Lee Date: Thu, 9 Oct 2025 20:32:20 +0800 Subject: [PATCH 1/2] fix: update weather description handling for showers and add precipitation type support --- pymetaf/parser.py | 30 ++++++++++++++++++++++++----- tests/case/metar/parse_text_case.py | 2 +- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/pymetaf/parser.py b/pymetaf/parser.py index 012f595..c222676 100644 --- a/pymetaf/parser.py +++ b/pymetaf/parser.py @@ -154,6 +154,9 @@ def get_weather_description(code): intensity = "Light " code = code[1:] + # Precipitation types that can follow descriptors like SH + precipitation_types = ["DZ", "RA", "SN", "SG", "IC", "PL", "GR", "GS"] + weather_codes = { "DZ": "Drizzle", "RA": "Rain", @@ -177,7 +180,7 @@ def get_weather_description(code): "FC": "Funnel Cloud", "SS": "Sandstorm", "DS": "Duststorm", - "SH": "Showers of", + "SH": "Showers", "TS": "Thunderstorm", "BL": "Blowing", "MI": "Shallow", @@ -190,12 +193,29 @@ def get_weather_description(code): } description = "" - while len(code) > 0: + remaining_code = code + while len(remaining_code) > 0: + matched = False for key in weather_codes.keys(): - if code.startswith(key): - description += weather_codes[key] + " " - code = code[len(key) :] + if remaining_code.startswith(key): + # Special handling for SH: add "of" only if followed by precipitation + if key == "SH": + # Check if next part is a precipitation type + rest_of_code = remaining_code[len(key):] + has_precipitation = any(rest_of_code.startswith(pt) for pt in precipitation_types) + if has_precipitation: + description += "Showers of " + else: + description += "Showers " + else: + description += weather_codes[key] + " " + remaining_code = remaining_code[len(key):] + matched = True break + + # If no match found, skip this character to avoid infinite loop + if not matched: + remaining_code = remaining_code[1:] return intensity + description.strip() diff --git a/tests/case/metar/parse_text_case.py b/tests/case/metar/parse_text_case.py index 7dd44f4..49af16f 100644 --- a/tests/case/metar/parse_text_case.py +++ b/tests/case/metar/parse_text_case.py @@ -629,7 +629,7 @@ "cloud_type": None, }, ], - "weather": ["In the Vicinity Showers of"], + "weather": ["In the Vicinity Showers"], "auto": False, }, }, From be02f102570e220869d5f32691567bb848045648 Mon Sep 17 00:00:00 2001 From: Clarmy Lee Date: Thu, 9 Oct 2025 20:33:13 +0800 Subject: [PATCH 2/2] chore: bump version to 1.0.5 --- pymetaf/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymetaf/__init__.py b/pymetaf/__init__.py index 536f428..eaac86f 100644 --- a/pymetaf/__init__.py +++ b/pymetaf/__init__.py @@ -1,3 +1,3 @@ -__version__ = "1.0.4" +__version__ = "1.0.5" from .parser import *