# Calculate position of the Sun in ECEF

I'm making a very basic orbit simulator in C# using the Helix Toolkit.

I want to position the Helix lighting object at the sun, but also be able to calculate solar eclipse by the moon and earth for a satellite (position defined in ECEF).

Upon some brief research, I'm reading that getting an accurate position calculation for the sun in ECEF is a lot of work.

Is there an algorithm that can perform this calculation for me that I can include into an UpdateSunPosition() method so that I can have a reasonably accurate position calculation of the sun in ECEF?

Perhaps I have to scratch the eclipse calculation ambitions for now, instead calculate Geographical Position of the sun to get the Declination and Greenwich Hour Angle, Call those latitude and longitude, and then just give it a satisfactory altitude.

I've noticed that people are referring to SOFA a lot when talking about this, but I'm not sure what SOFA is, or whether I can use it in C#. I'm pretty sure it doesn't operate in a .NET environment like visual studio, but please correct me if I'm wrong.

I appreciate any and all suggestions or insights anyone may be able to offer here.

SOFA is the Standards Of Fundamental Astronomy, approved by the International Astronomical Union (IAU) as being the canonical standard for calculating positions and time in astronomy. It is available in C and F77 versions as standard, and wrappers have been written in other languages, most notably Python as part of AstroPy. For your purpose, the thirdparty part of the website mentions the World Wide Astronomy (WWA) library which is C# so that would be worth a look for your application. I would recommend reading the SOFA Cookbooks, particularly the ones on Earth Attitude and Time Scales to get a better idea of how the (many) various time and co-ordinate systems fit together.

• Thank you sir! I'll look into this and hopefully be able to update my question if I run into something I don't understand. – a_here_and_now Oct 5 '18 at 18:27
• I'd personally recommend CSPICE, which is what NASA uses, but SOFA is good to. – barrycarter Oct 7 '18 at 18:15
• Since the OP was looking for a C# version and seemed to have some exposure to SOFA already, it seemed the logical choice. It also comes with the reasonable precision ephemerides built in: epv00 for Earth position (reversible for Sun position) and plan94 for the planets. Saves having to download extra kernels – astrosnapper Oct 7 '18 at 21:34
• Given that the CSPICE toolkit is provided in C, how would I use it in C#? I actually am quite new to programming, and realistically I have no exposure to SOFA, just heard of it when I would search for previous inquiries toward my objective. Which one would be easier to implement? I have no problem installing extra stuff as long as its reasonably simple to do. Also, any tutorials or advice/pointers in the right direction toward implementing this would surely go a really long way and save me a lot of time. Otherwise, looking forward to learning and struggling with this for a day or so! – a_here_and_now Oct 8 '18 at 5:08
• I'm doing this for my work and they're pretty set up in the .NET environment with C# right now, so as much as I wish I could just use python, it's a bit of a stretch for my case since I can implement it with the SOFA functions. I've been reading through the SOFA documentation, they have equivalent C# functions posted on github already, and this seems to have potential to work really well. Thank you all! – a_here_and_now Oct 10 '18 at 23:06

@astrosnapper's answer is probably the one you want. But if you'd like something you can implement yourself, have a look at Astronomical Algorithms in this answer

This is something you'll have to dig into a bit, but if you like to program, it may be exactly what you're looking for. The Gaisma website is one of my favorites on the internet - easy to use and presents a bunch of information in easy-to-understand graphics. Click around!

I believe that this site uses algorithms from the collection found at this NOAA site. Click around there as well. They provide Excel spreadsheets which contain the algorithms and other resources. The "main" resource is a collection of algorithms published in the book Astronomical Algorithms - Jean Meeus. Search the books title and you can find that there are many similarly titled books. I'd recommend going to a library if possible, because it's (in my opinion) always good to go to libraries. However parts of these can be found on-line. For example, a few pages shown from the book Astronomical Formulae for Calculators (1988) include an interesting table of contents.

• Wow these are great resources. Thank you very much! – a_here_and_now Oct 10 '18 at 23:05

IAU SOFA and SPICE are overkill for what you need, and would require writing a C to C# wrapper around them. An implementation of VSOP87 in C# is all you really need. This is what was used to create NASA's Five Millennium Catalog of Solar Eclipses. The linked code has functions for getEarth() and getMoon() which return the heliocentric XYZ coordinates, the sun will always be at 0,0,0. To get geocentric coordinates, just subtract the Earth's position from the Moon and Sun positions.

You'll also want to look into Besselian elements, these are elements that are pre-computed in order to generate all of the data you see on NASA's pages. These already include all of the computations for light time, precession, nutation, abberation, etc. There are references on the Wikipedia page, but the most practical one is Elements of Solar Eclipses by Jean Meeus. To get an idea of what they can do, I used this method to create my Eclipse Map App. You can use the Besselian Elements either from Meeus' book, or from NASA's catalog. You'll find the elements in NASA's catalog after clicking through to the details, e.g. here's the one for the Aug 21 2017 Eclipse.

The benefit of using Besselian Elements rather than direct computation is first, a lot of the work is already done (e.g. precession, nutation...). Second, they are more computationally efficient, things like eclipse duration for a given lat/lon are solved directly rather that through itteration where you have to recompute the positions, precess, nutation, light time, etc all over again for each time step.

Each eclipse page in NASA's catalog also has a JavaScript implementation of how to use Besselian Elements to display the local circumstances on the map they display. But you might find the code a bit hard to follow without a reference like Meeus' book to explain what's going on.

Note the name Xavier Jubier in the NASA source code, he also has his own site on solar eclipses, has a similar JavaScript implementation and can also be another source of Besselian Elements.