forked from croxis/planetrise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astronomy.js
1880 lines (1535 loc) · 71.2 KB
/
astronomy.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
/*
astronomy.js - by Don Cross - http://cosinekitty.com
General-purpose astronomy calculation routines.
Designed for use by html, svg, and cscript/wscript.
*/
//----------------------------------------------------------------------------------------------
// Numeric constants...
var METERS_PER_ASTRONOMICAL_UNIT = 1.4959787e+11;
var METERS_PER_EARTH_EQUATORIAL_RADIUS = 6378140.0;
var EARTH_RADII_PER_ASTRONOMICAL_UNIT = METERS_PER_ASTRONOMICAL_UNIT / METERS_PER_EARTH_EQUATORIAL_RADIUS; // 23454.78
//----------------------------------------------------------------------------------------------
// Custom trigonometry and math functions.
// We do most of our math in angular degrees and sideral hours, instead of radians.
function AngleClass()
{
this.DEG_FROM_RAD = 180.0 / Math.PI;
this.RAD_FROM_DEG = Math.PI / 180.0;
this.HOURS_FROM_RAD = 12.0 / Math.PI;
this.RAD_FROM_HOURS = Math.PI / 12.0;
this.CosDeg = function (degrees)
{
return Math.cos (this.RAD_FROM_DEG * degrees);
}
this.SinDeg = function (degrees)
{
return Math.sin (this.RAD_FROM_DEG * degrees);
}
this.TanDeg = function (degrees)
{
return Math.tan (this.RAD_FROM_DEG * degrees);
}
this.CosHour = function (hours)
{
return Math.cos (this.RAD_FROM_HOURS * hours);
}
this.SinHour = function (hours)
{
return Math.sin (this.RAD_FROM_HOURS * hours);
}
this.AtanDeg2 = function (y, x)
{
return this.DEG_FROM_RAD * Math.atan2 (y, x);
}
this.FixHours = function (hours)
{
return this.FixCycle (hours, 24.0);
}
this.FixDegrees = function (degrees)
{
return this.FixCycle (degrees, 360.0);
}
this.FixCycle = function (angle, cycle)
{
var fraction = angle / cycle;
return cycle * (fraction - Math.floor (fraction));
}
this.Polar = function (x, y, z)
{
var rho = (x * x) + (y * y);
var radius = Math.sqrt (rho + (z * z));
var phi = Angle.AtanDeg2 (y, x);
if (phi < 0) {
phi += 360.0;
}
var rho = Math.sqrt (rho);
var theta = Angle.AtanDeg2 (z, rho);
return new SphericalCoordinates (phi, theta, radius);
}
this.DMS = function (x)
{
var a = {};
a.negative = (x < 0);
if (a.negative) {
x = -x;
}
a.degrees = Math.floor (x);
x = 60.0 * (x - a.degrees);
a.minutes = Math.floor (x);
x = 60.0 * (x - a.minutes);
a.seconds = Math.round (10.0 * x) / 10.0; // Round to the nearest tenth of an arcsecond.
if (a.seconds == 60) {
a.seconds = 0;
if (++a.minutes == 60) {
a.minutes = 0;
++a.degrees;
}
}
return a;
}
this.DMM = function (x)
{
var a = {};
a.negative = (x < 0);
if (a.negative) {
x = -x;
}
a.degrees = Math.floor (x);
x = 60.0 * (x - a.degrees);
a.minutes = Math.round (100.0 * x) / 100.0; // Round to nearest hundredth of an arcminute.
a.seconds = 0.0; // Fill in just for consistency with Angle.DMS
if (a.minutes >= 60.0) {
a.minutes -= 60.0;
++a.degrees;
}
return a;
}
this.SafeArcSinInDegrees = function (z)
{
var abs = Math.abs (z);
if (abs > 1.0) {
// Guard against blowing up due to slight roundoff errors in Math.Asin ...
if (abs > 1.00000001) {
throw "Invalid argument to SafeArcSinInDegrees";
} else if (z < -1.0) {
return -90.0;
} else {
return +90.0;
}
} else {
return this.DEG_FROM_RAD * Math.asin(z);
}
}
}
var Angle = new AngleClass();
//----------------------------------------------------------------------------------------------
function DefaultGeocentricCoordinates (day) // this function is used for most celestial bodies (but not the Moon)
{
var bc = this.EclipticCartesianCoordinates (day); // get this body's heliocentric coordinates
var ec = Astronomy.Earth.EclipticCartesianCoordinates (day); // get Earth's heliocentric coordinates
return bc.Subtract (ec); // subtract vectors to get the vector from Earth to this body
}
function DefaultEclipticAngularCoordinates (day)
{
var hc = this.EclipticCartesianCoordinates (day);
var ac = Angle.Polar (hc.x, hc.y, hc.z);
return ac;
}
function DefaultHorizontalCoordinates (day, location)
{
var sky = this.EquatorialCoordinates (day, location);
return new HorizontalCoordinates (sky, location, day);
}
function Log10 (x)
{
return Math.log(x) / Math.LN10;
}
//----------------------------------------------------------------------------------------------
function SunClass()
{
this.Name = "Sun";
this.EclipticCartesianCoordinates = function(day)
{
return new CartesianCoordinates (0.0, 0.0, 0.0);
}
this.EclipticAngularCoordinates = function(day)
{
return new SphericalCoordinates (0.0, 0.0, 0.0);
}
this.GeocentricCoordinates = DefaultGeocentricCoordinates;
this.EquatorialCoordinates = EqCoords;
this.DistanceFromEarth = function (day)
{
return Astronomy.Earth.EclipticCartesianCoordinates(day).Distance();
}
this.DistanceFromSun = function (day)
{
return 0.0;
}
this.HorizontalCoordinates = DefaultHorizontalCoordinates;
this.RadiusInMeters = 6.96e+8;
this.VisualMagnitude = function (day)
{
var e = this.DistanceFromEarth (day);
return -26.73 + (5.0 * Log10(e));
}
}
//----------------------------------------------------------------------------------------------
function EarthClass()
{
this.Name = "Earth";
this.EclipticCartesianCoordinates = function(day)
{
// We use formulas for finding the Sun as seen from Earth,
// then negate the (x,y,z) coordinates obtained to get the Earth's position
// from the Sun's perspective.
// http://www.astro.uio.no/~bgranslo/aares/calculate.html <== Note error in formula for DS, using sin(RS) where it should say sin(LS)
// http://www.meteorobs.org/maillist/msg09197.html <== Correct formulas, more accurate (complex)
// These formulas use 'd' based on days since 1/Jan/2000 12:00 UTC ("J2000.0"), instead of 0/Jan/2000 0:00 UTC ("day value").
// Correct by subtracting 1.5 days...
var d = day - 1.5;
var T = d / 36525.0; // Julian centuries since J2000.0
var L0 = 280.46645 + (36000.76983 * T) + (0.0003032 * T * T); // Sun's mean longitude, in degrees
var M0 = 357.52910 + (35999.05030 * T) - (0.0001559 * T * T) - (0.00000048 * T * T * T); // Sun's mean anomaly, in degrees
var C = // Sun's equation of center in degrees
(1.914600 - 0.004817 * T - 0.000014 * T * T) * Angle.SinDeg (M0) +
(0.01993 - 0.000101 * T) * Angle.SinDeg (2 * M0) +
0.000290 * Angle.SinDeg (3 * M0)
;
var LS = L0 + C; // true ecliptical longitude of Sun
var e = 0.016708617 - T * (0.000042037 + (0.0000001236 * T)); // The eccentricity of the Earth's orbit.
var distanceInAU = (1.000001018 * (1 - e * e)) / (1 + e * Angle.CosDeg(M0 + C)); // distance from Sun to Earth in astronomical units (AU)
var x = -distanceInAU * Angle.CosDeg (LS);
var y = -distanceInAU * Angle.SinDeg (LS);
return new CartesianCoordinates (x, y, 0.0); // the Earth's center is always on the plane of the ecliptic (z=0), by definition!
}
this.EclipticAngularCoordinates = DefaultEclipticAngularCoordinates;
this.GeocentricCoordinates = function(day)
{
return new CartesianCoordinates (0.0, 0.0, 0.0);
}
this.EquatorialCoordinates = EqCoords;
this.DistanceFromEarth = function(day)
{
return 0.0; // included for completeness and consistency of planet interface
}
this.DistanceFromSun = function (day)
{
return this.EclipticCartesianCoordinates(day).Distance();
}
this.VisualMagnitude = function (day)
{
throw "Cannot calculate visual magnitude for Earth!";
}
}
//----------------------------------------------------------------------------------------------
// We treat Pluto as a special case. See http://www.stjarnhimlen.se/comp/ppcomp.html#14
function PlutoClass()
{
this.Name = "Pluto";
this.EclipticCartesianCoordinates = function(day)
{
var S = 50.03 + (0.033459652 * day);
var P = 238.95 + (0.003968789 * day);
var lonecl = 238.9508 + (0.00400703 * day) -
19.799 * Angle.SinDeg( P) + 19.848 * Angle.CosDeg( P) +
0.897 * Angle.SinDeg(2*P) - 4.956 * Angle.CosDeg(2*P) +
0.610 * Angle.SinDeg(3*P) + 1.211 * Angle.CosDeg(3*P) -
0.341 * Angle.SinDeg(4*P) - 0.190 * Angle.CosDeg(4*P) +
0.128 * Angle.SinDeg(5*P) - 0.034 * Angle.CosDeg(5*P) -
0.038 * Angle.SinDeg(6*P) + 0.031 * Angle.CosDeg(6*P) +
0.020 * Angle.SinDeg(S-P) - 0.010 * Angle.CosDeg(S-P)
;
var latecl = -3.9082 -
5.453 * Angle.SinDeg( P) - 14.975 * Angle.CosDeg( P) +
3.527 * Angle.SinDeg(2*P) + 1.673 * Angle.CosDeg(2*P) -
1.051 * Angle.SinDeg(3*P) + 0.328 * Angle.CosDeg(3*P) +
0.179 * Angle.SinDeg(4*P) - 0.292 * Angle.CosDeg(4*P) +
0.019 * Angle.SinDeg(5*P) + 0.100 * Angle.CosDeg(5*P) -
0.031 * Angle.SinDeg(6*P) - 0.026 * Angle.CosDeg(6*P) +
0.011 * Angle.CosDeg(S-P)
;
var r = 40.72 +
6.68 * Angle.SinDeg( P) + 6.90 * Angle.CosDeg( P) -
1.18 * Angle.SinDeg(2*P) - 0.03 * Angle.CosDeg(2*P) +
0.15 * Angle.SinDeg(3*P) - 0.14 * Angle.CosDeg(3*P)
;
var coslon = Angle.CosDeg (lonecl);
var sinlon = Angle.SinDeg (lonecl);
var coslat = Angle.CosDeg (latecl);
var sinlat = Angle.SinDeg (latecl);
var xp = r * coslon * coslat;
var yp = r * sinlon * coslat;
var zp = r * sinlat;
return new CartesianCoordinates (xp, yp, zp); // the Earth's center is always on the plane of the ecliptic (z=0), by definition!
}
this.HorizontalCoordinates = DefaultHorizontalCoordinates;
this.EclipticAngularCoordinates = DefaultEclipticAngularCoordinates;
this.GeocentricCoordinates = DefaultGeocentricCoordinates;
this.EquatorialCoordinates = EqCoords;
this.DistanceFromEarth = function (day)
{
return this.GeocentricCoordinates(day).Distance();
}
this.DistanceFromSun = function (day)
{
return this.EclipticCartesianCoordinates(day).Distance();
}
this.VisualMagnitude = function (day)
{
var s = this.DistanceFromSun (day);
var e = this.DistanceFromEarth (day);
return 14.0 + (5.0 * Log10 ((e * s) / (31.97177 * 31.982))); // a hack that ignores phase angle, based on data from http://www.heavens-above.com
}
}
//----------------------------------------------------------------------------------------------
function PlanetPS ( // See http://www.stjarnhimlen.se/comp/ppcomp.html#4
name, // name of the object, e.g. "Mars"
N0, Nc, // N0 = longitude of the ascending node (deg). Nc = rate of change in deg/day
i0, ic, // inclination to the ecliptic (deg)
w0, wc, // argument of perihelion (deg)
a0, ac, // semi-major axis, or mean distance from Sun (AU)
e0, ec, // eccentricity (0=circle, 0..1=ellipse, 1=parabola)
M0, Mc, // M0 = mean anomaly (deg) (0 at perihelion; increases uniformly with time). Mc ("mean motion") = rate of change in deg/day = 360/period
magBase,
magPhaseFactor,
magNonlinearFactor,
magNonlinearExponent )
{
this.Name = name;
this.BodyType = 'planet';
this.N0 = N0;
this.Nc = Nc;
this.i0 = i0;
this.ic = ic;
this.w0 = w0;
this.wc = wc;
this.a0 = a0;
this.ac = ac;
this.e0 = e0;
this.ec = ec;
this.M0 = M0;
this.Mc = Mc;
this.magBase = magBase;
this.magPhaseFactor = magPhaseFactor;
this.magNonlinearFactor = magNonlinearFactor;
this.magNonlinearExponent = magNonlinearExponent;
}
PlanetPS.prototype.HorizontalCoordinates = DefaultHorizontalCoordinates;
PlanetPS.prototype.EclipticAngularCoordinates = DefaultEclipticAngularCoordinates;
PlanetPS.prototype.MeanAnomaly = function (day) // day = number of days elapsed since December 31, 1999 0:00 UTC, i.e. JD 2451543.5
{
return this.M0 + (day * this.Mc);
}
PlanetPS.prototype.NodeLongitude = function (day)
{
return this.N0 + (day * this.Nc);
}
PlanetPS.prototype.Perihelion = function (day)
{
return this.w0 + (day * this.wc);
}
PlanetPS.prototype.GeocentricCoordinates = DefaultGeocentricCoordinates;
PlanetPS.prototype.EquatorialCoordinates = EqCoords;
PlanetPS.prototype.EclipticCartesianCoordinates = function (day)
{
var a = this.a0 + (day * this.ac);
var e = this.e0 + (day * this.ec);
var M = this.M0 + (day * this.Mc);
var N = this.N0 + (day * this.Nc);
var w = this.w0 + (day * this.wc);
var i = this.i0 + (day * this.ic);
var E = EccentricAnomaly (e, M);
// Calculate the body's position in its own orbital plane, and its distance from the thing it is orbiting.
var xv = a * (Angle.CosDeg(E) - e);
var yv = a * (Math.sqrt(1.0 - e*e) * Angle.SinDeg(E));
var v = Angle.AtanDeg2 (yv, xv); // True anomaly in degrees: the angle from perihelion of the body as seen by the Sun.
var r = Math.sqrt (xv*xv + yv*yv); // Distance from the Sun to the planet in AU
var cosN = Angle.CosDeg (N);
var sinN = Angle.SinDeg (N);
var cosi = Angle.CosDeg (i);
var sini = Angle.SinDeg (i);
var cosVW = Angle.CosDeg (v + w);
var sinVW = Angle.SinDeg (v + w);
// Now we are ready to calculate (unperturbed) ecliptic cartesian heliocentric coordinates.
var xh = r * (cosN*cosVW - sinN*sinVW*cosi);
var yh = r * (sinN*cosVW + cosN*sinVW*cosi);
var zh = r * sinVW * sini;
return this.Perturb (xh, yh, zh, day);
}
PlanetPS.prototype.Perturb = function (xh, yh, zh, day)
{
// By default we apply no perturbations.
// Some planets will override this method so that they can correct for
// the gravitational influences of other planets.
return new CartesianCoordinates (xh, yh, zh);
}
PlanetPS.prototype.DistanceFromEarth = function (day) // returns the distance of this planet from Earth in astronomical units (AU)
{
return this.GeocentricCoordinates(day).Distance();
}
PlanetPS.prototype.DistanceFromSun = function (day)
{
return this.EclipticCartesianCoordinates(day).Distance();
}
PlanetPS.prototype.VisualMagnitude = function (day)
{
var distEarth = this.DistanceFromEarth (day);
var distSun = this.DistanceFromSun (day);
var phase = Astronomy.SunEarthPhaseAngle (this, day);
var mag = this.magBase + (5 * Log10 (distSun * distEarth)) + (this.magPhaseFactor * phase);
if (this.magNonlinearExponent > 0) {
mag += this.magNonlinearFactor * Math.pow (phase, this.magNonlinearExponent);
}
return mag;
}
function EccentricAnomaly (e, M)
{
var E = M + (e * Angle.SinDeg(M) * (1.0 + (e * Angle.CosDeg(M))));
for(;;) {
var F = E - (E - (Angle.DEG_FROM_RAD * e * Angle.SinDeg (E)) - M) / (1 - e * Angle.CosDeg (E));
var error = Math.abs (F - E);
E = F;
if (error < 1.0e-8) {
break; // the angle is good enough now for our purposes
}
}
return E;
}
function PerturbMajorPlanet (xh, yh, zh, d)
{
var ecliptic = Astronomy.EclipticLatLon (xh, yh, zh);
var lonecl = ecliptic.longitude;
var latecl = ecliptic.latitude;
var r = Math.sqrt (xh*xh + yh*yh + zh*zh);
lonecl += this.PerturbEclipticLongitude (d);
latecl += this.PerturbEclipticLatitude (d);
var coslon = Angle.CosDeg (lonecl);
var sinlon = Angle.SinDeg (lonecl);
var coslat = Angle.CosDeg (latecl);
var sinlat = Angle.SinDeg (latecl);
var xp = r * coslon * coslat;
var yp = r * sinlon * coslat;
var zp = r * sinlat;
return new CartesianCoordinates (xp, yp, zp);
}
function PerturbEclipticLongitude_Jupiter (d)
{
var Mj = Astronomy.Jupiter.MeanAnomaly (d);
var Ms = Astronomy.Saturn.MeanAnomaly (d);
var deltaLong =
-0.332 * Angle.SinDeg(2*Mj - 5*Ms - 67.6) -
0.056 * Angle.SinDeg(2*Mj - 2*Ms + 21 ) +
0.042 * Angle.SinDeg(3*Mj - 5*Ms + 21 ) -
0.036 * Angle.SinDeg( Mj - 2*Ms ) +
0.022 * Angle.CosDeg( Mj - Ms ) +
0.023 * Angle.SinDeg(2*Mj - 3*Ms + 52 ) -
0.016 * Angle.SinDeg( Mj - 5*Ms - 69 )
;
return deltaLong;
}
function PerturbEclipticLatitude_Jupiter (d)
{
return 0.0;
}
function PerturbEclipticLongitude_Saturn (d)
{
var Mj = Astronomy.Jupiter.MeanAnomaly (d);
var Ms = Astronomy.Saturn.MeanAnomaly (d);
var deltaLong =
0.812 * Angle.SinDeg (2*Mj - 5*Ms - 67.6) -
0.229 * Angle.CosDeg (2*Mj - 4*Ms - 2.0) +
0.119 * Angle.SinDeg ( Mj - 2*Ms - 3.0) +
0.046 * Angle.SinDeg (2*Mj - 6*Ms - 69.0) +
0.014 * Angle.SinDeg ( Mj - 3*Ms + 32.0)
;
return deltaLong;
}
function PerturbEclipticLatitude_Saturn (d)
{
var Mj = Astronomy.Jupiter.MeanAnomaly (d);
var Ms = Astronomy.Saturn.MeanAnomaly (d);
var deltaLat =
-0.020 * Angle.CosDeg(2*Mj - 4*Ms - 2) +
0.018 * Angle.SinDeg(2*Mj - 6*Ms - 49)
;
return deltaLat;
}
function PerturbEclipticLongitude_Uranus (d)
{
var Mj = Astronomy.Jupiter.MeanAnomaly (d);
var Ms = Astronomy.Saturn.MeanAnomaly (d);
var Mu = this.MeanAnomaly (d);
var deltaLong =
+0.040 * Angle.SinDeg(Ms - 2*Mu + 6)
+0.035 * Angle.SinDeg(Ms - 3*Mu + 33)
-0.015 * Angle.SinDeg(Mj - Mu + 20)
;
return deltaLong;
}
function PerturbEclipticLatitude_Uranus (d)
{
return 0.0;
}
function EqCoords (day, location)
{
var vectorFromEarth = this.GeocentricCoordinates (day);
var dx = vectorFromEarth.x;
var dy = vectorFromEarth.y;
var dz = vectorFromEarth.z;
// Now (dx,dy,dz) comprise the vector from the Earth to the object in cartesian ecliptic coordinates.
// We convert here to equatorial coordinates, using formulas based on the precession of the Earth's axis of rotation.
var T = Astronomy.CenturiesSinceJ2000 (day);
var K = 23.4392911 - ((46.8150 * T) - (0.00059 * T * T) + (0.001813 * T * T * T))/3600.0; // obliquity of ecliptic in degrees.
var cosK = Angle.CosDeg(K);
var sinK = Angle.SinDeg(K);
// Calculate equatorial cartesian coordinates using ecliptic cartesian coordinates...
var qx = dx;
var qy = (dy * cosK) - (dz * sinK);
var qz = (dy * sinK) + (dz * cosK);
var eq = Angle.Polar (qx, qy, qz);
eq.longitude /= 15.0; // convert degrees to sidereal hours
var DEC = eq.latitude;
var RA = eq.longitude;
var distanceInAU = eq.radius;
// Determine whether a topocentric correction is warranted.
// Imagine the angle between the the center of the Earth (geocentric), the remote body,
// and the observer on the surface of the Earth (topocentric).
// If this angle is less than 1 arcminute, we won't bother with the extra calculation time.
// Use approximation to determine whether the parallax matters:
// asin(RE/r) >= 1" where RE = radius of Earth, r = distance to CelestialBody.
// RE/r >= pi / (180 * 3600)
var parallaxInRadians = 1.0 / (EARTH_RADII_PER_ASTRONOMICAL_UNIT * distanceInAU);
if (parallaxInRadians >= Math.PI / (180.0 * 3600.0)) {
// We were using the approximation that parallax = asin(parallax).
// Now that we know the parallax is significant, go ahead and calculate the exact angle.
parallaxInRadians = Math.asin (parallaxInRadians);
// It is easier to calculate topocentric correction in horizontal coordinates...
var hor = new HorizontalCoordinates (eq, location, day, 0.0);
var gclat;
var rho;
if (Astronomy.CorrectForOblateEarth) {
gclat = OblateLatitudeCorrection (location.latitude);
rho = OblateRadiusCorrection (location.latitude);
} else {
gclat = location.latitude;
rho = 1.0;
}
var altitude = hor.altitude - ((Angle.DEG_FROM_RAD * parallaxInRadians) * Angle.CosDeg (hor.altitude));
var Ls = MeanLongitudeOfSun (day);
var GMST0 = (Ls + 180.0) / 15.0;
var utcHours = (day - Math.floor(day)) * 24.0;
var LST = GMST0 + utcHours + (location.longitude / 15.0);
var hourAngle = LST - eq.longitude;
var g = Angle.DEG_FROM_RAD * (Math.atan(Angle.TanDeg(gclat) / Angle.CosHour(hourAngle)));
var topRA = eq.longitude - ((parallaxInRadians * Angle.HOURS_FROM_RAD) * rho * Angle.CosDeg(gclat) * Angle.SinHour(hourAngle) / Angle.CosDeg(eq.latitude));
var topDEC;
if (Math.abs(g) < 1.0e-6) {
topDEC = eq.latitude - ((parallaxInRadians * Angle.DEG_FROM_RAD) * rho * Angle.SinDeg(-eq.latitude) * Angle.CosHour(hourAngle));
} else {
topDEC = eq.latitude - (parallaxInRadians * Angle.DEG_FROM_RAD) * rho * Angle.SinDeg(gclat) * Angle.SinDeg(g - eq.latitude) / Angle.SinDeg(g);
}
eq = new SphericalCoordinates (topRA, topDEC, distanceInAU);
}
return eq;
}
function OblateLatitudeCorrection (latitude)
{
// This function corrects for the flattening of the Earth due to its rotation.
// Given a geographic latitude (which is defined based on the tilt of one's local horizon),
// this function returns geocentric latitude (based on the angle between the equatorial plane and the location).
// The correction is zero at the equator and at both poles, and is maximized at |latitude| = 45 degrees.
// See:
// http://en.wikipedia.org/wiki/Latitude#Common_.22latitude.22
// http://en.wikipedia.org/wiki/Latitude#Geocentric_latitude
return latitude - (0.1924 * Angle.SinDeg(2.0 * latitude));
}
function OblateRadiusCorrection (latitude)
{
// Returns the fraction of equatorial Earth radius for sea level at the given geographic latitude.
// This is due to flattening caused by Earth's rotation.
// When latitude==0 (i.e. a point on the equator), the value returned is 1.0.
// The value is minimized at latitude==+90 (North Pole) or latitude==-90 (South Pole).
return 0.99833 + (0.00167 * Angle.CosDeg(2.0 * latitude));
}
function HorizontalCoordinates (sky, location, day, horizonCorrectionInArcMinutes)
{
// http://en.wikipedia.org/wiki/Horizontal_coordinate_system
if (horizonCorrectionInArcMinutes == null) {
horizonCorrectionInArcMinutes = -34.0;
}
var GST = GreenwichSiderealTimeInHours (day);
var LST = GST + (location.longitude / 15.0);
var hourAngle = Angle.FixHours (LST - sky.longitude);
var sinLat = Angle.SinDeg (location.latitude);
var cosLat = Angle.CosDeg (location.latitude);
var sinHourAngle = Angle.SinHour (hourAngle);
var cosHourAngle = Angle.CosHour (hourAngle);
var sinDec = Angle.SinDeg (sky.latitude);
var cosDec = Angle.CosDeg (sky.latitude);
var altitudeRatio = (sinLat * sinDec) + (cosLat * cosDec * cosHourAngle);
// Correct for values that are out of range for inverse sine function...
var absAltitudeRatio = Math.abs (altitudeRatio);
if (absAltitudeRatio > 1.0) {
if (absAltitudeRatio > 1.000001) {
// This is an excessive amount of error: something must be wrong with the formula!
throw ("Internal error: altitude would be a complex number!");
} else {
// Just correct for apparent roundoff without going bezerk.
this.altitude = (altitudeRatio < 0) ? -90.0 : +90.0;
this.azimuth = 180.0; // doesn't really matter what angle we assign: use this value to assist culmination algorithm.
}
} else {
this.altitude = Angle.DEG_FROM_RAD * Math.asin (altitudeRatio);
var absAltitude = Math.abs (this.altitude);
var ANGLE_CORRECTION_WINDOW = 6.0; // I chose 6 degrees as the refraction cutoff, because that is used for Civil Twilight, which should not be corrected.
if ((absAltitude < ANGLE_CORRECTION_WINDOW) && (horizonCorrectionInArcMinutes != 0.0)) {
// http://www.jgiesen.de/refract/index.html For more technical details.
// Correct for atmospheric lensing making the object appear higher than it would if the Earth had no atmosphere.
// We want the correction to maximize its effect at the horizon, and vanish to zero as |altitude| approaches ANGLE_CORRECTION_WINDOW.
// For simplicity we apply a linear interpolation within this range of altitudes.
var linearFactor = (ANGLE_CORRECTION_WINDOW - absAltitude) / ANGLE_CORRECTION_WINDOW;
this.altitude -= (horizonCorrectionInArcMinutes / 60.0) * linearFactor;
}
this.azimuth = Angle.FixDegrees (Angle.AtanDeg2 (-cosDec * sinHourAngle, (cosLat * sinDec) - (sinLat * cosDec * cosHourAngle)));
}
}
function GreenwichSiderealTimeInHours (day)
{
var midnight = Math.floor (day); // discard fractional part to get same calendar day at midnight UTC
var T0 = Astronomy.CenturiesSinceJ2000 (midnight);
var tUT = (day - midnight) * 24.0; // Greenwich time of day, in hours
var SG = (6.6974 + 2400.0513 * T0) + (366.2422 / 365.2422) * tUT;
SG = Angle.FixHours (SG);
return SG;
}
//----------------------------------------------------------------------------------------------
function CreateAsteroid (
name, // name of asteroid
epochJD, // epoch of orbital elements as Julian Date
Nx, // longitude of the ascending node at epoch, in degrees
i, // inclination to the ecliptic, in degrees
w, // argument of perihelion, in degrees
a, // semi-major axis in AU
e, // eccentricity
Mx, // mean anomaly at epoch, in degrees
T, // orbital period, in days
amag ) // absolute magnitude
{
var day = epochJD - 2451543.5; // convert Julian Date to "0.0 January 2000" standard epoch day value.
var Mc = 360.0 / T; // "mean motion": how many degrees per day the body orbits around the Sun, on average.
var M0 = Angle.FixDegrees (Mx - Mc*day); // work backwards to figure out mean anomoly at standard epoch.
var N0 = Nx; //!!! FIXFIXFIX
var Nc = 0.0; //!!! FIXFIXFIX
var p = new PlanetPS (
name,
N0, Nc,
i, 0.0,
w, 0.0,
a, 0.0,
e, 0.0,
M0, Mc,
amag,
0.0,
0.0,
0.0
);
p.BodyType = 'asteroid'; // change body type from 'planet' to 'asteroid'
return p;
}
function CreateComet(/*see arguments for CreateAsteroid*/) {
// Pass along the same arguments to "CreateAsteroid".
var comet = CreateAsteroid.apply(this, arguments);
comet.BodyType = 'comet'; // change body type from 'asteroid' to 'comet'.
return comet;
}
function CreateMinor(/*see arguments for CreateAsteroid*/) {
// Pass along the same arguments to "CreateAsteroid".
var comet = CreateAsteroid.apply(this, arguments);
comet.BodyType = 'minor'; // indicate that this is a minor asteroid
return comet;
}
function CreatePlanetJPL( // Designed for use with JPL orbital data
name, // name of the object, e.g. "Mars"
ja0, jac, // semi-major axis (AU), rate (AU/century)
je0, jec, // eccentricity (1), (1/century)
jI0, jIc, // inclination (deg), (deg/century)
jL0, jLc, // mean longitude (deg), (deg/century)
ju0, juc, // longitude of perihelion (deg), (deg/century)
jQ0, jQc, // longitude of ascending node (deg), (deg/century)
magBase,
magPhaseFactor,
magNonlinearFactor,
magNonlinearExponent )
{
// Convert units and epoch from JPL format to PlanetPS format.
// http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf
// http://ssd.jpl.nasa.gov/txt/p_elem_t1.txt
var dpc = 36525.0; // days per century
var cofs = 1.5/dpc; // centuries that JPL epoch (J2000 = 1/1/2000 12UT) is ahead of PS (12/31/1999 0UT)
var N0 = jQ0 - cofs*jQc;
var Nc = jQc/dpc;
var i0 = jI0 - cofs*jIc;
var ic = jIc/dpc;
// w = ju - jQ
var w0 = Angle.FixDegrees((ju0 - cofs*juc) - (jQ0 - cofs*jQc));
var wc = (juc - jQc)/dpc;
var a0 = ja0 - cofs*jac;
var ac = jac/dpc;
var e0 = je0 - cofs*jec;
var ec = jec/dpc;
// M = jL - ju
var M0 = Angle.FixDegrees((jL0 - cofs*jLc) - (ju0 - cofs*juc));
var Mc = (jLc - juc)/dpc;
return new PlanetPS(
name, // name of the object, e.g. "Mars"
N0, Nc, // N0 = longitude of the ascending node (deg). Nc = rate of change in deg/day
i0, ic, // inclination to the ecliptic (deg)
w0, wc, // argument of perihelion (deg)
a0, ac, // semi-major axis, or mean distance from Sun (AU)
e0, ec, // eccentricity (0=circle, 0..1=ellipse, 1=parabola)
M0, Mc, // M0 = mean anomaly (deg) (0 at perihelion; increases uniformly with time). Mc ("mean motion") = rate of change in deg/day = 360/period
magBase,
magPhaseFactor,
magNonlinearFactor,
magNonlinearExponent
);
}
function CreateJupiter() {
var planet = CreatePlanetJPL(
"Jupiter",
5.20288700, -0.00011607, // ja0, jac = semi-major axis (AU), rate (AU/century)
0.04838624, -0.00013253, // je0, jec = eccentricity (1), (1/century)
1.30439695, 0.00183714, // jI0, jIc = inclination (deg), (deg/century)
34.39644051, 3034.74612775, // jL0, jLc = mean longitude (deg), (deg/century)
14.72847983, 0.21252668, // ju0, juc = longitude of perihelion (deg), (deg/century)
100.47390909, 0.20469106, // jQ0, jQc = longitude of ascending node (deg), (deg/century)
-9.25, 0.014, 0, 0 // visual magnitude elements)
);
planet.Perturb = PerturbMajorPlanet; // override the do-nothing method from PlanetPS
planet.PerturbEclipticLongitude = PerturbEclipticLongitude_Jupiter; // method called by PerturbMajorPlanet
planet.PerturbEclipticLatitude = PerturbEclipticLatitude_Jupiter; // method called by PerturbMajorPlanet
return planet;
}
function CreateSaturnJPL()
{
var planet = CreatePlanetJPL (
"SaturnJPL",
9.53667594, -0.00125060, // ja0, jac = semi-major axis (AU), rate (AU/century)
0.05386179, -0.00050991, // je0, jec = eccentricity (1), (1/century)
2.48599187, 0.00193609, // jI0, jIc = inclination (deg), (deg/century)
49.95424423, 1222.49362201, // jL0, jLc = mean longitude (deg), (deg/century)
92.59887831, -0.41897216, // ju0, juc = longitude of perihelion (deg), (deg/century)
113.66242448, -0.28867794, // jQ0, jQc = longitude of ascending node (deg), (deg/century)
-9.0, 0.044, 0, 0 // visual magnitude elements
);
planet.Perturb = PerturbMajorPlanet; // override the do-nothing method from PlanetPS
planet.PerturbEclipticLongitude = PerturbEclipticLongitude_Saturn; // method called by PerturbMajorPlanet
planet.PerturbEclipticLatitude = PerturbEclipticLatitude_Saturn; // method called by PerturbMajorPlanet
planet.__BaseVisualMagnitude = planet.VisualMagnitude; // we override the base function to also include a factor for the rings...
planet.VisualMagnitude = function (day)
{
var planetMagnitude = this.__BaseVisualMagnitude(day); // get magnitude of the planet body itself.
var Ir = 28.06; // inclination of Saturn's rings to ecliptic, in degrees
var cosIr = Angle.CosDeg (Ir);
var sinIr = Angle.SinDeg (Ir);
var gc = this.GeocentricCoordinates (day);
var Los = Angle.FixDegrees (Angle.AtanDeg2 (gc.y, gc.x));
var Las = Angle.FixDegrees (Angle.AtanDeg2 (gc.z, Math.sqrt(gc.x*gc.x + gc.y*gc.y)));
var sinLas = Angle.SinDeg (Las);
var cosLas = Angle.CosDeg (Las);
var Nr = 169.51 + (3.82e-5 * day); // ascending node of the plane of Saturn's rings
var sinLosMinusNr = Angle.SinDeg (Los - Nr);
var B = Math.asin (sinLas*cosIr - cosLas*sinIr*sinLosMinusNr);
var sinB = Math.abs (Math.sin (B)); // ??? can we get rid of doing both Asin and Sin?
var ringMagnitude = (-2.6 * Math.abs (sinB)) + (1.2 * sinB * sinB);
return planetMagnitude + ringMagnitude;
}
return planet;
}
function CreateSaturn()
{
var planet = new PlanetPS (
"Saturn",
113.6634, 2.3898e-5, 2.4886, -1.081e-7, 339.3939, 2.97661e-5, 9.55475, 0.0, 0.055546, -9.499e-9, 316.9670, 0.0334442282, // orbital elements
-9.0, 0.044, 0, 0 // visual magnitude elements
);
planet.Perturb = PerturbMajorPlanet; // override the do-nothing method from PlanetPS
planet.PerturbEclipticLongitude = PerturbEclipticLongitude_Saturn; // method called by PerturbMajorPlanet
planet.PerturbEclipticLatitude = PerturbEclipticLatitude_Saturn; // method called by PerturbMajorPlanet
planet.__BaseVisualMagnitude = planet.VisualMagnitude; // we override the base function to also include a factor for the rings...
planet.VisualMagnitude = function (day)
{
var planetMagnitude = this.__BaseVisualMagnitude(day); // get magnitude of the planet body itself.
var Ir = 28.06; // inclination of Saturn's rings to ecliptic, in degrees
var cosIr = Angle.CosDeg (Ir);
var sinIr = Angle.SinDeg (Ir);
var gc = this.GeocentricCoordinates (day);
var Los = Angle.FixDegrees (Angle.AtanDeg2 (gc.y, gc.x));
var Las = Angle.FixDegrees (Angle.AtanDeg2 (gc.z, Math.sqrt(gc.x*gc.x + gc.y*gc.y)));
var sinLas = Angle.SinDeg (Las);
var cosLas = Angle.CosDeg (Las);
var Nr = 169.51 + (3.82e-5 * day); // ascending node of the plane of Saturn's rings
var sinLosMinusNr = Angle.SinDeg (Los - Nr);
var B = Math.asin (sinLas*cosIr - cosLas*sinIr*sinLosMinusNr);
var sinB = Math.abs (Math.sin (B)); // ??? can we get rid of doing both Asin and Sin?
var ringMagnitude = (-2.6 * Math.abs (sinB)) + (1.2 * sinB * sinB);
return planetMagnitude + ringMagnitude;
}
return planet;
}
function CreateUranus()
{
var planet = CreatePlanetJPL(
"Uranus",
19.18916464, -0.00196176, // ja0, jac = semi-major axis (AU), rate (AU/century)
0.04725744, -0.00004397, // je0, jec = eccentricity (1), (1/century)
0.77263783, -0.00242939, // jI0, jIc = inclination (deg), (deg/century)
313.23810451, 428.48202785, // jL0, jLc = mean longitude (deg), (deg/century)
170.95427630, 0.40805281, // ju0, juc = longitude of perihelion (deg), (deg/century)
74.01692503, 0.04240589, // jQ0, jQc = longitude of ascending node (deg), (deg/century)
-7.15, 0.001, 0, 0 // visual magnitude elements
);
planet.Perturb = PerturbMajorPlanet; // override the do-nothing method from PlanetPS
planet.PerturbEclipticLongitude = PerturbEclipticLongitude_Uranus; // method called by PerturbMajorPlanet
planet.PerturbEclipticLatitude = PerturbEclipticLatitude_Uranus; // method called by PerturbMajorPlanet
return planet;
}
//----------------------------------------------------------------------------------------------
function MeanAnomalyOfSun (d)
{
return 356.0470 + (0.9856002585 * d);
}
function SunArgumentOfPerihelion (d)
{
return 282.9404 + (4.70935e-5 * d);
}
function MeanLongitudeOfSun (d)
{
return MeanAnomalyOfSun(d) + SunArgumentOfPerihelion(d);
}
function CreateMoon()
{
var moon = new PlanetPS (
"Moon",