Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

suncalc.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. (c) 2011-2015, Vladimir Agafonkin
  3. SunCalc is a JavaScript library for calculating sun/moon position and light phases.
  4. https://github.com/mourner/suncalc
  5. */
  6. (function () { 'use strict';
  7. // shortcuts for easier to read formulas
  8. var PI = Math.PI,
  9. sin = Math.sin,
  10. cos = Math.cos,
  11. tan = Math.tan,
  12. asin = Math.asin,
  13. atan = Math.atan2,
  14. acos = Math.acos,
  15. rad = PI / 180;
  16. // sun calculations are based on http://aa.quae.nl/en/reken/zonpositie.html formulas
  17. // date/time constants and conversions
  18. var dayMs = 1000 * 60 * 60 * 24,
  19. J1970 = 2440588,
  20. J2000 = 2451545;
  21. function toJulian(date) { return date.valueOf() / dayMs - 0.5 + J1970; }
  22. function fromJulian(j) { return new Date((j + 0.5 - J1970) * dayMs); }
  23. function toDays(date) { return toJulian(date) - J2000; }
  24. // general calculations for position
  25. var e = rad * 23.4397; // obliquity of the Earth
  26. function rightAscension(l, b) { return atan(sin(l) * cos(e) - tan(b) * sin(e), cos(l)); }
  27. function declination(l, b) { return asin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l)); }
  28. function azimuth(H, phi, dec) { return atan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi)); }
  29. function altitude(H, phi, dec) { return asin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H)); }
  30. function siderealTime(d, lw) { return rad * (280.16 + 360.9856235 * d) - lw; }
  31. function astroRefraction(h) {
  32. if (h < 0) // the following formula works for positive altitudes only.
  33. h = 0; // if h = -0.08901179 a div/0 would occur.
  34. // formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  35. // 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:
  36. return 0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179));
  37. }
  38. // general sun calculations
  39. function solarMeanAnomaly(d) { return rad * (357.5291 + 0.98560028 * d); }
  40. function eclipticLongitude(M) {
  41. var C = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)), // equation of center
  42. P = rad * 102.9372; // perihelion of the Earth
  43. return M + C + P + PI;
  44. }
  45. function sunCoords(d) {
  46. var M = solarMeanAnomaly(d),
  47. L = eclipticLongitude(M);
  48. return {
  49. dec: declination(L, 0),
  50. ra: rightAscension(L, 0)
  51. };
  52. }
  53. var SunCalc = {};
  54. // calculates sun position for a given date and latitude/longitude
  55. SunCalc.getPosition = function (date, lat, lng) {
  56. var lw = rad * -lng,
  57. phi = rad * lat,
  58. d = toDays(date),
  59. c = sunCoords(d),
  60. H = siderealTime(d, lw) - c.ra;
  61. return {
  62. azimuth: azimuth(H, phi, c.dec),
  63. altitude: altitude(H, phi, c.dec)
  64. };
  65. };
  66. // sun times configuration (angle, morning name, evening name)
  67. var times = SunCalc.times = [
  68. [-0.833, 'sunrise', 'sunset' ],
  69. [ -0.3, 'sunriseEnd', 'sunsetStart' ],
  70. [ -6, 'dawn', 'dusk' ],
  71. [ -12, 'nauticalDawn', 'nauticalDusk'],
  72. [ -18, 'nightEnd', 'night' ],
  73. [ 6, 'goldenHourEnd', 'goldenHour' ]
  74. ];
  75. // adds a custom time to the times config
  76. SunCalc.addTime = function (angle, riseName, setName) {
  77. times.push([angle, riseName, setName]);
  78. };
  79. // calculations for sun times
  80. var J0 = 0.0009;
  81. function julianCycle(d, lw) { return Math.round(d - J0 - lw / (2 * PI)); }
  82. function approxTransit(Ht, lw, n) { return J0 + (Ht + lw) / (2 * PI) + n; }
  83. function solarTransitJ(ds, M, L) { return J2000 + ds + 0.0053 * sin(M) - 0.0069 * sin(2 * L); }
  84. function hourAngle(h, phi, d) { return acos((sin(h) - sin(phi) * sin(d)) / (cos(phi) * cos(d))); }
  85. // returns set time for the given sun altitude
  86. function getSetJ(h, lw, phi, dec, n, M, L) {
  87. var w = hourAngle(h, phi, dec),
  88. a = approxTransit(w, lw, n);
  89. return solarTransitJ(a, M, L);
  90. }
  91. // calculates sun times for a given date and latitude/longitude
  92. SunCalc.getTimes = function (date, lat, lng) {
  93. var lw = rad * -lng,
  94. phi = rad * lat,
  95. d = toDays(date),
  96. n = julianCycle(d, lw),
  97. ds = approxTransit(0, lw, n),
  98. M = solarMeanAnomaly(ds),
  99. L = eclipticLongitude(M),
  100. dec = declination(L, 0),
  101. Jnoon = solarTransitJ(ds, M, L),
  102. i, len, time, Jset, Jrise;
  103. var result = {
  104. solarNoon: fromJulian(Jnoon),
  105. nadir: fromJulian(Jnoon - 0.5)
  106. };
  107. for (i = 0, len = times.length; i < len; i += 1) {
  108. time = times[i];
  109. Jset = getSetJ(time[0] * rad, lw, phi, dec, n, M, L);
  110. Jrise = Jnoon - (Jset - Jnoon);
  111. result[time[1]] = fromJulian(Jrise);
  112. result[time[2]] = fromJulian(Jset);
  113. }
  114. return result;
  115. };
  116. // moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
  117. function moonCoords(d) { // geocentric ecliptic coordinates of the moon
  118. var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude
  119. M = rad * (134.963 + 13.064993 * d), // mean anomaly
  120. F = rad * (93.272 + 13.229350 * d), // mean distance
  121. l = L + rad * 6.289 * sin(M), // longitude
  122. b = rad * 5.128 * sin(F), // latitude
  123. dt = 385001 - 20905 * cos(M); // distance to the moon in km
  124. return {
  125. ra: rightAscension(l, b),
  126. dec: declination(l, b),
  127. dist: dt
  128. };
  129. }
  130. SunCalc.getMoonPosition = function (date, lat, lng) {
  131. var lw = rad * -lng,
  132. phi = rad * lat,
  133. d = toDays(date),
  134. c = moonCoords(d),
  135. H = siderealTime(d, lw) - c.ra,
  136. h = altitude(H, phi, c.dec),
  137. // formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  138. pa = atan(sin(H), tan(phi) * cos(c.dec) - sin(c.dec) * cos(H));
  139. h = h + astroRefraction(h); // altitude correction for refraction
  140. return {
  141. azimuth: azimuth(H, phi, c.dec),
  142. altitude: h,
  143. distance: c.dist,
  144. parallacticAngle: pa
  145. };
  146. };
  147. // calculations for illumination parameters of the moon,
  148. // based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and
  149. // Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  150. SunCalc.getMoonIllumination = function (date) {
  151. var d = toDays(date || new Date()),
  152. s = sunCoords(d),
  153. m = moonCoords(d),
  154. sdist = 149598000, // distance from Earth to Sun in km
  155. phi = acos(sin(s.dec) * sin(m.dec) + cos(s.dec) * cos(m.dec) * cos(s.ra - m.ra)),
  156. inc = atan(sdist * sin(phi), m.dist - sdist * cos(phi)),
  157. angle = atan(cos(s.dec) * sin(s.ra - m.ra), sin(s.dec) * cos(m.dec) -
  158. cos(s.dec) * sin(m.dec) * cos(s.ra - m.ra));
  159. return {
  160. fraction: (1 + cos(inc)) / 2,
  161. phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
  162. angle: angle
  163. };
  164. };
  165. function hoursLater(date, h) {
  166. return new Date(date.valueOf() + h * dayMs / 24);
  167. }
  168. // calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article
  169. SunCalc.getMoonTimes = function (date, lat, lng, inUTC) {
  170. var t = new Date(date);
  171. if (inUTC) t.setUTCHours(0, 0, 0, 0);
  172. else t.setHours(0, 0, 0, 0);
  173. var hc = 0.133 * rad,
  174. h0 = SunCalc.getMoonPosition(t, lat, lng).altitude - hc,
  175. h1, h2, rise, set, a, b, xe, ye, d, roots, x1, x2, dx;
  176. // go in 2-hour chunks, each time seeing if a 3-point quadratic curve crosses zero (which means rise or set)
  177. for (var i = 1; i <= 24; i += 2) {
  178. h1 = SunCalc.getMoonPosition(hoursLater(t, i), lat, lng).altitude - hc;
  179. h2 = SunCalc.getMoonPosition(hoursLater(t, i + 1), lat, lng).altitude - hc;
  180. a = (h0 + h2) / 2 - h1;
  181. b = (h2 - h0) / 2;
  182. xe = -b / (2 * a);
  183. ye = (a * xe + b) * xe + h1;
  184. d = b * b - 4 * a * h1;
  185. roots = 0;
  186. if (d >= 0) {
  187. dx = Math.sqrt(d) / (Math.abs(a) * 2);
  188. x1 = xe - dx;
  189. x2 = xe + dx;
  190. if (Math.abs(x1) <= 1) roots++;
  191. if (Math.abs(x2) <= 1) roots++;
  192. if (x1 < -1) x1 = x2;
  193. }
  194. if (roots === 1) {
  195. if (h0 < 0) rise = i + x1;
  196. else set = i + x1;
  197. } else if (roots === 2) {
  198. rise = i + (ye < 0 ? x2 : x1);
  199. set = i + (ye < 0 ? x1 : x2);
  200. }
  201. if (rise && set) break;
  202. h0 = h2;
  203. }
  204. var result = {};
  205. if (rise) result.rise = hoursLater(t, rise);
  206. if (set) result.set = hoursLater(t, set);
  207. if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true;
  208. return result;
  209. };
  210. // export as Node module / AMD module / browser variable
  211. if (typeof exports === 'object' && typeof module !== 'undefined') module.exports = SunCalc;
  212. else if (typeof define === 'function' && define.amd) define(SunCalc);
  213. else window.SunCalc = SunCalc;
  214. }());