home *** CD-ROM | disk | FTP | other *** search
/ Explore the World of Soft…e: Engineering & Science / Explore_the_World_of_Software_Engineering_and_Science_HRS_Software_1998.iso / programs / datetime / sunclock.exe / ASTRO.C next >
C/C++ Source or Header  |  1991-03-24  |  4KB  |  160 lines

  1. /*
  2.  * Sun clock - astronomical routines.
  3.  */
  4.  
  5. #include "sunclock.h"
  6.  
  7. /*  JDATE  --  Convert internal GMT date and time to Julian day
  8.            and fraction.  */
  9.  
  10. long
  11. jdate(t)
  12. struct tm *t;
  13. {
  14.     long c, m, y;
  15.  
  16.     y = t->tm_year + 1900;
  17.     m = t->tm_mon + 1;
  18.     if (m > 2)
  19.        m = m - 3;
  20.     else {
  21.        m = m + 9;
  22.        y--;
  23.     }
  24.     c = y / 100L;           /* Compute century */
  25.     y -= 100L * c;
  26.     return t->tm_mday + (c * 146097L) / 4 + (y * 1461L) / 4 +
  27.         (m * 153L + 2) / 5 + 1721119L;
  28. }
  29.  
  30. /* JTIME --    Convert internal GMT  date  and    time  to  astronomical
  31.            Julian  time  (i.e.   Julian  date  plus  day fraction,
  32.            expressed as a double).    */
  33.  
  34. double
  35. jtime(t)
  36. struct tm *t;
  37. {
  38.     return (jdate(t) - 0.5) + 
  39.        (((long) t->tm_sec) +
  40.          60L * (t->tm_min + 60L * t->tm_hour)) / 86400.0;
  41. }
  42.  
  43. /*  KEPLER  --    Solve the equation of Kepler.  */
  44.  
  45. double
  46. kepler(m, ecc)
  47. double m, ecc;
  48. {
  49.     double e, delta;
  50. #define EPSILON 1E-6
  51.  
  52.     e = m = dtr(m);
  53.     do {
  54.        delta = e - ecc * sin(e) - m;
  55.        e -= delta / (1 - ecc * cos(e));
  56.     } while (abs(delta) > EPSILON);
  57.     return e;
  58. }
  59.  
  60. /*  SUNPOS  --    Calculate position of the Sun.    JD is the Julian  date
  61.         of  the  instant for which the position is desired and
  62.         APPARENT should be nonzero if  the  apparent  position
  63.         (corrected  for  nutation  and aberration) is desired.
  64.                 The Sun's co-ordinates are returned  in  RA  and  DEC,
  65.         both  specified  in degrees (divide RA by 15 to obtain
  66.         hours).  The radius vector to the Sun in  astronomical
  67.                 units  is returned in RV and the Sun's longitude (true
  68.         or apparent, as desired) is  returned  as  degrees  in
  69.         SLONG.    */
  70.  
  71. sunpos(jd, apparent, ra, dec, rv, slong)
  72. double jd;
  73. int apparent;
  74. double *ra, *dec, *rv, *slong;
  75. {
  76.     double t, t2, t3, l, m, e, ea, v, theta, omega,
  77.            eps;
  78.  
  79.     /* Time, in Julian centuries of 36525 ephemeris days,
  80.        measured from the epoch 1900 January 0.5 ET. */
  81.  
  82.     t = (jd - 2415020.0) / 36525.0;
  83.     t2 = t * t;
  84.     t3 = t2 * t;
  85.  
  86.     /* Geometric mean longitude of the Sun, referred to the
  87.        mean equinox of the date. */
  88.  
  89.     l = fixangle(279.69668 + 36000.76892 * t + 0.0003025 * t2);
  90.  
  91.         /* Sun's mean anomaly. */
  92.  
  93.     m = fixangle(358.47583 + 35999.04975*t - 0.000150*t2 - 0.0000033*t3);
  94.  
  95.         /* Eccentricity of the Earth's orbit. */
  96.  
  97.     e = 0.01675104 - 0.0000418 * t - 0.000000126 * t2;
  98.  
  99.     /* Eccentric anomaly. */
  100.  
  101.     ea = kepler(m, e);
  102.  
  103.     /* True anomaly */
  104.  
  105.     v = fixangle(2 * rtd(atan(sqrt((1 + e) / (1 - e))  * tan(ea / 2))));
  106.  
  107.         /* Sun's true longitude. */
  108.  
  109.     theta = l + v - m;
  110.  
  111.     /* Obliquity of the ecliptic. */
  112.  
  113.     eps = 23.452294 - 0.0130125 * t - 0.00000164 * t2 + 0.000000503 * t3;
  114.  
  115.         /* Corrections for Sun's apparent longitude, if desired. */
  116.  
  117.     if (apparent) {
  118.        omega = fixangle(259.18 - 1934.142 * t);
  119.        theta = theta - 0.00569 - 0.00479 * sin(dtr(omega));
  120.        eps += 0.00256 * cos(dtr(omega));
  121.     }
  122.  
  123.         /* Return Sun's longitude and radius vector */
  124.  
  125.     *slong = theta;
  126.     *rv = (1.0000002 * (1 - e * e)) / (1 + e * cos(dtr(v)));
  127.  
  128.     /* Determine solar co-ordinates. */
  129.  
  130.     *ra =
  131.     fixangle(rtd(atan2(cos(dtr(eps)) * sin(dtr(theta)), cos(dtr(theta)))));
  132.     *dec = rtd(asin(sin(dtr(eps)) * sin(dtr(theta))));
  133. }
  134.  
  135. /*  GMST  --  Calculate Greenwich Mean Siderial Time for a given
  136.           instant expressed as a Julian date and fraction.    */
  137.  
  138. double
  139. gmst(jd)
  140. double jd;
  141. {
  142.     double t, theta0;
  143.  
  144.  
  145.     /* Time, in Julian centuries of 36525 ephemeris days,
  146.        measured from the epoch 1900 January 0.5 ET. */
  147.  
  148.     t = ((floor(jd + 0.5) - 0.5) - 2415020.0) / 36525.0;
  149.  
  150.     theta0 = 6.6460656 + 2400.051262 * t + 0.00002581 * t * t;
  151.  
  152.     t = (jd + 0.5) - (floor(jd + 0.5));
  153.  
  154.     theta0 += (t * 24.0) * 1.002737908;
  155.  
  156.     theta0 = (theta0 - 24.0 * (floor(theta0 / 24.0)));
  157.  
  158.     return theta0;
  159. }
  160.