The Fork
http://the.fork.pl/code/moon/
 the.fork.pl
 Site
 Credits
 Contact
 Powered by
 The Story of Fork
 Words
 Sermon on the Hill
 Antybiografia
 Angel
 Devil
 Powrót z...
 Simon T.
 Favourite poems
 Roam
 Sounds
 The Lost Faith
 Atari Reloaded
 Players
 Code
 Saker
 Moon phase
 KB24
 Attic
 PLC
 33doi
 DemoScene
 Atari
 Logrus
 Cryogen
 Crew
 History
 Stuph
 Pictures
 Ascii Art
 Set One
 Set Two
 Set Three

Moon Phase

When the sky above is cloudy or when the steel bars in your window don't let you look outside... The moon is always there - the cold mirror of nations' dreams.

Catch it.

Moon image, please wait while generating...


C code for moonphase calculation, author unknown.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. //#define TRUE 1
  6. //#define FALSE 0
  7.  
  8. double phase(
  9.   double   pdate,                      /* Date for which to calculate phase */
  10.   double  *pphase,                    /* Illuminated fraction */
  11.   double  *mage,                      /* Age of moon in days */
  12.   double  *dist,                      /* Distance in kilometres */
  13.   double  *angdia,                    /* Angular diameter in degrees */
  14.   double  *sudist,                    /* Distance to Sun */
  15.   double  *suangdia);                 /* Sun's angular diameter */
  16.  
  17. double jtime(struct tm *t);
  18.  
  19. /*  Astronomical constants  */
  20.  
  21. #define epoch       2444238.5      /* 1980 January 0.0 */
  22.  
  23. /*  Constants defining the Sun's apparent orbit  */
  24.  
  25. #define elonge      278.833540     /* Ecliptic longitude of the Sun
  26.                                       at epoch 1980.0 */
  27. #define elongp      282.596403     /* Ecliptic longitude of the Sun at
  28.                                       perigee */
  29. #define eccent      0.016718       /* Eccentricity of Earth's orbit */
  30. #define sunsmax     1.495985e8     /* Semi-major axis of Earth's orbit, km */
  31. #define sunangsiz   0.533128       /* Sun's angular size, degrees, at
  32.                                       semi-major axis distance */
  33.  
  34. /*  Elements of the Moon's orbit, epoch 1980.0  */
  35.  
  36. #define mmlong      64.975464      /* Moon's mean longitude at the epoch */
  37. #define mmlongp     349.383063     /* Mean longitude of the perigee at the
  38.                                       epoch */
  39. #define mlnode      151.950429     /* Mean longitude of the node at the
  40.                                       epoch */
  41. #define minc        5.145396       /* Inclination of the Moon's orbit */
  42. #define mecc        0.054900       /* Eccentricity of the Moon's orbit */
  43. #define mangsiz     0.5181         /* Moon's angular size at distance a
  44.                                       from Earth */
  45. #define msmax       384401.0       /* Semi-major axis of Moon's orbit in km */
  46. #define mparallax   0.9507         /* Parallax at distance a from Earth */
  47. #define synmonth    29.53058868    /* Synodic month (new Moon to new Moon) */
  48. #define lunatbase   2423436.0      /* Base date for E. W. Brown's numbered
  49.                                       series of lunations (1923 January 16) */
  50.  
  51. /*  Properties of the Earth  */
  52.  
  53. #define earthrad    6378.16        /* Radius of Earth in kilometres */
  54.  
  55.  
  56. #define PI 3.14159265358979323846  /* Assume not near black hole nor in
  57.                                       Tennessee */
  58.  
  59. /*  Handy mathematical functions  */
  60.  
  61. #define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0))       /* Extract sign */
  62. //#define abs(x) ((x) < 0 ? (-(x)) : (x))                   /* Absolute val */
  63. #define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0)))  /* Fix angle    */
  64. #define torad(d) ((d) * (PI / 180.0))                     /* Deg->Rad     */
  65. #define todeg(d) ((d) * (180.0 / PI))                     /* Rad->Deg     */
  66. #define dsin(x) (sin(torad((x))))                         /* Sin from deg */
  67. #define dcos(x) (cos(torad((x))))                         /* Cos from deg */
  68.  
  69. double moon(int make_output)
  70. {
  71. //    int mm, dd;
  72.     int aom_d, aom_h, aom_m;
  73.     time_t t;
  74.     double jd, p, aom, cphase, cdist, cangdia, csund, csuang;
  75.     struct tm *gm;
  76.  
  77.     time(&t);
  78.     jd = jtime((gm = localtime(&t)));
  79.     printf("time=%d\n",t);
  80.     printf("jtime=%f\n",jd);
  81. //    mm = gm->tm_mon + 1;
  82. //    dd = gm->tm_mday;
  83.  
  84.     p = phase(jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
  85.  
  86.     if (!make_output) return p;
  87.  
  88.     aom_d = (int) aom;
  89.     aom_h = (int) (24 * (aom - floor(aom)));
  90.     aom_m = (int) (1440 * (aom - floor(aom))) % 60;
  91.  
  92. //    drawmoon(hDC, "colourmoon", 64, 32, 27, 28,
  93. //             tm.tmMaxCharWidth * mgeom.moonbitx, 10, p, mm, dd);
  94.  
  95.     /* Update textual information for open window */
  96.  
  97.         /* Local time */
  98.  
  99.         printf("Local time      %02d-%02d-%4d  %02d:%02d:%02d\n",
  100.           gm->tm_mday, gm->tm_mon, gm->tm_year+1900,
  101.           gm->tm_hour, gm->tm_min, gm->tm_sec);
  102.         printf("Moon age        %d day, %02d:%02d\n",aom_d,aom_h,aom_m);
  103.         printf("Moon phase      %.0f%%\n",cphase * 100);
  104.         printf("Moon dist.      %.0f km    %.2f e.rd.\n",cdist, cdist / earthrad);
  105.         printf("Sun dist.       %.0f km    %.2f au\n",csund, csund / sunsmax);
  106.         return p;
  107. }
  108.  
  109. /* JDATE -- Convert internal GMT date and time to Julian day and fraction. */
  110. long jdate(struct tm *t)
  111. {
  112.     long c, m, y;
  113.  
  114.     y = t->tm_year + 1900;
  115.     m = t->tm_mon + 1;
  116.     if (m > 2) {
  117.         m = m - 3;
  118.     } else {
  119.         m = m + 9;
  120.         y--;
  121.     }
  122.     c = y / 100L;                     /* Compute century */
  123.     y -= 100L * c;
  124.     return (t->tm_mday + (c * 146097L) / 4 + (y * 1461L) / 4 +
  125.            (m * 153L + 2) / 5 + 1721119L);
  126. }
  127.  
  128. /*  JTIME  --  Convert internal GMT  date  and  time  to  astronomical
  129.                Julian   time  (i.e. Julian  date  plus  day  fraction,
  130.                expressed as a double).  */
  131. double jtime(struct tm *t)
  132. {
  133.     printf("jdate=%d\n",jdate(t));
  134.     return (jdate(t) - 0.5) +
  135.            (t->tm_sec + 60L * (t->tm_min + 60L * t->tm_hour)) / 86400.0;
  136. }
  137.  
  138. /*  JYEAR  --  Convert  Julian  date  to  year,  month, day, which are
  139.                returned via integer pointers to integers.  */
  140.  
  141. void jyear(double td, int *yy, int *mm, int *dd)
  142. {
  143.     double j, d, y, m;
  144.  
  145.     td += 0.5;                        /* Astronomical to civil */
  146.     j = floor(td);
  147.     j = j - 1721119.0;
  148.     y = floor(((4 * j) - 1) / 146097.0);
  149.     j = (j * 4.0) - (1.0 + (146097.0 * y));
  150.     d = floor(j / 4.0);
  151.     j = floor(((4.0 * d) + 3.0) / 1461.0);
  152.     d = ((4.0 * d) + 3.0) - (1461.0 * j);
  153.     d = floor((d + 4.0) / 4.0);
  154.     m = floor(((5.0 * d) - 3) / 153.0);
  155.     d = (5.0 * d) - (3.0 + (153.0 * m));
  156.     d = floor((d + 5.0) / 5.0);
  157.     y = (100.0 * y) + j;
  158.     if (m < 10.0) {
  159.         m = m + 3;
  160.     } else {
  161.         m = m - 9;
  162.         y = y + 1;
  163.     }
  164.     *yy = (int) y;
  165.     *mm = (int) m;
  166.     *dd = (int) d;
  167. }
  168.  
  169. /*  JHMS  --  Convert Julian time to hour, minutes, and seconds.  */
  170.  
  171. void jhms(double j, int *h, int *m, int *s)
  172. {
  173.     long ij;
  174.  
  175.     j += 0.5;                         /* Astronomical to civil */
  176.     ij = (long) ((j - floor(j)) * 86400.0);
  177.     *h = (int) (ij / 3600L);
  178.     *m = (int) ((ij / 60L) % 60L);
  179.     *s = (int) (ij % 60L);
  180. }
  181.  
  182. /*  KEPLER  --   Solve the equation of Kepler.  */
  183.  
  184. static double kepler(double m, double ecc)
  185. {
  186.     double e, delta;
  187. #define EPSILON 1E-6
  188.  
  189.     e = m = torad(m);
  190.     do {
  191.         delta = e - ecc * sin(e) - m;
  192.         e -= delta / (1 - ecc * cos(e));
  193.     } while (abs(delta) > EPSILON);
  194.     return e;
  195. }
  196.  
  197. /*  PHASE  --  Calculate phase of moon as a fraction:
  198.  
  199.     The  argument  is  the  time  for  which  the  phase is requested,
  200.     expressed as a Julian date and fraction.  Returns  the  terminator
  201.     phase  angle  as a percentage of a full circle (i.e., 0 to 1), and
  202.     stores into pointer arguments  the  illuminated  fraction  of  the
  203.     Moon's  disc, the Moon's age in days and fraction, the distance of
  204.     the Moon from the centre of the Earth, and  the  angular  diameter
  205.     subtended  by the Moon as seen by an observer at the centre of the
  206.     Earth.
  207. */
  208.  
  209. double phase(
  210.   double  pdate,                      /* Date for which to calculate phase */
  211.   double  *pphase,                    /* Illuminated fraction */
  212.   double  *mage,                      /* Age of moon in days */
  213.   double  *dist,                      /* Distance in kilometres */
  214.   double  *angdia,                                 /* Angular diameter in degrees */
  215.   double  *sudist,                    /* Distance to Sun */
  216.   double  *suangdia)                  /* Sun's angular diameter */
  217. {
  218.  
  219.     double Day, N, M, Ec, Lambdasun, ml, MM, /* MN,*/ Ev, Ae, A3, MmP,
  220.            mEc, A4, lP, V, lPP, // NP, y, x, Lambdamoon, BetaM,
  221.            MoonAge, MoonPhase,
  222.            MoonDist, MoonDFrac, MoonAng, // MoonPar,
  223.            F, SunDist, SunAng;
  224.  
  225.     /* Calculation of the Sun's position */
  226.  
  227.     Day = pdate - epoch;                    /* Date within epoch */
  228.     N = fixangle((360 / 365.2422) * Day);   /* Mean anomaly of the Sun */
  229.     M = fixangle(N + elonge - elongp);      /* Convert from perigee
  230.                                                co-ordinates to epoch 1980.0 */
  231.     Ec = kepler(M, eccent);                 /* Solve equation of Kepler */
  232.     Ec = sqrt((1 + eccent) / (1 - eccent)) * tan(Ec / 2);
  233.     Ec = 2 * todeg(atan(Ec));               /* True anomaly */
  234.     Lambdasun = fixangle(Ec + elongp);      /* Sun's geocentric ecliptic
  235.                                                longitude */
  236.     /* Orbital distance factor */
  237.     F = ((1 + eccent * cos(torad(Ec))) / (1 - eccent * eccent));
  238.     SunDist = sunsmax / F;                  /* Distance to Sun in km */
  239.     SunAng = F * sunangsiz;                 /* Sun's angular size in degrees */
  240.  
  241.     /* Calculation of the Moon's position */
  242.  
  243.     /* Moon's mean longitude */
  244.     ml = fixangle(13.1763966 * Day + mmlong);
  245.  
  246.     /* Moon's mean anomaly */
  247.     MM = fixangle(ml - 0.1114041 * Day - mmlongp);
  248.  
  249.     /* Moon's ascending node mean longitude */
  250. //    MN = fixangle(mlnode - 0.0529539 * Day);
  251.  
  252.     /* Evection */
  253.     Ev = 1.2739 * sin(torad(2 * (ml - Lambdasun) - MM));
  254.  
  255.     /* Annual equation */
  256.     Ae = 0.1858 * sin(torad(M));
  257.  
  258.     /* Correction term */
  259.     A3 = 0.37 * sin(torad(M));
  260.  
  261.     /* Corrected anomaly */
  262.     MmP = MM + Ev - Ae - A3;
  263.  
  264.     /* Correction for the equation of the centre */
  265.     mEc = 6.2886 * sin(torad(MmP));
  266.  
  267.     /* Another correction term */
  268.     A4 = 0.214 * sin(torad(2 * MmP));
  269.  
  270.     /* Corrected longitude */
  271.     lP = ml + Ev + mEc - Ae + A4;
  272.  
  273.     /* Variation */
  274.     V = 0.6583 * sin(torad(2 * (lP - Lambdasun)));
  275.  
  276.     /* True longitude */
  277.     lPP = lP + V;
  278.  
  279.     /* Corrected longitude of the node */
  280. //    NP = MN - 0.16 * sin(torad(M));
  281.  
  282.     /* Y inclination coordinate */
  283. //    y = sin(torad(lPP - NP)) * cos(torad(minc));
  284.  
  285.     /* X inclination coordinate */
  286. //    x = cos(torad(lPP - NP));
  287.  
  288.     /* Ecliptic longitude */
  289. //    Lambdamoon = todeg(atan2(y, x));
  290. //    Lambdamoon += NP;
  291.  
  292.     /* Ecliptic latitude */
  293. //    BetaM = todeg(asin(sin(torad(lPP - NP)) * sin(torad(minc))));
  294.  
  295.     /* Calculation of the phase of the Moon */
  296.  
  297.     /* Age of the Moon in degrees */
  298.     MoonAge = lPP - Lambdasun;
  299.  
  300.     /* Phase of the Moon */
  301.     MoonPhase = (1 - cos(torad(MoonAge))) / 2;
  302.  
  303.     /* Calculate distance of moon from the centre of the Earth */
  304.  
  305.     MoonDist = (msmax * (1 - mecc * mecc)) /
  306.                (1 + mecc * cos(torad(MmP + mEc)));
  307.  
  308.     /* Calculate Moon's angular diameter */
  309.  
  310.     MoonDFrac = MoonDist / msmax;
  311.     MoonAng = mangsiz / MoonDFrac;
  312.  
  313.     /* Calculate Moon's parallax */
  314.  
  315. //    MoonPar = mparallax / MoonDFrac;
  316.  
  317.     *pphase = MoonPhase;
  318.     *mage = synmonth * (fixangle(MoonAge) / 360.0);
  319.     *dist = MoonDist;
  320.     *angdia = MoonAng;
  321.     *sudist = SunDist;
  322.     *suangdia = SunAng;
  323.     return fixangle(MoonAge) / 360.0;
  324. }
  325.  
  326. int main(void)
  327. {
  328. printf("%lf\n",moon(0));
  329. return 0;
  330. }
Last modified: 2006-11-04 01:40:44 (v423)   Valid XHTML 1.0

(c) 2009 Marcin Gryszkalis