/*	$NetBSD: pom.c,v 1.11 1998/09/11 14:07:04 hubertf Exp $	*/

/*
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    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.
 */

#include <sys/cdefs.h>

/*
 * 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
 *
 * Updated to the Third Edition of Duffett-Smith's book, Paul Janzen, IX 1998
 *
 */

#include <ctype.h>
#include <err.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#ifndef PI
#define	PI	  3.14159265358979323846
#endif

/*
 * The EPOCH in the third edition of the book is 1990 Jan 0.0 TDT.
 * In this program, we do not bother to correct for the differences
 * between UTC (as shown by the UNIX clock) and TDT.  (TDT = TAI + 32.184s;
 * TAI-UTC = 32s in Jan 1999.)
 */
#define EPOCH_MINUS_1970	(20 * 365 + 5 - 1)	/* 20 years, 5 leaps, back 1 day to Jan 0 */
#define	EPSILONg		279.403303		/* solar ecliptic long at EPOCH */
#define	RHOg			282.768422		/* solar ecliptic long of perigee at EPOCH */
#define	ECCEN			0.01671022		/* solar orbit eccentricity */
#define	lzero			318.351648		/* lunar mean long at EPOCH */
#define	Pzero			36.340410		/* lunar mean long of perigee at EPOCH */
#define	Nzero			318.510107		/* lunar mean long of node at EPOCH */

#define ASCEN	8
#define DSCEN	10
#define SPACE	32

/*
 * dtor --
 *	convert degrees to radians
 */
double dtor(deg)
	double deg;
{
	return(deg * PI / 180);
}

/*
 * adj360 --
 *	adjust value so 0 <= deg <= 360
 */
void adj360(deg)
	double *deg;
{
	for (;;)
		if (*deg < 0)
			*deg += 360;
		else if (*deg > 360)
			*deg -= 360;
		else
			break;
}

float get_moon(float days) {

	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
	double A4, lprime, V, ldprime, D, Nm;

	N = 360 * days / 365.242191;				/* sec 46 #3 */
	adj360(&N);
	Msol = N + EPSILONg - RHOg;				/* sec 46 #4 */
	adj360(&Msol);
	Msol = dtor(Msol);
	Ec = 360 / PI * ECCEN * sin(Msol);			/* sec 46 #5 */
	LambdaSol = N + Ec + EPSILONg;				/* sec 46 #6 */
	adj360(&LambdaSol);
	l = 13.1763966 * days + lzero;				/* sec 65 #4 */
	adj360(&l);
	Mm = l - (0.1114041 * days) - Pzero;			/* sec 65 #5 */
	adj360(&Mm);
	Nm = Nzero - (0.0529539 * days);			/* sec 65 #6 */
	adj360(&Nm);
	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 65 #7 */
	Ac = 0.1858 * sin(Msol);				/* sec 65 #8 */
	A3 = 0.37 * sin(Msol);
	Mmprime = Mm + Ev - Ac - A3;				/* sec 65 #9 */
	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 65 #10 */
	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 65 #11 */
	lprime = l + Ev + Ec - Ac + A4;				/* sec 65 #12 */
	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 65 #13 */
	ldprime = lprime + V;					/* sec 65 #14 */
	D = ldprime - LambdaSol;				/* sec 67 #2 */
	return (50 * (1 - cos(dtor(D))));
}

/*
 * potm --
 *	return phase of the moon
 */
void get_phase(time_t the_time, char char_buf[20]) {

	float days, now, then;
	int i;

	days = (the_time - EPOCH_MINUS_1970 * 86400) / 86400.0;
	now = get_moon(days);
	then = get_moon(days + 0.05);				/* an hour and twelve minutes ahead */
	i = (now<then) ? ASCEN : DSCEN;
	/* display quarters from about six hours before to six hours after */
	if (now<=0.1) {
		(void)sprintf(char_buf, "%8s", "New|Moon");
	} else if (now>=99.9) {
		(void)sprintf(char_buf, "%9s", "Full|Moon");
	} else if (now>47.3 && now<52.7) {
		if (i==ASCEN) {
			(void)sprintf(char_buf, "%12s", "Moon|@|1st|Q");
		} else {
			(void)sprintf(char_buf, "%12s", "Moon|@|3rd|Q");
		}
	} else {
		char_buf[10] = i;
		i = rint(now * 10);
		char_buf[8] = (i % 10) + 48;			/* convert digit to ascii */
		i /= 10;
		char_buf[6] = (i % 10) + 48;
		i /= 10;
		char_buf[5] = i ? (i % 10) + 48 : SPACE;
	}
}

