def airborne_position(msg0, msg1, t0, 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 Returns: (float, float): (latitude, longitude) of the aircraft """ msgbin0 = util.hex2bin(msg0) msgbin1 = util.hex2bin(msg1) # 131072 is 2^17, since CPR lat and lon are 17 bits each. cprlat_even = util.bin2int(msgbin0[54:71]) / 131072.0 cprlon_even = util.bin2int(msgbin0[71:88]) / 131072.0 cprlat_odd = util.bin2int(msgbin1[54:71]) / 131072.0 cprlon_odd = util.bin2int(msgbin1[71:88]) / 131072.0 air_d_lat_even = 360.0 / 60 air_d_lat_odd = 360.0 / 59 # compute latitude index 'j' j = util.floor(59 * cprlat_even - 60 * cprlat_odd + 0.5) lat_even = float(air_d_lat_even * (j % 60 + cprlat_even)) lat_odd = float(air_d_lat_odd * (j % 59 + cprlat_odd)) if lat_even >= 270: lat_even = lat_even - 360 if lat_odd >= 270: lat_odd = lat_odd - 360 # check if both are in the same latidude zone, exit if not if _cprNL(lat_even) != _cprNL(lat_odd): return None # compute ni, longitude index m, and longitude if (t0 > t1): lat = lat_even nl = _cprNL(lat) ni = max(_cprNL(lat) - 0, 1) m = util.floor(cprlon_even * (nl - 1) - cprlon_odd * nl + 0.5) lon = (360.0 / ni) * (m % ni + cprlon_even) else: lat = lat_odd nl = _cprNL(lat) ni = max(_cprNL(lat) - 1, 1) m = util.floor(cprlon_even * (nl - 1) - cprlon_odd * nl + 0.5) lon = (360.0 / ni) * (m % ni + cprlon_odd) if lon > 180: lon = lon - 360 return round(lat, 5), round(lon, 5)
def plot_resource_usage(self, resource, sample_size=100): counter, usage = {}, self.__usage[resource] for h, recs in usage.items(): for timepoint, amt in recs: counter.setdefault(floor(timepoint, sample_size), {}).setdefault(h, []).append(amt) for timepoint, hosts in dict(counter).items(): counter[timepoint] = np.mean( [np.mean(vals) for vals in hosts.values()]) x = list(sorted(counter.keys())) y = [counter[k] for k in x] return x, y
def create_blocks(difficulty, complexity, seed): # Map constants height = 16 width = 9 difficulty = util.floor(difficulty) complexity = util.floor(complexity) grid = [] #Default to a blocked play area for i in range(height): row = [] for j in range(width): row.append("X") grid.append(row) top = ["X"] * width bottom = ["X"] * width top[width // 2] = "O" G = blocks(height, width, difficulty, complexity, seed) for i in range(height): for j in range(width): if G.is_path[j][i]: grid[i][j] = "o" if G.is_unused_path[j][i] else "O" elif G.is_wall[j][i]: grid[i][j] = "w" if G.is_unused_wall[j][i] else "W" else: grid[i][j] = "X" grid.insert(0, top) grid.append(bottom) for i in range(len(grid)): grid[i].insert(0, "X") grid[i].append("X") return grid
def surface_position_with_ref(msg, lat_ref, lon_ref): """Decode surface position with only one message, knowing reference nearby location, such as previously calculated location, ground station, or airport location, etc. The reference position shall be with in 45NM of the true position. Args: msg (string): even message (28 bytes hexadecimal string) lat_ref: previous known latitude lon_ref: previous known longitude Returns: (float, float): (latitude, longitude) of the aircraft """ i = oe_flag(msg) d_lat = 90.0 / 59 if i else 90.0 / 60 msgbin = util.hex2bin(msg) cprlat = util.bin2int(msgbin[54:71]) / 131072.0 cprlon = util.bin2int(msgbin[71:88]) / 131072.0 j = util.floor(lat_ref / d_lat) \ + util.floor(0.5 + ((lat_ref % d_lat) / d_lat) - cprlat) lat = d_lat * (j + cprlat) ni = _cprNL(lat) - i if ni > 0: d_lon = 90.0 / ni else: d_lon = 90.0 m = util.floor(lon_ref / d_lon) \ + util.floor(0.5 + ((lon_ref % d_lon) / d_lon) - cprlon) lon = d_lon * (m + cprlon) return round(lat, 5), round(lon, 5)
def plot_host_usage(self, sample_size=100): counter = {} for h, durations in self.__hosts.items(): for start, end in durations: start, end = floor(start, sample_size), ceil(end, sample_size) cur_end = min(start + sample_size, end) while cur_end < end: timerange = (cur_end - sample_size, cur_end) if h not in counter.get(timerange, set()): counter.setdefault(timerange, set()).add(h) cur_end += sample_size x = list(sorted(counter.keys())) y = [len(counter[k]) for k in x] return x, y
def _cprNL(lat): """NL() function in CPR decoding """ if lat == 0: return 59 if lat == 87 or lat == -87: return 2 if lat > 87 or lat < -87: return 1 nz = 15 a = 1 - math.cos(math.pi / (2 * nz)) b = math.cos(math.pi / 180.0 * abs(lat))**2 nl = 2 * math.pi / (math.acos(1 - a / b)) NL = util.floor(nl) return NL
def surface_position(msg0, msg1, t0, t1, lat_ref, 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 lat_ref (float): latitude of the receiver lon_ref (float): longitude of the receiver Returns: (float, float): (latitude, longitude) of the aircraft """ msgbin0 = util.hex2bin(msg0) msgbin1 = util.hex2bin(msg1) # 131072 is 2^17, since CPR lat and lon are 17 bits each. cprlat_even = util.bin2int(msgbin0[54:71]) / 131072.0 cprlon_even = util.bin2int(msgbin0[71:88]) / 131072.0 cprlat_odd = util.bin2int(msgbin1[54:71]) / 131072.0 cprlon_odd = util.bin2int(msgbin1[71:88]) / 131072.0 air_d_lat_even = 90.0 / 60 air_d_lat_odd = 90.0 / 59 # compute latitude index 'j' j = util.floor(59 * cprlat_even - 60 * cprlat_odd + 0.5) # solution for north hemisphere lat_even_n = float(air_d_lat_even * (j % 60 + cprlat_even)) lat_odd_n = float(air_d_lat_odd * (j % 59 + cprlat_odd)) # solution for north hemisphere lat_even_s = lat_even_n - 90.0 lat_odd_s = lat_odd_n - 90.0 # chose which solution corrispondes to receiver location lat_even = lat_even_n if lat_ref > 0 else lat_even_s lat_odd = lat_odd_n if lat_ref > 0 else lat_odd_s # check if both are in the same latidude zone, rare but possible if _cprNL(lat_even) != _cprNL(lat_odd): return None # compute ni, longitude index m, and longitude if (t0 > t1): lat = lat_even nl = _cprNL(lat_even) ni = max(_cprNL(lat_even) - 0, 1) m = util.floor(cprlon_even * (nl - 1) - cprlon_odd * nl + 0.5) lon = (90.0 / ni) * (m % ni + cprlon_even) else: lat = lat_odd nl = _cprNL(lat_odd) ni = max(_cprNL(lat_odd) - 1, 1) m = util.floor(cprlon_even * (nl - 1) - cprlon_odd * nl + 0.5) lon = (90.0 / ni) * (m % ni + cprlon_odd) # four possible longitude solutions lons = [lon, lon + 90.0, lon + 180.0, lon + 270.0] # the closest solution to receiver is the correct one dls = [abs(lon_ref - l) for l in lons] imin = min(range(4), key=dls.__getitem__) lon = lons[imin] return round(lat, 5), round(lon, 5)