<pre><code>public static Vector3 GetSunDirection(DateTime time)
        {
            time = time.ToUniversalTime();   
            double JD = 367*time.Year-Math.Floor(7.0*(time.Year+Math.Floor((time.Month+9.0)/12.0))/4.0)+Math.Floor(275.0*time.Month/9.0)+time.Day+1721013.5+time.Hour/24.0+time.Minute/1440.0+time.Second/86400.0;
            double pi = 3.14159265359;
            double UT1 = (JD-2451545)/36525;
            double longMSUN = 280.4606184+36000.77005361*UT1;
            double mSUN = 357.5277233+35999.05034*UT1;
            double ecliptic = longMSUN+1.914666471*Math.Sin(mSUN*pi/180)+0.918994643*Math.Sin(2*mSUN*pi/180);
            double eccen = 23.439291-0.0130042*UT1;

            double x = Math.Cos(ecliptic*pi/180);
            double y = Math.Cos(eccen*pi/180)*Math.Sin(ecliptic*pi/180);
            double z = Math.Sin(eccen*pi/180)*Math.Sin(ecliptic*pi/180);

            return new Vector3(x, y, z);
        }

        public static Vector3 GetSun(DateTime time)
        {
            double sunDistance = 0.989 * 1.496E+8;
            var sunPosition = GetSunDirection(time.ToUniversalTime());

            sunPosition = sunPosition * sunDistance;

            return sunPosition;
        }
</code></pre>

CC BY-SA 4.0
2018-12-20T03:55:22.577Z
2018-12-20T03:55:22.577Z
Nick Brown
<p>If you know how to use python, you can do something like : </p>

<pre><code>import astropy as ap
ap.coordinates.get_sun(time)
</code></pre>

<p>It will return  a position in geocentric coordinates for the given time.</p>

CC BY-SA 4.0
2018-12-19T17:24:47.130Z
Unfortunately I am using c# not python.
Nick Brown
CC BY-SA 4.0
2018-12-19T20:51:33.787Z
Then, i don't know how to help you ... sorry
Yoann A.
CC BY-SA 4.0
2018-12-19T17:22:26.467Z
2018-12-19T17:22:26.467Z
Yoann A.
<p>The answer below is a combination of the first <a href="https://astronomy.stackexchange.com/a/28806/34646">answer</a> with a cross-check from <a href="https://en.wikipedia.org/wiki/Axial_tilt#Short_term" rel="nofollow noreferrer">Wikipedia</a> (for the obliquity of the ecliptic plane specifically) and <a href="http://www.stargazing.net/kepler/sun.html" rel="nofollow noreferrer">here</a> (the formula for ecliptic longitude of the Sun in the first answer uses 0.918994643 to multiply sin(2 * <code>g</code> * <code>pi</code>) in the final term instead of 0.020, so I used the factor below, but I am not sure which is correct).  Also, the unit vector's direction does not give the distance, so a complete answer needs to scale the unit vector as follows:</p>
<p>First, calculate the Julian offset from <a href="https://en.wikipedia.org/wiki/Epoch_(astronomy)#Julian_Dates_and_J2000" rel="nofollow noreferrer">J2000</a> as a decimal number of days <code>d</code>.  Then, do the following steps, in order:</p>
<ol>
<li>Calculate parameters</li>
<li>Calculate unit directional vector</li>
<li>Calculate distance to sun and scale the unit vector</li>
</ol>
<p>NOTE:  All trigonometric function calls (sine, cosine) used below are the versions which accept radians, not degrees!  Almost all of the literature in this field uses degrees rather than radians because they are more intuitive for readers of the publications.  Also, the reason for dividing by 36525 is to obtain a decimal number of centuries, given that the Julian year is 365.25 days exactly.</p>
<p><strong>1.  Calculate parameters</strong></p>
<blockquote>
<p><code>L</code> = 280.4606184 + [(36000.77005361 / 36525) * <code>d</code>] a.k.a. mean longitude, in degrees</p>
<p><code>g</code> = 357.5277233 + ([35999.05034 / 36525] * <code>d</code>) a.k.a. mean anomaly, in degrees</p>
<p><code>p</code> = <code>L</code> + [1.914666471 * sin(<code>g</code> * <code>pi</code> / 180)] + [0.918994643 * sin(2*<code>g</code> * <code>pi</code> / 180)] a.k.a. ecliptic
longitude <code>lambda</code>, in degrees</p>
<p><code>q</code> = 23.43929 - ((46.8093/3600) * (<code>d</code> / 36525)) a.k.a. obliquity of ecliptic plane
<code>epsilon</code>, in degrees</p>
</blockquote>
<p><strong>2.  Calculate unit directional vector in ECI coordinates</strong></p>
<blockquote>
<p><code>u_x</code> = cos(<code>p</code> * <code>pi</code> / 180)</p>
<p><code>u_y</code> = cos(<code>q</code> * <code>pi</code> / 180) * sin(<code>p</code> * <code>pi</code> / 180)</p>
<p><code>u_v</code> = sin(<code>q</code> * <code>pi</code> / 180) * sin(<code>p</code> * <code>pi</code> / 180)</p>
</blockquote>
<p><strong>3.  Calculate distance to sun and scale the unit vector</strong></p>
<blockquote>
<p><code>a</code> = 1.000140612 - [0.016708617 * cos(<code>g</code> * <code>pi</code> / 180)] - [0.000139589 * cos(2 *
<code>g</code> * <code>pi</code> / 180)] a.k.a distance from Earth's center to Sun's center in
astronomical units (AU)</p>
<p><code>m</code> = <code>a</code> * 149597870700 a.k.a. center-to-center distance from Earth
to Sun in meters</p>
<p><code>v</code> = <code>m</code> * <code>u_v</code> (or <code>a</code> * <code>u_v</code>) is distance to sun in meters (or in
AU)</p>
</blockquote>

