<commit_before>#!/usr/bin/env python

import rospy
import math
from sensor_msgs.msg import NavSatFix
from std_msgs.msg import Float64

class MedianFilter:
    def __init__(self, size):
        self.size = size
        self.array = []

    def add(self, item):
        if len(self.array) > self.size:
            self.array.pop(0)
        self.array.append(item)

    def median(self):
        sortedArray = sorted(self.array)
        index = (len(self.array) - 1) // 2

        if len(self.array) % 2:
            return sortedArray[index]
        else:
            return (sortedArray[index] + sortedArray[index + 1]) / 2.0


# Global variables
last_fix = None
med_filter = MedianFilter(rospy.get_param('median_filter_size'))
bearing_pub = rospy.Publisher('bearing', Float64, queue_size=1)

MIN_FIX_DISTANCE = rospy.get_param('min_fix_distance')
EARTH_RADIUS = 6.3710088e6


def bearing(fix):
    """
    Adds the latest computed bearing to the meadian filter, publishes
    the current median bearing, and sets last_fix to the given fix
    if the given fix is >= MIN_FIX_DISTANCE from last_fix or there is
    currently no last_fix
    """

    if fix is not None:
        if last_fix is not None:
            distance = get_distance(last_fix, fix)
            bearing = get_forward_angle(last_fix, fix)
            med_filter.add(bearing)
            bearing_pub.publish(med_filter.median())
            if distance >= MIN_FIX_DISTANCE:
                last_fix = fix
        else:
            last_fix = fix

def get_distance(fix1, fix2):
    """
    Calculates great-circle distance between two positions in meters
    """

    lat1 = math.radians(fix1.latitude)
    lon1 = math.radians(fix1.longitude)
    lat2 = math.radians(fix2.latitude)
    lon2 = math.radians(fix2.longitude)

    a = (math.pow(math.sin(lat2 - lat1), 2)
         + math.cos(lat1) * math.cos(lat2)
         * math.pow(math.sin((lon2 - lon1) / 2), 2))
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    return EARTH_RADIUS * c

def get_forward_angle(fix1, fix2):
    """
    Calculates forward azimuth between two positions in radians
    """

    lat1 = math.radians(fix1.latitude)
    lon1 = math.radians(fix1.longitude)<commit_msg>Update global dict for clarity<commit_after>#!/usr/bin/env python

import rospy
import math
from sensor_msgs.msg import NavSatFix
from std_msgs.msg import Float64

class MedianFilter:
    def __init__(self, size):
        self.size = size
        self.array = []

    def add(self, item):
        if len(self.array) > self.size:
            self.array.pop(0)
        self.array.append(item)

    def median(self):
        sortedArray = sorted(self.array)
        index = (len(self.array) - 1) // 2

        if len(self.array) % 2:
            return sortedArray[index]
        else:
            return (sortedArray[index] + sortedArray[index + 1]) / 2.0


# Global variables
globalVars = {}
globalVars['last_fix'] = None
globalVars['med_filter'] = MedianFilter(rospy.get_param('median_filter_size'))
bearing_pub = rospy.Publisher('bearing', Float64, queue_size=1)

MIN_FIX_DISTANCE = rospy.get_param('min_fix_distance')
EARTH_RADIUS = 6.3710088e6


def bearing(fix):
    """
    Adds the latest computed bearing to the meadian filter, publishes
    the current median bearing, and sets last_fix to the given fix
    if the given fix is >= MIN_FIX_DISTANCE from last_fix or there is
    currently no last_fix
    """

    last_fix = globalVars['last_fix']
    med_filter = globalVars['med_filter']

    if fix is not None:
        if last_fix is not None:
            distance = get_distance(last_fix, fix)
            bearing = get_forward_angle(last_fix, fix)
            med_filter.add(bearing)
            bearing_pub.publish(med_filter.median())
            if distance >= MIN_FIX_DISTANCE:
                last_fix = fix
        else:
            last_fix = fix
    globalVars['last_fix'] = last_fix


def get_distance(fix1, fix2):
    """
    Calculates great-circle distance between two positions in meters
    """

    lat1 = math.radians(fix1.latitude)
    lon1 = math.radians(fix1.longitude)
    lat2 = math.radians(fix2.latitude)
    lon2 = math.radians(fix2.longitude)

    a = (math.pow(math.sin(lat2 - lat1), 2)
         + math.cos(lat1) * math.cos(lat2)
         * math.pow(math.sin((lon2 - lon1) / 2), 2))
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    return EARTH_RADIUS * c

def get_forward_angle(fix1, fix2):
    """
    Calculates forward azimuth between two positions in radians
    """

    lat1 = math.radians(fix1.latitude)
    lon1 = math.radians(fix1.longitude)
{'fraction_non_alphanumeric': 0.10694955618099156, 'fraction_numerical': 0.021216713574366747, 'mean_word_length': 2.657957244655582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3936713', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1525, 'n_words': 366}