SHMFormatReader.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.Locale;

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

  32. /** Reader for the SHM gravity field format.
  33.  *
  34.  * <p> This format was used to describe the gravity field of EIGEN models
  35.  * published by the GFZ Potsdam up to 2003. It was then replaced by
  36.  * {@link ICGEMFormatReader ICGEM format}. The SHM format is described in
  37.  * <a href="http://www.gfz-potsdam.de/grace/results/"> Potsdam university
  38.  * website</a>.
  39.  *
  40.  * <p> The proper way to use this class is to call the {@link GravityFieldFactory}
  41.  *  which will determine which reader to use with the selected gravity field file.</p>
  42.  *
  43.  * @see GravityFieldFactory
  44.  * @author Fabien Maussion
  45.  */
  46. public class SHMFormatReader extends PotentialCoefficientsReader {

  47.     /** First field labels. */
  48.     private static final String GRCOEF = "GRCOEF";

  49.     /** Second field labels. */
  50.     private static final String GRCOF2 = "GRCOF2";

  51.     /** Drift coefficients labels. */
  52.     private static final String GRDOTA = "GRDOTA";

  53.     /** Reference date. */
  54.     private DateComponents referenceDate;

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

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

  59.     /** Simple constructor.
  60.      * @param supportedNames regular expression for supported files names
  61.      * @param missingCoefficientsAllowed if true, allows missing coefficients in the input data
  62.      */
  63.     public SHMFormatReader(final String supportedNames, final boolean missingCoefficientsAllowed) {
  64.         super(supportedNames, missingCoefficientsAllowed);
  65.         referenceDate = null;
  66.         cDot = new ArrayList<List<Double>>();
  67.         sDot = new ArrayList<List<Double>>();
  68.     }

  69.     /** {@inheritDoc} */
  70.     public void loadData(final InputStream input, final String name)
  71.         throws IOException, ParseException, OrekitException {

  72.         // reset the indicator before loading any data
  73.         setReadComplete(false);
  74.         referenceDate = null;
  75.         cDot.clear();
  76.         sDot.clear();

  77.         boolean    normalized = false;
  78.         TideSystem tideSystem = TideSystem.UNKNOWN;

  79.         final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
  80.         boolean okEarth            = false;
  81.         boolean okSHM              = false;
  82.         boolean okCoeffs           = false;
  83.         double[][] c               = null;
  84.         double[][] s               = null;
  85.         String line = r.readLine();
  86.         if ((line != null) &&
  87.             "FIRST ".equals(line.substring(0, 6)) &&
  88.             "SHM    ".equals(line.substring(49, 56))) {
  89.             for (line = r.readLine(); line != null; line = r.readLine()) {
  90.                 if (line.length() >= 6) {
  91.                     final String[] tab = line.split("\\s+");

  92.                     // read the earth values
  93.                     if ("EARTH".equals(tab[0])) {
  94.                         setMu(parseDouble(tab[1]));
  95.                         setAe(parseDouble(tab[2]));
  96.                         okEarth = true;
  97.                     }

  98.                     // initialize the arrays
  99.                     if ("SHM".equals(tab[0])) {

  100.                         final int degree = FastMath.min(getMaxParseDegree(), Integer.parseInt(tab[1]));
  101.                         final int order  = FastMath.min(getMaxParseOrder(), degree);
  102.                         c = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  103.                         s = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
  104.                         final String lowerCaseLine = line.toLowerCase(Locale.US);
  105.                         normalized = lowerCaseLine.contains("fully normalized");
  106.                         if (lowerCaseLine.contains("exclusive permanent tide")) {
  107.                             tideSystem = TideSystem.TIDE_FREE;
  108.                         } else {
  109.                             tideSystem = TideSystem.UNKNOWN;
  110.                         }
  111.                         okSHM = true;
  112.                     }

  113.                     // fill the arrays
  114.                     if (GRCOEF.equals(line.substring(0, 6)) || GRCOF2.equals(tab[0]) || GRDOTA.equals(tab[0])) {
  115.                         final int i = Integer.parseInt(tab[1]);
  116.                         final int j = Integer.parseInt(tab[2]);
  117.                         if (i < c.length && j < c[i].length) {
  118.                             if (GRDOTA.equals(tab[0])) {

  119.                                 // store the secular drift coefficients
  120.                                 extendListOfLists(cDot, i, j, 0.0);
  121.                                 extendListOfLists(sDot, i, j, 0.0);
  122.                                 parseCoefficient(tab[3], cDot, i, j, "Cdot", name);
  123.                                 parseCoefficient(tab[4], sDot, i, j, "Sdot", name);

  124.                                 // check the reference date (format yyyymmdd)
  125.                                 final DateComponents localRef = new DateComponents(Integer.parseInt(tab[7].substring(0, 4)),
  126.                                                                                    Integer.parseInt(tab[7].substring(4, 6)),
  127.                                                                                    Integer.parseInt(tab[7].substring(6, 8)));
  128.                                 if (referenceDate == null) {
  129.                                     // first reference found, store it
  130.                                     referenceDate = localRef;
  131.                                 } else if (!referenceDate.equals(localRef)) {
  132.                                     throw new OrekitException(OrekitMessages.SEVERAL_REFERENCE_DATES_IN_GRAVITY_FIELD,
  133.                                                               referenceDate, localRef, name);
  134.                                 }

  135.                             } else {

  136.                                 // store the constant coefficients
  137.                                 parseCoefficient(tab[3], c, i, j, "C", name);
  138.                                 parseCoefficient(tab[4], s, i, j, "S", name);
  139.                                 okCoeffs = true;

  140.                             }
  141.                         }
  142.                     }

  143.                 }
  144.             }
  145.         }

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

  152.         if (!(okEarth && okSHM && okCoeffs)) {
  153.             String loaderName = getClass().getName();
  154.             loaderName = loaderName.substring(loaderName.lastIndexOf('.') + 1);
  155.             throw new OrekitException(OrekitMessages.UNEXPECTED_FILE_FORMAT_ERROR_FOR_LOADER,
  156.                                       name, loaderName);
  157.         }

  158.         setRawCoefficients(normalized, c, s, name);
  159.         setTideSystem(tideSystem);
  160.         setReadComplete(true);

  161.     }

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

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

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

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

  187.         }

  188.         return provider;

  189.     }

  190. }