-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.ts
More file actions
208 lines (186 loc) · 6.36 KB
/
index.ts
File metadata and controls
208 lines (186 loc) · 6.36 KB
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
import { Temporal } from "@js-temporal/polyfill";
import { d2r, r2d } from "./constants.js";
import coefficients from "./coefficients.js";
export interface AstroValue {
value: number;
speed: number;
}
export interface AstroData {
s: AstroValue;
h: AstroValue;
p: AstroValue;
N: AstroValue;
pp: AstroValue;
"90": AstroValue;
omega: AstroValue;
i: AstroValue;
I: AstroValue;
xi: AstroValue;
nu: AstroValue;
nup: AstroValue;
nupp: AstroValue;
"T+h-s": AstroValue;
P: AstroValue;
}
// Convert Date or Temporal.Instant to Temporal.Instant
const toInstant = (time: Date | Temporal.Instant): Temporal.Instant => {
if (time instanceof Temporal.Instant) {
return time;
}
return Temporal.Instant.fromEpochMilliseconds(time.getTime());
};
// Evaluates a polynomial at argument
const polynomial = (coefficients: number[], argument: number): number => {
const result: number[] = [];
coefficients.forEach((coefficient, index) => {
result.push(coefficient * Math.pow(argument, index));
});
return result.reduce((a, b) => a + b);
};
// Evaluates a derivative polynomial at argument
const derivativePolynomial = (coefficients: number[], argument: number): number => {
const result: number[] = [];
coefficients.forEach((coefficient, index) => {
result.push(coefficient * index * Math.pow(argument, index - 1));
});
return result.reduce((a, b) => a + b);
};
// Meeus formula 11.1
const T = (t: Date | Temporal.Instant): number => {
return (JD(t) - 2451545.0) / 36525;
};
// Meeus formula 7.1
const JD = (t: Date | Temporal.Instant): number => {
const instant = toInstant(t);
// Extract UTC components directly from epoch milliseconds to avoid expensive ZonedDateTime conversion
const ms = Number(instant.epochMilliseconds);
const date = new Date(ms);
let Y = date.getUTCFullYear();
let M = date.getUTCMonth() + 1;
const D =
date.getUTCDate() +
date.getUTCHours() / 24.0 +
date.getUTCMinutes() / (24.0 * 60.0) +
date.getUTCSeconds() / (24.0 * 60.0 * 60.0) +
date.getUTCMilliseconds() / (24.0 * 60.0 * 60.0 * 1e6);
if (M <= 2) {
Y = Y - 1;
M = M + 12;
}
const A = Math.floor(Y / 100.0);
const B = 2 - A + Math.floor(A / 4.0);
return Math.floor(365.25 * (Y + 4716)) + Math.floor(30.6001 * (M + 1)) + D + B - 1524.5;
};
const _I = (N: number, i: number, omega: number): number => {
N = d2r * N;
i = d2r * i;
omega = d2r * omega;
const cosI = Math.cos(i) * Math.cos(omega) - Math.sin(i) * Math.sin(omega) * Math.cos(N);
return r2d * Math.acos(cosI);
};
const _xi = (N: number, i: number, omega: number): number => {
N = d2r * N;
i = d2r * i;
omega = d2r * omega;
let e1 = (Math.cos(0.5 * (omega - i)) / Math.cos(0.5 * (omega + i))) * Math.tan(0.5 * N);
let e2 = (Math.sin(0.5 * (omega - i)) / Math.sin(0.5 * (omega + i))) * Math.tan(0.5 * N);
e1 = Math.atan(e1);
e2 = Math.atan(e2);
e1 = e1 - 0.5 * N;
e2 = e2 - 0.5 * N;
return -(e1 + e2) * r2d;
};
const _nu = (N: number, i: number, omega: number): number => {
N = d2r * N;
i = d2r * i;
omega = d2r * omega;
let e1 = (Math.cos(0.5 * (omega - i)) / Math.cos(0.5 * (omega + i))) * Math.tan(0.5 * N);
let e2 = (Math.sin(0.5 * (omega - i)) / Math.sin(0.5 * (omega + i))) * Math.tan(0.5 * N);
e1 = Math.atan(e1);
e2 = Math.atan(e2);
e1 = e1 - 0.5 * N;
e2 = e2 - 0.5 * N;
return (e1 - e2) * r2d;
};
// Schureman equation 224
const _nup = (N: number, i: number, omega: number): number => {
const I = d2r * _I(N, i, omega);
const nu = d2r * _nu(N, i, omega);
return (
r2d * Math.atan((Math.sin(2 * I) * Math.sin(nu)) / (Math.sin(2 * I) * Math.cos(nu) + 0.3347))
);
};
// Schureman equation 232
const _nupp = (N: number, i: number, omega: number): number => {
const I = d2r * _I(N, i, omega);
const nu = d2r * _nu(N, i, omega);
const tan2nupp =
(Math.sin(I) ** 2 * Math.sin(2 * nu)) / (Math.sin(I) ** 2 * Math.cos(2 * nu) + 0.0727);
return r2d * 0.5 * Math.atan(tan2nupp);
};
const modulus = (a: number, b: number): number => {
return ((a % b) + b) % b;
};
const astro = (time: Date | Temporal.Instant): AstroData => {
const instant = toInstant(time);
// This gets cast to `AstroData` later, but we build it up step by step here
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: any = {};
const polynomials: Record<string, number[]> = {
s: coefficients.lunarLongitude,
h: coefficients.solarLongitude,
p: coefficients.lunarPerigee,
N: coefficients.lunarNode,
pp: coefficients.solarPerigee,
"90": [90.0],
omega: coefficients.terrestrialObliquity,
i: coefficients.lunarInclination,
};
// Polynomials are in T, that is Julian Centuries; we want our speeds to be
// in the more convenient unit of degrees per hour.
const dTdHour = 1 / (24 * 365.25 * 100);
for (const name in polynomials) {
result[name] = {
value: modulus(polynomial(polynomials[name], T(instant)), 360.0),
speed: derivativePolynomial(polynomials[name], T(instant)) * dTdHour,
};
}
// Some other parameters defined by Schureman which are dependent on the
// parameters N, i, omega for use in node factor calculations. We don't need
// their speeds.
const functions: Record<string, (N: number, i: number, omega: number) => number> = {
I: _I,
xi: _xi,
nu: _nu,
nup: _nup,
nupp: _nupp,
};
Object.keys(functions).forEach((name) => {
const functionCall = functions[name];
result[name] = {
value: modulus(functionCall(result.N.value, result.i.value, result.omega.value), 360.0),
speed: null,
};
});
// We don't work directly with the T (hours) parameter, instead our spanning
// set for equilibrium arguments #is given by T+h-s, s, h, p, N, pp, 90.
// This is in line with convention.
const hour = {
value: (JD(instant) - Math.floor(JD(instant))) * 360.0,
speed: 15.0,
};
result["T+h-s"] = {
value: hour.value + result.h.value - result.s.value,
speed: hour.speed + result.h.speed - result.s.speed,
};
// It is convenient to calculate Schureman's P here since several node
// factors need it, although it could be argued that these
// (along with I, xi, nu etc) belong somewhere else.
result.P = {
value: result.p.value - (result.xi.value % 360.0),
speed: null,
};
return result as AstroData;
};
export default astro;
export { toInstant, polynomial, derivativePolynomial, T, JD, _I, _xi, _nu, _nup, _nupp };