GRGSFormatReader.java

  1. /* Copyright 2002-2015 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.time.DateComponents;
  32. import org.orekit.utils.Constants;

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

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

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

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

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

  52.     static {

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

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

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

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

  75.     }

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

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

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

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

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

  110.             ++lineNumber;

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

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

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

  141.                     } else {

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

  145.                     }
  146.                 }
  147.             }

  148.         }

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

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

  158.     }

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

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

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

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

  184.         }

  185.         return provider;

  186.     }

  187. }