'''
Created on Sep 3, 2014

@author: ola
'''
import math

def distance_calc(lat1, long1, lat2, long2):
    """
    source: http://www.johndcook.com/python_longitude_latitude.html, in the public domain
    """

    # Convert latitude and longitude to 
    # spherical coordinates in radians.
    degrees_to_radians = math.pi/180.0
        
    # phi = 90 - latitude
    phi1 = (90.0 - lat1)*degrees_to_radians
    phi2 = (90.0 - lat2)*degrees_to_radians
        
    # theta = longitude
    theta1 = long1*degrees_to_radians
    theta2 = long2*degrees_to_radians
        
    # Compute spherical distance from spherical coordinates.
        
    # For two locations in spherical coordinates 
    # (1, theta, phi) and (1, theta, phi)
    # cosine( arc length ) = 
    #    sin phi sin phi' cos(theta-theta') + cos phi cos phi'
    # distance = rho * arc length
    
    cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) + 
           math.cos(phi1)*math.cos(phi2))
    arc = math.acos( cos )

    # Remember to multiply arc by the radius of the earth 
    # in your favorite set of units to get length.
    return arc


def readfile():
    durhamlat = 36.005493
    durhamlong = -78.914761
    f = open("data.csv")
    fardist = 0
    farlat = 0
    farlong = 0
    
    for line in f:
        data = line.split(",")
        dlati = float(data[1])
        dlong = float(data[2].strip())
        dist = distance_calc(dlati,dlong,durhamlat,durhamlong)*3960
        if dist > 10000:
            print "far error on",dlati,dlong
        if dist < 0:
            print "zero error on",dlati,dlong
        if dist > fardist:
            fardist = dist
            farlat = dlati
            farlong = dlong
        print dlati,dlong,dist
        
    print fardist,farlat,farlong

if __name__ == '__main__':
    readfile()