1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* weather-moon.c - Lunar calculations for gweather
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; either version 2 of the
7
* License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, see
16
* <http://www.gnu.org/licenses/>.
21
* "Practical Astronomy With Your Calculator" (3e), Peter Duffett-Smith
22
* Cambridge University Press 1988
30
#include <sys/types.h>
38
#define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
39
#include "weather-priv.h"
42
* Elements of the Moon's orbit, epoch 2000 Jan 1.5
43
* http://ssd.jpl.nasa.gov/?sat_elem#earth
44
* The page only lists most values to 2 decimal places
47
#define LUNAR_MEAN_LONGITUDE 218.316
48
#define LUNAR_PERIGEE_MEAN_LONG 318.15
49
#define LUNAR_NODE_MEAN_LONG 125.08
50
#define LUNAR_PROGRESSION 13.176358
51
#define LUNAR_INCLINATION DEGREES_TO_RADIANS(5.145396)
55
* @info: WeatherInfo containing time_t of interest. The
56
* values moonphase, moonlatitude and moonValid are updated
59
* Returns: gboolean indicating success or failure.
60
* moonphase is expressed as degrees where '0' is a new moon,
61
* '90' is first quarter, etc.
65
calc_moon (WeatherInfo *info)
70
gdouble ndays, sunMeanAnom_d;
72
gdouble moonMeanAnom_d, moonMeanAnom_r;
73
gdouble sunEclipLong_r;
74
gdouble ascNodeMeanLong_d;
75
gdouble corrLong_d, eviction_d;
76
gdouble sinSunMeanAnom;
77
gdouble Ae, A3, Ec, A4, lN_r;
78
gdouble lambda_r, beta_r;
81
* The comments refer to the enumerated steps to calculate the
82
* position of the moon (section 65 of above reference)
85
ndays = EPOCH_TO_J2000(t) / 86400.;
86
sunMeanAnom_d = fmod (MEAN_ECLIPTIC_LONGITUDE (ndays) - PERIGEE_LONGITUDE (ndays),
88
sunEclipLong_r = sunEclipLongitude (t);
89
moonLong_d = fmod (LUNAR_MEAN_LONGITUDE + (ndays * LUNAR_PROGRESSION),
91
/* 5: moon's mean anomaly */
92
moonMeanAnom_d = fmod ((moonLong_d - (0.1114041 * ndays)
93
- (LUNAR_PERIGEE_MEAN_LONG + LUNAR_NODE_MEAN_LONG)),
95
/* 6: ascending node mean longitude */
96
ascNodeMeanLong_d = fmod (LUNAR_NODE_MEAN_LONG - (0.0529539 * ndays),
98
eviction_d = 1.2739 /* 7: eviction */
99
* sin (DEGREES_TO_RADIANS (2.0 * (moonLong_d - RADIANS_TO_DEGREES (sunEclipLong_r))
101
sinSunMeanAnom = sin (DEGREES_TO_RADIANS (sunMeanAnom_d));
102
Ae = 0.1858 * sinSunMeanAnom;
103
A3 = 0.37 * sinSunMeanAnom; /* 8: annual equation */
104
moonMeanAnom_d += eviction_d - Ae - A3; /* 9: "third correction" */
105
moonMeanAnom_r = DEGREES_TO_RADIANS (moonMeanAnom_d);
106
Ec = 6.2886 * sin (moonMeanAnom_r); /* 10: equation of center */
107
A4 = 0.214 * sin (2.0 * moonMeanAnom_r); /* 11: "yet another correction" */
109
/* Steps 12-14 give the true longitude after correcting for variation */
110
moonLong_d += eviction_d + Ec - Ae + A4
111
+ (0.6583 * sin (2.0 * (moonMeanAnom_r - sunEclipLong_r)));
113
/* 15: corrected longitude of node */
114
corrLong_d = ascNodeMeanLong_d - 0.16 * sinSunMeanAnom;
117
* Calculate ecliptic latitude (16-19) and longitude (20) of the moon,
118
* then convert to right ascension and declination.
120
lN_r = DEGREES_TO_RADIANS (moonLong_d - corrLong_d); /* l''-N' */
121
lambda_r = DEGREES_TO_RADIANS(corrLong_d)
122
+ atan2 (sin (lN_r) * cos (LUNAR_INCLINATION), cos (lN_r));
123
beta_r = asin (sin (lN_r) * sin (LUNAR_INCLINATION));
124
ecl2equ (t, lambda_r, beta_r, &ra_h, &decl_r);
127
* The phase is the angle from the sun's longitude to the moon's
130
fmod (15.*ra_h - RADIANS_TO_DEGREES (sunEclipLongitude (info->update)),
132
if (info->moonphase < 0)
133
info->moonphase += 360;
134
info->moonlatitude = RADIANS_TO_DEGREES (decl_r);
135
info->moonValid = TRUE;
143
* @info: WeatherInfo containing the time_t of interest
144
* @phases: An array of four time_t values that will hold the returned values.
145
* The values are estimates of the time of the next new, quarter, full and
146
* three-quarter moons.
148
* Returns: gboolean indicating success or failure
152
calc_moon_phases (WeatherInfo *info, time_t *phases)
161
g_return_val_if_fail (info != NULL &&
162
(info->moonValid || calc_moon (info)),
166
memset(&temp, 0, sizeof(WeatherInfo));
168
for (idx = 0; idx < 4; idx++) {
169
temp.update = info->update;
170
temp.moonphase = info->moonphase;
173
* First estimate on how far the moon needs to advance
174
* to get to the required phase
176
advance = (idx * 90.) - info->moonphase;
180
for (iter = 0; iter < 10; iter++) {
181
/* Convert angle change (degrees) to dtime (seconds) */
182
dtime = advance / LUNAR_PROGRESSION * 86400.;
183
if ((dtime > -10) && (dtime < 10))
185
temp.update += dtime;
186
(void)calc_moon (&temp);
188
if (idx == 0 && temp.moonphase > 180.) {
189
advance = 360. - temp.moonphase;
191
advance = (idx * 90.) - temp.moonphase;
194
*ptime++ = temp.update;