pom.c revision 17201
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#ifndef lint 37static const char copyright[] = 38"@(#) Copyright (c) 1989, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43static const char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93"; 44#endif /* not lint */ 45 46/* 47 * Phase of the Moon. Calculates the current phase of the moon. 48 * Based on routines from `Practical Astronomy with Your Calculator', 49 * by Duffett-Smith. Comments give the section from the book that 50 * particular piece of code was adapted from. 51 * 52 * -- Keith E. Brandt VIII 1984 53 * 54 */ 55 56#include <time.h> 57#include <stdio.h> 58#include <math.h> 59 60#define PI 3.141592654 61#define EPOCH 85 62#define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */ 63#define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */ 64#define ECCEN 0.01671542 /* solar orbit eccentricity */ 65#define lzero 18.251907 /* lunar mean long at EPOCH */ 66#define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */ 67#define Nzero 55.204723 /* lunar mean long of node at EPOCH */ 68#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 69 70static void adj360 __P((double *)); 71static double dtor __P((double)); 72static double potm __P((double)); 73 74int 75main() 76{ 77 time_t tt; 78 struct tm *GMT; 79 double days, today, tomorrow; 80 int cnt; 81 82 (void) time(&tt); 83 GMT = gmtime(&tt); 84 days = (GMT->tm_yday + 1) + ((GMT->tm_hour + 85 (GMT->tm_min / 60.0) + (GMT->tm_sec / 3600.0)) / 24.0); 86 for (cnt = EPOCH; cnt < GMT->tm_year; ++cnt) 87 days += isleap(cnt) ? 366 : 365; 88 today = potm(days) + .5; 89 (void)printf("The Moon is "); 90 if ((int)today == 100) 91 (void)printf("Full\n"); 92 else if (!(int)today) 93 (void)printf("New\n"); 94 else { 95 tomorrow = potm(days + 1); 96 if ((int)today == 50) 97 (void)printf("%s\n", tomorrow > today ? 98 "at the First Quarter" : "at the Last Quarter"); 99 else { 100 (void)printf("%s ", tomorrow > today ? 101 "Waxing" : "Waning"); 102 if (today > 50) 103 (void)printf("Gibbous (%1.0f%% of Full)\n", 104 today); 105 else if (today < 50) 106 (void)printf("Crescent (%1.0f%% of Full)\n", 107 today); 108 } 109 } 110 111 return 0; 112} 113 114/* 115 * potm -- 116 * return phase of the moon 117 */ 118static double 119potm(days) 120 double days; 121{ 122 double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime; 123 double A4, lprime, V, ldprime, D, Nm; 124 125 N = 360 * days / 365.2422; /* sec 42 #3 */ 126 adj360(&N); 127 Msol = N + EPSILONg - RHOg; /* sec 42 #4 */ 128 adj360(&Msol); 129 Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */ 130 LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */ 131 adj360(&LambdaSol); 132 l = 13.1763966 * days + lzero; /* sec 61 #4 */ 133 adj360(&l); 134 Mm = l - (0.1114041 * days) - Pzero; /* sec 61 #5 */ 135 adj360(&Mm); 136 Nm = Nzero - (0.0529539 * days); /* sec 61 #6 */ 137 adj360(&Nm); 138 Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */ 139 Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */ 140 A3 = 0.37 * sin(dtor(Msol)); 141 Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */ 142 Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */ 143 A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */ 144 lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */ 145 V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */ 146 ldprime = lprime + V; /* sec 61 #14 */ 147 D = ldprime - LambdaSol; /* sec 63 #2 */ 148 return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */ 149} 150 151/* 152 * dtor -- 153 * convert degrees to radians 154 */ 155static double 156dtor(deg) 157 double deg; 158{ 159 return(deg * PI / 180); 160} 161 162/* 163 * adj360 -- 164 * adjust value so 0 <= deg <= 360 165 */ 166static void 167adj360(deg) 168 double *deg; 169{ 170 for (;;) 171 if (*deg < 0) 172 *deg += 360; 173 else if (*deg > 360) 174 *deg -= 360; 175 else 176 break; 177} 178