Calculating RA/dec from JPL ephemeris data I'm using JPL planetary ephemerides to calculate the position of planets. Using DE405 ephemeris (using a parser provided by Project Pluto with all tests passing). I am fetching the position of Mars with the observer set as the Earth (geocentric).

My first assumption is that the result is a set of geocentric equatorial coordinates in rectangular form (x, y, z). This may be incorrect.

My goal is to calculate RA/Dec. My second assumption is that I should use a formula converting from Cartesian to Spherical:

$r =\sqrt(x^2+y^2+z^2)$
$ra =\arctan(y/x)$
$dec = \pi/2 - arccos(z/r)$

Using Julian day 2457134 (21st Apr 2015, noon UTC), and cross-checking the result with NASA's Horizons service which apparently uses the same ephemeris (DE405) gets me almost there but my results are a little off:

x 1.721427968227801 y 1.5802812301744067 z 0.6897426775298559

RA 02h50m12.51s Dec +16°26'41.37"

Horizons:

RA 02h50m11.52s Dec +16°26'36.5"

I'm not sure what I'm missing. Do I have to factor distance of the planet due to the time it takes for light to reach us? Am I not using the correct time? Are my assumptions wrong?
Read the fine print under the Horizons output when you select an OBSERVER table. It says (emphasis mine)


  R.A._(ICRF/J2000.0)_DEC =
     J2000.0 astrometric right ascension and declination of target center.
  Adjusted for light-time. Units: HMS (HH MM SS.ff) and DMS (DD MM SS.f)


You could do the same, but it will take some extra work. The Mars you see at some time $t$ was emitted from Mars several minutes earlier than that. You need to find the $\Delta t$ such that the distance between Mars at time $t-\Delta t$ and Earth at time $t$ is equal to the speed of light times $\Delta t$. One correction will get you very close, a second will get you even closer.

Another issue is time. JPL uses its own time scale, JPL ephemeris time (Teph). This is a relativistic time scale. It differs from Terrestrial Time (TT) by at most a few milliseconds, so for most purposes you can use TT in lieu of Teph in your homebrew ephemeris calculator. TT currently differs from UTC by 67.184 seconds, and beginning in July (there's a leap second at the end of June), it will differ from UTC by 68.184 seconds.



Or you could just live with it. If you are using your calculations for your backyard telescope, the handful of arc seconds of error that results from not accounting for the finite speed of light is small.