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.hipparchus.util.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.orekit.data.DirectoryCrawler;
24  import org.orekit.data.LazyLoadedDataContext;
25  import org.orekit.frames.EopHistoryLoader;
26  import org.orekit.frames.ITRFVersion;
27  import org.orekit.frames.ITRFVersionLoader.ITRFVersionConfiguration;
28  import org.orekit.frames.ItrfVersionProvider;
29  import org.orekit.time.TAIUTCDatFilesLoader.Parser;
30  import org.orekit.utils.IERSConventions;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.List;
35  
36  /**
37   * Unit tests for {@link PreloadedTimeScales}
38   *
39   * @author Evan Ward
40   */
41  public class PreloadedTimeScalesTest {
42  
43      /** Subject under test. */
44      private TimeScales timeScales;
45  
46      /**
47       * Create the time scales under test.
48       *
49       * @throws IOException on error.
50       */
51      @BeforeEach
52      public void setUp() throws IOException {
53          final String leapPath = "/USNO/tai-utc.dat";
54          final String eopPath =
55                  "/regular-data/Earth-orientation-parameters/yearly/finals2000A.2002.xml";
56          final ITRFVersionConfiguration configuration = new ITRFVersionConfiguration(
57                  "", ITRFVersion.ITRF_2014, Integer.MIN_VALUE, Integer.MAX_VALUE);
58          final ItrfVersionProvider itrfVersionProvider = (name, mjd) -> configuration;
59          final List<OffsetModel> leapSeconds = new Parser().parse(
60                  PreloadedTimeScales.class.getResourceAsStream(leapPath),
61                  leapPath);
62          this.timeScales = TimeScales.of(
63                  leapSeconds,
64                  (conventions, timeScales) -> {
65                      try {
66                          return EopHistoryLoader.Parser
67                                  .newFinalsXmlParser(conventions, itrfVersionProvider, timeScales)
68                                  .parse(PreloadedTimeScales.class.getResourceAsStream(eopPath), eopPath);
69                      } catch (IOException e) {
70                          throw new RuntimeException(e);
71                      }
72                  });
73      }
74  
75      /** Rough check that the time scales work as expected. */
76      @Test
77      public void testTime() {
78          // actions
79          AbsoluteDate javaEpoch = timeScales.getJavaEpoch();
80          AbsoluteDate j2000Epoch = timeScales.getJ2000Epoch();
81          TAIScale tai = timeScales.getTAI();
82          UTCScale utc = timeScales.getUTC();
83          UT1Scale ut1 = timeScales.getUT1(IERSConventions.IERS_2010, true);
84          AbsoluteDate date = new AbsoluteDate(2002, 12, 31, utc);
85          AbsoluteDate date2 = new AbsoluteDate(1977, 1, 1, tai);
86          AbsoluteDate date3 = new AbsoluteDate(2000, 1, 1, 12, 0, 0.0, ut1);
87  
88          // verify
89          Assertions.assertEquals("1970-01-01T00:00:00.000", javaEpoch.toString(utc));
90          Assertions.assertEquals(-32, utc.offsetFromTAI(date).toDouble(), 0);
91          Assertions.assertEquals(-32.2889120, ut1.offsetFromTAI(date).toDouble(), 1e-14);
92          Assertions.assertEquals(0, tai.offsetFromTAI(date).toDouble(), 0);
93          Assertions.assertEquals(-32, timeScales.getUTC().offsetFromTAI(date).toDouble(), 0);
94          Assertions.assertEquals(-32 + 24110.54841, timeScales.getGMST(IERSConventions.IERS_2010, true).offsetFromTAI(date3).toDouble(), 0);
95          Assertions.assertEquals(-32 + 3 * 3600, timeScales.getGLONASS().offsetFromTAI(date).toDouble(), 0);
96          Assertions.assertEquals(-19, timeScales.getGPS().offsetFromTAI(date).toDouble(), 0);
97          Assertions.assertEquals(-19, timeScales.getQZSS().offsetFromTAI(date).toDouble(), 0);
98          Assertions.assertEquals(-19, timeScales.getGST().offsetFromTAI(date).toDouble(), 0);
99          Assertions.assertEquals(-19, timeScales.getNavIC().offsetFromTAI(date).toDouble(), 0);
100         Assertions.assertEquals(32.184, timeScales.getTT().offsetFromTAI(date).toDouble(), 0);
101         Assertions.assertEquals(32.184, timeScales.getTDB().offsetFromTAI(j2000Epoch.shiftedBy(216525.908119)).toDouble(), 0);
102         Assertions.assertEquals(32.184, timeScales.getTCB().offsetFromTAI(date2).toDouble(), 1e-4);
103         Assertions.assertEquals(32.184, timeScales.getTCG().offsetFromTAI(date2).toDouble(), 0);
104     }
105 
106     /** Check the UT1 creation logic. */
107     @Test
108     public void testUt1() {
109         // setup
110         UT1Scale ut12010Simple = timeScales.getUT1(IERSConventions.IERS_2010, true);
111         UT1Scale ut12010Full = timeScales.getUT1(IERSConventions.IERS_2010, false);
112         UT1Scale ut12003Simple = timeScales.getUT1(IERSConventions.IERS_2003, true);
113         UT1Scale ut12003Full = timeScales.getUT1(IERSConventions.IERS_2003, false);
114         UT1Scale ut11996Simple = timeScales.getUT1(IERSConventions.IERS_1996, true);
115         UT1Scale ut11996Full = timeScales.getUT1(IERSConventions.IERS_1996, false);
116         UTCScale utc = timeScales.getUTC();
117         AbsoluteDate date = new AbsoluteDate(2002, 12, 31, utc);
118 
119 
120         // verify
121         Assertions.assertSame(ut12010Simple, timeScales.getUT1(IERSConventions.IERS_2010, true));
122         Assertions.assertSame(ut12010Full, timeScales.getUT1(IERSConventions.IERS_2010, false));
123         Assertions.assertSame(ut12003Simple, timeScales.getUT1(IERSConventions.IERS_2003, true));
124         Assertions.assertSame(ut12003Full, timeScales.getUT1(IERSConventions.IERS_2003, false));
125         Assertions.assertSame(ut11996Simple, timeScales.getUT1(IERSConventions.IERS_1996, true));
126         Assertions.assertSame(ut11996Full, timeScales.getUT1(IERSConventions.IERS_1996, false));
127 
128 
129         Assertions.assertEquals(-32.2889120, ut12010Simple.offsetFromTAI(date).toDouble(), 1e-14);
130         Assertions.assertEquals(-32.2889120, ut12003Simple.offsetFromTAI(date).toDouble(), 1e-14);
131         Assertions.assertEquals(-32.2889120, ut11996Simple.offsetFromTAI(date).toDouble(), 1e-14);
132         Assertions.assertEquals(-2e-5, -32.2889120 - ut12010Full.offsetFromTAI(date).toDouble(), 1e-5);
133         Assertions.assertEquals(-2e-5, -32.2889120 - ut12003Full.offsetFromTAI(date).toDouble(), 1e-5);
134         Assertions.assertEquals(-2e-5, -32.2889120 - ut11996Full.offsetFromTAI(date).toDouble(), 1e-5);
135 
136     }
137 
138     /** Compare with another data context. */
139     @Test
140     public void testComparison() {
141         // setup
142         LazyLoadedDataContext dataContext = new LazyLoadedDataContext();
143         dataContext.getDataProvidersManager().addProvider(
144                 new DirectoryCrawler(new File("src/test/resources/zero-EOP")));
145         LazyLoadedTimeScales other = dataContext.getTimeScales();
146         DateTimeComponents date = new DateTimeComponents(2002, 12, 30);
147 
148         // action
149         final UT1Scale ut1 = timeScales.getUT1(IERSConventions.IERS_2010, true);
150         final UT1Scale otherUt1 = other.getUT1(IERSConventions.IERS_2010, true);
151         double actual = new AbsoluteDate(date, ut1)
152                 .durationFrom(new AbsoluteDate(date, otherUt1));
153 
154         // verify
155         Assertions.assertEquals(0.2881680, actual, FastMath.ulp(32));
156     }
157 
158 }