Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Oct 7, 2018 #1
    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: Oct 8, 2018
  2. jcsd
  3. Oct 12, 2018 #2


    Staff: 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:


    and here's Ackermans function as an example:


    and the C# code:

    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:

    Code (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: Oct 12, 2018
  4. Oct 12, 2018 #3
    Wow, I wasn't expecting someone to answer, but I'm sure it will be helpful. Thank you!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted