fixing code

This commit is contained in:
Junzi Sun 2018-06-22 22:52:11 +02:00
parent 457a948879
commit a3e44b5626
2 changed files with 108 additions and 117 deletions

View File

@ -166,53 +166,6 @@ def speed_heading(msg):
return spd, trk_or_hdg return spd, trk_or_hdg
def nic(msg):
"""Calculate NIC, navigation integrity category
Args:
msg (string): 28 bytes hexadecimal message string
Returns:
int: NIC number (from 0 to 11), -1 if not applicable
"""
if typecode(msg) < 9 or typecode(msg) > 18:
raise RuntimeError("%s: Not a airborne position message, expecting 8<TC<19" % msg)
msgbin = common.hex2bin(msg)
tc = typecode(msg)
nic_sup_b = common.bin2int(msgbin[39])
if tc in [0, 18, 22]:
nic = 0
elif tc == 17:
nic = 1
elif tc == 16:
if nic_sup_b:
nic = 3
else:
nic = 2
elif tc == 15:
nic = 4
elif tc == 14:
nic = 5
elif tc == 13:
nic = 6
elif tc == 12:
nic = 7
elif tc == 11:
if nic_sup_b:
nic = 9
else:
nic = 8
elif tc in [10, 21]:
nic = 10
elif tc in [9, 20]:
nic = 11
else:
nic = -1
return nic
def oe_flag(msg): def oe_flag(msg):
"""Check the odd/even flag. Bit 54, 0 for even, 1 for odd. """Check the odd/even flag. Bit 54, 0 for even, 1 for odd.
Args: Args:
@ -224,21 +177,35 @@ def oe_flag(msg):
return int(msgbin[53]) return int(msgbin[53])
# Uncertainty & accuracy # Uncertainty & accuracy
def nic(msg, *argv):
if len(argv) == 1:
# assume ads-b v1, only one supplement bit
return nic_v1(msg, *argv)
elif len(argv) == 3:
# assume ads-b v2, three supplement bits
return nic_v2(msg, *argv)
def nic_v1(msg,nic_sup_b):
"""Calculate NIC, navigation integrity category def nic_v1(msg, nic_sup_b):
"""Calculate NIC, navigation integrity category for ADS-B version 1
Args: Args:
msg (string): 28 bytes hexadecimal message string, nic_sup_b (int): NIC supplement msg (string): 28 bytes hexadecimal message string
nic_sup_b (int or string): NIC supplement
Returns: Returns:
int: NIC number (from 0 to 11), -1 if not applicable int: NIC number (from 0 to 11), -1 if not applicable
""" """
if typecode(msg) < 5 or typecode(msg) > 22: if typecode(msg) < 5 or typecode(msg) > 22:
raise RuntimeError("%s: Not a surface position message (5<TC<8, )airborne position message (8<TC<19), airborne position with GNSS height (20<TC<22)" % msg) raise RuntimeError("%s: Not a surface position message (5<TC<8), \
airborne position message (8<TC<19), \
or airborne position with GNSS height (20<TC<22)" % msg)
tc = typecode(msg) tc = typecode(msg)
if nic_sup_b in ['0', '1']:
nic_sup_b = int(nic_sup_b)
if tc in [0, 8, 18, 22]: if tc in [0, 8, 18, 22]:
nic = 0 nic = 0
elif tc == 17: elif tc == 17:
@ -277,19 +244,34 @@ def nic_v1(msg,nic_sup_b):
nic = -1 nic = -1
return nic return nic
def nic_v2(msg,nic_a,nic_b,nic_c):
"""Calculate NIC, navigation integrity category def nic_v2(msg, nic_a, nic_b, nic_c):
"""Calculate NIC, navigation integrity category, for ADS-B version 2
Args: Args:
msg (string): 28 bytes hexadecimal message string, nic_a (int): NIC supplement, nic_b (int): NIC supplement, nic_c (int): NIC supplement msg (string): 28 bytes hexadecimal message string
nic_a (int or string): NIC supplement
nic_b (int or srting): NIC supplement
nic_c (int or string): NIC supplement
Returns: Returns:
int: NIC number (from 0 to 11), -1 if not applicable int: NIC number (from 0 to 11), -1 if not applicable
""" """
if typecode(msg) < 5 or typecode(msg) > 22: if typecode(msg) < 5 or typecode(msg) > 22:
raise RuntimeError("%s: Not a surface position message (5<TC<8, )airborne position message (8<TC<19), airborne position with GNSS height (20<TC<22)" % msg) raise RuntimeError("%s: Not a surface position message (5<TC<8) \
airborne position message (8<TC<19), \
or airborne position with GNSS height (20<TC<22)" % msg)
tc = typecode(msg) tc = typecode(msg)
if nic_a in ['0', '1']:
nic_a = int(nic_a)
if nic_b in ['0', '1']:
nic_b = int(nic_b)
if nic_c in ['0', '1']:
nic_c = int(nic_c)
if tc in [0, 18, 22]: if tc in [0, 18, 22]:
nic = 0 nic = 0
elif tc == 17: elif tc == 17:
@ -343,59 +325,67 @@ def nic_v2(msg,nic_a,nic_b,nic_c):
return nic return nic
def nic_s(msg): def nic_s(msg):
"""Calculate NICs, navigation integrity category supplement """Obtain NIC supplement bit, TC=31 message
Args: Args:
msg (string): 28 bytes hexadecimal message string msg (string): 28 bytes hexadecimal message string
Returns: Returns:
int: NIC number (from 0 to 11), -1 if not applicable int: NICs number (0 or 1)
""" """
if typecode(msg) != 31: tc = typecode(msg)
if tc != 31:
raise RuntimeError("%s: Not a status operation message, expecting TC = 31" % msg) raise RuntimeError("%s: Not a status operation message, expecting TC = 31" % msg)
msgbin = common.hex2bin(msg) msgbin = common.hex2bin(msg)
nic_s = common.bin2int(msgbin[75]) nic_s = int(msgbin[75])
return nic_s return nic_s
def nic_a_and_c(msg):
"""Calculate NICa and NICc, navigation integrity category supplements def nic_a_c(msg):
"""Obtain NICa/c, navigation integrity category supplements a and c
Args: Args:
msg (string): 28 bytes hexadecimal message string msg (string): 28 bytes hexadecimal message string
Returns: Returns:
int: NIC number (from 0 to 11), -1 if not applicable (int, int): NICa and NICc number (0 or 1)
""" """
if typecode(msg) != 31: tc = typecode(msg)
if tc != 31:
raise RuntimeError("%s: Not a status operation message, expecting TC = 31" % msg) raise RuntimeError("%s: Not a status operation message, expecting TC = 31" % msg)
msgbin = common.hex2bin(msg) msgbin = common.hex2bin(msg)
nic_a = common.bin2int(msgbin[75]) nic_a = int(msgbin[75])
nic_c = common.bin2int(msgbin[51]) nic_c = int(msgbin[51])
return nic_a, nic_c return nic_a, nic_c
def nic_b(msg): def nic_b(msg):
"""Calculate NICb, navigation integrity category supplement """Obtain NICb, navigation integrity category supplement-b
Args: Args:
msg (string): 28 bytes hexadecimal message string msg (string): 28 bytes hexadecimal message string
Returns: Returns:
int: NIC number (from 0 to 11), -1 if not applicable int: NICb number (0 or 1)
""" """
if typecode(msg) < 9 or typecode(msg) > 18: tc = typecode(msg)
raise RuntimeError("%s: Not a airborne position message, expecting 8<TC<19" % msg)
if tc < 9 or tc > 18:
raise RuntimeError("%s: Not a airborne position message, expecting 8<TC<19" % msg)
msgbin = common.hex2bin(msg) msgbin = common.hex2bin(msg)
nic_b = common.bin2int(msgbin[39]) nic_b = int(msgbin[39])
return nic_b return nic_b
def nac_p(msg): def nac_p(msg):
"""Calculate NACp, Navigation Accuracy Category - Position """Calculate NACp, Navigation Accuracy Category - Position
@ -403,19 +393,21 @@ def nac_p(msg):
msg (string): 28 bytes hexadecimal message string, TC = 29 or 31 msg (string): 28 bytes hexadecimal message string, TC = 29 or 31
Returns: Returns:
int: NACp number (from 0 to 11), -1 if not applicable int: NACp number (0 or 1)
""" """
if typecode(msg) not in [29,31]: tc = typecode(msg)
raise RuntimeError("%s: Not a target state and status message neither operation status message, expecting TC = 29 or 31" % msg)
if tc not in [29, 31]:
raise RuntimeError("%s: Not a target state and status message, \
or operation status message, expecting TC = 29 or 31" % msg)
msgbin = common.hex2bin(msg) msgbin = common.hex2bin(msg)
tc = typecode(msg)
if tc == 29: if tc == 29:
nac_p = common.bin2int(msgbin[71:75]) nac_p = common.bin2int(msgbin[71:75])
elif tc == 31: elif tc == 31:
nac_p = common.bin2int(msgbin[76:80]) nac_p = common.bin2int(msgbin[76:80])
else:
nac_p = -1
return nac_p return nac_p
@ -428,47 +420,46 @@ def nac_v(msg):
Returns: Returns:
int: NACv number (from 0 to 4), -1 if not applicable int: NACv number (from 0 to 4), -1 if not applicable
""" """
if typecode(msg) != 19: tc = typecode(msg)
if tc != 19:
raise RuntimeError("%s: Not an airborne velocity message, expecting TC = 19" % msg) raise RuntimeError("%s: Not an airborne velocity message, expecting TC = 19" % msg)
msgbin = common.hex2bin(msg) msgbin = common.hex2bin(msg)
tc = typecode(msg) nac_v = common.bin2int(msgbin[42:45])
if tc == 19:
nac_v = common.bin2int(msgbin[42:45])
else:
nac_v = -1
return nac_v return nac_v
def sil(msg,version):
"""Calculate SIL, Surveillance Integrity Level def sil(msg, version):
"""Calculate SIL, Surveillance Integrity Level
Args: Args:
msg (string): 28 bytes hexadecimal message string with TC = 29, 31 msg (string): 28 bytes hexadecimal message string with TC = 29, 31
Returns: Returns:
int: sil number, -1 if not applicable (int, int): sil number and sil supplement (only for v2)
""" """
if typecode(msg) not in [29,31]: tc = typecode(msg)
raise RuntimeError("%s: Not a target state and status message neither operation status message, expecting TC = 29 or 31" % msg)
if tc not in [29, 31]:
raise RuntimeError("%s: Not a target state and status messag, \
or operation status message, expecting TC = 29 or 31" % msg)
msgbin = common.hex2bin(msg) msgbin = common.hex2bin(msg)
tc = typecode(msg)
if tc == 29: if tc == 29:
sil = common.bin2int(msgbin[76:78]) sil = common.bin2int(msgbin[76:78])
elif tc == 31: elif tc == 31:
sil = common.bin2int(msg[82:84]) sil = common.bin2int(msg[82:84])
else:
sil = -1
if version == 2: if version == 2:
if typecode(msg) == 29: if version == 29:
sils = common.bin2int(msgbin[39]) sil_sup = common.bin2int(msgbin[39])
elif typecode(msg) == 31: elif version == 31:
sils = common.bin2int(msgbin[86]) sil_sup = common.bin2int(msgbin[86])
else:
sils = -1 return sil, sil_sup
return sil, sils
def version(msg): def version(msg):
"""ADS-B Version """ADS-B Version
@ -479,12 +470,12 @@ def version(msg):
Returns: Returns:
int: version number int: version number
""" """
msgbin = common.hex2bin(msg) tc = typecode(msg)
if typecode(msg) not in [29,31]:
raise RuntimeError("%s: Not a target state and status message neither operation status message, expecting TC = 29 or 31" % msg) if tc != 31:
raise RuntimeError("%s: Not a status operation message, expecting TC = 31" % msg)
msgbin = common.hex2bin(msg)
version = common.bin2int(msgbin[72:75])
if typecode(msg) in [29,31]:
version = common.bin2int(msgbin[72:75])
else:
version = -1
return version return version

