############################################################
#  botched python version of Starlink's sla_DBEAR
# 
#  Bearing (position angle) of one point on a sphere relative to another
#
#  Given:
#     a1,b1    spherical coordinates of one point
#     a2,b2    spherical coordinates of the other point
#
#  Returns:    the bearing
############################################################

def bear(a1,b1,a2,b2):
    import math
    const = 0.017453293
    ra1 = a1*const
    rb1 = b1*const
    ra2 = a2*const
    rb2 = b2*const 
    da = ra2 - ra1
    y = math.sin(da)*math.cos(rb2)
    x = math.sin(rb2)*math.cos(rb1) - math.cos(rb2)*math.sin(rb1)*math.cos(da)
    if x != 0.0 or y != 0.0:
        direction = math.atan2(y,x)
    else:
        direction = 0.0
    if direction < 0.0: direction = direction + 2.*math.pi
    return direction/const


if __name__ == "__main__":
   print bear(0.0,0.0,0.0,0.0)
   print bear(0.0,0.0,0.0,10.0)
   print bear(0.0,0.0,10.0,0.0)
   print bear(0.0,0.0,0.0,-10.0)
   print bear(0.0,0.0,-10.0,0.0)
   print bear(0.0,0.0,-1.0,10.0)

