-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro.py
469 lines (356 loc) · 16.5 KB
/
astro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
from collections import namedtuple
import getopt
from operator import *
from re import search
from data import *
from sweph import *
import utils as utl
# Returns the Nakshatra
def get_nakshatra(time, planet):
longitude = swe_calc_ut(time, planet, SEFLG_SWIEPH)[1][0]
return NAKSHATRAS[int(longitude/NAKSHATRADEGREE)].name
# Return date object for julian day
def rev_julian(julian_day_ut):
return swe_revjul(julian_day_ut, SE_GREG_CAL)
# Returns the SWEPH planet data
def get_planet(time, planet):
return swe_calc_ut(time, planet, SEFLG_SWIEPH)[1]
# BROKEN: Doesn't handle retrograde motion.
# Calculates transit time of two planets. Direction is true/false if searching forward/backward in time.
def get_transit(julian_day_ut, body1, body2, direction):
#print 'Julian Day', julian_day_ut
# Later on we'll get in trouble when testing for an overshoot if one of our planets passes 0d,
# so we just add 1024 to both rectascensions. We need to add this after each swe_calc_ut call.
sun = swe_calc_ut(julian_day_ut, body1, SEFLG_SWIEPH | SEFLG_SPEED)[1]
moon = swe_calc_ut(julian_day_ut, body2, SEFLG_SWIEPH | SEFLG_SPEED)[1]
sun_degree = sun[0] + 1024
moon_degree = moon[0] + 1024
speed = abs(sun[3] - moon[3])
#print 'Start, jday:', julian_day_ut, 'date:', swe_revjul(julian_day_ut, 1), ' pl1:', sun[0], ' pl2:', moon[0]#, 'pl1speed:', sun[3], 'pl2speed:', moon[3]
# Take a first guess at the transit point.
if moon_degree > sun_degree:
if moon[3] - sun[3] > 0:
distance = (360 + (sun_degree - moon_degree))
else:
distance = (moon_degree - sun_degree)
else:
if moon[3] - sun[3] > 0:
distance = (sun_degree - moon_degree)
else:
distance = (360 - (sun_degree - moon_degree))
#print 'distance {0}'.format(distance)
if not direction:
time = (distance - 360) / speed
else:
time = distance / speed
#print 'time {0}'.format(time)
#print 'time', time
julian_day_ut = julian_day_ut + time
sun = swe_calc_ut(julian_day_ut, body1, SEFLG_SWIEPH | SEFLG_SPEED)[1]
moon = swe_calc_ut(julian_day_ut, body2, SEFLG_SWIEPH | SEFLG_SPEED)[1]
sun_degree = sun[0] + 1024
moon_degree = moon[0] + 1024
speed = abs(sun[3] - moon[3])
#print '1st pass, jday:', julian_day_ut, 'date:', swe_revjul(julian_day_ut, 1), ' pl1:', sun[0], ' pl2:', moon[0], 'pl1speed:', sun[3], 'pl2speed:', moon[3]
# Loop passes, will exit when accuracy of 0.0001 is achieved, or over 100 cycles calculated without any result
loop = 0
patience = 6
while True:
# Here we can ignore the direction. We relied on the first pass to place us within 60d of the transit.
# Now we just find the smaller arc and determine the time needed to close it.
# The hierarchy of conditions is: largest degree > short arc > direction
if moon_degree > sun_degree:
if moon_degree - sun_degree < 60:
if sun[3] - moon[3] > 0:
time = (moon_degree - sun_degree) / speed
else:
time = (sun_degree - moon_degree) / speed
else:
if sun[3] - moon[3] > 0:
time = -(moon_degree - sun_degree) / speed
else:
time = ((sun_degree + 360) - moon_degree) / speed
else:
if sun_degree - moon_degree < 60:
if moon[3] - sun[3] > 0:
time = (sun_degree - moon_degree) / speed
else:
time = (moon_degree - sun_degree) / speed
else:
if moon[3] - sun[3] > 0:
time = (sun_degree - (moon_degree + 360)) / speed
else:
time = ((moon_degree + 360) - sun_degree) / speed
julian_day_ut = julian_day_ut + time
sun = swe_calc_ut(julian_day_ut, body1, SEFLG_SWIEPH | SEFLG_SPEED)[1]
moon = swe_calc_ut(julian_day_ut, body2, SEFLG_SWIEPH | SEFLG_SPEED)[1]
if abs(time) < 0.0000001:
break
loop = loop + 1
if loop == patience:
print 'A transit point was not found after', patience, 'iterations!'
sys.exit()
break
sun_degree = sun[0] + 1024
moon_degree = moon[0] + 1024
speed = abs(sun[3] - moon[3])
print 'Result jday:', julian_day_ut, 'date:', swe_revjul(julian_day_ut, 1), ' pl1:', sun[0], ' pl2:', moon[0] #, 'pl1speed:', sun[3], 'pl2speed:', moon[3]
#print '{4:6} jday: {0:14} date: {1:38} sun: {2:14} moon: {3:14}'.format(julian_day_ut, swe_revjul(julian_day_ut, 1), sun[0], moon[0], str(direction))
return julian_day_ut + time, sun[0], moon[0]
# Determines future transit of sun and moon if direction is True, past time if False. Returns time, moon degree and moon speed.
def get_sun_moon_transit(julian_day_ut, direction):
# Later on we'll get in trouble when testing for an overshoot if one of our planets passes 0d,
# so we just add 1024 to both rectascensions. We need to add this after each swe_calc_ut call.
sun = swe_calc_ut(julian_day_ut, SE_SUN, SEFLG_SWIEPH | SEFLG_SPEED)[1]
moon = swe_calc_ut(julian_day_ut, SE_MOON, SEFLG_SWIEPH | SEFLG_SPEED)[1]
sun_degree = sun[0] + 1024
moon_degree = moon[0] + 1024
speed = abs(sun[3] - moon[3])
transit_date = julian_day_ut
# Take a first guess at the transit point.
if moon_degree > sun_degree:
distance = (360 + (sun_degree - moon_degree))
else:
distance = (sun_degree - moon_degree)
if not direction:
time = (distance - 360) / speed
else:
time = distance / speed
# Loop passes, will exit when accuracy of 0.0001 is achieved, or over 100 cycles calculated without any result
loop = 0
patience = 6
while True:
# Here we can ignore the direction. We relied on the first pass to place us within 60d of the transit.
# Now we just find the smaller arc and determine the time needed to close it.
# The hierarchy of conditions is: largest degree > short arc > direction
transit_date += time
sun = swe_calc_ut(transit_date, SE_SUN, SEFLG_SWIEPH | SEFLG_SPEED)[1]
moon = swe_calc_ut(transit_date, SE_MOON, SEFLG_SWIEPH | SEFLG_SPEED)[1]
if abs(time) < 0.0000001:
break
sun_degree = sun[0] + 1024
moon_degree = moon[0] + 1024
speed = abs(sun[3] - moon[3])
if moon_degree > sun_degree:
if moon_degree - sun_degree < 60:
time = (sun_degree - moon_degree) / speed
else:
time = ((sun_degree + 360) - moon_degree) / speed
else:
if sun_degree - moon_degree < 60:
time = (sun_degree - moon_degree) / speed
else:
time = (sun_degree - (moon_degree + 360)) / speed
return transit_date, moon[0], moon[3]
# Returns the exact time and degree of full moon
def get_full_moon(julian_day_ut):
offset = 1.0
while True:
plus = swe_pheno_ut(julian_day_ut+offset, SE_MOON, SEFLG_SWIEPH)[1][0]
minus = swe_pheno_ut(julian_day_ut-offset, SE_MOON, SEFLG_SWIEPH)[1][0]
if plus > minus:
offset_range = (plus + minus) / 2
offset_degree = offset_range - minus
offset *= offset_degree / offset_range
julian_day_ut -= offset
else:
offset_range = (plus + minus) / 2
offset_degree = offset_range - plus
offset *= offset_degree / offset_range
julian_day_ut += offset
# Stop when we're less than a second off. The phase degree often doesn't reach zero, because of the ascension of the moon.
if offset < 0.00001:
break
return julian_day_ut, swe_calc_ut(julian_day_ut, SE_MOON, SEFLG_SWIEPH)[1][0]
# Return phase info [{waxing or waning}, {day integer 1-30}, {progress%}]
def get_phase(julian_day_ut, method = PHASE_METHOD_FULL_BREATH):
progression, tithi, percent_complete = '', 0, 0
new_moon_past = get_sun_moon_transit(julian_day_ut, False)
new_moon_future = get_sun_moon_transit(julian_day_ut, True)
synodic_duration = new_moon_future[0] - new_moon_past[0]
if new_moon_future[1] < new_moon_past[1]:
synodic_distance = new_moon_future[1] + (360 - new_moon_past[1]) + 360
else:
synodic_distance = (new_moon_future[1] - new_moon_past[1]) + 360
# Take a guess at full moon and get an accurate time.
full_moon = get_full_moon(new_moon_past[0] + (synodic_duration / 2))
# Just a container to break out of doing extra work
while True:
# 1st method
if method == PHASE_METHOD_FULL_BREATH:
phase_length = new_moon_future[0] - new_moon_past[0]
tithi_length = phase_length / 30.0
position = julian_day_ut - new_moon_past[0]
tithi = (position // tithi_length) + 1
percent_complete = (position % tithi_length) * 100
if julian_day_ut < full_moon[0]:
progression = 'waxing'
else:
progression = 'waning'
break
# 2nd method
if method == PHASE_METHOD_HALF_BREATH:
if julian_day_ut < full_moon[0]:
progression = 'waxing'
waxing_length = full_moon[0] - new_moon_past[0]
tithi_length = waxing_length / 15.0
waxing_position = julian_day_ut - new_moon_past[0]
tithi = (waxing_position // tithi_length) + 1
percent_complete = (waxing_position % tithi_length) * 100
else:
progression = 'waning'
waning_length = new_moon_future[0] - full_moon[0]
tithi_length = waning_length / 15.0
waning_position = julian_day_ut - full_moon[0]
tithi = (waning_position // tithi_length) + 16
percent_complete = (waning_position % tithi_length) * 100
break
moon = swe_calc_ut(julian_day_ut, SE_MOON, SEFLG_SWIEPH | SEFLG_SPEED)[1]
moon_ahead = moon[0]
# 3rd method
if method == PHASE_METHOD_CRITICAL:
tithi_degree = 0
if julian_day_ut < full_moon[0]:
progression = 'waxing'
if full_moon[1] < new_moon_past[1]:
waxing_range = (full_moon[1] + 360) - new_moon_past[1]
else:
waxing_range = full_moon[1] - new_moon_past[1]
tithi_length = waxing_range / 15.0
if moon[0] < new_moon_past[1]:
moon_ahead += 360
k = range(1,16)
for i in k:
if moon_ahead < new_moon_past[1] + (tithi_length * i):
tithi = i
tithi_degree = (moon_ahead - new_moon_past[1]) % tithi_length
break
else:
progression = 'waning'
if new_moon_future[1] < full_moon[1]:
waning_range = (new_moon_future[1] + 360) - full_moon[1]
else:
waning_range = new_moon_future[1] - full_moon[1]
tithi_length = waning_range / 15.0
if moon[0] < full_moon[1]:
moon_ahead += 360
k = range(1,16)
for i in k:
if moon_ahead < full_moon[1] + (tithi_length * i):
tithi = i + 15
tithi_degree = (moon_ahead - full_moon[1]) % tithi_length
break
percent_complete = (tithi_degree / tithi_length) * 100
break
# 4th method
elif method == PHASE_METHOD_NOBLE:
karana = 0
karana_length = 0
karana_degree = 0
if julian_day_ut < full_moon[0]:
progression = 'waxing'
if full_moon[1] < new_moon_past[1]:
waxing_range = (full_moon[1] + 360) - new_moon_past[1]
else:
waxing_range = full_moon[1] - new_moon_past[1]
karana_length = waxing_range / 30.0
if moon[0] < new_moon_past[1]:
moon_ahead += 360
k = range(1,31)
for i in k:
if moon_ahead < new_moon_past[1] + (karana_length * i):
karana = i
karana_degree = (moon_ahead - new_moon_past[1]) % karana_length
break
else:
progression = 'waning'
if new_moon_future[1] < full_moon[1]:
waning_range = (new_moon_future[1] + 360) - full_moon[1]
else:
waning_range = new_moon_future[1] - full_moon[1]
karana_length = waning_range / 30.0
if moon[0] < full_moon[1]:
moon_ahead += 360
k = range(1,31)
for i in k:
if moon_ahead < full_moon[1] + (karana_length * i):
karana = i + 30
karana_degree = (moon_ahead - full_moon[1]) % karana_length
break
if karana == 1 or karana == 60:
tithi = 30
else:
tithi = karana / 2
tithi_length = karana_length * 2
if karana % 2 == 1:
percent_complete = ((karana_degree + karana_length) / tithi_length) * 100
else:
percent_complete = (karana_degree / tithi_length) * 100
break
# Wrong method
else:
print 'Phase method not supported. Exiting...'
sys.exit(0)
return progression, int(tithi), int(percent_complete), PHASE_METHOD_NAMES[method]
# Return Mahadasa and Antardasa tuples, each with a name, rise time, and end time
# Do this for the given cycle at the current time, if none is specified
def get_dasa(cycle, derivative_time=utl.getJulian()):
mdName, mdRise, mdEnd, adName, adRise, adEnd = '', '', '', '', '', ''
while True:
for md in cycle:
if md.antardasas[8][1] > derivative_time:
mdName = md.mahadasa
mdRise = md.rise
mdEnd = md.antardasas[8][1]
adRise = md.rise
for ad in md.antardasas:
if ad[1] > derivative_time:
adName = ad[0]
adEnd = ad[1]
break
else:
adRise = ad[1]
break
# If the person was born more than 120 before derivative_time
if adEnd == '':
derivative_time -= 120 * SUN_YEAR
else:
break
return Dasa(mdName, mdRise, mdEnd), Dasa(adName, adRise, adEnd)
# Returns a 120 year dasa cycle as an array of 9 Mahadasa namedtuplets
def get_dasa_cycle(time):
degree = swe_calc_ut(time, SE_MOON, SEFLG_SWIEPH)[1][0]
rDASAS = DASAS.__class__(DASAS) # New instance of dasas so when we rotate we don't disorder the original
dasa_cycle = []
for nakshatra, _ in enumerate(NAKSHATRAS):
if nakshatra == int(degree / NAKSHATRADEGREE):
# Find the 1st Mahadasa and its rising time
rDASAS.rotate(-nakshatra)
mahadasa = rDASAS[0]
period_days = rDASAS[0].dasa * SUN_YEAR
nakshatra_degree = degree % NAKSHATRADEGREE
progress = (nakshatra_degree / NAKSHATRADEGREE)
mahadasa_rise = time - (progress * period_days)
# Now find Antardasas and create Mahadasa namedtuple
# Do this for each dasa to complete 120years
for i in range(9):
antardasas = []
dasa_cusp = mahadasa_rise
for antardasa in rDASAS:
antardasa_length = (antardasa.dasa / 120.0) * (mahadasa.dasa * SUN_YEAR)
antardasa_end = dasa_cusp + antardasa_length
dasa_cusp = antardasa_end
antardasas.append((antardasa.name, antardasa_end))
md = Mahadasa(mahadasa.name, mahadasa_rise, antardasas)
dasa_cycle.append(md)
# If we've covered 120yrs, exit, else find next mahadasa
if i == 8:
break
mahadasa_rise += rDASAS[0].dasa * SUN_YEAR
rDASAS.rotate(-1)
mahadasa = rDASAS[0]
break
return dasa_cycle