Previous post
After I learned how to make a Mac widget with TerminalWidget and how to determine the locations of celestial objects with Astropy, the natural thing for me to do was combine the two into a widget that tracks the planets.
I’m using the ancient definition of planet, which includes the Sun and Moon but not anything past Saturn. The numbers are the azimuth and altitude, in that order, and are given to the nearest degree. The idea is to tell me what may be visible and where it is. This particular screenshot was taken just after 10:00 last night; there was no reason to go outside because everything was below the horizon.
I was torn on whether to include the Sun. Few of us need help finding the Sun in the sky, and you can’t see anything other than the Moon when the Sun is up. But I decided to include it anyway, partly for completeness, and partly because the visibility of some bodies depends on their separation from the Sun.
Let’s start with the code that generates the widget’s text. It’s a Python script called planets :
python: 1: import astropy.units as u 2: from astropy.time import Time 3: from astropy.coordinates import get_body, AltAz, EarthLocation 4: from subprocess import run 5: 6: def direction(az): 7: 'Return a string indication of the azimuth (given in degrees).' 8: 9: dirs = 'N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW'.split() 10: i = int(((az + 11.25) % 360) / 22.5) 11: return dirs[i] 12: 13: # Current time in UTC. 14: ut = Time.now() 15: 16: # Observation location. 17: home = EarthLocation(lat=41.81433*u.deg, lon=-88.07093*u.deg, height=208*u.m) 18: 19: # Bodies of interest. 20: planets = 'Moon Sun Mercury Venus Mars Jupiter Saturn'.split() 21: 22: # Current positions of all the bodies. 23: pos = {} 24: const = {} 25: for p in planets: 26: pos[p] = get_body(p, ut).transform_to(AltAz(obstime=ut, location=home)) 27: const[p] = pos[p].get_constellation() 28: 29: # Assemble the results. 30: output = [] 31: for p in planets: 32: output.append(f'{p:>8s}: {pos[p].az.value:3.0f} \ 33: {direction(pos[p].az.value):3s} {pos[p].alt.value:3.0f} {const[p]}') 34: 35: # Pipe the results through TerminalWidget. 36: tw = '/Applications/TerminalWidget.app/Contents/MacOS/TerminalWidget\ 37: --target planets --font Menlo --bg eeeeee --fg 000000 --text -'.split() 38: run(tw, input='
'.join(output).encode()) 39: 40: # print('
'.join(output))
There’s no shebang line because of how it gets called by launchd , which we’ll get to later.
After planets imports the necessary modules, Lines 6–11 define the direction function, which takes the azimuth and returns a string with the corresponding point of the compass. I have this because 223°, which is how Astropy reports the azimuth, doesn’t immediately say “southwest” to me. The function assumes a 16-point compass, like this one:
... continue reading