<commit_before><?php
class SBS {
    static $latlon = array();
    
    static function parse($buffer) {
	// Not yet finished, no CRC checks
	$hex = substr($buffer,1,-1);
	$bin = gmp_strval( gmp_init($hex,16), 2);
	//if (strlen($hex) == 28 && SBS::parityCheck(substr($bin,0,-24)) == substr($bin,-24)) {
	if (strlen($hex) == 28) {
	    $df = intval(substr($bin,0,5),2);
	    $ca = intval(substr($bin,5,3),2);
	    // Only support DF17 for now
	    //if ($df == 17 || ($df == 18 && ($ca == 0 || $ca == 1 || $ca == 6))) {
	    if ($df == 17) {
		$icao = substr($hex,2,6);
		$data['hex'] = $icao;
		$tc = intval(substr($bin,32,5),2);
		if ($tc >= 1 && $tc <= 4) {
		    //callsign
		    $csbin = substr($bin,40,56);
		    $charset = str_split('#ABCDEFGHIJKLMNOPQRSTUVWXYZ#####_###############0123456789######');
		    $cs = '';
		    $cs .= $charset[intval(substr($csbin,0,6),2)];
		    $cs .= $charset[intval(substr($csbin,6,6),2)];
		    $cs .= $charset[intval(substr($csbin,12,6),2)];
		    $cs .= $charset[intval(substr($csbin,18,6),2)];
		    $cs .= $charset[intval(substr($csbin,24,6),2)];
		    $cs .= $charset[intval(substr($csbin,30,6),2)];
		    $cs .= $charset[intval(substr($csbin,36,6),2)];
		    $cs .= $charset[intval(substr($csbin,42,6),2)];
		    $cs = str_replace('_','',$cs);
		    $cs = str_replace('#','',$cs);
		    $callsign = $cs;
		    $data['ident'] = $callsign;
		} elseif ($tc >= 9 && $tc <= 18) {
		    // Check Q-bit
		    $q = substr($bin,47,1);
		    if ($q) {
			$n = intval(substr($bin,40,7).substr($bin,48,4),2);
			$alt = $n*25-1000;
			$data['altitude'] = $alt;
		    }
		    // Check odd/even flag
		    $oe = substr($bin,53,1);
		    //if ($oe) => odd else even
		    //  131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits.
		    $cprlat = intval(substr($bin,54,17),2)/131072.0;
		    $cprlon = intval(substr($bin,71,17),2)/131072.0;
		    if ($oe == 0) self::$latlon[$icao] = array('latitude' => $cprlat,'longitude' => $cprlon,'created' => time());
		    elseif (isset(self::$latlon[$icao]) && (time() - self::$latlon[$icao]['created']) < 1000) {
			$cprlat_odd = $cprlat;
			$cprlon_odd = $cprlon;
			$cprlat_even = self::$latlon[$icao]['latitude'];
			$cprlon_even = self::$latlon[$icao]['longitude'];
		    
			$j = 59*$cprlat_even-60*$cprlat_odd+0.5;
			$lat_even = (360.0/60)*($j%60+$cprlat_even);
			$lat_odd = (360.0/59)*($j%59+$cprlat_odd);
			if ($lat_even >= 270) $lat_even = $lat_even - 360;
			if ($lat_odd >= 270) $lat_odd = $lat_odd - 360;
			// check latitude zone
			if (SBS::cprNL($lat_even) == SBS::cprNL($lat_odd)) {
			    if (self::$latlon[$icao]['created'] > time()) {
				$ni = SBS::cprN($lat_even,0);
				$m = floor($cprlon_even*(SBS::cprNL($lat_even)-1) - $cprlon_odd * SBS::cprNL($lat_even)+0.5);
				$lon = (360.0/$ni)*($m%$ni+$cprlon_even);
				$lat = $lat_even;<commit_msg>ADS-B Extended : Fix latitute/Longitude<commit_after><?php
class SBS {
    static $latlon = array();
    
    static function parse($buffer) {
	// Not yet finished, no CRC checks
	$hex = substr($buffer,1,-1);
	$bin = gmp_strval( gmp_init($hex,16), 2);
	//if (strlen($hex) == 28 && SBS::parityCheck(substr($bin,0,-24)) == substr($bin,-24)) {
	if (strlen($hex) == 28) {
	    $df = intval(substr($bin,0,5),2);
	    $ca = intval(substr($bin,5,3),2);
	    // Only support DF17 for now
	    //if ($df == 17 || ($df == 18 && ($ca == 0 || $ca == 1 || $ca == 6))) {
	    if ($df == 17) {
		$icao = substr($hex,2,6);
		$data['hex'] = $icao;
		$tc = intval(substr($bin,32,5),2);
		if ($tc >= 1 && $tc <= 4) {
		    //callsign
		    $csbin = substr($bin,40,56);
		    $charset = str_split('#ABCDEFGHIJKLMNOPQRSTUVWXYZ#####_###############0123456789######');
		    $cs = '';
		    $cs .= $charset[intval(substr($csbin,0,6),2)];
		    $cs .= $charset[intval(substr($csbin,6,6),2)];
		    $cs .= $charset[intval(substr($csbin,12,6),2)];
		    $cs .= $charset[intval(substr($csbin,18,6),2)];
		    $cs .= $charset[intval(substr($csbin,24,6),2)];
		    $cs .= $charset[intval(substr($csbin,30,6),2)];
		    $cs .= $charset[intval(substr($csbin,36,6),2)];
		    $cs .= $charset[intval(substr($csbin,42,6),2)];
		    $cs = str_replace('_','',$cs);
		    $cs = str_replace('#','',$cs);
		    $callsign = $cs;
		    $data['ident'] = $callsign;
		} elseif ($tc >= 9 && $tc <= 18) {
		    // Check Q-bit
		    $q = substr($bin,47,1);
		    if ($q) {
			$n = intval(substr($bin,40,7).substr($bin,48,4),2);
			$alt = $n*25-1000;
			$data['altitude'] = $alt;
		    }
		    // Check odd/even flag
		    $oe = substr($bin,53,1);
		    //if ($oe) => odd else even
		    //  131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits.
		    $cprlat = intval(substr($bin,54,17),2)/131072.0;
		    $cprlon = intval(substr($bin,71,17),2)/131072.0;
		    if ($oe == 0) self::$latlon[$icao] = array('latitude' => $cprlat,'longitude' => $cprlon,'created' => time());
		    elseif (isset(self::$latlon[$icao]) && (time() - self::$latlon[$icao]['created']) < 10) {
			$cprlat_odd = $cprlat;
			$cprlon_odd = $cprlon;
			$cprlat_even = self::$latlon[$icao]['latitude'];
			$cprlon_even = self::$latlon[$icao]['longitude'];
		    
			$j = 59*$cprlat_even-60*$cprlat_odd+0.5;
			$lat_even = (360.0/60)*($j%60+$cprlat_even);
			$lat_odd = (360.0/59)*($j%59+$cprlat_odd);
			if ($lat_even >= 270) $lat_even = $lat_even - 360;
			if ($lat_odd >= 270) $lat_odd = $lat_odd - 360;
			// check latitude zone
			if (SBS::cprNL($lat_even) == SBS::cprNL($lat_odd)) {
			    if (self::$latlon[$icao]['created'] > time()) {
				$ni = SBS::cprN($lat_even,0);
				$m = floor($cprlon_even*(SBS::cprNL($lat_even)-1) - $cprlon_odd * SBS::cprNL($lat_even)+0.5);
				$lon = (360.0/$ni)*($m%$ni+$cprlon_even);
				$lat = $lat_even;
{'fraction_non_alphanumeric': 0.25418118466898953, 'fraction_numerical': 0.06480836236933799, 'mean_word_length': 3.440061871616396, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6097597', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2376, 'n_words': 392}