PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/date/lib/astro.c

http://github.com/infusion/PHP
C | 303 lines | 132 code | 48 blank | 123 comment | 6 complexity | 5c0b428d3f2aff915e176afc5b8aa644 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2010 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Algorithms are taken from a public domain source by Paul |
  16. | Schlyter, who wrote this in December 1992 |
  17. +----------------------------------------------------------------------+
  18. | Authors: Derick Rethans <derick@derickrethans.nl> |
  19. +----------------------------------------------------------------------+
  20. */
  21. /* $Id: astro.c 293036 2010-01-03 09:23:27Z sebastian $ */
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include "timelib.h"
  25. #define days_since_2000_Jan_0(y,m,d) \
  26. (367L*(y)-((7*((y)+(((m)+9)/12)))/4)+((275*(m))/9)+(d)-730530L)
  27. #ifndef PI
  28. #define PI 3.1415926535897932384
  29. #endif
  30. #define RADEG ( 180.0 / PI )
  31. #define DEGRAD ( PI / 180.0 )
  32. /* The trigonometric functions in degrees */
  33. #define sind(x) sin((x)*DEGRAD)
  34. #define cosd(x) cos((x)*DEGRAD)
  35. #define tand(x) tan((x)*DEGRAD)
  36. #define atand(x) (RADEG*atan(x))
  37. #define asind(x) (RADEG*asin(x))
  38. #define acosd(x) (RADEG*acos(x))
  39. #define atan2d(y,x) (RADEG*atan2(y,x))
  40. /* Following are some macros around the "workhorse" function __daylen__ */
  41. /* They mainly fill in the desired values for the reference altitude */
  42. /* below the horizon, and also selects whether this altitude should */
  43. /* refer to the Sun's center or its upper limb. */
  44. #include "astro.h"
  45. /******************************************************************/
  46. /* This function reduces any angle to within the first revolution */
  47. /* by subtracting or adding even multiples of 360.0 until the */
  48. /* result is >= 0.0 and < 360.0 */
  49. /******************************************************************/
  50. #define INV360 (1.0 / 360.0)
  51. /*****************************************/
  52. /* Reduce angle to within 0..360 degrees */
  53. /*****************************************/
  54. static double astro_revolution(double x)
  55. {
  56. return (x - 360.0 * floor(x * INV360));
  57. }
  58. /*********************************************/
  59. /* Reduce angle to within +180..+180 degrees */
  60. /*********************************************/
  61. static double astro_rev180( double x )
  62. {
  63. return (x - 360.0 * floor(x * INV360 + 0.5));
  64. }
  65. /*******************************************************************/
  66. /* This function computes GMST0, the Greenwich Mean Sidereal Time */
  67. /* at 0h UT (i.e. the sidereal time at the Greenwhich meridian at */
  68. /* 0h UT). GMST is then the sidereal time at Greenwich at any */
  69. /* time of the day. I've generalized GMST0 as well, and define it */
  70. /* as: GMST0 = GMST - UT -- this allows GMST0 to be computed at */
  71. /* other times than 0h UT as well. While this sounds somewhat */
  72. /* contradictory, it is very practical: instead of computing */
  73. /* GMST like: */
  74. /* */
  75. /* GMST = (GMST0) + UT * (366.2422/365.2422) */
  76. /* */
  77. /* where (GMST0) is the GMST last time UT was 0 hours, one simply */
  78. /* computes: */
  79. /* */
  80. /* GMST = GMST0 + UT */
  81. /* */
  82. /* where GMST0 is the GMST "at 0h UT" but at the current moment! */
  83. /* Defined in this way, GMST0 will increase with about 4 min a */
  84. /* day. It also happens that GMST0 (in degrees, 1 hr = 15 degr) */
  85. /* is equal to the Sun's mean longitude plus/minus 180 degrees! */
  86. /* (if we neglect aberration, which amounts to 20 seconds of arc */
  87. /* or 1.33 seconds of time) */
  88. /* */
  89. /*******************************************************************/
  90. static double astro_GMST0(double d)
  91. {
  92. double sidtim0;
  93. /* Sidtime at 0h UT = L (Sun's mean longitude) + 180.0 degr */
  94. /* L = M + w, as defined in sunpos(). Since I'm too lazy to */
  95. /* add these numbers, I'll let the C compiler do it for me. */
  96. /* Any decent C compiler will add the constants at compile */
  97. /* time, imposing no runtime or code overhead. */
  98. sidtim0 = astro_revolution((180.0 + 356.0470 + 282.9404) + (0.9856002585 + 4.70935E-5) * d);
  99. return sidtim0;
  100. }
  101. /* This function computes the Sun's position at any instant */
  102. /******************************************************/
  103. /* Computes the Sun's ecliptic longitude and distance */
  104. /* at an instant given in d, number of days since */
  105. /* 2000 Jan 0.0. The Sun's ecliptic latitude is not */
  106. /* computed, since it's always very near 0. */
  107. /******************************************************/
  108. static void astro_sunpos(double d, double *lon, double *r)
  109. {
  110. double M, /* Mean anomaly of the Sun */
  111. w, /* Mean longitude of perihelion */
  112. /* Note: Sun's mean longitude = M + w */
  113. e, /* Eccentricity of Earth's orbit */
  114. E, /* Eccentric anomaly */
  115. x, y, /* x, y coordinates in orbit */
  116. v; /* True anomaly */
  117. /* Compute mean elements */
  118. M = astro_revolution(356.0470 + 0.9856002585 * d);
  119. w = 282.9404 + 4.70935E-5 * d;
  120. e = 0.016709 - 1.151E-9 * d;
  121. /* Compute true longitude and radius vector */
  122. E = M + e * RADEG * sind(M) * (1.0 + e * cosd(M));
  123. x = cosd(E) - e;
  124. y = sqrt(1.0 - e*e) * sind(E);
  125. *r = sqrt(x*x + y*y); /* Solar distance */
  126. v = atan2d(y, x); /* True anomaly */
  127. *lon = v + w; /* True solar longitude */
  128. if (*lon >= 360.0) {
  129. *lon -= 360.0; /* Make it 0..360 degrees */
  130. }
  131. }
  132. static void astro_sun_RA_dec(double d, double *RA, double *dec, double *r)
  133. {
  134. double lon, obl_ecl, x, y, z;
  135. /* Compute Sun's ecliptical coordinates */
  136. astro_sunpos(d, &lon, r);
  137. /* Compute ecliptic rectangular coordinates (z=0) */
  138. x = *r * cosd(lon);
  139. y = *r * sind(lon);
  140. /* Compute obliquity of ecliptic (inclination of Earth's axis) */
  141. obl_ecl = 23.4393 - 3.563E-7 * d;
  142. /* Convert to equatorial rectangular coordinates - x is unchanged */
  143. z = y * sind(obl_ecl);
  144. y = y * cosd(obl_ecl);
  145. /* Convert to spherical coordinates */
  146. *RA = atan2d(y, x);
  147. *dec = atan2d(z, sqrt(x*x + y*y));
  148. }
  149. /**
  150. * Note: timestamp = unixtimestamp (NEEDS to be 00:00:00 UT)
  151. * Eastern longitude positive, Western longitude negative
  152. * Northern latitude positive, Southern latitude negative
  153. * The longitude value IS critical in this function!
  154. * altit = the altitude which the Sun should cross
  155. * Set to -35/60 degrees for rise/set, -6 degrees
  156. * for civil, -12 degrees for nautical and -18
  157. * degrees for astronomical twilight.
  158. * upper_limb: non-zero -> upper limb, zero -> center
  159. * Set to non-zero (e.g. 1) when computing rise/set
  160. * times, and to zero when computing start/end of
  161. * twilight.
  162. * *rise = where to store the rise time
  163. * *set = where to store the set time
  164. * Both times are relative to the specified altitude,
  165. * and thus this function can be used to compute
  166. * various twilight times, as well as rise/set times
  167. * Return value: 0 = sun rises/sets this day, times stored at
  168. * *trise and *tset.
  169. * +1 = sun above the specified "horizon" 24 hours.
  170. * *trise set to time when the sun is at south,
  171. * minus 12 hours while *tset is set to the south
  172. * time plus 12 hours. "Day" length = 24 hours
  173. * -1 = sun is below the specified "horizon" 24 hours
  174. * "Day" length = 0 hours, *trise and *tset are
  175. * both set to the time when the sun is at south.
  176. *
  177. */
  178. int timelib_astro_rise_set_altitude(timelib_time *t_loc, double lon, double lat, double altit, int upper_limb, double *h_rise, double *h_set, timelib_sll *ts_rise, timelib_sll *ts_set, timelib_sll *ts_transit)
  179. {
  180. double d, /* Days since 2000 Jan 0.0 (negative before) */
  181. sr, /* Solar distance, astronomical units */
  182. sRA, /* Sun's Right Ascension */
  183. sdec, /* Sun's declination */
  184. sradius, /* Sun's apparent radius */
  185. t, /* Diurnal arc */
  186. tsouth, /* Time when Sun is at south */
  187. sidtime; /* Local sidereal time */
  188. timelib_time *t_utc;
  189. timelib_sll timestamp, old_sse;
  190. int rc = 0; /* Return cde from function - usually 0 */
  191. /* Normalize time */
  192. old_sse = t_loc->sse;
  193. t_loc->h = 12;
  194. t_loc->i = t_loc->s = 0;
  195. timelib_update_ts(t_loc, NULL);
  196. /* Calculate TS belonging to UTC 00:00 of the current day */
  197. t_utc = timelib_time_ctor();
  198. t_utc->y = t_loc->y;
  199. t_utc->m = t_loc->m;
  200. t_utc->d = t_loc->d;
  201. t_utc->h = t_utc->i = t_utc->s = 0;
  202. timelib_update_ts(t_utc, NULL);
  203. /* Compute d of 12h local mean solar time */
  204. timestamp = t_loc->sse;
  205. d = timelib_ts_to_juliandate(timestamp) - lon/360.0;
  206. /* Compute local sidereal time of this moment */
  207. sidtime = astro_revolution(astro_GMST0(d) + 180.0 + lon);
  208. /* Compute Sun's RA + Decl at this moment */
  209. astro_sun_RA_dec( d, &sRA, &sdec, &sr );
  210. /* Compute time when Sun is at south - in hours UT */
  211. tsouth = 12.0 - astro_rev180(sidtime - sRA) / 15.0;
  212. /* Compute the Sun's apparent radius, degrees */
  213. sradius = 0.2666 / sr;
  214. /* Do correction to upper limb, if necessary */
  215. if (upper_limb) {
  216. altit -= sradius;
  217. }
  218. /* Compute the diurnal arc that the Sun traverses to reach */
  219. /* the specified altitude altit: */
  220. {
  221. double cost;
  222. cost = (sind(altit) - sind(lat) * sind(sdec)) / (cosd(lat) * cosd(sdec));
  223. *ts_transit = t_utc->sse + (tsouth * 3600);
  224. if (cost >= 1.0) {
  225. rc = -1;
  226. t = 0.0; /* Sun always below altit */
  227. *ts_rise = *ts_set = t_utc->sse + (tsouth * 3600);
  228. } else if (cost <= -1.0) {
  229. rc = +1;
  230. t = 12.0; /* Sun always above altit */
  231. *ts_rise = t_loc->sse - (12 * 3600);
  232. *ts_set = t_loc->sse + (12 * 3600);
  233. } else {
  234. t = acosd(cost) / 15.0; /* The diurnal arc, hours */
  235. /* Store rise and set times - as Unix Timestamp */
  236. *ts_rise = ((tsouth - t) * 3600) + t_utc->sse;
  237. *ts_set = ((tsouth + t) * 3600) + t_utc->sse;
  238. *h_rise = (tsouth - t);
  239. *h_set = (tsouth + t);
  240. }
  241. }
  242. /* Kill temporary time and restore original sse */
  243. timelib_time_dtor(t_utc);
  244. t_loc->sse = old_sse;
  245. return rc;
  246. }
  247. double timelib_ts_to_juliandate(timelib_sll ts)
  248. {
  249. double tmp;
  250. tmp = ts;
  251. tmp /= 86400;
  252. tmp += 2440587.5;
  253. tmp -= 2451543;
  254. return tmp;
  255. }