2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software posted to USENET.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
42 static const char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
49 * Phase of the Moon. Calculates the current phase of the moon.
50 * Based on routines from `Practical Astronomy with Your Calculator',
51 * by Duffett-Smith. Comments give the section from the book that
52 * particular piece of code was adapted from.
54 * -- Keith E. Brandt VIII 1984
69 #define PI 3.14159265358979323846
72 #define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */
73 #define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */
74 #define ECCEN 0.01671542 /* solar orbit eccentricity */
75 #define lzero 18.251907 /* lunar mean long at EPOCH */
76 #define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */
77 #define Nzero 55.204723 /* lunar mean long of node at EPOCH */
78 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
80 static void adj360(double *);
81 static double dtor(double);
82 static double potm(double onday);
83 static double potm_minute(double onday, int olddir);
86 pom(int year, double utcoffset, int *fms, int *nms)
88 double ffms[MAXMOONS];
89 double fnms[MAXMOONS];
92 fpom(year, utcoffset, ffms, fnms);
95 for (i = 0; ffms[i] != 0; i++)
96 fms[j++] = round(ffms[i]);
98 for (i = 0; fnms[i] != 0; i++)
99 nms[i] = round(fnms[i]);
104 fpom(int year, double utcoffset, double *ffms, double *fnms)
107 struct tm GMT, tmd_today, tmd_tomorrow;
108 double days_today, days_tomorrow, today, tomorrow;
112 double *pfnms, *pffms, t;
118 * We take the phase of the moon one second before and one second
121 memset(&tmd_today, 0, sizeof(tmd_today));
122 tmd_today.tm_year = year - 1900;
123 tmd_today.tm_mon = 0;
124 tmd_today.tm_mday = -1; /* 31 December */
125 tmd_today.tm_hour = 23;
126 tmd_today.tm_min = 59;
127 tmd_today.tm_sec = 59;
128 memset(&tmd_tomorrow, 0, sizeof(tmd_tomorrow));
129 tmd_tomorrow.tm_year = year - 1900;
130 tmd_tomorrow.tm_mon = 0;
131 tmd_tomorrow.tm_mday = 0; /* 01 January */
132 tmd_tomorrow.tm_hour = 0;
133 tmd_tomorrow.tm_min = 0;
134 tmd_tomorrow.tm_sec = 1;
136 tt = mktime(&tmd_today);
139 for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
140 yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
141 days_today = (GMT.tm_yday + 1) + ((GMT.tm_hour +
142 (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
144 days_today += yeardays;
146 tt = mktime(&tmd_tomorrow);
149 for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
150 yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
151 days_tomorrow = (GMT.tm_yday + 1) + ((GMT.tm_hour +
152 (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
154 days_tomorrow += yeardays;
156 today = potm(days_today); /* 30 December 23:59:59 */
157 tomorrow = potm(days_tomorrow); /* 31 December 00:00:01 */
158 olddir = today > tomorrow ? -1 : +1;
160 yeardays = 1 + (isleap(year) ? DAYSPERLEAPYEAR : DAYSPERYEAR); /* reuse */
161 for (d = 0; d <= yeardays; d++) {
162 today = potm(days_today);
163 tomorrow = potm(days_tomorrow);
164 newdir = today > tomorrow ? -1 : +1;
165 if (olddir != newdir) {
166 t = potm_minute(days_today - 1, olddir) +
167 utcoffset / FHOURSPERDAY;
168 if (olddir == -1 && newdir == +1) {
171 } else if (olddir == +1 && newdir == -1) {
185 potm_minute(double onday, int olddir) {
186 double period = FSECSPERDAY / 2.0;
188 double before, after;
191 // printf("---> days:%g olddir:%d\n", days, olddir);
193 p1 = onday + (period / SECSPERDAY);
196 while (period > 30) { /* half a minute */
197 // printf("period:%g - p1:%g - ", period, p1);
198 p2 = p1 + (2.0 / SECSPERDAY);
201 // printf("before:%10.10g - after:%10.10g\n", before, after);
202 newdir = before < after ? -1 : +1;
203 if (olddir != newdir)
204 p1 += (period / SECSPERDAY);
206 p1 -= (period / SECSPERDAY);
208 // printf("newdir:%d - p1:%10.10f - period:%g\n",
209 // newdir, p1, period);
218 * return phase of the moon, as a percentage [0 ... 100]
223 double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
224 double A4, lprime, V, ldprime, D, Nm;
226 N = 360 * onday / 365.2422; /* sec 42 #3 */
228 Msol = N + EPSILONg - RHOg; /* sec 42 #4 */
230 Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */
231 LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */
233 l = 13.1763966 * onday + lzero; /* sec 61 #4 */
235 Mm = l - (0.1114041 * onday) - Pzero; /* sec 61 #5 */
237 Nm = Nzero - (0.0529539 * onday); /* sec 61 #6 */
239 Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */
240 Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */
241 A3 = 0.37 * sin(dtor(Msol));
242 Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */
243 Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */
244 A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */
245 lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */
246 V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */
247 ldprime = lprime + V; /* sec 61 #14 */
248 D = ldprime - LambdaSol; /* sec 63 #2 */
249 return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */
254 * convert degrees to radians
260 return(deg * PI / 180);
265 * adjust value so 0 <= deg <= 360