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.forces.gravity.potential;
18  
19  
20  import org.hipparchus.util.FastMath;
21  import org.hipparchus.util.Precision;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.orekit.Utils;
25  import org.orekit.errors.OrekitException;
26  import org.orekit.forces.gravity.potential.NormalizedSphericalHarmonicsProvider.NormalizedSphericalHarmonics;
27  import org.orekit.forces.gravity.potential.UnnormalizedSphericalHarmonicsProvider.UnnormalizedSphericalHarmonics;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.utils.Constants;
30  
31  public class EGMFormatReaderTest {
32  
33      @Test
34      public void testReadNormalized() {
35          Utils.setDataRoot("potential");
36          GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("egm96_to5.ascii", true));
37          NormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getNormalizedProvider(5, 5);
38          NormalizedSphericalHarmonics harmonics = provider.onDate(AbsoluteDate.FUTURE_INFINITY);
39          Assert.assertEquals(TideSystem.TIDE_FREE, provider.getTideSystem());
40          Assert.assertEquals( 0.957254173792E-06, harmonics.getNormalizedCnm(3, 0), 1.0e-15);
41          Assert.assertEquals( 0.174971983203E-06, harmonics.getNormalizedCnm(5, 5), 1.0e-15);
42          Assert.assertEquals( 0.0,                harmonics.getNormalizedSnm(4, 0), 1.0e-15);
43          Assert.assertEquals( 0.308853169333E-06, harmonics.getNormalizedSnm(4, 4), 1.0e-15);
44          Assert.assertEquals(-0.295301647654E-06, harmonics.getNormalizedCnm(5, 4), 1.0e-15);
45      }
46  
47      @Test
48      public void testReadUnnormalized() {
49          Utils.setDataRoot("potential");
50          GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("egm96_to5.ascii", true));
51          UnnormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getUnnormalizedProvider(5, 5);
52          UnnormalizedSphericalHarmonics harmonics = provider.onDate(AbsoluteDate.FUTURE_INFINITY);
53          Assert.assertEquals(TideSystem.TIDE_FREE, provider.getTideSystem());
54          int maxUlps = 1;
55          checkValue(harmonics.getUnnormalizedCnm(3, 0), 3, 0, 0.957254173792E-06, maxUlps);
56          checkValue(harmonics.getUnnormalizedCnm(5, 5), 5, 5, 0.174971983203E-06, maxUlps);
57          checkValue(harmonics.getUnnormalizedSnm(4, 0), 4, 0, 0.0,                maxUlps);
58          checkValue(harmonics.getUnnormalizedSnm(4, 4), 4, 4, 0.308853169333E-06, maxUlps);
59  
60          double a = (-0.295301647654E-06);
61          double b = 9*8*7*6*5*4*3*2;
62          double c = 2*11/b;
63          double result = a*FastMath.sqrt(c);
64  
65          Assert.assertEquals(result, harmonics.getUnnormalizedCnm(5, 4), 1.0e-20);
66  
67          a = -0.188560802735E-06;
68          b = 8*7*6*5*4*3*2;
69          c=2*9/b;
70          result = a*FastMath.sqrt(c);
71          Assert.assertEquals(result, harmonics.getUnnormalizedCnm(4, 4), 1.0e-20);
72  
73          Assert.assertEquals(1.0826266835531513e-3, -harmonics.getUnnormalizedCnm(2, 0), 1.0e-20);
74  
75      }
76  
77      @Test
78      @Deprecated
79      public void testDeprecated() throws OrekitException {
80          Utils.setDataRoot("potential");
81          GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("egm96_to5.ascii", true));
82          UnnormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getUnnormalizedProvider(5, 5);
83          Assert.assertNull(provider.getReferenceDate());
84          Assert.assertEquals(0, provider.getOffset(AbsoluteDate.J2000_EPOCH), Precision.SAFE_MIN);
85          Assert.assertEquals(0, provider.getOffset(AbsoluteDate.MODIFIED_JULIAN_EPOCH), Precision.SAFE_MIN);
86      }
87  
88      @Test
89      public void testReadLimits() {
90          Utils.setDataRoot("potential");
91          GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("egm96_to5.ascii", true));
92          UnnormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getUnnormalizedProvider(3, 2);
93          UnnormalizedSphericalHarmonics harmonics = provider.onDate(null);
94          try {
95              harmonics.getUnnormalizedCnm(3, 3);
96              Assert.fail("an exception should have been thrown");
97          } catch (OrekitException oe) {
98              // expected
99          } catch (Exception e) {
100             Assert.fail("wrong exception caught: " + e.getLocalizedMessage());
101         }
102         try {
103             harmonics.getUnnormalizedCnm(4, 2);
104             Assert.fail("an exception should have been thrown");
105         } catch (OrekitException oe) {
106             // expected
107         } catch (Exception e) {
108             Assert.fail("wrong exception caught: " + e.getLocalizedMessage());
109         }
110         harmonics.getUnnormalizedCnm(3, 2);
111         Assert.assertEquals(3, provider.getMaxDegree());
112         Assert.assertEquals(2, provider.getMaxOrder());
113     }
114 
115     @Test(expected=OrekitException.class)
116     public void testCorruptedFile1() {
117         Utils.setDataRoot("potential");
118         GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("corrupted-1-egm96_to5", false));
119         GravityFieldFactory.getUnnormalizedProvider(5, 5);
120     }
121 
122     @Test(expected=OrekitException.class)
123     public void testCorruptedFile2() {
124         Utils.setDataRoot("potential");
125         GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("corrupted-2-egm96_to5", false));
126         GravityFieldFactory.getUnnormalizedProvider(5, 5);
127     }
128 
129     @Test(expected=OrekitException.class)
130     public void testCorruptedFile3() {
131         Utils.setDataRoot("potential");
132         GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("corrupted-3-egm96_to5", false));
133         GravityFieldFactory.getUnnormalizedProvider(5, 5);
134     }
135 
136     @Test
137     public void testZeroTidePattern1() {
138         Utils.setDataRoot("potential");
139         GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("dummy_egm2008", true));
140         Assert.assertEquals(TideSystem.ZERO_TIDE,
141                             GravityFieldFactory.getUnnormalizedProvider(5, 5).getTideSystem());
142     }
143 
144     @Test
145     public void testZeroTidePattern2() {
146         Utils.setDataRoot("potential");
147         GravityFieldFactory.addPotentialCoefficientsReader(new EGMFormatReader("dummy_zerotide", true));
148         Assert.assertEquals(TideSystem.ZERO_TIDE,
149                             GravityFieldFactory.getUnnormalizedProvider(5, 5).getTideSystem());
150     }
151 
152     @Test
153     public void testWgs84CoefficientOverride()
154         {
155         final double epsilon = Precision.EPSILON;
156 
157         Utils.setDataRoot("potential");
158         EGMFormatReader egm96Reader = new EGMFormatReader("egm96_to5.ascii", true);
159         GravityFieldFactory.addPotentialCoefficientsReader(egm96Reader);
160         GravityFieldFactory.getNormalizedProvider(5, 5);
161         Assert.assertEquals(Constants.EGM96_EARTH_EQUATORIAL_RADIUS, egm96Reader.getAe(), epsilon);
162         Assert.assertEquals(Constants.EGM96_EARTH_MU, egm96Reader.getMu(), epsilon);
163 
164         Utils.setDataRoot("potential");
165         EGMFormatReader wgs84Egm96Reader = new EGMFormatReader("egm96_to5.ascii", true, true);
166         GravityFieldFactory.addPotentialCoefficientsReader(wgs84Egm96Reader);
167         GravityFieldFactory.getNormalizedProvider(5, 5);
168         Assert.assertEquals(Constants.WGS84_EARTH_EQUATORIAL_RADIUS, wgs84Egm96Reader.getAe(), epsilon);
169         Assert.assertEquals(Constants.WGS84_EARTH_MU, wgs84Egm96Reader.getMu(), epsilon);
170 
171     }
172 
173     private void checkValue(final double value, final int n, final int m,
174                             final double constant, final int maxUlps)
175         {
176         double factor = GravityFieldFactory.getUnnormalizationFactors(n, m)[n][m];
177         double normalized = factor * constant;
178         double epsilon = maxUlps * FastMath.ulp(normalized);
179         Assert.assertEquals(normalized, value, epsilon);
180     }
181 
182 }