Integrating SPICE into Wolfram Language : C Style​
​by Pranav Sai
My Wolfram Summer School project is an exploratory proof of concept project of integrating data from Jet Propulsion Laboratory's SPICE API into Mathematica .
The SPICE API is a framework developed by Jet Propulsion Laboratory that serves as a platform for reading,analyzing, calculating, and predicting data from space. SPICE is used at the highest levels of space science, and its capability is unparalleled in the field. This project explores on how to integrate SPICE into Mathematica-specifically integrating CSPICE into Mathematica (written in the C language). I have written 5 functions in C that I compiled into a Dynamic Library. These functions get linked onto symbolic expressions in the Wolfram Language through LibraryLink functionality. Using these 5 basic functions and data from the Cassini Orbiter, I constructed a graphical visualization of the spacecraft’s flight path from beginning to end.

SPICE Structure

SPICE is a very dynamic API that provides functionality in all areas of space science. Using SPICE, one can plot the orbits of many deep space missions, read data from one of the science instruments, assess when a spacecraft will be visible, learn the composition of planetary neighbors, and much more. Functionality is split up into kernels - a specific kernel relates to a specific aspect of spaceflight. Inside of the kernels is where the data lives, in binary or text form. SPICE data is hefty, and as a side note a system for dynamic loading and unloading of SPICE kernels would be very useful.

Process

Coding in C

To begin, I wrote a program in C using SPICE functions that pulls data and performs geometric analysis on it, determining key features of the Cassini Probe such as location, velocity, and orientation at any given time. The CSPICE framework is massive, but it is well documented, straight from the people who made it!
spkezr_c("CASSINI",ephemeris_time,..."SUN",state,...);
This function takes in thee name of a spacecraft and a time specified by the variable ephemeris_time. The function also asks for the name of the observing body (here is set to SUN). It returns an array of length 6 containing the state of the spacecraft of that time.

Integrating To Wolfram Language

Using LibraryLink, the process to call a CSPICE function in Mathematica is straightforward. First I compiled my C code with necessary functions into a Dynamic Library. Then I loaded the functions into Mathematica.
FindStateC=LibraryFunctionLoad["spicywolf","find_state",{"UTF8String","UTF8String"},{Real,1}];
The compiled library is called spicywolf and the function being loaded is called find_state. find_state finds the position of a spacecraft or celestial body if given a time and name. The function returns a list of reals comprised of the following: {x, y, z, dx, dy, dz}
As long as necessary dependencies are installed (SEE MATERIALS), the code runs and the C functions are linked.

Visualization

Using FindState[], I was able to write a function to create a list of positions for a spacecraft or celestial body if given a DateInterval entity.
FindState[time_,probe_]:=FindStateC[DateString[time],EntityValue[probe,"Name"]][[1;;3]]
FindState takes in a Date entity and a Probe or Celestial Body entity. Then it sends the data to compute in C. The function only takes the position components of the result, not the velocity.
Next I can generate a full orbit trajectory using all these pieces.
GenerateOrbit[interval_,probe_]:=Module[{dateList=DateRange[Min[interval],Max[interval],GRANULARITY],positions=List[]},For[i=1,i≤Length[dateList],i++,AppendTo[positions,FindState[dateList[[i]],probe]]];​​Return[positions];]
This function is a little daunting. For input it takes in a DateInterval entity and a Stellar entity. It then goes through every moment in time though the interval (with a step specified by GRANULARITY) and computes the state of the stellar entity for that moment in time. Finally all of the states are appended into one big array and the positions in time are returned.
This data can be visualized in many different ways with many different techniques. I chose to visualize the data with a Line[] graphics component.
PlotSolarSystem[listOfPositions_]:=Manipulate[Graphics3D[{Yellow,Point[{0,0,0}],Black,Line[listOfPositions[[1]][[1;;x]]],Red,Line[listOfPositions[[2]][[1;;x]]],Blue,Line[listOfPositions[[3]][[1;;x]]],Brown,Line[listOfPositions[[4]][[1;;x]]],Green,Line[listOfPositions[[5]][[1;;x]]]},BoxedFalse,BackgroundGrayLevel[0.64]],{x,1,Length[listOfPositions[[1]]],1}]
This function takes in a list of a list of orbital positions and displays them in a Graphics3D component. Index correspond as follows: 1 -> Cassini, 2 -> Saturn, 3 -> Earth, 4 -> Moon, 5 -> Enceladus
The results look something like below.

Results

Cassini’s path to Saturn (1997 - 2005). Different celestial objects are represented here in different colors: The sun is the yellow dot, the brown line is Earth, black is Cassini, and the yellow line is Saturn. You can see how Cassini moved around the sun as it made its way toward Saturn.
Cassini’s exploration around the Saturn System (2006 - 2012). Saturn is the red dot, Enceladus is the green line, and Cassini is the black line.

Materials

My project folder can be found at the following link:
​https://drive.google.com/file/d/16Ei0Ll0-GVQP-bl4AJvn2E448G4KvvVx/view?usp=sharing​
​
This project folder contains The SPICE dynamic library, data from the Cassini orbiter, a Wolfram notebook, and SPICE API dependencies. Please follow the instructions in the Readme.txt. My development environment is Mac and much of the code is specific to it. I specified necessary changes to be made in the Readme.txt. Also, check log output for runtime issues. Feel free to message me as well!

Conclusion

The SPICE framework is a very exciting software tool because of its limitless potential applications. This project serves as a starting point for an integration of SPICE into Mathematica, being able to view data from our Cosmos in a meaningful way. I really enjoyed this project! I have never worked with with data like this before, and I learned a lot about data structure, orbital mechanics, and our cosmos along the way. I hope my work can help anyone who wants to get their hands dirty with SPICE!