CC BY-SA 4.0
2020-07-29T16:14:29.703Z
If anyone has any more precise values for how to calculate the ecliptic longitude in radians, I would appreciate an edit.
tdMJN6B2JtUe
CC BY-SA 4.0
2020-07-29T17:52:17.370Z
Also the formula for the obliquity of the ecliptic plane only has one significant digit, so any help there would also be appreciated!
tdMJN6B2JtUe
CC BY-SA 4.0
2020-07-29T18:02:03.287Z
Alright I just corrected the formula for obliquity of the ecliptic plane, which was also actually in degrees, and made it have two significant digits (47 arcseconds per century of change).
tdMJN6B2JtUe
CC BY-SA 4.0
2020-07-29T18:19:28.097Z
Apparently a better number is 46.8093 arcseconds per century starting at J2000.0 according to https://en.wikipedia.org/wiki/Axial_tilt#Short_term, which is good for the next few thousand years?  Updating above...
tdMJN6B2JtUe
CC BY-SA 4.0
2020-07-29T18:23:00.320Z
Also apparently the starting point for Earth's tilt at J2000.0 is 23.439291... degrees according to the same article...updating formula again.
tdMJN6B2JtUe
CC BY-SA 4.0
2020-07-29T18:50:32.943Z
I gave up on using radians except in the calls to the sine and cosine functions, which should work better there because radians are the natural angular unit of trigonomety.
tdMJN6B2JtUe
CC BY-SA 4.0
2020-07-29T16:12:02.100Z
2020-07-29T19:00:33.740Z
2020-07-29T19:00:33.740Z
tdMJN6B2JtUe
<p>I’m trying to calculate visible ISS passes and I need to calculate if the ISS is behind the earth by “drawing” a line between the ISS and the sun and seeing if it crosses earth. I have the ECI position of the ISS, but I don’t have the position of the sun. How do I calculate the suns ECI position given a date and time? </p>

<p>Edit: I need to use c# for this so I can’t use python libraries </p>

