OMMParser.java

  1. /* Copyright 2002-2013 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.apache.commons.math3.exception.util.DummyLocalizable;
  25. import org.apache.commons.math3.util.FastMath;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.files.general.OrbitFileParser;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.IERSConventions;

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

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

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

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

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

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

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

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

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

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

  119.         try {

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

  122.             // initialize internal data structures
  123.             final ParseInfo pi = new ParseInfo();
  124.             pi.fileName = fileName;
  125.             final OMMFile file = pi.file;

  126.             // set the additional data that has been configured prior the parsing by the user.
  127.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  128.             pi.file.setMuSet(getMu());
  129.             pi.file.setConventions(getConventions());
  130.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  131.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  132.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  133.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  134.                 ++pi.lineNumber;
  135.                 if (line.trim().length() == 0) {
  136.                     continue;
  137.                 }
  138.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  139.                 if (pi.keyValue.getKeyword() == null) {
  140.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  141.                 }
  142.                 switch (pi.keyValue.getKeyword()) {
  143.                 case CCSDS_OMM_VERS:
  144.                     file.setFormatVersion(pi.keyValue.getDoubleValue());
  145.                     break;

  146.                 case MEAN_ELEMENT_THEORY:
  147.                     file.getMetaData().setMeanElementTheory(pi.keyValue.getValue());
  148.                     break;

  149.                 case MEAN_MOTION:
  150.                     file.setMeanMotion(pi.keyValue.getDoubleValue() * FastMath.PI / 43200.0);
  151.                     break;

  152.                 case EPHEMERIS_TYPE:
  153.                     file.setTLERelatedParametersComment(pi.commentTmp);
  154.                     pi.commentTmp.clear();
  155.                     file.setEphemerisType(Integer.parseInt(pi.keyValue.getValue()));
  156.                     break;

  157.                 case CLASSIFICATION_TYPE:
  158.                     file.setClassificationType(pi.keyValue.getValue().charAt(0));
  159.                     break;

  160.                 case NORAD_CAT_ID:
  161.                     file.setNoradID(Integer.parseInt(pi.keyValue.getValue()));
  162.                     break;

  163.                 case ELEMENT_SET_NO:
  164.                     file.setElementSetNo(pi.keyValue.getValue());
  165.                     break;

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

  169.                 case BSTAR:
  170.                     file.setbStar(pi.keyValue.getDoubleValue());
  171.                     break;

  172.                 case MEAN_MOTION_DOT:
  173.                     file.setMeanMotionDot(pi.keyValue.getDoubleValue() * FastMath.PI / 1.86624e9);
  174.                     break;

  175.                 case MEAN_MOTION_DDOT:
  176.                     file.setMeanMotionDotDot(pi.keyValue.getDoubleValue() *
  177.                                              FastMath.PI / 5.3747712e13);
  178.                     break;

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

  196.     /** Private class used to stock OMM parsing info.
  197.      * @author sports
  198.      */
  199.     private static class ParseInfo {

  200.         /** OMM file being read. */
  201.         private OMMFile file;

  202.         /** Name of the file. */
  203.         private String fileName;

  204.         /** Current line number. */
  205.         private int lineNumber;

  206.         /** Key value of the line being read. */
  207.         private KeyValue keyValue;

  208.         /** Stored comments. */
  209.         private List<String> commentTmp;

  210.         /** Create a new {@link ParseInfo} object. */
  211.         protected ParseInfo() {
  212.             lineNumber = 0;
  213.             file = new OMMFile();
  214.             commentTmp = new ArrayList<String>();
  215.         }
  216.     }
  217. }