UNPKG

12 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.Sunrise = undefined;
7
8var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
9 * @copyright 2016 commenthol
10 * @license MIT
11 * @module sunrise
12 */
13/**
14 * Sunrise: Compute rise, noon, set of the Sun for an earth observer
15 */
16
17/* eslint key-spacing: 0 */
18
19var _base = require('./base');
20
21var _base2 = _interopRequireDefault(_base);
22
23var _eqtime = require('./eqtime');
24
25var _eqtime2 = _interopRequireDefault(_eqtime);
26
27var _sexagesimal = require('./sexagesimal');
28
29var _sexagesimal2 = _interopRequireDefault(_sexagesimal);
30
31var _solar = require('./solar');
32
33var _solar2 = _interopRequireDefault(_solar);
34
35var _julian = require('./julian');
36
37var _julian2 = _interopRequireDefault(_julian);
38
39var _rise = require('./rise');
40
41var _rise2 = _interopRequireDefault(_rise);
42
43function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
44
45function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
46
47var stdh0 = {
48 sunrise: new _sexagesimal2.default.Angle(true, 0, 50, 0).rad(),
49 sunriseEnd: new _sexagesimal2.default.Angle(true, 0, 18, 0).rad(),
50 twilight: new _sexagesimal2.default.Angle(true, 6, 0, 0).rad(),
51 nauticalTwilight: new _sexagesimal2.default.Angle(true, 12, 0, 0).rad(),
52 night: new _sexagesimal2.default.Angle(true, 18, 0, 0).rad(),
53 goldenHour: new _sexagesimal2.default.Angle(false, 6, 0, 0).rad()
54};
55
56var stdh0Sunrise = function stdh0Sunrise(refraction) {
57 return _rise2.default.refraction(stdh0.sunrise, refraction);
58};
59var stdh0SunriseEnd = function stdh0SunriseEnd(refraction) {
60 return _rise2.default.refraction(stdh0.sunriseEnd, refraction);
61};
62var stdh0Twilight = function stdh0Twilight(refraction) {
63 return _rise2.default.refraction(stdh0.twilight, refraction);
64};
65var stdh0NauticalTwilight = function stdh0NauticalTwilight(refraction) {
66 return _rise2.default.refraction(stdh0.nauticalTwilight, refraction);
67};
68var stdh0Night = function stdh0Night(refraction) {
69 return _rise2.default.refraction(stdh0.night, refraction);
70};
71var stdh0GoldenHour = function stdh0GoldenHour(refraction) {
72 return _rise2.default.refraction(stdh0.goldenHour, refraction);
73};
74
75var Sunrise = exports.Sunrise = function () {
76 /**
77 * Computes time of sunrise, sunset for a given day `date` of an observer on earth given by latitude and longitude.
78 * Methods may return `undefined` instead of `julian.Calendar` for latitudes very near the poles.
79 * @param {julian.Calendar} date - calendar date
80 * @param {number} lat - latitude of observer in the range of (-89.6, 89.6)
81 * @param {number} lon - longitude of observer (measured positively westwards, New York = 40.7° lat, 74° lon)
82 * @param {number} [refraction] - optional refraction
83 */
84 function Sunrise(date, lat, lon, refraction) {
85 _classCallCheck(this, Sunrise);
86
87 this.date = date;
88 this.jde = date.midnight().toJDE();
89 this.lat = _sexagesimal2.default.angleFromDeg(lat);
90 this.lon = _sexagesimal2.default.angleFromDeg(lon);
91 this.refraction = refraction;
92 }
93
94 _createClass(Sunrise, [{
95 key: '_calcNoon',
96 value: function _calcNoon(jde) {
97 var etime = _sexagesimal2.default.secFromHourAngle(_eqtime2.default.eSmart(jde));
98 var delta = _sexagesimal2.default.secFromHourAngle(this.lon);
99 var time = 43200 /* noon */ + delta - etime; // in seconds
100 return _base2.default.pmod(time / 86400, 86400);
101 }
102 }, {
103 key: '_calcRiseOrSet',
104 value: function _calcRiseOrSet(jde, h0, isSet) {
105 var etime = _sexagesimal2.default.secFromHourAngle(_eqtime2.default.eSmart(jde));
106 var solarDec = _solar2.default.apparentEquatorial(jde).dec;
107 var ha = _rise2.default.hourAngle(this.lat, h0, solarDec);
108 if (isSet) ha = -ha;
109 var delta = _sexagesimal2.default.secFromHourAngle(ha - this.lon);
110 var time = 43200 /* noon */ - delta - etime; // in seconds
111 return time / 86400;
112 }
113 }, {
114 key: '_calcPolarDayNight',
115 value: function _calcPolarDayNight(h0, isSet, step) {
116 var jde = this.jde;
117 var t = void 0;
118 var failCnt = 0;
119 while (failCnt < 190) {
120 // a bit more than days of half a year
121 jde += step;
122 try {
123 t = this._calcRiseOrSet(jde, h0, isSet);
124 t = this._calcRiseOrSet(jde + t, h0, isSet);
125 break;
126 } catch (e) {
127 t = undefined;
128 failCnt++;
129 }
130 }
131 if (t === undefined) {
132 return;
133 }
134 return new _julian2.default.Calendar().fromJDE(jde + t);
135 }
136 }, {
137 key: '_calc',
138 value: function _calc(h0, isSet) {
139 var t = void 0;
140 var jde = this.jde;
141 // calc 2times for higher accuracy
142 try {
143 t = this._calcRiseOrSet(jde, h0, isSet);
144 t = this._calcRiseOrSet(jde + t, h0, isSet);
145 return new _julian2.default.Calendar().fromJDE(jde + t);
146 } catch (e) {
147 var step = isSet ? -1 : 1;
148 var doy = this.date.dayOfYear();
149 if ( // overlap with march, september equinoxes
150 this.lat > 0 && doy > 76 && doy < 267 || // northern hemisphere
151 this.lat < 0 && (doy < 83 || doy > 262) // southern hemisphere
152 ) {
153 step = -step;
154 }
155 return this._calcPolarDayNight(h0, isSet, step);
156 }
157 }
158
159 /**
160 * time of solar transit
161 * @return {julian.Calendar} time of noon
162 */
163
164 }, {
165 key: 'noon',
166 value: function noon() {
167 var jde = this.jde;
168 // calc 2times for higher accuracy
169 var t = this._calcNoon(jde + this.lon / (2 * Math.PI));
170 t = this._calcNoon(jde + t);
171 return new _julian2.default.Calendar().fromJDE(jde + t);
172 }
173
174 /**
175 * Solar limb appears over the easter horizon in the morning
176 * @return {julian.Calendar} time of sunrise
177 */
178
179 }, {
180 key: 'rise',
181 value: function rise() {
182 return this._calc(stdh0Sunrise(this.refraction), false);
183 }
184
185 /**
186 * @return {julian.Calendar} time of sunset
187 * Solar limb disappears on the western horizon in the evening
188 */
189
190 }, {
191 key: 'set',
192 value: function set() {
193 return this._calc(stdh0Sunrise(this.refraction), true);
194 }
195
196 /**
197 * Solar limb is fully visible at the easter horizon
198 * @return {julian.Calendar} time of sunrise end
199 */
200
201 }, {
202 key: 'riseEnd',
203 value: function riseEnd() {
204 return this._calc(stdh0SunriseEnd(this.refraction), false);
205 }
206
207 /**
208 * Solar limb starts disappearing on the western horizon in the evening
209 * @return {julian.Calendar} time of sunset start
210 */
211
212 }, {
213 key: 'setStart',
214 value: function setStart() {
215 return this._calc(stdh0SunriseEnd(this.refraction), true);
216 }
217
218 /**
219 * Dawn, there is still enough light for objects to be distinguishable,
220 * @return {julian.Calendar} time of dawn
221 */
222
223 }, {
224 key: 'dawn',
225 value: function dawn() {
226 return this._calc(stdh0Twilight(this.refraction), false);
227 }
228
229 /**
230 * Dusk, there is still enough light for objects to be distinguishable
231 * Bright stars and planets are visible by naked eye
232 * @return {julian.Calendar} time of dusk
233 */
234
235 }, {
236 key: 'dusk',
237 value: function dusk() {
238 return this._calc(stdh0Twilight(this.refraction), true);
239 }
240
241 /**
242 * nautical dawn - Horizon gets visible by naked eye
243 * @return {julian.Calendar} time of nautical dawn
244 */
245
246 }, {
247 key: 'nauticalDawn',
248 value: function nauticalDawn() {
249 return this._calc(stdh0NauticalTwilight(this.refraction), false);
250 }
251
252 /**
253 * nautical dusk - Horizon is no longer visible by naked eye
254 * @return {julian.Calendar} time of nautical dusk
255 */
256
257 }, {
258 key: 'nauticalDusk',
259 value: function nauticalDusk() {
260 return this._calc(stdh0NauticalTwilight(this.refraction), true);
261 }
262
263 /**
264 * night starts - No sunlight illumination of the sky, such no intereferance
265 * with astronomical observations.
266 * @return {julian.Calendar} time of start of night
267 */
268
269 }, {
270 key: 'nightStart',
271 value: function nightStart() {
272 return this._calc(stdh0Night(this.refraction), true);
273 }
274
275 /**
276 * night end - Sunlight starts illumination of the sky and interferes
277 * with astronomical observations.
278 * @return {julian.Calendar} time of end of night
279 */
280
281 }, {
282 key: 'nightEnd',
283 value: function nightEnd() {
284 return this._calc(stdh0Night(this.refraction), false);
285 }
286
287 /**
288 * Start of "golden hour" before sunset
289 * @return {julian.Calendar} time of start of golden hour
290 */
291
292 }, {
293 key: 'goldenHourStart',
294 value: function goldenHourStart() {
295 return this._calc(stdh0GoldenHour(this.refraction), true);
296 }
297
298 /**
299 * End of "golden hour" after sunrise
300 * @return {julian.Calendar} time of end of golden hour
301 */
302
303 }, {
304 key: 'goldenHourEnd',
305 value: function goldenHourEnd() {
306 return this._calc(stdh0GoldenHour(this.refraction), false);
307 }
308 }]);
309
310 return Sunrise;
311}();
312
313exports.default = {
314 Sunrise: Sunrise
315};
\No newline at end of file