## The Timing for Given Particular Sun Position

The following astronomical calculation of the timing for given particular sun position was taken from Almanac for Computers (1990) published by Nautical Almanac Office, United States Naval Observatory, Washington. Small modification has been applied, that is on the consistency of using degree (instead of radian) and on the inclusion of daylight saving time if necessary.

1. Calculate the day of the year (N):
2. Here, the inputs are YY, MM, and DD, which correspond to year, month, and day.
N_1 = \rm{floor}\left(\frac{275 \times MM}{9}\right)
N_2 = \rm{floor}\left(\frac{MM + 9}{12}\right)
N_3 = 1 + \rm{floor}\left[\frac{1}{3}\left\{YY-4 \times \rm{floor}\left(\frac{YY}{4}\right)+2\right\}\right]
N = N_1 - (N_2 \times N_3) + DD - 30

3. Convert the longitude to hour value and calculate an approximate time (t):
4. lngHour = \frac{1}{15}longitude
If rising time is desired then:
t = N + \left\{\frac{1}{24}\left(6-lngHour\right)\right\}
If setting time is desired then:
t = N + \left\{\frac{1}{24}\left(18-lngHour\right)\right\}

5. Calculate the Sun's mean anomaly (M):
6. M = (0.9856 \times t) - 3.289

7. Calculate the Sun's true longitude (L)
8. L = M + \left[1.916\times \mathrm{sin}\left(\mathrm{deg2rad}(M)\right)\right] + \left[0.020\times \mathrm{sin}\left(2\times \mathrm{deg2rad}(M)\right)\right] + 282.634
NOTE: L potentially needs to be adjusted into the range between 0 and 360 by adding/subtracting with 360.

9. Calculate the Sun's right ascension (RA)
10. RA & = \mathrm{atan}\left\{0.91764\times \mathrm{tan}\left(\mathrm{deg2rad}(L)\right)\right\}
NOTE: RA potentially needs to be adjusted into the range between 0 and 360 by adding/subtracting with 360.

Right ascension value needs to be in the same quadrant as $L$, such that:

Right ascension value needs to be converted into hours.
RA = \frac{RA}{15}

11. Calculate the Sun's declination (sinDec, cosDec)
12. sinDec = 0.39782 \times \mathrm{sin}\left(\mathrm{deg2rad}(L)\right)
cosDec = \mathrm{cos}\left(\mathrm{asin}(sinDec)\right)

13. Calculate the Sun's local hour angle (H)
NOTE: If cosH>1 then the sun never rises on this location (on the specified date). If cosH<-1 then the sun never sets on this location (on the specified date).

If rising time is desired then:
If setting time is desired then:

Convert H into hours:
H = \frac{H}{15}

15. Calculate local mean time of rising/setting (T)
16. T = H + RA - (0.06571 \times t) - 6.622
NOTE: T potentially needs to be adjusted into the range between 0 and 24 by adding/subtracting with 24.