astro.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015-2019 Derick Rethans
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /*
  25. | Algorithms are taken from a public domain source by Paul |
  26. | Schlyter, who wrote this in December 1992 |
  27. */
  28. #include "timelib.h"
  29. #include <stdio.h>
  30. #include <math.h>
  31. #define days_since_2000_Jan_0(y,m,d) \
  32. (367L*(y)-((7*((y)+(((m)+9)/12)))/4)+((275*(m))/9)+(d)-730530L)
  33. #ifndef PI
  34. # define PI 3.1415926535897932384
  35. #endif
  36. #define RADEG ( 180.0 / PI )
  37. #define DEGRAD ( PI / 180.0 )
  38. /* The trigonometric functions in degrees */
  39. #define sind(x) sin((x)*DEGRAD)
  40. #define cosd(x) cos((x)*DEGRAD)
  41. #define tand(x) tan((x)*DEGRAD)
  42. #define atand(x) (RADEG*atan(x))
  43. #define asind(x) (RADEG*asin(x))
  44. #define acosd(x) (RADEG*acos(x))
  45. #define atan2d(y,x) (RADEG*atan2(y,x))
  46. /* Following are some macros around the "workhorse" function __daylen__ */
  47. /* They mainly fill in the desired values for the reference altitude */
  48. /* below the horizon, and also selects whether this altitude should */
  49. /* refer to the Sun's center or its upper limb. */
  50. #include "astro.h"
  51. /******************************************************************/
  52. /* This function reduces any angle to within the first revolution */
  53. /* by subtracting or adding even multiples of 360.0 until the */
  54. /* result is >= 0.0 and < 360.0 */
  55. /******************************************************************/
  56. #define INV360 (1.0 / 360.0)
  57. /*****************************************/
  58. /* Reduce angle to within 0..360 degrees */
  59. /*****************************************/
  60. static double astro_revolution(double x)
  61. {
  62. return (x - 360.0 * floor(x * INV360));
  63. }
  64. /*********************************************/
  65. /* Reduce angle to within +180..+180 degrees */
  66. /*********************************************/
  67. static double astro_rev180( double x )
  68. {
  69. return (x - 360.0 * floor(x * INV360 + 0.5));
  70. }
  71. /*******************************************************************/
  72. /* This function computes GMST0, the Greenwich Mean Sidereal Time */
  73. /* at 0h UT (i.e. the sidereal time at the Greenwhich meridian at */
  74. /* 0h UT). GMST is then the sidereal time at Greenwich at any */
  75. /* time of the day. I've generalized GMST0 as well, and define it */
  76. /* as: GMST0 = GMST - UT -- this allows GMST0 to be computed at */
  77. /* other times than 0h UT as well. While this sounds somewhat */
  78. /* contradictory, it is very practical: instead of computing */
  79. /* GMST like: */
  80. /* */
  81. /* GMST = (GMST0) + UT * (366.2422/365.2422) */
  82. /* */
  83. /* where (GMST0) is the GMST last time UT was 0 hours, one simply */
  84. /* computes: */
  85. /* */
  86. /* GMST = GMST0 + UT */
  87. /* */
  88. /* where GMST0 is the GMST "at 0h UT" but at the current moment! */
  89. /* Defined in this way, GMST0 will increase with about 4 min a */
  90. /* day. It also happens that GMST0 (in degrees, 1 hr = 15 degr) */
  91. /* is equal to the Sun's mean longitude plus/minus 180 degrees! */
  92. /* (if we neglect aberration, which amounts to 20 seconds of arc */
  93. /* or 1.33 seconds of time) */
  94. /* */
  95. /*******************************************************************/
  96. static double astro_GMST0(double d)
  97. {
  98. double sidtim0;
  99. /* Sidtime at 0h UT = L (Sun's mean longitude) + 180.0 degr */
  100. /* L = M + w, as defined in sunpos(). Since I'm too lazy to */
  101. /* add these numbers, I'll let the C compiler do it for me. */
  102. /* Any decent C compiler will add the constants at compile */
  103. /* time, imposing no runtime or code overhead. */
  104. sidtim0 = astro_revolution((180.0 + 356.0470 + 282.9404) + (0.9856002585 + 4.70935E-5) * d);
  105. return sidtim0;
  106. }
  107. /* This function computes the Sun's position at any instant */
  108. /******************************************************/
  109. /* Computes the Sun's ecliptic longitude and distance */
  110. /* at an instant given in d, number of days since */
  111. /* 2000 Jan 0.0. The Sun's ecliptic latitude is not */
  112. /* computed, since it's always very near 0. */
  113. /******************************************************/
  114. static void astro_sunpos(double d, double *lon, double *r)
  115. {
  116. double M, /* Mean anomaly of the Sun */
  117. w, /* Mean longitude of perihelion */
  118. /* Note: Sun's mean longitude = M + w */
  119. e, /* Eccentricity of Earth's orbit */
  120. E, /* Eccentric anomaly */
  121. x, y, /* x, y coordinates in orbit */
  122. v; /* True anomaly */
  123. /* Compute mean elements */
  124. M = astro_revolution(356.0470 + 0.9856002585 * d);
  125. w = 282.9404 + 4.70935E-5 * d;
  126. e = 0.016709 - 1.151E-9 * d;
  127. /* Compute true longitude and radius vector */
  128. E = M + e * RADEG * sind(M) * (1.0 + e * cosd(M));
  129. x = cosd(E) - e;
  130. y = sqrt(1.0 - e*e) * sind(E);
  131. *r = sqrt(x*x + y*y); /* Solar distance */
  132. v = atan2d(y, x); /* True anomaly */
  133. *lon = v + w; /* True solar longitude */
  134. if (*lon >= 360.0) {
  135. *lon -= 360.0; /* Make it 0..360 degrees */
  136. }
  137. }
  138. static void astro_sun_RA_dec(double d, double *RA, double *dec, double *r)
  139. {
  140. double lon, obl_ecl, x, y, z;
  141. /* Compute Sun's ecliptical coordinates */
  142. astro_sunpos(d, &lon, r);
  143. /* Compute ecliptic rectangular coordinates (z=0) */
  144. x = *r * cosd(lon);
  145. y = *r * sind(lon);
  146. /* Compute obliquity of ecliptic (inclination of Earth's axis) */
  147. obl_ecl = 23.4393 - 3.563E-7 * d;
  148. /* Convert to equatorial rectangular coordinates - x is unchanged */
  149. z = y * sind(obl_ecl);
  150. y = y * cosd(obl_ecl);
  151. /* Convert to spherical coordinates */
  152. *RA = atan2d(y, x);
  153. *dec = atan2d(z, sqrt(x*x + y*y));
  154. }
  155. /**
  156. * Note: timestamp = unixtimestamp (NEEDS to be 00:00:00 UT)
  157. * Eastern longitude positive, Western longitude negative
  158. * Northern latitude positive, Southern latitude negative
  159. * The longitude value IS critical in this function!
  160. * altit = the altitude which the Sun should cross
  161. * Set to -35/60 degrees for rise/set, -6 degrees
  162. * for civil, -12 degrees for nautical and -18
  163. * degrees for astronomical twilight.
  164. * upper_limb: non-zero -> upper limb, zero -> center
  165. * Set to non-zero (e.g. 1) when computing rise/set
  166. * times, and to zero when computing start/end of
  167. * twilight.
  168. * *rise = where to store the rise time
  169. * *set = where to store the set time
  170. * Both times are relative to the specified altitude,
  171. * and thus this function can be used to compute
  172. * various twilight times, as well as rise/set times
  173. * Return value: 0 = sun rises/sets this day, times stored at
  174. * *trise and *tset.
  175. * +1 = sun above the specified "horizon" 24 hours.
  176. * *trise set to time when the sun is at south,
  177. * minus 12 hours while *tset is set to the south
  178. * time plus 12 hours. "Day" length = 24 hours
  179. * -1 = sun is below the specified "horizon" 24 hours
  180. * "Day" length = 0 hours, *trise and *tset are
  181. * both set to the time when the sun is at south.
  182. *
  183. */
  184. 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)
  185. {
  186. double d, /* Days since 2000 Jan 0.0 (negative before) */
  187. sr, /* Solar distance, astronomical units */
  188. sRA, /* Sun's Right Ascension */
  189. sdec, /* Sun's declination */
  190. sradius, /* Sun's apparent radius */
  191. t, /* Diurnal arc */
  192. tsouth, /* Time when Sun is at south */
  193. sidtime; /* Local sidereal time */
  194. timelib_time *t_utc;
  195. timelib_sll timestamp, old_sse;
  196. int rc = 0; /* Return cde from function - usually 0 */
  197. /* Normalize time */
  198. old_sse = t_loc->sse;
  199. t_loc->h = 12;
  200. t_loc->i = t_loc->s = 0;
  201. timelib_update_ts(t_loc, NULL);
  202. /* Calculate TS belonging to UTC 00:00 of the current day, for input into
  203. * the algorithm */
  204. t_utc = timelib_time_ctor();
  205. t_utc->y = t_loc->y;
  206. t_utc->m = t_loc->m;
  207. t_utc->d = t_loc->d;
  208. t_utc->h = t_utc->i = t_utc->s = 0;
  209. timelib_update_ts(t_utc, NULL);
  210. /* Compute d of 12h local mean solar time */
  211. timestamp = t_utc->sse;
  212. d = timelib_ts_to_j2000(timestamp) + 2 - lon/360.0;
  213. /* Compute local sidereal time of this moment */
  214. sidtime = astro_revolution(astro_GMST0(d) + 180.0 + lon);
  215. /* Compute Sun's RA + Decl at this moment */
  216. astro_sun_RA_dec( d, &sRA, &sdec, &sr );
  217. /* Compute time when Sun is at south - in hours UT */
  218. tsouth = 12.0 - astro_rev180(sidtime - sRA) / 15.0;
  219. /* Compute the Sun's apparent radius, degrees */
  220. sradius = 0.2666 / sr;
  221. /* Do correction to upper limb, if necessary */
  222. if (upper_limb) {
  223. altit -= sradius;
  224. }
  225. /* Compute the diurnal arc that the Sun traverses to reach */
  226. /* the specified altitude altit: */
  227. {
  228. double cost;
  229. cost = (sind(altit) - sind(lat) * sind(sdec)) / (cosd(lat) * cosd(sdec));
  230. *ts_transit = t_utc->sse + (tsouth * 3600);
  231. if (cost >= 1.0) {
  232. rc = -1;
  233. t = 0.0; /* Sun always below altit */
  234. *ts_rise = *ts_set = t_utc->sse + (tsouth * 3600);
  235. } else if (cost <= -1.0) {
  236. rc = +1;
  237. t = 12.0; /* Sun always above altit */
  238. *ts_rise = t_loc->sse - (12 * 3600);
  239. *ts_set = t_loc->sse + (12 * 3600);
  240. } else {
  241. t = acosd(cost) / 15.0; /* The diurnal arc, hours */
  242. /* Store rise and set times - as Unix Timestamp */
  243. *ts_rise = ((tsouth - t) * 3600) + t_utc->sse;
  244. *ts_set = ((tsouth + t) * 3600) + t_utc->sse;
  245. *h_rise = (tsouth - t);
  246. *h_set = (tsouth + t);
  247. }
  248. }
  249. /* Kill temporary time and restore original sse */
  250. timelib_time_dtor(t_utc);
  251. t_loc->sse = old_sse;
  252. return rc;
  253. }
  254. double timelib_ts_to_julianday(timelib_sll ts)
  255. {
  256. double tmp;
  257. tmp = (double) ts;
  258. tmp /= (double) 86400;
  259. tmp += (double) 2440587.5;
  260. return tmp;
  261. }
  262. double timelib_ts_to_j2000(timelib_sll ts)
  263. {
  264. return timelib_ts_to_julianday(ts) - 2451545;
  265. }