home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / calentool / part05 / moon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-27  |  22.9 KB  |  729 lines

  1. /*
  2.  * $Header: moon.c,v 2.1 89/05/09 14:19:22 billr Exp $
  3.  */
  4. /*
  5.  * moon.c
  6.  *
  7.  * Compute various interesting data about the moon.  Based on the
  8.  * moontool program by John Walker (see below), and modified by
  9.  * Bill Randle, Tektronix, Inc. to interface to the calentool
  10.  * program.
  11.  */
  12. #include "ct.h"        /* for the NO_SUN_MOON #define */
  13. #ifndef NO_SUN_MOON
  14.  
  15. /*
  16.  
  17.     A Moon for the Sun
  18.  
  19.     Release 2.0
  20.  
  21.     Designed and implemented by John Walker in December 1987,
  22.     revised and updated in February of 1988.
  23.  
  24.     cc -O moontool.c -o moontool -lm -lsuntool -lsunwindow -lpixrect
  25.  
  26.     Adding  appropriate  floating  point  options  to your hardware.  This
  27.     program is a SunView tool which displays, as the  icon  for  a  closed
  28.     window,  the  current phase of the Moon.  A subtitle in the icon gives
  29.     the age of the Moon in days  and  hours.   If  called  with  the  "-t"
  30.     switch,  it  rapidly  increments  forward  through time to display the
  31.     cycle of phases.
  32.  
  33.     If you open the window, additional information is displayed  regarding
  34.     the  Moon.     The  information  is  generally  accurate  to    within ten
  35.     minutes.
  36.  
  37.     The algorithms used in this program to calculate the positions Sun and
  38.     Moon as seen from the Earth are given in the book "Practical Astronomy
  39.     With  Your  Calculator"  by  Peter  Duffett-Smith,   Second   Edition,
  40.     Cambridge University Press, 1981.  Ignore the word "Calculator" in the
  41.     title;  this  is  an  essential  reference  if  you're  interested  in
  42.     developing    software  which  calculates  planetary    positions, orbits,
  43.     eclipses, and  the  like.   If  you're  interested  in  pursuing  such
  44.     programming, you should also obtain:
  45.  
  46.     "Astronomical  Formulae for Calculators" by Jean Meeus, Third Edition,
  47.     Willmann-Bell, 1985.  A must-have.
  48.  
  49.     "Planetary  Programs  and  Tables  from  -4000  to  +2800"  by  Pierre
  50.     Bretagnon  and Jean-Louis Simon, Willmann-Bell, 1986.  If you want the
  51.     utmost  (outside  of  JPL)  accuracy  for  the  planets,  it's   here.
  52.  
  53.     "Celestial BASIC" by Eric Burgess, Revised Edition, Sybex, 1985.  Very
  54.     cookbook oriented, and many of the algorithms are hard to dig  out    of
  55.     the turgid BASIC code, but you'll probably want it anyway.
  56.  
  57.     Many of these references can be obtained from Willmann-Bell, P.O.  Box
  58.     35025,  Richmond,  VA 23235, USA.  Phone: (804) 320-7016.  In addition
  59.     to their own publications, they stock most of the standard    references
  60.     for mathematical and positional astronomy.
  61.  
  62.     This program was written by:
  63.  
  64.        John Walker
  65.        Autodesk, Inc.
  66.        2320 Marinship Way
  67.        Sausalito, CA  94965
  68.        (415) 332-2344 Ext. 829
  69.  
  70.        Usenet: {sun!well}!acad!kelvin
  71.  
  72.     This  program is in the public domain: "Do what thou wilt shall be the
  73.     whole of the law".  I'd appreciate  receiving  any  bug  fixes  and/or
  74.     enhancements,  which  I'll  incorporate  in  future  versions  of  the
  75.     program.  Please leave the original attribution information intact    so
  76.     that credit and blame may be properly apportioned.
  77.  
  78. */
  79.  
  80. /*  Astronomical constants  */
  81.  
  82. #define epoch        2444238.5       /* 1980 January 0.0 */
  83. #define    J1970        2440587.5       /* VAX clock Epoch 1970 Jan 1 (0h UT) */
  84.  
  85. /*  Constants defining the Sun's apparent orbit  */
  86.  
  87. #define elonge        278.833540       /* Ecliptic longitude of the Sun
  88.                       at epoch 1980.0 */
  89. #define elongp        282.596403       /* Ecliptic longitude of the Sun at
  90.                       perigee */
  91. #define eccent      0.016718       /* Eccentricity of Earth's orbit */
  92. #define sunsmax     1.495985e8     /* Semi-major axis of Earth's orbit, km */
  93. #define sunangsiz   0.533128       /* Sun's angular size, degrees, at
  94.                       semi-major axis distance */
  95.  
  96. /*  Elements of the Moon's orbit, epoch 1980.0  */
  97.  
  98. #define mmlong      64.975464      /* Moon's mean lonigitude at the epoch */
  99. #define mmlongp     349.383063       /* Mean longitude of the perigee at the
  100.                       epoch */
  101. #define mlnode        151.950429       /* Mean longitude of the node at the
  102.                       epoch */
  103. #define minc        5.145396       /* Inclination of the Moon's orbit */
  104. #define mecc        0.054900       /* Eccentricity of the Moon's orbit */
  105. #define mangsiz     0.5181         /* Moon's angular size at distance a
  106.                       from Earth */
  107. #define msmax       384401.0       /* Semi-major axis of Moon's orbit in km */
  108. #define mparallax   0.9507       /* Parallax at distance a from Earth */
  109. #define synmonth    29.53058868    /* Synodic month (new Moon to new Moon) */
  110. #define lunatbase   2423436.0      /* Base date for E. W. Brown's numbered
  111.                       series of lunations (1923 January 16) */
  112.  
  113. /*  Properties of the Earth  */
  114.  
  115. #define earthrad    6378.16       /* Radius of Earth in kilometres */
  116.  
  117. #include <stdio.h>
  118. #include <math.h>
  119. #include <sys/time.h>
  120.  
  121. #include <suntool/sunview.h>
  122. #include <suntool/canvas.h>
  123.  
  124. /* Icon definition.  This is just a black field with rounded corners
  125.    that blend into the root desktop pattern. The image of the moon and
  126.    the text are added by the program later. */
  127.  
  128. static short moon_img[64][4] = {
  129. /* Format_version=1, Width=64, Height=64, Depth=1, Valid_bits_per_item=16 */
  130.     0x8FFF,0xFFFF,0xFFFF,0xFFE8,0x9FFF,0xFFFF,0xFFFF,0xFFF8,
  131.     0x3FFF,0xFFFF,0xFFFF,0xFFFE,0x7FFF,0xFFFF,0xFFFF,0xFFFE,
  132.     0xFFFF,0xFFFF,0xFFFF,0xFFFE,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  133.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  134.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  135.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  136.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  137.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  138.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  139.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  140.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  141.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  142.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  143.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  144.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  145.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  146.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  147.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  148.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  149.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  150.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  151.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  152.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  153.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  154.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  155.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  156.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  157.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  158.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  159.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x7FFF,0xFFFF,0xFFFF,0xFFFE,
  160.     0xFFFF,0xFFFF,0xFFFF,0xFFFE,0xBFFF,0xFFFF,0xFFFF,0xFFFC,
  161.     0x3FFF,0xFFFF,0xFFFF,0xFFFA,0x27FF,0xFFFF,0xFFFF,0xFFE2
  162. };
  163. DEFINE_ICON_FROM_IMAGE(moon_icon, moon_img);
  164. mpr_static(icon_mpr, 64, 64, 1, moon_img);
  165.  
  166. extern Canvas mcanvas;
  167. extern Pixfont *font;
  168. extern char *monthnames[];
  169. static Pixwin *cpw;
  170. static int charhgt, charwid;
  171.  
  172. #define PI 3.14159265358979323846  /* Assume not near black hole nor in
  173.                       Tennessee */
  174.  
  175. /*  Handy mathematical functions  */
  176.  
  177. #define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0))      /* Extract sign */
  178. #define abs(x) ((x) < 0 ? (-(x)) : (x))           /* Absolute val */
  179. #define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0)))  /* Fix angle      */
  180. #define torad(d) ((d) * (PI / 180.0))              /* Deg->Rad      */
  181. #define todeg(d) ((d) * (180.0 / PI))              /* Rad->Deg      */
  182. #define dsin(x) (sin(torad((x))))              /* Sin from deg */
  183. #define dcos(x) (cos(torad((x))))              /* Cos from deg */
  184.  
  185. /*  Forward functions  */
  186.  
  187. double jtime(), phase();
  188. void phasehunt();
  189. void drawmoon(), jyear(), jhms();
  190.  
  191.  
  192. /*  DRAWMOON  --  Construct icon for moon, given phase of moon.  */
  193.  
  194. static void drawmoon(ph)
  195. double ph;
  196. {
  197.     int i, j, lx, rx;
  198.     int lb[4];
  199.     double cp, xscale;
  200.  
  201.     xscale = cos(2 * PI * ph);
  202.     for (i = 0; i < 24; i++) {
  203.        lb[0] = lb[1] = lb[2] = lb[3] = 0xFFFF;
  204.        cp = 24.0 * cos(asin(i / 24.0));
  205.        if (ph < 0.5) {
  206.           rx = 32 + cp;
  207.           lx = 32 + xscale * cp;
  208.        } else {
  209.           lx = 33 - cp;
  210.           rx = 33 - xscale * cp;
  211.        }
  212.        for (j = lx; j <= rx; j++) {
  213.           lb[j >> 4] &= (0x8000 >> (j & 0xF)) ^ 0xFFFF;
  214.        }
  215.        for (j = 0; j < 4; j++)
  216.           moon_img[28 + i][j] = moon_img[28 - i][j] = lb[j];
  217.     }
  218. }
  219.  
  220. /*  MOON_DATA  -- print useful info about the moon */
  221.  
  222. moon_data(seconds)
  223. long seconds;
  224. {
  225.     int lunation, wclosed;
  226.     long t;
  227.     double jd, p, aom, cphase, cdist, cangdia, csund, csuang, lptime;
  228.     double phasar[5];
  229.     struct pr_prpos tloc;
  230.     char amsg[12], tbuf[80];
  231.     static double faketime = 0.0;
  232.     static short moonilast[64][4] = {0};
  233.     int yy, mm, dd, hh, mmm, ss;
  234.     Pixfont *pfont;
  235.     struct tm *gm, *ltm, *localtime();
  236.     struct timeval tvp;
  237.     struct timezone tzp;
  238.     long clock;
  239.     char *atp, *asctime();
  240.  
  241.  
  242.     pfont = pf_open("/usr/lib/fonts/fixedwidthfonts/screen.r.7");
  243.     cpw = canvas_pixwin(mcanvas);
  244.     charwid = font->pf_defaultsize.x;
  245.     charhgt = font->pf_defaultsize.y;
  246.     jd = jtime((gm = gmtime(&seconds)));
  247.     gettimeofday(&tvp, &tzp); /* for timezone info */
  248.     p = phase(jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
  249.     drawmoon(p);
  250.         sprintf(amsg, " %dd %dh  ",
  251.        (int) aom, ((int) (24 * (aom - floor(aom)))));
  252.     tloc.pr = (Pixrect *) icon_get(&moon_icon, ICON_IMAGE);
  253.     tloc.pos.x = 2;
  254.     tloc.pos.y = 62;
  255.     pf_text(tloc, PIX_NOT(PIX_SRC), pfont, amsg);
  256.  
  257.     /* Only update icon if it changed (this eliminates gratuitous
  258.        flashing of the icon on-screen). */
  259.  
  260.     if (bcmp(moonilast, moon_img, sizeof moon_img) != 0)
  261.        bcopy(moon_img, moonilast, sizeof moon_img);
  262.  
  263.     /* Update textual information for open window */
  264.  
  265. #define prt(x) pw_text(cpw, charwid, charhgt * (x), PIX_SRC, font, tbuf)
  266. #define prtxy(x,y) pw_text(cpw,charwid*(y+1),charhgt*(x),PIX_SRC,font,tbuf)
  267.  
  268.         sprintf(tbuf, "Moon phase:      %d%%   [0%% = New, 100%% = Full]  ",
  269.        (int) (cphase * 100));
  270.     prt(1);
  271.  
  272.     /* Information about the Moon */
  273.  
  274. #define EPL(x) (x), (x) == 1 ? "" : "s"
  275.     sprintf(tbuf,
  276.            "Age of moon:     %d day%s, %d hour%s, %d minute%s.       ",
  277.        EPL((int) aom), EPL(((int) (24 * (aom - floor(aom))))),
  278.        EPL(((int) (1440 * (aom - floor(aom)))) % 60));
  279.     prt(2);
  280.     sprintf(tbuf,
  281.            "Moon's distance: %ld kilometres, %.1f Earth radii.  ",
  282.        (long) cdist, cdist / earthrad);
  283.     prt(3);
  284.     sprintf(tbuf,
  285.            "Moon subtends:   %.4f degrees.       ", cangdia);
  286.     prt(4);
  287.     /* Draw the moon icon in the text window */
  288.     pw_rop(cpw, 60 * charwid, charhgt, 64, 64, PIX_SRC,
  289.        &icon_mpr, 0, 0);
  290.  
  291.  
  292.     /* Calculate times of phases of this lunation. */
  293.  
  294. #define APOS(x) (x + 11)
  295.     phasehunt(jd, phasar);
  296.     lptime = phasar[0];
  297.     lunation = floor(((lptime + 7) - lunatbase) / synmonth) + 1;
  298.     /* convert to seconds local time, so we can use localtime()
  299.        to handle dst calculations */
  300.     clock = (long)((lptime - J1970) * 24. * 3600.);
  301.     ltm = localtime(&clock);
  302.     sprintf(tbuf,
  303.            "Last new moon:   %02d:%02d %s %2d %s %d             ",
  304.        ltm->tm_hour, ltm->tm_min, timezone(tzp.tz_minuteswest, ltm->tm_isdst), ltm->tm_mday, monthnames[ltm->tm_mon], ltm->tm_year+1900);
  305.     prt(APOS(0));
  306.         sprintf(tbuf, "Lunation %d    ", lunation);
  307.     prtxy(APOS(0), 52);
  308.  
  309.     lptime = phasar[1];
  310.     /* convert to seconds local time, so we can use localtime()
  311.        to handle dst calculations */
  312.     clock = (long)((lptime - J1970) * 24. * 3600.);
  313.     ltm = localtime(&clock);
  314.     sprintf(tbuf,
  315.            "First quarter:   %02d:%02d %s %2d %s %d              ",
  316.        ltm->tm_hour, ltm->tm_min, timezone(tzp.tz_minuteswest, ltm->tm_isdst), ltm->tm_mday, monthnames[ltm->tm_mon], ltm->tm_year+1900);
  317.     prt(APOS(1));
  318.  
  319.     lptime = phasar[2];
  320.     /* convert to seconds local time, so we can use localtime()
  321.        to handle dst calculations */
  322.     clock = (long)((lptime - J1970) * 24. * 3600.);
  323.     ltm = localtime(&clock);
  324.     sprintf(tbuf,
  325.            "Full moon:       %02d:%02d %s %2d %s %d              ",
  326.        ltm->tm_hour, ltm->tm_min, timezone(tzp.tz_minuteswest, ltm->tm_isdst), ltm->tm_mday, monthnames[ltm->tm_mon], ltm->tm_year+1900);
  327.     prt(APOS(2));
  328.  
  329.     lptime = phasar[3];
  330.     /* convert to seconds local time, so we can use localtime()
  331.        to handle dst calculations */
  332.     clock = (long)((lptime - J1970) * 24. * 3600.);
  333.     ltm = localtime(&clock);
  334.     sprintf(tbuf,
  335.            "Last quarter:    %02d:%02d %s %2d %s %d              ",
  336.        ltm->tm_hour, ltm->tm_min, timezone(tzp.tz_minuteswest, ltm->tm_isdst), ltm->tm_mday, monthnames[ltm->tm_mon], ltm->tm_year+1900);
  337.     prt(APOS(3));
  338.  
  339.     lptime = phasar[4];
  340.     /* convert to seconds local time, so we can use localtime()
  341.        to handle dst calculations */
  342.     clock = (long)rint((lptime - J1970) * 24. * 3600.);
  343.     ltm = localtime(&clock);
  344.     sprintf(tbuf,
  345.            "Next new moon:   %02d:%02d %s %2d %s %d              ",
  346.        ltm->tm_hour, ltm->tm_min, timezone(tzp.tz_minuteswest, ltm->tm_isdst), ltm->tm_mday, monthnames[ltm->tm_mon], ltm->tm_year+1900);
  347.     prt(APOS(4));
  348.         sprintf(tbuf, "Lunation %d    ", lunation + 1);
  349.     prtxy(APOS(4), 52);
  350. #undef APOS
  351. }
  352.  
  353. /*  JDATE  --  Convert internal GMT date and time to Julian day
  354.            and fraction.  */
  355.  
  356. static long jdate(t)
  357. struct tm *t;
  358. {
  359.     long c, m, y;
  360.  
  361.     y = t->tm_year + 1900;
  362.     m = t->tm_mon + 1;
  363.     if (m > 2)
  364.        m = m - 3;
  365.     else {
  366.        m = m + 9;
  367.        y--;
  368.     }
  369.     c = y / 100L;           /* Compute century */
  370.     y -= 100L * c;
  371.     return t->tm_mday + (c * 146097L) / 4 + (y * 1461L) / 4 +
  372.         (m * 153L + 2) / 5 + 1721119L;
  373. }
  374.  
  375. /* JTIME --    Convert internal GMT date and time to astronomical Julian
  376.            time (i.e. Julian date plus day fraction, expressed as
  377.            a double).  */
  378.  
  379. static double jtime(t)
  380. struct tm *t;
  381. {
  382.     return (jdate(t) - 0.5) + 
  383.        (t->tm_sec + 60 * (t->tm_min + 60 * t->tm_hour)) / 86400.0;
  384. }
  385.  
  386. /*  JYEAR  --  Convert Julian date to year, month, day, which are
  387.            returned via integer pointers to integers.  */
  388.  
  389. static void jyear(td, yy, mm, dd)
  390. double td;
  391. int *yy, *mm, *dd;
  392. {
  393.     double j, d, y, m;
  394.  
  395.     td += 0.5;           /* Astronomical to civil */
  396.     j = floor(td);
  397.     j = j - 1721119.0;
  398.     y = floor(((4 * j) - 1) / 146097.0);
  399.     j = (j * 4.0) - (1.0 + (146097.0 * y));
  400.     d = floor(j / 4.0);
  401.     j = floor(((4.0 * d) + 3.0) / 1461.0);
  402.     d = ((4.0 * d) + 3.0) - (1461.0 * j);
  403.     d = floor((d + 4.0) / 4.0);
  404.     m = floor(((5.0 * d) - 3) / 153.0);
  405.     d = (5.0 * d) - (3.0 + (153.0 * m));
  406.     d = floor((d + 5.0) / 5.0);
  407.     y = (100.0 * y) + j;
  408.     if (m < 10.0)
  409.        m = m + 3;
  410.     else {
  411.        m = m - 9;
  412.        y = y + 1;
  413.     }
  414.     *yy = y;
  415.     *mm = m;
  416.     *dd = d;
  417. }
  418.  
  419. /*  JHMS  --  Convert Julian time to hour, minutes, and seconds.  */
  420.  
  421. static void jhms(j, h, m, s)
  422. double j;
  423. int *h, *m, *s;
  424. {
  425.     long ij;
  426.  
  427.     j += 0.5;           /* Astronomical to civil */
  428.     ij = (j - floor(j)) * 86400.0;
  429.     *h = ij / 3600L;
  430.     *m = (ij / 60L) % 60L;
  431.     *s = ij % 60L;
  432. }
  433.  
  434. /*  MEANPHASE  --  Calculates mean phase of the Moon for a given
  435.            base date and desired phase:
  436.                0.0   New Moon
  437.                0.25  First quarter
  438.                0.5   Full moon
  439.                0.75  Last quarter
  440.             Beware!!!  This routine returns meaningless
  441.                     results for any other phase arguments.  Don't
  442.             attempt to generalise it without understanding
  443.             that the motion of the moon is far more complicated
  444.             that this calculation reveals. */
  445.  
  446. static double meanphase(sdate, phase, usek)
  447. double sdate, phase;
  448. double *usek;
  449. {
  450.     int yy, mm, dd;
  451.     double k, t, t2, t3, nt1;
  452.  
  453.     jyear(sdate, &yy, &mm, &dd);
  454.  
  455.     k = (yy + ((mm - 1) * (1.0 / 12.0)) - 1900) * 12.3685;
  456.  
  457.     /* Time in Julian centuries from 1900 January 0.5 */
  458.     t = (sdate - 2415020.0) / 36525;
  459.     t2 = t * t;           /* Square for frequent use */
  460.     t3 = t2 * t;           /* Cube for frequent use */
  461.  
  462.     *usek = k = floor(k) + phase;
  463.     nt1 = 2415020.75933 + synmonth * k
  464.           + 0.0001178 * t2
  465.           - 0.000000155 * t3
  466.           + 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
  467.  
  468.     return nt1;
  469. }
  470.  
  471. /*  TRUEPHASE  --  Given a K value used to determine the
  472.            mean phase of the new moon, and a phase
  473.            selector (0.0, 0.25, 0.5, 0.75), obtain
  474.            the true, corrected phase time.  */
  475.  
  476. static double truephase(k, phase)
  477. double k, phase;
  478. {
  479.     double t, t2, t3, pt, m, mprime, f;
  480.     int apcor = FALSE;
  481.  
  482.     k += phase;           /* Add phase to new moon time */
  483.     t = k / 1236.85;       /* Time in Julian centuries from
  484.                       1900 January 0.5 */
  485.     t2 = t * t;           /* Square for frequent use */
  486.     t3 = t2 * t;           /* Cube for frequent use */
  487.     pt = 2415020.75933       /* Mean time of phase */
  488.          + synmonth * k
  489.          + 0.0001178 * t2
  490.          - 0.000000155 * t3
  491.          + 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
  492.  
  493.         m = 359.2242               /* Sun's mean anomaly */
  494.         + 29.10535608 * k
  495.         - 0.0000333 * t2
  496.         - 0.00000347 * t3;
  497.         mprime = 306.0253          /* Moon's mean anomaly */
  498.         + 385.81691806 * k
  499.         + 0.0107306 * t2
  500.         + 0.00001236 * t3;
  501.         f = 21.2964                /* Moon's argument of latitude */
  502.         + 390.67050646 * k
  503.         - 0.0016528 * t2
  504.         - 0.00000239 * t3;
  505.     if ((phase < 0.01) || (abs(phase - 0.5) < 0.01)) {
  506.  
  507.        /* Corrections for New and Full Moon */
  508.  
  509.        pt +=     (0.1734 - 0.000393 * t) * dsin(m)
  510.             + 0.0021 * dsin(2 * m)
  511.             - 0.4068 * dsin(mprime)
  512.             + 0.0161 * dsin(2 * mprime)
  513.             - 0.0004 * dsin(3 * mprime)
  514.             + 0.0104 * dsin(2 * f)
  515.             - 0.0051 * dsin(m + mprime)
  516.             - 0.0074 * dsin(m - mprime)
  517.             + 0.0004 * dsin(2 * f + m)
  518.             - 0.0004 * dsin(2 * f - m)
  519.             - 0.0006 * dsin(2 * f + mprime)
  520.             + 0.0010 * dsin(2 * f - mprime)
  521.             + 0.0005 * dsin(m + 2 * mprime);
  522.        apcor = TRUE;
  523.     } else if ((abs(phase - 0.25) < 0.01 || (abs(phase - 0.75) < 0.01))) {
  524.        pt +=     (0.1721 - 0.0004 * t) * dsin(m)
  525.             + 0.0021 * dsin(2 * m)
  526.             - 0.6280 * dsin(mprime)
  527.             + 0.0089 * dsin(2 * mprime)
  528.             - 0.0004 * dsin(3 * mprime)
  529.             + 0.0079 * dsin(2 * f)
  530.             - 0.0119 * dsin(m + mprime)
  531.             - 0.0047 * dsin(m - mprime)
  532.             + 0.0003 * dsin(2 * f + m)
  533.             - 0.0004 * dsin(2 * f - m)
  534.             - 0.0006 * dsin(2 * f + mprime)
  535.             + 0.0021 * dsin(2 * f - mprime)
  536.             + 0.0003 * dsin(m + 2 * mprime)
  537.             + 0.0004 * dsin(m - 2 * mprime)
  538.             - 0.0003 * dsin(2 * m + mprime);
  539.        if (phase < 0.5)
  540.           /* First quarter correction */
  541.           pt += 0.0028 - 0.0004 * dcos(m) + 0.0003 * dcos(mprime);
  542.        else
  543.           /* Last quarter correction */
  544.           pt += -0.0028 + 0.0004 * dcos(m) - 0.0003 * dcos(mprime);
  545.        apcor = TRUE;
  546.     }
  547.     if (!apcor) {
  548.            fprintf(stderr, "TRUEPHASE called with invalid phase selector.\n");
  549.        abort();
  550.     }
  551.     return pt;
  552. }
  553.  
  554. /*  PHASEHUNT  --  Find time of phases of the moon which surround
  555.            the current date.  Five phases are found, starting
  556.            and ending with the new moons which bound the
  557.            current lunation.  */
  558.  
  559. static void phasehunt(sdate, phases)
  560. double sdate;
  561. double phases[5];
  562. {
  563.     double adate, k1, k2, nt1, nt2;
  564.  
  565.     adate = sdate - 45;
  566.     nt1 = meanphase(adate, 0.0, &k1);
  567.     while (TRUE) {
  568.        adate += synmonth;
  569.        nt2 = meanphase(adate, 0.0, &k2);
  570.        if (nt1 <= sdate && nt2 > sdate)
  571.           break;
  572.        nt1 = nt2;
  573.        k1 = k2;
  574.     }
  575.     phases[0] = truephase(k1, 0.0);
  576.     phases[1] = truephase(k1, 0.25);
  577.     phases[2] = truephase(k1, 0.5);
  578.     phases[3] = truephase(k1, 0.75);
  579.     phases[4] = truephase(k2, 0.0);
  580. }
  581.  
  582. /*  KEPLER  --    Solve the equation of Kepler.  */
  583.  
  584. static double kepler(m, ecc)
  585. double m, ecc;
  586. {
  587.     double e, delta;
  588. #define EPSILON 1E-6
  589.  
  590.     e = m = torad(m);
  591.     do {
  592.        delta = e - ecc * sin(e) - m;
  593.        e -= delta / (1 - ecc * cos(e));
  594.     } while (abs(delta) > EPSILON);
  595.     return e;
  596. }
  597.  
  598. /*  PHASE  --  Calculate phase of moon as a fraction:
  599.  
  600.     The argument is the time for which the phase is requested,
  601.     expressed as a Julian date and fraction.  Returns the terminator
  602.     phase angle as a percentage of a full circle (i.e., 0 to 1),
  603.     and stores into pointer arguments the illuminated fraction of
  604.         the Moon's disc, the Moon's age in days and fraction, the
  605.     distance of the Moon from the centre of the Earth, and the
  606.     angular diameter subtended by the Moon as seen by an observer
  607.     at the centre of the Earth.
  608.  
  609. */
  610.  
  611. static double phase(pdate, pphase, mage, dist, angdia, sudist, suangdia)
  612. double pdate;
  613. double *pphase;            /* Illuminated fraction */
  614. double *mage;               /* Age of moon in days */
  615. double *dist;               /* Distance in kilometres */
  616. double *angdia;            /* Angular diameter in degrees */
  617. double *sudist;            /* Distance to Sun */
  618. double *suangdia;                  /* Sun's angular diameter */
  619. {
  620.  
  621.     double Day, N, M, Ec, Lambdasun, ml, MM, MN, Ev, Ae, A3, MmP,
  622.            mEc, A4, lP, V, lPP, NP, y, x, Lambdamoon, BetaM,
  623.            MoonAge, MoonPhase,
  624.            MoonDist, MoonDFrac, MoonAng, MoonPar,
  625.            F, SunDist, SunAng;
  626.  
  627.         /* Calculation of the Sun's position */
  628.  
  629.     Day = pdate - epoch;        /* Date within epoch */
  630.     N = fixangle((360 / 365.2422) * Day); /* Mean anomaly of the Sun */
  631.     M = fixangle(N + elonge - elongp);    /* Convert from perigee
  632.                        co-ordinates to epoch 1980.0 */
  633.     Ec = kepler(M, eccent);     /* Solve equation of Kepler */
  634.     Ec = sqrt((1 + eccent) / (1 - eccent)) * tan(Ec / 2);
  635.     Ec = 2 * todeg(atan(Ec));   /* True anomaly */
  636.         Lambdasun = fixangle(Ec + elongp);  /* Sun's geocentric ecliptic
  637.                            longitude */
  638.     /* Orbital distance factor */
  639.     F = ((1 + eccent * cos(torad(Ec))) / (1 - eccent * eccent));
  640.     SunDist = sunsmax / F;        /* Distance to Sun in km */
  641.         SunAng = F * sunangsiz;     /* Sun's angular size in degrees */
  642.  
  643.  
  644.         /* Calculation of the Moon's position */
  645.  
  646.         /* Moon's mean longitude */
  647.     ml = fixangle(13.1763966 * Day + mmlong);
  648.  
  649.         /* Moon's mean anomaly */
  650.     MM = fixangle(ml - 0.1114041 * Day - mmlongp);
  651.  
  652.         /* Moon's ascending node mean longitude */
  653.     MN = fixangle(mlnode - 0.0529539 * Day);
  654.  
  655.     /* Evection */
  656.     Ev = 1.2739 * sin(torad(2 * (ml - Lambdasun) - MM));
  657.  
  658.     /* Annual equation */
  659.     Ae = 0.1858 * sin(torad(M));
  660.  
  661.     /* Correction term */
  662.     A3 = 0.37 * sin(torad(M));
  663.  
  664.     /* Corrected anomaly */
  665.     MmP = MM + Ev - Ae - A3;
  666.  
  667.     /* Correction for the equation of the centre */
  668.     mEc = 6.2886 * sin(torad(MmP));
  669.  
  670.     /* Another correction term */
  671.     A4 = 0.214 * sin(torad(2 * MmP));
  672.  
  673.     /* Corrected longitude */
  674.     lP = ml + Ev + mEc - Ae + A4;
  675.  
  676.     /* Variation */
  677.     V = 0.6583 * sin(torad(2 * (lP - Lambdasun)));
  678.  
  679.     /* True longitude */
  680.     lPP = lP + V;
  681.  
  682.     /* Corrected longitude of the node */
  683.     NP = MN - 0.16 * sin(torad(M));
  684.  
  685.     /* Y inclination coordinate */
  686.     y = sin(torad(lPP - NP)) * cos(torad(minc));
  687.  
  688.     /* X inclination coordinate */
  689.     x = cos(torad(lPP - NP));
  690.  
  691.     /* Ecliptic longitude */
  692.     Lambdamoon = todeg(atan2(y, x));
  693.     Lambdamoon += NP;
  694.  
  695.     /* Ecliptic latitude */
  696.     BetaM = todeg(asin(sin(torad(lPP - NP)) * sin(torad(minc))));
  697.  
  698.     /* Calculation of the phase of the Moon */
  699.  
  700.     /* Age of the Moon in degrees */
  701.     MoonAge = lPP - Lambdasun;
  702.  
  703.     /* Phase of the Moon */
  704.     MoonPhase = (1 - cos(torad(MoonAge))) / 2;
  705.  
  706.     /* Calculate distance of moon from the centre of the Earth */
  707.  
  708.     MoonDist = (msmax * (1 - mecc * mecc)) /
  709.        (1 + mecc * cos(torad(MmP + mEc)));
  710.  
  711.         /* Calculate Moon's angular diameter */
  712.  
  713.     MoonDFrac = MoonDist / msmax;
  714.     MoonAng = mangsiz / MoonDFrac;
  715.  
  716.         /* Calculate Moon's parallax */
  717.  
  718.     MoonPar = mparallax / MoonDFrac;
  719.  
  720.     *pphase = MoonPhase;
  721.     *mage = synmonth * (fixangle(MoonAge) / 360.0);
  722.     *dist = MoonDist;
  723.     *angdia = MoonAng;
  724.     *sudist = SunDist;
  725.     *suangdia = SunAng;
  726.     return fixangle(MoonAge) / 360.0;
  727. }
  728. #endif    /* NO_SUN_MOON */
  729.