1   /* Copyright 2002-2025 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.time;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.Utils;
23  import org.orekit.frames.ITRFVersion;
24  import org.orekit.utils.IERSConventions;
25  
26  
27  public class TDBScaleTest {
28  
29      @Test
30      public void testReference() {
31          TimeScale scale = TimeScalesFactory.getTDB();
32          Assertions.assertEquals("TDB", scale.toString());
33          Assertions.assertEquals(32.183927340791372839, scale.offsetFromTAI(AbsoluteDate.J2000_EPOCH).toDouble(), 1.0e-15);
34      }
35  
36      @Test
37      public void testDate5000000() {
38          TimeScale scale = TimeScalesFactory.getTDB();
39          AbsoluteDate date = AbsoluteDate.J2000_EPOCH.shiftedBy(5000000);
40          Assertions.assertEquals(32.185364155950634549, scale.offsetFromTAI(date).toDouble(), 1.0e-13);
41      }
42  
43      @Test
44      public void testToTAI5000000() {
45          TimeScale scale = TimeScalesFactory.getTDB();
46          AbsoluteDate date = new AbsoluteDate(2000, 2, 28, 8, 53, 20.001364155950634549, scale);
47          double dt = AbsoluteDate.J2000_EPOCH.shiftedBy(5000000).durationFrom(date);
48          Assertions.assertEquals(0.0, dt, 1.0e-13);
49      }
50  
51      @Test
52      public void testToTAI() {
53          TimeScale scale = TimeScalesFactory.getTDB();
54          AbsoluteDate date = new AbsoluteDate(2000, 1, 1, 11, 59, 59.999927340791372839, scale);
55          double dt = AbsoluteDate.J2000_EPOCH.durationFrom(date);
56          Assertions.assertEquals(0.0, dt, 1.0e-13);
57      }
58  
59      @Test
60      public void testSofa() {
61  
62          // the reference data for this test was obtained by running the following program
63          // with version 2012-03-01 of the SOFA library in C
64          //        double tai1, tai2, tttdb;
65          //
66          //        tai1 = 2448939.5;
67          //        tai2 = 0.123;
68          //        tttdb = iauDtdb(tai1, tai2, 0.0, 0.0, 0.0, 0.0);
69          //
70          //        printf("iauDtdb(%.20g, %.20g, 0.0, 0.0, 0.0, 0.0)\n  --> %.20g\n", tai1, tai2, tttdb);
71          // which displays the following result:
72          //        iauDtdb(2448939.5, 0.12299999999999999822, 0.0, 0.0, 0.0, 0.0)
73          //        --> -0.001279984433218163669
74  
75          // the difference with SOFA is quite big (10 microseconds) because SOFA uses
76          // the full Fairhead & Bretagnon model from 1990, including planetary effects,
77          // whereas in Orekit we use only the conventional definition from IAU general
78          // assembly 2006. So this difference is expected
79  
80          AbsoluteDate date = new AbsoluteDate(1992, 11, 13, 2, 57, 7.2,
81                                               TimeScalesFactory.getTAI());
82          TimeOffset delta = TimeScalesFactory.getTDB().offsetFromTAI(date).
83                            subtract(TimeScalesFactory.getTT().offsetFromTAI(date));
84          Assertions.assertEquals(-0.001279984433218163669, delta.toDouble(), 1.0e-5);
85  
86      }
87  
88      @Test
89      public void testAAS06134() {
90  
91          // this reference test has been extracted from the following paper:
92          // Implementation Issues Surrounding the New IAU Reference Systems for Astrodynamics
93          // David A. Vallado, John H. Seago, P. Kenneth Seidelmann
94          // http://www.centerforspace.com/downloads/files/pubs/AAS-06-134.pdf
95          // Note that the dUT1 here is -0.439962, whereas it is -0.4399619 in the book
96          Utils.setLoaders(IERSConventions.IERS_1996,
97                           Utils.buildEOPList(IERSConventions.IERS_1996, ITRFVersion.ITRF_2008, new double[][] {
98                               { 53098, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN },
99                               { 53099, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN },
100                              { 53100, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN },
101                              { 53101, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN },
102                              { 53102, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN },
103                              { 53103, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN },
104                              { 53104, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN },
105                              { 53105, -0.439962, 0.0015563, -0.140682, 0.333309, -0.052195, -0.003875, Double.NaN, Double.NaN }
106                          }));
107         AbsoluteDate date =
108                 new AbsoluteDate(2004, 4, 6, 7, 51, 28.386009, TimeScalesFactory.getUTC());
109         DateTimeComponents components = date.getComponents(TimeScalesFactory.getTDB());
110         Assertions.assertEquals(2004,            components.getDate().getYear());
111         Assertions.assertEquals(   4,            components.getDate().getMonth());
112         Assertions.assertEquals(   6,            components.getDate().getDay());
113         Assertions.assertEquals(   7,            components.getTime().getHour());
114         Assertions.assertEquals(  52,            components.getTime().getMinute());
115 
116         // the "large" threshold in this test is due to the fact TDB model is
117         // approximated both in Orekit and in the reference paper. the difference
118         // is however small as the model in the paper is announced as being accurate
119         // to 50 micro seconds, and the test here is far below this value
120         Assertions.assertEquals(  32.5716651154, components.getTime().getSecond(), 1.4e-8);
121 
122     }
123 
124     @Test
125     public void testDuringLeap() {
126         final TimeScale utc   = TimeScalesFactory.getUTC();
127         final TimeScale scale = TimeScalesFactory.getTDB();
128         final AbsoluteDate before = new AbsoluteDate(new DateComponents(1983, 6, 30),
129                                                      new TimeComponents(23, 59, 59),
130                                                      utc);
131         final AbsoluteDate during = before.shiftedBy(1.25);
132         Assertions.assertEquals(61, utc.minuteDuration(during));
133         Assertions.assertEquals(1.0, utc.getLeap(during).toDouble(), 1.0e-10);
134         Assertions.assertEquals(60, scale.minuteDuration(during));
135         Assertions.assertEquals(0.0, scale.getLeap(during).toDouble(), 1.0e-10);
136     }
137 
138     @BeforeEach
139     public void setUp() {
140         Utils.setDataRoot("regular-data");
141     }
142 
143 }