View File

@ -46,7 +46,7 @@ class Stream():
'ias': None, 'ias': None,
'mach': None, 'mach': None,
'hdg': None, 'hdg': None,
'adsb_version' : None, 'adsb_version' : None,
'nic_s' : None, 'nic_s' : None,
'nic_a' : None, 'nic_a' : None,
'nic_b' : None, 'nic_b' : None,
@ -106,36 +106,36 @@ class Stream():
self.acs[icao]['lat'] = latlon[0] self.acs[icao]['lat'] = latlon[0]
self.acs[icao]['lon'] = latlon[1] self.acs[icao]['lon'] = latlon[1]
self.acs[icao]['alt'] = adsb.altitude(msg) self.acs[icao]['alt'] = adsb.altitude(msg)
local_updated_acs_buffer.append(icao)acs[icao]['adsb_version'] local_updated_acs_buffer.append(icao)
# Uncertainty & accuracy # Uncertainty & accuracy
if (5 <= tc <= 8): if (5 <= tc <= 8):
if self.acs[icao]['adsb_version'] == 1: if self.acs[icao]['adsb_version'] == 1:
if self.acs[icao]['nic_s'] != None: if self.acs[icao]['nic_s'] != None:
self.nic = adsb.nic_v1(msg,self.acs[icao]['nic_s']) self.nic = adsb.nic_v1(msg, self.acs[icao]['nic_s'])
elif self.acs[icao]['adsb_version'] == 2: elif self.acs[icao]['adsb_version'] == 2:
if self.acs[icao]['nic_a'] != None and self.acs[icao]['nic_b'] != None: if self.acs[icao]['nic_a'] != None and self.acs[icao]['nic_b'] != None:
self.nic = adsb.nic_v2(msg,self.nic_a,self.acs[icao]['nic_b'],self.acs[icao]['nic_c']) self.nic = adsb.nic_v2(msg, self.nic_a, self.acs[icao]['nic_b'], self.acs[icao]['nic_c'])
if (9 <= tc <= 18): if (9 <= tc <= 18):
if self.acs[icao]['adsb_version'] == 1: if self.acs[icao]['adsb_version'] == 1:
if self.acs[icao]['nic_s'] != None: if self.acs[icao]['nic_s'] != None:
self.nic = adsb.nic_v1(msg,self.acs[icao]['nic_s']) self.nic = adsb.nic_v1(msg, self.acs[icao]['nic_s'])
elif self.acs[icao]['adsb_version'] == 2: elif self.acs[icao]['adsb_version'] == 2:
self.acs[icao]['nic_b'] = adsb.nic_b(msg) self.acs[icao]['nic_b'] = adsb.nic_b(msg)
if self.acs[icao]['nic_a'] != None and self.acs[icao]['nic_b'] != None: if self.acs[icao]['nic_a'] != None and self.acs[icao]['nic_b'] != None:
self.nic = adsb.nic_v2(msg,self.acs[icao]['nic_a'],self.nic_b,self.acs[icao]['nic_c']) self.nic = adsb.nic_v2(msg, self.acs[icao]['nic_a'], self.nic_b, self.acs[icao]['nic_c'])
if tc == 19: if tc == 19:
self.acs[icao]['nac_v'] = adsb.nac_v(msg) self.acs[icao]['nac_v'] = adsb.nac_v(msg)
if (20 <= tc <= 22): if (20 <= tc <= 22):
if self.acs[icao]['adsb_version'] == 1: if self.acs[icao]['adsb_version'] == 1:
if self.acs[icao]['nic_s'] != None: if self.acs[icao]['nic_s'] != None:
self.nic = adsb.nic_v1(msg,self.acs[icao]['nic_s']) self.nic = adsb.nic_v1(msg, self.acs[icao]['nic_s'])
elif self.acs[icao]['adsb_version'] == 2: elif self.acs[icao]['adsb_version'] == 2:
if self.acs[icao]['nic_a'] != None and self.acs[icao]['nic_b'] != None: if self.acs[icao]['nic_a'] != None and self.acs[icao]['nic_b'] != None:
self.nic = adsb.nic_v2(msg,self.acs[icao]['nic_a'],self.acs[icao]['nic_b'],self.acs[icao]['nic_c']) self.nic = adsb.nic_v2(msg, self.acs[icao]['nic_a'], self.acs[icao]['nic_b'], self.acs[icao]['nic_c'])
if tc == 29: if tc == 29:
if self.acs[icao]['adsb_version'] != None: if self.acs[icao]['adsb_version'] != None:
self.acs[icao]['sil'] = adsb.sil(msg,self.acs[icao]['adsb_version']) self.acs[icao]['sil'] = adsb.sil(msg, self.acs[icao]['adsb_version'])
self.acs[icao]['nac_p'] = adsb.nac_p(msg) self.acs[icao]['nac_p'] = adsb.nac_p(msg)
if tc == 31: if tc == 31:
self.acs[icao]['adsb_version'] = adsb.version(msg) self.acs[icao]['adsb_version'] = adsb.version(msg)
@ -144,10 +144,10 @@ class Stream():
if self.acs[icao]['adsb_version'] == 1: if self.acs[icao]['adsb_version'] == 1:
self.acs[icao]['nic_s'] = adsb.nic_s(msg) self.acs[icao]['nic_s'] = adsb.nic_s(msg)
elif self.acs[icao]['adsb_version'] == 2: elif self.acs[icao]['adsb_version'] == 2:
self.acs[icao]['nic_a'] , self.acs[icao]['nic_c'] = adsb.nic_a_and_c(msg) self.acs[icao]['nic_a'], self.acs[icao]['nic_c'] = adsb.nic_a_c(msg)
# process ehs message # process ehs message
for t, msg in zip(ehs_ts, ehs_msgs): for t, msg in zip(ehs_ts, ehs_msgs):
@ -213,4 +213,4 @@ class Stream():
def reset_new_aircraft(self): def reset_new_aircraft(self):
"""reset the updated icao buffer once been read""" """reset the updated icao buffer once been read"""
self.__new_acs = set() self.__new_acs = set()