# C# Accurate Earth, Moon Sun visual model to predict eclipses

Hello all!

I am trying to make a VISUAL model (in C#) to predict lunar and earth eclipse periods for a satellite that I have accurate ephemeris for.

I want to calculate everything in ECEF reference frame.

How would a go about modeling this system to the accuracy I'm interested in?

I assume I need the following:

1) A reference epoch that coincides with a particular position of moon and position/orientation of earth.
- how would I arrive at this?

2) A time scale, I assume I can use Terrestrial Time, please correct me if I'm wrong.

3) an ephemeris model.
- I would like to be able to update celestial object positions according to a time-slider and/or stop/start button which proceeds at a specified pace. Would calculating my own ephemeris be necessary, or can I use the JPL Horizons data?

4) An earth rotation model. This one is mainly so that I can report coordinates accurately in ECEF, which is the reference frame that I'm interested in.

I'm thoroughly bewildered after looking through SOFA documentation. After about an hour of reading I guess I got that I can just use a Terrestrial Time scale, but I'm not real sure how I would use SOFA (certainly I have more to read) to implement my objectives. I've also heard of CSPICE, used by NASA, but haven't looked into it yet because I saw that the toolkit was offered in C, and I don't know if I can use that for a C# program, as I'm not sure how that works.

I'm basically just getting started, but any guidance or tutorials or hints/shortcuts that anyone can provide to me will be very much appreciated, and save me a lot of lurking around in the dark until I realize what it is that I need.

Thank you so much in advance!

Last edited:

## Answers and Replies

jedishrfu
Mentor
If you have code in one language and need it in another, its possible to convert it line by line. This requires understanding both languages enough to do that. However, sometimes it can be quite daunting especially if they are based on different paradigms.

In the C to C# case, I don't think the port would be too difficult as they are both "C" style languages. Of course, you're probably going from procedural to object oriented but that is an easy mapping.

To get an idea of what it might take look at the rosettacode.org site. They have many examples of tasks solved in a multitude of languages and while in many cases they are not direct ports of one another you can still see how things are done in C and then done in C#.

Here's a list of C# examples, of which many have C versions too:

http://rosettacode.org/wiki/Category:C_sharp

and here's Ackermans function as an example:

http://rosettacode.org/wiki/Ackermann_function

The Ackermann function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. It grows very quickly in value, as does the size of its call tree.

The Ackermann function is usually defined as follows:

{\displaystyle A(m,n)={\begin{cases}n+1&{\mbox{if }}m=0\\A(m-1,1)&{\mbox{if }}m>0{\mbox{ and }}n=0\\A(m-1,A(m,n-1))&{\mbox{if }}m>0{\mbox{ and }}n>0.\end{cases}}}

Its arguments are never negative and it always terminates. Write a function which returns the value of {\displaystyle A(m,n)}. Arbitrary precision is preferred (since the function grows so quickly), but not required.

and the C# code:

C#:
using System;
class Program
{
public static long Ackermann(long m, long n)
{
if(m > 0)
{
if (n > 0)
return Ackermann(m - 1, Ackermann(m, n - 1));
else if (n == 0)
return Ackermann(m - 1, 1);
}
else if(m == 0)
{
if(n >= 0)
return n + 1;
}

throw [URL='http://www.google.com/search?q=new+msdn.microsoft.com']new[/URL] System.ArgumentOutOfRangeException();
}

static void Main()
{
for (long m = 0; m <= 3; ++m)
{
for (long n = 0; n <= 4; ++n)
{
Console.WriteLine("Ackermann({0}, {1}) = {2}", m, n, Ackermann(m, n));
}
}
}
}

and the C version:

C:
#include <stdio.h>

int ackermann(int m, int n)
{
if (!m) return n + 1;
if (!n) return ackermann(m - 1, 1);
return ackermann(m - 1, ackermann(m, n - 1));
}

int main()
{
int m, n;
for (m = 0; m <= 4; m++)
for (n = 0; n < 6 - m; n++)
[URL='http://www.opengroup.org/onlinepubs/009695399/functions/printf.html']printf[/URL]("A(%d, %d) = %d\n", m, n, ackermann(m, n));

return 0;
}

Last edited:
BvU
Wow, I wasn't expecting someone to answer, but I'm sure it will be helpful. Thank you!