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 java.util.List;
20  import java.util.SortedSet;
21  import java.util.TreeSet;
22  
23  import org.hipparchus.stat.descriptive.rank.Percentile;
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  import org.orekit.data.AbstractFilesLoaderTest;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.time.ChronologicalComparator;
30  import org.orekit.utils.IERSConventions;
31  
32  
33  public class EopCsvFilesLoaderTest extends AbstractFilesLoaderTest {
34  
35      @Test
36      public void testEopc04Rates() {
37          EOPHistory history = load("eopc04_20.2022-now.csv");
38          Assertions.assertEquals(IERSConventions.IERS_2010, history.getConventions());
39          Assertions.assertEquals(new AbsoluteDate(2022, 1, 1, utc), history.getStartDate());
40          Assertions.assertEquals(new AbsoluteDate(2023, 8, 28, utc), history.getEndDate());
41          checkRatesConsistency(history, 0.049, 0.072, 0.063, 0.046);
42      }
43  
44      @Test
45      public void testBulletinAWithoutRates() {
46          EOPHistory history = load("bulletina-xxxvi-037.csv");
47          Assertions.assertEquals(IERSConventions.IERS_2010, history.getConventions());
48          Assertions.assertEquals(new AbsoluteDate(2023, 8, 24, utc), history.getStartDate());
49          Assertions.assertEquals(new AbsoluteDate(2024, 9, 13, utc), history.getEndDate());
50      }
51  
52      @Test
53      public void testBulletinAWithRatesOnlyInHeader() {
54          EOPHistory history = load("bulletina-xxxvi-038.csv");
55          Assertions.assertEquals(IERSConventions.IERS_2010, history.getConventions());
56          Assertions.assertEquals(new AbsoluteDate(2023, 8, 30, utc), history.getStartDate());
57          Assertions.assertEquals(new AbsoluteDate(2024, 9, 20, utc), history.getEndDate());
58      }
59  
60      @Test
61      public void testBulletinBWithoutRates() {
62          EOPHistory history = load("bulletinb-423.csv");
63          Assertions.assertEquals(IERSConventions.IERS_2010, history.getConventions());
64          Assertions.assertEquals(new AbsoluteDate(2023, 3, 2, utc), history.getStartDate());
65          Assertions.assertEquals(new AbsoluteDate(2023, 5, 1, utc), history.getEndDate());
66      }
67  
68      @Test
69      public void testBulletinBWithRatesOnlyInHeader() {
70          EOPHistory history = load("bulletinb-427.csv");
71          Assertions.assertEquals(IERSConventions.IERS_2010, history.getConventions());
72          Assertions.assertEquals(new AbsoluteDate(2023, 7, 2, utc), history.getStartDate());
73          Assertions.assertEquals(new AbsoluteDate(2023, 9, 1, utc), history.getEndDate());
74      }
75  
76      private EOPHistory load(final String name) {
77          IERSConventions.NutationCorrectionConverter converter =
78                          IERSConventions.IERS_2010.getNutationCorrectionConverter();
79          SortedSet<EOPEntry> data = new TreeSet<EOPEntry>(new ChronologicalComparator());
80          new EopCsvFilesLoader("^" + name + "$", manager, () -> utc).
81          fillHistory(converter, data);
82          return new EOPHistory(IERSConventions.IERS_2010, EOPHistory.DEFAULT_INTERPOLATION_DEGREE, data, true);
83      }
84  
85      private final void checkRatesConsistency(final EOPHistory history,
86                                               final double tol10X, final double tol90X,
87                                               final double tol10Y, final double tol90Y) {
88          
89          final List<EOPEntry> entries = history.getEntries();
90          double[] sampleX = new double[entries.size() - 2];
91          double[] sampleY = new double[entries.size() - 2];
92          for (int i = 1; i < entries.size() - 1; ++i) {
93              final EOPEntry previous = entries.get(i - 1);
94              final EOPEntry current  = entries.get(i);
95              final EOPEntry next     = entries.get(i + 1);
96              final double xRate = (next.getX() - previous.getX()) / next.durationFrom(previous);
97              final double yRate = (next.getY() - previous.getY()) / next.durationFrom(previous);
98              sampleX[i - 1] = (xRate - current.getXRate()) / xRate;
99              sampleY[i - 1] = (yRate - current.getYRate()) / yRate;
100         }
101 
102         Assertions.assertEquals(0.0, new Percentile(10.0).evaluate(sampleX), tol10X);
103         Assertions.assertEquals(0.0, new Percentile(90.0).evaluate(sampleX), tol90X);
104         Assertions.assertEquals(0.0, new Percentile(10.0).evaluate(sampleY), tol10Y);
105         Assertions.assertEquals(0.0, new Percentile(90.0).evaluate(sampleY), tol90Y);
106 
107     }
108 
109     @BeforeEach
110     public void setUp() {
111         setRoot("eop-csv");
112     }
113 
114 }