deal with Python 2 long int

This commit is contained in:
Junzi Sun
2019-09-10 13:31:27 +02:00
parent 458b02028d
commit 0eb333ba8c
+74 -27
View File
@@ -2,6 +2,7 @@ from __future__ import absolute_import, print_function, division
import numpy as np import numpy as np
from textwrap import wrap from textwrap import wrap
def hex2bin(hexstr): def hex2bin(hexstr):
"""Convert a hexdecimal string to binary string, with zero fillings.""" """Convert a hexdecimal string to binary string, with zero fillings."""
num_of_bits = len(hexstr) * 4 num_of_bits = len(hexstr) * 4
@@ -16,7 +17,8 @@ def hex2int(hexstr):
def int2hex(n): def int2hex(n):
"""Convert a integer to hexadecimal string.""" """Convert a integer to hexadecimal string."""
return hex(n)[2:].rjust(6, '0').upper() # strip 'L' for python 2
return hex(n)[2:].rjust(6, "0").upper().rstrip("L")
def bin2int(binstr): def bin2int(binstr):
@@ -36,7 +38,7 @@ def bin2np(binstr):
def np2bin(npbin): def np2bin(npbin):
"""Convert a binary numpy array to string.""" """Convert a binary numpy array to string."""
return np.array2string(npbin, separator='')[1:-1] return np.array2string(npbin, separator="")[1:-1]
def df(msg): def df(msg):
@@ -60,8 +62,10 @@ def crc(msg, encode=False):
""" """
# the CRC generator # the CRC generator
G = [ G = [
int("11111111", 2), int("11111010", 2), int("11111111", 2),
int("00000100", 2), int("10000000", 2) int("11111010", 2),
int("00000100", 2),
int("10000000", 2),
] ]
if encode: if encode:
@@ -78,9 +82,15 @@ def crc(msg, encode=False):
if bits > 0: if bits > 0:
mbytes[ibyte] = mbytes[ibyte] ^ (G[0] >> ibit) mbytes[ibyte] = mbytes[ibyte] ^ (G[0] >> ibit)
mbytes[ibyte+1] = mbytes[ibyte+1] ^ (0xFF & ((G[0] << 8-ibit) | (G[1] >> ibit))) mbytes[ibyte + 1] = mbytes[ibyte + 1] ^ (
mbytes[ibyte+2] = mbytes[ibyte+2] ^ (0xFF & ((G[1] << 8-ibit) | (G[2] >> ibit))) 0xFF & ((G[0] << 8 - ibit) | (G[1] >> ibit))
mbytes[ibyte+3] = mbytes[ibyte+3] ^ (0xFF & ((G[2] << 8-ibit) | (G[3] >> ibit))) )
mbytes[ibyte + 2] = mbytes[ibyte + 2] ^ (
0xFF & ((G[1] << 8 - ibit) | (G[2] >> ibit))
)
mbytes[ibyte + 3] = mbytes[ibyte + 3] ^ (
0xFF & ((G[2] << 8 - ibit) | (G[3] >> ibit))
)
result = (mbytes[-3] << 16) | (mbytes[-2] << 8) | mbytes[-1] result = (mbytes[-3] << 16) | (mbytes[-2] << 8) | mbytes[-1]
@@ -90,7 +100,35 @@ def crc(msg, encode=False):
def crc_legacy(msg, encode=False): def crc_legacy(msg, encode=False):
"""Mode-S Cyclic Redundancy Check. (Legacy code, 2x slow).""" """Mode-S Cyclic Redundancy Check. (Legacy code, 2x slow)."""
# the polynominal generattor code for CRC [1111111111111010000001001] # the polynominal generattor code for CRC [1111111111111010000001001]
generator = np.array([1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,1,0,0,1]) generator = np.array(
[
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
0,
1,
0,
0,
0,
0,
0,
0,
1,
0,
0,
1,
]
)
ng = len(generator) ng = len(generator)
msgnpbin = bin2np(hex2bin(msg)) msgnpbin = bin2np(hex2bin(msg))
@@ -140,7 +178,7 @@ def icao(msg):
elif DF in (0, 4, 5, 16, 20, 21): elif DF in (0, 4, 5, 16, 20, 21):
c0 = crc(msg, encode=True) c0 = crc(msg, encode=True)
c1 = hex2int(msg[-6:]) c1 = hex2int(msg[-6:])
addr = '%06X' % (c0 ^ c1) addr = "%06X" % (c0 ^ c1)
else: else:
addr = None addr = None
@@ -154,18 +192,28 @@ def is_icao_assigned(icao):
icaoint = hex2int(icao) icaoint = hex2int(icao)
if 0x200000 < icaoint < 0x27FFFF: return False # AFI if 0x200000 < icaoint < 0x27FFFF:
if 0x280000 < icaoint < 0x28FFFF: return False # SAM return False # AFI
if 0x500000 < icaoint < 0x5FFFFF: return False # EUR, NAT if 0x280000 < icaoint < 0x28FFFF:
if 0x600000 < icaoint < 0x67FFFF: return False # MID return False # SAM
if 0x680000 < icaoint < 0x6F0000: return False # ASIA if 0x500000 < icaoint < 0x5FFFFF:
if 0x900000 < icaoint < 0x9FFFFF: return False # NAM, PAC return False # EUR, NAT
if 0xB00000 < icaoint < 0xBFFFFF: return False # CAR if 0x600000 < icaoint < 0x67FFFF:
if 0xD00000 < icaoint < 0xDFFFFF: return False # future return False # MID
if 0xF00000 < icaoint < 0xFFFFFF: return False # future if 0x680000 < icaoint < 0x6F0000:
return False # ASIA
if 0x900000 < icaoint < 0x9FFFFF:
return False # NAM, PAC
if 0xB00000 < icaoint < 0xBFFFFF:
return False # CAR
if 0xD00000 < icaoint < 0xDFFFFF:
return False # future
if 0xF00000 < icaoint < 0xFFFFFF:
return False # future
return True return True
def typecode(msg): def typecode(msg):
"""Type code of ADS-B message """Type code of ADS-B message
@@ -264,12 +312,11 @@ def altcode(msg):
mbit = mbin[25] # M bit: 26 mbit = mbin[25] # M bit: 26
qbit = mbin[27] # Q bit: 28 qbit = mbin[27] # Q bit: 28
if mbit == "0": # unit in ft
if mbit == '0': # unit in ft if qbit == "1": # 25ft interval
if qbit == '1': # 25ft interval
vbin = mbin[19:25] + mbin[26] + mbin[28:32] vbin = mbin[19:25] + mbin[26] + mbin[28:32]
alt = bin2int(vbin) * 25 - 1000 alt = bin2int(vbin) * 25 - 1000
if qbit == '0': # 100ft interval, above 50175ft if qbit == "0": # 100ft interval, above 50175ft
C1 = mbin[19] C1 = mbin[19]
A1 = mbin[20] A1 = mbin[20]
C2 = mbin[21] C2 = mbin[21]
@@ -287,7 +334,7 @@ def altcode(msg):
graystr = D2 + D4 + A1 + A2 + A4 + B1 + B2 + B4 + C1 + C2 + C4 graystr = D2 + D4 + A1 + A2 + A4 + B1 + B2 + B4 + C1 + C2 + C4
alt = gray2alt(graystr) alt = gray2alt(graystr)
if mbit == '1': # unit in meter if mbit == "1": # unit in meter
vbin = mbin[19:25] + mbin[26:31] vbin = mbin[19:25] + mbin[26:31]
alt = int(bin2int(vbin) * 3.28084) # convert to ft alt = int(bin2int(vbin) * 3.28084) # convert to ft
@@ -318,10 +365,10 @@ def gray2alt(codestr):
def gray2int(graystr): def gray2int(graystr):
"""Convert greycode to binary.""" """Convert greycode to binary."""
num = bin2int(graystr) num = bin2int(graystr)
num ^= (num >> 8) num ^= num >> 8
num ^= (num >> 4) num ^= num >> 4
num ^= (num >> 2) num ^= num >> 2
num ^= (num >> 1) num ^= num >> 1
return num return num