Home | History | Annotate | Line # | Download | only in pom
pom.c revision 1.5
      1 /*	$NetBSD: pom.c,v 1.5 1995/06/07 16:30:46 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software posted to USENET.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     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. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #ifndef lint
     39 static char copyright[] =
     40 "@(#) Copyright (c) 1989, 1993\n\
     41 	The Regents of the University of California.  All rights reserved.\n";
     42 #endif /* not lint */
     43 
     44 #ifndef lint
     45 #if 0
     46 static char sccsid[] = "@(#)pom.c	8.1 (Berkeley) 5/31/93";
     47 #else
     48 static char rcsid[] = "$NetBSD: pom.c,v 1.5 1995/06/07 16:30:46 cgd Exp $";
     49 #endif
     50 #endif /* not lint */
     51 
     52 /*
     53  * Phase of the Moon.  Calculates the current phase of the moon.
     54  * Based on routines from `Practical Astronomy with Your Calculator',
     55  * by Duffett-Smith.  Comments give the section from the book that
     56  * particular piece of code was adapted from.
     57  *
     58  * -- Keith E. Brandt  VIII 1984
     59  *
     60  */
     61 
     62 #include <sys/time.h>
     63 #include <stdio.h>
     64 #include <string.h>
     65 #include <tzfile.h>
     66 #include <math.h>
     67 
     68 #define	PI	  3.141592654
     69 #define	EPOCH	  85
     70 #define	EPSILONg  279.611371	/* solar ecliptic long at EPOCH */
     71 #define	RHOg	  282.680403	/* solar ecliptic long of perigee at EPOCH */
     72 #define	ECCEN	  0.01671542	/* solar orbit eccentricity */
     73 #define	lzero	  18.251907	/* lunar mean long at EPOCH */
     74 #define	Pzero	  192.917585	/* lunar mean long of perigee at EPOCH */
     75 #define	Nzero	  55.204723	/* lunar mean long of node at EPOCH */
     76 
     77 double dtor(), potm(), adj360();
     78 
     79 main()
     80 {
     81 	extern int errno;
     82 	struct timeval tp;
     83 	struct timezone tzp;
     84 	struct tm *GMT, *gmtime();
     85 	time_t tmpt;
     86 	double days, today, tomorrow;
     87 	int cnt;
     88 
     89 	if (gettimeofday(&tp,&tzp)) {
     90 		(void)fprintf(stderr, "pom: %s\n", strerror(errno));
     91 		exit(1);
     92 	}
     93 	tmpt = tp.tv_sec;
     94 	GMT = gmtime(&tmpt);
     95 	days = (GMT->tm_yday + 1) + ((GMT->tm_hour +
     96 	    (GMT->tm_min / 60.0) + (GMT->tm_sec / 3600.0)) / 24.0);
     97 	for (cnt = EPOCH; cnt < GMT->tm_year; ++cnt)
     98 		days += isleap(cnt) ? 366 : 365;
     99 	today = potm(days) + .5;
    100 	(void)printf("The Moon is ");
    101 	if ((int)today == 100)
    102 		(void)printf("Full\n");
    103 	else if (!(int)today)
    104 		(void)printf("New\n");
    105 	else {
    106 		tomorrow = potm(days + 1);
    107 		if ((int)today == 50)
    108 			(void)printf("%s\n", tomorrow > today ?
    109 			    "at the First Quarter" : "at the Last Quarter");
    110 		else {
    111 			(void)printf("%s ", tomorrow > today ?
    112 			    "Waxing" : "Waning");
    113 			if (today > 50)
    114 				(void)printf("Gibbous (%1.0f%% of Full)\n",
    115 				    today);
    116 			else if (today < 50)
    117 				(void)printf("Crescent (%1.0f%% of Full)\n",
    118 				    today);
    119 		}
    120 	}
    121 	exit(0);
    122 }
    123 
    124 /*
    125  * potm --
    126  *	return phase of the moon
    127  */
    128 double
    129 potm(days)
    130 	double days;
    131 {
    132 	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
    133 	double A4, lprime, V, ldprime, D, Nm;
    134 
    135 	N = 360 * days / 365.2422;				/* sec 42 #3 */
    136 	adj360(&N);
    137 	Msol = N + EPSILONg - RHOg;				/* sec 42 #4 */
    138 	adj360(&Msol);
    139 	Ec = 360 / PI * ECCEN * sin(dtor(Msol));		/* sec 42 #5 */
    140 	LambdaSol = N + Ec + EPSILONg;				/* sec 42 #6 */
    141 	adj360(&LambdaSol);
    142 	l = 13.1763966 * days + lzero;				/* sec 61 #4 */
    143 	adj360(&l);
    144 	Mm = l - (0.1114041 * days) - Pzero;			/* sec 61 #5 */
    145 	adj360(&Mm);
    146 	Nm = Nzero - (0.0529539 * days);			/* sec 61 #6 */
    147 	adj360(&Nm);
    148 	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 61 #7 */
    149 	Ac = 0.1858 * sin(dtor(Msol));				/* sec 61 #8 */
    150 	A3 = 0.37 * sin(dtor(Msol));
    151 	Mmprime = Mm + Ev - Ac - A3;				/* sec 61 #9 */
    152 	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 61 #10 */
    153 	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 61 #11 */
    154 	lprime = l + Ev + Ec - Ac + A4;				/* sec 61 #12 */
    155 	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 61 #13 */
    156 	ldprime = lprime + V;					/* sec 61 #14 */
    157 	D = ldprime - LambdaSol;				/* sec 63 #2 */
    158 	return(50 * (1 - cos(dtor(D))));			/* sec 63 #3 */
    159 }
    160 
    161 /*
    162  * dtor --
    163  *	convert degrees to radians
    164  */
    165 double
    166 dtor(deg)
    167 	double deg;
    168 {
    169 	return(deg * PI / 180);
    170 }
    171 
    172 /*
    173  * adj360 --
    174  *	adjust value so 0 <= deg <= 360
    175  */
    176 double
    177 adj360(deg)
    178 	double *deg;
    179 {
    180 	for (;;)
    181 		if (*deg < 0)
    182 			*deg += 360;
    183 		else if (*deg > 360)
    184 			*deg -= 360;
    185 		else
    186 			break;
    187 }
    188