Porting the SPICE Library
by Ed Wright


Example 1: 

SUBROUTINE SPKEZ ( TARG, ET, REF, ABCORR, OBS, STARG, LT )
INTEGER               TARG
DOUBLE PRECISION      ET
CHARACTER*(*)         REF
CHARACTER*(*)         ABCORR
INTEGER               OBS
DOUBLE PRECISION      STARG    ( 6 )
DOUBLE PRECISION      LT


Example 2: 

int spkez_( integer    *targ    ,
            doublereal *et      ,
            char       *ref     ,
            char       *abcorr  ,
            integer    *obs     ,
            doublereal *starg   ,
            doublereal *lt      ,
            ftnlen      ref_len ,
            ftnlen      abcorr_len )

Example 3:

void spkez_c ( SpiceInt            targ,
               SpiceDouble         et,
               ConstSpiceChar    * ref,
               ConstSpiceChar    * abcorr,
               SpiceInt            obs,
               SpiceDouble         starg[6],
               SpiceDouble       * lt       )



Listing One
/*
-Procedure states( Compute state of one body relative to another )
-Abstract
   This "cookbook" program demonstrates the use of NAIF S- and P-
   Kernel (SPK) files and subroutines to calculate the state
   (position and velocity) of one solar system body relative to
   another solar system body. The purpose of this program is twofold:
      1) To show how NAIF ephemeris data may be made available to a program.
      2) To show how the apparent, true, or geometric state
         (inertially referenced cartesian position and velocity)
         of one solar system body relative to another solar
         system body may be calculated.
   The CSPICE subroutine spklef_c (S/P Kernel, Load Ephemeris
   File) handles the first task by maintaining a database of
   ephemeris files. The calling program indicates which files
   to load by passing their names to spklef_c.

   spkezr_c (S/P Kernel, Real easy reader) handles the second task
   by accessing the data loaded with spklef_c (spkezr_c does not
   require the name of an SPK file as input).

-Copyright
   Copyright (1998), California Institute of Technology.
   U.S. Government sponsorship acknowledged.

-Input
   The program prompts the user for the following input:
      - The name of a NAIF leapseconds kernel file.
      - The name of a NAIF binary SPK ephemeris file.
      - The name for the observing body.
      - The name for the target body.
      - A time string of interest.
      - An reference frame, i.e., "J2000".
      - The type of aberration correction desired.

-Output
      - The state of the target body relative to the observing body.
      - The one-way light-time from the target body to the observing body.

-Particulars
   The user supplies a NAIF leapseconds kernel file, a NAIF binary
   SPK ephemeris file, valid names for both the target and
   observing bodies, and the time to calculate the body's state.

   Note that the `target body' and the `observing body' are both
   NAIF ephemeris objects described via their common names, and
   may be any of the following, provided ephemeris data are
   available for them in the SPK file:
      - a spacecraft
      - a planet or satellite mass center
      - a planet barycenter
      - the sun
      - the solar system barycenter
      - a comet
      - an asteroid
   By definition, the ephemerides in SPK files are continuous. The
   user can obtain states for any epoch within the interval of
   coverage. Epochs are always specified in ephemeris seconds past
   Julian year 2000 when accessing SPK files.

   The ephemeris data in a single SPK file may be referenced to a
   number of different (inertial or non-inertial) frames. The user
   can specify that states be returned in any of the recognized
   frames listed in the NAIF IDs Required Reading, including J2000
   and B1950.

   spkezr_c returns apparent, true, or geometric states depending
   on the value of the aberration flag when it is called.

   Flag    Type of correction           State computed by spkezr_c
   ---------------------------------------------------------------
   "LT+S"   light-time and stellar aberration             Apparent
   "LT"     light-time only                                   True
   "NONE"   no correction                                Geometric

   For the sake of brevity, this program performs no error checks
   on its inputs. Mistakes will cause the program to crash.

-References
   For additional information, see NAIF IDS Required Reading, and
   the headers of the CSPICE subroutines spklef_c, spkez_c and str2et_c.

-Restrictions
   None.

-Literature_References
   None.

-Author_and_Institution
   E.D. Wright     (JPL)

-Version
   -CSPICE Version 1.0.0, 01-MAR-1998   (EDW)

-&
*/

   /* Load needed headers. */
   #include <stdio.h>
   #include "SpiceUsr.h"

   /* Local declarations. */
   #define                 UTCLEN   48
   #define                 LENOUT   32
   #define                 FILELEN  72

   SpiceDouble             vec    [3];
   SpiceDouble             vec1   [3];
   SpiceDouble             vec2   [3];
   SpiceDouble             vout   [3];
   SpiceDouble             state  [6];
   SpiceDouble             lt;
   SpiceDouble             et;

   SpiceChar            *  leap;
   SpiceChar            *  spk;
   SpiceChar            *  corr;
   SpiceChar            *  ref;
   SpiceChar            *  utc;
   SpiceChar            *  format;
   SpiceChar            *  targ;
   SpiceChar            *  obs;
   SpiceChar               utcstr[ UTCLEN ];

   SpiceInt                prec;
   SpiceInt                handle;

void main()
   {
   /* Set the time output format and the precision of that output. */
   format = "C";
   prec   = 0;
   /* Start out by prompting for names of kernel files. Load each kernel as
   name is supplied. prompt_c allocates needed memory for returned strings.
   */

   /* Get and load the leapsecond kernel. */
   leap   = prompt_c ( "Enter name of leapseconds kernel    : ");
   ldpool_c ( leap );

   /* Get and load the spk kernel. */
   spk    = prompt_c ( "Enter name of SPK file              : ");
   spklef_c ( spk, &handle );

   /* Get the rest of the needed parameters. */
   targ   = prompt_c ( "Target (what am I looking at)       : ");
   ref    = prompt_c ( "Reference frame (J2000, B1950, etc.): ");
   corr   = prompt_c ( "Aberration correction                : ");
   obs    = prompt_c ( "Observer (where am I)               : ");
   utc    = prompt_c ( "Event time                          : ");

   /* Convert the time string to ephemeris time J2000. */
   str2et_c ( utc, &et );

   /* Compute the state of targ from obs at et. */
   spkezr_c (  targ, et, ref, corr, obs, state, &lt );

   /* Convert the ephemeris time to a calendar format. */
   et2utc_c ( et , format, prec, UTCLEN, utcstr );

   /*
   Everything's computed.  Output the results.  Units are
   kilometers and kilometers per second.
   */
   printf ("\n The state of %s wrt %s at UTC time %s\n", targ, obs, utcstr );
   printf ( " X :  %f KM \n", state[0] );
   printf ( " VX:  %f KMS\n", state[3] );
   printf ( " Y :  %f KM \n", state[1] );
   printf ( " VY:  %f KMS\n", state[4] );
   printf ( " Z :  %f KM \n", state[2] );
   printf ( " VZ:  %f KMS\n", state[5] );
   }


1


