Post · 5 min read· pp. 172–175

Interfacing to SPICE and JPL DE ephs

A pykep.planet is able to represent, with a unique interface, objects moving through space. and we here showcase the case of asteroids/comets and satellites whose motion is not integrated numerically, but rather fitted to observations, simulations or historical data and encapsulated into NAIF SPICE kernels.

Under the hoods, the SPICE C code made available by NAIF, is used and interfaced to the pykep relevant classes.

python
import pykep as pk
import numpy as np

Let us compute the position of the barycenter of the Jupiter system with respect to the Solar system barycenter. We do this using the JPL DE440 ephemerides which, released in 2022, are considered to be the most accurate ones.

First we must download the binary .bsp file that contains the corresponding data. In the case of the DE440 ephemerides pykep actually includes such a file in its distribution (for convenience). We may thus, for this case, skip this step and get the file path directly:

python
de440s_kernel = pk.udpla.de440s.kernel_file()
print(de440s_kernel)
myst-ansi
/home/runner/local/lib/python3.13/site-packages/pykep/data/de440s.bsp

If we have no idea on what objects are contained in this kernel, we may get their NAIF IDS using one of the utilities in pykep.

python
naifids = pk.utils.inspect_spice_kernel(de440s_kernel)

and convert into more readible names as follows:

python
names = [pk.naifid2name(item) for item in naifids]
print(names)
myst-ansi
['MERCURY BARYCENTER', 'VENUS BARYCENTER', 'EARTH BARYCENTER', 'MARS BARYCENTER', 'JUPITER BARYCENTER', 'SATURN BARYCENTER', 'URANUS BARYCENTER', 'NEPTUNE BARYCENTER', 'PLUTO BARYCENTER', 'SUN', 'MERCURY', 'VENUS', 'MOON', 'EARTH']

Nice!, inspecting this list we realize that most solar system bodies are contained in this kernel (their barycenters) as well as some of the non barycentric positions. Now, lets proceed in our task to compute the Jupyter barycenter position. The first thing to do (following the common usage of SPICE) is to pre-load in memory the kernel file:

python
pk.utils.load_spice_kernels(de440s_kernel)

This is done only once and we can now forget about it (unless memory is an issue and we want to unload the data, in which case we provide a corresponding unload_spice_kernels() function).

We now instantiate a planet form the UDPLA spice.

python
udpla = pk.udpla.spice("JUPITER BARYCENTER", "ECLIPJ2000", "SSB")
jupiter = pk.planet(udpla)
when = pk.epoch("2025-01-02")
r, v = jupiter.eph(when)
print(f"Position (m): {r}\nVelocity (m/s): {v}")
myst-ansi
Position (m): [156004760068.32562, 743270809056.2997, -6573215599.203884]
Velocity (m/s): [-12935.996273349603, 3306.509066262011, 275.7323039161468]

And inspect it:

python
print("name: ", jupiter)
print("extra info:\n",jupiter.extra_info)
myst-ansi
name:  JUPITER BARYCENTER - SPICE
extra info:
 Observer: SSB
Reference Frame: ECLIPJ2000

Note that physical parameters of the body are not defined (μ\mu, etc..), as for this particular UDPLA (the pykep.udpla.spice), the ephemerides computations are interpolated from tables and hence do not require any physical parameters. The user can always define them if needed.

Any SPICE provided ephemerides have a validity window. Querying the body eph method outside of such window will generate an error which can be annoying. Fortunately we can extract such window:

python
ep_s, ep_f = pk.utils.extract_coverage_window(de440s_kernel, udpla.naifid, 0)
print("Our Jupiter spice udpla only exists from: ", ep_s, " to: ", ep_f)
myst-ansi
Our Jupiter spice udpla only exists from:  1849-12-25T23:58:55.816095  to:  2150-01-21T23:58:55.816078

Note that pykep epoch system is a continuous time representation not UTC as it ignores leapseconds. The formatted epoch looks like a UTC but IT IS NOT. You can recover the actual UTC as follows:

python
pk.utils.epoch2utc(ep_s)
myst-ansi
'1849 DEC 25 23:59:18.816184'

The DE 440 JPL Ephemerides

What we have presented above is valid in general and works with any SPICE kernel, be it a comet, a rover a spacecraft. Clearly the backdraw is that we have to first download and position the correct kernel file and then load it into memory for it to work.

Since most of times, or anyway the most common usage pattern, is to query the position of the solar system planets, in pykep we offer a dedicated udpla for that, one that avoid loading kernels in memory. Lets start unloading the current kernel as to not create confusion.

python
pk.utils.unload_spice_kernels(de440s_kernel)

The UDPLA pykep.udpla.de440s automatically loads the correct kernel shipped with pykep module.

python
udpla = pk.udpla.de440s("JUPITER BARYCENTER", "ECLIPJ2000", "SSB")
jupiter = pk.planet(udpla)
when = pk.epoch("2025-01-02")
r, v = jupiter.eph(when)
print(f"Position (m): {r}\nVelocity (m/s): {v}")
myst-ansi
Position (m): [156004760068.32562, 743270809056.2997, -6573215599.203884]
Velocity (m/s): [-12935.996273349603, 3306.509066262011, 275.7323039161468]

What if we wanted a different reference frame? Well then we may either instantiate a different pykep.udpla.de440s with a different frame, and/or observer, or perform the transformation ourselves. If we want to perform the transformation ourselves, we need a rotation matrix, and we would then get it as follow:

python
R = pk.utils.rotation_matrix("ECLIPJ2000", "J2000")

Note that we have not specified an epoch, since these are both inertial frames and their relative orientation does not depend on time. We then perform the transformation.

python
r_j2000 = np.dot(R,r)
print(f"Position (m): {r_j2000}")
myst-ansi
Position (m): [  1.56004760e+11   6.84552310e+11   2.89625341e+11]

The same result would be obtained instantiating directly the pykep.planet in the final reference frame.

python
udpla = pk.udpla.de440s("JUPITER BARYCENTER", "J2000", "SSB")
jupiter_j2000 = pk.planet(udpla)
r, v = jupiter_j2000.eph(when)
print(f"Position (m): {r}")
myst-ansi
Position (m): [156004760068.32562, 684552309575.1816, 289625341111.2445]