From fc835ea851d8f655a082e718741f415d39f2513a Mon Sep 17 00:00:00 2001 From: John Ronan Date: Thu, 17 Dec 2015 18:29:22 +0000 Subject: [PATCH] Initial attempt at parsing weather data --- aprslib/parsing.py | 145 +++++++++++++++- tests/test_parse_comment_weather.py | 254 ++++++++++++++++++++++++++++ 2 files changed, 393 insertions(+), 6 deletions(-) create mode 100644 tests/test_parse_comment_weather.py diff --git a/aprslib/parsing.py b/aprslib/parsing.py index b977129..c402c16 100644 --- a/aprslib/parsing.py +++ b/aprslib/parsing.py @@ -243,10 +243,16 @@ def parse(packet): logger.debug("Parsed as normal position report") else: raise ParseError("invalid format") - - # decode comment - body, result = _parse_comment(body) - parsed.update(result) + # check comment for weather information + # Page 62 of the spec + if parsed['symbol'] == '_': + logger.debug("Attempting to parse weather report from comment") + body, result = _parse_comment_weather(body) + parsed.update(result) + else: + # decode comment + body, result = _parse_comment(body) + parsed.update(result) if packet_type == ';': parsed.update({ @@ -396,9 +402,8 @@ def _parse_timestamp(body, packet_type=''): def _parse_comment(body): parsed = {} - # attempt to parse remaining part of the packet (comment field) - # try CRS/SPD/ + # try CRS/SPD match = re.findall(r"^([0-9]{3})/([0-9]{3})", body) if match: cse, spd = match[0] @@ -926,3 +931,131 @@ def _parse_normal(body): }) return (body, parsed) + + +def _parse_comment_weather(body): + wind_multiplier = 0.44704 + rain_multiplier = 0.254 + + parsed = {} + match = re.findall(r"^([0-9]{3})/([0-9]{3})", body) + if match: + wind_direction, wind_speed = match[0] + parsed.update({ + 'wind_direction': int(wind_direction), + # mph to meters per second + 'wind_speed': float(int(wind_speed) * wind_multiplier) + }) + + match = re.findall(r"g([0-9]{3})", body) + if match: + wind_gust = match[0] + parsed.update({ + # mph to meters per second + 'wind_gust': float(int(wind_gust) * wind_multiplier) + }) + + # Positionless Weather report + match = re.findall(r"c([0-9]{3})s([0-9]{3})", body) + if match: + wind_direction, wind_speed = match[0] + parsed.update({ + 'wind_direction': int(wind_direction), + # mph to meters per second + 'wind_speed': float(int(wind_speed) * wind_multiplier) + }) + + match = re.findall(r"(?