]> git.mxchange.org Git - flightgear.git/blob - Time/sunpos.c
Prepended "fg" on the name of all global structures that didn't have it yet.
[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 "fg_time.h"
48 #include "../constants.h"
49 #include "../Main/views.h"
50 #include "../Math/fg_geodesy.h"
51 #include "../Math/mat3.h"
52 #include "../Math/polar.h"
53
54 #undef E
55
56
57 /*
58  * the epoch upon which these astronomical calculations are based is
59  * 1990 january 0.0, 631065600 seconds since the beginning of the
60  * "unix epoch" (00:00:00 GMT, Jan. 1, 1970)
61  *
62  * given a number of seconds since the start of the unix epoch,
63  * DaysSinceEpoch() computes the number of days since the start of the
64  * astronomical epoch (1990 january 0.0)
65  */
66
67 #define EpochStart           (631065600)
68 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
69
70 /*
71  * assuming the apparent orbit of the sun about the earth is circular,
72  * the rate at which the orbit progresses is given by RadsPerDay --
73  * FG_2PI radians per orbit divided by 365.242191 days per year:
74  */
75
76 #define RadsPerDay (FG_2PI/365.242191)
77
78 /*
79  * details of sun's apparent orbit at epoch 1990.0 (after
80  * duffett-smith, table 6, section 46)
81  *
82  * Epsilon_g    (ecliptic longitude at epoch 1990.0) 279.403303 degrees
83  * OmegaBar_g   (ecliptic longitude of perigee)      282.768422 degrees
84  * Eccentricity (eccentricity of orbit)                0.016713
85  */
86
87 #define Epsilon_g    (279.403303*(FG_2PI/360))
88 #define OmegaBar_g   (282.768422*(FG_2PI/360))
89 #define Eccentricity (0.016713)
90
91 /*
92  * MeanObliquity gives the mean obliquity of the earth's axis at epoch
93  * 1990.0 (computed as 23.440592 degrees according to the method given
94  * in duffett-smith, section 27)
95  */
96 #define MeanObliquity (23.440592*(FG_2PI/360))
97
98 static double solve_keplers_equation(double);
99 static double sun_ecliptic_longitude(time_t);
100 static void   ecliptic_to_equatorial(double, double, double *, double *);
101 static double julian_date(int, int, int);
102 static double GST(time_t);
103
104 /*
105  * solve Kepler's equation via Newton's method
106  * (after duffett-smith, section 47)
107  */
108 static double solve_keplers_equation(double M) {
109     double E;
110     double delta;
111
112     E = M;
113     while (1) {
114         delta = E - Eccentricity*sin(E) - M;
115         if (fabs(delta) <= 1e-10) break;
116         E -= delta / (1 - Eccentricity*cos(E));
117     }
118
119     return E;
120 }
121
122
123 /* compute ecliptic longitude of sun (in radians) (after
124  * duffett-smith, section 47) */
125
126 static double sun_ecliptic_longitude(time_t ssue) {
127     /* time_t ssue;              seconds since unix epoch */
128     double D, N;
129     double M_sun, E;
130     double v;
131
132     D = DaysSinceEpoch(ssue);
133
134     N = RadsPerDay * D;
135     N = fmod(N, FG_2PI);
136     if (N < 0) N += FG_2PI;
137
138     M_sun = N + Epsilon_g - OmegaBar_g;
139     if (M_sun < 0) M_sun += FG_2PI;
140
141     E = solve_keplers_equation(M_sun);
142     v = 2 * atan(sqrt((1+Eccentricity)/(1-Eccentricity)) * tan(E/2));
143
144     return (v + OmegaBar_g);
145 }
146
147
148 /* convert from ecliptic to equatorial coordinates (after
149  * duffett-smith, section 27) */
150
151 static void ecliptic_to_equatorial(double lambda, double beta, 
152                                    double *alpha, double *delta) {
153     /* double  lambda;            ecliptic longitude       */
154     /* double  beta;              ecliptic latitude        */
155     /* double *alpha;             (return) right ascension */
156     /* double *delta;             (return) declination     */
157
158     double sin_e, cos_e;
159
160     sin_e = sin(MeanObliquity);
161     cos_e = cos(MeanObliquity);
162
163     *alpha = atan2(sin(lambda)*cos_e - tan(beta)*sin_e, cos(lambda));
164     *delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin(lambda));
165 }
166
167
168 /* computing julian dates (assuming gregorian calendar, thus this is
169  * only valid for dates of 1582 oct 15 or later) (after duffett-smith,
170  * section 4) */
171
172 static double julian_date(int y, int m, int d) {
173     /* int y;                    year (e.g. 19xx)          */
174     /* int m;                    month (jan=1, feb=2, ...) */
175     /* int d;                    day of month              */
176
177     int    A, B, C, D;
178     double JD;
179
180     /* lazy test to ensure gregorian calendar */
181     if (y < 1583) {
182         printf("WHOOPS! Julian dates only valid for 1582 oct 15 or later\n");
183     }
184
185     if ((m == 1) || (m == 2)) {
186         y -= 1;
187         m += 12;
188     }
189
190     A = y / 100;
191     B = 2 - A + (A / 4);
192     C = 365.25 * y;
193     D = 30.6001 * (m + 1);
194
195     JD = B + C + D + d + 1720994.5;
196
197     return JD;
198 }
199
200
201 /* compute greenwich mean sidereal time (GST) corresponding to a given
202  * number of seconds since the unix epoch (after duffett-smith,
203  * section 12) */
204 static double GST(time_t ssue) {
205     /* time_t ssue;           seconds since unix epoch */
206
207     double     JD;
208     double     T, T0;
209     double     UT;
210     struct tm *tm;
211
212     tm = gmtime(&ssue);
213
214     JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
215     T  = (JD - 2451545) / 36525;
216
217     T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
218
219     T0 = fmod(T0, 24.0);
220     if (T0 < 0) T0 += 24;
221
222     UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
223
224     T0 += UT * 1.002737909;
225     T0 = fmod(T0, 24.0);
226     if (T0 < 0) T0 += 24;
227
228     return T0;
229 }
230
231
232 /* given a particular time (expressed in seconds since the unix
233  * epoch), compute position on the earth (lat, lon) such that sun is
234  * directly overhead.  (lat, lon are reported in radians */
235
236 void fgSunPosition(time_t ssue, double *lon, double *lat) {
237     /* time_t  ssue;           seconds since unix epoch */
238     /* double *lat;            (return) latitude        */
239     /* double *lon;            (return) longitude       */
240
241     double lambda;
242     double alpha, delta;
243     double tmp;
244
245     lambda = sun_ecliptic_longitude(ssue);
246     ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta);
247
248     tmp = alpha - (FG_2PI/24)*GST(ssue);
249     if (tmp < -FG_PI) {
250         do tmp += FG_2PI;
251         while (tmp < -FG_PI);
252     } else if (tmp > FG_PI) {
253         do tmp -= FG_2PI;
254         while (tmp < -FG_PI);
255     }
256
257     *lon = tmp;
258     *lat = delta;
259 }
260
261
262 /* update the cur_time_params structure with the current sun position */
263 void fgUpdateSunPos(struct fgCartesianPoint scenery_center) {
264     struct fgLIGHT *l;
265     struct fgTIME *t;
266     struct fgVIEW *v;
267     MAT3vec nup, nsun;
268     double sun_gd_lat, sl_radius, temp;
269     static int time_warp = 0;
270
271     l = &cur_light_params;
272     t = &cur_time_params;
273     v = &current_view;
274
275     time_warp += 0; /* increase this to make the world spin real fast */
276
277     fgSunPosition(t->cur_time + time_warp, &l->sun_lon, &sun_gd_lat);
278
279     fgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
280
281     l->fg_sunpos = fgPolarToCart(l->sun_lon, l->sun_gc_lat, sl_radius);
282
283     /* printf("Geodetic lat = %.5f Geocentric lat = %.5f\n", sun_gd_lat,
284        t->sun_gc_lat); */
285
286     /* the sun position has to be translated just like everything else */
287     l->sun_vec_inv[0] = l->fg_sunpos.x - scenery_center.x; 
288     l->sun_vec_inv[1] = l->fg_sunpos.y - scenery_center.y;
289     l->sun_vec_inv[2] = l->fg_sunpos.z - scenery_center.z;
290     MAT3_SCALE_VEC(l->sun_vec, l->sun_vec_inv, -1.0);
291
292     /* make these are directional light sources only */
293     l->sun_vec[3] = 0.0;
294     l->sun_vec_inv[3] = 0.0;
295
296     /* calculate thesun's relative angle to local up */
297     MAT3_COPY_VEC(nup, v->local_up);
298     nsun[0] = l->fg_sunpos.x; 
299     nsun[1] = l->fg_sunpos.y;
300     nsun[2] = l->fg_sunpos.z;
301     MAT3_NORMALIZE_VEC(nup, temp);
302     MAT3_NORMALIZE_VEC(nsun, temp);
303
304     l->sun_angle = acos(MAT3_DOT_PRODUCT(nup, nsun));
305     printf("SUN ANGLE relative to current location = %.3f rads.\n", 
306            l->sun_angle);
307 }
308
309
310 /* $Log$
311 /* Revision 1.15  1997/12/10 22:37:55  curt
312 /* Prepended "fg" on the name of all global structures that didn't have it yet.
313 /* i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
314 /*
315  * Revision 1.14  1997/12/09 04:25:39  curt
316  * Working on adding a global lighting params structure.
317  *
318  * Revision 1.13  1997/11/25 19:25:42  curt
319  * Changes to integrate Durk's moon/sun code updates + clean up.
320  *
321  * Revision 1.12  1997/11/15 18:15:39  curt
322  * Reverse direction of sun vector, so object normals can be more normal.
323  *
324  * Revision 1.11  1997/10/28 21:07:21  curt
325  * Changed GLUT/ -> Main/
326  *
327  * Revision 1.10  1997/09/13 02:00:09  curt
328  * Mostly working on stars and generating sidereal time for accurate star
329  * placement.
330  *
331  * Revision 1.9  1997/09/05 14:17:31  curt
332  * More tweaking with stars.
333  *
334  * Revision 1.8  1997/09/05 01:36:04  curt
335  * Working on getting stars right.
336  *
337  * Revision 1.7  1997/09/04 02:17:40  curt
338  * Shufflin' stuff.
339  *
340  * Revision 1.6  1997/08/27 03:30:37  curt
341  * Changed naming scheme of basic shared structures.
342  *
343  * Revision 1.5  1997/08/22 21:34:41  curt
344  * Doing a bit of reorganizing and house cleaning.
345  *
346  * Revision 1.4  1997/08/19 23:55:09  curt
347  * Worked on better simulating real lighting.
348  *
349  * Revision 1.3  1997/08/13 20:23:49  curt
350  * The interface to sunpos now updates a global structure rather than returning
351  * current sun position.
352  *
353  * Revision 1.2  1997/08/06 00:24:32  curt
354  * Working on correct real time sun lighting.
355  *
356  * Revision 1.1  1997/08/01 15:27:56  curt
357  * Initial revision.
358  *
359  */