-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputus.ts
1822 lines (1687 loc) · 55.1 KB
/
Computus.ts
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
/***********************************************************************
* Computus: Calendar and astronomical calculations.
* Computus is a javascript calendar class thath make calculations
* for perpetual calendar with easter an others moveable feast.
***********************************************************************/
/**
* Interface for Math polyfill and extensions
*/
declare interface Math {
frac: (x: number) => number;
dsin: (x: number) => number;
dcos: (x: number) => number;
dtan: (x: number) => number;
dasin: (x: number) => number;
dacos: (x: number) => number;
datan: (x: number) => number;
datan2: (x: number,y: number) => number;
dmod: (x: number) => number;
deg2rad: (x: number) => number;
rad2deg: (x: number) => number;
isNumber: (x: number) => number;
isPosInteger: (x: number) => number;
}
const abs = Math.abs;
const sign = Math.sign;
// sin in degrees (polyfill and alias)
Math.dsin = Math.dsin || function(d: number): number {
return Math.sin(d*Math.PI/180)
}
const dsin = Math.dsin;
// cos in degrees (polyfill and alias)
Math.dcos = Math.dcos || function(d: number): number {
return Math.cos(d*Math.PI/180)
}
const dcos = Math.dcos;
// tan in degrees (polyfill and alias)
Math.dtan = Math.dtan || function(d: number): number {
return Math.tan(d*Math.PI/180)
}
const dtan = Math.dtan;
// asin in degrees (polyfill and alias)
Math.dasin = Math.dasin || function(x: number): number {
return Math.asin(x)*180/Math.PI
}
const dasin = Math.dasin;
// acos in degrees (polyfill and alias)
Math.dacos = Math.dacos || function(x: number): number {
return Math.acos(x)*180/Math.PI
}
const dacos = Math.dacos;
// atan in degrees (polyfill and alias)
Math.datan = Math.datan || function(x: number): number {
return Math.atan(x)*180/Math.PI
}
const datan = Math.datan;
// atan2 in degrees (polyfill and alias)
Math.datan2 = Math.datan2 || function(y: number,x: number): number {
return Math.atan2(y,x)*180/Math.PI
}
const datan2 = Math.datan2;
// normalize degrees in range 0-360
Math.dmod = Math.dmod || function(d: number): number {
while(d > 360) d -= 360;
while(d < 0) d += 360;
return d; // in degrees
}
const dmod = Math.dmod;
Math.deg2rad = Math.deg2rad || function(d: number): number {
return (Math.PI * d / 180);
}
const deg2rad = Math.deg2rad;
Math.rad2deg = Math.rad2deg || function(r: number): number {
return (180 * r / Math.PI);
}
const rad2deg = Math.rad2deg;
const trunc = Math.trunc;
const round = Math.round;
const floor = Math.floor;
const ceil = Math.ceil;
// Return fractional part of number (as positive number)
/**
* Name: Math.frac
*
* Alias: frac
*
* Purpose: Get fractional part of number. The function Math.frac is defined
* in a way that
*
* x = Math.trunc(x) + Math.frac(x)
*
* for any x. If x < 0 then Math.frac(x) < 0.
*/
Math.frac = Math.frac || function(x: number): number {
if (Number.isInteger(x)) {
return 0;
}
else if (x>-1 && x<1) {
return x;
}
else {
return Number(x.toString().replace(/([\+\-]?)([0-9]*)(\.[0-9]*([Ee][\+\-]?[0-9]*)?)/,'$1$3'));
}
}
const frac = Math.frac;
Math.isNumber = Math.isNumber || function isNumber(n: number): boolean {
return typeof n === 'number' && isFinite(n);
}
const isNumber = Math.isNumber;
Math.isPosInteger = Math.isPosInteger || function(n: number): boolean {
n = Number(n);
return (Number.isInteger(n) && (n>=0))
}
const isPosInteger = Math.isPosInteger;
/**
* Computus Class: Calendar and astronomical calculations.
* It make calculations for perpetual calendar with easter an others moveable
* feast.
*/
type timeYMD = [number,number,number];
type moonMonth = [number,number,number,number];
class Computus {
/**
* Name: Computus.isleap
* @param {*} y year
* @returns true if is leap year in gregorian calendar, false otherwise.
*/
static isleap(y: number): number | boolean {
return ((y % 4 == 0) && (y % 100 != 0 || y % 400 == 0));
}
static yeardays(y: number): number {
return Computus.isleap(y)?366:365;
}
/**
* Name: Computus.ylshift
* Purpose: calculate amount of day shift in week cycle in [0...y] by leap years.
* @param {*} y
* @returns
*/
static ylshift(y: number): number {
return y + trunc(y/4) - trunc(y/100) + trunc(y/400);
}
/**
* Name: Computus.weekday
* @param {*} y year
* @param {*} m month
* @param {*} d date
* @returns weekday in gregorian calendar
*/
static weekday(y: number, m: number, d: number): number {
const t = [0,3,2,5,0,3,5,1,4,6,2,4];
return (Computus.ylshift((m < 2)?y-1:y) + t[m] + d) % 7;
}
/**
* Name: Computus.domlet
* @param d
* @returns
*/
static domlet(d: number): string {
while (d<0) d+= 7;
d = 7 - (d % 7);
return String.fromCharCode(65+(d<7?d:0));
}
/**
* Name: Computus.dominical
* @param {*} y
* @returns dominical letter from year in gregorian calendar.
*/
static dominical(y: number): string {
let l = Computus.isleap(y);
let i = ( Computus.ylshift(y) - 1 ) % 7 + (l?0:1);
return Computus.domlet(i) + (l?Computus.domlet(i+1):"") ;
}
/**
* Name: Computus.monthlength
*
* Meaning: Number of days in [i][0] each month, [i][1] cumulative at
* start of each month (non-leap year), [i][2] cumulative at start of
* each month (leap year).
*/
static monthlength: Array<[number,number,number]> = [
[31, 0, 0],
[28, 31, 31],
[31, 59, 60],
[30, 90, 91],
[31,120,121],
[30,151,152],
[31,181,182],
[31,212,213],
[30,243,244],
[31,273,274],
[30,304,305],
[31,334,335],
];
/**
* Name: Computus.monthdays
* @param {*} y year
* @param {*} m month
* @returns days in month
*/
static monthdays(y: number, m: number): number {
return (((m==1)&&Computus.isleap(y))?Computus.monthlength[m][0]+1:Computus.monthlength[m][0])
}
/**
* Name: Computus.cal2doy
* @param {*} y year
* @param {*} m month
* @param {*} d date
* @returns days past from start of year
*/
static cal2doy(y: number, m: number, d: number): number {
return trunc((275 * (m+1))/9) - (Computus.isleap(y)?1:2) * trunc((m+10)/12) + d - 30;
//return Computus.monthlength[m][Computus.isleap(y)?2:1]+d;
}
/**
* Name: Computus.doy2cal
* @param y year
* @param d days past from start of year
* @returns date in calendar
*/
static doy2cal(y: number, d: number): timeYMD {
let leap = Computus.isleap(y) as number;
let i: number;
for (i=0; i<Computus.monthlength.length; i++) {
if (d<=Computus.monthlength[i][leap+1]) break;
}
return [y,i-1,d-Computus.monthlength[i-1][leap+1]];
}
/**
* Name: Computus.calweek2doy
* @param y year
* @param m month
* @param w weekday
* @param n nth weekday of month (0 - 1st, 1 - 2nd, 2 - 3rd, ...)
* @returns days past from start of year
*/
static calweek2doy(y: number, m: number, w: number, n: number): number {
const msw = Computus.weekday(y,m,1); // weekday of month first day
return Computus.cal2doy(y,m,1) + 7 * (n+((msw>w)?1:0)) + w - msw;
}
/**
* Name: Computus.cal2foy
* @param {*} y year
* @param {*} m month
* @param {*} d date
* @returns fraction past start of year
*/
static cal2foy(y: number, m: number, d: number): number {
return Computus.cal2doy(y,m,d)/(Computus.yeardays(y));
}
/**
* Name: weekTable
*
* Purpose: Array containing name of week days in some languages.
* Source:
* - https://en.wikipedia.org/wiki/Week
* - https://en.wikipedia.org/wiki/Names_of_the_days_of_the_week
*/
static weekTable: {[k: string]: Array<any>} = {
EN :
[["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
["S", "M", "T", "W", "T", "F", "S"]],
PT :
[["Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"],
["Dom", "2ª", "3ª", "4ª", "5ª", "6ª", "Sáb"],
["D", "S", "T", "Q", "Q", "S", "S"]],
ES :
[["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"]],
IT :
[["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"]],
FR :
[["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"]],
DE :
[["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]],
LA :
[["dies Sōlis", "dies Lūnae", "dies Martis", "dies Mercuriī", "dies Iovis", "dies Veneris", "dies Saturnī"]],
HE :
[["ראשון", "שני", "שלישי", "רביעי", "חמישי", "שישי", "שבת"]],
EL :
[["ἡμέρα Ἡλίου", "ἡμέρα Σελήνης", "ἡμέρα Ἄρεως", "ἡμέρα Ἑρμοῦ", "ἡμέρα Διός", "ἡμέρα Ἀφροδίτης", "ἡμέρα Κρόνου"]],
Greek :
[["hēmérā Hēlíou", "hēmérā Selḗnēs", "hēmérā Áreōs", "hēmérā Hermoû", "hēmérā Diós", "hēmérā Aphrodī́tēs","hēmérā Krónou"]],
Hebrew :
[["rishon", "sheyni", "shlishi", "revi'i", "khamishi", "shishi", "Shabbat"]],
Eclesiastical :
[["Dominica", "feria secunda", "feria tertia", "feria quarta", "feria quinta", "feria sexta", "sabbatum"],
["A", "G", "F", "E", "D", "C", "B"]],
Planet :
[["Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn"],
["☉", "☽", "♂", "☿", "♃", "♀", "♄"]],
GrecoRoman :
[["Helios", "Selene", "Ares", "Hermes", "Zeus", "Aphrodite", "Cronus"],
["Sol", "Luna", "Mars", "Mercury", "Jupiter", "Venus", "Saturn"]],
Germanica :
[["Sun", "Moon", "Tiwaz", "Wodanaz", "Þunraz", "Frige", "—"]],
English :
[["sunnandæg", "mōnandæg", "tiwesdæg", "wōdnesdæg", "þunresdæg", "frīgedæg", "sæterndæg"]],
};
/**
* Name: monthTable
*
* Purpose: Array containing name of months in some languages.
*/
static monthTable: {[k: string]: Array<any>} = {
EN:
[["January","February","March","April","May","June","July","August","September","October","November","December"],
["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]],
PT:
[["Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro"],
["Jan","Fev","Mar","Abr","Mai","Jun","Jul","Ago","Set","Out","Nov","Dez"]],
ES:
[["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"]],
IT:
[["Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","December"]],
FR:
[["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"]],
DE:
[["Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"]],
LA:
[["Ianuarii", "Februarii","Martius","Aprilis","Maii","June","Iulii","August","Septembris","Octobris","November","Decembris"]],
HE:
[["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]],
EL:
[["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]],
};
/**
* Name: moonphasename
*
* Purpose: Array containing name of Moon's true phase in some languages.
*/
static moonphasename: {[k: string]: Array<string>} = {
EN:
["New Moon", "First Quarter", "Full Moon", "Last Quarter"],
PT:
["Lua Nova", "Quarto Crescente", "Lua Cheia", "Quarto Minguante"],
ES:
["Luna Nueva", "Cuarto Creciente", "Luna Llena", "Cuarto Menguante"],
IT:
["Luna Nuova", "Primo Quarto", "Luna Piena", "Ultimo Quarto"],
FR:
["Nouvelle lune", "Premier Quart", "Pleine Lune", "Dernier Quart"],
DE:
["Neumond", "Erstes Viertel", "Vollmond", "Letztes Viertel"],
LA:
["Neomenia", "Primum Quartum", "Plena luna", "Novissime Quartum"],
HE:
["ירח חדש", "רבע ראשון", "ירח מלא", "רבע אחרון"],
EL:
["νέα Σελήνη", "Πρώτο τέταρτο", "Πανσέληνος", "τελευταίο τέταρτο"],
};
/**
* Name: Computus.saturnaliaFinis
*
* Meaning: Carnival finnish (-47)
*/
static saturnaliaFinis = -(6*7+5);
/**
* Name: Computus.mercuriiCinereo
*
* Meaning: Ash Wednesday (-46)
*/
static mercuriiCinereo = -(6*7+4);
/**
* Name: Computus.solisPalmarum
*
* Meaning: Palm Sunday (-7)
*/
static solisPalmarum = -(0*7+7);
/**
* Name: Computus.pentaecoste
*
* Meaning: Pentecostes (+49)
*/
static pentaecoste = +(7*7+0);
/**
* Name: Computus.corpusDomini
*
* Meaning: Chorpus Christi (+60)
*/
static corpusDomini = +(8*7+4);
/**
* Name: Computus.easterSunday
*
* Purpose: Calculate Easter Sunday for a given year.
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 67–68.
* @param {*} year
* @returns Easter Sunday date.
*/
static easterSunday(year: number): timeYMD {
let n, c, u, s, t, p, q, e, b, d, m, j, L, h;
n = year % 19; // Metonic cycle
c = trunc(year / 100); // number of centuries completed
u = year % 100; // past years beyond complete centuries
s = trunc(c / 4); // number of completed leap centuries Gregorian cycles (400 year cycles, 97 leap years)
t = c % 4; // centuries beyond complete leap cycles
p = trunc((c + 8) / 25); // proemptosis cycles completed
q = trunc((c - p + 1) / 3); // proemptosis passed beyond full cycles
e = (19 * n + c - s - q + 15) % 30; // epacta
b = trunc(u / 4); // number of leap years Julian cycles completed
d = u % 4; // years beyond the Julian cycles of complete leap years
L = (32 + 2 * t + 2 * b - e - d) % 7; // dominical letter
h = trunc((n + 11 * e + 22 * L) / 451); // correction
m = trunc((e + L - 7 * h + 114) / 31) - 1; //
j = 1 + (e + L - 7 * h + 114) % 31; //
return [year,m,j];
}
/**
* Name: Computus.cal2jd
* @param {*} year
* @param {*} month
* @param {*} date
* @param {*} hours a number representing fraction of day
* @returns Julian day from gregorian calendar date
*/
static cal2jd(year: number,month: number,date: number,hours?: number): number {
let my = trunc((month-13)/12);
return trunc((1461 * (year + my + 4800))/4)
+ trunc((367 * (month - 1 - 12 * my))/12)
- trunc(3/4 * trunc(((year + my + 4900)/100)))
+ date - 32075.5 + ((hours!==undefined)?hours:0);
}
/**
* Name: Computus.jd2cal
*
* Purpose: Convert Julian day to date in gregorian calendar
* @param {*} jd Julian day
* @returns
*/
static jd2cal(jd: number) {
let l,n,i,k,d,m,y;
l = trunc(jd+0.5) + 68569;
n = trunc((4 * l) / 146097);
l -= trunc((146097 * n + 3) / 4);
i = trunc((4000 * (l + 1)) / 1461001);
l -= trunc((1461 * i) / 4) - 31;
k = trunc((80 * l) / 2447);
d = trunc(l - (2447 * k) / 80);
l = trunc(k / 11);
m = k + 2 - 12 * l;
y = 100 * (n - 49) + i + l;
return [y, m-1, d+1, frac(jd-0.5)]
// return {'year':y, 'month':m-1, 'date':d+1, 'hours':frac(jd-0.5)};//verificar frac
}
/**
* Name: Computus.jd2weekday
* @param {*} jd Julian day
* @returns weekday from Julian day
*/
static jd2weekday(jd: number): number {
return ((jd + 1.5) % 7);
}
/**
* Name: Computus.jCentury
*
* Meaning: Number of days in a Julian century. (36525)
*/
static jCentury = 100 * (365+1/4);
/**
* Name: Computus.gCentury
*
* Meaning: Number of days in a gregorian century. (36524.25)
*/
static gCentury = 100 * (365+97/400);
/**
* Name: Computus.j2000Era
*
* Meaning: Julian day of J2000 epoch.
*
* Note that if we make Computus.j2000Era / Computus.jCentury the rusult is 67.11964407939767
* It means that JD=0 is at 67.11964407939767 Julian centuries before J2000
*/
static j2000Era = 2451545.0;
/**
* Name: Computus.mjdEra
*
* Meaning: Modified Julian Date epoch. (2400000.5)
*/
static mjdEra = 2400000.5;
/**
* Name: Computus.unixEra
*
* Meaning: Julian Day of 00:00:00 UTC on 1 January 1970.
* UNIX Era is exactly 3/10 of a Julian century before J2000
*/
static unixEra = Computus.j2000Era - 3/10 * Computus.jCentury;
/**
* Name: Computus.jd2jc
*
* Purpose: convert Julian Day to centuries since J2000.0
* @param {*} jd Julian day to convert
* @returns T value in Julian centuries corresponding to the given Julian Day
*/
static jd2jc(jd: number): number {
return ((jd - Computus.j2000Era) / Computus.jCentury);
}
/**
* Name: Computus.jc2jd
*
* Purpose: convert centuries since J2000.0 to Julian jay.
* @param {*} T number of Julian centuries since J2000.0
* @returns the Julian Day corresponding to the T value
*/
static jc2jd(T: number): number {
return (T * Computus.jCentury + Computus.j2000Era);
}
/**
* Name: Computus.daymin
*
* Meaning: Number of minutes in a day
*/
static daymin = 24 * 60;
/**
* Name: Computus.dayms
*
* Meaning: Number of milliseconds in a day (millisecond is ANSI C time_t resolution)
*/
static dayms = 24 * 60 * 60 * 1e3;
/**
* Name: Computus.unix2jd
*
* Purpose: Convert UNIX timestamp to Julian jay
* @param time UNIX timestamp
* @returns Julian day
*/
static unix2jd(time: number): number {
return time / Computus.dayms + Computus.unixEra;
}
/**
* Name: Computus.unix2jd
*
* Purpose: Convert Julian day to UNIX timestamp
* @param jd Julian day
* @returns UNIX timestamp
*/
static jd2unix(jd: number): number {
return (jd - Computus.unixEra) * Computus.dayms;
}
/**
* Name: Computus.cardinal
*
* Meaning: Array containing cardinal directions symmbols.
*
* Source: https://en.wikipedia.org/wiki/Points_of_the_compass
*/
static cardinal = [
[
"N", "NNE", "NE", "ENE",
"E", "ESE", "SE", "SSE",
"S", "SSO", "SO", "OSO",
"O", "ONO", "NO", "NNO",
"N"
],
[
"N", "NNE", "NE", "ENE",
"E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW",
"W", "WNW", "NW", "NNW",
"N"
]
];
/**
* ========== Sun's functions ==========
*/
static polyT(d: Array<number>, T: number): number {
let r = 0;
for (let i=0, t=1; i<d.length; i++, t *= T) {
r += d[i] * t;
}
return r;
}
/**
* Name: Computus.azcenter
*
* Purpose: centralize azimutal coordinate around south or north direction
* (northern or south hemisphere perspective of Sun's sky motion,
* respectively), depending of latitude.
* @param az azimuth
* @param lat latitude
* @returns azimuth centered around 180º (lat>0) or centered around 0º (lat<0)
*/
static azcenter(az: number, lat: number): number {
return (lat<0 && az>180)?(az-360):az;
}
/**
* Name: Computus.tropicalYear
*
* Purpose: calculate duration of tropical year valid for a period
* centered 8000 year around J2000 (denoted as \tau).
*
* Source: Borkowski, K. M., The Tropical Year and Solar Calendar,
* Journal of the Royal Astronomical Society of Canada, Vol. 85,
* NO. 3/JUN, P.121, 1991.
*
* URL: https://articles.adsabs.harvard.edu//full/1991JRASC..85..121B/0000121.000.html
* @param T number of Julian centuries since J2000.0
* @returns duration of tropical year in days of 86400 SI seconds
*/
static tropicalYear(T: number): number {
let tau = 365.242189669781 + T * (- 6.161870e-6 - 6.44e-10 * T);
return tau;
}
/**
* Name: Computus.meanLongSun
*
* Purpose: calculate the geometric mean longitude of the Sun (denoted
* as L_0) referred to the dynamic equinox.
*
* Source: Borkowski, K. M., The Tropical Year and Solar Calendar,
* Journal of the Royal Astronomical Society of Canada, Vol. 85,
* NO. 3/JUN, P.121, 1991.
*
* URL: https://articles.adsabs.harvard.edu//full/1991JRASC..85..121B/0000121.000.html
* @param {*} T number of Julian centuries since J2000.0
* @returns the geometric mean longitude of the Sun in degrees
*/
static geomMeanLongSun_borkowski(T: number): number {
let L_0 = dmod(280.4664485 + T * (0.7698231361137005 + T * (1.093241/3600 + 0.0000762/3600 * T) )); // in degrees
return L_0;
}
/**
* Name: Computus.calGeomMeanLongSun
*
* Purpose: calculate the geometric mean longitude of the Sun (denoted
* as L_0) referred to the mean equinox of the date.
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 163.
* @param {*} T number of Julian centuries since J2000.0
* @returns the geometric mean longitude of the Sun in degrees
*/
static geomMeanLongSun(T: number): number {
let L_0 = dmod(280.46646 + T * (36000.76983 + 0.0003032 * T)); // in degrees
return L_0;
}
/**
* Name: Computus.calGeomAnomalySun
*
* Purpose: calculate the Geometric Mean Anomaly of the Sun (denoted as M).
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 163.
* @param {*} T number of Julian centuries since J2000.0
* @returns the Geometric Mean Anomaly of the Sun in degrees
*/
static geomMeanAnomalySun(T: number): number {
let M = (357.52911 + T * (35999.05029 - 0.0001537 * T)); // in degrees
return M;
}
/**
* Name: Computus.eccentricityEarthOrbit
*
* Purpose: calculate the eccentricity of earth's orbit (denoted as e).
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 163.
* @param {*} T number of Julian centuries since J2000.0
* @returns the unitless eccentricity
*/
static eccentricityEarthOrbit(T: number): number {
let e = 0.016708634 - T * (0.000042037 + 0.0000001267 * T);
return e; // unitless
}
/**
* Name: Computus.sunEqOfCenter
*
* Purpose: calculate the equation of center for the Sun (denoted as C).
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 164.
* @param {*} T number of Julian centuries since J2000.0
* @returns in degrees
*/
static sunEqOfCenter(T: number): number {
let M = Computus.geomMeanAnomalySun(T);
let C = dsin(M) * (1.914602 - T * (0.004817 + 0.000014 * T)) + dsin(2*M) * (0.019993 - 0.000101 * T) + dsin(3*M) * 0.000289; // in degrees
return C;
}
/**
* Name: Computus.sunTrueLong
*
* Purpose: calculate the true longitude of the Sun (denoted as \odot).
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 164.
* @param {*} T number of Julian centuries since J2000.0
* @returns Sun's true longitude in degrees
*/
static sunTrueLong(T: number): number {
let L_0 = Computus.geomMeanLongSun(T);
let C = Computus.sunEqOfCenter(T);
let O = L_0 + C;
return O; // in degrees
}
/**
* Name: Computus.sunTrueAnomaly
*
* Purpose: calculate the true anamoly of the Sun (denoted as \nu).
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 164.
* @param {*} T number of Julian centuries since J2000.0
* @returns Sun's true anamoly in degrees
*/
static sunTrueAnomaly(T: number): number {
let M = Computus.geomMeanAnomalySun(T);
let C = Computus.sunEqOfCenter(T);
let nu = M + C;
return nu; // in degrees
}
/**
* Name: Computus.sunRadVector
*
* Purpose: calculate the distance to the Sun in AU (denoted as R).
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 164.
* @param {*} T number of Julian centuries since J2000.0
* @returns Sun radius vector in AUs
*/
static sunRadVector(T: number): number {
let nu = Computus.sunTrueAnomaly(T);
let e = Computus.eccentricityEarthOrbit(T);
let R = (1.000001018 * (1 - e * e)) / (1 + e * dcos(nu));
return R; // in AUs
}
/**
* Name: Computus.AU
*
* Meaning: Astronomic Unit in kilometers
*/
static AU = 149597870.7;
/**
* Name: Computus.sunRadius
* Meaning: Sun radius in kilometers
*/
static sunRadius = 695700;
/**
* Name: Computus.sunAperture
*
* Purpose: Calculate solar disk aperture angle
* @param {*} T number of Julian centuries since J2000.0
* @returns solar disk angle in degrees
*/
static sunAperture(T: number): number {
let alpha = datan2(Computus.sunRadius,Computus.sunRadVector(T)*Computus.AU)
return alpha;
}
/**
* Name: Computus.sunApparentLong
*
* Purpose: calculate the apparent longitude of the Sun referred to the
* true equinox of date.
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 164.
* @param {*} T number of Julian centuries since J2000.0
* @returns Sun's apparent longitude in degrees
*/
static sunApparentLong(T: number): number {
let Omega = 125.04 - 1934.136 * T;
let lambda = Computus.sunTrueLong(T) - 0.00569 - 0.00478 * dsin(Omega);
return lambda; // in degrees
}
/**
* Name: Computus.meanObliquityOfEcliptic
*
* Purpose: calculate the mean obliquity of the ecliptic.
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 147.
* @param {*} T number of Julian centuries since J2000.0
* @returns mean obliquity in degrees
*/
static meanObliquityOfEcliptic(T: number): number {
let seconds = 21.448 - T*(46.8150 + T*(0.00059 - T*(0.001813)));
let epsilon_0 = 23 + (26 + (seconds/60))/60;
return epsilon_0; // in degrees
}
/**
* Name: Computus.obliquityCorrection
*
* Purpose: calculate the corrected obliquity of the ecliptic.
*
* Source: NOAA Sun Calculator.
* @param {*} T number of Julian centuries since J2000.0
* @returns corrected obliquity in degrees
*/
static obliquityCorrection(T: number): number {
let epsilon_0 = Computus.meanObliquityOfEcliptic(T);
let Omega = 125.04 - 1934.136 * T;
let epsilon = epsilon_0 + 0.00256 * dcos(Omega);
return epsilon; // in degrees
}
/**
* Name: Computus.sunRtAscension
*
* Purpose: calculate the right ascension of the Sun.
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 165.
* @param {*} T number of Julian centuries since J2000.0
* @returns Sun's right ascension in degrees
*/
static sunRtAscension(T: number): number {
let e = Computus.obliquityCorrection(T);
let lambda = Computus.sunApparentLong(T);
let alpha = datan2(dcos(e) * dsin(lambda), dcos(lambda));
return alpha; // in degrees
}
/**
* Name: Computus.sunDeclination
*
* Purpose: Calculate the declination of the Sun (denoted as \delta).
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 165.
* @param {*} T number of Julian centuries since J2000.0
* @returns Sun's declination in degrees
*/
static sunDeclination(T: number): number {
let epsilon = Computus.obliquityCorrection(T);
let lambda = Computus.sunApparentLong(T);
let delta = dasin(dsin(epsilon) * dsin(lambda));
return delta; // in degrees
}
/**
* Name: Computus.equationOfTime
*
* Purpose: Calculate the difference between true solar time and mean
* solar time.
*
* Source: Jean Meeus, Astronomical Algorithms ; Richmond (Virginia,
* États-Unis), Willmann-Bell, 1998, pp. 185.
* @param {*} T number of Julian centuries since J2000.0
* @returns equation of time in minutes of time
*/
static equationOfTime(T: number): number {
let epsilon = Computus.obliquityCorrection(T);
let L_0 = Computus.geomMeanLongSun(T);
let e = Computus.eccentricityEarthOrbit(T);
let M = Computus.geomMeanAnomalySun(T);
let y = dtan(epsilon/2)**2;
let E = y * dsin(2*L_0) - 2 * e * dsin(M) + 4 * e * y * dsin(M) * dcos(2*L_0) - 0.5 * y ** 2 * dsin(4*L_0) - 1.25 * e ** 2 * dsin(2*M);
return rad2deg(E) * Computus.daymin/360; // in minutes of time
}
/**
* Name: Computus.hourAngle
*
* Purpose: Calculate the hour angle for the given location, decl, and
* time of day.
* @param {*} time
* @param {*} lng
* @param {*} eqtime
* @returns
*/
static hourAngle(time: number, lng: number, eqtime: number): number {
return (15*(time + (lng/15) - (eqtime/60)));
// in degrees
}
/**
* Name: Computus.hourAngleSunrise
*
* Purpose: Calculate the hour angle of the Sun at sunrise for the
* latitude.
* @param {*} lat latitude of observer in degrees
* @param {*} solarDec declination angle of Sun in degrees
* @returns hour angle of sunrise in degrees
*/
static hourAngleSunrise(lat: number, solarDec: number): number {
return dacos(dcos(90.833)/(dcos(lat)*dcos(solarDec))-dtan(lat)*dtan(solarDec)); // in degrees
}
/**
* Name: Computus.solarTimeUTC
*
* Purpose: Calculate the Universal Coordinated Time (UTC) of solar time
* for the given day at the given location on earth. Solar time is
* specified as fraction of day (0 = solar noon)
* @param {*} JD Julian day
* @param {*} lng longitude of observer in degrees (West=- East=+)
* @param {*} time Solar time as fraction of day (-0.5 < time < 0.5; 0 = solar noon).
* @returns
*/
static solarTimeUTC(JD: number, lng: number, time: number): number {
const eps = 1e-9;
JD = round(JD);
// First pass uses approximate solar time to calculate equation of time
let Ttime = Computus.jd2jc(JD + time - lng/360);
let eqtime = Computus.equationOfTime(Ttime);
let soltime = Computus.daymin * (time+0.5) - Computus.daymin/360 * lng - eqtime; // min
let lasttime;
do {
lasttime = soltime;
Ttime = Computus.jd2jc(JD - 0.5 + soltime/Computus.daymin);
eqtime = Computus.equationOfTime(Ttime);
soltime = Computus.daymin * (time+0.5) - Computus.daymin/360 * lng - eqtime; // min
} while (abs(abs(lasttime)-abs(soltime)) > eps)
return soltime;
}
/**
* Name: Computus.sunRiseNoonSetUTC
*
* Purpose: Calculate the Universal Coordinated Time (UTC) of sunrise for
* the given day at the given location on earth.
* @param {*} JD Julian day
* @param {*} lat latitude of observer in degrees
* @param {*} lng longitude of observer in degrees
* @param {*} risenoonset -1 for rise 1 for set, 0 for noon
* @returns time in minutes from zero Z
*/
static sunRiseNoonSetUTC(JD: number, lat: number, lng: number, risenoonset: number): number {
const eps = 1e-9;
JD = round(JD);
// Find the time of solar noon at the location, and use
// that declination. This is better than start of the
// Julian day
// First pass to approximate sunrise (using solar noon)
// Second pass includes fractional jday in iterative gamma calc.