-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoon.c
1386 lines (1217 loc) · 42.7 KB
/
moon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* moon.c
* arrenged by H.Tsujimura 1989,1990,1991,1995,1998,1999,2000,
* 2001,2002
* Last updated: 20th December 2002
*
* This program is for computing the phase of the moon.
*
* History :
* $Log: moon.c $
* Revision 1.2 2003/11/11 07:02:19 tsujimura543
* K&R 表記 → ANSI C 表記 に完全移行
*
* Revision 1.1 2003/10/24 01:35:04 tsujimura543
* Initial revision (renewal)
*
* Revision 1.69 2003/03/03 12:09:08 tsujimura543
* バグ修正 (参照している変数が間違っていた)
*
* Revision 1.68 2003/03/03 12:07:41 tsujimura543
* CGI にパラメータを渡せるようにした
*
* Revision 1.67 2003/02/24 09:25:42 tsujimura543
* 実行形式の拡張子が .cgi の場合、Content-type: 行を出力するようにした
*
* Revision 1.66 2003/02/07 08:08:35 tsujimura543
* 起動時オプションとして -H, -v を正式サポート
*
* Revision 1.65 2002/12/20 07:49:27 tsujimura543
* 輝度の表示を追加
*
* Revision 1.64 2002/12/20 06:43:32 tsujimura543
* 朔~次の朔 の表示が 1 lunation 前のものを表示してしまうことが
* ある不具合に対処
*
* Revision 1.63 2001/04/17 13:43:38 tsujimura543
* jdate() と jtime() の引数を変更 (2038年問題から解放)
*
* Revision 1.62 2001/02/22 13:31:23 tsujimura543
* 1970年1月1日以前、2038年1月19日以降の日時を指定するとmktime()で例外が発生
* する現象に対処
*
* Revision 1.61 2001/02/22 12:47:08 tsujimura543
* moon2.c を moon.c に統合。さらに、Web アプリケーションとして実運用してい
* た版と統合。
*
* Revision 1.60 2001/02/20 12:23:36 tsujimura543
* 他の派生バージョンと統合するため、いったん revision を固定
*
* Revision 1.54 2001/01/17 18:09:23 tsujimura543
* lunation の変わり目の直前になると月齢が負の値になってしまうことがある不具
* 合に対処
*
* Revision 1.53 2000/10/18 15:43:28 tsujimura543
* 月齢を表示したい年月日をコマンドラインで指定できるようにする
*
* Revision 1.52 1999/12/20 15:39:04 tsujimura543
* 実際の月齢より 約1.5 くらい大きな値になってしまう不具合に対処
*
* Revision 1.51 1999/03/29 15:53:10 tsujimura543
* moon.c の次期実験バージョン (moon2.c)
* 開発環境を Win32 に移す
* -- HTML 出力対応作業や動作確認は今後もUNIX (peach.na.rim.or.jp) 上で行なう
*
* Revision 1.1 1996/08/19 12:01:10 tsujimura
* Initial revision
* Withdrawal from `shizuka' (UNIX上での開発終了版)
*
*/
/*
A Moon for the Sun
Release 2.0
Designed and implemented by John Walker in December 1987,
revised and updated in February of 1988.
Make with:
cc -O moontool.c -o moontool -lm -lsuntool -lsunwindow -lpixrect
Adding appropriate floating point options to your hardware. This
program is a SunView tool which displays, as the icon for a closed
window, the current phase of the Moon. A subtitle in the icon gives
the age of the Moon in days and hours. If called with the "-t"
switch, it rapidly increments forward through time to display the
cycle of phases.
If you open the window, additional information is displayed regarding
the Moon. The information is generally accurate to within ten
minutes.
The algorithms used in this program to calculate the positions Sun and
Moon as seen from the Earth are given in the book "Practical Astronomy
With Your Calculator" by Peter Duffett-Smith, Second Edition,
Cambridge University Press, 1981. Ignore the word "Calculator" in the
title; this is an essential reference if you're interested in
developing software which calculates planetary positions, orbits,
eclipses, and the like. If you're interested in pursuing such
programming, you should also obtain:
"Astronomical Formulae for Calculators" by Jean Meeus, Third Edition,
Willmann-Bell, 1985. A must-have.
"Planetary Programs and Tables from -4000 to +2800" by Pierre
Bretagnon and Jean-Louis Simon, Willmann-Bell, 1986. If you want the
utmost (outside of JPL) accuracy for the planets, it's here.
"Celestial BASIC" by Eric Burgess, Revised Edition, Sybex, 1985. Very
cookbook oriented, and many of the algorithms are hard to dig out of
the turgid BASIC code, but you'll probably want it anyway.
Many of these references can be obtained from Willmann-Bell, P.O. Box
35025, Richmond, VA 23235, USA. Phone: (804) 320-7016. In addition
to their own publications, they stock most of the standard references
for mathematical and positional astronomy.
This program was written by:
John Walker
Autodesk, Inc.
2320 Marinship Way
Sausalito, CA 94965
(415) 332-2344 Ext. 829
Usenet: {sun!well}!acad!kelvin
This program is in the public domain: "Do what thou wilt shall be the
whole of the law". I'd appreciate receiving any bug fixes and/or
enhancements, which I'll incorporate in future versions of the
program. Please leave the original attribution information intact so
that credit and blame may be properly apportioned.
*/
/* Astronomical constants */
#define epoch 2444238.5 /* 1980 January 0.0 */
/* Constants defining the Sun's apparent orbit */
#define elonge 278.833540 /* Ecliptic longitude of the Sun
at epoch 1980.0 */
#define elongp 282.596403 /* Ecliptic longitude of the Sun at
perigee */
#define eccent 0.016718 /* Eccentricity of Earth's orbit */
#define sunsmax 1.495985e8 /* Semi-major axis of Earth's orbit, km */
#define sunangsiz 0.533128 /* Sun's angular size, degrees, at
semi-major axis distance */
/* Elements of the Moon's orbit, epoch 1980.0 */
#define mmlong 64.975464 /* Moon's mean lonigitude at the epoch */
#define mmlongp 349.383063 /* Mean longitude of the perigee at the
epoch */
#define mlnode 151.950429 /* Mean longitude of the node at the
epoch */
#define minc 5.145396 /* Inclination of the Moon's orbit */
#define mecc 0.054900 /* Eccentricity of the Moon's orbit */
#define mangsiz 0.5181 /* Moon's angular size at distance a
from Earth */
#define msmax 384401.0 /* Semi-major axis of Moon's orbit in km */
#define mparallax 0.9507 /* Parallax at distance a from Earth */
#define synmonth 29.53058868 /* Synodic month (new Moon to new Moon) */
#define lunatbase 2423436.0 /* Base date for E. W. Brown's numbered
series of lunations (1923 January 16) */
/* Properties of the Earth */
#define earthrad 6378.16 /* Radius of Earth in kilometres */
#ifdef WIN32
# include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#ifdef M_PI
# define PI M_PI
#else
# define PI 3.14159265358979323846 /* Assume not near black hole nor in
Tennessee */
#endif
/* Handy mathematical functions */
#define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0)) /* Extract sign */
#define abs(x) ((x) < 0 ? (-(x)) : (x)) /* Absolute val */
#define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0))) /* Fix angle */
#define torad(d) ((d) * (PI / 180.0)) /* Deg->Rad */
#define todeg(d) ((d) * (180.0 / PI)) /* Rad->Deg */
#define dsin(x) (sin(torad((x)))) /* Sin from deg */
#define dcos(x) (cos(torad((x)))) /* Cos from deg */
#ifndef WIN32
# ifndef TRUE
# define TRUE 1
# define FALSE (!TRUE)
# endif
#endif
#if 0
static int testmode = FALSE; /* Rapid warp through time for debugging */
#endif
/* Forward functions */
#ifdef __STDC__
static double jtime( long, long, long, long, long, long );
static double phase( double, double *, double *, double *, double *, double *, double * );
static void phasehunt( double, double * );
static void jyear( double, long *, long *, long* );
static void jhms( double, long *, long *, long* );
#else
double jtime(), phase();
void phasehunt();
void jyear(), jhms();
#endif
/* JDATE -- Convert internal GMT date and time to Julian day
and fraction. */
static long jdate( yy, mm, dd )
long yy, mm, dd;
{
long c, m, y;
y = yy;
m = mm;
if (m > 2)
m = m - 3;
else {
m = m + 9;
y--;
}
c = y / 100L; /* Compute century */
y -= 100L * c;
return dd + (c * 146097L) / 4 + (y * 1461L) / 4 +
(m * 153L + 2) / 5 + 1721119L;
}
/* JTIME -- Convert internal GMT date and time to astronomical Julian
time (i.e. Julian date plus day fraction, expressed as
a double). */
static double jtime( yy, mm, dd, HH, MM, SS )
long yy, mm, dd, HH, MM, SS;
{
return (jdate(yy, mm, dd) - 0.5) +
(SS + 60 * (MM + 60 * HH)) / 86400.0;
}
/* JYEAR -- Convert Julian date to year, month, day, which are
returned via integer pointers to integers. */
static void jyear(td, yy, mm, dd)
double td;
long *yy, *mm, *dd;
{
double j, d, y, m;
td += 0.5; /* Astronomical to civil */
j = floor(td);
j = j - 1721119.0;
y = floor(((4 * j) - 1) / 146097.0);
j = (j * 4.0) - (1.0 + (146097.0 * y));
d = floor(j / 4.0);
j = floor(((4.0 * d) + 3.0) / 1461.0);
d = ((4.0 * d) + 3.0) - (1461.0 * j);
d = floor((d + 4.0) / 4.0);
m = floor(((5.0 * d) - 3) / 153.0);
d = (5.0 * d) - (3.0 + (153.0 * m));
d = floor((d + 5.0) / 5.0);
y = (100.0 * y) + j;
if (m < 10.0)
m = m + 3;
else {
m = m - 9;
y = y + 1;
}
*yy = (long)y;
*mm = (long)m;
*dd = (long)d;
}
/* JHMS -- Convert Julian time to hour, minutes, and seconds. */
static void jhms(j, h, m, s)
double j;
long *h, *m, *s;
{
long ij;
j += 0.5; /* Astronomical to civil */
ij = (long)((j - floor(j)) * 86400.0);
*h = ij / 3600L;
*m = (ij / 60L) % 60L;
*s = ij % 60L;
}
/* MEANPHASE -- Calculates mean phase of the Moon for a given
base date and desired phase:
0.0 New Moon
0.25 First quarter
0.5 Full moon
0.75 Last quarter
Beware!!! This routine returns meaningless
results for any other phase arguments. Don't
attempt to generalise it without understanding
that the motion of the moon is far more complicated
that this calculation reveals. */
static double meanphase(sdate, phase, usek, cnt)
double sdate, phase;
double *usek;
int *cnt;
{
long yy, mm, dd;
double k, t, t2, t3, nt1;
double preK;
static double preNt1 = 0.0;
jyear(sdate, &yy, &mm, &dd);
preK = k = (yy + ((mm - 1L) * (1.0 / 12.0)) - 1900.0) * 12.3685;
/* Time in Julian centuries from 1900 January 0.5 */
t = (sdate - 2415020.0) / 36525.64;
t2 = t * t; /* Square for frequent use */
t3 = t2 * t; /* Cube for frequent use */
*usek = k = floor(k) + phase;
nt1 = 2415020.75933 + synmonth * k
+ 0.0001178 * t2
- 0.000000155 * t3
+ 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
if ( *cnt >= 1 ) {
/* printf( "nt1 = %f, preNt1 = %f\n", nt1, preNt1 ); */
if ( nt1 - preNt1 > 45.0 ) {
*usek = k = floor(preK) - 1.0;
nt1 = 2415020.75933 + synmonth * k
+ 0.0001178 * t2
- 0.000000155 * t3
+ 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
}
}
(*cnt)++;
preNt1 = nt1;
return nt1;
}
/* TRUEPHASE -- Given a K value used to determine the
mean phase of the new moon, and a phase
selector (0.0, 0.25, 0.5, 0.75), obtain
the true, corrected phase time. */
static double truephase(k, phase)
double k, phase;
{
double t, t2, t3, pt, m, mprime, f;
int apcor = FALSE;
k += phase; /* Add phase to new moon time */
t = k / 1236.85; /* Time in Julian centuries from
1900 January 0.5 */
t2 = t * t; /* Square for frequent use */
t3 = t2 * t; /* Cube for frequent use */
pt = 2415020.75933 /* Mean time of phase */
+ synmonth * k
+ 0.0001178 * t2
- 0.000000155 * t3
+ 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
m = 359.2242 /* Sun's mean anomaly */
+ 29.10535608 * k
- 0.0000333 * t2
- 0.00000347 * t3;
mprime = 306.0253 /* Moon's mean anomaly */
+ 385.81691806 * k
+ 0.0107306 * t2
+ 0.00001236 * t3;
f = 21.2964 /* Moon's argument of latitude */
+ 390.67050646 * k
- 0.0016528 * t2
- 0.00000239 * t3;
if ((phase < 0.01) || (abs(phase - 0.5) < 0.01)) {
/* Corrections for New and Full Moon */
pt += (0.1734 - 0.000393 * t) * dsin(m)
+ 0.0021 * dsin(2 * m)
- 0.4068 * dsin(mprime)
+ 0.0161 * dsin(2 * mprime)
- 0.0004 * dsin(3 * mprime)
+ 0.0104 * dsin(2 * f)
- 0.0051 * dsin(m + mprime)
- 0.0074 * dsin(m - mprime)
+ 0.0004 * dsin(2 * f + m)
- 0.0004 * dsin(2 * f - m)
- 0.0006 * dsin(2 * f + mprime)
+ 0.0010 * dsin(2 * f - mprime)
+ 0.0005 * dsin(m + 2 * mprime);
apcor = TRUE;
}
else if ((abs(phase - 0.25) < 0.01 || (abs(phase - 0.75) < 0.01))) {
pt += (0.1721 - 0.0004 * t) * dsin(m)
+ 0.0021 * dsin(2 * m)
- 0.6280 * dsin(mprime)
+ 0.0089 * dsin(2 * mprime)
- 0.0004 * dsin(3 * mprime)
+ 0.0079 * dsin(2 * f)
- 0.0119 * dsin(m + mprime)
- 0.0047 * dsin(m - mprime)
+ 0.0003 * dsin(2 * f + m)
- 0.0004 * dsin(2 * f - m)
- 0.0006 * dsin(2 * f + mprime)
+ 0.0021 * dsin(2 * f - mprime)
+ 0.0003 * dsin(m + 2 * mprime)
+ 0.0004 * dsin(m - 2 * mprime)
- 0.0003 * dsin(2 * m + mprime);
if (phase < 0.5)
/* First quarter correction */
pt += 0.0028 - 0.0004 * dcos(m) + 0.0003 * dcos(mprime);
else
/* Last quarter correction */
pt += -0.0028 + 0.0004 * dcos(m) - 0.0003 * dcos(mprime);
apcor = TRUE;
}
if (!apcor) {
fprintf(stderr, "TRUEPHASE called with invalid phase selector.\n");
abort();
}
return pt;
}
/* PHASEHUNT -- Find time of phases of the moon which surround
the current date. Five phases are found, starting
and ending with the new moons which bound the
current lunation. */
static void phasehunt(sdate, phases)
double sdate;
double phases[5];
{
double adate, k1, k2, nt1, nt2;
int cnt = 0;
adate = sdate - 45.0;
nt1 = meanphase(adate, 0.0, &k1, &cnt);
while (TRUE) {
adate += synmonth;
nt2 = meanphase(adate, 0.0, &k2, &cnt);
if ((nt1 <= sdate) && (nt2 > sdate))
break;
nt1 = nt2;
k1 = k2;
}
if (k2 - k1 >= 2.0)
k2 = k1 + 1.0;
phases[0] = truephase(k1, 0.0);
phases[1] = truephase(k1, 0.25);
phases[2] = truephase(k1, 0.5);
phases[3] = truephase(k1, 0.75);
phases[4] = truephase(k2, 0.0);
}
/* KEPLER -- Solve the equation of Kepler. */
static double kepler(m, ecc)
double m, ecc;
{
double e, delta;
#define EPSILON 1E-6
e = m = torad(m);
do {
delta = e - ecc * sin(e) - m;
e -= delta / (1 - ecc * cos(e));
} while (abs(delta) > EPSILON);
return e;
}
/* PHASE -- Calculate phase of moon as a fraction:
The argument is the time for which the phase is requested,
expressed as a Julian date and fraction. Returns the terminator
phase angle as a percentage of a full circle (i.e., 0 to 1),
and stores into pointer arguments the illuminated fraction of
the Moon's disc, the Moon's age in days and fraction, the
distance of the Moon from the centre of the Earth, and the
angular diameter subtended by the Moon as seen by an observer
at the centre of the Earth.
*/
static double phase(pdate, pphase, mage, dist, angdia, sudist, suangdia)
double pdate;
double *pphase; /* Illuminated fraction */
double *mage; /* Age of moon in days */
double *dist; /* Distance in kilometres */
double *angdia; /* Angular diameter in degrees */
double *sudist; /* Distance to Sun */
double *suangdia; /* Sun's angular diameter */
{
double Day, N, M, Ec, Lambdasun, ml, MM, MN, Ev, Ae, A3, MmP,
mEc, A4, lP, V, lPP, NP, y, x, Lambdamoon,
MoonAge, MoonPhase,
MoonDist, MoonDFrac, MoonAng,
F, SunDist, SunAng;
/* Calculation of the Sun's position */
Day = pdate - epoch; /* Date within epoch */
N = fixangle((360.0 / 365.2422) * Day); /* Mean anomaly of the Sun */
M = fixangle(N + elonge - elongp); /* Convert from perigee
co-ordinates to epoch 1980.0 */
Ec = kepler(M, eccent); /* Solve equation of Kepler */
Ec = sqrt((1.0 + eccent) / (1.0 - eccent)) * tan(Ec / 2.0);
Ec = 2.0 * todeg(atan(Ec)); /* True anomaly */
Lambdasun = fixangle(Ec + elongp); /* Sun's geocentric ecliptic
longitude */
/* Orbital distance factor */
F = ((1.0 + eccent * cos(torad(Ec))) / (1.0 - eccent * eccent));
SunDist = sunsmax / F; /* Distance to Sun in km */
SunAng = F * sunangsiz; /* Sun's angular size in degrees */
/* Calculation of the Moon's position */
/* Moon's mean longitude */
ml = fixangle(13.1763966 * Day + mmlong);
/* Moon's mean anomaly */
MM = fixangle(ml - 0.1114041 * Day - mmlongp);
/* Moon's ascending node mean longitude */
MN = fixangle(mlnode - 0.0529539 * Day);
/* Evection */
Ev = 1.2739 * sin(torad(2.0 * (ml - Lambdasun) - MM));
/* Annual equation */
Ae = 0.1858 * sin(torad(M));
/* Correction term */
A3 = 0.37 * sin(torad(M));
/* Corrected anomaly */
MmP = MM + Ev - Ae - A3;
/* Correction for the equation of the centre */
mEc = 6.2886 * sin(torad(MmP));
/* Another correction term */
A4 = 0.214 * sin(torad(2.0 * MmP));
/* Corrected longitude */
lP = ml + Ev + mEc - Ae + A4;
/* Variation */
V = 0.6583 * sin(torad(2.0 * (lP - Lambdasun)));
/* True longitude */
lPP = lP + V;
/* Corrected longitude of the node */
NP = MN - 0.16 * sin(torad(M));
/* Y inclination coordinate */
y = sin(torad(lPP - NP)) * cos(torad(minc));
/* X inclination coordinate */
x = cos(torad(lPP - NP));
/* Ecliptic longitude */
Lambdamoon = todeg(atan2(y, x));
Lambdamoon += NP;
#if 0
/* Ecliptic latitude */
(void)todeg(asin(sin(torad(lPP - NP)) * sin(torad(minc))));
#endif
/* Calculation of the phase of the Moon */
/* Age of the Moon in degrees */
MoonAge = lPP - Lambdasun;
/* Phase of the Moon */
MoonPhase = (1.0 - cos(torad(MoonAge))) / 2.0;
/* Calculate distance of moon from the centre of the Earth */
MoonDist = (msmax * (1.0 - mecc * mecc)) /
(1.0 + mecc * cos(torad(MmP + mEc)));
/* Calculate Moon's angular diameter */
MoonDFrac = MoonDist / msmax;
MoonAng = mangsiz / MoonDFrac;
#if 0
/* Calculate Moon's parallax */
MoonPar = mparallax / MoonDFrac;
#endif
*pphase = MoonPhase;
*mage = synmonth * (fixangle(MoonAge) / 360.0);
*dist = MoonDist;
*angdia = MoonAng;
*sudist = SunDist;
*suangdia = SunAng;
return fixangle(MoonAge) / 360.0;
}
/*** qpom2() is arranged from qpom(). qpom() is written by Christopher Osburn.
/*
** qpom.c
** Christopher Osburn 911102
**
** Routines returning age of the moon (qpom), quantity of lunar face
** under illumination (illum), and the current lunation number
** (lunation) using the extremely cheesy epoch/iteration method.
**
*/
#define LPERIOD ((double)2551442.9) /* synodic period of the moon, seconds,
from astronomical almanac 1992 */
#ifdef __STDC__
double qpom2(time_t now, time_t epoch_near)
#else
double qpom2(now, epoch_near)
time_t now, epoch_near;
#endif
{
double diff, howfar;
diff = (double) now - epoch_near;
/* 本家の qpom() は epoch_near の部分に固定値を使用している
* ため、lunation の進行に伴って誤差がどんどん増えていくとい
* う欠点がある(実際に、qpom.c は数年毎に固定値部分を変更す
* る修正版が出ている)。
* qpom2() では、固定値ではなく、直近の epoch (朔のユリウス
* 日)を算出し、使用することで、長期にわたってプログラム変
* 更せずに済ませることができるものを目指している。
*/
howfar = diff / LPERIOD;
return LPERIOD * (howfar - (int) howfar) / 86400.0;
}
/*
* phasehunt() で求めた朔、上弦、望、下弦、次の朔の日時を
* qpom2() の第1引数に指定した場合の月齢が 0.0, 7.5, 15.0,
* 22.5, 30.0 にはならない(月齢計算の方法が違う方式のため)
* 以下の関数は、qpom2() の値が 0.0, 7.5, 15.0, 22.5, 30.0
* になる日時を求めるためのものである。
*/
#ifdef __STDC__
struct tm *
getTruePhaseWithQpom( time_t t1, time_t t2, double curAom, double moonPhase )
#else
struct tm *
getTruePhaseWithQpom( t1, t2, curAom, moonPhase )
time_t t1;
time_t t2;
double curAom;
double moonPhase;
#endif
{
time_t t;
double aom, diff;
struct tm *tm;
diff = moonPhase - curAom;
t = t1 + (int)(diff * 24.0 * 60.0 * 60.0);
aom = qpom2( t, t2 );
if ( aom > moonPhase ) {
t -= (int)((aom - moonPhase) * 24.0 * 60.0 * 60.0);
aom = qpom2( t, t2 );
}
tm = gmtime( &t );
return ( tm );
}
#ifdef __STDC__
void
getPhasesWithQpom(
time_t t1, time_t t2, double curAom, /*long lunation,*/ char flag )
#else
getPhasesWithQpom( t1, t2, curAom, /*lunation,*/ flag )
time_t t1;
time_t t2;
double curAom;
/*long lunation;*/
char flag;
#endif
{
struct tm *tm;
if ( (flag & 2) == 0 ) {
return;
}
if ( (flag & 1) == 1 )
printf( "<TR><TD COLSPAN=2>" );
printf( "\n参考: \n" );
if ( (flag & 1) == 1 )
printf( "</TD></TR>\n<TR><TD ALIGN=\"RIGHT\">" );
tm = getTruePhaseWithQpom( t1, t2, curAom, synmonth * 0.00 );
printf(
/*" 朔%s %d年%02d月%02d日 %02d時%02d分 UTC (Lunation %d)\n",*/
" 朔%s %d年%02d月%02d日 %02d時%02d分 UTC\n",
(flag & 1) == 1 ? "</TD><TD COLSPAN=2>" : ":",
tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min /*,
lunation*/ );
if ( (flag & 1) == 1 )
printf( "</TD></TR>\n<TR><TD ALIGN=\"RIGHT\">" );
tm = getTruePhaseWithQpom( t1, t2, curAom, synmonth * 0.25 );
printf( " 上弦%s %d年%02d月%02d日 %02d時%02d分 UTC\n",
(flag & 1) == 1 ? "</TD><TD COLSPAN=2>" : ":",
tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min );
if ( (flag & 1) == 1 )
printf( "</TD></TR>\n<TR><TD ALIGN=\"RIGHT\">" );
tm = getTruePhaseWithQpom( t1, t2, curAom, synmonth * 0.50 );
printf( " 望%s %d年%02d月%02d日 %02d時%02d分 UTC\n",
(flag & 1) == 1 ? "</TD><TD COLSPAN=2>" : ":",
tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min );
if ( (flag & 1) == 1 )
printf( "</TD></TR>\n<TR><TD ALIGN=\"RIGHT\">" );
tm = getTruePhaseWithQpom( t1, t2, curAom, synmonth * 0.75 );
printf( " 下弦%s %d年%02d月%02d日 %02d時%02d分 UTC\n",
(flag & 1) == 1 ? "</TD><TD COLSPAN=2>" : ":",
tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min );
if ( (flag & 1) == 1 )
printf( "</TD></TR>\n<TR><TD ALIGN=\"RIGHT\">" );
tm = getTruePhaseWithQpom( t1, t2, curAom, synmonth * 1.00 );
printf(
/*" 次の朔%s %d年%02d月%02d日 %02d時%02d分 UTC (Lunation %d)\n",*/
" 次の朔%s %d年%02d月%02d日 %02d時%02d分 UTC\n",
(flag & 1) == 1 ? "</TD><TD COLSPAN=2>" : ":",
tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min /*,
lunation + 1*/ );
if ( (flag & 1) == 1 )
printf( "</TD></TR>\n" );
}
#ifdef UNIX
#ifdef SYSV
#define _tzname tzname
#define _timezone timezone
#else
char *_tzname[2];
long _timezone;
#endif
#endif
time_t
getTargetDateTime( argc, argv, flag )
int argc;
char *argv[];
char *flag;
{
struct tm _tm;
time_t t = 0;
/* *flag = 0; */
if ( argc > 1 ) {
int i, j;
for ( i = 1; i < argc; i++ ) {
if ( argv[i][0] == '-' ) {
for ( j = 1; argv[i][j] != '\0'; j++ ) {
switch ( argv[i][j] ) {
case 'H':
*flag |= 1;
break;
case 'v':
*flag |= 2;
break;
}
}
continue;
}
else if ( (argv[i][0] >= '0') && (argv[i][0] <= '9') &&
( (strlen( argv[i] ) == 8) ||
(strlen( argv[i] ) == 12) ||
(strlen( argv[i] ) == 14) ) ) {
int y, m, d, H = 0, M = 0, S = 0;
y = (argv[i][0] - '0') * 1000 +
(argv[i][1] - '0') * 100 +
(argv[i][2] - '0') * 10 +
(argv[i][3] - '0');
m = (argv[i][4] - '0') * 10 + (argv[i][5] - '0');
d = (argv[i][6] - '0') * 10 + (argv[i][7] - '0');
if ( argv[i][8] != '\0' ) {
H = (argv[i][ 8] - '0') * 10 + (argv[i][ 9] - '0');
M = (argv[i][10] - '0') * 10 + (argv[i][11] - '0');
if ( argv[i][12] != '\0' ) {
S = (argv[i][12] - '0') * 10 + (argv[i][13] - '0');
}
}
if ( y < 1970 ) {
fprintf( stderr,
"1970年以前の月齢計算はサポートしていません。\n" );
exit( 1 );
}
if ( (y == 1970) && (m == 1) && (d == 1) && (H < 9) ) {
fprintf( stderr, "%s%s\n",
"1970年1月1日の午前9時(日本時間)以前の",
"月齢計算はサポートしていません。" );
fprintf( stderr, "%s%s\n",
"代わりに、1970年1月1日の午前9時0分1秒",
"(日本時間)の月齢を表示します。" );
H = 9; M = 0; S = 1;
}
if ( y > 2038 ) {
fprintf( stderr,
"2038年以降の月齢計算はサポートしていません。\n" );
exit( 1 );
}
if ( (y == 2038) &&
( (m > 1) ||
((m == 1) &&
( (d > 19) ||
((d == 19) &&
( (H > 3) ||
((H == 3) &&
( (M > 14) ||
((M == 14) && (S >= 8))
)
)
)
)
)
)
) ) {
fprintf( stderr, "%s%s\n",
"2038年1月19日の午前3時14分8秒(日本時間)以降の",
"月齢計算はサポートしていません。" );
fprintf( stderr, "%s%s\n",
"代わりに、2038年1月19日の午前3時14分7秒",
"を(日本時間)の月齢表示します。" );
m = 1; d = 19;
H = 3; M = 14; S = 7;
}
memset( &_tm, 0, sizeof ( struct tm ) );
_tm.tm_year = y - 1900;
_tm.tm_mon = m - 1;
_tm.tm_mday = d;
_tm.tm_hour = H;
_tm.tm_min = M;
_tm.tm_sec = S;
t = mktime( &_tm );
}
else if ( !strcmp( argv[i], "yesterday" ) ) {
t = time( NULL ) - 24 * 60 * 60;
}
else if ( !strcmp( argv[i], "tomorrow" ) ) {
t = time( NULL ) + 24 * 60 * 60;
}
}
}
if ( t == 0 )
t = time( NULL );
return ( t );
}
#ifndef NUL
#define NUL '\0'
#endif
#ifdef __STDC__
char *decodeURL( const char *pp );
#else
char *decodeURL();
#endif
static void
setParameter( p, yy, mm, dd, HH, MM, SS )
char *p;
int *yy, *mm, *dd, *HH, *MM, *SS;
{
char *q, *r;
char buf[BUFSIZ];
while ( *p ) {
q = NULL;
if ( !strncmp( p, "y=", 2 ) ) {
p += 2;
q = &(buf[0]);
while ( *p && (*p != '&') )
*q++ = *p++;
*q = NUL;
if ( *p == '&' )
p++;
r = decodeURL( buf );
if ( (*r >= '0') && (*r <= '9') )
*yy = atoi( r );
}
if ( !strncmp( p, "m=", 2 ) ) {
p += 2;
q = &(buf[0]);
while ( *p && (*p != '&') )
*q++ = *p++;
*q = NUL;
if ( *p == '&' )
p++;
r = decodeURL( buf );
if ( (*r >= '0') && (*r <= '9') )
*mm = atoi( r );
}
if ( !strncmp( p, "d=", 2 ) ) {
p += 2;
q = &(buf[0]);
while ( *p && (*p != '&') )
*q++ = *p++;
*q = NUL;
if ( *p == '&' )
p++;
r = decodeURL( buf );
if ( (*r >= '0') && (*r <= '9') )
*dd = atoi( r );
}
if ( !strncmp( p, "H=", 2 ) ) {
p += 2;
q = &(buf[0]);
while ( *p && (*p != '&') )
*q++ = *p++;
*q = NUL;
if ( *p == '&' )
p++;
r = decodeURL( buf );
if ( (*r >= '0') && (*r <= '9') ) {
*HH = atoi( r );
}
}
if ( !strncmp( p, "M=", 2 ) ) {
p += 2;
q = &(buf[0]);
while ( *p && (*p != '&') )
*q++ = *p++;
*q = NUL;
if ( *p == '&' )
p++;
r = decodeURL( buf );
if ( (*r >= '0') && (*r <= '9') ) {
*MM = atoi( r );
}
}
if ( !strncmp( p, "S=", 2 ) ) {
p += 2;
q = &(buf[0]);
while ( *p && (*p != '&') )
*q++ = *p++;
*q = NUL;
if ( *p == '&' )
p++;
r = decodeURL( buf );
if ( (*r >= '0') && (*r <= '9') ) {
*SS = atoi( r );
}
}
if ( !q )
p++;
}
}
static int
isLeapYear( yy )