1use std::f64::consts::TAU;
9use std::iter::zip;
10
11use fast_polynomial::poly_array;
12use glam::DMat3;
13use lox_core::f64::consts::{SECONDS_PER_DAY, SECONDS_PER_HALF_DAY};
14use lox_core::units::{Angle, AngleUnits};
15use lox_test_utils::ApproxEq;
16use lox_time::time_scales::{Tdb, Tt, Ut1};
17use lox_time::{Time, julian_dates::JulianDate};
18
19use crate::iers::ecliptic::MeanObliquity;
20use crate::iers::fundamental::iers03::{
21 d_iers03, earth_l_iers03, f_iers03, l_iers03, lp_iers03, omega_iers03, pa_iers03,
22 venus_l_iers03,
23};
24use crate::iers::nutation::Nutation;
25use crate::iers::precession::PrecessionCorrectionsIau2000;
26use crate::iers::{Corrections, Iau2000Model, ReferenceSystem};
27
28mod complementary_terms;
29
30impl ReferenceSystem {
31 pub fn earth_rotation(&self, tt: Time<Tt>, ut1: Time<Ut1>, corr: Corrections) -> DMat3 {
33 self.greenwich_apparent_sidereal_time(tt, ut1, corr)
34 .0
35 .rotation_z()
36 }
37
38 pub fn greenwich_mean_sidereal_time(
40 &self,
41 tt: Time<Tt>,
42 ut1: Time<Ut1>,
43 ) -> GreenwichMeanSiderealTime {
44 match self {
45 ReferenceSystem::Iers1996 => GreenwichMeanSiderealTime::iau1982(ut1),
46 ReferenceSystem::Iers2003(_) => GreenwichMeanSiderealTime::iau2000(tt, ut1),
47 ReferenceSystem::Iers2010 => GreenwichMeanSiderealTime::iau2006(tt, ut1),
48 }
49 }
50
51 pub fn greenwich_apparent_sidereal_time(
53 &self,
54 tt: Time<Tt>,
55 ut1: Time<Ut1>,
56 corr: Corrections,
57 ) -> GreenwichApparentSiderealTime {
58 if corr.is_zero() {
59 return match self {
60 ReferenceSystem::Iers1996 => Gast::iau1994(ut1),
61 ReferenceSystem::Iers2003(model) => match model {
62 Iau2000Model::A => Gast::iau2000a(tt, ut1),
63 Iau2000Model::B => Gast::iau2000b(tt, ut1),
64 },
65 ReferenceSystem::Iers2010 => Gast::iau2006a(tt, ut1),
66 };
67 };
68 let gmst = self.greenwich_mean_sidereal_time(tt, ut1);
69 let tdb = tt.with_scale(Tdb);
70 let rpb = self.bias_precession_matrix(tt);
71 let epsa = self.mean_obliquity(tt);
72 let mut nut = self.nutation(tdb);
73 let ecl_corr = self.ecliptic_corrections(corr, nut, epsa, rpb);
74 nut += ecl_corr;
75 match self {
76 ReferenceSystem::Iers1996 => {
77 let ee = EquationOfTheEquinoxes::iau1994(tdb);
78 GreenwichApparentSiderealTime(
79 (gmst.0 + ee.0 + epsa.0.cos() * ecl_corr.0).mod_two_pi(),
80 )
81 }
82 ReferenceSystem::Iers2003(_) => {
83 let ee = EquationOfTheEquinoxes::iau2000(tt, epsa.0, nut.dpsi);
84 GreenwichApparentSiderealTime((gmst.0 + ee.0).mod_two_pi())
85 }
86 ReferenceSystem::Iers2010 => {
87 let ee = EquationOfTheEquinoxes::iau2000(tt, epsa.0, nut.dpsi);
88 GreenwichApparentSiderealTime((gmst.0 + ee.0).mod_two_pi())
89 }
90 }
91 }
92}
93
94#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, ApproxEq)]
96pub struct EarthRotationAngle(pub Angle);
97
98impl EarthRotationAngle {
99 pub fn iau2000(time: Time<Ut1>) -> Self {
101 let d = time.days_since_j2000();
102 let f = d.rem_euclid(1.0); Self(
104 (TAU * (f + 0.7790572732640 + 0.00273781191135448 * d))
105 .rad()
106 .mod_two_pi(),
107 )
108 }
109}
110
111pub type Era = EarthRotationAngle;
113
114#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, ApproxEq)]
116pub struct GreenwichMeanSiderealTime(pub Angle);
117
118pub type Gmst = GreenwichMeanSiderealTime;
120
121const A: f64 = 24110.54841 - SECONDS_PER_HALF_DAY;
123const B: f64 = 8640184.812866;
124const C: f64 = 0.093104;
125const D: f64 = -6.2e-6;
126
127impl GreenwichMeanSiderealTime {
128 pub fn iau1982(time: Time<Ut1>) -> Self {
130 let t = time.centuries_since_j2000();
131 let f = time.days_since_j2000().rem_euclid(1.0) * SECONDS_PER_DAY;
132 Self(Angle::from_hms(0, 0, poly_array(t, &[A, B, C, D]) + f).mod_two_pi())
133 }
134
135 pub fn iau2000(tt: Time<Tt>, ut1: Time<Ut1>) -> Self {
137 let t = tt.centuries_since_j2000();
138 Self(
139 EarthRotationAngle::iau2000(ut1).0
140 + Angle::arcseconds(poly_array(
141 t,
142 &[0.014506, 4612.15739966, 1.39667721, -0.00009344, 0.00001882],
143 ))
144 .mod_two_pi(),
145 )
146 }
147
148 pub fn iau2006(tt: Time<Tt>, ut1: Time<Ut1>) -> Self {
150 let t = tt.centuries_since_j2000();
151 Self(
152 EarthRotationAngle::iau2000(ut1).0
153 + Angle::arcseconds(poly_array(
154 t,
155 &[
156 0.014506,
157 4612.156534,
158 1.3915817,
159 -0.00000044,
160 -0.000029956,
161 -0.0000000368,
162 ],
163 ))
164 .mod_two_pi(),
165 )
166 }
167}
168
169#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, ApproxEq)]
171pub struct GreenwichApparentSiderealTime(pub Angle);
172
173pub type Gast = GreenwichApparentSiderealTime;
175
176impl GreenwichApparentSiderealTime {
177 pub fn iau1994(time: Time<Ut1>) -> Self {
179 Self(
180 (GreenwichMeanSiderealTime::iau1982(time).0
181 + EquationOfTheEquinoxes::iau1994(time.with_scale(Tdb)).0)
182 .mod_two_pi(),
183 )
184 }
185
186 pub fn iau2000a(tt: Time<Tt>, ut1: Time<Ut1>) -> Self {
188 Self(
189 (GreenwichMeanSiderealTime::iau2000(tt, ut1).0
190 + EquationOfTheEquinoxes::iau2000a(tt).0)
191 .mod_two_pi(),
192 )
193 }
194
195 pub fn iau2000b(tt: Time<Tt>, ut1: Time<Ut1>) -> Self {
197 Self(
198 (GreenwichMeanSiderealTime::iau2000(tt, ut1).0
199 + EquationOfTheEquinoxes::iau2000b(tt).0)
200 .mod_two_pi(),
201 )
202 }
203
204 pub fn iau2006a(tt: Time<Tt>, ut1: Time<Ut1>) -> Self {
206 Self(
207 (GreenwichMeanSiderealTime::iau2006(tt, ut1).0
208 + EquationOfTheEquinoxes::iau2006a(tt).0)
209 .mod_two_pi(),
210 )
211 }
212}
213
214#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, ApproxEq)]
216pub struct EquationOfTheEquinoxes(pub Angle);
217
218impl EquationOfTheEquinoxes {
219 pub fn iau1994(time: Time<Tdb>) -> Self {
221 let t = time.centuries_since_j2000();
222 let om = (Angle::arcseconds(poly_array(t, &[450160.280, -482890.539, 7.455, 0.008]))
223 + Angle::new((-5.0 * t).rem_euclid(1.0) * TAU))
224 .mod_two_pi();
225 let Nutation { dpsi, .. } = Nutation::iau1980(time);
226 let eps0 = MeanObliquity::iau1980(time.with_scale(Tt));
227 Self(
228 eps0.0.cos() * dpsi
229 + Angle::arcseconds(0.00264 * om.sin() + 0.000063 * (om + om).sin()),
230 )
231 }
232
233 pub fn iau2000a(time: Time<Tt>) -> Self {
235 let PrecessionCorrectionsIau2000 { depspr, .. } = PrecessionCorrectionsIau2000::new(time);
236 let epsa = MeanObliquity::iau1980(time).0 + depspr;
237 let Nutation { dpsi, .. } = Nutation::iau2000a(time.with_scale(Tdb));
238 Self::iau2000(time, epsa, dpsi)
239 }
240
241 pub fn iau2000b(time: Time<Tt>) -> Self {
243 let PrecessionCorrectionsIau2000 { depspr, .. } = PrecessionCorrectionsIau2000::new(time);
244 let epsa = MeanObliquity::iau1980(time).0 + depspr;
245 let Nutation { dpsi, .. } = Nutation::iau2000b(time.with_scale(Tdb));
246 Self::iau2000(time, epsa, dpsi)
247 }
248
249 pub fn iau2006a(time: Time<Tt>) -> Self {
251 let epsa = MeanObliquity::iau2006(time);
252 let Nutation { dpsi, .. } = Nutation::iau2006a(time.with_scale(Tdb));
253 Self::iau2000(time, epsa.0, dpsi)
254 }
255
256 pub fn iau2000(time: Time<Tt>, epsa: Angle, dpsi: Angle) -> Self {
258 Self(epsa.cos() * dpsi + Self::complimentary_terms_iau2000(time))
259 }
260
261 fn complimentary_terms_iau2000(time: Time<Tt>) -> Angle {
262 let t = time.centuries_since_j2000();
263
264 let fa = [
265 l_iers03(t).as_f64(),
266 lp_iers03(t).as_f64(),
267 f_iers03(t).as_f64(),
268 d_iers03(t).as_f64(),
269 omega_iers03(t).as_f64(),
270 venus_l_iers03(t).as_f64(),
271 earth_l_iers03(t).as_f64(),
272 pa_iers03(t).as_f64(),
273 ];
274
275 let s0 = complementary_terms::E0.iter().rev().fold(0.0, |s0, term| {
276 let a = zip(&term.nfa, &fa).fold(0.0, |a, (&nfa, &fa)| a + nfa as f64 * fa);
277 let (sa, ca) = a.sin_cos();
278 s0 + term.s * sa + term.c * ca
279 });
280
281 let s1 = complementary_terms::E1.iter().rev().fold(0.0, |s1, term| {
282 let a = zip(&term.nfa, &fa).fold(0.0, |a, (&nfa, &fa)| a + nfa as f64 * fa);
283 let (sa, ca) = a.sin_cos();
284 s1 + term.s * sa + term.c * ca
285 });
286
287 Angle::arcseconds(s0 + s1 * t)
288 }
289}
290
291#[cfg(test)]
292mod tests {
293 use lox_test_utils::assert_approx_eq;
294
295 use super::*;
296
297 #[test]
298 fn test_earth_rotation_angle_iau2000() {
299 let time = Time::from_two_part_julian_date(Ut1, 2400000.5, 54388.0);
300 let exp = EarthRotationAngle(Angle::new(0.402_283_724_002_815_8));
301 let act = EarthRotationAngle::iau2000(time);
302 assert_approx_eq!(act, exp, rtol <= 1e-12);
303 }
304
305 #[test]
306 fn test_greenwich_apparent_sidereal_time_iau1994() {
307 let ut1 = Time::from_two_part_julian_date(Ut1, 2400000.5, 53736.0);
308 let exp = GreenwichApparentSiderealTime(Angle::new(1.754_166_136_020_645_3));
309 let act = Gast::iau1994(ut1);
310 assert_approx_eq!(act, exp, rtol <= 1e-12);
311 }
312
313 #[test]
314 fn test_greenwich_apparent_sidereal_time_iau2000a() {
315 let tt = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
316 let ut1 = Time::from_two_part_julian_date(Ut1, 2400000.5, 53736.0);
317 let exp = GreenwichApparentSiderealTime(Angle::new(1.754_166_138_018_281_4));
318 let act = Gast::iau2000a(tt, ut1);
319 assert_approx_eq!(act, exp, rtol <= 1e-12);
320 }
321
322 #[test]
323 fn test_greenwich_apparent_sidereal_time_iau2000b() {
324 let tt = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
325 let ut1 = Time::from_two_part_julian_date(Ut1, 2400000.5, 53736.0);
326 let exp = GreenwichApparentSiderealTime(Angle::new(1.754_166_136_510_680_7));
327 let act = Gast::iau2000b(tt, ut1);
328 assert_approx_eq!(act, exp, rtol <= 1e-12);
329 }
330
331 #[test]
332 fn test_greenwich_mean_sidereal_time_iau1982() {
333 let ut1 = Time::from_two_part_julian_date(Ut1, 2400000.5, 53736.0);
334 let exp = GreenwichMeanSiderealTime(Angle::new(1.754_174_981_860_675));
335 let act = Gmst::iau1982(ut1);
336 assert_approx_eq!(act, exp, rtol <= 1e-12);
337 }
338
339 #[test]
340 fn test_greenwich_mean_sidereal_time_iau2000() {
341 let tt = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
342 let ut1 = Time::from_two_part_julian_date(Ut1, 2400000.5, 53736.0);
343 let exp = GreenwichMeanSiderealTime(Angle::new(1.754_174_972_210_740_7));
344 let act = Gmst::iau2000(tt, ut1);
345 assert_approx_eq!(act, exp, rtol <= 1e-12);
346 }
347
348 #[test]
349 fn test_greenwich_mean_sidereal_time_iau2006() {
350 let tt = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
351 let ut1 = Time::from_two_part_julian_date(Ut1, 2400000.5, 53736.0);
352 let exp = GreenwichMeanSiderealTime(Angle::new(1.754_174_971_870_091_2));
353 let act = Gmst::iau2006(tt, ut1);
354 assert_approx_eq!(act, exp, rtol <= 1e-12);
355 }
356
357 #[test]
358 fn test_equation_of_the_equinoxes_iau1994() {
359 let time = Time::from_two_part_julian_date(Tdb, 2400000.5, 41234.0);
360 let exp = EquationOfTheEquinoxes(Angle::new(5.357_758_254_609_257e-5));
361 let act = EquationOfTheEquinoxes::iau1994(time);
362 assert_approx_eq!(act, exp, atol <= 1e-17);
363 }
364
365 #[test]
366 fn test_equation_of_the_equinoxes_iau2000a() {
367 let time = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
368 let exp = EquationOfTheEquinoxes(Angle::new(-8.834_192_459_222_587e-6));
369 let act = EquationOfTheEquinoxes::iau2000a(time);
370 assert_approx_eq!(act, exp, atol <= 1e-18);
371 }
372
373 #[test]
374 fn test_equation_of_the_equinoxes_iau2000b() {
375 let time = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
376 let exp = EquationOfTheEquinoxes(Angle::new(-8.835_700_060_003_032e-6));
377 let act = EquationOfTheEquinoxes::iau2000b(time);
378 assert_approx_eq!(act, exp, atol <= 1e-18);
379 }
380
381 #[test]
382 fn test_equation_of_the_equinoxes_iau2000() {
383 let epsa = Angle::new(0.409_078_976_335_651);
384 let dpsi = Angle::new(-9.630_909_107_115_582e-6);
385 let time = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
386 let exp = EquationOfTheEquinoxes(Angle::new(-8.834_193_235_367_966e-6));
387 let act = EquationOfTheEquinoxes::iau2000(time, epsa, dpsi);
388 assert_approx_eq!(act, exp, atol <= 1e-20);
389 }
390
391 #[test]
392 fn test_equation_of_the_equinoxes_complimentary_terms() {
393 let time = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
394 let exp = Angle::new(2.046_085_004_885_125e-9);
395 let act = EquationOfTheEquinoxes::complimentary_terms_iau2000(time);
396 assert_approx_eq!(act, exp, atol <= 1e-20);
397 }
398
399 #[test]
400 fn test_greenwich_apparent_sidereal_time_iau2006a() {
401 let tt = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
402 let ut1 = Time::from_two_part_julian_date(Ut1, 2400000.5, 53736.0);
403 let exp = GreenwichApparentSiderealTime(Angle::new(1.754_166_137_675_019_2));
404 let act = Gast::iau2006a(tt, ut1);
405 assert_approx_eq!(act, exp, rtol <= 1e-12);
406 }
407
408 #[test]
409 fn test_equation_of_the_equinoxes_iau2006a() {
410 let time = Time::from_two_part_julian_date(Tt, 2400000.5, 53736.0);
415 let exp = EquationOfTheEquinoxes(Angle::new(-8.834_195_072_043_79e-6));
416 let act = EquationOfTheEquinoxes::iau2006a(time);
417 assert_approx_eq!(act, exp, atol <= 1e-12);
418 }
419}