wmail: fixed comparisons between signed and unsigned.
[dockapps.git] / wmmoonclock / src / Moon.c
blob6cc4e211fceb81ae9318956889606030c75c5373
1 /*
2 * Calculates Moon's position according to Brown's Lunar Theory...
3 */
5 #include <math.h>
6 #define DegPerRad 57.29577951308232087680
7 #define RadPerDeg 0.01745329251994329576
9 double angle360();
11 void addthe(double, double, double, double, double*, double*);
12 void addsol(double, double, double, double, int, int, int, int);
13 void addn(double, int, int, int, int);
14 void term(int, int, int, int, double*, double*);
16 double TwoPi = 6.283185308;
17 double ARC = 206264.81;
18 double sine(), frac();
19 double DLAM, DLAMS;
20 double DS;
21 double GAM1C;
22 double SINPI;
23 double N;
24 double CO[14][5], SI[14][5];
26 double Moon(double T, double *LAMBDA, double *BETA, double *R, double *AGE){
28 double T2;
29 double S1, S2, S3, S4, S5, S6, S7;
30 double DL0, DL, DD, DGAM, DLS, DF;
31 double L0, L, LS, F, D;
32 double ARG = 0.0, FAC = 0.0;
33 int MAX = 0, i, j;
34 double S;
37 T2 = T*T;
38 DLAM = 0.0, DS = 0.0, GAM1C = 0.0; SINPI = 3422.7000;
41 * Long Periodic variations
43 S1 = sine( 0.19833 + 0.05611*T );
44 S2 = sine( 0.27869 + 0.04508*T );
45 S3 = sine( 0.16827 - 0.36903*T );
46 S4 = sine( 0.34734 - 5.37261*T );
47 S5 = sine( 0.10498 - 5.37899*T );
48 S6 = sine( 0.42681 - 0.41855*T );
49 S7 = sine( 0.14943 - 5.37511*T );
50 DL0 = 0.84*S1 + 0.31*S2 + 14.27*S3 + 7.26*S4 + 0.28*S5 + 0.24*S6;
51 DL = 2.94*S1 + 0.31*S2 + 14.27*S3 + 9.34*S4 + 1.12*S5 + 0.83*S6;
52 DLS = -6.40*S1 - 1.89*S6;
53 DF = 0.21*S1 + 0.31*S2 + 14.27*S3 - 88.70*S4 - 15.30*S5 + 0.24*S6 - 1.86*S7;
54 DD = DL0 - DLS;
55 DGAM = -3332e-9 * sine( 0.59734 - 5.37261*T)
56 -539e-9 * sine( 0.35498 - 5.37899*T)
57 -64e-9 * sine( 0.39943 - 5.37511*T);
61 L0 = TwoPi*frac( 0.60643382 + 1336.85522467*T - 0.00000313*T2 ) + DL0/ARC;
62 L = TwoPi*frac( 0.37489701 + 1325.55240982*T + 0.00002565*T2 ) + DL/ARC;
63 LS = TwoPi*frac( 0.99312619 + 99.99735956*T - 0.00000044*T2 ) + DLS/ARC;
64 F = TwoPi*frac( 0.25909118 + 1342.22782980*T - 0.00000892*T2 ) + DF/ARC;
65 D = TwoPi*frac( 0.82736186 + 1236.85308708*T - 0.00000397*T2 ) + DD/ARC;
67 for (i=1; i<=4; ++i){
68 switch (i){
69 case 1:
70 ARG = L, MAX = 4, FAC = 1.000002208;
71 break;
72 case 2:
73 ARG = LS, MAX = 3, FAC = 0.997504612 - 0.002495388*T;
74 break;
75 case 3:
76 ARG = F, MAX = 4, FAC = 1.000002708 + 139.978*DGAM;
77 break;
78 case 4:
79 ARG = D, MAX = 6, FAC = 1.0;
80 break;
83 CO[6+0][i] = 1.0, CO[6+1][i] = cos(ARG)*FAC;
84 SI[6+0][i] = 0.0, SI[6+1][i] = sin(ARG)*FAC;
85 for (j=2; j<=MAX; ++j) addthe(CO[6+j-1][i], SI[6+j-1][i], CO[6+1][i], SI[6+1][i], &CO[6+j][i], &SI[6+j][i]);
86 for (j=1; j<=MAX; ++j) {
87 CO[6-j][i] = CO[6+j][i];
88 SI[6-j][i] = -SI[6+j][i];
97 * Solar1
99 addsol( 13.902, 14.06,-0.001, 0.2607,0, 0, 0, 4);
100 addsol( 0.403, -4.01,+0.394, 0.0023,0, 0, 0, 3);
101 addsol( 2369.912, 2373.36,+0.601, 28.2333,0, 0, 0, 2);
102 addsol( -125.154, -112.79,-0.725, -0.9781,0, 0, 0, 1);
103 addsol( 1.979, 6.98,-0.445, 0.0433,1, 0, 0, 4);
104 addsol( 191.953, 192.72,+0.029, 3.0861,1, 0, 0, 2);
105 addsol( -8.466, -13.51,+0.455, -0.1093,1, 0, 0, 1);
106 addsol(22639.500,22609.07,+0.079, 186.5398,1, 0, 0, 0);
107 addsol( 18.609, 3.59,-0.094, 0.0118,1, 0, 0,-1);
108 addsol(-4586.465,-4578.13,-0.077, 34.3117,1, 0, 0,-2);
109 addsol( +3.215, 5.44,+0.192, -0.0386,1, 0, 0,-3);
110 addsol( -38.428, -38.64,+0.001, 0.6008,1, 0, 0,-4);
111 addsol( -0.393, -1.43,-0.092, 0.0086,1, 0, 0,-6);
112 addsol( -0.289, -1.59,+0.123, -0.0053,0, 1, 0, 4);
113 addsol( -24.420, -25.10,+0.040, -0.3000,0, 1, 0, 2);
114 addsol( 18.023, 17.93,+0.007, 0.1494,0, 1, 0, 1);
115 addsol( -668.146, -126.98,-1.302, -0.3997,0, 1, 0, 0);
116 addsol( 0.560, 0.32,-0.001, -0.0037,0, 1, 0,-1);
117 addsol( -165.145, -165.06,+0.054, 1.9178,0, 1, 0,-2);
118 addsol( -1.877, -6.46,-0.416, 0.0339,0, 1, 0,-4);
119 addsol( 0.213, 1.02,-0.074, 0.0054,2, 0, 0, 4);
120 addsol( 14.387, 14.78,-0.017, 0.2833,2, 0, 0, 2);
121 addsol( -0.586, -1.20,+0.054, -0.0100,2, 0, 0, 1);
122 addsol( 769.016, 767.96,+0.107, 10.1657,2, 0, 0, 0);
123 addsol( +1.750, 2.01,-0.018, 0.0155,2, 0, 0,-1);
124 addsol( -211.656, -152.53,+5.679, -0.3039,2, 0, 0,-2);
125 addsol( +1.225, 0.91,-0.030, -0.0088,2, 0, 0,-3);
126 addsol( -30.773, -34.07,-0.308, 0.3722,2, 0, 0,-4);
127 addsol( -0.570, -1.40,-0.074, 0.0109,2, 0, 0,-6);
128 addsol( -2.921, -11.75,+0.787, -0.0484,1, 1, 0, 2);
129 addsol( +1.267, 1.52,-0.022, 0.0164,1, 1, 0, 1);
130 addsol( -109.673, -115.18,+0.461, -0.9490,1, 1, 0, 0);
131 addsol( -205.962, -182.36,+2.056, +1.4437,1, 1, 0,-2);
132 addsol( 0.233, 0.36, 0.012, -0.0025,1, 1, 0,-3);
133 addsol( -4.391, -9.66,-0.471, 0.0673,1, 1, 0,-4);
137 * Solar2
139 addsol( 0.283, 1.53,-0.111, +0.0060,1,-1, 0,+4);
140 addsol( 14.577, 31.70,-1.540, +0.2302,1,-1, 0, 2);
141 addsol( 147.687, 138.76,+0.679, +1.1528,1,-1, 0, 0);
142 addsol( -1.089, 0.55,+0.021, 0.0 ,1,-1, 0,-1);
143 addsol( 28.475, 23.59,-0.443, -0.2257,1,-1, 0,-2);
144 addsol( -0.276, -0.38,-0.006, -0.0036,1,-1, 0,-3);
145 addsol( 0.636, 2.27,+0.146, -0.0102,1,-1, 0,-4);
146 addsol( -0.189, -1.68,+0.131, -0.0028,0, 2, 0, 2);
147 addsol( -7.486, -0.66,-0.037, -0.0086,0, 2, 0, 0);
148 addsol( -8.096, -16.35,-0.740, 0.0918,0, 2, 0,-2);
149 addsol( -5.741, -0.04, 0.0 , -0.0009,0, 0, 2, 2);
150 addsol( 0.255, 0.0 , 0.0 , 0.0 ,0, 0, 2, 1);
151 addsol( -411.608, -0.20, 0.0 , -0.0124,0, 0, 2, 0);
152 addsol( 0.584, 0.84, 0.0 , +0.0071,0, 0, 2,-1);
153 addsol( -55.173, -52.14, 0.0 , -0.1052,0, 0, 2,-2);
154 addsol( 0.254, 0.25, 0.0 , -0.0017,0, 0, 2,-3);
155 addsol( +0.025, -1.67, 0.0 , +0.0031,0, 0, 2,-4);
156 addsol( 1.060, 2.96,-0.166, 0.0243,3, 0, 0,+2);
157 addsol( 36.124, 50.64,-1.300, 0.6215,3, 0, 0, 0);
158 addsol( -13.193, -16.40,+0.258, -0.1187,3, 0, 0,-2);
159 addsol( -1.187, -0.74,+0.042, 0.0074,3, 0, 0,-4);
160 addsol( -0.293, -0.31,-0.002, 0.0046,3, 0, 0,-6);
161 addsol( -0.290, -1.45,+0.116, -0.0051,2, 1, 0, 2);
162 addsol( -7.649, -10.56,+0.259, -0.1038,2, 1, 0, 0);
163 addsol( -8.627, -7.59,+0.078, -0.0192,2, 1, 0,-2);
164 addsol( -2.740, -2.54,+0.022, 0.0324,2, 1, 0,-4);
165 addsol( 1.181, 3.32,-0.212, 0.0213,2,-1, 0,+2);
166 addsol( 9.703, 11.67,-0.151, 0.1268,2,-1, 0, 0);
167 addsol( -0.352, -0.37,+0.001, -0.0028,2,-1, 0,-1);
168 addsol( -2.494, -1.17,-0.003, -0.0017,2,-1, 0,-2);
169 addsol( 0.360, 0.20,-0.012, -0.0043,2,-1, 0,-4);
170 addsol( -1.167, -1.25,+0.008, -0.0106,1, 2, 0, 0);
171 addsol( -7.412, -6.12,+0.117, 0.0484,1, 2, 0,-2);
172 addsol( -0.311, -0.65,-0.032, 0.0044,1, 2, 0,-4);
173 addsol( +0.757, 1.82,-0.105, 0.0112,1,-2, 0, 2);
174 addsol( +2.580, 2.32,+0.027, 0.0196,1,-2, 0, 0);
175 addsol( +2.533, 2.40,-0.014, -0.0212,1,-2, 0,-2);
176 addsol( -0.344, -0.57,-0.025, +0.0036,0, 3, 0,-2);
177 addsol( -0.992, -0.02, 0.0 , 0.0 ,1, 0, 2, 2);
178 addsol( -45.099, -0.02, 0.0 , -0.0010,1, 0, 2, 0);
179 addsol( -0.179, -9.52, 0.0 , -0.0833,1, 0, 2,-2);
180 addsol( -0.301, -0.33, 0.0 , 0.0014,1, 0, 2,-4);
181 addsol( -6.382, -3.37, 0.0 , -0.0481,1, 0,-2, 2);
182 addsol( 39.528, 85.13, 0.0 , -0.7136,1, 0,-2, 0);
183 addsol( 9.366, 0.71, 0.0 , -0.0112,1, 0,-2,-2);
184 addsol( 0.202, 0.02, 0.0 , 0.0 ,1, 0,-2,-4);
187 * Solar3
189 addsol( 0.415, 0.10, 0.0 , 0.0013,0, 1, 2, 0);
190 addsol( -2.152, -2.26, 0.0 , -0.0066,0, 1, 2,-2);
191 addsol( -1.440, -1.30, 0.0 , +0.0014,0, 1,-2, 2);
192 addsol( 0.384, -0.04, 0.0 , 0.0 ,0, 1,-2,-2);
193 addsol( +1.938, +3.60,-0.145, +0.0401,4, 0, 0, 0);
194 addsol( -0.952, -1.58,+0.052, -0.0130,4, 0, 0,-2);
195 addsol( -0.551, -0.94,+0.032, -0.0097,3, 1, 0, 0);
196 addsol( -0.482, -0.57,+0.005, -0.0045,3, 1, 0,-2);
197 addsol( 0.681, 0.96,-0.026, 0.0115,3,-1, 0, 0);
198 addsol( -0.297, -0.27, 0.002, -0.0009,2, 2, 0,-2);
199 addsol( 0.254, +0.21,-0.003, 0.0 ,2,-2, 0,-2);
200 addsol( -0.250, -0.22, 0.004, 0.0014,1, 3, 0,-2);
201 addsol( -3.996, 0.0 , 0.0 , +0.0004,2, 0, 2, 0);
202 addsol( 0.557, -0.75, 0.0 , -0.0090,2, 0, 2,-2);
203 addsol( -0.459, -0.38, 0.0 , -0.0053,2, 0,-2, 2);
204 addsol( -1.298, 0.74, 0.0 , +0.0004,2, 0,-2, 0);
205 addsol( 0.538, 1.14, 0.0 , -0.0141,2, 0,-2,-2);
206 addsol( 0.263, 0.02, 0.0 , 0.0 ,1, 1, 2, 0);
207 addsol( 0.426, +0.07, 0.0 , -0.0006,1, 1,-2,-2);
208 addsol( -0.304, +0.03, 0.0 , +0.0003,1,-1, 2, 0);
209 addsol( -0.372, -0.19, 0.0 , -0.0027,1,-1,-2, 2);
210 addsol( +0.418, 0.0 , 0.0 , 0.0 ,0, 0, 4, 0);
211 addsol( -0.330, -0.04, 0.0 , 0.0 ,3, 0, 2, 0);
213 N = 0.0;
214 addn(-526.069, 0, 0,1,-2); addn( -3.352, 0, 0,1,-4);
215 addn( +44.297,+1, 0,1,-2); addn( -6.000,+1, 0,1,-4);
216 addn( +20.599,-1, 0,1, 0); addn( -30.598,-1, 0,1,-2);
217 addn( -24.649,-2, 0,1, 0); addn( -2.000,-2, 0,1,-2);
218 addn( -22.571, 0,+1,1,-2); addn( +10.985, 0,-1,1,-2);
220 DLAM += +0.82*sine( 0.7736 -62.5512*T ) + 0.31*sine( 0.0466 -125.1025*T )
221 +0.35*sine( 0.5785 -25.1042*T ) + 0.66*sine( 0.4591 +1335.8075*T )
222 +0.64*sine( 0.3130 -91.5680*T ) + 1.14*sine( 0.1480 +1331.2898*T )
223 +0.21*sine( 0.5918 +1056.5859*T ) + 0.44*sine( 0.5784 +1322.8595*T )
224 +0.24*sine( 0.2275 -5.7374*T ) + 0.28*sine( 0.2965 +2.6929*T )
225 +0.33*sine( 0.3132 +6.3368*T );
235 *LAMBDA = 360.0*frac( (L0+DLAM/ARC)/TwoPi );
237 S = F + DS/ARC;
238 FAC = 1.000002708 + 139.978*DGAM;
239 *BETA = (FAC*(18518.511 + 1.189 + GAM1C)*sin(S) - 6.24*sin(3*S) + N)/3600.0;
241 SINPI *= 0.999953253;
242 *R = ARC/SINPI;
245 DLAMS = 6893.0 * sin(LS) + 72.0 * sin(2.0*LS);
247 *AGE = 29.530589*frac((D+(DLAM-DLAMS)/ARC)/TwoPi);
249 printf("Diff = %f\n", 360.0*frac((D+(DLAM-DLAMS)/ARC)/TwoPi));
253 * Return the phase.
256 return( 0.5*(1.0 - cos(D+(DLAM-DLAMS)/ARC)) );
258 return( *AGE/29.530589 );
265 double sine(double phi){
267 return( sin(TwoPi*frac(phi)) );
272 double frac(double x){
274 x -= (int)x;
275 return( (x<0) ? x+1.0 : x );
281 void addsol(double COEFFL, double COEFFS, double COEFFG, double COEFFP, int P, int Q, int R, int S){
283 double X, Y;
285 term(P, Q, R, S, &X, &Y);
286 DLAM += COEFFL*Y;
287 DS += COEFFS*Y;
288 GAM1C += COEFFG*X;
289 SINPI += COEFFP*X;
296 void term(int P, int Q, int R, int S, double *X, double *Y){
298 double XX, YY;
299 int k, I[5];
301 I[1] = P, I[2] = Q, I[3] = R, I[4] = S, XX = 1.0, YY = 0.0;
302 for (k=1; k<=4; ++k){
303 if (I[k] != 0.0){
304 addthe(XX, YY, CO[6+I[k]][k], SI[6+I[k]][k], &XX, &YY);
307 *X = XX;
308 *Y = YY;
314 void addthe(double C1, double S1, double C2, double S2, double *C, double *S){
316 *C = C1*C2 - S1*S2;
317 *S = S1*C2 + C1*S2;
323 void addn(double COEFFN, int P, int Q, int R, int S){
325 double X, Y;
327 term(P, Q, R, S, &X, &Y);
328 N += COEFFN*Y;
335 #define R 0.61803399
336 #define C 0.38196601
338 double NewMoon(double ax, double bx, double cx){
340 double f1, f2, x0, x1, x2, x3, Moon();
341 double L, B, Rad, AGE, tol=1e-7;
343 x0 = ax;
344 x3 = cx;
345 if (fabs(cx-bx) > fabs(bx-ax)){
346 x1 = bx;
347 x2 = bx + C*(cx-bx);
348 } else {
349 x2 = bx;
350 x1 = bx - C*(bx-ax);
352 f1 = Moon(x1, &L, &B, &Rad, &AGE);
353 f2 = Moon(x2, &L, &B, &Rad, &AGE);
354 while (fabs(x3-x0) > tol*(fabs(x1)+fabs(x2))){
355 if (f2 < f1){
356 x0 = x1;
357 x1 = x2;
358 x2 = R*x1+C*x3;
359 f1 = f2;
360 f2 = Moon(x2, &L, &B, &Rad, &AGE);
361 } else {
362 x3 = x2;
363 x2 = x1;
364 x1 = R*x2+C*x0;
365 f2 = f1;
366 f1 = Moon(x1, &L, &B, &Rad, &AGE);
369 if (f1 < f2){
370 return(x1);
371 } else {
372 return(x2);
381 * MINI_MOON: low precision lunar coordinates (approx. 5'/1')
382 * T : time in Julian centuries since J2000
383 * ( T=(JD-2451545)/36525 )
384 * RA : right ascension (in h; equinox of date)
385 * DEC: declination (in deg; equinox of date)
388 void MiniMoon(double T, double *RA, double *DEC){
390 double L0,L,LS,F,D,H,S,N,DL,CB,L_MOON,B_MOON,V,W,X,Y,Z,RHO;
391 double frac(), cosEPS, sinEPS, P2, ARC;
394 cosEPS = 0.91748;
395 sinEPS = 0.39778;
396 P2 = 6.283185307;
397 ARC = 206264.8062;
401 * mean elements of lunar orbit
403 L0 = frac(0.606433+1336.855225*T); /* mean longitude Moon (in rev) */
404 L = P2*frac(0.374897+1325.552410*T); /* mean anomaly of the Moon */
405 LS = P2*frac(0.993133+ 99.997361*T); /* mean anomaly of the Sun */
406 D = P2*frac(0.827361+1236.853086*T); /* diff. longitude Moon-Sun */
407 F = P2*frac(0.259086+1342.227825*T); /* mean argument of latitude */
408 DL = +22640.0*sin(L) - 4586.0*sin(L-2.0*D) + 2370.0*sin(2.0*D) + 769.0*sin(2.0*L)
409 -668.0*sin(LS)- 412.0*sin(2.0*F) - 212.0*sin(2.0*L-2.0*D) - 206.0*sin(L+LS-2.0*D)
410 +192.0*sin(L+2.0*D) - 165.0*sin(LS-2.0*D) - 125.0*sin(D) - 110.0*sin(L+LS)
411 +148.0*sin(L-LS) - 55.0*sin(2.0*F-2.0*D);
412 S = F + (DL+412.0*sin(2.0*F)+541.0*sin(LS)) / ARC;
413 H = F-2.0*D;
414 N = -526.0*sin(H) + 44.0*sin(L+H) - 31.0*sin(-L+H) - 23.0*sin(LS+H)
415 + 11.0*sin(-LS+H) -25.0*sin(-2.0*L+F) + 21.0*sin(-L+F);
416 L_MOON = P2 * frac ( L0 + DL/1296e3 ); /* in rad */
417 B_MOON = ( 18520.0*sin(S) + N ) / ARC; /* in rad */
419 /* equatorial coordinates */
420 CB = cos(B_MOON);
421 X = CB*cos(L_MOON);
422 V = CB*sin(L_MOON);
423 W = sin(B_MOON);
424 Y = cosEPS*V-sinEPS*W;
425 Z = sinEPS*V+cosEPS*W;
426 RHO = sqrt(1.0-Z*Z);
427 *DEC = (360.0/P2)*atan2(Z, RHO);
428 *RA = ( 48.0/P2)*atan2(Y, X+RHO);
429 if (*RA<0.0) *RA += 24.0;