1 // moonpos.cxx (basically, this is a slightly modified version of the 'sunpos.cxx' file, adapted from XEarth)
6 // code for calculating the position on the earth's surface for which
7 // the moon is directly overhead (adapted from _practical astronomy
8 // with your calculator, third edition_, peter duffett-smith,
9 // cambridge university press, 1988.)
11 // Copyright (C) 1989, 1990, 1993, 1994, 1995 Kirk Lauritz Johnson
13 // Parts of the source code (as marked) are:
14 // Copyright (C) 1989, 1990, 1991 by Jim Frost
15 // Copyright (C) 1992 by Jamie Zawinski <jwz@lucid.com>
17 // Permission to use, copy, modify and freely distribute xearth for
18 // non-commercial and not-for-profit purposes is hereby granted
19 // without fee, provided that both the above copyright notice and this
20 // permission notice appear in all copies and in supporting
23 // The author makes no representations about the suitability of this
24 // software for any purpose. It is provided "as is" without express or
27 // THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
28 // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
29 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
30 // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
31 // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
32 // NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
33 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42 #include "Include/compiler.h"
43 #ifdef FG_HAVE_STD_INCLUDES
54 //#include <Astro/orbits.hxx>
55 #include <Astro/solarsystem.hxx>
56 #include <Debug/logstream.hxx>
57 #include <Include/fg_constants.h>
58 #include <Main/views.hxx>
59 #include <Math/fg_geodesy.hxx>
60 #include <Math/mat3.h>
61 #include <Math/point3d.hxx>
62 #include <Math/polar3d.hxx>
63 #include <Math/vector.hxx>
64 #include <Scenery/scenery.hxx>
66 #include "fg_time.hxx"
67 #include "moonpos.hxx"
69 extern SolarSystem *solarSystem;
75 * the epoch upon which these astronomical calculations are based is
76 * 1990 january 0.0, 631065600 seconds since the beginning of the
77 * "unix epoch" (00:00:00 GMT, Jan. 1, 1970)
79 * given a number of seconds since the start of the unix epoch,
80 * DaysSinceEpoch() computes the number of days since the start of the
81 * astronomical epoch (1990 january 0.0)
84 #define EpochStart (631065600)
85 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
88 * assuming the apparent orbit of the moon about the earth is circular,
89 * the rate at which the orbit progresses is given by RadsPerDay --
90 * FG_2PI radians per orbit divided by 365.242191 days per year:
93 #define RadsPerDay (FG_2PI/365.242191)
96 * details of moon's apparent orbit at epoch 1990.0 (after
97 * duffett-smith, table 6, section 46)
99 * Epsilon_g (ecliptic longitude at epoch 1990.0) 279.403303 degrees
100 * OmegaBar_g (ecliptic longitude of perigee) 282.768422 degrees
101 * Eccentricity (eccentricity of orbit) 0.016713
104 #define Epsilon_g (279.403303*(FG_2PI/360))
105 #define OmegaBar_g (282.768422*(FG_2PI/360))
106 #define Eccentricity (0.016713)
109 * MeanObliquity gives the mean obliquity of the earth's axis at epoch
110 * 1990.0 (computed as 23.440592 degrees according to the method given
111 * in duffett-smith, section 27)
113 #define MeanObliquity (23.440592*(FG_2PI/360))
115 /* static double solve_keplers_equation(double); */
116 /* static double moon_ecliptic_longitude(time_t); */
117 static void ecliptic_to_equatorial(double, double, double *, double *);
118 static double julian_date(int, int, int);
119 static double GST(time_t);
122 * solve Kepler's equation via Newton's method
123 * (after duffett-smith, section 47)
126 static double solve_keplers_equation(double M) {
132 delta = E - Eccentricity*sin(E) - M;
133 if (fabs(delta) <= 1e-10) break;
134 E -= delta / (1 - Eccentricity*cos(E));
142 /* compute ecliptic longitude of moon (in radians) (after
143 * duffett-smith, section 47) */
145 static double moon_ecliptic_longitude(time_t ssue) {
146 // time_t ssue; // seconds since unix epoch
151 D = DaysSinceEpoch(ssue);
155 if (N < 0) N += FG_2PI;
157 M_moon = N + Epsilon_g - OmegaBar_g;
158 if (M_moon < 0) M_moon += FG_2PI;
160 E = solve_keplers_equation(M_moon);
161 v = 2 * atan(sqrt((1+Eccentricity)/(1-Eccentricity)) * tan(E/2));
163 return (v + OmegaBar_g);
168 /* convert from ecliptic to equatorial coordinates (after
169 * duffett-smith, section 27) */
171 static void ecliptic_to_equatorial(double lambda, double beta,
172 double *alpha, double *delta) {
173 /* double lambda; ecliptic longitude */
174 /* double beta; ecliptic latitude */
175 /* double *alpha; (return) right ascension */
176 /* double *delta; (return) declination */
181 sin_e = sin(MeanObliquity);
182 cos_e = cos(MeanObliquity);
186 *alpha = atan2(sin_l*cos_e - tan(beta)*sin_e, cos_l);
187 *delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin_l);
191 /* computing julian dates (assuming gregorian calendar, thus this is
192 * only valid for dates of 1582 oct 15 or later) (after duffett-smith,
195 static double julian_date(int y, int m, int d) {
196 /* int y; year (e.g. 19xx) */
197 /* int m; month (jan=1, feb=2, ...) */
198 /* int d; day of month */
203 /* lazy test to ensure gregorian calendar */
205 FG_LOG( FG_EVENT, FG_ALERT,
206 "WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
209 if ((m == 1) || (m == 2)) {
216 C = (int)(365.25 * y);
217 D = (int)(30.6001 * (m + 1));
219 JD = B + C + D + d + 1720994.5;
225 /* compute greenwich mean sidereal time (GST) corresponding to a given
226 * number of seconds since the unix epoch (after duffett-smith,
228 static double GST(time_t ssue) {
229 /* time_t ssue; seconds since unix epoch */
238 JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
239 T = (JD - 2451545) / 36525;
241 T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
244 if (T0 < 0) T0 += 24;
246 UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
248 T0 += UT * 1.002737909;
250 if (T0 < 0) T0 += 24;
256 /* given a particular time (expressed in seconds since the unix
257 * epoch), compute position on the earth (lat, lon) such that moon is
258 * directly overhead. (lat, lon are reported in radians */
260 void fgMoonPosition(time_t ssue, double *lon, double *lat) {
261 /* time_t ssue; seconds since unix epoch */
262 /* double *lat; (return) latitude */
263 /* double *lon; (return) longitude */
269 /* lambda = moon_ecliptic_longitude(ssue); */
270 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
271 //ecliptic_to_equatorial (solarPosition.lonMoon, 0.0, &alpha, &delta);
273 /* **********************************************************************
274 * NOTE: in the next function, each time the moon's position is updated, the
275 * the moon's longitude is returned from solarSystem->moon. Note that the
276 * moon's position is updated at a much higher frequency than the rate at
277 * which the solar system's rebuilds occur. This is not a problem, however,
278 * because the fgMoonPosition we're talking about here concerns the changing
279 * position of the moon due to the daily rotation of the earth.
280 * The ecliptic longitude, however, represents the position of the moon with
281 * respect to the stars, and completes just one cycle over the course of a
282 * year. Its therefore pretty safe to update the moon's longitude only once
283 * every ten minutes. (Comment added by Durk Talsma).
284 ************************************************************************/
286 ecliptic_to_equatorial( SolarSystem::theSolarSystem->getMoon()->getLon(),
287 0.0, &alpha, &delta );
288 tmp = alpha - (FG_2PI/24)*GST(ssue);
291 while (tmp < -FG_PI);
292 } else if (tmp > FG_PI) {
294 while (tmp < -FG_PI);
302 /* given a particular time expressed in side real time at prime
303 * meridian (GST), compute position on the earth (lat, lon) such that
304 * moon is directly overhead. (lat, lon are reported in radians */
306 static void fgMoonPositionGST(double gst, double *lon, double *lat) {
307 /* time_t ssue; seconds since unix epoch */
308 /* double *lat; (return) latitude */
309 /* double *lon; (return) longitude */
315 /* lambda = moon_ecliptic_longitude(ssue); */
316 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
317 //ecliptic_to_equatorial (solarPosition.lonMoon, 0.0, &alpha, &delta);
318 ecliptic_to_equatorial( SolarSystem::theSolarSystem->getMoon()->getLon(),
319 SolarSystem::theSolarSystem->getMoon()->getLat(),
322 // tmp = alpha - (FG_2PI/24)*GST(ssue);
323 tmp = alpha - (FG_2PI/24)*gst;
326 while (tmp < -FG_PI);
327 } else if (tmp > FG_PI) {
329 while (tmp < -FG_PI);
337 // update the cur_time_params structure with the current moon position
338 void fgUpdateMoonPos( void ) {
342 MAT3vec nup, nmoon, v0, surface_to_moon;
343 Point3D p, rel_moonpos;
344 double dot, east_dot;
345 double moon_gd_lat, sl_radius;
348 l = &cur_light_params;
349 t = FGTime::cur_time_params;
352 FG_LOG( FG_EVENT, FG_INFO, " Updating Moon position" );
354 // (not sure why there was two)
355 // fgMoonPosition(t->cur_time, &l->moon_lon, &moon_gd_lat);
356 fgMoonPositionGST(t->getGst(), &l->moon_lon, &moon_gd_lat);
358 fgGeodToGeoc(moon_gd_lat, 0.0, &sl_radius, &l->moon_gc_lat);
360 p = Point3D( l->moon_lon, l->moon_gc_lat, sl_radius );
361 l->fg_moonpos = fgPolarToCart3d(p);
363 FG_LOG( FG_EVENT, FG_INFO, " t->cur_time = " << t->get_cur_time() );
364 FG_LOG( FG_EVENT, FG_INFO,
365 " Moon Geodetic lat = " << moon_gd_lat
366 << " Geocentric lat = " << l->moon_gc_lat );
368 // I think this will work better for generating the moon light vector
369 l->moon_vec[0] = l->fg_moonpos.x();
370 l->moon_vec[1] = l->fg_moonpos.y();
371 l->moon_vec[2] = l->fg_moonpos.z();
372 MAT3_NORMALIZE_VEC(l->moon_vec, ntmp);
373 MAT3_SCALE_VEC(l->moon_vec_inv, l->moon_vec, -1.0);
375 // make sure these are directional light sources only
376 l->moon_vec[3] = 0.0;
377 l->moon_vec_inv[3] = 0.0;
379 // printf(" l->moon_vec = %.2f %.2f %.2f\n", l->moon_vec[0], l->moon_vec[1],
382 // calculate the moon's relative angle to local up
383 MAT3_COPY_VEC(nup, v->get_local_up());
384 nmoon[0] = l->fg_moonpos.x();
385 nmoon[1] = l->fg_moonpos.y();
386 nmoon[2] = l->fg_moonpos.z();
387 MAT3_NORMALIZE_VEC(nup, ntmp);
388 MAT3_NORMALIZE_VEC(nmoon, ntmp);
390 l->moon_angle = acos(MAT3_DOT_PRODUCT(nup, nmoon));
391 // printf(" MOON ANGLE relative to current location = %.3f rads.\n",
394 // calculate vector to moon's position on the earth's surface
395 rel_moonpos = l->fg_moonpos - (v->get_view_pos() + scenery.center);
396 v->set_to_moon( rel_moonpos.x(), rel_moonpos.y(), rel_moonpos.z() );
397 // printf( "Vector to moon = %.2f %.2f %.2f\n",
398 // v->to_moon[0], v->to_moon[1], v->to_moon[2]);
400 // make a vector to the current view position
401 Point3D view_pos = v->get_view_pos();
402 MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
404 // Given a vector from the view position to the point on the
405 // earth's surface the moon is directly over, map into onto the
406 // local plane representing "horizontal".
407 map_vec_onto_cur_surface_plane( v->get_local_up(), v0, v->get_to_moon(),
409 MAT3_NORMALIZE_VEC(surface_to_moon, ntmp);
410 v->set_surface_to_moon( surface_to_moon[0], surface_to_moon[1],
411 surface_to_moon[2] );
412 // printf("Surface direction to moon is %.2f %.2f %.2f\n",
413 // v->surface_to_moon[0], v->surface_to_moon[1], v->surface_to_moon[2]);
414 // printf("Should be close to zero = %.2f\n",
415 // MAT3_DOT_PRODUCT(v->local_up, v->surface_to_moon));
417 // calculate the angle between v->surface_to_moon and
418 // v->surface_east. We do this so we can sort out the acos()
419 // ambiguity. I wish I could think of a more efficient way ... :-(
420 east_dot = MAT3_DOT_PRODUCT( surface_to_moon, v->get_surface_east() );
421 // printf(" East dot product = %.2f\n", east_dot);
423 // calculate the angle between v->surface_to_moon and
424 // v->surface_south. this is how much we have to rotate the sky
425 // for it to align with the moon
426 dot = MAT3_DOT_PRODUCT( surface_to_moon, v->get_surface_south() );
427 // printf(" Dot product = %.2f\n", dot);
428 if ( east_dot >= 0 ) {
429 l->moon_rotation = acos(dot);
431 l->moon_rotation = -acos(dot);
433 // printf(" Sky needs to rotate = %.3f rads = %.1f degrees.\n",
434 // angle, angle * RAD_TO_DEG); */