Take the 2-minute tour ×

For a given observer (lon, lat, time) on Earth and a given Galactic coordinate (GLON, GLAT), how can I compute the corresponding (alt, az) point in the sky with PyEphem?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Given the way that PyEphem currently works, there are two steps to answering your question. First, you have to convert a pair of galactic coordinates into an equatorial RA/dec pair of coordinates instead.

import ephem

# Convert a Galactic coordinate to RA and dec                          

galactic_center = ephem.Galactic(0, 0)
eq = ephem.Equatorial(galactic_center)
print 'RA:', eq.ra, 'dec:', eq.dec

→ RA: 17:45:37.20 dec: -28:56:10.2

Those coordinates are pretty close to the ones that the Wikipedia gives for the galactic center.

Now that we have a normal RA/dec coordinate, we just need to figure out where it is in the sky right now. Since PyEphem is built atop a library that knows only about celestial “bodies” like stars and planets, we simply need to create a fake “star” at that location and ask its azimuth and altitude.

# So where is that RA and dec above Boston?
# Pretend that a star or other fixed body is there.

body = ephem.FixedBody()
body._ra = eq.ra
body._dec = eq.dec
body._epoch = eq.epoch

obs = ephem.city('Boston')
obs.date = '2012/6/24 02:00' # 10pm EDT
print 'Az:', body.az, 'Alt:', body.alt

→ Az: 149:07:25.6 Alt: 11:48:43.0

And we can check that this answer is reasonable by looking at a sky chart for Boston late that evening: Sagittarius — the location of the Galactic Center — is just rising over the southeastern rim of the sky, which makes perfect sense of a southeast azimuth like 149°, and for an as-yet low altitude like 11°.

share|improve this answer
The (RA,DEC) coordinate from PyEphem matches exactly the one I find with the HEASARC coordinate converter, maybe the Wikipedia/Galactic_Center position is for the black hole and not for (GLON,GLAT) = (0,0). –  Chris Jul 11 '12 at 12:47
You could change eq = ephem.Equatorial(galactic_center) to equatorial = ephem.Equatorial(galactic_center), then the code will run directly when pasted in a python shell. –  Chris Jul 11 '12 at 12:51
This solution is very nice and simply. The only weird thing is that you assign private attributes of body instead of setting the values in the constructor. –  Chris Jul 11 '12 at 12:57
I saw here that it is possible to give coordinates "in the usual python way" to the constructor like this body = ephem.FixedBody(ra=123.123, dec=45.45), but for some reason neiter body = ephem.FixedBody(ra=eq.ra, dec=eq.dec) nor body = ephem.FixedBody(ra=eq.ra.real, dec=eq.dec.real) works for me, in both cases I get body._ra == 0. Why is that? –  Chris Jul 11 '12 at 13:07
add comment

Your Answer


