-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmoonphase.c
104 lines (87 loc) · 2.5 KB
/
moonphase.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
/*
moonphase.c - by Don Cross - 2019-05-25
Example C program for Astronomy Engine:
https://github.com/cosinekitty/astronomy
This program calculates the Moon's phase for a given date and time,
or the computer's current date and time if none is given.
It also finds the dates and times of the subsequent 10 quarter phase changes.
*/
#include <stdio.h>
#include "astro_demo_common.h"
static const char *QuarterName(int quarter)
{
switch (quarter)
{
case 0: return "New Moon";
case 1: return "First Quarter";
case 2: return "Full Moon";
case 3: return "Third Quarter";
default: return "INVALID QUARTER";
}
}
int main(int argc, const char *argv[])
{
astro_time_t time;
astro_angle_result_t phase;
astro_moon_quarter_t mq;
astro_illum_t illum;
int i;
switch (argc)
{
case 1:
time = Astronomy_CurrentTime();
break;
case 2:
if (ParseTime(argv[1], &time))
return 1;
break;
default:
fprintf(stderr, "USAGE: moonphase [date]\n");
return 1;
}
/*
Calculate the Moon's ecliptic phase angle,
which ranges from 0 to 360 degrees.
0 = new moon,
90 = first quarter,
180 = full moon,
270 = third quarter.
*/
phase = Astronomy_MoonPhase(time);
if (phase.status != ASTRO_SUCCESS)
{
printf("Astronomy_MoonPhase error %d\n", phase.status);
return 1;
}
PrintTime(time);
printf(" : Moon's ecliptic phase angle = %0.3lf degrees.\n", phase.angle);
/*
Calculate the percentage of the Moon's disc that is illuminated
from the Earth's point of view.
*/
illum = Astronomy_Illumination(BODY_MOON, time);
if (illum.status != ASTRO_SUCCESS)
{
printf("Astronomy_Illumination error %d\n", illum.status);
return 1;
}
PrintTime(time);
printf(" : Moon's illuminated fraction = %0.2lf%%.", 100.0 * illum.phase_fraction);
/* Find the next 10 lunar quarter phases. */
printf("\nThe next 10 lunar quarters are:\n");
for (i=0; i < 10; ++i)
{
if (i == 0)
mq = Astronomy_SearchMoonQuarter(time);
else
mq = Astronomy_NextMoonQuarter(mq);
if (mq.status != ASTRO_SUCCESS)
{
printf("Error %d trying to find moon quarter.\n", mq.status);
return 1;
}
PrintTime(mq.time);
printf(" : %s\n", QuarterName(mq.quarter));
}
return 0;
}