]> git.mxchange.org Git - flightgear.git/blob - Time/sunpos.c
074265d876bc6eb5f7714d700b15fcb480a372b6
[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 "../Math/fg_geodesy.h"
50 #include "../Math/polar.h"
51
52 #undef E
53
54
55 /*
56  * the epoch upon which these astronomical calculations are based is
57  * 1990 january 0.0, 631065600 seconds since the beginning of the
58  * "unix epoch" (00:00:00 GMT, Jan. 1, 1970)
59  *
60  * given a number of seconds since the start of the unix epoch,
61  * DaysSinceEpoch() computes the number of days since the start of the
62  * astronomical epoch (1990 january 0.0)
63  */
64
65 #define EpochStart           (631065600)
66 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
67
68 /*
69  * assuming the apparent orbit of the sun about the earth is circular,
70  * the rate at which the orbit progresses is given by RadsPerDay --
71  * FG_2PI radians per orbit divided by 365.242191 days per year:
72  */
73
74 #define RadsPerDay (FG_2PI/365.242191)
75
76 /*
77  * details of sun's apparent orbit at epoch 1990.0 (after
78  * duffett-smith, table 6, section 46)
79  *
80  * Epsilon_g    (ecliptic longitude at epoch 1990.0) 279.403303 degrees
81  * OmegaBar_g   (ecliptic longitude of perigee)      282.768422 degrees
82  * Eccentricity (eccentricity of orbit)                0.016713
83  */
84
85 #define Epsilon_g    (279.403303*(FG_2PI/360))
86 #define OmegaBar_g   (282.768422*(FG_2PI/360))
87 #define Eccentricity (0.016713)
88
89 /*
90  * MeanObliquity gives the mean obliquity of the earth's axis at epoch
91  * 1990.0 (computed as 23.440592 degrees according to the method given
92  * in duffett-smith, section 27)
93  */
94 #define MeanObliquity (23.440592*(FG_2PI/360))
95
96 static double solve_keplers_equation(double);
97 static double sun_ecliptic_longitude(time_t);
98 static void   ecliptic_to_equatorial(double, double, double *, double *);
99 static double julian_date(int, int, int);
100 static double GST(time_t);
101
102 /*
103  * solve Kepler's equation via Newton's method
104  * (after duffett-smith, section 47)
105  */
106 static double solve_keplers_equation(double M) {
107     double E;
108     double delta;
109
110     E = M;
111     while (1) {
112         delta = E - Eccentricity*sin(E) - M;
113         if (fabs(delta) <= 1e-10) break;
114         E -= delta / (1 - Eccentricity*cos(E));
115     }
116
117     return E;
118 }
119
120
121 /* compute ecliptic longitude of sun (in radians) (after
122  * duffett-smith, section 47) */
123
124 static double sun_ecliptic_longitude(time_t ssue) {
125     /* time_t ssue;              seconds since unix epoch */
126     double D, N;
127     double M_sun, E;
128     double v;
129
130     D = DaysSinceEpoch(ssue);
131
132     N = RadsPerDay * D;
133     N = fmod(N, FG_2PI);
134     if (N < 0) N += FG_2PI;
135
136     M_sun = N + Epsilon_g - OmegaBar_g;
137     if (M_sun < 0) M_sun += FG_2PI;
138
139     E = solve_keplers_equation(M_sun);
140     v = 2 * atan(sqrt((1+Eccentricity)/(1-Eccentricity)) * tan(E/2));
141
142     return (v + OmegaBar_g);
143 }
144
145
146 /* convert from ecliptic to equatorial coordinates (after
147  * duffett-smith, section 27) */
148
149 static void ecliptic_to_equatorial(double lambda, double beta, 
150                                    double *alpha, double *delta) {
151     /* double  lambda;            ecliptic longitude       */
152     /* double  beta;              ecliptic latitude        */
153     /* double *alpha;             (return) right ascension */
154     /* double *delta;             (return) declination     */
155
156     double sin_e, cos_e;
157
158     sin_e = sin(MeanObliquity);
159     cos_e = cos(MeanObliquity);
160
161     *alpha = atan2(sin(lambda)*cos_e - tan(beta)*sin_e, cos(lambda));
162     *delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin(lambda));
163 }
164
165
166 /* computing julian dates (assuming gregorian calendar, thus this is
167  * only valid for dates of 1582 oct 15 or later) (after duffett-smith,
168  * section 4) */
169
170 static double julian_date(int y, int m, int d) {
171     /* int y;                    year (e.g. 19xx)          */
172     /* int m;                    month (jan=1, feb=2, ...) */
173     /* int d;                    day of month              */
174
175     int    A, B, C, D;
176     double JD;
177
178     /* lazy test to ensure gregorian calendar */
179     if (y < 1583) {
180         printf("WHOOPS! Julian dates only valid for 1582 oct 15 or later\n");
181     }
182
183     if ((m == 1) || (m == 2)) {
184         y -= 1;
185         m += 12;
186     }
187
188     A = y / 100;
189     B = 2 - A + (A / 4);
190     C = 365.25 * y;
191     D = 30.6001 * (m + 1);
192
193     JD = B + C + D + d + 1720994.5;
194
195     return JD;
196 }
197
198
199 /* compute greenwich mean sidereal time (GST) corresponding to a given
200  * number of seconds since the unix epoch (after duffett-smith,
201  * section 12) */
202 static double GST(time_t ssue) {
203     /* time_t ssue;           seconds since unix epoch */
204
205     double     JD;
206     double     T, T0;
207     double     UT;
208     struct tm *tm;
209
210     tm = gmtime(&ssue);
211
212     JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
213     T  = (JD - 2451545) / 36525;
214
215     T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
216
217     T0 = fmod(T0, 24.0);
218     if (T0 < 0) T0 += 24;
219
220     UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
221
222     T0 += UT * 1.002737909;
223     T0 = fmod(T0, 24.0);
224     if (T0 < 0) T0 += 24;
225
226     return T0;
227 }
228
229
230 /* given a particular time (expressed in seconds since the unix
231  * epoch), compute position on the earth (lat, lon) such that sun is
232  * directly overhead.  (lat, lon are reported in radians */
233
234 void fgSunPosition(time_t ssue, double *lon, double *lat) {
235     /* time_t  ssue;           seconds since unix epoch */
236     /* double *lat;            (return) latitude        */
237     /* double *lon;            (return) longitude       */
238
239     double lambda;
240     double alpha, delta;
241     double tmp;
242
243     lambda = sun_ecliptic_longitude(ssue);
244     ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta);
245
246     tmp = alpha - (FG_2PI/24)*GST(ssue);
247     if (tmp < -FG_PI) {
248         do tmp += FG_2PI;
249         while (tmp < -FG_PI);
250     } else if (tmp > FG_PI) {
251         do tmp -= FG_2PI;
252         while (tmp < -FG_PI);
253     }
254
255     *lon = tmp;
256     *lat = delta;
257 }
258
259
260 /* update the cur_time_params structure with the current sun position */
261 void fgUpdateSunPos() {
262     struct fgTIME *t;
263     double sun_gd_lat, sl_radius;
264     static int time_warp = 0;
265
266     t = &cur_time_params;
267
268     time_warp += 200; /* increase this to make the world spin real fast */
269
270     fgSunPosition(time(NULL) + time_warp, &t->sun_lon, &sun_gd_lat);
271
272     fgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &t->sun_gc_lat);
273
274     t->fg_sunpos = fgPolarToCart(t->sun_lon, t->sun_gc_lat, sl_radius);
275
276     /* printf("Geodetic lat = %.5f Geocentric lat = %.5f\n", sun_gd_lat,
277        t->sun_gc_lat); */
278 }
279
280
281 /* $Log$
282 /* Revision 1.6  1997/08/27 03:30:37  curt
283 /* Changed naming scheme of basic shared structures.
284 /*
285  * Revision 1.5  1997/08/22 21:34:41  curt
286  * Doing a bit of reorganizing and house cleaning.
287  *
288  * Revision 1.4  1997/08/19 23:55:09  curt
289  * Worked on better simulating real lighting.
290  *
291  * Revision 1.3  1997/08/13 20:23:49  curt
292  * The interface to sunpos now updates a global structure rather than returning
293  * current sun position.
294  *
295  * Revision 1.2  1997/08/06 00:24:32  curt
296  * Working on correct real time sun lighting.
297  *
298  * Revision 1.1  1997/08/01 15:27:56  curt
299  * Initial revision.
300  *
301  */