Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

JPL SPK Kernels

The built-in series are compact, but they miss JPL’s ephemeris by 2 mas to 3″ depending on the body, and they give only heliocentric positions. When you need better than that, or need the Earth’s velocity about the solar system barycenter for aberration, read a JPL kernel.

JPL’s Development Ephemerides, such as DE440 and DE432s, are distributed as SPK kernels. A kernel stores JPL’s numerical integration as Chebyshev polynomials. NAIF publishes them at https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/.

This example opens a kernel and gets the Moon’s position relative to the Earth:

use celestial_ephemeris::jpl::bodies;
use celestial_ephemeris::jpl::spk::SpkFile;
use celestial_time::scales::tdb::tdb_from_calendar;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let spk = SpkFile::open("de440.bsp")?;
    let tdb = tdb_from_calendar(2026, 10, 5, 0, 0, 0.0)?;

    let moon = spk.compute_position(bodies::MOON, bodies::EARTH, &tdb)?;
    let mars = spk.compute_position(bodies::MARS_BARYCENTER, bodies::EARTH, &tdb)?;

    println!("Moon: {:.3} km", moon.magnitude());
    println!("Mars: {:.3} km", mars.magnitude());
    Ok(())
}

SpkFile::open reads the whole file into memory and validates every segment it can evaluate. A truncated or damaged kernel fails at open rather than at the first lookup. Kernels in either byte order can be read.

Positions and velocities

compute_state(body, center, &tdb) returns the position of body relative to center in km and its velocity in km/s, as a (Vector3, Vector3). compute_position returns the position alone. Both use the kernel’s axes, which for the DE kernels are the ICRF.

These units differ from the built-in theories, which use AU and AU/day. This example converts the Earth’s barycentric state, the one to use for aberration:

use celestial_core::constants::{AU_KM, SECONDS_PER_DAY_F64};
use celestial_ephemeris::jpl::bodies;
use celestial_ephemeris::jpl::spk::SpkFile;
use celestial_time::scales::tdb::tdb_from_calendar;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let spk = SpkFile::open("de440.bsp")?;
    let tdb = tdb_from_calendar(2026, 10, 5, 0, 0, 0.0)?;

    let (position, velocity) =
        spk.compute_state(bodies::EARTH, bodies::SOLAR_SYSTEM_BARYCENTER, &tdb)?;
    let position = position / AU_KM;
    let velocity = velocity * (SECONDS_PER_DAY_F64 / AU_KM);

    println!("Earth: {:.9} AU from the barycenter", position.magnitude());
    println!("Speed: {:.9} AU/day", velocity.magnitude());
    Ok(())
}

How lookups chain through centers

A kernel stores each body relative to one center. In the DE kernels:

  • The planetary barycenters (1 to 9) and the Sun (10) are relative to the solar system barycenter (0).
  • The Moon (301) and the Earth (399) are relative to the Earth-Moon barycenter (3).
  • Mercury (199) and Venus (299) are relative to their own barycenters (1 and 2).

The kernel has no segment for the Moon relative to the Earth, yet the first example asks for it. compute_state and compute_position relate any two bodies the kernel connects by walking each one’s chain of centers until the chains meet. The Moon relative to the Earth is the Moon’s offset from the Earth-Moon barycenter minus the Earth’s offset from it.

At each step the lookup follows the last segment in the file that covers the epoch, as SPICE does, so later segments override earlier ones. A chain can have at most 32 links.

Body codes

celestial_ephemeris::jpl::bodies names the NAIF codes the DE kernels use:

ConstantCode
SOLAR_SYSTEM_BARYCENTER0
MERCURY_BARYCENTER1
VENUS_BARYCENTER2
EARTH_MOON_BARYCENTER3
MARS_BARYCENTER4
JUPITER_BARYCENTER5
SATURN_BARYCENTER6
URANUS_BARYCENTER7
NEPTUNE_BARYCENTER8
PLUTO_BARYCENTER9
SUN10
MERCURY199
VENUS299
MOON301
EARTH399

The lookup methods take plain i32 codes, so a code outside this list works too if the kernel has it.

List a kernel’s segments

segments() returns every segment in the file, including ones this crate can’t evaluate. Use it to check what a kernel covers before you query it:

use celestial_ephemeris::jpl::spk::SpkFile;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let spk = SpkFile::open("de440.bsp")?;
    for segment in spk.segments() {
        println!(
            "{:>3} relative to {:>2}: type {}, frame {}, JD {} to {}",
            segment.body(),
            segment.center(),
            segment.data_type(),
            segment.frame(),
            segment.start().to_julian_date().to_f64(),
            segment.end().to_julian_date().to_f64(),
        );
    }
    Ok(())
}

start() and end() return the segment’s coverage as TDB.

Supported segments

Only type 2 segments in frame 1 can be evaluated. Type 2 stores Chebyshev polynomials for position, and the velocity is their derivative. Frame 1 is the one SPICE calls J2000, which the DE kernels use for the ICRF. Every DE planetary kernel is made of these.

A lookup whose chain passes through a segment of another type or frame returns UnsupportedType or UnsupportedFrame.

Errors

Every fallible method returns Result<_, SpkError>, where SpkError is celestial_ephemeris::jpl::SpkError:

VariantCause
Iothe file can’t be read
InvalidFormatnot an SPK file, or damaged (found at open)
InvalidDatathe segments covering the epoch form a loop or exceed 32 links
InvalidEpoch { jd }the epoch is NaN or infinite
SegmentNotFound { body, center, jd }no segments connect the pair at that epoch
UnsupportedTypethe chain needs a segment of a type other than 2
UnsupportedFramethe chain needs a segment in a frame other than J2000

SpkError converts into AstroError, so ? works in functions that return AstroResult.

Where next