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.hipparchus.util.Precision;
21  import org.orekit.errors.OrekitException;
22  import org.orekit.errors.OrekitMessages;
23  import org.orekit.utils.Constants;
24  
25  import java.io.BufferedReader;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.InputStreamReader;
29  import java.nio.charset.StandardCharsets;
30  import java.text.ParseException;
31  import java.util.Locale;
32  import java.util.regex.Pattern;
33  
34  /**This reader is adapted to the EGM Format.
35   *
36   * <p> The proper way to use this class is to call the {@link GravityFieldFactory}
37   *  which will determine which reader to use with the selected gravity field file.</p>
38   *
39   * @see GravityFields
40   * @author Fabien Maussion
41   */
42  public class EGMFormatReader extends PotentialCoefficientsReader {
43  
44      /** Pattern for delimiting regular expressions. */
45      private static final Pattern SEPARATOR = Pattern.compile("\\s+");
46  
47      /** Start degree and order for coefficients container. */
48      private static final int START_DEGREE_ORDER = 120;
49  
50      /** Flag for using WGS84 values for equatorial radius and central attraction coefficient. */
51      private final boolean useWgs84Coefficients;
52  
53      /** Simple constructor.
54       * @param supportedNames regular expression for supported files names
55       * @param missingCoefficientsAllowed if true, allows missing coefficients in the input data
56       */
57      public EGMFormatReader(final String supportedNames, final boolean missingCoefficientsAllowed) {
58          this(supportedNames, missingCoefficientsAllowed, false);
59      }
60  
61      /**
62       * Simple constructor that allows overriding 'standard' EGM96 ae and mu with
63       * WGS84 variants.
64       *
65       * @param supportedNames regular expression for supported files names
66       * @param missingCoefficientsAllowed if true, allows missing coefficients in the input data
67       * @param useWgs84Coefficients if true, the WGS84 values will be used for equatorial radius
68       * and central attraction coefficient
69       */
70      public EGMFormatReader(final String supportedNames, final boolean missingCoefficientsAllowed,
71                             final boolean useWgs84Coefficients) {
72          super(supportedNames, missingCoefficientsAllowed, null);
73          this.useWgs84Coefficients = useWgs84Coefficients;
74      }
75  
76  
77      /** {@inheritDoc} */
78      public void loadData(final InputStream input, final String name)
79          throws IOException, ParseException, OrekitException {
80  
81          // reset the indicator before loading any data
82          setReadComplete(false);
83  
84          // both EGM96 and EGM2008 use the same values for ae and mu
85          // if a new EGM model changes them, we should have some selection logic
86          // based on file name (a better way would be to have the data in the
87          // file...)
88          if (this.useWgs84Coefficients) {
89              setAe(Constants.WGS84_EARTH_EQUATORIAL_RADIUS);
90              setMu(Constants.WGS84_EARTH_MU);
91          } else {
92              setAe(Constants.EGM96_EARTH_EQUATORIAL_RADIUS);
93              setMu(Constants.EGM96_EARTH_MU);
94          }
95  
96          final String lowerCaseName = name.toLowerCase(Locale.US);
97          if (lowerCaseName.contains("zerotide")) {
98              setTideSystem(TideSystem.ZERO_TIDE);
99          } else if (lowerCaseName.contains("tidefree")) {
100             setTideSystem(TideSystem.TIDE_FREE);
101         } else if (lowerCaseName.contains("2008")) {
102             setTideSystem(TideSystem.ZERO_TIDE);
103         } else {
104             setTideSystem(TideSystem.TIDE_FREE);
105         }
106 
107         TemporaryCoefficientsContainer container = new TemporaryCoefficientsContainer(START_DEGREE_ORDER, START_DEGREE_ORDER,
108                                                                                       missingCoefficientsAllowed() ? 0.0 : Double.NaN);
109         boolean okFields = true;
110         int       maxDegree  = -1;
111         int       maxOrder   = -1;
112         int lineNumber = 0;
113         String line = null;
114         try (BufferedReader r = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
115             for (line = r.readLine(); okFields && line != null; line = r.readLine()) {
116                 lineNumber++;
117                 if (line.length() >= 15) {
118 
119                     // get the fields defining the current potential terms
120                     final String[] tab = SEPARATOR.split(line.trim());
121                     if (tab.length != 6) {
122                         okFields = false;
123                     }
124 
125                     final int i = Integer.parseInt(tab[0]);
126                     final int j = Integer.parseInt(tab[1]);
127                     if (i < 0 || j < 0) {
128                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
129                                                   lineNumber, name, line);
130                     }
131 
132                     if (i <= getMaxParseDegree() && j <= getMaxParseOrder()) {
133 
134                         while (!container.getFlattener().withinRange(i, j)) {
135                             // we need to resize the container
136                             container = container.resize(container.getFlattener().getDegree() * 2,
137                                                          container.getFlattener().getOrder() * 2);
138                         }
139 
140                         parseCoefficient(tab[2], container.getFlattener(), container.getC(), i, j, "C", name);
141                         parseCoefficient(tab[3], container.getFlattener(), container.getS(), i, j, "S", name);
142                         maxDegree = FastMath.max(maxDegree, i);
143                         maxOrder  = FastMath.max(maxOrder,  j);
144 
145                     }
146 
147                 }
148             }
149         } catch (NumberFormatException nfe) {
150             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
151                                       lineNumber, name, line);
152         }
153 
154         if (missingCoefficientsAllowed() && getMaxParseDegree() > 0 && getMaxParseOrder() > 0) {
155             // ensure at least the (0, 0) element is properly set
156             if (Precision.equals(container.getC()[container.getFlattener().index(0, 0)], 0.0, 0)) {
157                 container.getC()[container.getFlattener().index(0, 0)] = 1.0;
158             }
159         }
160 
161         if (!(okFields && maxDegree >= 0)) {
162             String loaderName = getClass().getName();
163             loaderName = loaderName.substring(loaderName.lastIndexOf('.') + 1);
164             throw new OrekitException(OrekitMessages.UNEXPECTED_FILE_FORMAT_ERROR_FOR_LOADER,
165                                       name, loaderName);
166         }
167 
168         container = container.resize(maxDegree, maxOrder);
169         setRawCoefficients(true, container.getFlattener(), container.getC(), container.getS(), name);
170         setReadComplete(true);
171 
172     }
173 
174     /** Get a provider for read spherical harmonics coefficients.
175      * <p>
176      * EGM fields don't include time-dependent parts, so this method returns
177      * directly a constant provider.
178      * </p>
179      * @param wantNormalized if true, the provider will provide normalized coefficients,
180      * otherwise it will provide un-normalized coefficients
181      * @param degree maximal degree
182      * @param order maximal order
183      * @return a new provider
184      * @since 6.0
185      */
186     public RawSphericalHarmonicsProvider getProvider(final boolean wantNormalized,
187                                                      final int degree, final int order) {
188         return getBaseProvider(wantNormalized, degree, order);
189     }
190 }