<commit_before>"""
sphere: Tools for working with spherical distances.

Author: <NAME> <<EMAIL>>
"""

__author__ = "<NAME> <<EMAIL>>"
import math
import random
import numpy
import scipy.spatial,scipy.constants
from scipy.spatial.distance import euclidean
from math import pi,cos,sin,asin

__all__ = ['RADIUS_EARTH_KM', 'RADIUS_EARTH_MILES', 'arcdist', 'arcdist2linear', 'brute_knn', 'fast_knn', 'fast_threshold', 'linear2arcdist', 'toLngLat', 'toXYZ']


RADIUS_EARTH_KM = 6371.0 # Source: http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
RADIUS_EARTH_MILES = (RADIUS_EARTH_KM*scipy.constants.kilo) / scipy.constants.mile


def arcdist(pt0,pt1,radius = RADIUS_EARTH_KM):
    """
    Returns the arc distance between pt0 and pt1 using supplied radius
    pt0 and pt1 are assumed to be in the form (lng,lat)
    
    Examples
    >>> pt0 = (0,0)
    >>> pt1 = (180,0)
    >>> d = arcdist(pt0,pt1,RADIUS_EARTH_MILES)
    >>> d == math.pi*RADIUS_EARTH_MILES
    True
    """
    return linear2arcdist(euclidean(toXYZ(pt0),toXYZ(pt1)), radius)

def arcdist2linear(arc_dist,radius = RADIUS_EARTH_KM):
    """
    Convert an arc distance (spherical earth) to a linear distance (R3) in the unit sphere.

    Examples
    >>> pt0 = (0,0)
    >>> pt1 = (180,0)
    >>> d = arcdist(pt0,pt1,RADIUS_EARTH_MILES)
    >>> d == math.pi*RADIUS_EARTH_MILES
    True
    >>> arcdist2linear(d,RADIUS_EARTH_MILES)
    2.0
    """
    c = 2*math.pi*radius
    d = (2-(2*math.cos(math.radians((arc_dist*360.0)/c)))) ** (0.5)
    return d

def linear2arcdist(linear_dist, radius = RADIUS_EARTH_KM):
    """
    Convert a linear distance in the unit sphere (R3) to an arc distance based on supplied radius

    Examples
    >>> pt0 = (0,0)
    >>> pt1 = (180,0)
    >>> d = arcdist(pt0,pt1,RADIUS_EARTH_MILES)
    >>> d == linear2arcdist(2.0, radius = RADIUS_EARTH_MILES)
    True
    """
    if linear_dist > 2.0:
        raise ValueError, "linear_dist, must not exceed the diameter of the unit sphere, 2.0"
    c = 2*math.pi*radius
    a2 = linear_dist**2
    theta = math.degrees(math.acos((2-a2)/(2.)))
    d = (theta*c)/360.0
    return d
    
def toXYZ(pt):
    """ ASSUMPTION: pt = (lng,lat)
        REASON: pi = 180 degrees,
                theta+(pi/2)....
                theta = 90 degrees,
                180 =  90+180/2"""
    phi,theta = map(math.radians,pt)
    phi,theta = phi+pi,theta+(pi/2)
    x = 1*sin(theta)*cos(phi)
    y = 1*sin(theta)*sin(phi)
    z = 1*cos(theta)
    return x,y,z

def toLngLat(xyz):
    x,y,z = xyz
    if z == -1 or z == 1:
        phi = 0
    else:
        phi = math.atan2(y,x)
        if phi > 0:<commit_msg>Allowing linear2arcdist function to maintin 'inf', this allows compatability with Scipy's KDTree and addresses issue 208.<commit_after>"""
sphere: Tools for working with spherical distances.

Author: <NAME> <<EMAIL>>
"""

__author__ = "<NAME> <<EMAIL>>"
import math
import random
import numpy
import scipy.spatial,scipy.constants
from scipy.spatial.distance import euclidean
from math import pi,cos,sin,asin

__all__ = ['RADIUS_EARTH_KM', 'RADIUS_EARTH_MILES', 'arcdist', 'arcdist2linear', 'brute_knn', 'fast_knn', 'fast_threshold', 'linear2arcdist', 'toLngLat', 'toXYZ']


RADIUS_EARTH_KM = 6371.0 # Source: http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
RADIUS_EARTH_MILES = (RADIUS_EARTH_KM*scipy.constants.kilo) / scipy.constants.mile


def arcdist(pt0,pt1,radius = RADIUS_EARTH_KM):
    """
    Returns the arc distance between pt0 and pt1 using supplied radius
    pt0 and pt1 are assumed to be in the form (lng,lat)
    
    Examples
    >>> pt0 = (0,0)
    >>> pt1 = (180,0)
    >>> d = arcdist(pt0,pt1,RADIUS_EARTH_MILES)
    >>> d == math.pi*RADIUS_EARTH_MILES
    True
    """
    return linear2arcdist(euclidean(toXYZ(pt0),toXYZ(pt1)), radius)

def arcdist2linear(arc_dist,radius = RADIUS_EARTH_KM):
    """
    Convert an arc distance (spherical earth) to a linear distance (R3) in the unit sphere.

    Examples
    >>> pt0 = (0,0)
    >>> pt1 = (180,0)
    >>> d = arcdist(pt0,pt1,RADIUS_EARTH_MILES)
    >>> d == math.pi*RADIUS_EARTH_MILES
    True
    >>> arcdist2linear(d,RADIUS_EARTH_MILES)
    2.0
    """
    c = 2*math.pi*radius
    d = (2-(2*math.cos(math.radians((arc_dist*360.0)/c)))) ** (0.5)
    return d

def linear2arcdist(linear_dist, radius = RADIUS_EARTH_KM):
    """
    Convert a linear distance in the unit sphere (R3) to an arc distance based on supplied radius

    Examples
    >>> pt0 = (0,0)
    >>> pt1 = (180,0)
    >>> d = arcdist(pt0,pt1,RADIUS_EARTH_MILES)
    >>> d == linear2arcdist(2.0, radius = RADIUS_EARTH_MILES)
    True
    """
    if linear_dist == float('inf'):
        return float('inf')
    elif linear_dist > 2.0:
        raise ValueError, "linear_dist, must not exceed the diameter of the unit sphere, 2.0"
    c = 2*math.pi*radius
    a2 = linear_dist**2
    theta = math.degrees(math.acos((2-a2)/(2.)))
    d = (theta*c)/360.0
    return d
    
def toXYZ(pt):
    """ ASSUMPTION: pt = (lng,lat)
        REASON: pi = 180 degrees,
                theta+(pi/2)....
                theta = 90 degrees,
                180 =  90+180/2"""
    phi,theta = map(math.radians,pt)
    phi,theta = phi+pi,theta+(pi/2)
    x = 1*sin(theta)*cos(phi)
    y = 1*sin(theta)*sin(phi)
    z = 1*cos(theta)
    return x,y,z

def toLngLat(xyz):
    x,y,z = xyz
    if z == -1 or z == 1:
        phi = 0
    else:
        phi = math.atan2(y,x)
        if phi > 0:
{'fraction_non_alphanumeric': 0.14536799707067008, 'fraction_numerical': 0.03844745514463566, 'mean_word_length': 3.0617100371747212, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 97, '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': '3992712', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2192, 'n_words': 473}