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.     @Override
  141.     public OMMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  142.         return new OMMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  143.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  144.     }

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

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

  157.     /** {@inheritDoc} */
  158.     @Override
  159.     public OMMParser withSimpleEOP(final boolean newSimpleEOP) {
  160.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  161.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  162.     }

  163.     /** {@inheritDoc} */
  164.     @Override
  165.     public OMMParser withInternationalDesignator(final int newLaunchYear,
  166.                                                  final int newLaunchNumber,
  167.                                                  final String newLaunchPiece) {
  168.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  169.                              newLaunchYear, newLaunchNumber, newLaunchPiece, getDataContext());
  170.     }

  171.     /** {@inheritDoc} */
  172.     @Override
  173.     public OMMParser withDataContext(final DataContext newDataContext) {
  174.         return new OMMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  175.                 getLaunchYear(), getLaunchNumber(), getLaunchPiece(), newDataContext);
  176.     }

  177.     /** {@inheritDoc} */
  178.     @Override
  179.     public OMMFile parse(final String fileName) {
  180.         return (OMMFile) super.parse(fileName);
  181.     }

  182.     /** {@inheritDoc} */
  183.     @Override
  184.     public OMMFile parse(final InputStream stream) {
  185.         return (OMMFile) super.parse(stream);
  186.     }

  187.     /** {@inheritDoc} */
  188.     @Override
  189.     public OMMFile parse(final InputStream stream, final String fileName) {

  190.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {

  191.             // initialize internal data structures
  192.             final ParseInfo pi = new ParseInfo();
  193.             pi.fileName = fileName;
  194.             final OMMFile file = pi.file;

  195.             // set the additional data that has been configured prior the parsing by the user.
  196.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  197.             pi.file.setMuSet(getMu());
  198.             pi.file.setConventions(getConventions());
  199.             pi.file.setDataContext(getDataContext());
  200.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  201.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  202.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

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

  216.                     case MEAN_ELEMENT_THEORY:
  217.                         file.getMetaData().setMeanElementTheory(pi.keyValue.getValue());
  218.                         break;

  219.                     case MEAN_MOTION:
  220.                         file.setMeanMotion(pi.keyValue.getDoubleValue() * FastMath.PI / 43200.0);
  221.                         break;

  222.                     case EPHEMERIS_TYPE:
  223.                         file.setTLERelatedParametersComment(pi.commentTmp);
  224.                         pi.commentTmp.clear();
  225.                         file.setEphemerisType(Integer.parseInt(pi.keyValue.getValue()));
  226.                         break;

  227.                     case CLASSIFICATION_TYPE:
  228.                         file.setClassificationType(pi.keyValue.getValue().charAt(0));
  229.                         break;

  230.                     case NORAD_CAT_ID:
  231.                         file.setNoradID(Integer.parseInt(pi.keyValue.getValue()));
  232.                         break;

  233.                     case ELEMENT_SET_NO:
  234.                         file.setElementSetNo(pi.keyValue.getValue());
  235.                         break;

  236.                     case REV_AT_EPOCH:
  237.                         file.setRevAtEpoch(Integer.parseInt(pi.keyValue.getValue()));
  238.                         break;

  239.                     case BSTAR:
  240.                         file.setbStar(pi.keyValue.getDoubleValue());
  241.                         break;

  242.                     case MEAN_MOTION_DOT:
  243.                         file.setMeanMotionDot(pi.keyValue.getDoubleValue() * FastMath.PI / 1.86624e9);
  244.                         break;

  245.                     case MEAN_MOTION_DDOT:
  246.                         file.setMeanMotionDotDot(pi.keyValue.getDoubleValue() *
  247.                                                  FastMath.PI / 5.3747712e13);
  248.                         break;

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

  265.     /** Private class used to stock OMM parsing info.
  266.      * @author sports
  267.      */
  268.     private static class ParseInfo {

  269.         /** OMM file being read. */
  270.         private OMMFile file;

  271.         /** Name of the file. */
  272.         private String fileName;

  273.         /** Current line number. */
  274.         private int lineNumber;

  275.         /** Key value of the line being read. */
  276.         private KeyValue keyValue;

  277.         /** Stored comments. */
  278.         private List<String> commentTmp;

  279.         /** Create a new {@link ParseInfo} object. */
  280.         protected ParseInfo() {
  281.             lineNumber = 0;
  282.             file = new OMMFile();
  283.             commentTmp = new ArrayList<String>();
  284.         }
  285.     }
  286. }