OMMParser.java

  1. /* Copyright 2002-2018 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.files.ccsds;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.hipparchus.exception.DummyLocalizable;
  25. import org.hipparchus.util.FastMath;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.utils.IERSConventions;

  30. /** A parser for the CCSDS OMM (Orbiter Mean-Elements Message).
  31.  * @author sports
  32.  * @since 6.1
  33.  */
  34. public class OMMParser extends ODMParser {

  35.     /** Simple constructor.
  36.      * <p>
  37.      * This class is immutable, and hence thread safe. When parts
  38.      * must be changed, such as reference date for Mission Elapsed Time or
  39.      * Mission Relative Time time systems, or the gravitational coefficient or
  40.      * the IERS conventions, the various {@code withXxx} methods must be called,
  41.      * which create a new immutable instance with the new parameters. This
  42.      * is a combination of the
  43.      * <a href="https://en.wikipedia.org/wiki/Builder_pattern">builder design
  44.      * pattern</a> and a
  45.      * <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
  46.      * interface</a>.
  47.      * </p>
  48.      * <p>
  49.      * The initial date for Mission Elapsed Time and Mission Relative Time time systems is not set here.
  50.      * If such time systems are used, it must be initialized before parsing by calling {@link
  51.      * #withMissionReferenceDate(AbsoluteDate)}.
  52.      * </p>
  53.      * <p>
  54.      * The gravitational coefficient is not set here. If it is needed in order
  55.      * to parse Cartesian orbits where the value is not set in the CCSDS file, it must
  56.      * be initialized before parsing by calling {@link #withMu(double)}.
  57.      * </p>
  58.      * <p>
  59.      * The IERS conventions to use is not set here. If it is needed in order to
  60.      * parse some reference frames or UT1 time scale, it must be initialized before
  61.      * parsing by calling {@link #withConventions(IERSConventions)}.
  62.      * </p>
  63.      * <p>
  64.      * The international designator parameters (launch year, launch number and
  65.      * launch piece) are not set here. If they are needed, they must be initialized before
  66.      * parsing by calling {@link #withInternationalDesignator(int, int, String)}
  67.      * </p>
  68.      */
  69.     public OMMParser() {
  70.         this(AbsoluteDate.FUTURE_INFINITY, Double.NaN, null, true, 0, 0, "");
  71.     }

  72.     /** Complete constructor.
  73.      * @param missionReferenceDate reference date for Mission Elapsed Time or Mission Relative Time time systems
  74.      * @param mu gravitational coefficient
  75.      * @param conventions IERS Conventions
  76.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  77.      * @param launchYear launch year for TLEs
  78.      * @param launchNumber launch number for TLEs
  79.      * @param launchPiece piece of launch (from "A" to "ZZZ") for TLEs
  80.      */
  81.     private OMMParser(final AbsoluteDate missionReferenceDate, final double mu,
  82.                       final IERSConventions conventions, final boolean simpleEOP,
  83.                       final int launchYear, final int launchNumber, final String launchPiece) {
  84.         super(missionReferenceDate, mu, conventions, simpleEOP, launchYear, launchNumber, launchPiece);
  85.     }

  86.     /** {@inheritDoc} */
  87.     public OMMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  88.         return new OMMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  89.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  90.     }

  91.     /** {@inheritDoc} */
  92.     public OMMParser withMu(final double newMu) {
  93.         return new OMMParser(getMissionReferenceDate(), newMu, getConventions(), isSimpleEOP(),
  94.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  95.     }

  96.     /** {@inheritDoc} */
  97.     public OMMParser withConventions(final IERSConventions newConventions) {
  98.         return new OMMParser(getMissionReferenceDate(), getMu(), newConventions, isSimpleEOP(),
  99.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  100.     }

  101.     /** {@inheritDoc} */
  102.     public OMMParser withSimpleEOP(final boolean newSimpleEOP) {
  103.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  104.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  105.     }

  106.     /** {@inheritDoc} */
  107.     public OMMParser withInternationalDesignator(final int newLaunchYear,
  108.                                                  final int newLaunchNumber,
  109.                                                  final String newLaunchPiece) {
  110.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  111.                              newLaunchYear, newLaunchNumber, newLaunchPiece);
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public OMMFile parse(final String fileName) throws OrekitException {
  116.         return (OMMFile) super.parse(fileName);
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public OMMFile parse(final InputStream stream) throws OrekitException {
  121.         return (OMMFile) super.parse(stream);
  122.     }

  123.     /** {@inheritDoc} */
  124.     public OMMFile parse(final InputStream stream, final String fileName) throws OrekitException {

  125.         try {

  126.             final BufferedReader reader =
  127.                     new BufferedReader(new InputStreamReader(stream, "UTF-8"));

  128.             // initialize internal data structures
  129.             final ParseInfo pi = new ParseInfo();
  130.             pi.fileName = fileName;
  131.             final OMMFile file = pi.file;

  132.             // set the additional data that has been configured prior the parsing by the user.
  133.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  134.             pi.file.setMuSet(getMu());
  135.             pi.file.setConventions(getConventions());
  136.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  137.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  138.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  139.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  140.                 ++pi.lineNumber;
  141.                 if (line.trim().length() == 0) {
  142.                     continue;
  143.                 }
  144.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  145.                 if (pi.keyValue.getKeyword() == null) {
  146.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  147.                 }
  148.                 switch (pi.keyValue.getKeyword()) {
  149.                     case CCSDS_OMM_VERS:
  150.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  151.                         break;

  152.                     case MEAN_ELEMENT_THEORY:
  153.                         file.getMetaData().setMeanElementTheory(pi.keyValue.getValue());
  154.                         break;

  155.                     case MEAN_MOTION:
  156.                         file.setMeanMotion(pi.keyValue.getDoubleValue() * FastMath.PI / 43200.0);
  157.                         break;

  158.                     case EPHEMERIS_TYPE:
  159.                         file.setTLERelatedParametersComment(pi.commentTmp);
  160.                         pi.commentTmp.clear();
  161.                         file.setEphemerisType(Integer.parseInt(pi.keyValue.getValue()));
  162.                         break;

  163.                     case CLASSIFICATION_TYPE:
  164.                         file.setClassificationType(pi.keyValue.getValue().charAt(0));
  165.                         break;

  166.                     case NORAD_CAT_ID:
  167.                         file.setNoradID(Integer.parseInt(pi.keyValue.getValue()));
  168.                         break;

  169.                     case ELEMENT_SET_NO:
  170.                         file.setElementSetNo(pi.keyValue.getValue());
  171.                         break;

  172.                     case REV_AT_EPOCH:
  173.                         file.setRevAtEpoch(Integer.parseInt(pi.keyValue.getValue()));
  174.                         break;

  175.                     case BSTAR:
  176.                         file.setbStar(pi.keyValue.getDoubleValue());
  177.                         break;

  178.                     case MEAN_MOTION_DOT:
  179.                         file.setMeanMotionDot(pi.keyValue.getDoubleValue() * FastMath.PI / 1.86624e9);
  180.                         break;

  181.                     case MEAN_MOTION_DDOT:
  182.                         file.setMeanMotionDotDot(pi.keyValue.getDoubleValue() *
  183.                                                  FastMath.PI / 5.3747712e13);
  184.                         break;

  185.                     default:
  186.                         boolean parsed = false;
  187.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  188.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  189.                         parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  190.                         parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  191.                         if (!parsed) {
  192.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  193.                         }
  194.                 }
  195.             }
  196.             reader.close();
  197.             return file;
  198.         } catch (IOException ioe) {
  199.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  200.         }
  201.     }

  202.     /** Private class used to stock OMM parsing info.
  203.      * @author sports
  204.      */
  205.     private static class ParseInfo {

  206.         /** OMM file being read. */
  207.         private OMMFile file;

  208.         /** Name of the file. */
  209.         private String fileName;

  210.         /** Current line number. */
  211.         private int lineNumber;

  212.         /** Key value of the line being read. */
  213.         private KeyValue keyValue;

  214.         /** Stored comments. */
  215.         private List<String> commentTmp;

  216.         /** Create a new {@link ParseInfo} object. */
  217.         protected ParseInfo() {
  218.             lineNumber = 0;
  219.             file = new OMMFile();
  220.             commentTmp = new ArrayList<String>();
  221.         }
  222.     }
  223. }