From 02c5117de59d0ad97fa41f421fd77caa3c396a73 Mon Sep 17 00:00:00 2001 From: Junzi Sun Date: Tue, 29 Oct 2019 22:33:48 +0100 Subject: [PATCH] minor updates to C code --- pyModeS/c_decoder/adsb.pyx | 28 +++++++++++++++------------- pyModeS/c_decoder/bds/bds05.pyx | 6 +++--- pyModeS/c_decoder/bds/bds06.pyx | 6 +++--- pyModeS/c_decoder/common.pxd | 2 +- pyModeS/c_decoder/common.pyx | 18 +++++++++--------- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/pyModeS/c_decoder/adsb.pyx b/pyModeS/c_decoder/adsb.pyx index 3379f7c..d871add 100644 --- a/pyModeS/c_decoder/adsb.pyx +++ b/pyModeS/c_decoder/adsb.pyx @@ -17,8 +17,7 @@ The ADS-B wrapper also imports functions from the following modules: from libc.math cimport NAN as nan -# from pyModeS.decoder.bds import bds05, bds06, bds09 -from .common cimport typecode, icao as c_icao, df, hex2bin, bin2int, char_to_int +from . cimport common from .bds.bds05 import ( airborne_position, @@ -34,17 +33,20 @@ from .bds.bds08 import category, callsign from .bds.bds09 import airborne_velocity, altitude_diff def icao(bytes msg): - return c_icao(msg) + return common.icao(msg) -def position(bytes msg0 not None, bytes msg1 not None, int t0, int t1, double lat_ref=nan, double lon_ref=nan): +def typecode(bytes msg): + return common.typecode(msg) + +def position(bytes msg0 not None, bytes msg1 not None, double t0, double t1, double lat_ref=nan, double lon_ref=nan): """Decode position from a pair of even and odd position message (works with both airborne and surface position messages) Args: msg0 (string): even message (28 bytes hexadecimal string) msg1 (string): odd message (28 bytes hexadecimal string) - t0 (int): timestamps for the even message - t1 (int): timestamps for the odd message + t0 (double): timestamps for the even message + t1 (double): timestamps for the odd message Returns: (float, float): (latitude, longitude) of the aircraft @@ -122,12 +124,12 @@ def altitude(bytes msg): # surface position, altitude 0 return 0 - cdef bytearray msgbin = hex2bin(msg) - cdef int q = char_to_int(msgbin[47]) + cdef bytearray msgbin = common.hex2bin(msg) + cdef int q = common.char_to_int(msgbin[47]) cdef int n cdef double alt if q: - n = bin2int(msgbin[40:47] + msgbin[48:52]) + n = common.bin2int(msgbin[40:47] + msgbin[48:52]) alt = n * 25 - 1000 return alt else: @@ -194,8 +196,8 @@ def oe_flag(bytes msg): Returns: int: 0 or 1, for even or odd frame """ - cdef bytearray msgbin = hex2bin(msg.encode()) - return char_to_int(msgbin[53]) + cdef bytearray msgbin = common.hex2bin(msg) + return common.char_to_int(msgbin[53]) def version(bytes msg): @@ -214,7 +216,7 @@ def version(bytes msg): "%s: Not a status operation message, expecting TC = 31" % msg ) - cdef bytearray msgbin = hex2bin(msg) - cdef int version = bin2int(msgbin[72:75]) + cdef bytearray msgbin = common.hex2bin(msg) + cdef int version = common.bin2int(msgbin[72:75]) return version diff --git a/pyModeS/c_decoder/bds/bds05.pyx b/pyModeS/c_decoder/bds/bds05.pyx index ff19116..a211ac4 100644 --- a/pyModeS/c_decoder/bds/bds05.pyx +++ b/pyModeS/c_decoder/bds/bds05.pyx @@ -13,14 +13,14 @@ from libc.math cimport NAN as nan @cython.cdivision(True) -def airborne_position(bytes msg0 not None, bytes msg1 not None, long t0, long t1): +def airborne_position(bytes msg0 not None, bytes msg1 not None, double t0, double t1): """Decode airborn position from a pair of even and odd position message Args: msg0 (string): even message (28 bytes hexadecimal string) msg1 (string): odd message (28 bytes hexadecimal string) - t0 (int): timestamps for the even message - t1 (int): timestamps for the odd message + t0 (double): timestamps for the even message + t1 (double): timestamps for the odd message Returns: (float, float): (latitude, longitude) of the aircraft diff --git a/pyModeS/c_decoder/bds/bds06.pyx b/pyModeS/c_decoder/bds/bds06.pyx index a53d24f..b169763 100644 --- a/pyModeS/c_decoder/bds/bds06.pyx +++ b/pyModeS/c_decoder/bds/bds06.pyx @@ -16,15 +16,15 @@ import math @cython.cdivision(True) -def surface_position(bytes msg0 not None, bytes msg1 not None, long t0, long t1, double lat_ref, double lon_ref): +def surface_position(bytes msg0 not None, bytes msg1 not None, double t0, double t1, double lat_ref, double lon_ref): """Decode surface position from a pair of even and odd position message, the lat/lon of receiver must be provided to yield the correct solution. Args: msg0 (string): even message (28 bytes hexadecimal string) msg1 (string): odd message (28 bytes hexadecimal string) - t0 (int): timestamps for the even message - t1 (int): timestamps for the odd message + t0 (double): timestamps for the even message + t1 (double): timestamps for the odd message lat_ref (float): latitude of the receiver lon_ref (float): longitude of the receiver diff --git a/pyModeS/c_decoder/common.pxd b/pyModeS/c_decoder/common.pxd index 36e165c..59081fb 100644 --- a/pyModeS/c_decoder/common.pxd +++ b/pyModeS/c_decoder/common.pxd @@ -20,4 +20,4 @@ cpdef str idcode(bytes msg) cpdef int altcode(bytes msg) cdef bytes data(bytes msg) -cpdef bint allzeros(bytes msg) \ No newline at end of file +cpdef bint allzeros(bytes msg) diff --git a/pyModeS/c_decoder/common.pyx b/pyModeS/c_decoder/common.pyx index e1809e6..1928e5f 100644 --- a/pyModeS/c_decoder/common.pyx +++ b/pyModeS/c_decoder/common.pyx @@ -96,16 +96,16 @@ cpdef long crc(bytes msg, bint encode=False): # G = [int("11111111", 2), int("11111010", 2), int("00000100", 2), int("10000000", 2)] # cdef array.array _G = array.array('l', [0b11111111, 0b11111010, 0b00000100, 0b10000000]) cdef long[4] G = _G - + # msgbin_split = wrap(msgbin, 8) # mbytes = list(map(bin2int, msgbin_split)) cdef bytearray _msgbin = hex2bin(msg) cdef unsigned char[:] msgbin = _msgbin - + cdef Py_ssize_t len_msgbin = PyByteArray_GET_SIZE(_msgbin) cdef Py_ssize_t len_mbytes = len_msgbin // 8 cdef Py_ssize_t i - + if encode: for i in range(len_msgbin - 24, len_msgbin): msgbin[i] = 0 @@ -165,7 +165,7 @@ cpdef str icao(bytes msg): """ cdef unsigned char DF = df(msg) cdef long c0, c1 - + cdef bytearray bmsg = bytearray(msg) if DF in (11, 17, 18): @@ -207,7 +207,7 @@ cpdef bint is_icao_assigned(bytes icao): return False # future return True - + @cython.boundscheck(False) @cython.wraparound(False) cpdef int typecode(bytes msg): @@ -266,7 +266,7 @@ cpdef str idcode(bytes msg): cdef bytearray _mbin = hex2bin(msg) cdef unsigned char[:] mbin = _mbin - + cdef bytearray _idcode = bytearray(4) cdef unsigned char[:] idcode = _idcode @@ -283,17 +283,17 @@ cpdef str idcode(bytes msg): cdef unsigned char D2 = mbin[29] cdef unsigned char B4 = mbin[30] cdef unsigned char D4 = mbin[31] - + # byte1 = int(A4 + A2 + A1, 2) # byte2 = int(B4 + B2 + B1, 2) # byte3 = int(C4 + C2 + C1, 2) # byte4 = int(D4 + D2 + D1, 2) - + idcode[0] = int_to_char((char_to_int(A4)*2 + char_to_int(A2))*2 + char_to_int(A1)) idcode[1] = int_to_char((char_to_int(B4)*2 + char_to_int(B2))*2 + char_to_int(B1)) idcode[2] = int_to_char((char_to_int(C4)*2 + char_to_int(C2))*2 + char_to_int(C1)) idcode[3] = int_to_char((char_to_int(D4)*2 + char_to_int(D2))*2 + char_to_int(D1)) - + return _idcode.decode() #return str(byte1) + str(byte2) + str(byte3) + str(byte4)