<p><a href="http://www.iausofa.org/" rel="nofollow noreferrer">SOFA</a> 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 <a href="https://github.com/abrudana/wwa" rel="nofollow noreferrer">World Wide Astronomy (WWA) library</a> which is C# so that would be worth a look for your application. I would recommend reading the <a href="http://www.iausofa.org/cookbooks.html" rel="nofollow noreferrer">SOFA Cookbooks</a>, 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.</p>

CC BY-SA 4.0
2018-10-05T18:27:47.990Z
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
CC BY-SA 4.0
2018-10-07T21:34:35.763Z
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
CC BY-SA 4.0
2018-10-08T05:08:27.133Z
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
CC BY-SA 4.0
2018-10-10T13:38:06.010Z
The easiest way in my opinion would be to take a walk on the wild side and try a little Python. There's an example in [this answer](https://astronomy.stackexchange.com/a/18029/7982) and if `topo.at(times).observe(sun).apparent().altaz()` isn't clear enough already let me know and I can help further. https://xkcd.com/353/
uhoh
CC BY-SA 4.0
2018-10-10T23:06:57.017Z
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
CC BY-SA 4.0
2018-10-05T16:13:51.287Z
2018-10-05T16:13:51.287Z
astrosnapper
<p>@astrosnapper's <a href="https://astronomy.stackexchange.com/a/27906/7982">answer</a> is probably the one you want. But if you'd like something you can implement yourself, have a look at <strong>Astronomical Algorithms</strong> in <a href="https://astronomy.stackexchange.com/a/18029/7982">this answer</a></p>
<blockquote>
<p>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 <a href="http://www.gaisma.com/en/" rel="nofollow noreferrer">Gaisma website</a> is one of my favorites on the internet - easy to use and presents a bunch of information in easy-to-understand graphics. Click around!</p>
<p>I believe that this site uses algorithms from the collection found at <a href="http://www.esrl.noaa.gov/gmd/grad/solcalc/calcdetails.html" rel="nofollow noreferrer">this NOAA site</a>. Click around there as well. They provide Excel spreadsheets which contain the algorithms and other resources. The &quot;main&quot; resource is a collection of algorithms published in the book <a href="http://www.willbell.com/math/mc1.htm" rel="nofollow noreferrer">Astronomical Algorithms - Jean Meeus</a>. 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 <a href="http://www.math.harvard.edu/%7Eknill/3dprinter/exhibits/bc/Meeus1988.pdf" rel="nofollow noreferrer">Astronomical Formulae for Calculators (1988)</a> include an interesting table of contents.</p>
</blockquote>

CC BY-SA 4.0
2018-10-10T23:05:01.727Z
Wow these are great resources. Thank you very much!
a_here_and_now
CC BY-SA 4.0
2018-10-10T12:37:56.643Z
2018-10-10T12:37:56.643Z
2020-06-17T09:47:40.653Z
uhoh
<p>IAU SOFA and SPICE are overkill for what you need, and would require writing a C to C# wrapper around them.  An implementation of <a href="https://github.com/gmiller123456/vsop87-multilang/blob/master/Languages/C%23/vsop87a_full.cs" rel="nofollow noreferrer">VSOP87 in C#</a> is all you really need.  This is what was used to create NASA's <a href="https://eclipse.gsfc.nasa.gov/SEcat5/catalog.html" rel="nofollow noreferrer">Five Millennium Catalog of Solar Eclipses</a>.  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.</p>

<p>You'll also want to look into <a href="https://en.wikipedia.org/wiki/Besselian_elements" rel="nofollow noreferrer">Besselian elements</a>, 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 <a href="https://www.willbell.com/math/mc6.htm" rel="nofollow noreferrer">Elements of Solar Eclipses</a> by Jean Meeus.  To get an idea of what they can do, I used this method to create my <a href="https://play.google.com/store/apps/details?id=com.principiaprogramatica.eclipsemap" rel="nofollow noreferrer">Eclipse Map App</a>.  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 <a href="https://eclipse.gsfc.nasa.gov/SEsearch/SEdata.php?Ecl=%2020170821" rel="nofollow noreferrer">Aug 21 2017</a> Eclipse.</p>

<p>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.</p>

<p>Each eclipse page in NASA's catalog also has a <a href="https://eclipse.gsfc.nasa.gov/SEgoogle/SEcirc.js" rel="nofollow noreferrer">JavaScript implementation</a> 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.</p>

<p>Note the name Xavier Jubier in the NASA source code, he also has <a href="http://xjubier.free.fr/en/site_pages/Solar_Eclipses.html" rel="nofollow noreferrer">his own site</a> on solar eclipses, has a similar JavaScript implementation and can also be another source of Besselian Elements.</p>

CC BY-SA 4.0
2019-09-02T17:37:07.827Z
2019-09-02T17:37:07.827Z
Greg Miller
<p>I'm making a very basic orbit simulator in C# using the Helix Toolkit.</p>

<p>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).</p>

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

<p>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?</p>

<p>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.</p>

<p>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.</p>

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

<the-sun><earth><coordinate><frame-of-reference>
Calculate position of the Sun in ECEF
a_here_and_now
# Calculate position of the Sun in ECEF

(Asked by: username_0 on 2018-10-04)

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.

**Tags:** the-sun, earth, coordinate, frame-of-reference


---

## 3 answers

### Answer by username_1 on 2018-10-05. Score: 1

[SOFA](http://www.iausofa.org/) 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](https://github.com/abrudana/wwa) which is C# so that would be worth a look for your application. I would recommend reading the [SOFA Cookbooks](http://www.iausofa.org/cookbooks.html), 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.

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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  
> **username_0**: 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!  
> **username_2**: The easiest way in my opinion would be to take a walk on the wild side and try a little Python. There's an example in [this answer](https://astronomy.stackexchange.com/a/18029/7982) and if `topo.at(times).observe(sun).apparent().altaz()` isn't clear enough already let me know and I can help further. https://xkcd.com/353/  
> **username_0**: 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!  

---

### Answer by username_2 on 2018-10-10. Score: 0

@username_1's [answer](https://astronomy.stackexchange.com/a/27906/7982) 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](https://astronomy.stackexchange.com/a/18029/7982)

> 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](http://www.gaisma.com/en/) 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](http://www.esrl.noaa.gov/gmd/grad/solcalc/calcdetails.html). 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](http://www.willbell.com/math/mc1.htm). 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)](http://www.math.harvard.edu/%7Eknill/3dprinter/exhibits/bc/Meeus1988.pdf) include an interesting table of contents.

> **Comments:**
> **username_0**: Wow these are great resources. Thank you very much!  

---

### Answer by username_3 on 2019-09-02. Score: 0

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#](https://github.com/gmiller123456/vsop87-multilang/blob/master/Languages/C%23/vsop87a_full.cs) is all you really need. This is what was used to create NASA's [Five Millennium Catalog of Solar Eclipses](https://eclipse.gsfc.nasa.gov/SEcat5/catalog.html). 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](https://en.wikipedia.org/wiki/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](https://www.willbell.com/math/mc6.htm) by Jean Meeus. To get an idea of what they can do, I used this method to create my [Eclipse Map App](https://play.google.com/store/apps/details?id=com.principiaprogramatica.eclipsemap). 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](https://eclipse.gsfc.nasa.gov/SEsearch/SEdata.php?Ecl=%2020170821) 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](https://eclipse.gsfc.nasa.gov/SEgoogle/SEcirc.js) 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](http://xjubier.free.fr/en/site_pages/Solar_Eclipses.html) on solar eclipses, has a similar JavaScript implementation and can also be another source of Besselian Elements.


---

