@hebcal/noaa 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1053 @@
1
+ import { Temporal } from 'temporal-polyfill';
2
+ /**
3
+ * java.lang.Math.toRadians
4
+ * @param degrees
5
+ */
6
+ function degreesToRadians(degrees) {
7
+ return (degrees * Math.PI) / 180;
8
+ }
9
+ /**
10
+ * java.lang.Math.toDegrees
11
+ * @param radians
12
+ */
13
+ function radiansToDegrees(radians) {
14
+ return (radians * 180) / Math.PI;
15
+ }
16
+ const Long_MIN_VALUE = NaN;
17
+ /**
18
+ * A class that contains location information such as latitude and longitude required for astronomical calculations. The
19
+ * elevation field may not be used by some calculation engines and would be ignored if set. Check the documentation for
20
+ * specific implementations of the {@link AstronomicalCalculator} to see if elevation is calculated as part of the
21
+ * algorithm.
22
+ *
23
+ * @author © Eliyahu Hershfeld 2004 - 2016
24
+ * @version 1.1
25
+ */
26
+ export class GeoLocation {
27
+ /**
28
+ * Method to get the elevation in Meters.
29
+ *
30
+ * @return Returns the elevation in Meters.
31
+ */
32
+ getElevation() {
33
+ return this.elevation;
34
+ }
35
+ /**
36
+ * Method to set the elevation in Meters <b>above </b> sea level.
37
+ *
38
+ * @param elevation
39
+ * The elevation to set in Meters. An IllegalArgumentException will be thrown if the value is a negative.
40
+ */
41
+ setElevation(elevation) {
42
+ this.elevation = elevation;
43
+ }
44
+ constructor(name = 'Greenwich, England', latitude = 51.4772, longitude = 0, elevationOrTimeZoneId, timeZoneId) {
45
+ /**
46
+ * @see #getLocationName()
47
+ * @see #setLocationName(String)
48
+ */
49
+ this.locationName = null;
50
+ let elevation = 0;
51
+ if (timeZoneId) {
52
+ elevation = elevationOrTimeZoneId;
53
+ }
54
+ else {
55
+ timeZoneId = elevationOrTimeZoneId;
56
+ }
57
+ this.setLocationName(name);
58
+ this.setLatitude(latitude);
59
+ this.setLongitude(longitude);
60
+ this.setElevation(elevation);
61
+ this.setTimeZone(timeZoneId);
62
+ }
63
+ setLatitude(latitude) {
64
+ this.latitude = latitude;
65
+ }
66
+ /**
67
+ * @return Returns the latitude.
68
+ */
69
+ getLatitude() {
70
+ return this.latitude;
71
+ }
72
+ setLongitude(longitude) {
73
+ this.longitude = longitude;
74
+ }
75
+ /**
76
+ * @return Returns the longitude.
77
+ */
78
+ getLongitude() {
79
+ return this.longitude;
80
+ }
81
+ /**
82
+ * @return Returns the location name.
83
+ */
84
+ getLocationName() {
85
+ return this.locationName;
86
+ }
87
+ /**
88
+ * @param name
89
+ * The setter method for the display name.
90
+ */
91
+ setLocationName(name) {
92
+ this.locationName = name;
93
+ }
94
+ /**
95
+ * @return Returns the timeZone.
96
+ */
97
+ getTimeZone() {
98
+ return this.timeZoneId;
99
+ }
100
+ /**
101
+ * Method to set the TimeZone. If this is ever set after the GeoLocation is set in the
102
+ * {@link AstronomicalCalendar}, it is critical that
103
+ * {@link AstronomicalCalendar#getCalendar()}.
104
+ * {@link java.util.Calendar#setTimeZone(TimeZone) setTimeZone(TimeZone)} be called in order for the
105
+ * AstronomicalCalendar to output times in the expected offset. This situation will arise if the
106
+ * AstronomicalCalendar is ever {@link AstronomicalCalendar#clone() cloned}.
107
+ *
108
+ * @param timeZone
109
+ * The timeZone to set.
110
+ */
111
+ setTimeZone(timeZoneId) {
112
+ this.timeZoneId = timeZoneId;
113
+ }
114
+ }
115
+ /**
116
+ * The commonly used average solar refraction. Calendrical Calculations lists a more accurate global average of
117
+ * 34.478885263888294
118
+ * @private
119
+ */
120
+ const refraction = 34 / 60;
121
+ // private double refraction = 34.478885263888294 / 60d;
122
+ /**
123
+ * The commonly used average solar radius in minutes of a degree.
124
+ * @private
125
+ */
126
+ const solarRadius = 16 / 60;
127
+ /**
128
+ * The commonly used average earth radius in KM. At this time, this only affects elevation adjustment and not the
129
+ * sunrise and sunset calculations. The value currently defaults to 6356.9 KM.
130
+ * @private
131
+ */
132
+ const earthRadius = 6356.9; // in KM
133
+ /**
134
+ * Implementation of sunrise and sunset methods to calculate astronomical times based on the <a
135
+ * href="http://noaa.gov">NOAA</a> algorithm. This calculator uses the Java algorithm based on the implementation by <a
136
+ * href="http://noaa.gov">NOAA - National Oceanic and Atmospheric Administration</a>'s <a href =
137
+ * "http://www.srrb.noaa.gov/highlights/sunrise/sunrise.html">Surface Radiation Research Branch</a>. NOAA's <a
138
+ * href="http://www.srrb.noaa.gov/highlights/sunrise/solareqns.PDF">implementation</a> is based on equations from <a
139
+ * href="http://www.willbell.com/math/mc1.htm">Astronomical Algorithms</a> by <a
140
+ * href="http://en.wikipedia.org/wiki/Jean_Meeus">Jean Meeus</a>. Added to the algorithm is an adjustment of the zenith
141
+ * to account for elevation. The algorithm can be found in the <a
142
+ * href="http://en.wikipedia.org/wiki/Sunrise_equation">Wikipedia Sunrise Equation</a> article.
143
+ *
144
+ * @author &copy; Eliyahu Hershfeld 2011 - 2019
145
+ */
146
+ export class NOAACalculator {
147
+ /**
148
+ * The getSunrise method Returns a <code>Date</code> representing the
149
+ * {@link AstronomicalCalculator#getElevationAdjustment(double) elevation adjusted} sunrise time. The zenith used
150
+ * for the calculation uses {@link #GEOMETRIC_ZENITH geometric zenith} of 90&deg; plus
151
+ * {@link AstronomicalCalculator#getElevationAdjustment(double)}. This is adjusted by the
152
+ * {@link AstronomicalCalculator} to add approximately 50/60 of a degree to account for 34 archminutes of refraction
153
+ * and 16 archminutes for the sun's radius for a total of {@link AstronomicalCalculator#adjustZenith 90.83333&deg;}.
154
+ * See documentation for the specific implementation of the {@link AstronomicalCalculator} that you are using.
155
+ *
156
+ * @return the <code>Date</code> representing the exact sunrise time. If the calculation can't be computed such as
157
+ * in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it
158
+ * does not set, a null will be returned. See detailed explanation on top of the page.
159
+ * @see AstronomicalCalculator#adjustZenith
160
+ * @see #getSeaLevelSunrise()
161
+ * @see AstronomicalCalendar#getUTCSunrise
162
+ */
163
+ getSunrise() {
164
+ const sunrise = this.getUTCSunrise0(NOAACalculator.GEOMETRIC_ZENITH);
165
+ if (Number.isNaN(sunrise))
166
+ return null;
167
+ return this.getDateFromTime(sunrise, true);
168
+ }
169
+ /**
170
+ * A method that returns the sunrise without {@link AstronomicalCalculator#getElevationAdjustment(double) elevation
171
+ * adjustment}. Non-sunrise and sunset calculations such as dawn and dusk, depend on the amount of visible light,
172
+ * something that is not affected by elevation. This method returns sunrise calculated at sea level. This forms the
173
+ * base for dawn calculations that are calculated as a dip below the horizon before sunrise.
174
+ *
175
+ * @return the <code>Date</code> representing the exact sea-level sunrise time. If the calculation can't be computed
176
+ * such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one
177
+ * where it does not set, a null will be returned. See detailed explanation on top of the page.
178
+ * @see AstronomicalCalendar#getSunrise
179
+ * @see AstronomicalCalendar#getUTCSeaLevelSunrise
180
+ * @see #getSeaLevelSunset()
181
+ */
182
+ getSeaLevelSunrise() {
183
+ const sunrise = this.getUTCSeaLevelSunrise(NOAACalculator.GEOMETRIC_ZENITH);
184
+ if (Number.isNaN(sunrise))
185
+ return null;
186
+ return this.getDateFromTime(sunrise, true);
187
+ }
188
+ /**
189
+ * A method that returns the beginning of civil twilight (dawn) using a zenith of {@link #CIVIL_ZENITH 96&deg;}.
190
+ *
191
+ * @return The <code>Date</code> of the beginning of civil twilight using a zenith of 96&deg;. If the calculation
192
+ * can't be computed, null will be returned. See detailed explanation on top of the page.
193
+ * @see #CIVIL_ZENITH
194
+ */
195
+ getBeginCivilTwilight() {
196
+ return this.getSunriseOffsetByDegrees(NOAACalculator.CIVIL_ZENITH);
197
+ }
198
+ /**
199
+ * A method that returns the beginning of nautical twilight using a zenith of {@link #NAUTICAL_ZENITH 102&deg;}.
200
+ *
201
+ * @return The <code>Date</code> of the beginning of nautical twilight using a zenith of 102&deg;. If the
202
+ * calculation can't be computed null will be returned. See detailed explanation on top of the page.
203
+ * @see #NAUTICAL_ZENITH
204
+ */
205
+ getBeginNauticalTwilight() {
206
+ return this.getSunriseOffsetByDegrees(NOAACalculator.NAUTICAL_ZENITH);
207
+ }
208
+ /**
209
+ * A method that returns the beginning of astronomical twilight using a zenith of {@link #ASTRONOMICAL_ZENITH
210
+ * 108&deg;}.
211
+ *
212
+ * @return The <code>Date</code> of the beginning of astronomical twilight using a zenith of 108&deg;. If the
213
+ * calculation can't be computed, null will be returned. See detailed explanation on top of the page.
214
+ * @see #ASTRONOMICAL_ZENITH
215
+ */
216
+ getBeginAstronomicalTwilight() {
217
+ return this.getSunriseOffsetByDegrees(NOAACalculator.ASTRONOMICAL_ZENITH);
218
+ }
219
+ /**
220
+ * The getSunset method Returns a <code>Date</code> representing the
221
+ * {@link AstronomicalCalculator#getElevationAdjustment(double) elevation adjusted} sunset time. The zenith used for
222
+ * the calculation uses {@link #GEOMETRIC_ZENITH geometric zenith} of 90&deg; plus
223
+ * {@link AstronomicalCalculator#getElevationAdjustment(double)}. This is adjusted by the
224
+ * {@link AstronomicalCalculator} to add approximately 50/60 of a degree to account for 34 archminutes of refraction
225
+ * and 16 archminutes for the sun's radius for a total of {@link AstronomicalCalculator#adjustZenith 90.83333&deg;}.
226
+ * See documentation for the specific implementation of the {@link AstronomicalCalculator} that you are using. Note:
227
+ * In certain cases the calculates sunset will occur before sunrise. This will typically happen when a timezone
228
+ * other than the local timezone is used (calculating Los Angeles sunset using a GMT timezone for example). In this
229
+ * case the sunset date will be incremented to the following date.
230
+ *
231
+ * @return the <code>Date</code> representing the exact sunset time. If the calculation can't be computed such as in
232
+ * the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it
233
+ * does not set, a null will be returned. See detailed explanation on top of the page.
234
+ * @see AstronomicalCalculator#adjustZenith
235
+ * @see #getSeaLevelSunset()
236
+ * @see AstronomicalCalendar#getUTCSunset
237
+ */
238
+ getSunset() {
239
+ const sunset = this.getUTCSunset0(NOAACalculator.GEOMETRIC_ZENITH);
240
+ if (Number.isNaN(sunset))
241
+ return null;
242
+ return this.getDateFromTime(sunset, false);
243
+ }
244
+ /**
245
+ * A method that returns the sunset without {@link AstronomicalCalculator#getElevationAdjustment(double) elevation
246
+ * adjustment}. Non-sunrise and sunset calculations such as dawn and dusk, depend on the amount of visible light,
247
+ * something that is not affected by elevation. This method returns sunset calculated at sea level. This forms the
248
+ * base for dusk calculations that are calculated as a dip below the horizon after sunset.
249
+ *
250
+ * @return the <code>Date</code> representing the exact sea-level sunset time. If the calculation can't be computed
251
+ * such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one
252
+ * where it does not set, a null will be returned. See detailed explanation on top of the page.
253
+ * @see AstronomicalCalendar#getSunset
254
+ * @see AstronomicalCalendar#getUTCSeaLevelSunset 2see {@link #getSunset()}
255
+ */
256
+ getSeaLevelSunset() {
257
+ const sunset = this.getUTCSeaLevelSunset(NOAACalculator.GEOMETRIC_ZENITH);
258
+ if (Number.isNaN(sunset))
259
+ return null;
260
+ return this.getDateFromTime(sunset, false);
261
+ }
262
+ /**
263
+ * A method that returns the end of civil twilight using a zenith of {@link #CIVIL_ZENITH 96&deg;}.
264
+ *
265
+ * @return The <code>Date</code> of the end of civil twilight using a zenith of {@link #CIVIL_ZENITH 96&deg;}. If
266
+ * the calculation can't be computed, null will be returned. See detailed explanation on top of the page.
267
+ * @see #CIVIL_ZENITH
268
+ */
269
+ getEndCivilTwilight() {
270
+ return this.getSunsetOffsetByDegrees(NOAACalculator.CIVIL_ZENITH);
271
+ }
272
+ /**
273
+ * A method that returns the end of nautical twilight using a zenith of {@link #NAUTICAL_ZENITH 102&deg;}.
274
+ *
275
+ * @return The <code>Date</code> of the end of nautical twilight using a zenith of {@link #NAUTICAL_ZENITH 102&deg;}
276
+ * . If the calculation can't be computed, null will be returned. See detailed explanation on top of the
277
+ * page.
278
+ * @see #NAUTICAL_ZENITH
279
+ */
280
+ getEndNauticalTwilight() {
281
+ return this.getSunsetOffsetByDegrees(NOAACalculator.NAUTICAL_ZENITH);
282
+ }
283
+ /**
284
+ * A method that returns the end of astronomical twilight using a zenith of {@link #ASTRONOMICAL_ZENITH 108&deg;}.
285
+ *
286
+ * @return the <code>Date</code> of the end of astronomical twilight using a zenith of {@link #ASTRONOMICAL_ZENITH
287
+ * 108&deg;}. If the calculation can't be computed, null will be returned. See detailed explanation on top
288
+ * of the page.
289
+ * @see #ASTRONOMICAL_ZENITH
290
+ */
291
+ getEndAstronomicalTwilight() {
292
+ return this.getSunsetOffsetByDegrees(NOAACalculator.ASTRONOMICAL_ZENITH);
293
+ }
294
+ /**
295
+ * A utility method that returns a date offset by the offset time passed in. Please note that the level of light
296
+ * during twilight is not affected by elevation, so if this is being used to calculate an offset before sunrise or
297
+ * after sunset with the intent of getting a rough "level of light" calculation, the sunrise or sunset time passed
298
+ * to this method should be sea level sunrise and sunset.
299
+ *
300
+ * @param time
301
+ * the start time
302
+ * @param offset
303
+ * the offset in milliseconds to add to the time.
304
+ * @return the {@link java.util.Date} with the offset in milliseconds added to it
305
+ */
306
+ static getTimeOffset(time, offset) {
307
+ if (time === null || offset === Long_MIN_VALUE || Number.isNaN(offset)) {
308
+ return null;
309
+ }
310
+ return time.add({ milliseconds: offset });
311
+ }
312
+ /**
313
+ * A utility method that returns the time of an offset by degrees below or above the horizon of
314
+ * {@link #getSunrise() sunrise}. Note that the degree offset is from the vertical, so for a calculation of 14&deg;
315
+ * before sunrise, an offset of 14 + {@link #GEOMETRIC_ZENITH} = 104 would have to be passed as a parameter.
316
+ *
317
+ * @param offsetZenith
318
+ * the degrees before {@link #getSunrise()} to use in the calculation. For time after sunrise use
319
+ * negative numbers. Note that the degree offset is from the vertical, so for a calculation of 14&deg;
320
+ * before sunrise, an offset of 14 + {@link #GEOMETRIC_ZENITH} = 104 would have to be passed as a
321
+ * parameter.
322
+ * @return The {@link java.util.Date} of the offset after (or before) {@link #getSunrise()}. If the calculation
323
+ * can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does
324
+ * not rise, and one where it does not set, a null will be returned. See detailed explanation on top of the
325
+ * page.
326
+ */
327
+ getSunriseOffsetByDegrees(offsetZenith) {
328
+ const dawn = this.getUTCSunrise0(offsetZenith);
329
+ if (Number.isNaN(dawn))
330
+ return null;
331
+ return this.getDateFromTime(dawn, true);
332
+ }
333
+ /**
334
+ * A utility method that returns the time of an offset by degrees below or above the horizon of {@link #getSunset()
335
+ * sunset}. Note that the degree offset is from the vertical, so for a calculation of 14&deg; after sunset, an
336
+ * offset of 14 + {@link #GEOMETRIC_ZENITH} = 104 would have to be passed as a parameter.
337
+ *
338
+ * @param offsetZenith
339
+ * the degrees after {@link #getSunset()} to use in the calculation. For time before sunset use negative
340
+ * numbers. Note that the degree offset is from the vertical, so for a calculation of 14&deg; after
341
+ * sunset, an offset of 14 + {@link #GEOMETRIC_ZENITH} = 104 would have to be passed as a parameter.
342
+ * @return The {@link java.util.Date}of the offset after (or before) {@link #getSunset()}. If the calculation can't
343
+ * be computed such as in the Arctic Circle where there is at least one day a year where the sun does not
344
+ * rise, and one where it does not set, a null will be returned. See detailed explanation on top of the
345
+ * page.
346
+ */
347
+ getSunsetOffsetByDegrees(offsetZenith) {
348
+ const sunset = this.getUTCSunset0(offsetZenith);
349
+ if (Number.isNaN(sunset))
350
+ return null;
351
+ return this.getDateFromTime(sunset, false);
352
+ }
353
+ /**
354
+ * A constructor that takes in <a href="http://en.wikipedia.org/wiki/Geolocation">geolocation</a> information as a
355
+ * parameter. The default {@link AstronomicalCalculator#getDefault() AstronomicalCalculator} used for solar
356
+ * calculations is the the {@link NOAACalculator}.
357
+ *
358
+ * @param geoLocation
359
+ * The location information used for calculating astronomical sun times.
360
+ *
361
+ * @see #setAstronomicalCalculator(AstronomicalCalculator) for changing the calculator class.
362
+ */
363
+ constructor(geoLocation, date) {
364
+ this.date = date;
365
+ this.geoLocation = geoLocation;
366
+ }
367
+ /**
368
+ * A method that returns the sunrise in UTC time without correction for time zone offset from GMT and without using
369
+ * daylight savings time.
370
+ *
371
+ * @param zenith
372
+ * the degrees below the horizon. For time after sunrise use negative numbers.
373
+ * @return The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the
374
+ * Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does
375
+ * not set, {@link Double#NaN} will be returned. See detailed explanation on top of the page.
376
+ */
377
+ getUTCSunrise0(zenith) {
378
+ return this.getUTCSunrise(this.getAdjustedDate(), this.geoLocation, zenith, true);
379
+ }
380
+ /**
381
+ * A method that returns the sunrise in UTC time without correction for time zone offset from GMT and without using
382
+ * daylight savings time. Non-sunrise and sunset calculations such as dawn and dusk, depend on the amount of visible
383
+ * light, something that is not affected by elevation. This method returns UTC sunrise calculated at sea level. This
384
+ * forms the base for dawn calculations that are calculated as a dip below the horizon before sunrise.
385
+ *
386
+ * @param zenith
387
+ * the degrees below the horizon. For time after sunrise use negative numbers.
388
+ * @return The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the
389
+ * Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does
390
+ * not set, {@link Double#NaN} will be returned. See detailed explanation on top of the page.
391
+ * @see AstronomicalCalendar#getUTCSunrise
392
+ * @see AstronomicalCalendar#getUTCSeaLevelSunset
393
+ */
394
+ getUTCSeaLevelSunrise(zenith) {
395
+ return this.getUTCSunrise(this.getAdjustedDate(), this.geoLocation, zenith, false);
396
+ }
397
+ /**
398
+ * A method that returns the sunset in UTC time without correction for time zone offset from GMT and without using
399
+ * daylight savings time.
400
+ *
401
+ * @param zenith
402
+ * the degrees below the horizon. For time after sunset use negative numbers.
403
+ * @return The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the
404
+ * Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does
405
+ * not set, {@link Double#NaN} will be returned. See detailed explanation on top of the page.
406
+ * @see AstronomicalCalendar#getUTCSeaLevelSunset
407
+ */
408
+ getUTCSunset0(zenith) {
409
+ return this.getUTCSunset(this.getAdjustedDate(), this.geoLocation, zenith, true);
410
+ }
411
+ /**
412
+ * A method that returns the sunset in UTC time without correction for elevation, time zone offset from GMT and
413
+ * without using daylight savings time. Non-sunrise and sunset calculations such as dawn and dusk, depend on the
414
+ * amount of visible light, something that is not affected by elevation. This method returns UTC sunset calculated
415
+ * at sea level. This forms the base for dusk calculations that are calculated as a dip below the horizon after
416
+ * sunset.
417
+ *
418
+ * @param zenith
419
+ * the degrees below the horizon. For time before sunset use negative numbers.
420
+ * @return The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the
421
+ * Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does
422
+ * not set, {@link Double#NaN} will be returned. See detailed explanation on top of the page.
423
+ * @see AstronomicalCalendar#getUTCSunset
424
+ * @see AstronomicalCalendar#getUTCSeaLevelSunrise
425
+ */
426
+ getUTCSeaLevelSunset(zenith) {
427
+ return this.getUTCSunset(this.getAdjustedDate(), this.geoLocation, zenith, false);
428
+ }
429
+ /**
430
+ * Adjusts the <code>Calendar</code> to deal with edge cases where the location crosses the antimeridian.
431
+ * @private
432
+ * @see GeoLocation#getAntimeridianAdjustment()
433
+ * @return the adjusted Calendar
434
+ */
435
+ getAdjustedDate() {
436
+ return this.date;
437
+ }
438
+ /**
439
+ * Method to return the adjustment to the zenith required to account for the elevation. Since a person at a higher
440
+ * elevation can see farther below the horizon, the calculation for sunrise / sunset is calculated below the horizon
441
+ * used at sea level. This is only used for sunrise and sunset and not times before or after it such as
442
+ * {@link AstronomicalCalendar#getBeginNauticalTwilight() nautical twilight} since those
443
+ * calculations are based on the level of available light at the given dip below the horizon, something that is not
444
+ * affected by elevation, the adjustment should only made if the zenith == 90&deg; {@link #adjustZenith adjusted}
445
+ * for refraction and solar radius. The algorithm used is
446
+ *
447
+ * <pre>
448
+ * elevationAdjustment = Math.toDegrees(Math.acos(earthRadiusInMeters / (earthRadiusInMeters + elevationMeters)));
449
+ * </pre>
450
+ *
451
+ * The source of this algorithm is <a href="http://www.calendarists.com">Calendrical Calculations</a> by Edward M.
452
+ * Reingold and Nachum Dershowitz. An alternate algorithm that produces an almost identical (but not accurate)
453
+ * result found in Ma'aglay Tzedek by Moishe Kosower and other sources is:
454
+ *
455
+ * <pre>
456
+ * elevationAdjustment = 0.0347 * Math.sqrt(elevationMeters);
457
+ * </pre>
458
+ *
459
+ * @param elevation
460
+ * elevation in Meters.
461
+ * @return the adjusted zenith
462
+ */
463
+ getElevationAdjustment(elevation) {
464
+ // double elevationAdjustment = 0.0347 * Math.sqrt(elevation);
465
+ const elevationAdjustment = radiansToDegrees(Math.acos(earthRadius / (earthRadius + elevation / 1000)));
466
+ return elevationAdjustment;
467
+ }
468
+ /**
469
+ * Adjusts the zenith of astronomical sunrise and sunset to account for solar refraction, solar radius and
470
+ * elevation. The value for Sun's zenith and true rise/set Zenith (used in this class and subclasses) is the angle
471
+ * that the center of the Sun makes to a line perpendicular to the Earth's surface. If the Sun were a point and the
472
+ * Earth were without an atmosphere, true sunset and sunrise would correspond to a 90&deg; zenith. Because the Sun
473
+ * is not a point, and because the atmosphere refracts light, this 90&deg; zenith does not, in fact, correspond to
474
+ * true sunset or sunrise, instead the centre of the Sun's disk must lie just below the horizon for the upper edge
475
+ * to be obscured. This means that a zenith of just above 90&deg; must be used. The Sun subtends an angle of 16
476
+ * minutes of arc (this can be changed via the {@link #setSolarRadius(double)} method , and atmospheric refraction
477
+ * accounts for 34 minutes or so (this can be changed via the {@link #setRefraction(double)} method), giving a total
478
+ * of 50 arcminutes. The total value for ZENITH is 90+(5/6) or 90.8333333&deg; for true sunrise/sunset. Since a
479
+ * person at an elevation can see blow the horizon of a person at sea level, this will also adjust the zenith to
480
+ * account for elevation if available. Note that this will only adjust the value if the zenith is exactly 90 degrees.
481
+ * For values below and above this no correction is done. As an example, astronomical twilight is when the sun is
482
+ * 18&deg; below the horizon or {@link AstronomicalCalendar#ASTRONOMICAL_ZENITH 108&deg;
483
+ * below the zenith}. This is traditionally calculated with none of the above mentioned adjustments. The same goes
484
+ * for various <em>tzais</em> and <em>alos</em> times such as the
485
+ * {@link ZmanimCalendar#ZENITH_16_POINT_1 16.1&deg;} dip used in
486
+ * {@link ComplexZmanimCalendar#getAlos16Point1Degrees()}.
487
+ *
488
+ * @param zenith
489
+ * the azimuth below the vertical zenith of 90&deg;. For sunset typically the {@link #adjustZenith
490
+ * zenith} used for the calculation uses geometric zenith of 90&deg; and {@link #adjustZenith adjusts}
491
+ * this slightly to account for solar refraction and the sun's radius. Another example would be
492
+ * {@link AstronomicalCalendar#getEndNauticalTwilight()} that passes
493
+ * {@link AstronomicalCalendar#NAUTICAL_ZENITH} to this method.
494
+ * @param elevation
495
+ * elevation in Meters.
496
+ * @return The zenith adjusted to include the {@link #getSolarRadius sun's radius}, {@link #getRefraction
497
+ * refraction} and {@link #getElevationAdjustment elevation} adjustment. This will only be adjusted for
498
+ * sunrise and sunset (if the zenith == 90&deg;)
499
+ * @see #getElevationAdjustment(double)
500
+ */
501
+ adjustZenith(zenith, elevation) {
502
+ let adjustedZenith = zenith;
503
+ if (zenith === NOAACalculator.GEOMETRIC_ZENITH) {
504
+ // only adjust if it is exactly sunrise or sunset
505
+ adjustedZenith =
506
+ zenith +
507
+ (solarRadius + refraction + this.getElevationAdjustment(elevation));
508
+ }
509
+ return adjustedZenith;
510
+ }
511
+ /**
512
+ * @see AstronomicalCalculator#getUTCSunrise(Calendar, GeoLocation, double, boolean)
513
+ */
514
+ getUTCSunrise(date, geoLocation, zenith, adjustForElevation) {
515
+ const elevation = adjustForElevation
516
+ ? geoLocation.getElevation()
517
+ : 0;
518
+ const adjustedZenith = this.adjustZenith(zenith, elevation);
519
+ let sunrise = NOAACalculator.getSunriseUTC(NOAACalculator.getJulianDay(date), geoLocation.getLatitude(), -geoLocation.getLongitude(), adjustedZenith);
520
+ sunrise = sunrise / 60;
521
+ // ensure that the time is >= 0 and < 24
522
+ while (sunrise < 0) {
523
+ sunrise += 24;
524
+ }
525
+ while (sunrise >= 24) {
526
+ sunrise -= 24;
527
+ }
528
+ return sunrise;
529
+ }
530
+ /**
531
+ * @see AstronomicalCalculator#getUTCSunset(Calendar, GeoLocation, double, boolean)
532
+ */
533
+ getUTCSunset(date, geoLocation, zenith, adjustForElevation) {
534
+ const elevation = adjustForElevation
535
+ ? geoLocation.getElevation()
536
+ : 0;
537
+ const adjustedZenith = this.adjustZenith(zenith, elevation);
538
+ let sunset = NOAACalculator.getSunsetUTC(NOAACalculator.getJulianDay(date), geoLocation.getLatitude(), -geoLocation.getLongitude(), adjustedZenith);
539
+ sunset = sunset / 60;
540
+ // ensure that the time is >= 0 and < 24
541
+ while (sunset < 0) {
542
+ sunset += 24;
543
+ }
544
+ while (sunset >= 24) {
545
+ sunset -= 24;
546
+ }
547
+ return sunset;
548
+ }
549
+ /**
550
+ * A utility method that will allow the calculation of a temporal (solar) hour based on the sunrise and sunset
551
+ * passed as parameters to this method. An example of the use of this method would be the calculation of a
552
+ * non-elevation adjusted temporal hour by passing in {@link #getSeaLevelSunrise() sea level sunrise} and
553
+ * {@link #getSeaLevelSunset() sea level sunset} as parameters.
554
+ *
555
+ * @param startOfday
556
+ * The start of the day.
557
+ * @param endOfDay
558
+ * The end of the day.
559
+ *
560
+ * @return the <code>long</code> millisecond length of the temporal hour. If the calculation can't be computed a
561
+ * {@link Long#MIN_VALUE} will be returned. See detailed explanation on top of the page.
562
+ *
563
+ * @see #getTemporalHour()
564
+ */
565
+ getTemporalHour(startOfday = this.getSeaLevelSunrise(), endOfDay = this.getSeaLevelSunset()) {
566
+ if (startOfday === null || endOfDay === null) {
567
+ return Long_MIN_VALUE;
568
+ }
569
+ const delta = endOfDay.epochMilliseconds - startOfday.epochMilliseconds;
570
+ return Math.floor(delta / 12);
571
+ }
572
+ /**
573
+ * A method that returns sundial or solar noon. It occurs when the Sun is <a href
574
+ * ="http://en.wikipedia.org/wiki/Transit_%28astronomy%29">transiting</a> the <a
575
+ * href="http://en.wikipedia.org/wiki/Meridian_%28astronomy%29">celestial meridian</a>. In this class it is
576
+ * calculated as halfway between the sunrise and sunset passed to this method. This time can be slightly off the
577
+ * real transit time due to changes in declination (the lengthening or shortening day).
578
+ *
579
+ * @param startOfDay
580
+ * the start of day for calculating the sun's transit. This can be sea level sunrise, visual sunrise (or
581
+ * any arbitrary start of day) passed to this method.
582
+ * @param endOfDay
583
+ * the end of day for calculating the sun's transit. This can be sea level sunset, visual sunset (or any
584
+ * arbitrary end of day) passed to this method.
585
+ *
586
+ * @return the <code>Date</code> representing Sun's transit. If the calculation can't be computed such as in the
587
+ * Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does
588
+ * not set, null will be returned. See detailed explanation on top of the page.
589
+ */
590
+ getSunTransit(startOfDay = this.getSeaLevelSunrise(), endOfDay = this.getSeaLevelSunset()) {
591
+ const temporalHour = this.getTemporalHour(startOfDay, endOfDay);
592
+ return NOAACalculator.getTimeOffset(startOfDay, temporalHour * 6);
593
+ }
594
+ /**
595
+ * A method that returns a <code>Date</code> from the time passed in as a parameter.
596
+ * @protected
597
+ * @param time
598
+ * The time to be set as the time for the <code>Date</code>. The time expected is in the format: 18.75
599
+ * for 6:45:00 PM.
600
+ * @param isSunrise true if the time is sunrise, and false if it is sunset
601
+ * @return The Date.
602
+ */
603
+ getDateFromTime(time, isSunrise) {
604
+ if (Number.isNaN(time)) {
605
+ return null;
606
+ }
607
+ let calculatedTime = time;
608
+ let cal = this.getAdjustedDate();
609
+ // let cal = new Temporal.PlainDate(adj.year, adj.month, adj.day);
610
+ const hours = Math.trunc(calculatedTime); // retain only the hours
611
+ calculatedTime -= hours;
612
+ const minutes = Math.trunc((calculatedTime *= 60)); // retain only the minutes
613
+ calculatedTime -= minutes;
614
+ const seconds = Math.trunc((calculatedTime *= 60)); // retain only the seconds
615
+ calculatedTime -= seconds; // remaining milliseconds
616
+ // Check if a date transition has occurred, or is about to occur - this indicates the date of the event is
617
+ // actually not the target date, but the day prior or after
618
+ const localTimeHours = Math.trunc(this.geoLocation.getLongitude() / 15);
619
+ if (isSunrise && localTimeHours + hours > 18) {
620
+ cal = cal.add({ days: -1 });
621
+ // cal = cal.minus({days: 1});
622
+ }
623
+ else if (!isSunrise && localTimeHours + hours < 6) {
624
+ cal = cal.add({ days: 1 });
625
+ }
626
+ return cal
627
+ .toZonedDateTime({
628
+ timeZone: 'UTC',
629
+ plainTime: new Temporal.PlainTime(hours, minutes, seconds, Math.trunc(calculatedTime * 1000)),
630
+ })
631
+ .withTimeZone(this.geoLocation.getTimeZone());
632
+ }
633
+ /**
634
+ * Return the <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> from a Java Calendar
635
+ * @private
636
+ * @param calendar
637
+ * The Java Calendar
638
+ * @return the Julian day corresponding to the date Note: Number is returned for start of day. Fractional days
639
+ * should be added later.
640
+ */
641
+ static getJulianDay(date) {
642
+ let { year, month } = date;
643
+ const { day } = date;
644
+ if (month <= 2) {
645
+ year -= 1;
646
+ month += 12;
647
+ }
648
+ const a = Math.trunc(year / 100);
649
+ const b = Math.trunc(2 - a + a / 4);
650
+ return (Math.floor(365.25 * (year + 4716)) +
651
+ Math.floor(30.6001 * (month + 1)) +
652
+ day +
653
+ b -
654
+ 1524.5);
655
+ }
656
+ /**
657
+ * Convert <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> to centuries since J2000.0.
658
+ * @private
659
+ * @param julianDay
660
+ * the Julian Day to convert
661
+ * @return the centuries since 2000 Julian corresponding to the Julian Day
662
+ */
663
+ static getJulianCenturiesFromJulianDay(julianDay) {
664
+ return ((julianDay - NOAACalculator.JULIAN_DAY_JAN_1_2000) /
665
+ NOAACalculator.JULIAN_DAYS_PER_CENTURY);
666
+ }
667
+ /**
668
+ * Convert centuries since J2000.0 to <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a>.
669
+ * @private
670
+ * @param julianCenturies
671
+ * the number of Julian centuries since J2000.0
672
+ * @return the Julian Day corresponding to the Julian centuries passed in
673
+ */
674
+ static getJulianDayFromJulianCenturies(julianCenturies) {
675
+ return (julianCenturies * NOAACalculator.JULIAN_DAYS_PER_CENTURY +
676
+ NOAACalculator.JULIAN_DAY_JAN_1_2000);
677
+ }
678
+ /**
679
+ * Returns the Geometric <a href="http://en.wikipedia.org/wiki/Mean_longitude">Mean Longitude</a> of the Sun.
680
+ * @private
681
+ * @param julianCenturies
682
+ * the number of Julian centuries since J2000.0
683
+ * @return the Geometric Mean Longitude of the Sun in degrees
684
+ */
685
+ static getSunGeometricMeanLongitude(julianCenturies) {
686
+ let longitude = 280.46646 + julianCenturies * (36000.76983 + 0.0003032 * julianCenturies);
687
+ while (longitude > 360) {
688
+ longitude -= 360;
689
+ }
690
+ while (longitude < 0) {
691
+ longitude += 360;
692
+ }
693
+ return longitude; // in degrees
694
+ }
695
+ /**
696
+ * Returns the Geometric <a href="http://en.wikipedia.org/wiki/Mean_anomaly">Mean Anomaly</a> of the Sun.
697
+ * @private
698
+ * @param julianCenturies
699
+ * the number of Julian centuries since J2000.0
700
+ * @return the Geometric Mean Anomaly of the Sun in degrees
701
+ */
702
+ static getSunGeometricMeanAnomaly(julianCenturies) {
703
+ return (357.52911 + julianCenturies * (35999.05029 - 0.0001537 * julianCenturies)); // in degrees
704
+ }
705
+ /**
706
+ * Return the <a href="http://en.wikipedia.org/wiki/Eccentricity_%28orbit%29">eccentricity of earth's orbit</a>.
707
+ * @private
708
+ * @param julianCenturies
709
+ * the number of Julian centuries since J2000.0
710
+ * @return the unitless eccentricity
711
+ */
712
+ static getEarthOrbitEccentricity(julianCenturies) {
713
+ return (0.016708634 -
714
+ julianCenturies * (0.000042037 + 0.0000001267 * julianCenturies)); // unitless
715
+ }
716
+ /**
717
+ * Returns the <a href="http://en.wikipedia.org/wiki/Equation_of_the_center">equation of center</a> for the sun.
718
+ * @private
719
+ * @param julianCenturies
720
+ * the number of Julian centuries since J2000.0
721
+ * @return the equation of center for the sun in degrees
722
+ */
723
+ static getSunEquationOfCenter(julianCenturies) {
724
+ const m = NOAACalculator.getSunGeometricMeanAnomaly(julianCenturies);
725
+ const mrad = degreesToRadians(m);
726
+ const sinm = Math.sin(mrad);
727
+ const sin2m = Math.sin(mrad + mrad);
728
+ const sin3m = Math.sin(mrad + mrad + mrad);
729
+ return (sinm *
730
+ (1.914602 - julianCenturies * (0.004817 + 0.000014 * julianCenturies)) +
731
+ sin2m * (0.019993 - 0.000101 * julianCenturies) +
732
+ sin3m * 0.000289); // in degrees
733
+ }
734
+ /**
735
+ * Return the true longitude of the sun
736
+ * @private
737
+ * @param julianCenturies
738
+ * the number of Julian centuries since J2000.0
739
+ * @return the sun's true longitude in degrees
740
+ */
741
+ static getSunTrueLongitude(julianCenturies) {
742
+ const sunLongitude = NOAACalculator.getSunGeometricMeanLongitude(julianCenturies);
743
+ const center = NOAACalculator.getSunEquationOfCenter(julianCenturies);
744
+ return sunLongitude + center; // in degrees
745
+ }
746
+ /**
747
+ * Return the apparent longitude of the sun
748
+ * @private
749
+ * @param julianCenturies
750
+ * the number of Julian centuries since J2000.0
751
+ * @return sun's apparent longitude in degrees
752
+ */
753
+ static getSunApparentLongitude(julianCenturies) {
754
+ const sunTrueLongitude = NOAACalculator.getSunTrueLongitude(julianCenturies);
755
+ const omega = 125.04 - 1934.136 * julianCenturies;
756
+ const lambda = sunTrueLongitude - 0.00569 - 0.00478 * Math.sin(degreesToRadians(omega));
757
+ return lambda; // in degrees
758
+ }
759
+ /**
760
+ * Returns the mean <a href="http://en.wikipedia.org/wiki/Axial_tilt">obliquity of the ecliptic</a> (Axial tilt).
761
+ * @private
762
+ * @param julianCenturies
763
+ * the number of Julian centuries since J2000.0
764
+ * @return the mean obliquity in degrees
765
+ */
766
+ static getMeanObliquityOfEcliptic(julianCenturies) {
767
+ const seconds = 21.448 -
768
+ julianCenturies *
769
+ (46.815 + julianCenturies * (0.00059 - julianCenturies * 0.001813));
770
+ return 23 + (26 + seconds / 60) / 60; // in degrees
771
+ }
772
+ /**
773
+ * Returns the corrected <a href="http://en.wikipedia.org/wiki/Axial_tilt">obliquity of the ecliptic</a> (Axial
774
+ * tilt).
775
+ * @private
776
+ * @param julianCenturies
777
+ * the number of Julian centuries since J2000.0
778
+ * @return the corrected obliquity in degrees
779
+ */
780
+ static getObliquityCorrection(julianCenturies) {
781
+ const obliquityOfEcliptic = NOAACalculator.getMeanObliquityOfEcliptic(julianCenturies);
782
+ const omega = 125.04 - 1934.136 * julianCenturies;
783
+ return obliquityOfEcliptic + 0.00256 * Math.cos(degreesToRadians(omega)); // in degrees
784
+ }
785
+ /**
786
+ * Return the <a href="http://en.wikipedia.org/wiki/Declination">declination</a> of the sun.
787
+ * @private
788
+ * @param julianCenturies
789
+ * the number of Julian centuries since J2000.0
790
+ * @return
791
+ * the sun's declination in degrees
792
+ */
793
+ static getSunDeclination(julianCenturies) {
794
+ const obliquityCorrection = NOAACalculator.getObliquityCorrection(julianCenturies);
795
+ const lambda = NOAACalculator.getSunApparentLongitude(julianCenturies);
796
+ const sint = Math.sin(degreesToRadians(obliquityCorrection)) *
797
+ Math.sin(degreesToRadians(lambda));
798
+ const theta = radiansToDegrees(Math.asin(sint));
799
+ return theta; // in degrees
800
+ }
801
+ /**
802
+ * Return the <a href="http://en.wikipedia.org/wiki/Equation_of_time">Equation of Time</a> - the difference between
803
+ * true solar time and mean solar time
804
+ * @private
805
+ * @param julianCenturies
806
+ * the number of Julian centuries since J2000.0
807
+ * @return equation of time in minutes of time
808
+ */
809
+ static getEquationOfTime(julianCenturies) {
810
+ const epsilon = NOAACalculator.getObliquityCorrection(julianCenturies);
811
+ const geomMeanLongSun = NOAACalculator.getSunGeometricMeanLongitude(julianCenturies);
812
+ const eccentricityEarthOrbit = NOAACalculator.getEarthOrbitEccentricity(julianCenturies);
813
+ const geomMeanAnomalySun = NOAACalculator.getSunGeometricMeanAnomaly(julianCenturies);
814
+ let y = Math.tan(degreesToRadians(epsilon) / 2);
815
+ y *= y;
816
+ const sin2l0 = Math.sin(2 * degreesToRadians(geomMeanLongSun));
817
+ const sinm = Math.sin(degreesToRadians(geomMeanAnomalySun));
818
+ const cos2l0 = Math.cos(2 * degreesToRadians(geomMeanLongSun));
819
+ const sin4l0 = Math.sin(4 * degreesToRadians(geomMeanLongSun));
820
+ const sin2m = Math.sin(2 * degreesToRadians(geomMeanAnomalySun));
821
+ const equationOfTime = y * sin2l0 -
822
+ 2 * eccentricityEarthOrbit * sinm +
823
+ 4 * eccentricityEarthOrbit * y * sinm * cos2l0 -
824
+ 0.5 * y * y * sin4l0 -
825
+ 1.25 * eccentricityEarthOrbit * eccentricityEarthOrbit * sin2m;
826
+ return radiansToDegrees(equationOfTime) * 4; // in minutes of time
827
+ }
828
+ /**
829
+ * Return the <a href="http://en.wikipedia.org/wiki/Hour_angle">hour angle</a> of the sun at sunrise for the
830
+ * latitude.
831
+ * @private
832
+ * @param lat
833
+ * , the latitude of observer in degrees
834
+ * @param solarDec
835
+ * the declination angle of sun in degrees
836
+ * @param zenith
837
+ * the zenith
838
+ * @return hour angle of sunrise in radians
839
+ */
840
+ static getSunHourAngleAtSunrise(lat, solarDec, zenith) {
841
+ const latRad = degreesToRadians(lat);
842
+ const sdRad = degreesToRadians(solarDec);
843
+ return Math.acos(Math.cos(degreesToRadians(zenith)) /
844
+ (Math.cos(latRad) * Math.cos(sdRad)) -
845
+ Math.tan(latRad) * Math.tan(sdRad)); // in radians
846
+ }
847
+ /**
848
+ * Returns the <a href="http://en.wikipedia.org/wiki/Hour_angle">hour angle</a> of the sun at sunset for the
849
+ * latitude. TODO: use - {@link #getSunHourAngleAtSunrise(double, double, double)} implementation to avoid
850
+ * duplication of code.
851
+ * @private
852
+ * @param lat
853
+ * the latitude of observer in degrees
854
+ * @param solarDec
855
+ * the declination angle of sun in degrees
856
+ * @param zenith
857
+ * the zenith
858
+ * @return the hour angle of sunset in radians
859
+ */
860
+ static getSunHourAngleAtSunset(lat, solarDec, zenith) {
861
+ const latRad = degreesToRadians(lat);
862
+ const sdRad = degreesToRadians(solarDec);
863
+ const hourAngle = Math.acos(Math.cos(degreesToRadians(zenith)) /
864
+ (Math.cos(latRad) * Math.cos(sdRad)) -
865
+ Math.tan(latRad) * Math.tan(sdRad));
866
+ return -hourAngle; // in radians
867
+ }
868
+ /**
869
+ * Return the <a href="http://en.wikipedia.org/wiki/Celestial_coordinate_system">Solar Elevation</a> for the
870
+ * horizontal coordinate system at the given location at the given time. Can be negative if the sun is below the
871
+ * horizon. Not corrected for altitude.
872
+ *
873
+ * @param cal
874
+ * time of calculation
875
+ * @param lat
876
+ * latitude of location for calculation
877
+ * @param lon
878
+ * longitude of location for calculation
879
+ * @return solar elevation in degrees - horizon is 0 degrees, civil twilight is -6 degrees
880
+ */
881
+ static getSolarElevation(date, lat, lon) {
882
+ const julianDay = NOAACalculator.getJulianDay(date.toPlainDate());
883
+ const julianCenturies = NOAACalculator.getJulianCenturiesFromJulianDay(julianDay);
884
+ const equationOfTime = NOAACalculator.getEquationOfTime(julianCenturies);
885
+ let longitude = date.hour + 12 + (date.minute + equationOfTime + date.second / 60) / 60;
886
+ longitude = -((longitude * 360) / 24) % 360;
887
+ const hourAngleRad = degreesToRadians(lon - longitude);
888
+ const declination = NOAACalculator.getSunDeclination(julianCenturies);
889
+ const decRad = degreesToRadians(declination);
890
+ const latRad = degreesToRadians(lat);
891
+ return radiansToDegrees(Math.asin(Math.sin(latRad) * Math.sin(decRad) +
892
+ Math.cos(latRad) * Math.cos(decRad) * Math.cos(hourAngleRad)));
893
+ }
894
+ /**
895
+ * Return the <a href="http://en.wikipedia.org/wiki/Celestial_coordinate_system">Solar Azimuth</a> for the
896
+ * horizontal coordinate system at the given location at the given time. Not corrected for altitude. True south is 0
897
+ * degrees.
898
+ *
899
+ * @param cal
900
+ * time of calculation
901
+ * @param latitude
902
+ * latitude of location for calculation
903
+ * @param lon
904
+ * longitude of location for calculation
905
+ * @return FIXME
906
+ */
907
+ static getSolarAzimuth(date, latitude, lon) {
908
+ const julianDay = NOAACalculator.getJulianDay(date.toPlainDate());
909
+ const julianCenturies = NOAACalculator.getJulianCenturiesFromJulianDay(julianDay);
910
+ const equationOfTime = NOAACalculator.getEquationOfTime(julianCenturies);
911
+ let longitude = date.hour + 12 + (date.minute + equationOfTime + date.second / 60) / 60;
912
+ longitude = -((longitude * 360) / 24) % 360;
913
+ const hourAngleRad = degreesToRadians(lon - longitude);
914
+ const declination = NOAACalculator.getSunDeclination(julianCenturies);
915
+ const decRad = degreesToRadians(declination);
916
+ const latRad = degreesToRadians(latitude);
917
+ return (radiansToDegrees(Math.atan(Math.sin(hourAngleRad) /
918
+ (Math.cos(hourAngleRad) * Math.sin(latRad) -
919
+ Math.tan(decRad) * Math.cos(latRad)))) + 180);
920
+ }
921
+ /**
922
+ * Return the <a href="http://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
923
+ * of sunrise for the given day at the given location on earth
924
+ * @private
925
+ * @param julianDay
926
+ * the Julian day
927
+ * @param latitude
928
+ * the latitude of observer in degrees
929
+ * @param longitude
930
+ * the longitude of observer in degrees
931
+ * @param zenith
932
+ * the zenith
933
+ * @return the time in minutes from zero UTC
934
+ */
935
+ static getSunriseUTC(julianDay, latitude, longitude, zenith) {
936
+ const julianCenturies = NOAACalculator.getJulianCenturiesFromJulianDay(julianDay);
937
+ // Find the time of solar noon at the location, and use that declination. This is better than start of the
938
+ // Julian day
939
+ const noonmin = NOAACalculator.getSolarNoonUTC(julianCenturies, longitude);
940
+ const tnoon = NOAACalculator.getJulianCenturiesFromJulianDay(julianDay + noonmin / 1440);
941
+ // First pass to approximate sunrise (using solar noon)
942
+ let eqTime = NOAACalculator.getEquationOfTime(tnoon);
943
+ let solarDec = NOAACalculator.getSunDeclination(tnoon);
944
+ let hourAngle = NOAACalculator.getSunHourAngleAtSunrise(latitude, solarDec, zenith);
945
+ let delta = longitude - radiansToDegrees(hourAngle);
946
+ let timeDiff = 4 * delta; // in minutes of time
947
+ let timeUTC = 720 + timeDiff - eqTime; // in minutes
948
+ // Second pass includes fractional Julian Day in gamma calc
949
+ const newt = NOAACalculator.getJulianCenturiesFromJulianDay(NOAACalculator.getJulianDayFromJulianCenturies(julianCenturies) +
950
+ timeUTC / 1440);
951
+ eqTime = NOAACalculator.getEquationOfTime(newt);
952
+ solarDec = NOAACalculator.getSunDeclination(newt);
953
+ hourAngle = NOAACalculator.getSunHourAngleAtSunrise(latitude, solarDec, zenith);
954
+ delta = longitude - radiansToDegrees(hourAngle);
955
+ timeDiff = 4 * delta;
956
+ timeUTC = 720 + timeDiff - eqTime; // in minutes
957
+ return timeUTC;
958
+ }
959
+ /**
960
+ * Return the <a href="http://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
961
+ * of <a href="http://en.wikipedia.org/wiki/Noon#Solar_noon">solar noon</a> for the given day at the given location
962
+ * on earth.
963
+ * @private
964
+ * @param julianCenturies
965
+ * the number of Julian centuries since J2000.0
966
+ * @param longitude
967
+ * the longitude of observer in degrees
968
+ * @return the time in minutes from zero UTC
969
+ */
970
+ static getSolarNoonUTC(julianCenturies, longitude) {
971
+ // First pass uses approximate solar noon to calculate eqtime
972
+ const tnoon = NOAACalculator.getJulianCenturiesFromJulianDay(NOAACalculator.getJulianDayFromJulianCenturies(julianCenturies) +
973
+ longitude / 360);
974
+ let eqTime = NOAACalculator.getEquationOfTime(tnoon);
975
+ const solNoonUTC = 720 + longitude * 4 - eqTime; // min
976
+ const newt = NOAACalculator.getJulianCenturiesFromJulianDay(NOAACalculator.getJulianDayFromJulianCenturies(julianCenturies) -
977
+ 0.5 +
978
+ solNoonUTC / 1440);
979
+ eqTime = NOAACalculator.getEquationOfTime(newt);
980
+ return 720 + longitude * 4 - eqTime; // min
981
+ }
982
+ /**
983
+ * Return the <a href="http://en.wikipedia.org/wiki/Universal_Coordinated_Time">Universal Coordinated Time</a> (UTC)
984
+ * of sunset for the given day at the given location on earth
985
+ * @private
986
+ * @param julianDay
987
+ * the Julian day
988
+ * @param latitude
989
+ * the latitude of observer in degrees
990
+ * @param longitude
991
+ * : longitude of observer in degrees
992
+ * @param zenith
993
+ * the zenith
994
+ * @return the time in minutes from zero Universal Coordinated Time (UTC)
995
+ */
996
+ static getSunsetUTC(julianDay, latitude, longitude, zenith) {
997
+ const julianCenturies = NOAACalculator.getJulianCenturiesFromJulianDay(julianDay);
998
+ // Find the time of solar noon at the location, and use that declination. This is better than start of the
999
+ // Julian day
1000
+ const noonmin = NOAACalculator.getSolarNoonUTC(julianCenturies, longitude);
1001
+ const tnoon = NOAACalculator.getJulianCenturiesFromJulianDay(julianDay + noonmin / 1440);
1002
+ // First calculates sunrise and approx length of day
1003
+ let eqTime = NOAACalculator.getEquationOfTime(tnoon);
1004
+ let solarDec = NOAACalculator.getSunDeclination(tnoon);
1005
+ let hourAngle = NOAACalculator.getSunHourAngleAtSunset(latitude, solarDec, zenith);
1006
+ let delta = longitude - radiansToDegrees(hourAngle);
1007
+ let timeDiff = 4 * delta;
1008
+ let timeUTC = 720 + timeDiff - eqTime;
1009
+ // Second pass includes fractional Julian Day in gamma calc
1010
+ const newt = NOAACalculator.getJulianCenturiesFromJulianDay(NOAACalculator.getJulianDayFromJulianCenturies(julianCenturies) +
1011
+ timeUTC / 1440);
1012
+ eqTime = NOAACalculator.getEquationOfTime(newt);
1013
+ solarDec = NOAACalculator.getSunDeclination(newt);
1014
+ hourAngle = NOAACalculator.getSunHourAngleAtSunset(latitude, solarDec, zenith);
1015
+ delta = longitude - radiansToDegrees(hourAngle);
1016
+ timeDiff = 4 * delta;
1017
+ timeUTC = 720 + timeDiff - eqTime; // in minutes
1018
+ return timeUTC;
1019
+ }
1020
+ }
1021
+ /**
1022
+ * The zenith of astronomical sunrise and sunset. The sun is 90&deg; from the vertical 0&deg;
1023
+ * @private
1024
+ */
1025
+ NOAACalculator.GEOMETRIC_ZENITH = 90;
1026
+ /**
1027
+ * Default value for Sun's zenith and true rise/set Zenith (used in this class and subclasses) is the angle that the
1028
+ * center of the Sun makes to a line perpendicular to the Earth's surface. If the Sun were a point and the Earth
1029
+ * were without an atmosphere, true sunset and sunrise would correspond to a 90&deg; zenith. Because the Sun is not
1030
+ * a point, and because the atmosphere refracts light, this 90&deg; zenith does not, in fact, correspond to true
1031
+ * sunset or sunrise, instead the center of the Sun's disk must lie just below the horizon for the upper edge to be
1032
+ * obscured. This means that a zenith of just above 90&deg; must be used. The Sun subtends an angle of 16 minutes of
1033
+ * arc (this can be changed via the {@link #setSunRadius(double)} method , and atmospheric refraction accounts for
1034
+ * 34 minutes or so (this can be changed via the {@link #setRefraction(double)} method), giving a total of 50
1035
+ * arcminutes. The total value for ZENITH is 90+(5/6) or 90.8333333&deg; for true sunrise/sunset.
1036
+ */
1037
+ // const ZENITH: number = GEOMETRIC_ZENITH + 5.0 / 6.0;
1038
+ /** Sun's zenith at civil twilight (96&deg;). */
1039
+ NOAACalculator.CIVIL_ZENITH = 96;
1040
+ /** Sun's zenith at nautical twilight (102&deg;). */
1041
+ NOAACalculator.NAUTICAL_ZENITH = 102;
1042
+ /** Sun's zenith at astronomical twilight (108&deg;). */
1043
+ NOAACalculator.ASTRONOMICAL_ZENITH = 108;
1044
+ /**
1045
+ * The <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> of January 1, 2000
1046
+ * @private
1047
+ */
1048
+ NOAACalculator.JULIAN_DAY_JAN_1_2000 = 2451545;
1049
+ /**
1050
+ * Julian days per century
1051
+ * @private
1052
+ */
1053
+ NOAACalculator.JULIAN_DAYS_PER_CENTURY = 36525;