refactored wx parsing

This commit is contained in:
Rossen Georgiev 2016-01-07 15:23:49 +00:00
parent fc835ea851
commit 67b37ca8a1
2 changed files with 89 additions and 177 deletions

View File

@ -248,7 +248,7 @@ def parse(packet):
if parsed['symbol'] == '_':
logger.debug("Attempting to parse weather report from comment")
body, result = _parse_comment_weather(body)
parsed.update(result)
parsed.update({'weather': result})
else:
# decode comment
body, result = _parse_comment(body)
@ -937,125 +937,49 @@ def _parse_comment_weather(body):
wind_multiplier = 0.44704
rain_multiplier = 0.254
key_map = {
'g': 'wind_gust',
'c': 'wind_direction',
't': 'temperature',
'S': 'wind_speed',
'r': 'rain_1h',
'p': 'rain_24h',
'P': 'rain_since_midnight',
'h': 'humidity',
'b': 'pressure',
'l': 'luminosity',
'L': 'luminosity',
's': 'snow',
'#': 'rain_raw',
}
val_map = {
'g': lambda x: int(x) * wind_multiplier,
'c': lambda x: int(x),
'S': lambda x: int(x) * wind_multiplier,
't': lambda x: (float(x) - 32) / 1.8,
'r': lambda x: int(x) * rain_multiplier,
'p': lambda x: int(x) * rain_multiplier,
'P': lambda x: int(x) * rain_multiplier,
'h': lambda x: int(x),
'b': lambda x: float(x) / 10,
'l': lambda x: int(x) + 1000,
'L': lambda x: int(x),
's': lambda x: float(x) * 25.4,
'#': lambda x: int(x),
}
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)
})
# parse weather data
body = re.sub(r"^([0-9]{3})/([0-9]{3})", "c\\1s\\2", body)
body = body.replace('s', 'S', 1)
# 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)
})
data = re.findall(r"([cSgtrpPlLs#]\d{3}|t-\d{2}|h\d{2}|b\d{5}|s\.\d{2}|s\d\.\d)", body)
data = map(lambda x: (key_map[x[0]] , val_map[x[0]](x[1:])), data)
match = re.findall(r"(?<!c[0-9. ]{3})s([.0-9]{3})", body)
if match:
snow = match[0]
parsed.update({
# inches to mm
'snow': float(snow) * 25.4
})
parsed.update(dict(data))
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)
})
match = re.findall(r"t([0-9]{3})", body)
if match:
degrees_farenheit = match[0]
parsed.update({
# Farenheight to Centigrade
'temp': float(int(degrees_farenheit) - 32) / 1.8
})
# Negative Farenheight
match = re.findall(r"t(-[0-9]{2})", body)
if match:
degrees_farenheit = match[0]
parsed.update({
# Farenheight to Centigrade
'temp': (float(degrees_farenheit) - 32) / 1.8
})
match = re.findall(r"r([0-9]{3})", body)
if match:
rainfall_hour = match[0]
parsed.update({
# hundreths of an inch to mm
'rain_1h': float(int(rainfall_hour) * rain_multiplier)
})
match = re.findall(r"p([0-9]{3})", body)
if match:
rainfall_24h = match[0]
parsed.update({
'rain_24h': float(int(rainfall_24h) * rain_multiplier)
})
match = re.findall(r"P([0-9]{3})", body)
if match:
rainfall_since_midnight = match[0]
parsed.update({
# hundreths of an inch to mm
'rain_since_mid': float(int(rainfall_since_midnight) * 0.254)
})
match = re.findall(r"h([0-9]{2})", body)
if match:
humidity = match[0]
parsed.update({
'humidity': int(humidity)
})
match = re.findall(r"b([0-9]{5})", body)
if match:
pressure = match[0]
parsed.update({
# deci hPA to hPa
'hPa': float(pressure) / 10
})
match = re.findall(r"l([0-9]{3})", body)
if match:
luminosity = match[0]
parsed.update({
# http://www.aprs.org/aprs11/spec-wx.txt
'luminosity': int(luminosity) + 1000
})
match = re.findall(r"L([0-9]{3})", body)
if match:
luminosity = match[0]
parsed.update({
'luminosity': int(luminosity)
})
match = re.findall(r"\#([0-9]{3})", body)
if match:
rain_raw = match[0]
parsed.update({
'rain_raw': int(rain_raw)
})
# strip weather data
body = re.sub(r"([cSgtrpPlLs#][0-9\-\. ]{3}|h[0-9\. ]{2}|b[0-9\. ]{5})", '', body)
return (body, parsed)

