Node-Red configuration
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

suncalc.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. function observerAngle(height) { return -2.076 * Math.sqrt(height) / 60; }
  86. // returns set time for the given sun altitude
  87. function getSetJ(h, lw, phi, dec, n, M, L) {
  88. var w = hourAngle(h, phi, dec),
  89. a = approxTransit(w, lw, n);
  90. return solarTransitJ(a, M, L);
  91. }
  92. // calculates sun times for a given date, latitude/longitude, and, optionally,
  93. // the observer height (in meters) relative to the horizon
  94. SunCalc.getTimes = function (date, lat, lng, height) {
  95. height = height || 0;
  96. var lw = rad * -lng,
  97. phi = rad * lat,
  98. dh = observerAngle(height),
  99. d = toDays(date),
  100. n = julianCycle(d, lw),
  101. ds = approxTransit(0, lw, n),
  102. M = solarMeanAnomaly(ds),
  103. L = eclipticLongitude(M),
  104. dec = declination(L, 0),
  105. Jnoon = solarTransitJ(ds, M, L),
  106. i, len, time, h0, Jset, Jrise;
  107. var result = {
  108. solarNoon: fromJulian(Jnoon),
  109. nadir: fromJulian(Jnoon - 0.5)
  110. };
  111. for (i = 0, len = times.length; i < len; i += 1) {
  112. time = times[i];
  113. h0 = (time[0] + dh) * rad;
  114. Jset = getSetJ(h0, lw, phi, dec, n, M, L);
  115. Jrise = Jnoon - (Jset - Jnoon);
  116. result[time[1]] = fromJulian(Jrise);
  117. result[time[2]] = fromJulian(Jset);
  118. }
  119. return result;
  120. };
  121. // moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
  122. function moonCoords(d) { // geocentric ecliptic coordinates of the moon
  123. var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude
  124. M = rad * (134.963 + 13.064993 * d), // mean anomaly
  125. F = rad * (93.272 + 13.229350 * d), // mean distance
  126. l = L + rad * 6.289 * sin(M), // longitude
  127. b = rad * 5.128 * sin(F), // latitude
  128. dt = 385001 - 20905 * cos(M); // distance to the moon in km
  129. return {
  130. ra: rightAscension(l, b),
  131. dec: declination(l, b),
  132. dist: dt
  133. };
  134. }
  135. SunCalc.getMoonPosition = function (date, lat, lng) {
  136. var lw = rad * -lng,
  137. phi = rad * lat,
  138. d = toDays(date),
  139. c = moonCoords(d),
  140. H = siderealTime(d, lw) - c.ra,
  141. h = altitude(H, phi, c.dec),
  142. // formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  143. pa = atan(sin(H), tan(phi) * cos(c.dec) - sin(c.dec) * cos(H));
  144. h = h + astroRefraction(h); // altitude correction for refraction
  145. return {
  146. azimuth: azimuth(H, phi, c.dec),
  147. altitude: h,
  148. distance: c.dist,
  149. parallacticAngle: pa
  150. };
  151. };
  152. // calculations for illumination parameters of the moon,
  153. // based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and
  154. // Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  155. SunCalc.getMoonIllumination = function (date) {
  156. var d = toDays(date || new Date()),
  157. s = sunCoords(d),
  158. m = moonCoords(d),
  159. sdist = 149598000, // distance from Earth to Sun in km
  160. phi = acos(sin(s.dec) * sin(m.dec) + cos(s.dec) * cos(m.dec) * cos(s.ra - m.ra)),
  161. inc = atan(sdist * sin(phi), m.dist - sdist * cos(phi)),
  162. angle = atan(cos(s.dec) * sin(s.ra - m.ra), sin(s.dec) * cos(m.dec) -
  163. cos(s.dec) * sin(m.dec) * cos(s.ra - m.ra));
  164. return {
  165. fraction: (1 + cos(inc)) / 2,
  166. phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
  167. angle: angle
  168. };
  169. };
  170. function hoursLater(date, h) {
  171. return new Date(date.valueOf() + h * dayMs / 24);
  172. }
  173. // calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article
  174. SunCalc.getMoonTimes = function (date, lat, lng, inUTC) {
  175. var t = new Date(date);
  176. if (inUTC) t.setUTCHours(0, 0, 0, 0);
  177. else t.setHours(0, 0, 0, 0);
  178. var hc = 0.133 * rad,
  179. h0 = SunCalc.getMoonPosition(t, lat, lng).altitude - hc,
  180. h1, h2, rise, set, a, b, xe, ye, d, roots, x1, x2, dx;
  181. // go in 2-hour chunks, each time seeing if a 3-point quadratic curve crosses zero (which means rise or set)
  182. for (var i = 1; i <= 24; i += 2) {
  183. h1 = SunCalc.getMoonPosition(hoursLater(t, i), lat, lng).altitude - hc;
  184. h2 = SunCalc.getMoonPosition(hoursLater(t, i + 1), lat, lng).altitude - hc;
  185. a = (h0 + h2) / 2 - h1;
  186. b = (h2 - h0) / 2;
  187. xe = -b / (2 * a);
  188. ye = (a * xe + b) * xe + h1;
  189. d = b * b - 4 * a * h1;
  190. roots = 0;
  191. if (d >= 0) {
  192. dx = Math.sqrt(d) / (Math.abs(a) * 2);
  193. x1 = xe - dx;
  194. x2 = xe + dx;
  195. if (Math.abs(x1) <= 1) roots++;
  196. if (Math.abs(x2) <= 1) roots++;
  197. if (x1 < -1) x1 = x2;
  198. }
  199. if (roots === 1) {
  200. if (h0 < 0) rise = i + x1;
  201. else set = i + x1;
  202. } else if (roots === 2) {
  203. rise = i + (ye < 0 ? x2 : x1);
  204. set = i + (ye < 0 ? x1 : x2);
  205. }
  206. if (rise && set) break;
  207. h0 = h2;
  208. }
  209. var result = {};
  210. if (rise) result.rise = hoursLater(t, rise);
  211. if (set) result.set = hoursLater(t, set);
  212. if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true;
  213. return result;
  214. };
  215. // export as Node module / AMD module / browser variable
  216. if (typeof exports === 'object' && typeof module !== 'undefined') module.exports = SunCalc;
  217. else if (typeof define === 'function' && define.amd) define(SunCalc);
  218. else window.SunCalc = SunCalc;
  219. }());