From 128163b41d782e3bb2104795ca97cb0ce3996531 Mon Sep 17 00:00:00 2001 From: junzis Date: Fri, 17 Mar 2017 18:03:54 +0100 Subject: [PATCH] update BDS 4,4 decoder, default to revised version --- pyModeS/ehs.py | 117 ++++++++++++++++++++++++++------------ tests/sample_data_test.py | 3 +- 2 files changed, 84 insertions(+), 36 deletions(-) diff --git a/pyModeS/ehs.py b/pyModeS/ehs.py index e77f35c..d2d6bd6 100644 --- a/pyModeS/ehs.py +++ b/pyModeS/ehs.py @@ -209,36 +209,38 @@ def pbaro(msg): # DF 20/21, BDS 4,4 # ------------------------------------------ -def isBDS44(msg): +def isBDS44(msg, rev=True): """Check if a message is likely to be BDS code 4,4 Meteorological routine air report - WARNING: there is two different definition for BDS4,4. This part of the - decoder is likely to be wrong... - Args: msg (String): 28 bytes hexadecimal message string + rev (bool): using revised version Returns: bool: True or False """ - # status bit 5, 15, 24, 36, 49 d = util.hex2bin(data(msg)) result = True - # --- current version --- - result = result & checkbits(d, 5, 6, 23) \ - & checkbits(d, 35, 36, 46) & checkbits(d, 47, 48, 49) \ - & checkbits(d, 50, 51, 56) + if not rev: + # status bit 5, 35, 47, 50 + result = result & checkbits(d, 5, 6, 23) \ + & checkbits(d, 35, 36, 46) & checkbits(d, 47, 48, 49) \ + & checkbits(d, 50, 51, 56) - # # --- revised future version --- - # result = result & checkbits(d, 5, 6, 14) \ - # & checkbits(d, 15, 16, 23) & checkbits(d, 24, 25, 35) \ - # & checkbits(d, 36, 37, 47) & checkbits(d, 49, 50, 56) - if wind(msg) and wind(msg)[0] > 250: + else: + # status bit 5, 15, 24, 36, 49 + + result = result & checkbits(d, 5, 6, 14) \ + & checkbits(d, 15, 16, 23) & checkbits(d, 24, 25, 35) \ + & checkbits(d, 36, 37, 47) & checkbits(d, 49, 50, 56) + + vw = wind(msg, rev=rev) + if vw and vw[0] > 250: result &= False # if temperature(msg): @@ -248,78 +250,123 @@ def isBDS44(msg): return result -def wind(msg): +def wind(msg, rev=True): """reported wind speed and direction Args: msg (String): 28 bytes hexadecimal message (BDS44) string + rev (bool): using revised version Returns: (int, float): speed (kt), direction (degree) """ d = util.hex2bin(data(msg)) - status = int(d[4]) - if not status: - return None + if not rev: + status = int(d[4]) + if not status: + return None + + speed = util.bin2int(d[5:14]) # knots + direction = util.bin2int(d[14:23]) * 180.0 / 256.0 # degree + + else: + spd_status = int(d[4]) + dir_status = int(d[14]) + + if (not spd_status) or (not dir_status): + return None + + speed = util.bin2int(d[5:14]) # knots + direction = util.bin2int(d[15:23]) * 180.0 / 128.0 # degree - speed = util.bin2int(d[5:14]) # knots - direction = util.bin2int(d[14:23]) * 180.0 / 256.0 # degree return round(speed, 0), round(direction, 1) -def temperature(msg): +def temperature(msg, rev=True): """reported air temperature Args: msg (String): 28 bytes hexadecimal message (BDS44) string + rev (bool): using revised version Returns: float: tmeperature in Celsius degree """ d = util.hex2bin(data(msg)) - sign = int(d[23]) # 1 -> minus - temp = util.bin2int(d[24:34]) * 0.25 # celsius - temp = round(temp, 1) - return -1 * temp if sign else temp + if not rev: + sign = int(d[23]) + temp = util.bin2int(d[24:34]) * 0.125 # celsius + temp = round(temp, 1) + + else: + status = int(d[23]) + if not status: + return None + + sign = int(d[24]) + temp = util.bin2int(d[25:35]) * 0.125 # celsius + temp = round(temp, 1) + + return temp if sign else -1*temp -def pressure(msg): +def pressure(msg, rev=True): """reported average static pressure Args: msg (String): 28 bytes hexadecimal message (BDS44) string + rev (bool): using revised version Returns: int: static pressure in hPa """ d = util.hex2bin(data(msg)) - status = int(d[34]) - if not status: - return None + if not rev: + status = int(d[34]) + if not status: + return None + + p = util.bin2int(d[35:46]) # hPa + + else: + status = int(d[35]) + if not status: + return None + + p = util.bin2int(d[36:47]) # hPa - p = util.bin2int(d[35:46]) # hPa return p -def humidity(msg): +def humidity(msg, rev=True): """reported humidity Args: msg (String): 28 bytes hexadecimal message (BDS44) string + rev (bool): using revised version Returns: float: percentage of humidity, [0 - 100] % """ d = util.hex2bin(data(msg)) - status = int(d[49]) - if not status: - return None + if not rev: + status = int(d[49]) + if not status: + return None + + hm = util.bin2int(d[50:56]) * 100.0 / 64 # % + + else: + status = int(d[48]) + if not status: + return None + + hm = util.bin2int(d[49:56]) # % - hm = util.bin2int(d[50:56]) * 100.0 / 64 # % return round(hm, 1) diff --git a/tests/sample_data_test.py b/tests/sample_data_test.py index 7749257..83da4d2 100644 --- a/tests/sample_data_test.py +++ b/tests/sample_data_test.py @@ -62,7 +62,8 @@ def ehs_decode_all(n=None): ehs.alt_fms(m), ehs.pbaro(m)) if vBDS == "BDS44": - print(ts, m, icao, vBDS, ehs.wind(m)) + print(ts, m, icao, vBDS, ehs.wind(m), + ehs.temperature(m), ehs.pressure(m)) if vBDS == "BDS50": print(ts, m, icao, vBDS, ehs.roll(m), ehs.track(m),