OMMParser.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.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.nio.charset.StandardCharsets;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import org.hipparchus.exception.DummyLocalizable;
  26. import org.hipparchus.util.FastMath;
  27. import org.orekit.annotation.DefaultDataContext;
  28. import org.orekit.data.DataContext;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.IERSConventions;

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

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

  79.     /** Constructor with data context.
  80.      * <p>
  81.      * This class is immutable, and hence thread safe. When parts
  82.      * must be changed, such as reference date for Mission Elapsed Time or
  83.      * Mission Relative Time time systems, or the gravitational coefficient or
  84.      * the IERS conventions, the various {@code withXxx} methods must be called,
  85.      * which create a new immutable instance with the new parameters. This
  86.      * is a combination of the
  87.      * <a href="https://en.wikipedia.org/wiki/Builder_pattern">builder design
  88.      * pattern</a> and a
  89.      * <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
  90.      * interface</a>.
  91.      * </p>
  92.      * <p>
  93.      * The initial date for Mission Elapsed Time and Mission Relative Time time systems is not set here.
  94.      * If such time systems are used, it must be initialized before parsing by calling {@link
  95.      * #withMissionReferenceDate(AbsoluteDate)}.
  96.      * </p>
  97.      * <p>
  98.      * The gravitational coefficient is not set here. If it is needed in order
  99.      * to parse Cartesian orbits where the value is not set in the CCSDS file, it must
  100.      * be initialized before parsing by calling {@link #withMu(double)}.
  101.      * </p>
  102.      * <p>
  103.      * The IERS conventions to use is not set here. If it is needed in order to
  104.      * parse some reference frames or UT1 time scale, it must be initialized before
  105.      * parsing by calling {@link #withConventions(IERSConventions)}.
  106.      * </p>
  107.      * <p>
  108.      * The international designator parameters (launch year, launch number and
  109.      * launch piece) are not set here. If they are needed, they must be initialized before
  110.      * parsing by calling {@link #withInternationalDesignator(int, int, String)}
  111.      * </p>
  112.      *
  113.      * @param dataContext used by the parser.
  114.      *
  115.      * @see #OMMParser()
  116.      * @see #withDataContext(DataContext)
  117.      * @since 10.1
  118.      */
  119.     public OMMParser(final DataContext dataContext) {
  120.         this(AbsoluteDate.FUTURE_INFINITY, Double.NaN, null, true, 0, 0, "", dataContext);
  121.     }

  122.     /** Complete constructor.
  123.      * @param missionReferenceDate reference date for Mission Elapsed Time or Mission Relative Time time systems
  124.      * @param mu gravitational coefficient
  125.      * @param conventions IERS Conventions
  126.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  127.      * @param launchYear launch year for TLEs
  128.      * @param launchNumber launch number for TLEs
  129.      * @param launchPiece piece of launch (from "A" to "ZZZ") for TLEs
  130.      * @param dataContext used to retrieve frames, time scales, etc.
  131.      */
  132.     private OMMParser(final AbsoluteDate missionReferenceDate, final double mu,
  133.                       final IERSConventions conventions, final boolean simpleEOP,
  134.                       final int launchYear, final int launchNumber,
  135.                       final String launchPiece, final DataContext dataContext) {
  136.         super(missionReferenceDate, mu, conventions, simpleEOP, launchYear, launchNumber,
  137.                 launchPiece, dataContext);
  138.     }

  139.     /** {@inheritDoc} */
  140.     public OMMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  141.         return new OMMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  142.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  143.     }

  144.     /** {@inheritDoc} */
  145.     public OMMParser withMu(final double newMu) {
  146.         return new OMMParser(getMissionReferenceDate(), newMu, getConventions(), isSimpleEOP(),
  147.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  148.     }

  149.     /** {@inheritDoc} */
  150.     public OMMParser withConventions(final IERSConventions newConventions) {
  151.         return new OMMParser(getMissionReferenceDate(), getMu(), newConventions, isSimpleEOP(),
  152.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  153.     }

  154.     /** {@inheritDoc} */
  155.     public OMMParser withSimpleEOP(final boolean newSimpleEOP) {
  156.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  157.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  158.     }

  159.     /** {@inheritDoc} */
  160.     public OMMParser withInternationalDesignator(final int newLaunchYear,
  161.                                                  final int newLaunchNumber,
  162.                                                  final String newLaunchPiece) {
  163.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  164.                              newLaunchYear, newLaunchNumber, newLaunchPiece, getDataContext());
  165.     }

  166.     @Override
  167.     public OMMParser withDataContext(final DataContext newDataContext) {
  168.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  169.                 getLaunchYear(), getLaunchNumber(), getLaunchPiece(), newDataContext);
  170.     }

  171.     /** {@inheritDoc} */
  172.     @Override
  173.     public OMMFile parse(final String fileName) {
  174.         return (OMMFile) super.parse(fileName);
  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public OMMFile parse(final InputStream stream) {
  179.         return (OMMFile) super.parse(stream);
  180.     }

  181.     /** {@inheritDoc} */
  182.     public OMMFile parse(final InputStream stream, final String fileName) {

  183.         try {

  184.             final BufferedReader reader =
  185.                     new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));

  186.             // initialize internal data structures
  187.             final ParseInfo pi = new ParseInfo();
  188.             pi.fileName = fileName;
  189.             final OMMFile file = pi.file;

  190.             // set the additional data that has been configured prior the parsing by the user.
  191.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  192.             pi.file.setMuSet(getMu());
  193.             pi.file.setConventions(getConventions());
  194.             pi.file.setDataContext(getDataContext());
  195.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  196.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  197.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  198.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  199.                 ++pi.lineNumber;
  200.                 if (line.trim().length() == 0) {
  201.                     continue;
  202.                 }
  203.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  204.                 if (pi.keyValue.getKeyword() == null) {
  205.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  206.                 }
  207.                 switch (pi.keyValue.getKeyword()) {
  208.                     case CCSDS_OMM_VERS:
  209.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  210.                         break;

  211.                     case MEAN_ELEMENT_THEORY:
  212.                         file.getMetaData().setMeanElementTheory(pi.keyValue.getValue());
  213.                         break;

  214.                     case MEAN_MOTION:
  215.                         file.setMeanMotion(pi.keyValue.getDoubleValue() * FastMath.PI / 43200.0);
  216.                         break;

  217.                     case EPHEMERIS_TYPE:
  218.                         file.setTLERelatedParametersComment(pi.commentTmp);
  219.                         pi.commentTmp.clear();
  220.                         file.setEphemerisType(Integer.parseInt(pi.keyValue.getValue()));
  221.                         break;

  222.                     case CLASSIFICATION_TYPE:
  223.                         file.setClassificationType(pi.keyValue.getValue().charAt(0));
  224.                         break;

  225.                     case NORAD_CAT_ID:
  226.                         file.setNoradID(Integer.parseInt(pi.keyValue.getValue()));
  227.                         break;

  228.                     case ELEMENT_SET_NO:
  229.                         file.setElementSetNo(pi.keyValue.getValue());
  230.                         break;

  231.                     case REV_AT_EPOCH:
  232.                         file.setRevAtEpoch(Integer.parseInt(pi.keyValue.getValue()));
  233.                         break;

  234.                     case BSTAR:
  235.                         file.setbStar(pi.keyValue.getDoubleValue());
  236.                         break;

  237.                     case MEAN_MOTION_DOT:
  238.                         file.setMeanMotionDot(pi.keyValue.getDoubleValue() * FastMath.PI / 1.86624e9);
  239.                         break;

  240.                     case MEAN_MOTION_DDOT:
  241.                         file.setMeanMotionDotDot(pi.keyValue.getDoubleValue() *
  242.                                                  FastMath.PI / 5.3747712e13);
  243.                         break;

  244.                     default:
  245.                         boolean parsed = false;
  246.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  247.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  248.                         parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  249.                         parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  250.                         if (!parsed) {
  251.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  252.                         }
  253.                 }
  254.             }
  255.             reader.close();
  256.             return file;
  257.         } catch (IOException ioe) {
  258.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  259.         }
  260.     }

  261.     /** Private class used to stock OMM parsing info.
  262.      * @author sports
  263.      */
  264.     private static class ParseInfo {

  265.         /** OMM file being read. */
  266.         private OMMFile file;

  267.         /** Name of the file. */
  268.         private String fileName;

  269.         /** Current line number. */
  270.         private int lineNumber;

  271.         /** Key value of the line being read. */
  272.         private KeyValue keyValue;

  273.         /** Stored comments. */
  274.         private List<String> commentTmp;

  275.         /** Create a new {@link ParseInfo} object. */
  276.         protected ParseInfo() {
  277.             lineNumber = 0;
  278.             file = new OMMFile();
  279.             commentTmp = new ArrayList<String>();
  280.         }
  281.     }
  282. }