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  
20  import java.util.SortedSet;
21  import java.util.TreeSet;
22  
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.orekit.data.AbstractFilesLoaderTest;
26  import org.orekit.errors.OrekitException;
27  import org.orekit.errors.OrekitMessages;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.time.ChronologicalComparator;
30  import org.orekit.time.TimeScalesFactory;
31  import org.orekit.utils.Constants;
32  import org.orekit.utils.IERSConventions;
33  
34  
35  public class BulletinBFilesLoaderTest extends AbstractFilesLoaderTest {
36  
37      @Test
38      public void testMissingMonths() {
39          setRoot("missing-months");
40          IERSConventions.NutationCorrectionConverter converter =
41                  IERSConventions.IERS_2010.getNutationCorrectionConverter();
42           SortedSet<EOPEntry> history = new TreeSet<EOPEntry>(new ChronologicalComparator());
43          new BulletinBFilesLoader(FramesFactory.BULLETINB_2000_FILENAME, manager, () -> utc).fillHistory(converter, history);
44          Assert.assertTrue(getMaxGap(history) > 5);
45      }
46  
47      @Test
48      public void testStartDate() {
49          setRoot("regular-data");
50          IERSConventions.NutationCorrectionConverter converter =
51                  IERSConventions.IERS_2010.getNutationCorrectionConverter();
52          SortedSet<EOPEntry> history = new TreeSet<EOPEntry>(new ChronologicalComparator());
53          new BulletinBFilesLoader(FramesFactory.BULLETINB_2000_FILENAME, manager, () -> utc).fillHistory(converter, history);
54          Assert.assertEquals(new AbsoluteDate(2005, 12, 5, TimeScalesFactory.getUTC()),
55                              new EOPHistory(IERSConventions.IERS_2010, history, true).getStartDate());
56      }
57  
58      @Test
59      public void testEndDate() {
60          setRoot("regular-data");
61          IERSConventions.NutationCorrectionConverter converter =
62                  IERSConventions.IERS_2010.getNutationCorrectionConverter();
63          SortedSet<EOPEntry> history = new TreeSet<EOPEntry>(new ChronologicalComparator());
64          new BulletinBFilesLoader(FramesFactory.BULLETINB_2000_FILENAME, manager, () -> utc).fillHistory(converter, history);
65          Assert.assertTrue(getMaxGap(history) < 5);
66          Assert.assertEquals(new AbsoluteDate(2006, 3, 5, TimeScalesFactory.getUTC()),
67                              new EOPHistory(IERSConventions.IERS_2010, history, false).getEndDate());
68      }
69  
70      @Test
71      public void testNewFormatNominal() {
72          setRoot("new-bulletinB");
73          IERSConventions.NutationCorrectionConverter converter =
74                  IERSConventions.IERS_2010.getNutationCorrectionConverter();
75          SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
76          new BulletinBFilesLoader("^bulletinb\\.270$", manager, () -> utc).fillHistory(converter, data);
77          EOPHistory history = new EOPHistory(IERSConventions.IERS_2010, data, true);
78          Assert.assertEquals(new AbsoluteDate(2010, 6, 2, TimeScalesFactory.getUTC()),
79                              history.getStartDate());
80          Assert.assertEquals(new AbsoluteDate(2010, 7, 1, TimeScalesFactory.getUTC()),
81                              history.getEndDate());
82      }
83  
84      @Test
85      public void testOldFormatContent() {
86          setRoot("regular-data");
87          IERSConventions.NutationCorrectionConverter converter =
88                  IERSConventions.IERS_2010.getNutationCorrectionConverter();
89          SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
90          new BulletinBFilesLoader(FramesFactory.BULLETINB_2000_FILENAME, manager, () -> utc).fillHistory(converter, data);
91          EOPHistory history = new EOPHistory(IERSConventions.IERS_2010, data, true);
92          AbsoluteDate date = new AbsoluteDate(2006, 1, 11, 12, 0, 0, TimeScalesFactory.getUTC());
93          Assert.assertEquals(msToS(  (-3 * 0.073    + 27 * -0.130   + 27 * -0.244   - 3 * -0.264)   / 48), history.getLOD(date), 1.0e-10);
94          Assert.assertEquals(        (-3 * 0.333275 + 27 * 0.333310 + 27 * 0.333506 - 3 * 0.333768) / 48,  history.getUT1MinusUTC(date), 1.0e-10);
95          Assert.assertEquals(asToRad((-3 * 0.04958  + 27 * 0.04927  + 27 * 0.04876  - 3 * 0.04854)  / 48), history.getPoleCorrection(date).getXp(), 1.0e-10);
96          Assert.assertEquals(asToRad((-3 * 0.38117  + 27 * 0.38105  + 27 * 0.38071  - 3 * 0.38036)  / 48), history.getPoleCorrection(date).getYp(), 1.0e-10);
97      }
98  
99      @Test
100     public void testOldFormat1980() {
101         setRoot("old-bulletinB");
102         IERSConventions.NutationCorrectionConverter converter =
103                 IERSConventions.IERS_1996.getNutationCorrectionConverter();
104         SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
105         new BulletinBFilesLoader("^bulletinb_IAU1980-220\\.txt$", manager, () -> utc).fillHistory(converter, data);
106         EOPHistory history = new EOPHistory(IERSConventions.IERS_1996, data, true);
107         Assert.assertEquals(new AbsoluteDate(2006,  4,  4, TimeScalesFactory.getUTC()),
108                             history.getStartDate());
109         Assert.assertEquals(new AbsoluteDate(2006,  5,  4, TimeScalesFactory.getUTC()),
110                             history.getEndDate());
111     }
112 
113     @Test
114     public void testOldFormat1980RemovedFirstDates() {
115         setRoot("old-bulletinB");
116         IERSConventions.NutationCorrectionConverter converter =
117                 IERSConventions.IERS_1996.getNutationCorrectionConverter();
118         SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
119         new BulletinBFilesLoader("^bulletinb_IAU1980-220-edited\\.txt$", manager, () -> utc).fillHistory(converter, data);
120         EOPHistory history = new EOPHistory(IERSConventions.IERS_1996, data, true);
121         Assert.assertEquals(new AbsoluteDate(2006,  4, 14, TimeScalesFactory.getUTC()),
122                             history.getStartDate());
123         Assert.assertEquals(new AbsoluteDate(2006,  5,  4, TimeScalesFactory.getUTC()),
124                             history.getEndDate());
125     }
126 
127     @Test
128     public void testOldFormatTruncated() {
129         setRoot("old-bulletinB");
130         IERSConventions.NutationCorrectionConverter converter =
131                 IERSConventions.IERS_2010.getNutationCorrectionConverter();
132         SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
133         try {
134             new BulletinBFilesLoader("^bulletinb_IAU2000-216-truncated\\.txt$", manager, () -> utc).fillHistory(converter, data);
135         } catch (OrekitException oe) {
136             Assert.assertEquals(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE, oe.getSpecifier());
137             Assert.assertEquals(54, ((Integer) oe.getParts()[1]).intValue());
138         }
139     }
140 
141     @Test
142     public void testNewFormatContent() {
143         setRoot("new-bulletinB");
144         IERSConventions.NutationCorrectionConverter converter =
145                 IERSConventions.IERS_2010.getNutationCorrectionConverter();
146         SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
147         new BulletinBFilesLoader("^bulletinb\\.270$", manager, () -> utc).fillHistory(converter, data);
148         EOPHistory history = new EOPHistory(IERSConventions.IERS_2010, data, true);
149         AbsoluteDate date = new AbsoluteDate(2010, 6, 12, 12, 0, 0, TimeScalesFactory.getUTC());
150         Assert.assertEquals(msToS((   -3 *   0.1202 + 27 *   0.0294 + 27 *   0.0682 - 3 *   0.1531) / 48), history.getLOD(date), 1.0e-10);
151         Assert.assertEquals(msToS((   -3 * -57.1711 + 27 * -57.2523 + 27 * -57.3103 - 3 * -57.4101) / 48), history.getUT1MinusUTC(date), 1.0e-10);
152         Assert.assertEquals(masToRad((-3 *  -1.216  + 27 *   1.658  + 27 *   4.926  - 3 *   7.789)  / 48), history.getPoleCorrection(date).getXp(), 1.0e-10);
153         Assert.assertEquals(masToRad((-3 * 467.780  + 27 * 469.330  + 27 * 470.931  - 3 * 472.388)  / 48), history.getPoleCorrection(date).getYp(), 1.0e-10);
154         Assert.assertEquals(masToRad((-3 *   0.097  + 27 *   0.089  + 27 *   0.050  - 3 *  -0.007)  / 48), history.getNonRotatinOriginNutationCorrection(date)[0],  1.0e-10);
155         Assert.assertEquals(masToRad((-3 *   0.071  + 27 *   0.066  + 27 *   0.090  - 3 *   0.111)  / 48), history.getNonRotatinOriginNutationCorrection(date)[1],  1.0e-10);
156     }
157 
158     @Test
159     public void testNewFormatRemovedFirstDates() {
160         setRoot("new-bulletinB");
161         IERSConventions.NutationCorrectionConverter converter =
162                 IERSConventions.IERS_2010.getNutationCorrectionConverter();
163         SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
164         new BulletinBFilesLoader("^bulletinb-edited\\.270$", manager, () -> utc).fillHistory(converter, data);
165         EOPHistory history = new EOPHistory(IERSConventions.IERS_2010, data, true);
166         Assert.assertEquals(new AbsoluteDate(2010,  6, 11, TimeScalesFactory.getUTC()),
167                             history.getStartDate());
168     }
169 
170     private double msToS(double ms) {
171         return ms / 1000.0;
172     }
173 
174     private double asToRad(double mas) {
175         return mas * Constants.ARC_SECONDS_TO_RADIANS;
176     }
177 
178     private double masToRad(double mas) {
179         return mas * Constants.ARC_SECONDS_TO_RADIANS / 1000.0;
180     }
181 
182     @Test(expected=OrekitException.class)
183     public void testNewFormatTruncated() {
184         setRoot("new-bulletinB");
185         IERSConventions.NutationCorrectionConverter converter =
186                 IERSConventions.IERS_2010.getNutationCorrectionConverter();
187        SortedSet<EOPEntry> history = new TreeSet<EOPEntry>(new ChronologicalComparator());
188         new BulletinBFilesLoader("^bulletinb-truncated\\.270$", manager, () -> utc).fillHistory(converter, history);
189     }
190 
191     @Test(expected=OrekitException.class)
192     public void testNewFormatTruncatedEarly() {
193         setRoot("new-bulletinB");
194         IERSConventions.NutationCorrectionConverter converter =
195                 IERSConventions.IERS_2010.getNutationCorrectionConverter();
196        SortedSet<EOPEntry> history = new TreeSet<EOPEntry>(new ChronologicalComparator());
197         new BulletinBFilesLoader("^bulletinb-truncated-early\\.270$", manager, () -> utc).fillHistory(converter, history);
198     }
199 
200     @Test(expected=OrekitException.class)
201     public void testNewFormatInconsistent() {
202         setRoot("new-bulletinB");
203         IERSConventions.NutationCorrectionConverter converter =
204                 IERSConventions.IERS_2010.getNutationCorrectionConverter();
205        SortedSet<EOPEntry> history = new TreeSet<EOPEntry>(new ChronologicalComparator());
206         new BulletinBFilesLoader("^bulletinb-inconsistent\\.270$", manager, () -> utc).fillHistory(converter, history);
207     }
208 
209     @Test
210     public void testNewFormatInconsistentDate() {
211         setRoot("new-bulletinB");
212         IERSConventions.NutationCorrectionConverter converter =
213                 IERSConventions.IERS_2010.getNutationCorrectionConverter();
214        SortedSet<EOPEntry> history = new TreeSet<EOPEntry>(new ChronologicalComparator());
215        try {
216            new BulletinBFilesLoader("bulletinb-inconsistent-date.270", manager, () -> utc).fillHistory(converter, history);
217            Assert.fail("an exception should have been thrown");
218        } catch (OrekitException oe) {
219            Assert.assertEquals(OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE, oe.getSpecifier());
220        }
221     }
222 
223 }