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