GRGSFormatReader.java

  1. /* Copyright 2002-2020 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 java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.nio.charset.StandardCharsets;
  23. import java.text.ParseException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;

  28. import org.hipparchus.util.FastMath;
  29. import org.hipparchus.util.Precision;
  30. import org.orekit.annotation.DefaultDataContext;
  31. import org.orekit.data.DataContext;
  32. import org.orekit.errors.OrekitException;
  33. import org.orekit.errors.OrekitMessages;
  34. import org.orekit.errors.OrekitParseException;
  35. import org.orekit.time.AbsoluteDate;
  36. import org.orekit.time.DateComponents;
  37. import org.orekit.time.TimeScale;
  38. import org.orekit.utils.Constants;

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

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

  52.     /** Reference date. */
  53.     private AbsoluteDate referenceDate;

  54.     /** Secular drift of the cosine coefficients. */
  55.     private final List<List<Double>> cDot;

  56.     /** Secular drift of the sine coefficients. */
  57.     private final List<List<Double>> sDot;

  58.     static {

  59.         // sub-patterns
  60.         final String real = "[-+]?\\d?\\.\\d+[eEdD][-+]\\d\\d";
  61.         final String sep = ")\\s*(";

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

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

  75.         // compile the regular expressions
  76.         LINES = new Pattern[header.length + 1];
  77.         for (int i = 0; i < header.length; ++i) {
  78.             LINES[i] = Pattern.compile(header[i]);
  79.         }
  80.         LINES[LINES.length - 1] = Pattern.compile(data);

  81.     }

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

  95.     /**
  96.      * Simple constructor.
  97.      *
  98.      * @param supportedNames             regular expression for supported files names
  99.      * @param missingCoefficientsAllowed if true, allows missing coefficients in the input
  100.      *                                   data
  101.      * @param timeScale                  to use when parsing dates.
  102.      * @since 10.1
  103.      */
  104.     public GRGSFormatReader(final String supportedNames,
  105.                             final boolean missingCoefficientsAllowed,
  106.                             final TimeScale timeScale) {
  107.         super(supportedNames, missingCoefficientsAllowed, timeScale);
  108.         referenceDate = null;
  109.         cDot = new ArrayList<>();
  110.         sDot = new ArrayList<>();
  111.     }

  112.     /** {@inheritDoc} */
  113.     public void loadData(final InputStream input, final String name)
  114.         throws IOException, ParseException, OrekitException {

  115.         // reset the indicator before loading any data
  116.         setReadComplete(false);
  117.         referenceDate = null;
  118.         cDot.clear();
  119.         sDot.clear();

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

  131.         int lineNumber = 0;
  132.         double[][] c   = null;
  133.         double[][] s   = null;
  134.         try (BufferedReader r = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
  135.             for (String line = r.readLine(); line != null; line = r.readLine()) {

  136.                 ++lineNumber;

  137.                 // match current header or data line
  138.                 final Matcher matcher = LINES[FastMath.min(LINES.length, lineNumber) - 1].matcher(line);
  139.                 if (!matcher.matches()) {
  140.                     throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  141.                                                    lineNumber, name, line);
  142.                 }

  143.                 if (lineNumber == 3) {
  144.                     // header line defining ae, 1/f, GM and Omega
  145.                     setAe(parseDouble(matcher.group(1)));
  146.                     setMu(parseDouble(matcher.group(3)));
  147.                 } else if (lineNumber == 4) {
  148.                     // header line containing the reference date
  149.                     referenceDate  = toDate(
  150.                             new DateComponents(Integer.parseInt(matcher.group(1)), 1, 1));
  151.                 } else if (lineNumber == 5) {
  152.                     // header line defining max degree
  153.                     final int degree = FastMath.min(getMaxParseDegree(), Integer.parseInt(matcher.group(1)));
  154.                     final int order  = FastMath.min(getMaxParseOrder(), degree);
  155.                     c = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  156.                     s = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  157.                 } else if (lineNumber > 6) {
  158.                     // data line
  159.                     final int i = Integer.parseInt(matcher.group(1).trim());
  160.                     final int j = Integer.parseInt(matcher.group(2).trim());
  161.                     if (i < c.length && j < c[i].length) {
  162.                         if ("DOT".equals(matcher.group(3).trim())) {

  163.                             // store the secular drift coefficients
  164.                             extendListOfLists(cDot, i, j, 0.0);
  165.                             extendListOfLists(sDot, i, j, 0.0);
  166.                             parseCoefficient(matcher.group(4), cDot, i, j, "Cdot", name);
  167.                             parseCoefficient(matcher.group(5), sDot, i, j, "Sdot", name);

  168.                         } else {

  169.                             // store the constant coefficients
  170.                             parseCoefficient(matcher.group(4), c, i, j, "C", name);
  171.                             parseCoefficient(matcher.group(5), s, i, j, "S", name);

  172.                         }
  173.                     }
  174.                 }

  175.             }
  176.         }

  177.         if (missingCoefficientsAllowed() && c.length > 0 && c[0].length > 0) {
  178.             // ensure at least the (0, 0) element is properly set
  179.             if (Precision.equals(c[0][0], 0.0, 0)) {
  180.                 c[0][0] = 1.0;
  181.             }
  182.         }

  183.         setRawCoefficients(true, c, s, name);
  184.         setTideSystem(TideSystem.UNKNOWN);
  185.         setReadComplete(true);

  186.     }

  187.     /** Get a provider for read spherical harmonics coefficients.
  188.      * <p>
  189.      * GRGS fields may include time-dependent parts which are taken into account
  190.      * in the returned provider.
  191.      * </p>
  192.      * @param wantNormalized if true, the provider will provide normalized coefficients,
  193.      * otherwise it will provide un-normalized coefficients
  194.      * @param degree maximal degree
  195.      * @param order maximal order
  196.      * @return a new provider
  197.      * @since 6.0
  198.      */
  199.     public RawSphericalHarmonicsProvider getProvider(final boolean wantNormalized,
  200.                                                      final int degree, final int order) {

  201.         // get the constant part
  202.         RawSphericalHarmonicsProvider provider = getConstantProvider(wantNormalized, degree, order);

  203.         if (!cDot.isEmpty()) {

  204.             // add the secular trend layer
  205.             final double[][] cArray = toArray(cDot);
  206.             final double[][] sArray = toArray(sDot);
  207.             rescale(1.0 / Constants.JULIAN_YEAR, true, cArray, sArray, wantNormalized, cArray, sArray);
  208.             provider = new SecularTrendSphericalHarmonics(provider, referenceDate, cArray, sArray);

  209.         }

  210.         return provider;

  211.     }

  212. }