home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume15 / moontool / moontool.c < prev    next >
C/C++ Source or Header  |  1988-06-02  |  25KB  |  804 lines

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