[Spice_discussion] Re: Spice and .NET (C#)

YAMAMOTO Yukio yukio at planeta.sci.isas.jaxa.jp
Tue Feb 24 12:35:38 PST 2009


Keith:

> I'm wondering if anyone has worked with the spice toolkit in .NET  
> with C#?


I've just tested and a sample code works fine.
DLL is required to access from C#.NET.

I did the following steps to make cspice.dll:

0. Download Visual C 32bit cspice.exe from NAIF Toolkit and extract it
1. Put attached files: 'cspice.def', 'mkcspice_dll.bat' into  
(somewhere)\cspice\src\cspice
2. Make 'erf' and 'erfc' functions due to the lack of these functions  
in VC++
# attached 'erf.c' works for 32 bit architecture (put it in the same  
directory above)
3. Execute mkcspice_dll.bat
# cspice.dll can be found in (somewhere)\cspice\lib

Just in case, I'll show a sample code using cspice.dll.
ConsoleApplication1.exe, de421.bsp, and cspice.dll must be in the same  
directory.

Regards,
Yukio



--- Program.cs ---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
     class Program
     {
         private static readonly string ABCORR = "NONE";
         private static readonly string FRAME = "J2000";
         private static readonly string SPK = "de421.bsp";
         private static readonly double ET0 = 0.0;
         private static readonly double STEP = 3600.0;
         private static readonly int MAXITR = 100;
         private static readonly string OBSERVER = "earth";
         private static readonly string TARGET = "moon";


         [DllImport("cspice.dll")]
         private static extern void spklef_c(string filename, ref int  
handle);

         [DllImport("cspice.dll")]
         private static extern void spkezr_c(string targ, double et,  
string refer, string abcorr, string obs, double[] starg, ref double lt);

         static void Main(string[] args)
         {
             int handle = 0;
             double[] state = new double[6];
             double et = 0.0;
             double lt = 0.0;

             spklef_c(SPK, ref handle);

             for (int i = 0; i < MAXITR; i++)
             {
                 et = ET0 + i * STEP;
                 spkezr_c(TARGET, et, FRAME, ABCORR, OBSERVER, state,  
ref lt);
                 System.Console.WriteLine("\net={0}\n", et);
                 System.Console.WriteLine("J2000 x-position (km):    
{0:F10}", state[0]);
                 System.Console.WriteLine("J2000 y-position (km):    
{0:F10}", state[1]);
                 System.Console.WriteLine("J2000 z-position (km):    
{0:F10}", state[2]);
                 System.Console.WriteLine("J2000 x-velocity (km/s):  
{0:F10}", state[3]);
                 System.Console.WriteLine("J2000 y-velocity (km/s):  
{0:F10}", state[4]);
                 System.Console.WriteLine("J2000 z-velocity (km/s):  
{0:F10}", state[5]);
             }
         }
     }
}

-----
YAMAMOTO Yukio
Center for science Satellite Operation and Data Archive(C-SODA),
Institute of Space and Astronautical Science (ISAS),
Japan Aerospace Exploration Agency (JAXA)
3-1-1 Yoshinodai, Sagamihara, Kanagawa 229-8510, JAPAN
yukio at planeta.sci.isas.jaxa.jp/yamamoto.yukio at jaxa.jp
TEL: +81-42-759-8711  FAX: +81-42-759-8405
-------------- next part --------------
Skipped content of type multipart/mixed


More information about the Spice_discussion mailing list