forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conjunctions.py
executable file
·404 lines (341 loc) · 14.1 KB
/
conjunctions.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
#! /usr/bin/env python
# Predict planetary visibility in the early evening (sunset to midnight),
# and upcoming conjunctions between two or more planets.
# Copyright 2014 Akkana Peck -- share and enjoy under the GPLv2 or later.
import ephem
import math
verbose = False
output_csv = True
# How low can a planet be at sunset or midnight before it's not interesting?
min_alt = 10. * math.pi / 180.
# How close do two bodies have to be to consider it a conjunction?
max_sep = 3.5 * math.pi / 180.
# How little percent illuminated do we need to consider something a crescent?
crescent_percent = 40
# Start and end times for seeing a crescent phase:
crescents = { "Mercury": [ None, None ], "Venus": [ None, None ] }
sun = ephem.Sun()
planets = [
ephem.Moon(),
ephem.Mercury(),
ephem.Venus(),
ephem.Mars(),
ephem.Jupiter(),
ephem.Saturn()
]
planets_up = {}
for planet in planets:
planets_up[planet.name] = None
def datestr(d):
tup = d.tuple()
return "%d/%d/%d" % (tup[0], tup[1], tup[2])
def sepstr(sep):
deg = float(sep) * 180. / math.pi
# if deg < .5:
# return "less than a half a degree (%.2f)" % deg
# if deg < 1.:
# return "less than a degree (%.2f)" % deg
return "%.1f deg" % deg
class ConjunctionPair:
'''A conjunction between a pair of objects'''
def __init__(self, b1, b2, date, sep):
self.bodies = [b1, b2]
self.date = date
self.sep = sep
def __repr__(self):
return "%s: %s and %s, sep %s" % (datestr(self.date), self.bodies[0],
self.bodies[1], sepstr(self.sep))
def __contains__(self, body):
return body in self.bodies
class Conjunction:
'''A collection of ConjunctionPairs which may encompass more
than two bodies and several days.
The list is not guaranteed to be in date (or any other) order.
'''
def __init__(self):
self.bodies = []
self.pairs = []
def __contains__(self, body):
return body in self.bodies
def add(self, body1, body2, date, sep):
self.pairs.append(ConjunctionPair(body1, body2, date, sep))
if body1 not in self.bodies:
self.bodies.append(body1)
if body2 not in self.bodies:
self.bodies.append(body2)
def start_date(self):
date = ephem.date('3000/1/1')
for pair in self.pairs:
if pair.date < date:
date = pair.date
return date
def end_date(self):
date = ephem.date('0001/1/1')
for pair in self.pairs:
if pair.date > date:
date = pair.date
return date
def find_min_seps(self):
return mindate, maxdate, minseps
def andjoin(self, names):
'''Join a list together like a, b, c and d'''
if len(names) == 1:
return names[0]
elif len(names) < 4:
return ', '.join(names[:-1]) + ' and ' + names[-1]
def closeout(self):
'''Time to figure out what we have and print it.'''
# Find the list of minimum separations between each pair.
startdate = ephem.date('3000/1/1')
enddate = ephem.date('0001/1/1')
minseps = []
for i, b1 in enumerate(self.bodies):
for b2 in self.bodies[i+1:]:
minsep = 360 # degrees
closest_date = None
for pair in self.pairs:
if pair.date < startdate:
startdate = pair.date
if pair.date > enddate:
enddate = pair.date
if b1 in pair and b2 in pair:
if pair.sep < minsep:
minsep = pair.sep
closest_date = pair.date
# Not all pairs will be represented. In a triple conjunction,
# the two outer bodies may never get close enough to register
# as a conjunction in their own right.
if minsep < max_sep:
minseps.append((closest_date, minsep, b1, b2))
minseps.sort()
if output_csv:
s = '"Conjunction of ' + self.andjoin(self.bodies) + '",'
s += datestr(startdate) + "," + datestr(enddate) + ",,"
s += "\""
for m in minseps:
s += " %s and %s will be closest on %s (%s)." % \
(m[2], m[3], datestr(m[0]), sepstr(m[1]))
s += "\",,http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Sachin_Nigam_-_starry_moon_%28by-sa%29.jpg/320px-Sachin_Nigam_-_starry_moon_%28by-sa%29.jpg,240,169,\"<a href='http://commons.wikimedia.org/wiki/File:Sachin_Nigam_-_starry_moon_%28by-sa%29.jpg'>starry moon on Wikimedia Commons</a>\""
print s
else:
print "Conjunction of", self.andjoin(self.bodies),
print "lasts from %s to %s." % (datestr(startdate), datestr(enddate))
for m in minseps:
print " %s and %s are closest on %s (%s)." % \
(m[2], m[3], datestr(m[0]), sepstr(m[1]))
def merge(self, conj):
'''Merge in another Conjunction -- it must be that the two
sets of pairs have bodies in common.
'''
for p in conj.pairs:
self.pairs.append(p)
for body in conj.bodies:
if body not in self.bodies:
self.bodies.append(body)
class ConjunctionList:
'''A collection of Conjunctions -- no bodies should be shared
between any of the conjunctions we contain.
'''
def __init__(self):
self.clist = []
def add(self, b1, b2, date, sep):
for i, c in enumerate(self.clist):
if b1 in c or b2 in c:
c.add(b1, b2, date, sep)
# But what if one of the bodies is already in one of our
# other Conjunctions? In that case, we have to merge.
for cc in self.clist[i+1:]:
if b1 in cc or b2 in cc:
c.merge(cc)
self.clist.delete(cc)
return
# It's new, so just add it
c = Conjunction()
c.add(b1, b2, date, sep)
self.clist.append(c)
def closeout(self):
'''When we have a day with no conjunctions, check the list
and close out any pending conjunctions.
'''
for c in self.clist:
c.closeout()
self.clist = []
oneday = ephem.hour * 24
web_image = {
"Moon" : ("http://upload.wikimedia.org/wikipedia/commons/thumb/5/54/Phase-088.jpg/240px-Phase-088.jpg", '''"<a href='http://commons.wikimedia.org/wiki/User:JayTanner/gallery'>Jay Tanner</a>"''', 240, 240),
"Mercury" : ("../resources/astronomy/mercury.jpg", "", 240, 182),
"Venus" : ("../resources/astronomy/venus.jpg", "", 240, 192),
"Mars" : ("http://imgsrc.hubblesite.org/hu/db/images/hs-2001-24-a-small_web.jpg,200,200", "Hubble Space Telescope", 200, 200),
"Jupiter" : ("http://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Jupiter.jpg/240px-Jupiter.jpg", '"USGS, JPL and NASA"', 240, 240),
"Saturn" : ("http://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/Saturn_%28planet%29_large.jpg/384px-Saturn_%28planet%29_large.jpg", "Voyager 2", 192, 240)
}
descriptions = {
"Mars": "Mars is visible as a bright, reddish \"star\".",
"Saturn": "Saturn is visible. A small telescope will show its rings.",
"Jupiter": "Jupiter is visible. With binoculars you can see its four brightest moons."
}
def quotecsv(s):
if ',' in s or '"' in s:
return '"' + s.replace('"', '""') + '"'
return s
def finish_planet(p, d):
if not planets_up[p]:
return
if p in descriptions.keys():
if output_csv:
isvis = quotecsv(descriptions[p])
else:
isvis = descriptions[p]
elif p == "Venus" or p == "Mercury":
isvis = p + " is visible in the early evening sky."
else:
isvis = p + " is visible."
# How about crescent info?
if p in crescents.keys():
if crescents[p][0]:
isvis += " A telescope will show a crescent from " \
+ datestr(crescents[p][0])
if crescents[p][1]:
isvis += " to " + datestr(crescents[p][1])
isvis += '.'
crescents[p] = [ None, None ]
if output_csv:
if p != 'Moon':
if web_image[p]:
img = web_image[p][0]
cred = web_image[p][1]
w = web_image[p][2]
h = web_image[p][3]
else:
img = ""
cred = ""
w = ""
h = ""
print "%s,%s,%s,,%s,,%s,%s,%s,%s" % \
(p, datestr(planets_up[p]), datestr(d), isvis,
img, w, h, cred)
else:
print datestr(planets_up[p]), "to", datestr(d), ":", isvis
planets_up[p] = None
def run(start, end, observer, toolate):
'''Find planetary visibility between dates start and end,
for an observer whose location has been set,
between sunset and "toolate" on each date, where toolate is a GMT hour,
e.g. toolate=7 means we'll stop at 0700 GMT or midnight MDT.
'''
d = start
conjunctions = ConjunctionList()
if output_csv:
print 'name,start,end,time,longname,URL,image,image width,image height,image credit'
else:
print "Looking for planetary events between %s and %s:\n" % \
(datestr(d), datestr(end))
def check_if_planet_up(planet, d):
'''If the planet is currently up, do housekeeping to remember
that status, then return True if it's up, False otherwise.
'''
global crescents, planets_up
if planet.alt < min_alt: # planet is not up
return False
if not planets_up[planet.name]:
planets_up[planet.name] = d;
visible_planets.append(planet)
if planet.name not in crescents.keys():
return True
# Is it a crescent? Update its crescent dates.
if planet.phase <= crescent_percent: # It's a crescent now
if not crescents[planet.name][0]:
crescents[planet.name][0] = d
else:
crescents[planet.name][1] = d
return True
while d < end:
observer.date = d
sunset = observer.previous_setting(sun)
# sunrise = observer.next_rising(sun)
# print "Sunset:", sunset, " Sunrise:", sunrise
midnight = list(observer.date.tuple())
midnight[3:6] = [toolate, 0, 0]
midnight = ephem.date(tuple(midnight))
# We have two lists of planets: planets_up and visible_planets.
# planets_up is a dictionary of the time we first saw each planet
# in its current apparition. It's global, and used by finish_planet.
# visible_planets is a list of planets currently visible.
visible_planets = []
for planet in planets:
# A planet is observable this evening (not morning)
# if its altitude at sunset OR its altitude at midnight
# is greater than a threshold, which we'll set at 10 degrees.
observer.date = sunset
planet.compute(observer)
# print planet.name, "alt at sunset:", planet.alt
if not check_if_planet_up(planet, observer.date):
# If it's not up at sunset, try midnight
observer.date = midnight
if observer.date < sunset:
observer.date += oneday
planet.compute(observer)
if not check_if_planet_up(planet, observer.date):
# Planet is not up. Was it up yesterday?
if planets_up[planet.name]:
finish_planet(planet.name, observer.date)
# print datestr(d), "visible planets:", \
# ' '.join([p.name for p in visible_planets])
# print "planets_up:", planets_up
# Done with computing visible_planets.
# Now look for conjunctions, anything closer than 5 degrees.
# Split the difference, use a time halfway between sunset and midnight.
saw_conjunction = False
observer.date = ephem.date((sunset + midnight)/2)
if len(visible_planets) > 1:
for p, planet in enumerate(visible_planets):
for planet2 in visible_planets[p+1:]:
sep = ephem.separation(planet, planet2)
if sep <= max_sep:
# print datestr(observer.date), planet.name, \
# planet2.name, sepstr(sep)
conjunctions.add(planet.name, planet2.name,
observer.date, sep)
saw_conjunction = True
if not saw_conjunction:
conjunctions.closeout()
# Add a day:
d = ephem.date(d + oneday)
for p in visible_planets:
finish_planet(p.name, d)
if __name__ == '__main__':
import sys
if len(sys.argv) > 1 and sys.argv[1] == "-c":
output_csv = True
sys.argv = sys.argv[1:]
else:
output_csv = False
if len(sys.argv) > 1:
start = ephem.date(sys.argv[1])
else:
start = ephem.date('2014/8/15 04:00')
if len(sys.argv) > 2:
end = ephem.date(sys.argv[2])
else:
end = ephem.date('2017/1/1')
# Loop from start date to end date,
# using a time of 10pm MST, which is 4am GMT the following day.
# end = ephem.date('2016/1/1')
# For testing, this spans a Mars/Moon/Venus conjunction:
# d = ephem.date('2015/2/10 04:00')
# end = ephem.date('2015/3/10')
observer = ephem.Observer()
observer.name = "Los Alamos"
observer.lon = '-106.2978'
observer.lat = '35.8911'
observer.elevation = 2286 # meters, though the docs don't actually say
# What hour GMT corresponds to midnight here?
# Note: we're not smart about time zones. This will calculate
# a time based on the time zone offset right now, whether we're
# currently in DST or not.
# And for now we don't even calculate it, just hardwire it.
midnight = 7
try:
run(start, end, observer, midnight)
except KeyboardInterrupt:
print "Interrupted"