SPICE Ephemeris Examples

The SPICEBodies.KernelBody type allows us to idiomatically query information from the SPICE kernel pool. First, download some common generic kernels (such as de432s below) and load them into the kernel pool with SPICE.furnsh. If you want more information about what's in each kernel, inspect each kernel's docstring; for example, @doc de432s, or help?> de432s in Julia's REPL. For a complete list of kernels available, look at the SPICEKernels.jlreference.

using SPICE, SPICEKernels, SPICEBodies

return furnsh(
    de432s(),                   # position and velocity data for nearby planets
    latest_leapseconds_tls(),   # timekeeping, parsing epochs
    gm_de440(),                 # mass parameters for major solar system bodies
    pck00011(),                 # physical properties of major solar system bodies
)

earth = KernelBody("earth")
KernelBody(399)

We can now call the earth variable like a function of time, and get back the positions (and velocities) interpolated by CSPICE from the data in the kernel pool.

using Dates

timepoints = DateTime(1950,1,1) : Month(1) : DateTime(2049,1,1)

states = earth.(timepoints)
1189-element Vector{@NamedTuple{x::Float64, y::Float64, z::Float64, ẋ::Float64, ẏ::Float64, ż::Float64}}:
 (x = -2.720318046700959e7, y = 1.329407955300235e8, z = 5.764165533644051e7, ẋ = -29.75363625884183, ẏ = -5.189341035287447, ż = -2.251484913827908)
 (x = -9.902833382017332e7, y = 1.0040652826781224e8, z = 4.35313358727315e7, ẋ = -22.52720648973097, ẏ = -18.483917251059818, ż = -8.01614801559218)
 (x = -1.3966845566407815e8, y = 4.5616557443582095e7, z = 1.9769471350835178e7, ẋ = -10.406557648847198, ẏ = -25.870566979175912, ż = -11.21927258293611)
 (x = -1.4646614073234868e8, y = -2.681254099998115e7, z = -1.1641570965742975e7, ẋ = 5.39774046234072, ẏ = -26.88711882718476, ż = -11.659181585345461)
 (x = -1.1412223453041473e8, y = -9.00012171516033e7, z = -3.904475573446325e7, ẋ = 18.949244871998815, ẏ = -20.799617346896955, ż = -9.018952187767812)
 (x = -5.0254058086600736e7, y = -1.3104251646834615e8, z = -5.684258860509865e7, ẋ = 27.599100194084826, ẏ = -9.166248876325023, ż = -3.9744472807380777)
 (x = 2.45855429651294e7, y = -1.3746019509427235e8, z = -5.962479092263712e7, ẋ = 28.89418357087274, ẏ = 4.300619312948164, ż = 1.8651216947865241)
 (x = 9.530566342897053e7, y = -1.0822207396061464e8, z = -4.694383946935118e7, ẋ = 22.69545683775163, ẏ = 17.0340544319733, ż = 7.386655120353878)
 (x = 1.4063767822966304e8, y = -5.01483309106074e7, z = -2.175763130944883e7, ẋ = 10.343457611056518, ẏ = 25.34130720390118, ż = 10.989363166602146)
 (x = 1.4831704591675627e8, y = 1.9154263066786677e7, z = 8.298149984385307e6, ẋ = -4.593535542991541, ẏ = 26.95264040491047, ż = 11.688513275532829)
 ⋮
 (x = -1.1479127662543999e8, y = -9.046661246165085e7, z = -3.919741928724267e7, ẋ = 18.91109913460718, ẏ = -20.847394072624287, ż = -9.037334299355386)
 (x = -5.0970398412952796e7, y = -1.3164011643425582e8, z = -5.704440712100888e7, ẋ = 27.59883164226738, ẏ = -9.228887548273814, ż = -4.001465726083546)
 (x = 2.3901537283196002e7, y = -1.3819892269296312e8, z = -5.988850411926041e7, ẋ = 28.928680406980053, ẏ = 4.242215206048147, ż = 1.837860506454644)
 (x = 9.473252736073178e7, y = -1.090858920467224e8, z = -4.727139498119433e7, ẋ = 22.75835961436075, ẏ = 16.995655305980616, ż = 7.366525799161241)
 (x = 1.4023667750423107e8, y = -5.1085996288589984e7, z = -2.2133563280025195e7, ẋ = 10.421054405574981, ẏ = 25.331786454982666, ż = 10.980201242757428)
 (x = 1.4811370063122842e8, y = 1.821453232052046e7, z = 7.902587114713652e6, ẋ = -4.51509370125112, ẏ = 26.974236732165476, ż = 11.69211923153682)
 (x = 1.1586107551621568e8, y = 8.44408385916219e7, z = 3.660667737006641e7, ẋ = -19.038036915484867, ẏ = 21.276038055926627, ż = 9.221812706044332)
 (x = 5.3172643192910545e7, y = 1.2568721481573243e8, z = 5.448400122407259e7, ẋ = -28.245680832165448, ẏ = 9.798827709455324, ż = 4.246680091504465)
 (x = -2.652440650869857e7, y = 1.3229950354878967e8, z = 5.735002431569345e7, ẋ = -29.793459658981774, ẏ = -4.989871774104861, ż = -2.1638229459886342)

Finally, let's plot the data we just collected.

using Plots

x = map(u -> u[begin], states)
y = map(u -> u[begin+1], states)

fig = scatter(
    x, y;
    label=nothing, markersize=1,
    xlabel="X (KM)", ylabel="Y (KM)", zlabel="Z (KM)",
    title="Earth's Position w.r.t. SSB",
    aspect_ratio=1, dpi = 125,
)
Example block output

Reference

You can find more information about SPICEKernels and SPICEBodies at their respective documentation pages, which are linked above in the website's navigation bar. For convenience, their pages are each linked below as well.

Documentation
SPICEKernels.jl
SPICEBodies.jl