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 UTCTAIBulletinAFilesLoaderTest {
28  
29      @Test
30      public void test2006Leap() {
31          Utils.setDataRoot("bulletinA");
32          // this file contains a single leap second on 2006-01-01, from 32s to 33s
33          TimeScalesFactory.addUTCTAIOffsetsLoader(new UTCTAIBulletinAFilesLoader("bulletina-xix-001\\.txt$"));
34  
35          UTCScale utc = TimeScalesFactory.getUTC();
36          AbsoluteDate afterLeap = new AbsoluteDate(1961, 1, 1, 0, 0, 0.0, utc);
37          Assert.assertEquals(1.4228180,
38                              afterLeap.durationFrom(utc.getFirstKnownLeapSecond()),
39                              1.0e-12);
40          afterLeap = new AbsoluteDate(2006, 1, 1, 0, 0, 0.0, utc);
41          Assert.assertEquals(1.0,
42                              afterLeap.durationFrom(utc.getLastKnownLeapSecond()),
43                              1.0e-12);
44  
45          // correct values, as the single leap second is close enough
46          checkOffset("2000-01-01", -32.0);
47          checkOffset("2008-01-01", -33.0);
48  
49          // expected wrong estimation as the leap seconds from 1997-01-01 and 1999-01-01 are not known from the file read
50          checkOffset("1996-04-03", -32.0);
51  
52          // expected wrong estimation as the leap seconds from 2009-01-01 and 2012-07-01 are not known from the file read
53          checkOffset("2013-01-22", -33.0);
54  
55      }
56  
57      @Test
58      public void test2009WrongLeap() {
59          Utils.setDataRoot("bulletinA");
60          // this file contains a single leap second on 2009-01-01, from 33s to 34s,
61          // but it has a known ERROR in it, as line 66 reads:
62          //    TAI-UTC(MJD 54832) = 33.0
63          // whereas the value should be 34.0, as the leap second was introduced
64          // just before this day
65          TimeScalesFactory.addUTCTAIOffsetsLoader(new UTCTAIBulletinAFilesLoader("bulletina-xxi-053-original\\.txt$"));
66  
67          UTCScale utc = TimeScalesFactory.getUTC();
68          AbsoluteDate afterLeap = new AbsoluteDate(1961, 1, 1, 0, 0, 0.0, utc);
69          Assert.assertEquals(1.4228180,
70                              afterLeap.durationFrom(utc.getFirstKnownLeapSecond()),
71                              1.0e-12);
72          afterLeap = new AbsoluteDate(2009, 1, 1, 0, 0, 0.0, utc);
73          Assert.assertEquals(1.0,
74                              afterLeap.durationFrom(utc.getLastKnownLeapSecond()),
75                              1.0e-12);
76  
77          // expected incorrect values, as the file contains an error
78          checkOffset("2008-01-01", -32.0); // the real value should be -33.0
79          checkOffset("2009-06-30", -33.0); // the real value should be -34.0
80  
81      }
82  
83      @Test
84      public void test2009FixedLeap() {
85          Utils.setDataRoot("bulletinA");
86          // this file is a fixed version of IERS bulletin
87          TimeScalesFactory.addUTCTAIOffsetsLoader(new UTCTAIBulletinAFilesLoader("bulletina-xxi-053-fixed\\.txt$"));
88  
89          UTCScale utc = TimeScalesFactory.getUTC();
90          AbsoluteDate afterLeap = new AbsoluteDate(1961, 1, 1, 0, 0, 0.0, utc);
91          Assert.assertEquals(1.4228180,
92                              afterLeap.durationFrom(utc.getFirstKnownLeapSecond()),
93                              1.0e-12);
94          afterLeap = new AbsoluteDate(2009, 1, 1, 0, 0, 0.0, utc);
95          Assert.assertEquals(1.0,
96                              afterLeap.durationFrom(utc.getLastKnownLeapSecond()),
97                              1.0e-12);
98  
99          // correct values, as the original file error has been fixed
100         checkOffset("2008-01-01", -33.0);
101         checkOffset("2009-06-30", -34.0);
102 
103     }
104 
105     @Test
106     public void testNoLeap() {
107         Utils.setDataRoot("bulletinA");
108         // these files contains no leap seconds
109         TimeScalesFactory.addUTCTAIOffsetsLoader(new UTCTAIBulletinAFilesLoader("bulletina-xxvi.*\\.txt$"));
110 
111         UTCScale utc = TimeScalesFactory.getUTC();
112         AbsoluteDate afterLeap = new AbsoluteDate(1961, 1, 1, 0, 0, 0.0, utc);
113         Assert.assertEquals(1.4228180,
114                             afterLeap.durationFrom(utc.getFirstKnownLeapSecond()),
115                             1.0e-12);
116 
117         // the artificial first leap is big ...
118         afterLeap = new AbsoluteDate(1972, 1, 1, 0, 0, 0.0, utc);
119         Assert.assertTrue(afterLeap.durationFrom(utc.getLastKnownLeapSecond()) > 25.1);
120 
121         // as there are no leap seconds identified, everything should be at 35s
122         checkOffset("1973-01-01", -35.0);
123         checkOffset("2000-01-01", -35.0);
124         checkOffset("2002-01-01", -35.0);
125         checkOffset("2004-01-01", -35.0);
126         checkOffset("2006-01-01", -35.0);
127         checkOffset("2008-01-01", -35.0);
128         checkOffset("2010-01-01", -35.0);
129         checkOffset("2012-01-01", -35.0);
130         checkOffset("2014-01-01", -35.0);
131         checkOffset("2100-01-01", -35.0);
132 
133     }
134 
135     @Test
136     public void testMissingTimeSteps() {
137         checkException("bulletina-(?:xix|xxii)-001\\.txt",
138                        OrekitMessages.MISSING_EARTH_ORIENTATION_PARAMETERS_BETWEEN_DATES);
139     }
140 
141     @Test
142     public void testMissingRapidSections() {
143         checkException("bulletina-missing-eop-rapid-service.txt",
144                        OrekitMessages.NOT_A_SUPPORTED_IERS_DATA_FILE);
145         checkException("bulletina-missing-eop-rapid-service.txt",
146                        OrekitMessages.NOT_A_SUPPORTED_IERS_DATA_FILE);
147     }
148 
149     @Test
150     public void testMissingData() {
151         checkException("bulletina-truncated-in-prediction-header.txt",
152                        OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE);
153         checkException("bulletina-truncated-after-prediction-header.txt",
154                        OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE);
155     }
156 
157     @Test
158     public void testInconsistentDate() {
159         checkException("bulletina-inconsistent-year.txt", OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE);
160         checkException("bulletina-inconsistent-month.txt", OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE);
161         checkException("bulletina-inconsistent-day.txt", OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE);
162     }
163 
164     private void checkOffset(final String s, final double expected) {
165         final AbsoluteDate date = new AbsoluteDate(s, TimeScalesFactory.getTAI());
166         Assert.assertEquals(expected, TimeScalesFactory.getUTC().offsetFromTAI(date), 10e-8);
167     }
168 
169     private void checkException(String name, OrekitMessages message) {
170         Utils.setDataRoot("bulletinA");
171         TimeScalesFactory.addUTCTAIOffsetsLoader(new UTCTAIBulletinAFilesLoader(name));
172         try {
173             TimeScalesFactory.getUTC();
174             Assert.fail("an exception should have been thrown");
175         } catch (OrekitException oe) {
176             Assert.assertEquals(message, oe.getSpecifier());
177         }
178     }
179 
180 }