Main Page
Basics, Building SPICE Applications (Python)

Table of Contents

   Basics, Building SPICE Applications (Python)
      Note About HTML Links
      Environment Set-up
         Unix/Linux/Windows
         Mac
      Confirm that Python can access SpiceyPy
      A simple example program
      SpiceyPy Documentation




Top

Basics, Building SPICE Applications (Python)





November 20, 2017



Top

Note About HTML Links




The HTML version of this lesson contains links pointing to various HTML documents provided with the Toolkit. All of these links are relative and, in order to function, require this document to be in a certain location in the Toolkit HTML documentation directory tree.

In order for the links to be resolved, if not done already by installing the lessons package under the Toolkit's ``doc/html'' directory, create a subdirectory called ``lessons'' under the ``doc/html'' directory of the ``cspice/'' tree and copy this document to that subdirectory before loading it into a Web browser.



Top

Environment Set-up




SpiceyPy is currently supported on Mac, Linux, and Windows systems.



Top

Unix/Linux/Windows



In order to install SpiceyPy, using pip, first install the following required packages:

              $ pip install six
              $ pip install numpy
              $ pip install spiceypy
Using anaconda, miniconda or conda:

      $ conda install -c https://conda.anaconda.org/andrewannex spiceypy


Top

Mac



On Mac, in order to install SpiceyPy with pip, there are two options:

    1. With Python < 3.6, a home-brew install of Python will be required. First install openssl:

                            $ brew update
                            $ brew install openssl
    Update your PATH with the directory where openssl was installed:

                $ setenv PATH /usr/local/opt/openssl/bin:$PATH
    Force brew to link the newly installed openssl and install Python with the brewed openssl option:

                            $ brew link openssl --force
                            $ brew install python --with-brewed-openssl
    This will install Python on /usr/local/Cellar/python/X.Y.Z, with X.Y.Z being the Python's version number (e.g. 2.7.14). This version of Python supports OpenSSL > 1.0.1j which is enough for installing SpiceyPy with pip.

    Update your PATH again with the directory where the brewed Python was installed:

                $ setenv PATH /usr/local/opt/python/libexec/bin:$PATH
    2. With Python >= 3.6, install the SSL certificates, by running the script Install Certificates.command provided with the installation from python.org, and the run the pip to install six and spiceypy.



Top

Confirm that Python can access SpiceyPy




Run pip list in order to get a list of the third party libraries that are currently available to your Python installation:

      $ pip list --format=columns
      Package    Version
      ---------- -------
      pip        9.0.1
      numpy      1.13.3
      setuptools 36.5.0
      six        1.11.0
      ...
      spicepy    2.0.0
      ...


Top

A simple example program




This script calls the spiceypy function 'tkvrsn' and outputs the return value.

           File tkvrsn.py
 
              from __future__ import print_function
              import spiceypy
 
              def print_ver():
                      """Prints the TOOLKIT version
                      """
                      print(spiceypy.tkvrsn('TOOLKIT'))
 
              if __name__ == '__main__':
                      print_ver()
From the command line, execute the function:

           $ python tkvrsn.py
           CSPICE_N0066
From Python, execute the function:

           $ python
           >>> import tkvrsn
           >>> tkvrsn.print_ver()
           CSPICE_N0066


Top

SpiceyPy Documentation




The current version of SpiceyPy does not provide extensive documentation, but there are several ways to navigate your way through the Python version of the toolkit. One simple way is to use the standard Python mechanisms. All interfaces implemented in SpiceyPy can be listed using the standard built-in function dir(), which returns an alphabetized list of names comprising (among) other things, the API names. If you need to get additional information about an API parameters, the standard built-in function help() could be used:

   >>> import spiceypy
   >>> help(spiceypy.)
which produces

   Help on function tkvrsn in module spiceypy.spiceypy:
 
   tkvrsn(item)
       Given an item such as the Toolkit or an entry point name, return
       the latest version string.
 
       http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/tkvrsn_c.
   html
 
       :param item: Item for which a version string is desired.
       :type item: str
       :return: the latest version string.
       :rtype: str
As indicated in the help on the function, the complete documentation is available on the CSPICE toolkit version. Therefore it is recommended to have the CSPICE toolkit version installed locally in order to access its documentation offline.