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.frames;
18  
19  import org.hipparchus.util.Binary64;
20  import org.hipparchus.util.Binary64Field;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  import org.orekit.Utils;
25  import org.orekit.errors.OrekitException;
26  import org.orekit.errors.OrekitMessages;
27  import org.orekit.time.AbsoluteDate;
28  import org.orekit.time.FieldAbsoluteDate;
29  import org.orekit.time.TimeScalesFactory;
30  import org.orekit.utils.Constants;
31  import org.orekit.utils.IERSConventions;
32  
33  import java.io.IOException;
34  
35  public class EOPHistoryTest {
36  
37      @Test
38      public void testRegular() {
39          AbsoluteDate date = new AbsoluteDate(2004, 1, 4, TimeScalesFactory.getUTC());
40          double dt = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true).getUT1MinusUTC(date);
41          Assertions.assertEquals(-0.3906070, dt, 1.0e-10);
42      }
43  
44      @Test
45      public void testOutOfRange() {
46          EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
47          AbsoluteDate endDate = new AbsoluteDate(2006, 3, 5, TimeScalesFactory.getUTC());
48          for (double t = -1000; t < 1000 ; t += 3) {
49              AbsoluteDate date = endDate.shiftedBy(t);
50              double dt = history.getUT1MinusUTC(date);
51              if (t <= 0) {
52                  Assertions.assertTrue(dt < 0.29236);
53                  Assertions.assertTrue(dt > 0.29233);
54              } else {
55                  // no more data after end date
56                  Assertions.assertEquals(0.0, dt, 1.0e-10);
57              }
58          }
59      }
60  
61      @Test
62      public void testFieldOutOfRange() {
63          EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
64          FieldAbsoluteDate<Binary64> endDate = new FieldAbsoluteDate<>(Binary64Field.getInstance(),
65                                                                         2006, 3, 5, TimeScalesFactory.getUTC());
66          for (double t = -1000; t < 1000 ; t += 3) {
67              FieldAbsoluteDate<Binary64> date = endDate.shiftedBy(t);
68              Binary64 dt = history.getUT1MinusUTC(date);
69              if (t <= 0) {
70                  Assertions.assertTrue(dt.getReal() < 0.29236);
71                  Assertions.assertTrue(dt.getReal() > 0.29233);
72              } else {
73                  // no more data after end date
74                  Assertions.assertEquals(0.0, dt.getReal(), 1.0e-10);
75              }
76          }
77      }
78  
79      @Test
80      public void testContinuityThreshold() {
81          try {
82              FramesFactory.setEOPContinuityThreshold(0.5 * Constants.JULIAN_DAY);
83              AbsoluteDate date = new AbsoluteDate(2004, 1, 4, TimeScalesFactory.getUTC());
84              FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true).getUT1MinusUTC(date);
85              Assertions.fail("an exception should have been thrown");
86          } catch (OrekitException oe) {
87              Assertions.assertEquals(OrekitMessages.MISSING_EARTH_ORIENTATION_PARAMETERS_BETWEEN_DATES_GAP,
88                                  oe.getSpecifier());
89          }
90      }
91  
92      @Test
93      public void testUTCLeap() {
94          EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
95          AbsoluteDate endLeap = new AbsoluteDate(2006, 1, 1, TimeScalesFactory.getUTC());
96          for (double dt = -200; dt < 200; dt += 3) {
97              final AbsoluteDate date = endLeap.shiftedBy(dt);
98              double dtu1 = history.getUT1MinusUTC(date);
99              if (dt <= 0) {
100                 Assertions.assertEquals(-0.6612, dtu1, 3.0e-5);
101             } else {
102                 Assertions.assertEquals(0.3388, dtu1, 3.0e-5);
103             }
104         }
105     }
106 
107     @Test
108     public void testFieldUTCLeap() {
109         EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
110         FieldAbsoluteDate<Binary64> endLeap = new FieldAbsoluteDate<>(Binary64Field.getInstance(),
111                                                                        2006, 1, 1, TimeScalesFactory.getUTC());
112         for (double dt = -200; dt < 200; dt += 3) {
113             final FieldAbsoluteDate<Binary64> date = endLeap.shiftedBy(dt);
114             Binary64 dtu1 = history.getUT1MinusUTC(date);
115             if (dt <= 0) {
116                 Assertions.assertEquals(-0.6612, dtu1.getReal(), 3.0e-5);
117             } else {
118                 Assertions.assertEquals(0.3388, dtu1.getReal(), 3.0e-5);
119             }
120         }
121     }
122 
123     @Test
124     public void testTidalInterpolationEffects() throws IOException, OrekitException {
125 
126         final EOPHistory h1 = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, false);
127         final EOPHistory h2 = h1.getEOPHistoryWithoutCachedTidalCorrection();
128         final AbsoluteDate date0 = new AbsoluteDate(2004, 8, 16, 20, 0, 0, TimeScalesFactory.getUTC());
129 
130         for (double dt = 0; dt < Constants.JULIAN_DAY; dt += 10) {
131             final AbsoluteDate date = date0.shiftedBy(dt);
132             final double interpolationErrorUT1 = h1.getUT1MinusUTC(date) - h2.getUT1MinusUTC(date);
133             final double interpolationErrorLOD = h1.getLOD(date)         - h2.getLOD(date);
134             final PoleCorrection p1 = h1.getPoleCorrection(date);
135             final PoleCorrection p2 = h2.getPoleCorrection(date);
136             final double interpolationErrorXp  = (p1.getXp() - p2.getXp()) / Constants.ARC_SECONDS_TO_RADIANS;
137             final double interpolationErrorYp  = (p1.getYp() - p2.getYp()) / Constants.ARC_SECONDS_TO_RADIANS;
138             Assertions.assertEquals(0.0, interpolationErrorUT1, 1.2e-10); // seconds
139             Assertions.assertEquals(0.0, interpolationErrorLOD, 1.5e-9);  // seconds
140             Assertions.assertEquals(0.0, interpolationErrorXp,  2.3e-9);  // arcseconds
141             Assertions.assertEquals(0.0, interpolationErrorYp,  1.5e-9);  // arcseconds
142         }
143 
144     }
145 
146     @BeforeEach
147     public void setUp() {
148         Utils.setDataRoot("regular-data");
149     }
150 
151 }