GRGSFormatReader.java

  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. import org.hipparchus.util.FastMath;
  19. import org.hipparchus.util.Precision;
  20. import org.orekit.annotation.DefaultDataContext;
  21. import org.orekit.data.DataContext;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.errors.OrekitParseException;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.time.DateComponents;
  27. import org.orekit.time.TimeScale;
  28. import org.orekit.utils.Constants;

  29. import java.io.BufferedReader;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.io.InputStreamReader;
  33. import java.nio.charset.StandardCharsets;
  34. import java.text.ParseException;
  35. import java.util.regex.Matcher;
  36. import java.util.regex.Pattern;

  37. /** Reader for the GRGS gravity field format.
  38.  *
  39.  * <p> This format was used to describe various gravity fields at GRGS (Toulouse).
  40.  *
  41.  * <p> The proper way to use this class is to call the {@link GravityFieldFactory}
  42.  *  which will determine which reader to use with the selected gravity field file.</p>
  43.  *
  44.  * @see GravityFields
  45.  * @author Luc Maisonobe
  46.  */
  47. public class GRGSFormatReader extends PotentialCoefficientsReader {

  48.     /** Patterns for lines (the last pattern is repeated for all data lines). */
  49.     private static final Pattern[] LINES;

  50.     /** Flag for Earth data. */
  51.     private static final int EARTH = 0x1;

  52.     /** Flag for degree/order. */
  53.     private static final int LIMITS = 0x2;

  54.     /** Flag for coefficients. */
  55.     private static final int COEFFS = 0x4;

  56.     /** Reference date. */
  57.     private AbsoluteDate referenceDate;

  58.     /** Converter from triangular to flat form. */
  59.     private Flattener dotFlattener;

  60.     /** Secular drift of the cosine coefficients. */
  61.     private double[] cDot;

  62.     /** Secular drift of the sine coefficients. */
  63.     private double[] sDot;

  64.     static {

  65.         // sub-patterns
  66.         final String real = "[-+]?\\d?\\.\\d+[eEdD][-+]\\d\\d";
  67.         final String sep = ")\\s*(";

  68.         // regular expression for header lines
  69.         final String[] header = {
  70.             "^\\s*FIELD - .*$",
  71.             "^\\s+AE\\s+1/F\\s+GM\\s+OMEGA\\s*$",
  72.             "^\\s*(" + real + sep + real + sep + real + sep + real + ")\\s*$",
  73.             "^\\s*REFERENCE\\s+DATE\\s+:\\s+(\\d+)\\.0+\\s*$",
  74.             "^\\s*MAXIMAL\\s+DEGREE\\s+:\\s+(\\d+)\\s.*$",
  75.             "^\\s*L\\s+M\\s+DOT\\s+CBAR\\s+SBAR\\s+SIGMA C\\s+SIGMA S(\\s+LIB)?\\s*$"
  76.         };

  77.         // regular expression for data lines
  78.         final String data = "^([ 0-9]{3})([ 0-9]{3})( {3}|DOT)\\s*(" +
  79.                             real + sep + real + sep + real + sep + real +
  80.                             ")(\\s+[0-9]+)?\\s*$";

  81.         // compile the regular expressions
  82.         LINES = new Pattern[header.length + 1];
  83.         for (int i = 0; i < header.length; ++i) {
  84.             LINES[i] = Pattern.compile(header[i]);
  85.         }
  86.         LINES[LINES.length - 1] = Pattern.compile(data);

  87.     }

  88.     /** Simple constructor.
  89.      *
  90.      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  91.      *
  92.      * @param supportedNames regular expression for supported files names
  93.      * @param missingCoefficientsAllowed if true, allows missing coefficients in the input data
  94.      * @see #GRGSFormatReader(String, boolean, TimeScale)
  95.      */
  96.     @DefaultDataContext
  97.     public GRGSFormatReader(final String supportedNames, final boolean missingCoefficientsAllowed) {
  98.         this(supportedNames, missingCoefficientsAllowed,
  99.                 DataContext.getDefault().getTimeScales().getTT());
  100.     }

  101.     /**
  102.      * Simple constructor.
  103.      *
  104.      * @param supportedNames             regular expression for supported files names
  105.      * @param missingCoefficientsAllowed if true, allows missing coefficients in the input
  106.      *                                   data
  107.      * @param timeScale                  to use when parsing dates.
  108.      * @since 10.1
  109.      */
  110.     public GRGSFormatReader(final String supportedNames,
  111.                             final boolean missingCoefficientsAllowed,
  112.                             final TimeScale timeScale) {
  113.         super(supportedNames, missingCoefficientsAllowed, timeScale);
  114.         reset();
  115.     }

  116.     /** {@inheritDoc} */
  117.     public void loadData(final InputStream input, final String name)
  118.         throws IOException, ParseException, OrekitException {

  119.         // reset the indicator before loading any data
  120.         reset();

  121.         //        FIELD - GRIM5, VERSION : C1, november 1999
  122.         //        AE                  1/F                 GM                 OMEGA
  123.         //0.63781364600000E+070.29825765000000E+030.39860044150000E+150.72921150000000E-04
  124.         //REFERENCE DATE : 1997.00
  125.         //MAXIMAL DEGREE : 120     Sigmas calibration factor : .5000E+01 (applied)
  126.         //L  M DOT         CBAR                SBAR             SIGMA C      SIGMA S
  127.         // 2  0DOT 0.13637590952454E-10 0.00000000000000E+00  .143968E-11  .000000E+00
  128.         // 3  0DOT 0.28175700027753E-11 0.00000000000000E+00  .496704E-12  .000000E+00
  129.         // 4  0DOT 0.12249148508277E-10 0.00000000000000E+00  .129977E-11  .000000E+00
  130.         // 0  0     .99999999988600E+00  .00000000000000E+00  .153900E-09  .000000E+00
  131.         // 2  0   -0.48416511550920E-03 0.00000000000000E+00  .204904E-10  .000000E+00

  132.         Flattener flattener  = null;
  133.         int       dotDegree  = -1;
  134.         int       dotOrder   = -1;
  135.         int       flags      = 0;
  136.         int       lineNumber = 0;
  137.         double[]  c0         = null;
  138.         double[]  s0         = null;
  139.         double[]  c1         = null;
  140.         double[]  s1         = null;
  141.         try (BufferedReader r = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
  142.             for (String line = r.readLine(); line != null; line = r.readLine()) {

  143.                 ++lineNumber;

  144.                 // match current header or data line
  145.                 final Matcher matcher = LINES[FastMath.min(LINES.length, lineNumber) - 1].matcher(line);
  146.                 if (!matcher.matches()) {
  147.                     throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  148.                                                    lineNumber, name, line);
  149.                 }

  150.                 if (lineNumber == 3) {
  151.                     // header line defining ae, 1/f, GM and Omega
  152.                     setAe(parseDouble(matcher.group(1)));
  153.                     setMu(parseDouble(matcher.group(3)));
  154.                     flags |= EARTH;
  155.                 } else if (lineNumber == 4) {
  156.                     // header line containing the reference date
  157.                     referenceDate  = toDate(
  158.                             new DateComponents(Integer.parseInt(matcher.group(1)), 1, 1));
  159.                 } else if (lineNumber == 5) {
  160.                     // header line defining max degree
  161.                     final int degree = FastMath.min(getMaxParseDegree(), Integer.parseInt(matcher.group(1)));
  162.                     final int order  = FastMath.min(getMaxParseOrder(), degree);
  163.                     flattener = new Flattener(degree, order);
  164.                     c0 = buildFlatArray(flattener, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  165.                     s0 = buildFlatArray(flattener, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  166.                     c1 = buildFlatArray(flattener, 0.0);
  167.                     s1 = buildFlatArray(flattener, 0.0);
  168.                     flags |= LIMITS;
  169.                 } else if (lineNumber > 6) {
  170.                     // data line
  171.                     final int i = Integer.parseInt(matcher.group(1).trim());
  172.                     final int j = Integer.parseInt(matcher.group(2).trim());
  173.                     if (flattener.withinRange(i, j)) {
  174.                         if ("DOT".equals(matcher.group(3).trim())) {

  175.                             // store the secular drift coefficients
  176.                             parseCoefficient(matcher.group(4), flattener, c1, i, j, "Cdot", name);
  177.                             parseCoefficient(matcher.group(5), flattener, s1, i, j, "Sdot", name);
  178.                             dotDegree = FastMath.max(dotDegree, i);
  179.                             dotOrder  = FastMath.max(dotOrder,  j);

  180.                         } else {

  181.                             // store the constant coefficients
  182.                             parseCoefficient(matcher.group(4), flattener, c0, i, j, "C", name);
  183.                             parseCoefficient(matcher.group(5), flattener, s0, i, j, "S", name);

  184.                         }
  185.                     }
  186.                     flags |= COEFFS;
  187.                 }

  188.             }
  189.         }

  190.         if (flags != (EARTH | LIMITS | COEFFS)) {
  191.             String loaderName = getClass().getName();
  192.             loaderName = loaderName.substring(loaderName.lastIndexOf('.') + 1);
  193.             throw new OrekitException(OrekitMessages.UNEXPECTED_FILE_FORMAT_ERROR_FOR_LOADER,
  194.                                       name, loaderName);
  195.         }

  196.         if (missingCoefficientsAllowed()) {
  197.             // ensure at least the (0, 0) element is properly set
  198.             if (Precision.equals(c0[flattener.index(0, 0)], 0.0, 0)) {
  199.                 c0[flattener.index(0, 0)] = 1.0;
  200.             }
  201.         }

  202.         // resize secular drift arrays
  203.         if (dotDegree >= 0) {
  204.             dotFlattener = new Flattener(dotDegree, dotOrder);
  205.             cDot         = new double[dotFlattener.arraySize()];
  206.             sDot         = new double[dotFlattener.arraySize()];
  207.             for (int n = 0; n <= dotDegree; ++n) {
  208.                 for (int m = 0; m <= FastMath.min(n, dotOrder); ++m) {
  209.                     cDot[dotFlattener.index(n, m)] = c1[flattener.index(n, m)];
  210.                     sDot[dotFlattener.index(n, m)] = s1[flattener.index(n, m)];
  211.                 }
  212.             }
  213.         }

  214.         setRawCoefficients(true, flattener, c0, s0, name);
  215.         setTideSystem(TideSystem.UNKNOWN);
  216.         setReadComplete(true);

  217.     }

  218.     /** Reset instance before read.
  219.      * @since 11.1
  220.      */
  221.     private void reset() {
  222.         setReadComplete(false);
  223.         referenceDate = null;
  224.         dotFlattener  = null;
  225.         cDot          = null;
  226.         sDot          = null;
  227.     }

  228.     /** {@inheritDoc}
  229.      * <p>
  230.      * GRGS fields may include time-dependent parts which are taken into account
  231.      * in the returned provider.
  232.      * </p>
  233.      */
  234.     public RawSphericalHarmonicsProvider getProvider(final boolean wantNormalized,
  235.                                                      final int degree, final int order) {

  236.         // get the constant part
  237.         RawSphericalHarmonicsProvider provider = getBaseProvider(wantNormalized, degree, order);

  238.         if (dotFlattener != null) {

  239.             // add the secular trend layer
  240.             final double scale = 1.0 / Constants.JULIAN_YEAR;
  241.             final Flattener rescaledFlattener = new Flattener(FastMath.min(degree, dotFlattener.getDegree()),
  242.                                                               FastMath.min(order, dotFlattener.getOrder()));
  243.             provider = new SecularTrendSphericalHarmonics(provider, referenceDate, rescaledFlattener,
  244.                                                           rescale(scale, wantNormalized, rescaledFlattener, dotFlattener, cDot),
  245.                                                           rescale(scale, wantNormalized, rescaledFlattener, dotFlattener, sDot));

  246.         }

  247.         return provider;

  248.     }

  249. }