CC BY-SA 4.0
2018-12-21T13:32:37.980Z
[It's possible to call C from C#](https://stackoverflow.com/questions/11425202/is-it-possible-to-call-a-c-function-from-c-net). Given that, use [SPICE](https://naif.jpl.nasa.gov/naif/toolkit.html) and download one of the DE4xx kernels. From there it's a simple call to [spkezp_c](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/spkezp_c.html).
David Hammen
<the-sun><astrophysics><mathematics>
Calculating the sun’s position in ECI
Nick Brown
# Calculating the sun’s position in ECI

(Asked by: username_0 on 2018-12-19)

I’m trying to calculate visible ISS passes and I need to calculate if the ISS is behind the earth by “drawing” a line between the ISS and the sun and seeing if it crosses earth. I have the ECI position of the ISS, but I don’t have the position of the sun. How do I calculate the suns ECI position given a date and time?

Edit: I need to use c# for this so I can’t use python libraries

**Tags:** the-sun, astrophysics, mathematics
> **Comments:**
> **username_1**: [It's possible to call C from C#](https://stackoverflow.com/questions/11425202/is-it-possible-to-call-a-c-function-from-c-net). Given that, use [SPICE](https://naif.jpl.nasa.gov/naif/toolkit.html) and download one of the DE4xx kernels. From there it's a simple call to [spkezp_c](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/spkezp_c.html).  

---

## 3 answers

### Answer by username_0 on 2018-12-20. Score: 1 | Accepted answer

public static Vector3 GetSunDirection(DateTime time)
        {
            time = time.ToUniversalTime();   
            double JD = 367*time.Year-Math.Floor(7.0*(time.Year+Math.Floor((time.Month+9.0)/12.0))/4.0)+Math.Floor(275.0*time.Month/9.0)+time.Day+1721013.5+time.Hour/24.0+time.Minute/1440.0+time.Second/86400.0;
            double pi = 3.14159265359;
            double UT1 = (JD-2451545)/36525;
            double longMSUN = 280.4606184+36000.77005361*UT1;
            double mSUN = 357.5277233+35999.05034*UT1;
            double ecliptic = longMSUN+1.914666471*Math.Sin(mSUN*pi/180)+0.918994643*Math.Sin(2*mSUN*pi/180);
            double eccen = 23.439291-0.0130042*UT1;

            double x = Math.Cos(ecliptic*pi/180);
            double y = Math.Cos(eccen*pi/180)*Math.Sin(ecliptic*pi/180);
            double z = Math.Sin(eccen*pi/180)*Math.Sin(ecliptic*pi/180);

            return new Vector3(x, y, z);
        }

        public static Vector3 GetSun(DateTime time)
        {
            double sunDistance = 0.989 * 1.496E+8;
            var sunPosition = GetSunDirection(time.ToUniversalTime());

            sunPosition = sunPosition * sunDistance;

            return sunPosition;
        }


---

### Answer by username_2 on 2018-12-19. Score: 3

If you know how to use python, you can do something like : 
import astropy as ap
ap.coordinates.get_sun(time)

It will return  a position in geocentric coordinates for the given time.

> **Comments:**
> **username_0**: Unfortunately I am using c# not python.  
> **username_2**: Then, i don't know how to help you ... sorry  

---

### Answer by username_3 on 2020-07-29. Score: 3

The answer below is a combination of the first [answer](https://astronomy.stackexchange.com/a/28806/34646) with a cross-check from [Wikipedia](https://en.wikipedia.org/wiki/Axial_tilt#Short_term) (for the obliquity of the ecliptic plane specifically) and [here](http://www.stargazing.net/kepler/sun.html) (the formula for ecliptic longitude of the Sun in the first answer uses 0.918994643 to multiply sin(2 \* `g` \* `pi`) in the final term instead of 0.020, so I used the factor below, but I am not sure which is correct). Also, the unit vector's direction does not give the distance, so a complete answer needs to scale the unit vector as follows:

First, calculate the Julian offset from [J2000](https://en.wikipedia.org/wiki/Epoch_(astronomy)#Julian_Dates_and_J2000) as a decimal number of days `d`. Then, do the following steps, in order:

1. Calculate parameters
2. Calculate unit directional vector
3. Calculate distance to sun and scale the unit vector

NOTE: All trigonometric function calls (sine, cosine) used below are the versions which accept radians, not degrees! Almost all of the literature in this field uses degrees rather than radians because they are more intuitive for readers of the publications. Also, the reason for dividing by 36525 is to obtain a decimal number of centuries, given that the Julian year is 365.25 days exactly.

**1. Calculate parameters**

> `L` = 280.4606184 + [(36000.77005361 / 36525) \* `d`] a.k.a. mean longitude, in degrees
>
> `g` = 357.5277233 + ([35999.05034 / 36525] \* `d`) a.k.a. mean anomaly, in degrees
>
> `p` = `L` + [1.914666471 \* sin(`g` \* `pi` / 180)] + [0.918994643 \* sin(2\*`g` \* `pi` / 180)] a.k.a. ecliptic
> longitude `lambda`, in degrees
>
> `q` = 23.43929 - ((46.8093/3600) \* (`d` / 36525)) a.k.a. obliquity of ecliptic plane
> `epsilon`, in degrees

**2. Calculate unit directional vector in ECI coordinates**

> `u_x` = cos(`p` \* `pi` / 180)
>
> `u_y` = cos(`q` \* `pi` / 180) \* sin(`p` \* `pi` / 180)
>
> `u_v` = sin(`q` \* `pi` / 180) \* sin(`p` \* `pi` / 180)

**3. Calculate distance to sun and scale the unit vector**

> `a` = 1.000140612 - [0.016708617 \* cos(`g` \* `pi` / 180)] - [0.000139589 \* cos(2 \*
> `g` \* `pi` / 180)] a.k.a distance from Earth's center to Sun's center in
> astronomical units (AU)
>
> `m` = `a` \* 149597870700 a.k.a. center-to-center distance from Earth
> to Sun in meters
>
> `v` = `m` \* `u_v` (or `a` \* `u_v`) is distance to sun in meters (or in
> AU)

> **Comments:**
> **username_3**: If anyone has any more precise values for how to calculate the ecliptic longitude in radians, I would appreciate an edit.  
> **username_3**: Also the formula for the obliquity of the ecliptic plane only has one significant digit, so any help there would also be appreciated!  
> **username_3**: Alright I just corrected the formula for obliquity of the ecliptic plane, which was also actually in degrees, and made it have two significant digits (47 arcseconds per century of change).  
> **username_3**: Apparently a better number is 46.8093 arcseconds per century starting at J2000.0 according to https://en.wikipedia.org/wiki/Axial_tilt#Short_term, which is good for the next few thousand years?  Updating above...  
> **username_3**: Also apparently the starting point for Earth's tilt at J2000.0 is 23.439291... degrees according to the same article...updating formula again.  
> **username_3**: I gave up on using radians except in the calls to the sine and cosine functions, which should work better there because radians are the natural angular unit of trigonomety.  

---

