pom.c revision 205821
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software posted to USENET.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#if 0
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44static const char sccsid[] = "@(#)pom.c       8.1 (Berkeley) 5/31/93";
45#endif /* not lint */
46#endif
47#include <sys/cdefs.h>
48__FBSDID("$FreeBSD: head/usr.bin/calendar/pom.c 205821 2010-03-29 06:49:20Z edwin $");
49
50/*
51 * Phase of the Moon.  Calculates the current phase of the moon.
52 * Based on routines from `Practical Astronomy with Your Calculator',
53 * by Duffett-Smith.  Comments give the section from the book that
54 * particular piece of code was adapted from.
55 *
56 * -- Keith E. Brandt  VIII 1984
57 *
58 */
59
60#include <stdio.h>
61#include <stdlib.h>
62#include <math.h>
63#include <string.h>
64#include <sysexits.h>
65#include <time.h>
66#include <unistd.h>
67
68#include "calendar.h"
69
70#ifndef	PI
71#define	PI	  3.14159265358979323846
72#endif
73#define	EPOCH	  85
74#define	EPSILONg  279.611371	/* solar ecliptic long at EPOCH */
75#define	RHOg	  282.680403	/* solar ecliptic long of perigee at EPOCH */
76#define	ECCEN	  0.01671542	/* solar orbit eccentricity */
77#define	lzero	  18.251907	/* lunar mean long at EPOCH */
78#define	Pzero	  192.917585	/* lunar mean long of perigee at EPOCH */
79#define	Nzero	  55.204723	/* lunar mean long of node at EPOCH */
80#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
81
82static void	adj360(double *);
83static double	dtor(double);
84static double	potm(double onday);
85static double	potm_minute(double onday, int olddir);
86
87void
88pom(int year, double utcoffset, int *fms, int *nms)
89{
90	double ffms[MAXMOONS];
91	double fnms[MAXMOONS];
92	int i, j;
93
94	fpom(year, utcoffset, ffms, fnms);
95
96	j = 0;
97	for (i = 0; ffms[i] != 0; i++)
98		fms[j++] = round(ffms[i]);
99	fms[i] = -1;
100	for (i = 0; fnms[i] != 0; i++)
101		nms[i] = round(fnms[i]);
102	nms[i] = -1;
103}
104
105void
106fpom(int year, double utcoffset, double *ffms, double *fnms)
107{
108	time_t tt;
109	struct tm GMT, tmd_today, tmd_tomorrow;
110	double days_today, days_tomorrow, today, tomorrow;
111	int cnt, d;
112	int yeardays;
113	int olddir, newdir;
114	double *pfnms, *pffms, t;
115
116	pfnms = fnms;
117	pffms = ffms;
118
119	/*
120	 * We take the phase of the moon one second before and one second
121	 * after midnight.
122	 */
123	memset(&tmd_today, 0, sizeof(tmd_today));
124	tmd_today.tm_year = year - 1900;
125	tmd_today.tm_mon = 0;
126	tmd_today.tm_mday = -1;		/* 31 December */
127	tmd_today.tm_hour = 23;
128	tmd_today.tm_min = 59;
129	tmd_today.tm_sec = 59;
130	memset(&tmd_tomorrow, 0, sizeof(tmd_tomorrow));
131	tmd_tomorrow.tm_year = year - 1900;
132	tmd_tomorrow.tm_mon = 0;
133	tmd_tomorrow.tm_mday = 0;	/* 01 January */
134	tmd_tomorrow.tm_hour = 0;
135	tmd_tomorrow.tm_min = 0;
136	tmd_tomorrow.tm_sec = 1;
137
138	tt = mktime(&tmd_today);
139	gmtime_r(&tt, &GMT);
140	yeardays = 0;
141	for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
142		yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
143	days_today = (GMT.tm_yday + 1) + ((GMT.tm_hour +
144	    (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
145	    FHOURSPERDAY);
146	days_today += yeardays;
147
148	tt = mktime(&tmd_tomorrow);
149	gmtime_r(&tt, &GMT);
150	yeardays = 0;
151	for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
152		yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
153	days_tomorrow = (GMT.tm_yday + 1) + ((GMT.tm_hour +
154	    (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
155	    FHOURSPERDAY);
156	days_tomorrow += yeardays;
157
158	today = potm(days_today);		/* 30 December 23:59:59 */
159	tomorrow = potm(days_tomorrow);		/* 31 December 00:00:01 */
160	olddir = today > tomorrow ? -1 : +1;
161
162	yeardays = 1 + isleap(year) ? DAYSPERLEAPYEAR : DAYSPERYEAR; /* reuse */
163	for (d = 0; d <= yeardays; d++) {
164		today = potm(days_today);
165		tomorrow = potm(days_tomorrow);
166		newdir = today > tomorrow ? -1 : +1;
167		if (olddir != newdir) {
168			t = potm_minute(days_today - 1, olddir) +
169			     utcoffset / FHOURSPERDAY;
170			if (olddir == -1 && newdir == +1) {
171				*pfnms = d - 1 + t;
172				pfnms++;
173			} else if (olddir == +1 && newdir == -1) {
174				*pffms = d - 1 + t;
175				pffms++;
176			}
177		}
178		olddir = newdir;
179		days_today++;
180		days_tomorrow++;
181	}
182	*pffms = -1;
183	*pfnms = -1;
184}
185
186static double
187potm_minute(double onday, int olddir) {
188	double period = FSECSPERDAY / 2.0;
189	double p1, p2;
190	double before, after;
191	int newdir;
192
193//	printf("---> days:%g olddir:%d\n", days, olddir);
194
195	p1 = onday + (period / SECSPERDAY);
196	period /= 2;
197
198	while (period > 30) {	/* half a minute */
199//		printf("period:%g - p1:%g - ", period, p1);
200		p2 = p1 + (2.0 / SECSPERDAY);
201		before = potm(p1);
202		after = potm(p2);
203//		printf("before:%10.10g - after:%10.10g\n", before, after);
204		newdir = before < after ? -1 : +1;
205		if (olddir != newdir)
206			p1 += (period / SECSPERDAY);
207		else
208			p1 -= (period / SECSPERDAY);
209		period /= 2;
210//		printf("newdir:%d - p1:%10.10f - period:%g\n",
211//		    newdir, p1, period);
212	}
213	p1 -= floor(p1);
214	//exit(0);
215	return (p1);
216}
217
218/*
219 * potm --
220 *	return phase of the moon, as a percentage [0 ... 100]
221 */
222static double
223potm(double onday)
224{
225	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
226	double A4, lprime, V, ldprime, D, Nm;
227
228	N = 360 * onday / 365.2422;				/* sec 42 #3 */
229	adj360(&N);
230	Msol = N + EPSILONg - RHOg;				/* sec 42 #4 */
231	adj360(&Msol);
232	Ec = 360 / PI * ECCEN * sin(dtor(Msol));		/* sec 42 #5 */
233	LambdaSol = N + Ec + EPSILONg;				/* sec 42 #6 */
234	adj360(&LambdaSol);
235	l = 13.1763966 * onday + lzero;				/* sec 61 #4 */
236	adj360(&l);
237	Mm = l - (0.1114041 * onday) - Pzero;			/* sec 61 #5 */
238	adj360(&Mm);
239	Nm = Nzero - (0.0529539 * onday);			/* sec 61 #6 */
240	adj360(&Nm);
241	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 61 #7 */
242	Ac = 0.1858 * sin(dtor(Msol));				/* sec 61 #8 */
243	A3 = 0.37 * sin(dtor(Msol));
244	Mmprime = Mm + Ev - Ac - A3;				/* sec 61 #9 */
245	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 61 #10 */
246	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 61 #11 */
247	lprime = l + Ev + Ec - Ac + A4;				/* sec 61 #12 */
248	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 61 #13 */
249	ldprime = lprime + V;					/* sec 61 #14 */
250	D = ldprime - LambdaSol;				/* sec 63 #2 */
251	return(50 * (1 - cos(dtor(D))));			/* sec 63 #3 */
252}
253
254/*
255 * dtor --
256 *	convert degrees to radians
257 */
258static double
259dtor(double deg)
260{
261
262	return(deg * PI / 180);
263}
264
265/*
266 * adj360 --
267 *	adjust value so 0 <= deg <= 360
268 */
269static void
270adj360(double *deg)
271{
272
273	for (;;)
274		if (*deg < 0)
275			*deg += 360;
276		else if (*deg > 360)
277			*deg -= 360;
278		else
279			break;
280}
281