update EHS module

This commit is contained in:
junzis 2017-03-23 11:48:51 +01:00
parent 621d3e7580
commit 46fee6b7dc
2 changed files with 176 additions and 13 deletions

View File

@ -75,6 +75,59 @@ def checkbits(data, sb, msb, lsb):
return True return True
# ------------------------------------------
# Common functions
# ------------------------------------------
def isnull(msg):
"""check if the data bits are all zeros
Args:
msg (String): 28 bytes hexadecimal message string
Returns:
bool: True or False
"""
d = util.hex2bin(data(msg))
if util.bin2int(d) > 0:
return False
else:
return True
def df20alt(msg):
"""Computes the altitude from DF20 bit 20-32
Args:
msg (String): 28 bytes hexadecimal message string
Returns:
int: altitude in ft
"""
if df(msg) != 20:
raise RuntimeError("Message must be Downlink Format 20.")
# Altitude code, bit 20-32
mbin = util.hex2bin(msg)
mbit = mbin[25] # M bit: 26
qbit = mbin[27] # Q bit: 28
if mbit == '0': # unit in ft
if qbit == '1': # 25ft interval
vbin = mbin[19:25] + mbin[26] + mbin[28:32]
alt = util.bin2int(vbin) * 25
if qbit == '0': # 100ft interval
# to be implemented
alt = None
if mbit == '1': # unit in meter
vbin = mbin[19:25] + mbin[26:32]
alt = int(util.bin2int(vbin) * 3.28084) # convert to ft
return alt
# ------------------------------------------ # ------------------------------------------
# DF 20/21, BDS 2,0 # DF 20/21, BDS 2,0
# ------------------------------------------ # ------------------------------------------
@ -89,6 +142,9 @@ def isBDS20(msg):
bool: True or False bool: True or False
""" """
if isnull(msg):
return False
# status bit 1, 14, and 27 # status bit 1, 14, and 27
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
@ -145,6 +201,9 @@ def isBDS40(msg):
bool: True or False bool: True or False
""" """
if isnull(msg):
return False
# status bit 1, 14, and 27 # status bit 1, 14, and 27
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
@ -173,6 +232,10 @@ def alt40mcp(msg):
int: altitude in feet int: altitude in feet
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[0] == '0':
return None
alt = util.bin2int(d[1:13]) * 16 # ft alt = util.bin2int(d[1:13]) * 16 # ft
return alt return alt
@ -187,6 +250,10 @@ def alt40fms(msg):
int: altitude in feet int: altitude in feet
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[13] == '0':
return None
alt = util.bin2int(d[14:26]) * 16 # ft alt = util.bin2int(d[14:26]) * 16 # ft
return alt return alt
@ -201,6 +268,10 @@ def p40baro(msg):
float: pressure in millibar float: pressure in millibar
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[26] == '0':
return None
p = util.bin2int(d[27:39]) * 0.1 + 800 # millibar p = util.bin2int(d[27:39]) * 0.1 + 800 # millibar
return p return p
@ -221,6 +292,9 @@ def isBDS44(msg, rev=False):
bool: True or False bool: True or False
""" """
if isnull(msg):
return False
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
result = True result = True
@ -231,13 +305,15 @@ def isBDS44(msg, rev=False):
& checkbits(d, 35, 36, 46) & checkbits(d, 47, 48, 49) \ & checkbits(d, 35, 36, 46) & checkbits(d, 47, 48, 49) \
& checkbits(d, 50, 51, 56) & checkbits(d, 50, 51, 56)
else: else:
# status bit 5, 15, 24, 36, 49 # status bit 5, 15, 24, 36, 49
result = result & checkbits(d, 5, 6, 14) \ result = result & checkbits(d, 5, 6, 14) \
& checkbits(d, 15, 16, 23) & checkbits(d, 24, 25, 35) \ & checkbits(d, 15, 16, 23) & checkbits(d, 24, 25, 35) \
& checkbits(d, 36, 37, 47) & checkbits(d, 49, 50, 56) & checkbits(d, 36, 37, 47) & checkbits(d, 49, 50, 56)
if not result:
return False
vw = wind44(msg, rev=rev) vw = wind44(msg, rev=rev)
if vw and vw[0] > 250: if vw and vw[0] > 250:
result &= False result &= False
@ -295,13 +371,14 @@ def temp44(msg, rev=False):
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if not rev: if not rev:
if d[22] == '0':
return None
sign = int(d[23]) sign = int(d[23])
temp = util.bin2int(d[24:34]) * 0.125 # celsius temp = util.bin2int(d[24:34]) * 0.125 # celsius
temp = round(temp, 1) temp = round(temp, 1)
else: else:
status = int(d[23]) if d[23] == '0':
if not status:
return None return None
sign = int(d[24]) sign = int(d[24])
@ -324,15 +401,13 @@ def p44(msg, rev=False):
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if not rev: if not rev:
status = int(d[34]) if d[34] == '0':
if not status:
return None return None
p = util.bin2int(d[35:46]) # hPa p = util.bin2int(d[35:46]) # hPa
else: else:
status = int(d[35]) if d[35] == '0':
if not status:
return None return None
p = util.bin2int(d[36:47]) # hPa p = util.bin2int(d[36:47]) # hPa
@ -353,15 +428,13 @@ def hum44(msg, rev=False):
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if not rev: if not rev:
status = int(d[49]) if d[49] == '0':
if not status:
return None return None
hm = util.bin2int(d[50:56]) * 100.0 / 64 # % hm = util.bin2int(d[50:56]) * 100.0 / 64 # %
else: else:
status = int(d[48]) if d[48] == '0':
if not status:
return None return None
hm = util.bin2int(d[49:56]) # % hm = util.bin2int(d[49:56]) # %
@ -385,6 +458,9 @@ def isBDS50(msg):
bool: True or False bool: True or False
""" """
if isnull(msg):
return False
# status bit 1, 12, 24, 35, 46 # status bit 1, 12, 24, 35, 46
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
@ -394,6 +470,9 @@ def isBDS50(msg):
& checkbits(d, 24, 25, 34) & checkbits(d, 35, 36, 45) \ & checkbits(d, 24, 25, 34) & checkbits(d, 35, 36, 45) \
& checkbits(d, 46, 47, 56) & checkbits(d, 46, 47, 56)
if not result:
return False
if d[2:11] == "000000000": if d[2:11] == "000000000":
result &= True result &= True
else: else:
@ -406,7 +485,7 @@ def isBDS50(msg):
if tas50(msg) > 500: if tas50(msg) > 500:
result &= False result &= False
if abs(tas50(msg) - gs50(msg)) > 100: if abs(tas50(msg) - gs50(msg)) > 200:
result &= False result &= False
return result return result
@ -423,6 +502,10 @@ def roll50(msg):
negative->left wing down, positive->right wing down negative->left wing down, positive->right wing down
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[0] == '0':
return None
sign = int(d[1]) # 1 -> left wing down sign = int(d[1]) # 1 -> left wing down
value = util.bin2int(d[2:11]) * 45.0 / 256.0 # degree value = util.bin2int(d[2:11]) * 45.0 / 256.0 # degree
angle = -1 * value if sign else value angle = -1 * value if sign else value
@ -439,6 +522,10 @@ def trk50(msg):
float: angle in degrees to true north (from 0 to 360) float: angle in degrees to true north (from 0 to 360)
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[11] == '0':
return None
sign = int(d[12]) # 1 -> west sign = int(d[12]) # 1 -> west
value = util.bin2int(d[13:23]) * 90.0 / 512.0 # degree value = util.bin2int(d[13:23]) * 90.0 / 512.0 # degree
angle = 360 - value if sign else value angle = 360 - value if sign else value
@ -455,6 +542,10 @@ def gs50(msg):
int: ground speed in knots int: ground speed in knots
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[23] == '0':
return None
spd = util.bin2int(d[24:34]) * 2 # kts spd = util.bin2int(d[24:34]) * 2 # kts
return spd return spd
@ -469,6 +560,13 @@ def rtrk50(msg):
float: angle rate in degrees/second float: angle rate in degrees/second
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[34] == '0':
return None
if d[36:45] == "111111111":
return None
sign = int(d[35]) # 1 -> minus sign = int(d[35]) # 1 -> minus
value = util.bin2int(d[36:45]) * 8.0 / 256.0 # degree / sec value = util.bin2int(d[36:45]) * 8.0 / 256.0 # degree / sec
angle = -1 * value if sign else value angle = -1 * value if sign else value
@ -485,6 +583,10 @@ def tas50(msg):
int: true airspeed in knots int: true airspeed in knots
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[45] == '0':
return None
tas = util.bin2int(d[46:56]) * 2 # kts tas = util.bin2int(d[46:56]) * 2 # kts
return tas return tas
@ -505,6 +607,9 @@ def isBDS53(msg):
bool: True or False bool: True or False
""" """
if isnull(msg):
return False
# status bit 1, 13, 24, 34, 47 # status bit 1, 13, 24, 34, 47
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
@ -514,6 +619,9 @@ def isBDS53(msg):
& checkbits(d, 24, 25, 33) & checkbits(d, 34, 35, 46) \ & checkbits(d, 24, 25, 33) & checkbits(d, 34, 35, 46) \
& checkbits(d, 47, 49, 56) & checkbits(d, 47, 49, 56)
if not result:
return False
if ias53(msg) > 500: if ias53(msg) > 500:
result &= False result &= False
@ -539,6 +647,10 @@ def hdg53(msg):
float: angle in degrees to true north (from 0 to 360) float: angle in degrees to true north (from 0 to 360)
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[0] == '0':
return None
sign = int(d[1]) # 1 -> west sign = int(d[1]) # 1 -> west
value = util.bin2int(d[2:12]) * 90.0 / 512.0 # degree value = util.bin2int(d[2:12]) * 90.0 / 512.0 # degree
angle = 360 - value if sign else value angle = 360 - value if sign else value
@ -555,6 +667,10 @@ def ias53(msg):
int: indicated arispeed in knots int: indicated arispeed in knots
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[12] == '0':
return None
ias = util.bin2int(d[13:23]) # knots ias = util.bin2int(d[13:23]) # knots
return ias return ias
@ -569,6 +685,10 @@ def mach53(msg):
float: MACH number float: MACH number
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[23] == '0':
return None
mach = util.bin2int(d[24:33]) * 0.008 mach = util.bin2int(d[24:33]) * 0.008
return round(mach, 3) return round(mach, 3)
@ -583,6 +703,10 @@ def tas53(msg):
float: true airspeed in knots float: true airspeed in knots
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[33] == '0':
return None
tas = util.bin2int(d[34:46]) * 0.5 # kts tas = util.bin2int(d[34:46]) * 0.5 # kts
return round(tas, 1) return round(tas, 1)
@ -596,6 +720,10 @@ def vr53(msg):
int: vertical rate in feet/minutes int: vertical rate in feet/minutes
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[46] == '0':
return None
sign = d[47] # 1 -> minus sign = d[47] # 1 -> minus
value = util.bin2int(d[48:56]) * 64 # feet/min value = util.bin2int(d[48:56]) * 64 # feet/min
roc = -1*value if sign else value roc = -1*value if sign else value
@ -615,6 +743,10 @@ def isBDS60(msg):
Returns: Returns:
bool: True or False bool: True or False
""" """
if isnull(msg):
return False
# status bit 1, 13, 24, 35, 46 # status bit 1, 13, 24, 35, 46
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
@ -624,6 +756,9 @@ def isBDS60(msg):
& checkbits(d, 24, 25, 34) & checkbits(d, 35, 36, 45) \ & checkbits(d, 24, 25, 34) & checkbits(d, 35, 36, 45) \
& checkbits(d, 46, 47, 56) & checkbits(d, 46, 47, 56)
if not result:
return False
if not (1 < ias60(msg) < 500): if not (1 < ias60(msg) < 500):
result &= False result &= False
@ -649,6 +784,10 @@ def hdg60(msg):
float: heading in degrees to megnetic north (from 0 to 360) float: heading in degrees to megnetic north (from 0 to 360)
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[0] == '0':
return None
sign = int(d[1]) # 1 -> west sign = int(d[1]) # 1 -> west
value = util.bin2int(d[2:12]) * 90 / 512.0 # degree value = util.bin2int(d[2:12]) * 90 / 512.0 # degree
hdg = 360 - value if sign else value hdg = 360 - value if sign else value
@ -665,6 +804,10 @@ def ias60(msg):
int: indicated airspeed in knots int: indicated airspeed in knots
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[12] == '0':
return None
ias = util.bin2int(d[13:23]) # kts ias = util.bin2int(d[13:23]) # kts
return ias return ias
@ -679,6 +822,10 @@ def mach60(msg):
float: MACH number float: MACH number
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[23] == '0':
return None
mach = util.bin2int(d[24:34]) * 2.048 / 512.0 mach = util.bin2int(d[24:34]) * 2.048 / 512.0
return round(mach, 3) return round(mach, 3)
@ -693,6 +840,10 @@ def vr60baro(msg):
int: vertical rate in feet/minutes int: vertical rate in feet/minutes
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[34] == '0':
return None
sign = d[35] # 1 -> minus sign = d[35] # 1 -> minus
value = util.bin2int(d[36:45]) * 32 # feet/min value = util.bin2int(d[36:45]) * 32 # feet/min
roc = -1*value if sign else value roc = -1*value if sign else value
@ -709,6 +860,10 @@ def vr60ins(msg):
int: vertical rate in feet/minutes int: vertical rate in feet/minutes
""" """
d = util.hex2bin(data(msg)) d = util.hex2bin(data(msg))
if d[45] == '0':
return None
sign = d[46] # 1 -> minus sign = d[46] # 1 -> minus
value = util.bin2int(d[47:56]) * 32 # feet/min value = util.bin2int(d[47:56]) * 32 # feet/min
roc = -1*value if sign else value roc = -1*value if sign else value
@ -724,6 +879,10 @@ def BDS(msg):
Returns: Returns:
String or None: Version: "BDS20", "BDS40", "BDS50", or "BDS60". Or None, if nothing matched String or None: Version: "BDS20", "BDS40", "BDS50", or "BDS60". Or None, if nothing matched
""" """
if isnull(msg):
return None
is20 = isBDS20(msg) is20 = isBDS20(msg)
is40 = isBDS40(msg) is40 = isBDS40(msg)
is44 = isBDS44(msg) is44 = isBDS44(msg)

View File

@ -9,6 +9,10 @@ def test_ehs_icao():
assert ehs.icao("A000029CFFBAA11E2004727281F1") == '4243D0' assert ehs.icao("A000029CFFBAA11E2004727281F1") == '4243D0'
def test_df20alt():
assert ehs.df20alt("A02014B400000000000000F9D514") == 33300
def test_ehs_BDS(): def test_ehs_BDS():
assert ehs.BDS("A0001838201584F23468207CDFA5") == 'BDS20' assert ehs.BDS("A0001838201584F23468207CDFA5") == 'BDS20'
assert ehs.BDS("A0001839CA3800315800007448D9") == 'BDS40' assert ehs.BDS("A0001839CA3800315800007448D9") == 'BDS40'