# How to convert ephemerides to cartesian coordinates?

I got the orbital elements of the Moon from this page but it doesn't use keplerian orbital elements (with time derivatives), for which I've already written a converter. I don't understand why but it seems that they are only used for planets.

So, now I need to write a converter (in C#) that converts the data of the ephemerides to cartesian coordinates (and both position and velocity).

• Both answers below are fantastic, but I'm wondering if you're asking about the "orbital elements" option of HORIZONS? In other words, the data you need to compute the moon's elliptical orbit around the Earth? Note that these change over time so a snapshot may not be very useful.
– user7073
Jun 5, 2017 at 16:19
• Yes, with the orbital elements that HORIZONS give, and a date specified by the user, I want to get an approximate position of the moon in reference to Earth. But how often does them change? for example, this data is accurate for some centuries, but they are just for planets. Jun 7, 2017 at 15:15
• I think you're looking for naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/conics_c.html (read the entire page and do follow the links). This converts orbital elements (osculating elements) to position and speed. The other CSPICE functions are useful too: naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/index.html as is the general guide: naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/index.html
– user7073
Jun 7, 2017 at 15:30

You do not need something that converts JPL Horizons ephemerides to cartesian coordinates because cartesian coordinates is the system in which the Horizons systems naturally works. Requesting Keplerian elements forces the Horizons system to use a conversion algorithm. That algorithm doesn't quite make sense for the Moon due to the fact that the Moon's mass is over 1% of the Earth's mass.

The easiest way to programmatically obtain cartesian coordinates of a natural solar system object is to use JPL's SPICE Toolkit, which is freely available.

• Sorry for the delay. How exactly does SPICE work? are they libraries that I have to reference in the simulator? does it need internet connection to get the cartesian coordinates? do you know about some working example? Jun 7, 2017 at 15:04

Some options:

1. Caveat, check the jpl-horizions tag.
2. @DavidHammen's answer is right. That's the gold. The source. The horses mouth.
3. PyEphem is a python package that gives you some python-wrapped access to many of those same features and math, and is easy to use.
4. You can also just choose to get the state vectors (rather than the orbital elements) directly from Horizons like this:

To get the basic position and velocity (state vector) without any light time corrections, make sure it looks like this:

To get units of kilometers and kilometers per second, and ecliptic coordinates, choose these:

But if you want Coordinates compatible with RA and dec, use Earth's Mean Equator (z axis points towards declination of +90 degrees):

OK, so for the moon's position and velocity in ecliptic coordinates with the solar system barycenter as the origin (a good place to start) and to download a csv directly to your computer, make it look like this (note that z is smaller than x or y):

And 04-Jun-2017 00:00 will be (JD, date, x, y, z, vx, vy, vz):

2457908.500000000, A.D. 2017-Jun-04 00:00:00.0000,
-4.345736121825012E+07, -1.447334164993358E+08,  8.959531602375209E+03,
2.821087875766465E+01, -9.599004577093003E+00,  6.380929713994465E-02,


If you chose a Geocentric origin, it will look like this (note x, y, z are all of order 1E+05):

And 04-Jun-2017 00:00 will be (JD, date, x, y, z, vx, vy, vz):

2457908.500000000, A.D. 2017-Jun-04 00:00:00.0000,
-3.875221956720588E+05, -8.324494849553939E+04,  2.466711375946739E+04,
1.660829018806102E-01, -9.711046752921442E-01,  6.419466109667427E-02,


And if you chose the Earth's equator for a reference plane solar system barycenter for origin, it will look like this (note that z is now large):

And 04-Jun-2017 00:00 will be (JD, date, x, y, z, vx, vy, vz):

2457908.500000000, A.D. 2017-Jun-04 00:00:00.0000,
-4.345736121825012E+07, -1.327938773171277E+08, -5.756342657388520E+07,
2.821087875766465E+01, -8.832296393941144E+00, -3.759720854934335E+00,

• @davidhammen I may not have used the correct vocabulary here, could you take a look? Thanks!
– uhoh
Jun 4, 2017 at 6:54
• Very complete answer! Jun 4, 2017 at 23:53
• Do you know if I can get the state vector through an API, so the simulator itself get the most recent data upon start? Jun 7, 2017 at 15:20
• @vistaero there is a way to access via the internet (telnet?) but I don' think that's what you're looking for. Spice is the gold standard (ti interpolates from a database you download once) and it's the same stuff that runs this web site and also inside Skyfield. Depending on the level of accuracy you need, there are various ephemerides out there, algorithms and excel sheets and formulae in books, but only you can tell what level of accuracy you need. You could consider adding some info about your application or needed accuracy to the question, or asking a new question.
– uhoh
Jun 7, 2017 at 15:27