Search code examples
pyephem

Can pyephem look more than 360 degrees past a date


I want this code to output the date of every 30 degrees of a planets movement up until the date of today, but for some reason it stops at 360 degrees. Why is the code stopping here and not going through 390 degrees, 420, 450......etc.

from ephem import Venus, Date, degrees, newton
import datetime
v = Venus()
start_date = '2014/2/2 00:00'
v.compute(start_date)
Ls0 = v.hlon
print "VENUS",start_date, Ls0
print ""
arc = 0
while True:
    d = start_date
    today = datetime.date.today()
    arc = arc + degrees('30:00:00')
    if d == today:
        break

def Ls(date):
    v.compute(date)
    return degrees(v.hlon - Ls0).norm

def thirty_degrees(date):
    return Ls(date) - degrees(arc)

def find_thirty_degrees(start_date):
    start_date = Date(start_date)
    y0 = Ls(start_date)
    y1 = Ls(start_date + 1)
    rate = y1 - y0
    angle_to_go = degrees(degrees(arc) - y0).norm
    closer_date = start_date + angle_to_go / rate
    d = newton(thirty_degrees, closer_date, closer_date + 1)
    return Date(d)

d = find_thirty_degrees(start_date)
print d, Ls(d)

results are:

VENUS 2014/2/2 00:00 146:05:57.6

2014/2/20 11:27:19 30:00:00.0

2014/3/11 01:17:41 60:00:00.0

2014/3/29 18:50:49 90:00:00.0

2014/4/17 15:55:27 120:00:00.0

2014/5/6 15:00:08 150:00:00.0

2014/5/25 14:10:16 179:59:59.9

2014/6/13 12:02:34 210:00:00.0

2014/7/2 07:57:18 240:00:00.0

2014/7/21 01:36:05 270:00:00.0

2014/8/8 16:44:49 300:00:00.0

2014/8/27 05:27:54 330:00:00.0

2014/9/14 16:38:35 359:59:59.5


Solution

  • The reason is that there are only 360° in a circle, at which point you are back at the 0° point where you started your journey around the sky. You will have to start back at zero and search for a whole new set of moments when Venus is at 30°, 60°, and so forth.

    Note that it can be very difficult for a routine like newton() to operate around the point where 359° switches back to 0° because the transition is so abrupt. It can sometimes be helpful, when trying to detect the moment when something is 0°, to instead give newton() a function that really returns the value you want + 180°, and then ask newton() to find the 180° crossing.