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.time;
18  
19  
20  
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.orekit.Utils;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  
27  public class TAIUTCDatAFilesLoaderTest {
28  
29      @Test
30      public void testRegularFile() {
31  
32          Utils.setDataRoot("USNO");
33  
34          // we arbitrary put UTC == TAI before 1961-01-01
35          checkOffset(1950,  1,  1,   0);
36  
37          // linear models between 1961 and 1972
38          checkOffset(1961,  1,  2,  -(1.422818 +   1 * 0.001296));  // MJD 37300 +   1
39          checkOffset(1961,  8,  2,  -(1.372818 + 213 * 0.001296));  // MJD 37300 + 213
40          checkOffset(1962,  1,  2,  -(1.845858 +   1 * 0.0011232)); // MJD 37665 +   1
41          checkOffset(1963, 11,  2,  -(1.945858 + 670 * 0.0011232)); // MJD 37665 + 670
42          checkOffset(1964,  1,  2,  -(3.240130 - 365 * 0.001296));  // MJD 38761 - 365
43          checkOffset(1964,  4,  2,  -(3.340130 - 274 * 0.001296));  // MJD 38761 - 274
44          checkOffset(1964,  9,  2,  -(3.440130 - 121 * 0.001296));  // MJD 38761 - 121
45          checkOffset(1965,  1,  2,  -(3.540130 +   1 * 0.001296));  // MJD 38761 +   1
46          checkOffset(1965,  3,  2,  -(3.640130 +  60 * 0.001296));  // MJD 38761 +  60
47          checkOffset(1965,  7,  2,  -(3.740130 + 182 * 0.001296));  // MJD 38761 + 182
48          checkOffset(1965,  9,  2,  -(3.840130 + 244 * 0.001296));  // MJD 38761 + 244
49          checkOffset(1966,  1,  2,  -(4.313170 +   1 * 0.002592));  // MJD 39126 +   1
50          checkOffset(1968,  2,  2,  -(4.213170 + 762 * 0.002592));  // MJD 39126 + 762
51  
52          // since 1972-01-01, offsets are only whole seconds
53          checkOffset(1972,  3,  5, -10);
54          checkOffset(1972,  7, 14, -11);
55          checkOffset(1979, 12, 31, -18);
56          checkOffset(1980,  1, 22, -19);
57          checkOffset(2006,  7,  7, -33);
58          checkOffset(2010,  7,  7, -34);
59          checkOffset(2012,  7,  7, -35);
60          checkOffset(2015,  7,  7, -36);
61  
62      }
63  
64      @Test
65      public void testOnlyPre1972Data() {
66  
67          Utils.setDataRoot("USNO");
68          TimeScalesFactory.addUTCTAIOffsetsLoader(new TAIUTCDatFilesLoader("tai-utc-only-pre-1972-data.dat"));
69  
70          // linear models between 1961 and 1972
71          checkOffset(1961,  1,  2,  -(1.422818 +   1 * 0.001296));  // MJD 37300 +   1
72          checkOffset(1961,  8,  2,  -(1.372818 + 213 * 0.001296));  // MJD 37300 + 213
73          checkOffset(1962,  1,  2,  -(1.845858 +   1 * 0.0011232)); // MJD 37665 +   1
74          checkOffset(1963, 11,  2,  -(1.945858 + 670 * 0.0011232)); // MJD 37665 + 670
75          checkOffset(1964,  1,  2,  -(3.240130 - 365 * 0.001296));  // MJD 38761 - 365
76          checkOffset(1964,  4,  2,  -(3.340130 - 274 * 0.001296));  // MJD 38761 - 274
77          checkOffset(1964,  9,  2,  -(3.440130 - 121 * 0.001296));  // MJD 38761 - 121
78          checkOffset(1965,  1,  2,  -(3.540130 +   1 * 0.001296));  // MJD 38761 +   1
79          checkOffset(1965,  3,  2,  -(3.640130 +  60 * 0.001296));  // MJD 38761 +  60
80          checkOffset(1965,  7,  2,  -(3.740130 + 182 * 0.001296));  // MJD 38761 + 182
81          checkOffset(1965,  9,  2,  -(3.840130 + 244 * 0.001296));  // MJD 38761 + 244
82          checkOffset(1966,  1,  2,  -(4.313170 +   1 * 0.002592));  // MJD 39126 +   1
83          checkOffset(1968,  2,  2,  -(4.213170 + 762 * 0.002592));  // MJD 39126 + 762
84  
85          // last linear drift is not stopped as we miss the first constant offset in 1972
86          checkOffset(1972,  3,  5,  -(4.213170 +  2255 * 0.002592));  // MJD 39126 +  2255
87          checkOffset(1972,  7, 14,  -(4.213170 +  2386 * 0.002592));  // MJD 39126 +  2386
88          checkOffset(1979, 12, 31,  -(4.213170 +  5112 * 0.002592));  // MJD 39126 +  5112
89          checkOffset(1980,  1, 22,  -(4.213170 +  5134 * 0.002592));  // MJD 39126 +  5134
90          checkOffset(2006,  7,  7,  -(4.213170 + 14797 * 0.002592));  // MJD 39126 + 14797
91          checkOffset(2010,  7,  7,  -(4.213170 + 16258 * 0.002592));  // MJD 39126 + 16258
92          checkOffset(2012,  7,  7,  -(4.213170 + 16989 * 0.002592));  // MJD 39126 + 16989
93          checkOffset(2015,  7,  7,  -(4.213170 + 18084 * 0.002592));  // MJD 39126 + 18084
94  
95      }
96  
97      @Test
98      public void testModifiedLinearData() {
99  
100         Utils.setDataRoot("USNO");
101         TimeScalesFactory.addUTCTAIOffsetsLoader(new TAIUTCDatFilesLoader("tai-utc-modified-linear.dat"));
102 
103         // linear models between 1961 and 1972
104         checkOffset(1961,  1,  2,  -(1.4000000 +   1 * 0.001000));  // MJD 37300 +   1
105         checkOffset(1961,  8,  2,  -(1.5000000 + 213 * 0.001100));  // MJD 37300 + 213
106         checkOffset(1962,  1,  2,  -(1.6000000 +   1 * 0.001200));  // MJD 37665 +   1
107         checkOffset(1963, 11,  2,  -(1.7000000 + 670 * 0.001300));  // MJD 37665 + 670
108         checkOffset(1964,  1,  2,  -(1.8000000 - 365 * 0.001400));  // MJD 38761 - 365
109         checkOffset(1964,  4,  2,  -(1.9000000 - 274 * 0.001500));  // MJD 38761 - 274
110         checkOffset(1964,  9,  2,  -(2.0000000 - 121 * 0.001600));  // MJD 38761 - 121
111         checkOffset(1965,  1,  2,  -(2.1000000 +   1 * 0.001700));  // MJD 38761 +   1
112         checkOffset(1965,  3,  2,  -(2.2000000 +  60 * 0.001800));  // MJD 38761 +  60
113         checkOffset(1965,  7,  2,  -(2.3000000 + 182 * 0.001900));  // MJD 38761 + 182
114         checkOffset(1965,  9,  2,  -(2.4000000 + 244 * 0.002000));  // MJD 38761 + 244
115         checkOffset(1966,  1,  2,  -(2.5000000 +   1 * 0.002100));  // MJD 39126 +   1
116         checkOffset(1968,  2,  2,  -(2.6000000 + 762 * 0.002200));  // MJD 39126 + 762
117 
118         // last linear drift is not stopped as we miss the first constant offset in 1972
119         checkOffset(1972,  3,  5,  -(2.6000000 +  2255 * 0.002200));  // MJD 39126 +  2255
120         checkOffset(1972,  7, 14,  -(2.6000000 +  2386 * 0.002200));  // MJD 39126 +  2386
121         checkOffset(1979, 12, 31,  -(2.6000000 +  5112 * 0.002200));  // MJD 39126 +  5112
122         checkOffset(1980,  1, 22,  -(2.6000000 +  5134 * 0.002200));  // MJD 39126 +  5134
123         checkOffset(2006,  7,  7,  -(2.6000000 + 14797 * 0.002200));  // MJD 39126 + 14797
124         checkOffset(2010,  7,  7,  -(2.6000000 + 16258 * 0.002200));  // MJD 39126 + 16258
125         checkOffset(2012,  7,  7,  -(2.6000000 + 16989 * 0.002200));  // MJD 39126 + 16989
126         checkOffset(2015,  7,  7,  -(2.6000000 + 18084 * 0.002200));  // MJD 39126 + 18084
127 
128     }
129 
130     @Test
131     public void testInconsistentDate() {
132         checkException("tai-utc-inconsistent-date.dat",
133                        OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE);
134     }
135 
136     @Test
137     public void testNonChronological() {
138         checkException("tai-utc-non-chronological.dat",
139                        OrekitMessages.NON_CHRONOLOGICAL_DATES_IN_FILE);
140     }
141 
142     @Test
143     public void testFormatError() {
144         checkException("tai-utc-format-error.dat",
145                        OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE);
146     }
147 
148     private void checkOffset(int year, int month, int day, double offset) {
149         TimeScale utc = TimeScalesFactory.getUTC();
150         AbsoluteDate date = new AbsoluteDate(year, month, day, utc);
151         Assert.assertEquals(offset, utc.offsetFromTAI(date), 1.0e-10);
152     }
153 
154     private void checkException(String name, OrekitMessages message) {
155         Utils.setDataRoot("USNO");
156         TimeScalesFactory.addUTCTAIOffsetsLoader(new TAIUTCDatFilesLoader(name));
157         try {
158             TimeScalesFactory.getUTC();
159             Assert.fail("an exception should have been thrown");
160         } catch (OrekitException oe) {
161             Assert.assertEquals(message, oe.getSpecifier());
162         }
163     }
164 
165 }