GRGSFormatReader.java

  1. /* Copyright 2002-2016 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.text.ParseException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.regex.Matcher;
  26. import java.util.regex.Pattern;

  27. import org.apache.commons.math3.util.FastMath;
  28. import org.apache.commons.math3.util.Precision;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.errors.OrekitParseException;
  32. import org.orekit.time.DateComponents;
  33. import org.orekit.utils.Constants;

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

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

  47.     /** Reference date. */
  48.     private DateComponents referenceDate;

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

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

  53.     static {

  54.         // sub-patterns
  55.         final String real = "[-+]?\\d?\\.\\d+[eEdD][-+]\\d\\d";
  56.         final String sep = ")\\s*(";

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

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

  70.         // compile the regular expressions
  71.         LINES = new Pattern[header.length + 1];
  72.         for (int i = 0; i < header.length; ++i) {
  73.             LINES[i] = Pattern.compile(header[i]);
  74.         }
  75.         LINES[LINES.length - 1] = Pattern.compile(data);

  76.     }

  77.     /** Simple constructor.
  78.      * @param supportedNames regular expression for supported files names
  79.      * @param missingCoefficientsAllowed if true, allows missing coefficients in the input data
  80.      */
  81.     public GRGSFormatReader(final String supportedNames, final boolean missingCoefficientsAllowed) {
  82.         super(supportedNames, missingCoefficientsAllowed);
  83.         referenceDate = null;
  84.         cDot = new ArrayList<List<Double>>();
  85.         sDot = new ArrayList<List<Double>>();
  86.     }

  87.     /** {@inheritDoc} */
  88.     public void loadData(final InputStream input, final String name)
  89.         throws IOException, ParseException, OrekitException {

  90.         // reset the indicator before loading any data
  91.         setReadComplete(false);
  92.         referenceDate = null;
  93.         cDot.clear();
  94.         sDot.clear();

  95.         //        FIELD - GRIM5, VERSION : C1, november 1999
  96.         //        AE                  1/F                 GM                 OMEGA
  97.         //0.63781364600000E+070.29825765000000E+030.39860044150000E+150.72921150000000E-04
  98.         //REFERENCE DATE : 1997.00
  99.         //MAXIMAL DEGREE : 120     Sigmas calibration factor : .5000E+01 (applied)
  100.         //L  M DOT         CBAR                SBAR             SIGMA C      SIGMA S
  101.         // 2  0DOT 0.13637590952454E-10 0.00000000000000E+00  .143968E-11  .000000E+00
  102.         // 3  0DOT 0.28175700027753E-11 0.00000000000000E+00  .496704E-12  .000000E+00
  103.         // 4  0DOT 0.12249148508277E-10 0.00000000000000E+00  .129977E-11  .000000E+00
  104.         // 0  0     .99999999988600E+00  .00000000000000E+00  .153900E-09  .000000E+00
  105.         // 2  0   -0.48416511550920E-03 0.00000000000000E+00  .204904E-10  .000000E+00

  106.         final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
  107.         int lineNumber = 0;
  108.         double[][] c   = null;
  109.         double[][] s   = null;
  110.         for (String line = r.readLine(); line != null; line = r.readLine()) {

  111.             ++lineNumber;

  112.             // match current header or data line
  113.             final Matcher matcher = LINES[FastMath.min(LINES.length, lineNumber) - 1].matcher(line);
  114.             if (!matcher.matches()) {
  115.                 throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  116.                                                lineNumber, name, line);
  117.             }

  118.             if (lineNumber == 3) {
  119.                 // header line defining ae, 1/f, GM and Omega
  120.                 setAe(parseDouble(matcher.group(1)));
  121.                 setMu(parseDouble(matcher.group(3)));
  122.             } else if (lineNumber == 4) {
  123.                 // header line containing the reference date
  124.                 referenceDate  = new DateComponents(Integer.parseInt(matcher.group(1)), 1, 1);
  125.             } else if (lineNumber == 5) {
  126.                 // header line defining max degree
  127.                 final int degree = FastMath.min(getMaxParseDegree(), Integer.parseInt(matcher.group(1)));
  128.                 final int order  = FastMath.min(getMaxParseOrder(), degree);
  129.                 c = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  130.                 s = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  131.             } else if (lineNumber > 6) {
  132.                 // data line
  133.                 final int i = Integer.parseInt(matcher.group(1).trim());
  134.                 final int j = Integer.parseInt(matcher.group(2).trim());
  135.                 if (i < c.length && j < c[i].length) {
  136.                     if ("DOT".equals(matcher.group(3).trim())) {

  137.                         // store the secular drift coefficients
  138.                         extendListOfLists(cDot, i, j, 0.0);
  139.                         extendListOfLists(sDot, i, j, 0.0);
  140.                         parseCoefficient(matcher.group(4), cDot, i, j, "Cdot", name);
  141.                         parseCoefficient(matcher.group(5), sDot, i, j, "Sdot", name);

  142.                     } else {

  143.                         // store the constant coefficients
  144.                         parseCoefficient(matcher.group(4), c, i, j, "C", name);
  145.                         parseCoefficient(matcher.group(5), s, i, j, "S", name);

  146.                     }
  147.                 }
  148.             }

  149.         }

  150.         if (missingCoefficientsAllowed() && c.length > 0 && c[0].length > 0) {
  151.             // ensure at least the (0, 0) element is properly set
  152.             if (Precision.equals(c[0][0], 0.0, 1)) {
  153.                 c[0][0] = 1.0;
  154.             }
  155.         }

  156.         setRawCoefficients(true, c, s, name);
  157.         setTideSystem(TideSystem.UNKNOWN);
  158.         setReadComplete(true);

  159.     }

  160.     /** Get a provider for read spherical harmonics coefficients.
  161.      * <p>
  162.      * GRGS fields may include time-dependent parts which are taken into account
  163.      * in the returned provider.
  164.      * </p>
  165.      * @param wantNormalized if true, the provider will provide normalized coefficients,
  166.      * otherwise it will provide un-normalized coefficients
  167.      * @param degree maximal degree
  168.      * @param order maximal order
  169.      * @return a new provider
  170.      * @exception OrekitException if the requested maximal degree or order exceeds the
  171.      * available degree or order or if no gravity field has read yet
  172.      * @since 6.0
  173.      */
  174.     public RawSphericalHarmonicsProvider getProvider(final boolean wantNormalized,
  175.                                                      final int degree, final int order)
  176.         throws OrekitException {

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

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

  180.             // add the secular trend layer
  181.             final double[][] cArray = toArray(cDot);
  182.             final double[][] sArray = toArray(sDot);
  183.             rescale(1.0 / Constants.JULIAN_YEAR, true, cArray, sArray, wantNormalized, cArray, sArray);
  184.             provider = new SecularTrendSphericalHarmonics(provider, referenceDate, cArray, sArray);

  185.         }

  186.         return provider;

  187.     }

  188. }