Skip to content

Latest commit

 

History

History
214 lines (146 loc) · 245 KB

exampleone.rst

File metadata and controls

214 lines (146 loc) · 245 KB

Cassini Position Example

Below is an example that uses spiceypy to plot the position of the Cassini spacecraft relative to the barycenter of Saturn.

import numpy as np
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *
init_notebook_mode()

First import spiceypy and test it out.

import spiceypy as spice
# Print out the toolkit version
spice.tkvrsn("TOOLKIT")

'CSPICE_N0065'

We will need to load some kernels. You will need to download the following kernels from the NAIF servers via the links provided. After the kernels have been downloaded to a common directory write a metakernel containing the file names for each downloaded kernel (provided after the links). I named the metakernel 'cassMetaK.txt' for this example. For more on defining meta kernels in spice, please consult the Kernel Required Reading.

# The meta kernel file contains entries pointing to the following SPICE kernels, which the user needs to download.
#   http://naif.jpl.nasa.gov/pub/naif/generic_kernels/lsk/a_old_versions/naif0009.tls
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/sclk/cas00084.tsc
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/pck/cpck05Mar2004.tpc
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/fk/release.11/cas_v37.tf
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/ck/04135_04171pc_psiv2.bc
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/spk/030201AP_SK_SM546_T45.bsp
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/ik/release.11/cas_iss_v09.ti
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/spk/020514_SE_SAT105.bsp
#   http://naif.jpl.nasa.gov/pub/naif/CASSINI/kernels/spk/981005_PLTEPH-DE405S.bsp
#
#   The following is the contents of a metakernel that was saved with
#   the name 'cassMetaK.txt'.
#   \begindata
#   KERNELS_TO_LOAD=(
#   'naif0009.tls',
#   'cas00084.tsc',
#   'cpck05Mar2004.tpc',
#   '020514_SE_SAT105.bsp',
#   '981005_PLTEPH-DE405S.bsp',
#   '030201AP_SK_SM546_T45.bsp',
#   '04135_04171pc_psiv2.bc',
#   'cas_v37.tf',
#   'cas_iss_v09.ti')
#   \begintext
#

spice.furnsh("./cassMetaK.txt")
step = 4000
# we are going to get positions between these two dates
utc = ['Jun 20, 2004', 'Dec 1, 2005']

# get et values one and two, we could vectorize str2et
etOne = spice.str2et(utc[0])
etTwo = spice.str2et(utc[1])
print("ET One: {}, ET Two: {}".format(etOne, etTwo))

ET One: 140961664.18440723, ET Two: 186667264.18308285

# get times
times = [x*(etTwo-etOne)/step + etOne for x in range(step)]

# check first few times:
print(times[0:3])

[140961664.18440723, 140973090.5844069, 140984516.98440656]

# check the documentation on spkpos before continueing
help(spice.spkpos)

Help on function spkpos in module spiceypy.spiceypy:

spkpos(targ, et, ref, abcorr, obs)

Return the position of a target body relative to an observing body, optionally corrected for light time (planetary aberration) and stellar aberration.

http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/spkpos_c.html

param targ

Target body name.

type targ

str

param et

Observer epoch.

type et

float or List of Floats

param ref

Reference frame of output position vector.

type ref

str

param abcorr

Aberration correction flag.

type abcorr

str

param obs

Observing body name.

type obs

str

return

Position of target, One way light time between observer and target.

rtype

tuple

#Run spkpos as a vectorized function
positions, lightTimes = spice.spkpos('Cassini', times, 'J2000', 'NONE', 'SATURN BARYCENTER')

# Positions is a 3xN vector of XYZ positions
print("Positions: ")
print(positions[0])

# Light times is a N vector of time
print("Light Times: ")
print(lightTimes[0])

Positions: [-5461446.61080924 -4434793.40785864 -1200385.93315424] Light Times: 23.8062238783

# Clean up the kernels
spice.kclear()

We will use Plotly to visualize Cassini's coordinates.

threeDPlot = Scatter3d(
    x=positions[:, 0], # X coordinates
    y=positions[:, 1], # Y coordinates
    z=positions[:, 2], # Z coordinates
    name='Cassini',
    mode='lines',
    line=Line(width=3)
)

barycenter = Scatter3d(
    x=[0],
    y=[0],
    z=[0],
    name='bc',
    mode='marker',
    marker=dict(
        size=10,
        color='orange'
    )
)

data = Data([threeDPlot, barycenter])

layout = Layout(title="SpiceyPy Cassini Position Example")

fig = dict(data=data, layout=layout)
iplot(fig)