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.forces.gravity.potential;
18  
19  import org.hipparchus.util.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.orekit.Utils;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.forces.gravity.potential.NormalizedSphericalHarmonicsProvider.NormalizedSphericalHarmonics;
26  import org.orekit.forces.gravity.potential.UnnormalizedSphericalHarmonicsProvider.UnnormalizedSphericalHarmonics;
27  import org.orekit.time.AbsoluteDate;
28  import org.orekit.time.TimeScalesFactory;
29  import org.orekit.utils.Constants;
30  
31  public class GRGSFormatReaderTest {
32  
33      @Test
34      void testAdditionalColumn() {
35          GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("grim5-c1.txt", true));
36          UnnormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getUnnormalizedProvider(5, 5);
37  
38          Assertions.assertEquals(TideSystem.UNKNOWN, provider.getTideSystem());
39          AbsoluteDate date = new AbsoluteDate("2011-05-01T01:02:03", TimeScalesFactory.getTT());
40  
41          UnnormalizedSphericalHarmonics harmonics = provider.onDate(date);
42          int maxUlps = 2;
43          checkValue(harmonics.getUnnormalizedCnm(3, 0), date, 3, 0,
44                     1997, 1, 1, 0.95857491635129E-06, 0.28175700027753E-11, maxUlps);
45          checkValue(harmonics.getUnnormalizedCnm(5, 5), date, 5, 5,
46                     1997, 1, 1, 0.17481512311600E-06, 0.0, maxUlps);
47          checkValue(harmonics.getUnnormalizedSnm(4, 0), date, 4, 0,
48                     1997, 1, 1, 0, 0, maxUlps);
49          checkValue(harmonics.getUnnormalizedSnm(4, 4), date, 4, 4,
50                     1997, 1, 1, 0.30882755318300E-06, 0, maxUlps);
51          Assertions.assertEquals(0.3986004415E+15 , provider.getMu(),  0);
52          Assertions.assertEquals(0.6378136460E+07 , provider.getAe(),  0);
53  
54      }
55  
56      @Test
57      void testRegular05cNormalized() {
58          GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("grim5_C1.dat", true));
59          NormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getNormalizedProvider(5, 5);
60          Assertions.assertEquals(TideSystem.UNKNOWN, provider.getTideSystem());
61  
62          AbsoluteDate refDate = new AbsoluteDate("1997-01-01T12:00:00", TimeScalesFactory.getTT());
63          AbsoluteDate date = new AbsoluteDate("2011-05-01T01:02:03", TimeScalesFactory.getTT());
64  
65          double offset     = date.durationFrom(refDate);
66          double offsetYear = offset / Constants.JULIAN_YEAR;
67          NormalizedSphericalHarmonics harmonics = provider.onDate(date);
68          Assertions.assertEquals(0.95857491635129E-06 + offsetYear * 0.28175700027753E-11,
69                              harmonics.getNormalizedCnm(3, 0), 1.0e-15);
70          Assertions.assertEquals( 0.17481512311600E-06, harmonics.getNormalizedCnm(5, 5), 1.0e-15);
71          Assertions.assertEquals( 0.0,                harmonics.getNormalizedSnm(4, 0), 1.0e-15);
72          Assertions.assertEquals( 0.30882755318300E-06, harmonics.getNormalizedSnm(4, 4), 1.0e-15);
73          Assertions.assertEquals(0.3986004415E+15 , provider.getMu(),  0);
74          Assertions.assertEquals(0.6378136460E+07 , provider.getAe(),  0);
75      }
76  
77      @Test
78      void testRegular05cUnnormalized() {
79          GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("grim5_C1.dat", true));
80          UnnormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getUnnormalizedProvider(5, 5);
81  
82          AbsoluteDate date = new AbsoluteDate("2011-05-01T01:02:03", TimeScalesFactory.getTT());
83  
84          UnnormalizedSphericalHarmonics harmonics = provider.onDate(date);
85          int maxUlps = 2;
86          checkValue(harmonics.getUnnormalizedCnm(3, 0), date, 3, 0,
87                     1997, 1, 1, 0.95857491635129E-06, 0.28175700027753E-11, maxUlps);
88          checkValue(harmonics.getUnnormalizedCnm(5, 5), date, 5, 5,
89                     1997, 1, 1, 0.17481512311600E-06, 0.0, maxUlps);
90          checkValue(harmonics.getUnnormalizedSnm(4, 0), date, 4, 0,
91                     1997, 1, 1, 0, 0, maxUlps);
92          checkValue(harmonics.getUnnormalizedSnm(4, 4), date, 4, 4,
93                     1997, 1, 1, 0.30882755318300E-06, 0, maxUlps);
94          Assertions.assertEquals(0.3986004415E+15 , provider.getMu(),  0);
95          Assertions.assertEquals(0.6378136460E+07 , provider.getAe(),  0);
96  
97      }
98  
99      @Test
100     void testReadLimits() {
101         GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("grim5_C1.dat", true));
102         UnnormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getUnnormalizedProvider(3, 2);
103         UnnormalizedSphericalHarmonics harmonics = provider.onDate(new AbsoluteDate(1997, 1, 1, 12, 0, 0.0, TimeScalesFactory.getTT()));
104         try {
105             harmonics.getUnnormalizedCnm(3, 3);
106             Assertions.fail("an exception should have been thrown");
107         } catch (OrekitException oe) {
108             // expected
109         } catch (Exception e) {
110             Assertions.fail("wrong exception caught: " + e.getLocalizedMessage());
111         }
112         try {
113             harmonics.getUnnormalizedCnm(4, 2);
114             Assertions.fail("an exception should have been thrown");
115         } catch (OrekitException oe) {
116             // expected
117         } catch (Exception e) {
118             Assertions.fail("wrong exception caught: " + e.getLocalizedMessage());
119         }
120         harmonics.getUnnormalizedCnm(3, 2);
121         Assertions.assertEquals(3, provider.getMaxDegree());
122         Assertions.assertEquals(2, provider.getMaxOrder());
123     }
124 
125     @Test
126     void testCorruptedFile1() {
127         Assertions.assertThrows(OrekitException.class, () -> {
128             GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("corrupted-1-grim5.dat", false));
129             GravityFieldFactory.getUnnormalizedProvider(5, 5);
130         });
131     }
132 
133     @Test
134     void testCorruptedFile2() {
135         Assertions.assertThrows(OrekitException.class, () -> {
136             GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("corrupted-2-grim5.dat", false));
137             GravityFieldFactory.getUnnormalizedProvider(5, 5);
138         });
139     }
140 
141     @Test
142     void testCorruptedFile3() {
143         Assertions.assertThrows(OrekitException.class, () -> {
144             GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("corrupted-3-grim5.dat", false));
145             GravityFieldFactory.getUnnormalizedProvider(5, 5);
146         });
147     }
148 
149     @Test
150     void testCorruptedFile4() {
151         Assertions.assertThrows(OrekitException.class, () -> {
152             GravityFieldFactory.addPotentialCoefficientsReader(new GRGSFormatReader("corrupted-4-grim5.dat", false));
153             GravityFieldFactory.getUnnormalizedProvider(5, 5);
154         });
155     }
156 
157     @BeforeEach
158     public void setUp() {
159         Utils.setDataRoot("potential:regular-data");
160     }
161 
162     private void checkValue(final double value,
163                             final AbsoluteDate date, final int n, final int m,
164                             final int refYear, final int refMonth, final int refDay,
165                             final double constant, final double trend,
166                             final int maxUlps) {
167         double factor = GravityFieldFactory.getUnnormalizationFactors(n, m)[n][m];
168         AbsoluteDate refDate = new AbsoluteDate(refYear, refMonth, refDay, 12, 0, 0, TimeScalesFactory.getTT());
169         double dtYear = date.durationFrom(refDate) / Constants.JULIAN_YEAR;
170         double normalized = factor * (constant + trend * dtYear);
171         double epsilon = maxUlps * FastMath.ulp(normalized);
172         Assertions.assertEquals(normalized, value, epsilon);
173     }
174 
175 }