1994-09-04 04:03:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software posted to USENET.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2010-02-15 18:46:02 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-09-04 04:03:31 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-05-05 09:52:25 +00:00
|
|
|
#if 0
|
1994-09-04 04:03:31 +00:00
|
|
|
#ifndef lint
|
1996-05-27 23:11:26 +00:00
|
|
|
static const char copyright[] =
|
1994-09-04 04:03:31 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-05-27 23:11:26 +00:00
|
|
|
static const char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93";
|
1994-09-04 04:03:31 +00:00
|
|
|
#endif /* not lint */
|
2003-05-05 09:52:25 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Phase of the Moon. Calculates the current phase of the moon.
|
|
|
|
* Based on routines from `Practical Astronomy with Your Calculator',
|
|
|
|
* by Duffett-Smith. Comments give the section from the book that
|
|
|
|
* particular piece of code was adapted from.
|
|
|
|
*
|
|
|
|
* -- Keith E. Brandt VIII 1984
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-01-05 21:14:48 +00:00
|
|
|
#include <stdlib.h>
|
1994-09-04 04:03:31 +00:00
|
|
|
#include <math.h>
|
2010-01-05 21:14:48 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1994-09-04 04:03:31 +00:00
|
|
|
|
1999-04-19 03:59:02 +00:00
|
|
|
#ifndef PI
|
|
|
|
#define PI 3.14159265358979323846
|
|
|
|
#endif
|
1994-09-04 04:03:31 +00:00
|
|
|
#define EPOCH 85
|
|
|
|
#define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */
|
|
|
|
#define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */
|
|
|
|
#define ECCEN 0.01671542 /* solar orbit eccentricity */
|
|
|
|
#define lzero 18.251907 /* lunar mean long at EPOCH */
|
|
|
|
#define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */
|
|
|
|
#define Nzero 55.204723 /* lunar mean long of node at EPOCH */
|
1996-05-27 23:11:26 +00:00
|
|
|
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2002-02-18 05:15:19 +00:00
|
|
|
static void adj360(double *);
|
|
|
|
static double dtor(double);
|
|
|
|
static double potm(double);
|
2010-01-05 21:14:48 +00:00
|
|
|
static void usage(char *progname);
|
1994-09-04 04:03:31 +00:00
|
|
|
|
1996-07-17 20:13:33 +00:00
|
|
|
int
|
2010-01-05 21:14:48 +00:00
|
|
|
main(int argc, char **argv)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
1996-05-27 23:11:26 +00:00
|
|
|
time_t tt;
|
2010-01-05 21:14:48 +00:00
|
|
|
struct tm GMT, tmd;
|
1994-09-04 04:03:31 +00:00
|
|
|
double days, today, tomorrow;
|
2010-07-15 00:16:04 +00:00
|
|
|
int ch, cnt, pflag = 0;
|
2010-01-06 00:20:37 +00:00
|
|
|
char *odate = NULL, *otime = NULL;
|
2012-01-28 15:29:43 +00:00
|
|
|
char *progname = argv[0];
|
2010-01-05 21:14:48 +00:00
|
|
|
|
2010-07-15 00:16:04 +00:00
|
|
|
while ((ch = getopt(argc, argv, "d:pt:")) != -1)
|
2010-01-05 21:14:48 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
|
|
|
odate = optarg;
|
|
|
|
break;
|
2010-07-15 00:16:04 +00:00
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
2010-01-05 21:14:48 +00:00
|
|
|
case 't':
|
|
|
|
otime = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
2012-01-28 15:29:43 +00:00
|
|
|
usage(progname);
|
2010-01-05 21:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc)
|
2012-01-28 15:29:43 +00:00
|
|
|
usage(progname);
|
2010-01-05 21:14:48 +00:00
|
|
|
|
|
|
|
/* Adjust based on users preferences */
|
|
|
|
time(&tt);
|
|
|
|
if (otime != NULL || odate != NULL) {
|
|
|
|
/* Save today in case -d isn't specified */
|
|
|
|
localtime_r(&tt, &tmd);
|
|
|
|
|
|
|
|
if (odate != NULL) {
|
|
|
|
tmd.tm_year = strtol(odate, NULL, 10) - 1900;
|
|
|
|
tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1;
|
|
|
|
tmd.tm_mday = strtol(odate + 8, NULL, 10);
|
|
|
|
/* Use midnight as the middle of the night */
|
|
|
|
tmd.tm_hour = 0;
|
|
|
|
tmd.tm_min = 0;
|
|
|
|
tmd.tm_sec = 0;
|
|
|
|
}
|
|
|
|
if (otime != NULL) {
|
|
|
|
tmd.tm_hour = strtol(otime, NULL, 10);
|
|
|
|
tmd.tm_min = strtol(otime + 3, NULL, 10);
|
|
|
|
tmd.tm_sec = strtol(otime + 6, NULL, 10);
|
|
|
|
}
|
|
|
|
tt = mktime(&tmd);
|
|
|
|
}
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2010-01-05 21:14:48 +00:00
|
|
|
gmtime_r(&tt, &GMT);
|
|
|
|
days = (GMT.tm_yday + 1) + ((GMT.tm_hour +
|
|
|
|
(GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0);
|
|
|
|
for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
|
1998-07-26 15:30:48 +00:00
|
|
|
days += isleap(1900 + cnt) ? 366 : 365;
|
1994-09-04 04:03:31 +00:00
|
|
|
today = potm(days) + .5;
|
2010-07-15 00:16:04 +00:00
|
|
|
if (pflag) {
|
|
|
|
(void)printf("%1.0f\n", today);
|
|
|
|
return (0);
|
|
|
|
}
|
1994-09-04 04:03:31 +00:00
|
|
|
(void)printf("The Moon is ");
|
|
|
|
if ((int)today == 100)
|
|
|
|
(void)printf("Full\n");
|
|
|
|
else if (!(int)today)
|
|
|
|
(void)printf("New\n");
|
|
|
|
else {
|
|
|
|
tomorrow = potm(days + 1);
|
|
|
|
if ((int)today == 50)
|
|
|
|
(void)printf("%s\n", tomorrow > today ?
|
|
|
|
"at the First Quarter" : "at the Last Quarter");
|
|
|
|
else {
|
|
|
|
(void)printf("%s ", tomorrow > today ?
|
|
|
|
"Waxing" : "Waning");
|
|
|
|
if (today > 50)
|
|
|
|
(void)printf("Gibbous (%1.0f%% of Full)\n",
|
|
|
|
today);
|
|
|
|
else if (today < 50)
|
|
|
|
(void)printf("Crescent (%1.0f%% of Full)\n",
|
|
|
|
today);
|
|
|
|
}
|
|
|
|
}
|
1996-07-17 20:13:33 +00:00
|
|
|
|
|
|
|
return 0;
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* potm --
|
|
|
|
* return phase of the moon
|
|
|
|
*/
|
1996-07-17 20:13:33 +00:00
|
|
|
static double
|
2005-05-01 19:34:22 +00:00
|
|
|
potm(double days)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
|
|
|
double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
|
|
|
|
double A4, lprime, V, ldprime, D, Nm;
|
|
|
|
|
|
|
|
N = 360 * days / 365.2422; /* sec 42 #3 */
|
|
|
|
adj360(&N);
|
|
|
|
Msol = N + EPSILONg - RHOg; /* sec 42 #4 */
|
|
|
|
adj360(&Msol);
|
|
|
|
Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */
|
|
|
|
LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */
|
|
|
|
adj360(&LambdaSol);
|
|
|
|
l = 13.1763966 * days + lzero; /* sec 61 #4 */
|
|
|
|
adj360(&l);
|
|
|
|
Mm = l - (0.1114041 * days) - Pzero; /* sec 61 #5 */
|
|
|
|
adj360(&Mm);
|
|
|
|
Nm = Nzero - (0.0529539 * days); /* sec 61 #6 */
|
|
|
|
adj360(&Nm);
|
|
|
|
Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */
|
|
|
|
Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */
|
|
|
|
A3 = 0.37 * sin(dtor(Msol));
|
|
|
|
Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */
|
|
|
|
Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */
|
|
|
|
A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */
|
|
|
|
lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */
|
|
|
|
V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */
|
|
|
|
ldprime = lprime + V; /* sec 61 #14 */
|
|
|
|
D = ldprime - LambdaSol; /* sec 63 #2 */
|
|
|
|
return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dtor --
|
|
|
|
* convert degrees to radians
|
|
|
|
*/
|
1996-07-17 20:13:33 +00:00
|
|
|
static double
|
2005-05-01 19:34:22 +00:00
|
|
|
dtor(double deg)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2010-01-05 21:14:48 +00:00
|
|
|
|
1994-09-04 04:03:31 +00:00
|
|
|
return(deg * PI / 180);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* adj360 --
|
|
|
|
* adjust value so 0 <= deg <= 360
|
|
|
|
*/
|
1996-07-17 20:13:33 +00:00
|
|
|
static void
|
2005-05-01 19:34:22 +00:00
|
|
|
adj360(double *deg)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2010-01-05 21:14:48 +00:00
|
|
|
|
1994-09-04 04:03:31 +00:00
|
|
|
for (;;)
|
|
|
|
if (*deg < 0)
|
|
|
|
*deg += 360;
|
|
|
|
else if (*deg > 360)
|
|
|
|
*deg -= 360;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2010-01-05 21:14:48 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(char *progname)
|
|
|
|
{
|
|
|
|
|
2010-07-15 00:16:04 +00:00
|
|
|
fprintf(stderr, "Usage: %s [-p] [-d yyyy.mm.dd] [-t hh:mm:ss]\n",
|
|
|
|
progname);
|
2010-01-05 21:14:48 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|