Learning Scientific Programming with Python (2nd edition)
P4.4.2: The haversine formula
Question P4.4.2
The Haversine formula gives the shortest (great-circle) distance, $d$, between two points on a sphere of radius $R$ from their longitudes $(\lambda_1, \lambda_2)$ and latitudes $(\phi_1, \phi_2)$: $$ d = 2R \arcsin\left( \sqrt{\mathrm{haversin}(\phi_2-\phi_1) + \cos\phi_1\cos\phi_2\mathrm{haversin}(\lambda_2-\lambda_1)} \right), $$ where the haversine function of an angle is defined by $$ \mathrm{haversin}(\alpha) = \sin^2\left(\frac{\alpha}{2}\right). $$
Write a program to calculate the shortest distance in km between two points on the surface of the Earth (considered as a a sphere of radius 6378.1 km) given as two command line arguments, each of which is a comma-separated pair of latitude, longitude values in degrees. For example, the distance between Paris (48.9 °N, 2.4 °E)and Rome (41.9 °N, 12.5 °E) is given by executing:
$ python greatcircle.py 48.9,2.4 41.9,12.5
1107 km