]> git.mxchange.org Git - flightgear.git/blob - Time/sunpos.c
Working on correct real time sun lighting.
[flightgear.git] / Time / sunpos.c
1 /*
2  * sunpos.c
3  * kirk johnson
4  * july 1993
5  *
6  * code for calculating the position on the earth's surface for which
7  * the sun is directly overhead (adapted from _practical astronomy
8  * with your calculator, third edition_, peter duffett-smith,
9  * cambridge university press, 1988.)
10  *
11  * RCS $Id$
12  *
13  * Copyright (C) 1989, 1990, 1993, 1994, 1995 Kirk Lauritz Johnson
14  *
15  * Parts of the source code (as marked) are:
16  *   Copyright (C) 1989, 1990, 1991 by Jim Frost
17  *   Copyright (C) 1992 by Jamie Zawinski <jwz@lucid.com>
18  *
19  * Permission to use, copy, modify and freely distribute xearth for
20  * non-commercial and not-for-profit purposes is hereby granted
21  * without fee, provided that both the above copyright notice and this
22  * permission notice appear in all copies and in supporting
23  * documentation.
24  *
25  * The author makes no representations about the suitability of this
26  * software for any purpose. It is provided "as is" without express or
27  * implied warranty.
28  *
29  * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
30  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
32  * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
33  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
34  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
35  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36  *
37  * $Id$
38  * (Log is kept at end of this file)
39  */
40
41
42 #include <math.h>
43 #include <stdio.h>
44 #include <time.h>
45
46 #include "sunpos.h"
47 #include "../constants.h"
48 #undef E
49
50
51 /*
52  * the epoch upon which these astronomical calculations are based is
53  * 1990 january 0.0, 631065600 seconds since the beginning of the
54  * "unix epoch" (00:00:00 GMT, Jan. 1, 1970)
55  *
56  * given a number of seconds since the start of the unix epoch,
57  * DaysSinceEpoch() computes the number of days since the start of the
58  * astronomical epoch (1990 january 0.0)
59  */
60
61 #define EpochStart           (631065600)
62 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
63
64 /*
65  * assuming the apparent orbit of the sun about the earth is circular,
66  * the rate at which the orbit progresses is given by RadsPerDay --
67  * FG_2PI radians per orbit divided by 365.242191 days per year:
68  */
69
70 #define RadsPerDay (FG_2PI/365.242191)
71
72 /*
73  * details of sun's apparent orbit at epoch 1990.0 (after
74  * duffett-smith, table 6, section 46)
75  *
76  * Epsilon_g    (ecliptic longitude at epoch 1990.0) 279.403303 degrees
77  * OmegaBar_g   (ecliptic longitude of perigee)      282.768422 degrees
78  * Eccentricity (eccentricity of orbit)                0.016713
79  */
80
81 #define Epsilon_g    (279.403303*(FG_2PI/360))
82 #define OmegaBar_g   (282.768422*(FG_2PI/360))
83 #define Eccentricity (0.016713)
84
85 /*
86  * MeanObliquity gives the mean obliquity of the earth's axis at epoch
87  * 1990.0 (computed as 23.440592 degrees according to the method given
88  * in duffett-smith, section 27)
89  */
90 #define MeanObliquity (23.440592*(FG_2PI/360))
91
92 static double solve_keplers_equation(double);
93 static double sun_ecliptic_longitude(time_t);
94 static void   ecliptic_to_equatorial(double, double, double *, double *);
95 static double julian_date(int, int, int);
96 static double GST(time_t);
97
98 /*
99  * solve Kepler's equation via Newton's method
100  * (after duffett-smith, section 47)
101  */
102 static double solve_keplers_equation(double M) {
103     double E;
104     double delta;
105
106     E = M;
107     while (1) {
108         delta = E - Eccentricity*sin(E) - M;
109         if (fabs(delta) <= 1e-10) break;
110         E -= delta / (1 - Eccentricity*cos(E));
111     }
112
113     return E;
114 }
115
116
117 /* compute ecliptic longitude of sun (in radians) (after
118  * duffett-smith, section 47) */
119
120 static double sun_ecliptic_longitude(time_t ssue) {
121     /* time_t ssue;              seconds since unix epoch */
122     double D, N;
123     double M_sun, E;
124     double v;
125
126     D = DaysSinceEpoch(ssue);
127
128     N = RadsPerDay * D;
129     N = fmod(N, FG_2PI);
130     if (N < 0) N += FG_2PI;
131
132     M_sun = N + Epsilon_g - OmegaBar_g;
133     if (M_sun < 0) M_sun += FG_2PI;
134
135     E = solve_keplers_equation(M_sun);
136     v = 2 * atan(sqrt((1+Eccentricity)/(1-Eccentricity)) * tan(E/2));
137
138     return (v + OmegaBar_g);
139 }
140
141
142 /* convert from ecliptic to equatorial coordinates (after
143  * duffett-smith, section 27) */
144
145 static void ecliptic_to_equatorial(double lambda, double beta, 
146                                    double *alpha, double *delta) {
147     /* double  lambda;            ecliptic longitude       */
148     /* double  beta;              ecliptic latitude        */
149     /* double *alpha;             (return) right ascension */
150     /* double *delta;             (return) declination     */
151
152     double sin_e, cos_e;
153
154     sin_e = sin(MeanObliquity);
155     cos_e = cos(MeanObliquity);
156
157     *alpha = atan2(sin(lambda)*cos_e - tan(beta)*sin_e, cos(lambda));
158     *delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin(lambda));
159 }
160
161
162 /* computing julian dates (assuming gregorian calendar, thus this is
163  * only valid for dates of 1582 oct 15 or later) (after duffett-smith,
164  * section 4) */
165
166 static double julian_date(int y, int m, int d) {
167     /* int y;                    year (e.g. 19xx)          */
168     /* int m;                    month (jan=1, feb=2, ...) */
169     /* int d;                    day of month              */
170
171     int    A, B, C, D;
172     double JD;
173
174     /* lazy test to ensure gregorian calendar */
175     if (y < 1583) {
176         printf("WHOOPS! Julian dates only valid for 1582 oct 15 or later\n");
177     }
178
179     if ((m == 1) || (m == 2)) {
180         y -= 1;
181         m += 12;
182     }
183
184     A = y / 100;
185     B = 2 - A + (A / 4);
186     C = 365.25 * y;
187     D = 30.6001 * (m + 1);
188
189     JD = B + C + D + d + 1720994.5;
190
191     return JD;
192 }
193
194
195 /* compute greenwich mean sidereal time (GST) corresponding to a given
196  * number of seconds since the unix epoch (after duffett-smith,
197  * section 12) */
198 static double GST(time_t ssue) {
199     /* time_t ssue;           seconds since unix epoch */
200
201     double     JD;
202     double     T, T0;
203     double     UT;
204     struct tm *tm;
205
206     tm = gmtime(&ssue);
207
208     JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
209     T  = (JD - 2451545) / 36525;
210
211     T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
212
213     T0 = fmod(T0, 24.0);
214     if (T0 < 0) T0 += 24;
215
216     UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
217
218     T0 += UT * 1.002737909;
219     T0 = fmod(T0, 24.0);
220     if (T0 < 0) T0 += 24;
221
222     return T0;
223 }
224
225
226 /* given a particular time (expressed in seconds since the unix
227  * epoch), compute position on the earth (lat, lon) such that sun is
228  * directly overhead.  (lat, lon are reported in radians */
229
230 void fgSunPosition(time_t ssue, double *lon, double *lat) {
231     /* time_t  ssue;           seconds since unix epoch */
232     /* double *lat;            (return) latitude        */
233     /* double *lon;            (return) longitude       */
234
235     double lambda;
236     double alpha, delta;
237     double tmp;
238
239     lambda = sun_ecliptic_longitude(ssue);
240     ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta);
241
242     tmp = alpha - (FG_2PI/24)*GST(ssue);
243     if (tmp < -FG_PI) {
244         do tmp += FG_2PI;
245         while (tmp < -FG_PI);
246     } else if (tmp > FG_PI) {
247         do tmp -= FG_2PI;
248         while (tmp < -FG_PI);
249     }
250
251     *lon = tmp;
252     *lat = delta;
253 }
254
255
256 /* $Log$
257 /* Revision 1.2  1997/08/06 00:24:32  curt
258 /* Working on correct real time sun lighting.
259 /*
260  * Revision 1.1  1997/08/01 15:27:56  curt
261  * Initial revision.
262  *
263  */