suncalc.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // 太阳计算基于 http://aa.quae.nl/en/reken/zonpositie.html 上的公式
  18. // date/time constants and conversions 日期/时间常量和转换
  19. var dayMs = 1000 * 60 * 60 * 24,//一天的毫秒数
  20. J1970 = 2440588,
  21. J2000 = 2451545;
  22. function toJulian(date) { return date.valueOf() / dayMs - 0.5 + J1970; }
  23. function fromJulian(j) { return new Date((j + 0.5 - J1970) * dayMs); }
  24. function toDays(date) { return toJulian(date) - J2000; }
  25. // general calculations for position 通用位置计算
  26. var e = rad * 23.4397; // obliquity of the Earth
  27. function rightAscension(l, b) { return atan(sin(l) * cos(e) - tan(b) * sin(e), cos(l)); }
  28. function declination(l, b) { return asin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l)); }
  29. //方位角
  30. function azimuth(H, phi, dec) { return atan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi)); }
  31. //高度角
  32. function altitude(H, phi, dec) { return asin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H)); }
  33. function siderealTime(d, lw) { return rad * (280.16 + 360.9856235 * d) - lw; }
  34. function astroRefraction(h) {
  35. if (h < 0) // the following formula works for positive altitudes only.
  36. h = 0; // if h = -0.08901179 a div/0 would occur.
  37. // formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  38. // 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:
  39. return 0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179));
  40. }
  41. // general sun calculations 通用太阳计算
  42. function solarMeanAnomaly(d) { return rad * (357.5291 + 0.98560028 * d); }
  43. function eclipticLongitude(M) {
  44. var C = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)), // equation of center 中心差
  45. P = rad * 102.9372; // perihelion of the Earth 地球近日点
  46. return M + C + P + PI;
  47. }
  48. function sunCoords(d) {
  49. var M = solarMeanAnomaly(d),
  50. L = eclipticLongitude(M);
  51. return {
  52. dec: declination(L, 0),
  53. ra: rightAscension(L, 0)
  54. };
  55. }
  56. var SunCalc = {};
  57. // calculates sun position for a given date and latitude/longitude 根据给定的日期与经纬度计算太阳位置
  58. SunCalc.getPosition = function (date, lat, lng) {
  59. var lw = rad * -lng,
  60. phi = rad * lat,
  61. d = toDays(date),
  62. c = sunCoords(d),
  63. H = siderealTime(d, lw) - c.ra;
  64. return {
  65. azimuth: azimuth(H, phi, c.dec),
  66. altitude: altitude(H, phi, c.dec)
  67. };
  68. };
  69. // sun times configuration (angle, morning name, evening name) 太阳时间配置(高度,晨名,昏名)
  70. var times = SunCalc.times = [
  71. [-0.833, 'sunrise', 'sunset' ], //日出,日落
  72. [ -0.3, 'sunriseEnd', 'sunsetStart' ], //日出结束,日落开始
  73. [-6.833, 'dawn', 'dusk' ], //曙光升起,夜幕降临(修正蒙气差)
  74. [ -12, 'nauticalDawn', 'nauticalDusk'], //航海曙光,航海暮光
  75. [ -18, 'nightEnd', 'night' ], //夜尽,入夜
  76. [ 6, 'goldenHourEnd', 'goldenHour' ] //朝霞结束,晚霞开始
  77. ];
  78. // adds a custom time to the times config 在时间配置中添加自定义时间
  79. SunCalc.addTime = function (angle, riseName, setName) {
  80. times.push([angle, riseName, setName]);
  81. };
  82. // calculations for sun times 计算太阳时
  83. var J0 = 0.0009;
  84. function julianCycle(d, lw) { return Math.round(d - J0 - lw / (2 * PI)); }
  85. function approxTransit(Ht, lw, n) { return J0 + (Ht + lw) / (2 * PI) + n; }
  86. function solarTransitJ(ds, M, L) { return J2000 + ds + 0.0053 * sin(M) - 0.0069 * sin(2 * L); }
  87. function hourAngle(h, phi, d) { return acos((sin(h) - sin(phi) * sin(d)) / (cos(phi) * cos(d))); }
  88. function observerAngle(height) { return -2.076 * Math.sqrt(height) / 60; }
  89. // returns set time for the given sun altitude 返回给定太阳高度的设置时间
  90. function getSetJ(h, lw, phi, dec, n, M, L) {
  91. var w = hourAngle(h, phi, dec),
  92. a = approxTransit(w, lw, n);
  93. return solarTransitJ(a, M, L);
  94. }
  95. // calculates sun times for a given date, latitude/longitude, and, optionally,
  96. // 计算给定日期,纬度/经度的太阳时间,以及(可选)
  97. // the observer height (in meters) relative to the horizon
  98. // 观察者相对于地平线的高度(以米为单位)
  99. SunCalc.getTimes = function (date, lat, lng, height) {
  100. height = height || 0;
  101. var lw = rad * -lng,
  102. phi = rad * lat,
  103. dh = observerAngle(height),
  104. d = toDays(date),
  105. n = julianCycle(d, lw),
  106. ds = approxTransit(0, lw, n),
  107. M = solarMeanAnomaly(ds),
  108. L = eclipticLongitude(M),
  109. dec = declination(L, 0),
  110. Jnoon = solarTransitJ(ds, M, L),
  111. i, len, time, h0, Jset, Jrise;
  112. var result = {
  113. solarNoon: fromJulian(Jnoon),
  114. nadir: fromJulian(Jnoon - 0.5)
  115. };
  116. for (i = 0, len = times.length; i < len; i += 1) {
  117. time = times[i];
  118. h0 = (time[0] + dh) * rad;
  119. Jset = getSetJ(h0, lw, phi, dec, n, M, L);
  120. Jrise = Jnoon - (Jset - Jnoon);
  121. result[time[1]] = fromJulian(Jrise);
  122. result[time[2]] = fromJulian(Jset);
  123. }
  124. return result;
  125. };
  126. // moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
  127. // 月亮计算,基于 http://aa.quae.nl/en/reken/hemelpositie.html 上的公式
  128. function moonCoords(d) { // geocentric ecliptic coordinates of the moon 月球的地心黄道坐标
  129. var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude 黄道经度
  130. M = rad * (134.963 + 13.064993 * d), // mean anomaly 平均异常
  131. F = rad * (93.272 + 13.229350 * d), // mean distance 平均距离
  132. l = L + rad * 6.289 * sin(M), // longitude 经度
  133. b = rad * 5.128 * sin(F), // latitude 纬度
  134. dt = 385001 - 20905 * cos(M); // distance to the moon in km 地月距离
  135. return {
  136. ra: rightAscension(l, b),
  137. dec: declination(l, b),
  138. dist: dt
  139. };
  140. }
  141. SunCalc.getMoonPosition = function (date, lat, lng) {
  142. var lw = rad * -lng,
  143. phi = rad * lat,
  144. d = toDays(date),
  145. c = moonCoords(d),
  146. H = siderealTime(d, lw) - c.ra,
  147. h = altitude(H, phi, c.dec),
  148. // formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  149. // 公式14.1——“Astronomical Algorithms” 第二版 Jean Meeus (Willmann-Bell, Richmond) 1998
  150. pa = atan(sin(H), tan(phi) * cos(c.dec) - sin(c.dec) * cos(H));
  151. h = h + astroRefraction(h); // altitude correction for refraction 折射高度修正
  152. return {
  153. azimuth: azimuth(H, phi, c.dec),
  154. altitude: h,
  155. distance: c.dist,
  156. parallacticAngle: pa
  157. };
  158. };
  159. // calculations for illumination parameters of the moon, 计算月球的照明参数,
  160. // based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and
  161. // 基于 http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro 上的公式
  162. // Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
  163. // 第48章 ——“Astronomical Algorithms” 第二版 Jean Meeus (Willmann-Bell, Richmond) 1998
  164. SunCalc.getMoonIllumination = function (date) {
  165. var d = toDays(date || new Date()),
  166. s = sunCoords(d),
  167. m = moonCoords(d),
  168. sdist = 149598000, // distance from Earth to Sun in km 地日距离(单位:千米)
  169. phi = acos(sin(s.dec) * sin(m.dec) + cos(s.dec) * cos(m.dec) * cos(s.ra - m.ra)),
  170. inc = atan(sdist * sin(phi), m.dist - sdist * cos(phi)),
  171. angle = atan(cos(s.dec) * sin(s.ra - m.ra), sin(s.dec) * cos(m.dec) -
  172. cos(s.dec) * sin(m.dec) * cos(s.ra - m.ra));
  173. return {
  174. fraction: (1 + cos(inc)) / 2,
  175. phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
  176. angle: angle
  177. };
  178. };
  179. function hoursLater(date, h) {
  180. return new Date(date.valueOf() + h * dayMs / 24);
  181. }
  182. // calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article
  183. // 计算月亮升起和落下的实践,基于 http://www.stargazing.net/kepler/moonrise.html 上的文章
  184. SunCalc.getMoonTimes = function (date, lat, lng, inUTC) {
  185. var t = new Date(date);
  186. if (inUTC) t.setUTCHours(0, 0, 0, 0);
  187. else t.setHours(0, 0, 0, 0);
  188. var hc = 0.133 * rad,
  189. h0 = SunCalc.getMoonPosition(t, lat, lng).altitude - hc,
  190. h1, h2, rise, set, a, b, xe, ye, d, roots, x1, x2, dx;
  191. // go in 2-hour chunks, each time seeing if a 3-point quadratic curve crosses zero (which means rise or set)
  192. // 进行2小时的测试,每次查看3点二次曲线是否过零(表示上升或下降)
  193. for (var i = 1; i <= 24; i += 2) {
  194. h1 = SunCalc.getMoonPosition(hoursLater(t, i), lat, lng).altitude - hc;
  195. h2 = SunCalc.getMoonPosition(hoursLater(t, i + 1), lat, lng).altitude - hc;
  196. a = (h0 + h2) / 2 - h1;
  197. b = (h2 - h0) / 2;
  198. xe = -b / (2 * a);
  199. ye = (a * xe + b) * xe + h1;
  200. d = b * b - 4 * a * h1;
  201. roots = 0;
  202. if (d >= 0) {
  203. dx = Math.sqrt(d) / (Math.abs(a) * 2);
  204. x1 = xe - dx;
  205. x2 = xe + dx;
  206. if (Math.abs(x1) <= 1) roots++;
  207. if (Math.abs(x2) <= 1) roots++;
  208. if (x1 < -1) x1 = x2;
  209. }
  210. if (roots === 1) {
  211. if (h0 < 0) rise = i + x1;
  212. else set = i + x1;
  213. } else if (roots === 2) {
  214. rise = i + (ye < 0 ? x2 : x1);
  215. set = i + (ye < 0 ? x1 : x2);
  216. }
  217. if (rise && set) break;
  218. h0 = h2;
  219. }
  220. var result = {};
  221. if (rise) result.rise = hoursLater(t, rise);
  222. if (set) result.set = hoursLater(t, set);
  223. if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true;
  224. return result;
  225. };
  226. // export as Node module / AMD module / browser variable
  227. // 导出为节点模块/ AMD模块/浏览器变量
  228. if (typeof exports === 'object' && typeof module !== 'undefined') module.exports = SunCalc;
  229. else if (typeof define === 'function' && define.amd) define(SunCalc);
  230. else window.SunCalc = SunCalc;
  231. }());