1   /* Copyright 2002-2022 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 java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  
25  import org.hipparchus.util.Decimal64;
26  import org.hipparchus.util.Decimal64Field;
27  import org.junit.Assert;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.orekit.Utils;
31  import org.orekit.errors.OrekitException;
32  import org.orekit.errors.OrekitMessages;
33  import org.orekit.time.AbsoluteDate;
34  import org.orekit.time.FieldAbsoluteDate;
35  import org.orekit.time.TimeScalesFactory;
36  import org.orekit.utils.Constants;
37  import org.orekit.utils.IERSConventions;
38  
39  
40  public class EOPHistoryTest {
41  
42      @Test
43      public void testRegular() {
44          AbsoluteDate date = new AbsoluteDate(2004, 1, 4, TimeScalesFactory.getUTC());
45          double dt = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true).getUT1MinusUTC(date);
46          Assert.assertEquals(-0.3906070, dt, 1.0e-10);
47      }
48  
49      @Test
50      public void testOutOfRange() {
51          EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
52          AbsoluteDate endDate = new AbsoluteDate(2006, 3, 5, TimeScalesFactory.getUTC());
53          for (double t = -1000; t < 1000 ; t += 3) {
54              AbsoluteDate date = endDate.shiftedBy(t);
55              double dt = history.getUT1MinusUTC(date);
56              if (t <= 0) {
57                  Assert.assertTrue(dt < 0.29236);
58                  Assert.assertTrue(dt > 0.29233);
59              } else {
60                  // no more data after end date
61                  Assert.assertEquals(0.0, dt, 1.0e-10);
62              }
63          }
64      }
65  
66      @Test
67      public void testFieldOutOfRange() {
68          EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
69          FieldAbsoluteDate<Decimal64> endDate = new FieldAbsoluteDate<>(Decimal64Field.getInstance(),
70                                                                         2006, 3, 5, TimeScalesFactory.getUTC());
71          for (double t = -1000; t < 1000 ; t += 3) {
72              FieldAbsoluteDate<Decimal64> date = endDate.shiftedBy(t);
73              Decimal64 dt = history.getUT1MinusUTC(date);
74              if (t <= 0) {
75                  Assert.assertTrue(dt.getReal() < 0.29236);
76                  Assert.assertTrue(dt.getReal() > 0.29233);
77              } else {
78                  // no more data after end date
79                  Assert.assertEquals(0.0, dt.getReal(), 1.0e-10);
80              }
81          }
82      }
83  
84      @Test
85      public void testContinuityThreshold() {
86          try {
87              FramesFactory.setEOPContinuityThreshold(0.5 * Constants.JULIAN_DAY);
88              AbsoluteDate date = new AbsoluteDate(2004, 1, 4, TimeScalesFactory.getUTC());
89              FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true).getUT1MinusUTC(date);
90              Assert.fail("an exception should have been thrown");
91          } catch (OrekitException oe) {
92              Assert.assertEquals(OrekitMessages.MISSING_EARTH_ORIENTATION_PARAMETERS_BETWEEN_DATES_GAP,
93                                  oe.getSpecifier());
94          }
95      }
96  
97      @Test
98      public void testUTCLeap() {
99          EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
100         AbsoluteDate endLeap = new AbsoluteDate(2006, 1, 1, TimeScalesFactory.getUTC());
101         for (double dt = -200; dt < 200; dt += 3) {
102             final AbsoluteDate date = endLeap.shiftedBy(dt);
103             double dtu1 = history.getUT1MinusUTC(date);
104             if (dt <= 0) {
105                 Assert.assertEquals(-0.6612, dtu1, 3.0e-5);
106             } else {
107                 Assert.assertEquals(0.3388, dtu1, 3.0e-5);
108             }
109         }
110     }
111 
112     @Test
113     public void testFieldUTCLeap() {
114         EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
115         FieldAbsoluteDate<Decimal64> endLeap = new FieldAbsoluteDate<>(Decimal64Field.getInstance(),
116                                                                        2006, 1, 1, TimeScalesFactory.getUTC());
117         for (double dt = -200; dt < 200; dt += 3) {
118             final FieldAbsoluteDate<Decimal64> date = endLeap.shiftedBy(dt);
119             Decimal64 dtu1 = history.getUT1MinusUTC(date);
120             if (dt <= 0) {
121                 Assert.assertEquals(-0.6612, dtu1.getReal(), 3.0e-5);
122             } else {
123                 Assert.assertEquals(0.3388, dtu1.getReal(), 3.0e-5);
124             }
125         }
126     }
127 
128     @Test
129     public void testSerialization() throws IOException, ClassNotFoundException {
130         EOPHistory history = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, true);
131 
132         ByteArrayOutputStream bos = new ByteArrayOutputStream();
133         ObjectOutputStream    oos = new ObjectOutputStream(bos);
134         oos.writeObject(history);
135 
136         Assert.assertTrue(bos.size() > 150000);
137         Assert.assertTrue(bos.size() < 155000);
138 
139         ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
140         ObjectInputStream     ois = new ObjectInputStream(bis);
141         EOPHistory deserialized  = (EOPHistory) ois.readObject();
142         Assert.assertEquals(history.getStartDate(), deserialized.getStartDate());
143         Assert.assertEquals(history.getEndDate(), deserialized.getEndDate());
144         Assert.assertEquals(history.getEntries().size(), deserialized.getEntries().size());
145         for (int i = 0; i < history.getEntries().size(); ++i) {
146             EOPEntry e1 = history.getEntries().get(i);
147             EOPEntry e2 = deserialized.getEntries().get(i);
148             Assert.assertEquals(e1.getMjd(),         e2.getMjd());
149             Assert.assertEquals(e1.getDate(),        e2.getDate());
150             Assert.assertEquals(e1.getUT1MinusUTC(), e2.getUT1MinusUTC(), 1.0e-10);
151             Assert.assertEquals(e1.getLOD(),         e2.getLOD(),         1.0e-10);
152             Assert.assertEquals(e1.getDdEps(),       e2.getDdEps(),       1.0e-10);
153             Assert.assertEquals(e1.getDdPsi(),       e2.getDdPsi(),       1.0e-10);
154             Assert.assertEquals(e1.getDx(),          e2.getDx(),          1.0e-10);
155             Assert.assertEquals(e1.getDy(),          e2.getDy(),          1.0e-10);
156             Assert.assertEquals(e1.getX(),           e2.getX(),           1.0e-10);
157             Assert.assertEquals(e1.getY(),           e2.getY(),           1.0e-10);
158         }
159 
160     }
161 
162     @Test
163     public void testTidalInterpolationEffects() throws IOException, OrekitException {
164 
165         final EOPHistory h1 = FramesFactory.getEOPHistory(IERSConventions.IERS_2010, false);
166         final EOPHistory h2 = h1.getNonInterpolatingEOPHistory();
167         final AbsoluteDate date0 = new AbsoluteDate(2004, 8, 16, 20, 0, 0, TimeScalesFactory.getUTC());
168 
169         for (double dt = 0; dt < Constants.JULIAN_DAY; dt += 10) {
170             final AbsoluteDate date = date0.shiftedBy(dt);
171             final double interpolationErrorUT1 = h1.getUT1MinusUTC(date) - h2.getUT1MinusUTC(date);
172             final double interpolationErrorLOD = h1.getLOD(date)         - h2.getLOD(date);
173             final PoleCorrection p1 = h1.getPoleCorrection(date);
174             final PoleCorrection p2 = h2.getPoleCorrection(date);
175             final double interpolationErrorXp  = (p1.getXp() - p2.getXp()) / Constants.ARC_SECONDS_TO_RADIANS;
176             final double interpolationErrorYp  = (p1.getYp() - p2.getYp()) / Constants.ARC_SECONDS_TO_RADIANS;
177             Assert.assertEquals(0.0, interpolationErrorUT1, 1.2e-10); // seconds
178             Assert.assertEquals(0.0, interpolationErrorLOD, 1.5e-9);  // seconds
179             Assert.assertEquals(0.0, interpolationErrorXp,  2.3e-9);  // arcseconds
180             Assert.assertEquals(0.0, interpolationErrorYp,  1.5e-9);  // arcseconds
181         }
182 
183     }
184 
185     @Before
186     public void setUp() {
187         Utils.setDataRoot("regular-data");
188     }
189 
190 }