-
Notifications
You must be signed in to change notification settings - Fork 0
/
Astro.js
3338 lines (2976 loc) · 101 KB
/
Astro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------------------------------------------------
// __ ____ ____ ____ __
// / _\ / ___)(_ _)( _ \ / \
// / \\___ \ )( ) /( O )
// \_/\_/(____/ (__) (__\_) \__/
//
// Astronomical Calculation Library
// -----------------------------------------------------------------------------------
// VERSION 1.0.1
// -----------------------------------------------------------------------------------
//
// Solar and Lunar Astronomical Calculation Library written in Javascript
//
// Astro is a fork of Fabio Soldati's excellent MeeusJS library. It also contains
// some components from the SunCalc library by Vladimir Agafonkin. I added some new
// interfaces and utility functions to the library.
//
// Why the fork?
//
// I was creating a Hologram widget that displays solar and lunar informaiton. I tested
// a few libraries and wasn't able to find one that met all my needs, so I ended
// up using both MeeusJS and SunCalc together and writing additional functions.
// It worked, but it wasn't the cleanest approach.
//
// I had two goals for Asto. First, I wanted a complete library that had all the commonly
// needed solar and lunar calculations. The second goal was a library with very simple
// interfaces for fast development.
//
// For example, to get solar position data using MeeusJS requires several function calls:
//
// var jdo = new A.JulianDay(new Date())
// var coord = A.EclCoord.fromWgs84(lat, lon)
// var tp = A.Solar.topocentricPosition(jdo, coord, true)
//
// Using Astro only requires only one function:
//
// var sunpos = A.get.sunPosition(new Date, lat, lon)
//
// Asto's methods also provides additional return data types for convenience. For example,
// solar and lunar azimuth is also available in degrees rather than only radian, so that
// there is less post-processing necessary for the developer.
//
// -----------------------------------------------------------------------------------
// Authors : Astro : Rick Ellis https://github.com/rickellis/Astro
// : MeeusJS : Fabio Soldati https://github.com/Fabiz/MeeusJs
// : SunCalc : Vladimir Agafonkin https://github.com/mourner/suncalc
// License : MIT
// -----------------------------------------------------------------------------------
(function () { 'use strict';
// If using ES6 module syntax uncomment this line and comment out the above one.
// export default (function () {
// SUPER OBJECT
var A = {}
// -----------------------------------------------------------------------------------
// CONSTANTS
// Julian and Besselian years described in chapter 21, Precession.
// T, Julian centuries since J2000 described in chapter 22, Nutation.
// -----------------------------------------------------------------------------------
A.VER = '1.1.0'
// Julian date of the modified Julian date epoch.
A.JMod = 2400000.5
// Julian date corresponding to January 1.5, year 2000.
A.J2000 = 2451545.0
// Julian days of 1900.
A.J1900 = 2415020.0
// Julian days of B1900 (see p. 133)
A.B1900 = 2415020.3135
// Julian days of B1950 (see p. 133)
A.B1950 = 2433282.4235
// JulianYear in days.
A.JulianYear = 365.25
// JulianCentury in days.
A.JulianCentury = 36525
// BesselianYear in days.
A.BesselianYear = 365.2421988
// One astronomical unit in km. This is roughly the distance from Earth to the Sun.
A.AU = 149597870
// angle, morning name, evening name
A.sunEventsArray = [
[-0.833, 'sunriseStart', 'sunsetStart' ],
[-0.3, 'sunRiseEnd', 'sunSetStart' ],
[-12, 'nauticalDawn', 'nauticalDusk' ],
[-18, 'nightEnd', 'nightStart' ],
[-6, 'dawnStart', 'duskStart' ],
[-4, 'goldenHourAmStart', 'goldenHourPmEnd' ],
[ 6, 'goldenHourAmEnd', 'goldenHourPmStart' ]
]
// -----------------------------------------------------------------------------------
// A.Get
//
// The methods in this object are what you will most commonly use to retrieve data.
// -----------------------------------------------------------------------------------
A.Get = {
// -----------------------------------------------------------------------------------
// Sun Position
//
// Takes the following arguments:
// object: A Javascript date object.
// integer: Latitude
// integer: Longitude
// integer: Optional height above sea level, in meters
//
// Returns an object with:
// {
// azimuthInRads: In radians
// azimuthInDegs: Azimuth in corrected degrees. 0˚ N, 90˚ E, 180˚ S, 270˚ W
// altitude: In radians
// altitudeInDegs: Altitude in degrees. 0° horizon, +90° zenith, −90° is nadir (down).
// ascension: Right ascension in radians
// declination: Declination in radians
// }
// -----------------------------------------------------------------------------------
sunPosition: function(date, lat, lon, h = 0) {
var jdo = new A.JulianDay(date);
var coord = A.EclCoord.fromWgs84(lat, lon, h);
var suntp = A.Solar.topocentricPosition(jdo, coord, true)
return {
azimuthInRads: suntp.hz.az,
azimuthInDegs: A.Util.radiansToCorrectedDegrees(suntp.hz.az),
altitudeInRads: suntp.hz.alt,
altitudeInDegs: A.Util.radiansToDegrees(suntp.hz.alt, true),
ascension: suntp.eq.ra,
declination: suntp.eq.dec
}
},
// -----------------------------------------------------------------------------------
// Moon Position
//
// Takes the following arguments:
// object: A Javascript date object.
// integer: Latitude
// integer: Longitude
// integer: Optional height above sea level, in meters
//
// Returns an object with:
// {
// azimuthInRads: In radians
// azimuthInDegs: Azimuth in corrected degrees. 0˚ N, 90˚ E, 180˚ S, 270˚ W
// altitudeInRads: In radians
// altitudeInDegs: Altitude in degrees. 0° horizon, +90° zenith, −90° is nadir (down).
// ascension: Right ascension in radians
// declination: Declination in radians
// delta: Distance between centers of the Earth and Moon, in km
// paralacticAngle: In radians
// }
// -----------------------------------------------------------------------------------
moonPosition: function(date, lat, lon, h = 0) {
var jdo = new A.JulianDay(date);
var coord = A.EclCoord.fromWgs84(lat, lon, h);
var moontp = A.Moon.topocentricPosition(jdo, coord, true);
return {
azimuthInRads: moontp.hz.az,
azimuthInDegs: A.Util.radiansToCorrectedDegrees(moontp.hz.az),
altitudeInRads: moontp.hz.alt,
altitudeInDegs: A.Util.radiansToDegrees(moontp.hz.alt, true),
ascension: moontp.eq.ra,
declination: moontp.eq.dec,
delta: moontp.delta,
paralacticAngle: moontp.q
}
},
// -----------------------------------------------------------------------------------
// Lunar Distance
//
// Takes the following arguments:
// object: A Javascript date object.
// number: options number of decimal places to show
// bool: Whether to format the number with commas
//
// Returns an object with:
// {
// kilometers: Distance to the moon in kilometers
// miles: Distance to the moon in miles
// }
// -----------------------------------------------------------------------------------
lunarDistance: function(date, decimals = -1, format = false) {
let julianDate = (((date.valueOf() / 86400000) - 0.5 + 2440588) - 2451545)
let meanAnomaly = (Math.PI / 180) * (134.963 + 13.064993 * julianDate)
let kilometers = 385001 - 20905 * Math.cos(meanAnomaly)
let miles = kilometers / 1.6
if (decimals > 0) {
kilometers = kilometers.toFixed(decimals)
miles = miles.toFixed(decimals)
}
kilometers = (format == true) ? A.Util.numberWithCommas(kilometers) : kilometers
miles = (format == true) ? A.Util.numberWithCommas(miles) : miles
return {
kilometers: kilometers,
miles: miles
}
},
// -----------------------------------------------------------------------------------
// Moon Illumination
//
// Takes the following arguments:
// object: A Javascript date object.
//
// Returns an object with:
// {
// phaseAngle: The illuminated fraction of the moon in radians
// fraction: The ratio of the illuminated area of the disk to the total area.
// illumination: The fraction turned into a percentage (ie, fraction * 100)
// phase: moon phase as a number between 0 and 1. See below.
// }
//
// 0.00 = New Moon
// 0.125 = Waxing Crescent
// 0.25 = First Quarter
// 0.375 = Waxing Gibbous
// 0.5 = Full Moon
// 62.5 = Waning Gibbous
// 0.75 = Last Quarter
// 0.875 = Waning Crescent
// -----------------------------------------------------------------------------------
moonIllumination: function(date) {
var illum = A.Suncalc.getMoonIllumination(date)
return {
phaseAngle: illum.angle,
fraction: illum.fraction,
illumination: illum.fraction * 100,
phase: illum.phase
}
},
// -----------------------------------------------------------------------------------
// Moon Coordinates
//
// Takes the following arguments:
// object: A Javascript date object.
//
// Returns an object with:
// {
// rightAscension: right ascension in radians
// declination: declination in rads
// distance: in kilometers
// }
// -----------------------------------------------------------------------------------
moonCoordinates: function(date) {
var coords = A.Suncalc.moonCoords(date)
return {
rightAscdension: coords.ra,
declination: coords.dec,
distance: coords.dist
}
},
// -----------------------------------------------------------------------------------
// Sun Times
//
// Takes the following arguments:
// object: A Javascript date object.
// integer: Latitude
// integer: Longitude
// integer: Optional height above sea level, in meters
//
// Returns an object with:
// {
// sunrise: Sunrise time in Unix
// transit: The length of time the sun is above the horizon, in seconds
// sunset: Sunset time in Unix
// }
// -----------------------------------------------------------------------------------
sunTimes: function(date, lat, lon, h = 0) {
var jdo = new A.JulianDay(date);
var coord = A.EclCoord.fromWgs84(lat, lon, h);
var suntimes = A.Solar.times(jdo, coord);
return {
sunrise: new Date(A.Util.formatISOdateString(date, suntimes.rise)),
transit: new Date(A.Util.formatISOdateString(date, suntimes.transit)),
sunset: new Date(A.Util.formatISOdateString(date, suntimes.set)),
}
},
// -----------------------------------------------------------------------------------
// Sun Events
//
// Takes the following arguments:
// object: A Javascript date object.
// integer: Latitude
// integer: Longitude
// integer: Optional height above sea level, in meters
//
// Returns an object with:
// {
// dawn: date object
// nauticalDawn: date object
// dawnStart: date object
// goldenHourAmStart: date object
// sunriseStart: date object
// sunriseEnd: date object
// goldenHourAmEnd: date object
// transit: date object
// solarNoon: date object
// goldenHourPmStart: date object
// sunsetStart: date object
// duskStart: date object
// goldenHourPmEnd: date object
// nauticalDusk: date object
// nightStart: date object
// nadir: date object
// nightEnd: date object
// dayLength: String in "HH:MM:SS"
// nightLength: String in "HH:MM:SS"
// }
// -----------------------------------------------------------------------------------
sunEvents: function(date, lat, lon, h = 0) {
let jdo = new A.JulianDay(date);
let coord = A.EclCoord.fromWgs84(lat, lon, h);
let suntimes = A.Solar.times(jdo, coord);
let daylight = suntimes.set - suntimes.rise
let night = 86400 - daylight
let events = A.Suncalc.sunEvents(date, lat, lon, h)
events.transit = new Date(A.Util.formatISOdateString(date, suntimes.transit))
events.dayLength = A.Coord.secondsToHMSStr(daylight, false)
events.nightLength = A.Coord.secondsToHMSStr(night, false)
return events
},
// -----------------------------------------------------------------------------------
// Moon Times
//
// Takes the following arguments:
// object: A Javascript date object.
// integer: Latitude
// integer: Longitude
// integer: Optional height above sea level, in meters
//
// Returns an object with:
// {
// moonrise: Sunrise time in Unix
// transit: The length of time the sun is above the horizon, in seconds
// moonset: Sunset time in Unix
// }
// -----------------------------------------------------------------------------------
moonTimes: function(date, lat, lon, h = 0) {
var jdo = new A.JulianDay(date);
var coord = A.EclCoord.fromWgs84(lat, lon, h);
var moontimes = A.Moon.times(jdo, coord);
return {
moonrise: new Date(A.Util.formatISOdateString(date, moontimes.rise)),
transit: new Date(A.Util.formatISOdateString(date, moontimes.transit)),
moonset: new Date(A.Util.formatISOdateString(date, moontimes.set)),
}
},
// -----------------------------------------------------------------------------------
// Summer Solstice
//
// Longest day of the year - when the sun is at its most northerly excursion
//
// Takes one argument:
// Integer: The year
//
// Returns a Javascript date object
// -----------------------------------------------------------------------------------
summerSolstice: function(year) {
return A.JulianDay.jdToDate(A.Solstice.june(year))
},
// -----------------------------------------------------------------------------------
// Winter Solstice
//
// Shortest day of the year - when the sun is at its most southerly excursion
//
// Takes one argument:
// Integer: The year
//
// Returns a Javascript date object
// -----------------------------------------------------------------------------------
winterSolstice: function(year) {
return A.JulianDay.jdToDate(A.Solstice.december(year))
},
// -----------------------------------------------------------------------------------
// Vernal Equinox.
//
// Spring equinox in March. On the equinox, the day and night are the same length.
// The time returned indicates the moment when a straight line following the
// equator travels through the center of the sun.
//
// Takes one argument:
// Integer: The year
//
// Returns a Javascript date object
// -----------------------------------------------------------------------------------
vernalEquinox: function(year) {
return A.JulianDay.jdToDate(A.Solstice.march(year))
},
// -----------------------------------------------------------------------------------
// Fall Equinox.
//
// Fall Equinox in September. On the equinox, the day and night are the same length.
// The time returned indicates the moment when a straight line following the equator
// travels through the center of the sun.
//
// Takes one argument:
// Integer: The year
//
// Returns a Javascript date object
// -----------------------------------------------------------------------------------
fallEquinox: function(year) {
return A.JulianDay.jdToDate(A.Solstice.september(year))
}
}
// -----------------------------------------------------------------------------------
// A.Set
// Setter functions
// -----------------------------------------------------------------------------------
A.Set = {
// -----------------------------------------------------------------------------------
// Add a Sun Event
//
// This function works in conjuntion with the A.get.sunEvents() function above.
// It allows custom events to be added to the even array.
//
// A.Set.sunEvent(-18, 'astronomicalTwighlight' ,'astronomicalDawn')
//
// Takes 3 arguments:
// decimal: The solar angle (positive or negative) you wish to key the event with
// srtring: The name of the "rise" event
// string: The name of the "set" event
//
// Returns null
// -----------------------------------------------------------------------------------
sunEvent: function (angle, riseName, setName) {
A.sunEventsArray.push([angle, riseName, setName])
},
}
// -----------------------------------------------------------------------------------
// A.Util
// A few useful methods
// -----------------------------------------------------------------------------------
A.Util = {
// -----------------------------------------------------------------------------------
// Format ISO Date String
//
// Takes the following arguments:
// object: A Javascript date object
// number: The hours, minutes, seconds, represented in seconds
//
// Returns an integer in degrees
// -----------------------------------------------------------------------------------
formatISOdateString: function(date, seconds) {
var year = date.getFullYear()
var day = date.getDate()
var month = date.getMonth()
month = (month == 0) ? 1 : month + 1
month = (month < 10) ? '0' + month : month
day = (day < 10) ? '0' + day : day
return year + '-' + month + '-' + day + 'T' + A.Coord.secondsToHMSStr(seconds, false) + '.000Z'
},
// -----------------------------------------------------------------------------------
// Radians To Degrees
//
// Takes the following arguments:
// Integer: The value in radians you wish to convert
// bool: Whether to preserve the sign. i.e. negative radians returns negative degrees
//
// Returns an integer in degrees
// -----------------------------------------------------------------------------------
radiansToDegrees: function (rad, preserveSign = false) {
if (preserveSign == false) {
return rad * 180 / Math.PI
}
let degrees = rad * 180 / Math.PI
// If the radians are negative or positive,
// the degrees we convert to must match that
let negative = (Math.sign(rad) == -1) ? true : false
if (negative == true) {
if (Math.sign(degrees) == 0) {
degrees = degrees * -1
}
}
else {
// Is positive
if (Math.sign(degrees) == -1) {
degrees = degrees * -1
}
}
return degrees
},
// -----------------------------------------------------------------------------------
// Radians To Corrected Degrees
//
// Converts rads to positive degrees, with north at zero
//
// Takes the following arguments:
// Integer: The value in radians you wish to convert
// integer: An optional offset in degrees. This value is subtracted
//
// Returns an integer in degrees
// -----------------------------------------------------------------------------------
radiansToCorrectedDegrees: function(rads, correction = 0) {
var degrees = this.radiansToDegrees(rads)
degrees = degrees - correction
if (degrees < 0) {
degrees = degrees + 360
if (degrees > 360) {
degrees = degress - 360
}
}
return this.invertDegree(degrees)
},
// -----------------------------------------------------------------------------------
// Invert a degree
//
// In other words, 60˚ becomes 240˚
//
// Takes one argument:
// Integer: The value in degrees you wish to flip
//
// Returns an integer in degrees
// -----------------------------------------------------------------------------------
invertDegree: function(deg) {
return deg <= 179 ? deg + 180 : deg - 180
},
// -----------------------------------------------------------------------------------
// Format number with commas
//
// Takes one argument:
// Integer: The value in degrees you wish to format
//
// Returns an integer
// -----------------------------------------------------------------------------------
numberWithCommas: function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
// -----------------------------------------------------------------------------------
// The following methods are from SunCalc.js
// -----------------------------------------------------------------------------------
A.Suncalc = {
toJulian: function(date) {
return date.valueOf() / 86400000 - 0.5 + 2440588;
},
fromJulian: function(j) {
var dayMs = 1000 * 60 * 60 * 24
return new Date((j + 0.5 - 2440588) * dayMs);
},
toDays: function(date) {
return this.toJulian(date) - 2451545;
},
rightAscension: function(l, b) {
return Math.atan2(Math.sin(l) * Math.cos(((Math.PI / 180) * 23.4397)) - Math.tan(b) * Math.sin(((Math.PI / 180) * 23.4397)), Math.cos(l));
},
declination: function(l, b) {
return Math.asin(Math.sin(b) * Math.cos(((Math.PI / 180) * 23.4397)) + Math.cos(b) * Math.sin(((Math.PI / 180) * 23.4397)) * Math.sin(l));
},
solarMeanAnomaly: function(d) {
return Math.PI / 180 * (357.5291 + 0.98560028 * d);
},
eclipticLongitude: function(M) {
var C = Math.PI / 180 * (1.9148 * Math.sin(M) + 0.02 * Math.sin(2 * M) + 0.0003 * Math.sin(3 * M)), // equation of center
P = Math.PI / 180 * 102.9372; // perihelion of the Earth
return M + C + P + Math.PI;
},
julianCycle: function(d, lw) {
return Math.round(d - 0.0009 - lw / (2 * Math.PI));
},
approxTransit: function(Ht, lw, n) {
return 0.0009 + (Ht + lw) / (2 * Math.PI) + n;
},
solarTransitJ: function(ds, M, L) {
return 2451545 + ds + 0.0053 * Math.sin(M) - 0.0069 * Math.sin(2 * L);
},
hourAngle: function(h, phi, d) {
return Math.acos((Math.sin(h) - Math.sin(phi) * Math.sin(d)) / (Math.cos(phi) * Math.cos(d)));
},
observerAngle: function(height) {
return -2.076 * Math.sqrt(height) / 60;
},
// returns set time for the given sun altitude
getSetJ: function(h, lw, phi, dec, n, M, L) {
var w = this.hourAngle(h, phi, dec),
a = this.approxTransit(w, lw, n);
return this.solarTransitJ(a, M, L);
},
sunCoords: function(d) {
var M = this.solarMeanAnomaly(d),
L = this.eclipticLongitude(M);
return {
dec: this.declination(L, 0),
ra: this.rightAscension(L, 0)
}
},
/**
* Geocentric ecliptic coordinates of the moon
*
* @function moonCoords
* @static
*
* @param {object} date object
* @return {object}
*/
moonCoords: function(d) {
var rad = Math.PI / 180
var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude
M = rad * (134.963 + 13.064993 * d), // mean anomaly
F = rad * (93.272 + 13.229350 * d), // mean distance
l = L + rad * 6.289 * Math.sin(M), // longitude
b = rad * 5.128 * Math.sin(F), // latitude
dt = 385001 - 20905 * Math.cos(M); // distance to the moon in km
return {
ra: this.rightAscension(l, b),
dec: this.declination(l, b),
dist: dt
}
},
/**
* Moon Illumination
*
* @function getMoonIllumination
* @static
*
* @param {object} date object
* @return {object}
*/
getMoonIllumination: function (date) {
var d = this.toDays(date || new Date()),
s = this.sunCoords(d),
m = this.moonCoords(d),
sdist = 149598000, // distance from Earth to Sun in km
phi = Math.acos(Math.sin(s.dec) * Math.sin(m.dec) + Math.cos(s.dec) * Math.cos(m.dec) * Math.cos(s.ra - m.ra)),
inc = Math.atan2(sdist * Math.sin(phi), m.dist - sdist * Math.cos(phi)),
angle = Math.atan2(Math.cos(s.dec) * Math.sin(s.ra - m.ra), Math.sin(s.dec) * Math.cos(m.dec) - Math.cos(s.dec) * Math.sin(m.dec) * Math.cos(s.ra - m.ra));
return {
fraction: (1 + Math.cos(inc)) / 2,
phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
angle: angle
}
},
/**
* sunEvents returns an object with sun event times
*
* @function sunEvents
* @static
*
* @param {object} date object
* @param {number} latitude
* @param {number} longitude
* @param {number} optional elevation in meters
* @return {object}
*/
sunEvents: function(date, lat, lng, height = 0) {
var i
var len
var time
var h0
var Jset
var Jrise;
var lw = Math.PI / 180 * -lng
var phi = Math.PI / 180 * lat
var dh = this.observerAngle(height)
var d = this.toDays(date)
var n = this.julianCycle(d, lw)
var ds = this.approxTransit(0, lw, n)
var M = this.solarMeanAnomaly(ds)
var L = this.eclipticLongitude(M)
var dec = this.declination(L, 0)
var Jnoon = this.solarTransitJ(ds, M, L)
var result = {}
for (i = 0, len = A.sunEventsArray.length; i < len; i += 1) {
time = A.sunEventsArray[i];
h0 = (time[0] + dh) * Math.PI / 180
Jset = this.getSetJ(h0, lw, phi, dec, n, M, L)
Jrise = Jnoon - (Jset - Jnoon)
result[time[1]] = this.fromJulian(Jrise)
result[time[2]] = this.fromJulian(Jset)
}
result.solarNoon = this.fromJulian(Jnoon)
result.nadir = this.fromJulian(Jnoon - 0.5)
return result
}
}
// -----------------------------------------------------------------------------------
// The MeeusJS Library
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
// A.Coord
// Transformation of Coordinates
// Chapter 13
// -----------------------------------------------------------------------------------
/**
* @module A.Coord
*/
A.Coord = {
/**
* DMSToDeg converts from parsed sexagesimal angle components to decimal degrees.
*
* @function dmsToDeg
* @static
*
* @param {boolean} neg` - set to true if negative
* @param {number} d - degrees
* @param {number} m - minutes
* @param {number} s - seconds
* @return {number} decimal degrees
*/
dmsToDeg: function(neg, d, m, s) {
s = (((d*60+m)*60) + s) / 3600;
if (neg) {
return -s;
}
return s;
},
/**
* Returns radian value from an angle.
*
* @function calcAngle
* @static
*
* @param {boolean} neg - set to true if negative
* @param {number} d - degrees
* @param {number} m - minutes
* @param {number} s - seconds
* @return {number} degrees in radians
*/
calcAngle: function(neg, d, m, s) {
return A.Coord.dmsToDeg(neg, d, m, s) * Math.PI / 180;
},
/**
* Returns a radian value from hour, minute, and second components.
*
* Negative values are not supported, and NewRA wraps values larger than 24
* to the range [0,24) hours.
*
* @function calcRA
* @static
*
* @param {number} h - hours
* @param {number} m - minutes
* @param {number} s - seconds
* @return {number} degrees in radians
*/
calcRA: function(h, m, s) {
var r = A.Coord.dmsToDeg(false, h, m, s) % 24;
return r * 15 * Math.PI / 180;
},
/**
* Returns a pretty formatted HMS string from the given seconds
*
* @function secondsToHMSStr
* @static
* @param {number} sec - seconds
* @return {string} formatted string
*/
secondsToHMSStr: function(sec, showDays = true) {
var days = Math.floor(sec / 86400);
sec = A.Math.pMod(sec, 86400);
var hours = Math.floor(sec/3600) % 24;
var minutes = Math.floor(sec/60) % 60;
var seconds = Math.floor(sec % 60);
return (days !== 0 && showDays == true ? days + "d " : "") + (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
},
/**
* Returns a pretty formatted HM string from the given seconds
*
* @function secondsToHMStr
* @static
* @param {number} sec - seconds
* @return {string} formatted string
*/
secondsToHMStr: function(sec, showDays = true) {
var days = Math.floor(sec / 86400);
sec = A.Math.pMod(sec, 86400);
var hours = Math.floor(sec/3600) % 24;
var minutes = Math.floor(sec/60) % 60;
return (days !== 0 && showDays == true ? days + "d " : "") + (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;
},
/**
* EqToEcl converts equatorial coordinates to ecliptic coordinates.
* @function eqToEcl
* @static
*
* @param {EqCoord} eqcoord - equatorial coordinates, in radians
* @param {number} epsilon - obliquity of the ecliptic
*
* @return {EclCoord} ecliptic coordinates of observer on Earth
*/
eqToEcl: function(eqcoord, epsilon) {
var sra = Math.sin(eqcoord.ra);
var cra = Math.cos(eqcoord.ra);
var sdec = Math.sin(eqcoord.dec);
var cdec = Math.cos(eqcoord.dec);
var sepsilon = Math.sin(epsilon);
var cepsilon = Math.cos(epsilon);
return new A.EclCoord(
Math.atan2(sra * cepsilon + (sdec / cdec) * sepsilon, cra), // (13.1) p. 93
Math.asin(sdec * cepsilon - cdec * sepsilon * sra) // (13.2) p. 93
);
},
/**
* EclToEq converts ecliptic coordinates to equatorial coordinates.
* @function eclToEq
* @static
*
* @param {EclCoord} latlng - ecliptic coordinates of observer on Earth
* @param {number} epsilon - obliquity of the ecliptic
*
* @return {EqCoord} equatorial coordinates, in radians
*/
eclToEq: function(eclCoord, epsilon) {
var slat = Math.sin(eclCoord.lat);
var clat = Math.cos(eclCoord.lat);
var slng = Math.sin(eclCoord.lng);
var clng = Math.cos(eclCoord.lng);
var sepsilon = Math.sin(epsilon);
var cepsilon = Math.cos(epsilon);
var ra = Math.atan2(slat * cepsilon - (slng / clng) * sepsilon, clat); // (13.3) p. 93
if (ra < 0) {
ra += 2 * Math.PI;
}
return new A.EqCoord(
ra,
Math.asin(slng * cepsilon + clng * sepsilon * slat) // (13.4) p. 93
);
},
/**
* EqToHz computes Horizontal coordinates from equatorial coordinates.
* Sidereal time must be consistent with the equatorial coordinates.
* If coordinates are apparent, sidereal time must be apparent as well.
*
* @function eqToHz
* @static
*
* @param {EqCoord} eqcoord - equatorial coordinates, in radians
* @param {EclCoord} latlng - ecliptic coordinates of observer on Earth
* @param {number} st - sidereal time at Greenwich at time of observation in radians.
* @return {HzCoord} horizontal coordinates, in radians
*/
eqToHz: function(eqcoord, eclCoord, strad) {
var H = strad - eclCoord.lng - eqcoord.ra;
var sH = Math.sin(H);
var cH = Math.cos(H);
var slat = Math.sin(eclCoord.lat);
var clat = Math.cos(eclCoord.lat);
var sdec = Math.sin(eqcoord.dec);
var cdec = Math.cos(eqcoord.dec);
return new A.HzCoord(
Math.atan2(sH, cH * slat - (sdec / cdec) * clat), // (13.5) p. 93
Math.asin(slat * sdec + clat * cdec * cH) // (13.6) p. 93
);
}
};
// -----------------------------------------------------------------------------------
/**
* Represents a geographical point in ecliptic coordinates with latitude and longitude coordinates
* and an optional height.
* @constructor
* @param {number} lat - The latitude of the location in radians.
* @param {number} lng - The longitude of the location in radians. Longitudes are measured positively westward
* @param {?number} h - The height above the sea level.
*/
A.EclCoord = function (lat, lng, h) {
if (isNaN(lat) || isNaN(lng)) {
throw new Error('Invalid EclCoord object: (' + lat + ', ' + lng + ')');
}
this.lat = lat;
this.lng = lng;
if (h !== undefined) {
this.h = h;
}
};
A.EclCoord.prototype = {
/**
* Returns a pretty printed string in the WGS84 format.
* @return {String} Pretty printed string.
*/
toWgs84String: function () { // (Number) -> String
return A.Math.formatNum(this.lat * 180 / Math.PI) + ', ' + A.Math.formatNum(-this.lng * 180 / Math.PI);
}
};
/**
* Create a new EclCoord object from the given wgs coordinates.
*
* @param {number} lat - The latitude of the location degrees.
* @param {number} lng - The longitude of the location degrees.
* @param {?number} h - The height above the sea level.
*/
A.EclCoord.fromWgs84 = function(wgs84lat, wgs84lng, h) {
return new A.EclCoord(wgs84lat * Math.PI / 180, -wgs84lng * Math.PI / 180, h);
};
// -----------------------------------------------------------------------------------
/**
* Represents a geographical point in equatorial coordinates with right ascension and declination.
* @constructor
* @param {number} ra - The right ascension in radians.
* @param {number} dec - The declination in radians.
*/
A.EqCoord = function (ra, dec) { // (Number, Number, Number)
if (isNaN(ra) || isNaN(dec)) {
throw new Error('Invalid EqCoord object: (' + ra + ', ' + dec + ')');
}