skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.41k stars 211 forks source link

convert a direction vector in ICRF to RA and Dec? #86

Closed davidmikolas closed 8 years ago

davidmikolas commented 8 years ago

ditto: http://stackoverflow.com/questions/36740731/convert-a-direction-vector-to-r-a-and-dec-in-skyfield

Within Skyfield, if I have a vector (x, y, z) from the center of the earth, how can I back-convert it to a point on the "Celestial Sphere" (i.e. Right Ascension and Declination)?

For example purposes, suppose I have a few points in the orbit of a telescope around the earth, and I want to calculate the exact RA and Dec of the direction normal to the orbit - for whatever reason.

Effects like aberration and gravitation can be neglected here - I'm just looking to transform a direction in ICRF to RA and Dec.

In pseudocode:

from skyfield.api import load
import numpy as np

eph = load('de421.bsp')
ts  = load.timescale()
now = ts.now()

vec     = np.array([3141, 2718, 5820], dtype=float)
nvec = vec / np.sqrt((vec**2).sum())  # normalize for the heck of it

earth = eph['earth']
evec  = earth.at(now).vector(vec)   # pseudocode

print "It's pointing toward: ", evec.radec()   # pseudocode
brandon-rhodes commented 8 years ago

I answered the question on Stack Overflow! Let's keep this issue open for a few days, though, to remind me to also add this example to the Skyfield documentation, so that we can start centralizing in one place the knowledge that otherwise gets scattered everywhere. Let me know if the code works!

davidmikolas commented 8 years ago

Worked and seemed to be ok but I'll use it more frequently soon. I'm not set up for rigorous testing, but I'll give it a workout! Thanks for the speedy reply!