View File

@ -9,7 +9,7 @@ class ParseCommentWeather(unittest.TestCase):
def test_wind(self):
# misc value
expected = "009/009", {
expected = "", {
"wind_speed": (9 * wind_multiplier),
"wind_direction": 9
}
@ -17,7 +17,7 @@ class ParseCommentWeather(unittest.TestCase):
self.assertEqual(expected, result)
# Daft result but possible
expected = "999/999", {
expected = "", {
"wind_speed": (999 * wind_multiplier),
"wind_direction": 999
}
@ -25,7 +25,7 @@ class ParseCommentWeather(unittest.TestCase):
self.assertEqual(expected, result)
#Positionless packet
expected = "c009s009", {
expected = "", {
"wind_speed": float(9 * wind_multiplier),
"wind_direction": 9
}
@ -33,7 +33,7 @@ class ParseCommentWeather(unittest.TestCase):
self.assertEqual(expected, result)
# Daft result but possible
expected = "c999s999", {
expected = "", {
"wind_speed": (999 * wind_multiplier),
"wind_direction": 999
}
@ -42,156 +42,150 @@ class ParseCommentWeather(unittest.TestCase):
def test_temp(self):
# Min
expected = "t-99", {
"temp": float((-99.0 - 32) / 1.8)
expected = "", {
"temperature": float((-99.0 - 32) / 1.8)
}
result = _parse_comment_weather("t-99")
self.assertEqual(expected, result)
# Misc
expected = "t-40", {
"temp": -40.0
expected = "", {
"temperature": -40.0
}
result = _parse_comment_weather("t-40")
self.assertEqual(expected, result)
# Zero F
expected = "t000", {
"temp": -17.77777777777778
expected = "", {
"temperature": -17.77777777777778
}
result = _parse_comment_weather("t000")
self.assertEqual(expected, result)
# Daft, but possible
expected = "t999", {
"temp": 537.2222222222222
expected = "", {
"temperature": 537.2222222222222
}
result = _parse_comment_weather("t999")
self.assertEqual(expected, result)
def test_rain_1h(self):
expected = "r000", {
expected = "", {
"rain_1h": 0.0
}
result = _parse_comment_weather("r000")
self.assertEqual(expected, result)
expected = "r999", {
expected = "", {
"rain_1h": float(999 * mm_multiplier)
}
result = _parse_comment_weather("r999")
self.assertEqual(expected, result)
def test_rain_24h(self):
expected = "p000", {
expected = "", {
"rain_24h": 0.0
}
result = _parse_comment_weather("p000")
self.assertEqual(expected, result)
expected = "p999", {
expected = "", {
"rain_24h": float(999 * mm_multiplier)
}
result = _parse_comment_weather("p999")
self.assertEqual(expected, result)
def test_rain_since_mid(self):
expected = "P000", {
"rain_since_mid": 0.0
def test_rain_since_midnight(self):
expected = "", {
"rain_since_midnight": 0.0
}
result = _parse_comment_weather("P000")
self.assertEqual(expected, result)
expected = "P999", {
"rain_since_mid": float(999 * mm_multiplier)
expected = "", {
"rain_since_midnight": float(999 * mm_multiplier)
}
result = _parse_comment_weather("P999")
self.assertEqual(expected, result)
def test_humidity(self):
expected = "h00", {
expected = "", {
"humidity": 0.0
}
result = _parse_comment_weather("h00")
self.assertEqual(expected, result)
expected = "h99", {
expected = "", {
"humidity": 99
}
result = _parse_comment_weather("h99")
self.assertEqual(expected, result)
# Invalid value
expected = "h9", {
}
result = _parse_comment_weather("h9")
self.assertEqual(expected, result)
def test_pressure(self):
expected = "b00000", {
"hPa": 0.0
expected = "", {
"pressure": 0.0
}
result = _parse_comment_weather("b00000")
self.assertEqual(expected, result)
expected = "b99999", {
"hPa": 9999.9
expected = "", {
"pressure": 9999.9
}
result = _parse_comment_weather("b99999")
self.assertEqual(expected, result)
def test_luminosity(self):
expected = "L000", {
expected = "", {
"luminosity": 000
}
result = _parse_comment_weather("L000")
self.assertEqual(expected, result)
expected = "L999", {
expected = "", {
"luminosity": 999
}
result = _parse_comment_weather("L999")
self.assertEqual(expected, result)
expected = "l123", {
expected = "", {
"luminosity": 1123
}
result = _parse_comment_weather("l123")
self.assertEqual(expected, result)
expected = "l999", {
expected = "", {
"luminosity": 1999
}
result = _parse_comment_weather("l999")
self.assertEqual(expected, result)
def test_snow(self):
expected = "s000", {
expected = "", {
"snow": 000
}
result = _parse_comment_weather("s000")
result = _parse_comment_weather("s...s000")
self.assertEqual(expected, result)
expected = "s5.5", {
expected = "", {
"snow": float(5.5 * 25.4)
}
result = _parse_comment_weather("s5.5")
result = _parse_comment_weather("s...s5.5")
self.assertEqual(expected, result)
expected = "s999", {
expected = "", {
"snow": float(999 * 25.4)
}
result = _parse_comment_weather("s999")
result = _parse_comment_weather("s...s999")
self.assertEqual(expected, result)
def test_rain_raw(self):
expected = "#000", {
expected = "", {
"rain_raw": 000
}
result = _parse_comment_weather("#000")
self.assertEqual(expected, result)
expected = "#999", {
expected = "", {
"rain_raw": 999
}
result = _parse_comment_weather("#999")
@ -199,14 +193,14 @@ class ParseCommentWeather(unittest.TestCase):
# Not possible in real world (rain and snow measurements)
def test_positionless_packet(self):
expected = "10090556c220s004g005t077r010p020P030h50b09900s5.5wRSW", {
"hPa": 990.0,
expected = "10090556wRSW", {
"pressure": 990.0,
"humidity": 50,
"rain_1h": float(010.0 * mm_multiplier),
"rain_24h": float(020.0 * mm_multiplier),
"rain_since_mid": float(030.0 * mm_multiplier),
"rain_since_midnight": float(030.0 * mm_multiplier),
"snow": float(5.5 * 25.4),
"temp": float((77.0 - 32) / 1.8),
"temperature": float((77.0 - 32) / 1.8),
"wind_direction": 220,
"wind_gust": 5.0 * wind_multiplier,
"wind_speed": 4.0 * wind_multiplier
@ -215,7 +209,7 @@ class ParseCommentWeather(unittest.TestCase):
result = _parse_comment_weather("10090556c220s004g005t077r010p020P030h50b09900s5.5wRSW")
self.assertEqual(expected, result)
expected = "10090556c220s112g t r p P h b wRSW", {
expected = "10090556wRSW", {
"wind_direction": 220,
"wind_speed": 112 * wind_multiplier
}
@ -223,25 +217,19 @@ class ParseCommentWeather(unittest.TestCase):
result = _parse_comment_weather("10090556c220s112g t r p P h b wRSW")
self.assertEqual(expected, result)
expected = "10090556c220s112g...t...r...p...P...h..b.....wRSW", {
expected = "10090556wRSW", {
"wind_direction": 220,
"wind_speed": 112 * wind_multiplier
}
result = _parse_comment_weather("10090556c220s112g...t...r...p...P...h..b.....wRSW")
self.assertEqual(expected, result)
expected = "_10090556s9.9", {
"snow": 9.9 * 25.4
}
result = _parse_comment_weather("_10090556s9.9")
self.assertEqual(expected, result)
def test_position_packet(self):
expected = "319/001g004t048r...p P000h19b10294eCumulusWMR100", {
"hPa": 1029.4,
expected = "eCumulusWMR100", {
"pressure": 1029.4,
"humidity": 19,
"rain_since_mid": 0.0,
"temp": (48.0 - 32) / 1.8,
"rain_since_midnight": 0.0,
"temperature": (48.0 - 32) / 1.8,
"wind_direction": 319,
"wind_gust": 4.0 * wind_multiplier,
"wind_speed": 1 * wind_multiplier