OPMParser.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.geometry.euclidean.threed.Vector3D;
  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 OPM (Orbit Parameter Message).
  31.  * @author sports
  32.  * @since 6.1
  33.  */
  34. public class OPMParser 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.      */
  64.     public OPMParser() {
  65.         this(AbsoluteDate.FUTURE_INFINITY, Double.NaN, null, true, 0, 0, "");
  66.     }

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

  81.     /** {@inheritDoc} */
  82.     public OPMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  83.         return new OPMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  84.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  85.     }

  86.     /** {@inheritDoc} */
  87.     public OPMParser withMu(final double newMu) {
  88.         return new OPMParser(getMissionReferenceDate(), newMu, getConventions(), isSimpleEOP(),
  89.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  90.     }

  91.     /** {@inheritDoc} */
  92.     public OPMParser withConventions(final IERSConventions newConventions) {
  93.         return new OPMParser(getMissionReferenceDate(), getMu(), newConventions, isSimpleEOP(),
  94.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  95.     }

  96.     /** {@inheritDoc} */
  97.     public OPMParser withSimpleEOP(final boolean newSimpleEOP) {
  98.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  99.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  100.     }

  101.     /** {@inheritDoc} */
  102.     public OPMParser withInternationalDesignator(final int newLaunchYear,
  103.                                                  final int newLaunchNumber,
  104.                                                  final String newLaunchPiece) {
  105.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  106.                              newLaunchYear, newLaunchNumber, newLaunchPiece);
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public OPMFile parse(final String fileName) throws OrekitException {
  111.         return (OPMFile) super.parse(fileName);
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public OPMFile parse(final InputStream stream) throws OrekitException {
  116.         return (OPMFile) super.parse(stream);
  117.     }

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

  120.         try {
  121.             final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
  122.             // initialize internal data structures
  123.             final ParseInfo pi = new ParseInfo();
  124.             pi.fileName = fileName;
  125.             final OPMFile 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_OPM_VERS:
  144.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  145.                         break;

  146.                     case X:
  147.                         pi.x = pi.keyValue.getDoubleValue() * 1000;
  148.                         break;

  149.                     case Y:
  150.                         pi.y = pi.keyValue.getDoubleValue() * 1000;
  151.                         break;

  152.                     case Z:
  153.                         pi.z = pi.keyValue.getDoubleValue() * 1000;
  154.                         break;

  155.                     case X_DOT:
  156.                         pi.x_dot = pi.keyValue.getDoubleValue() * 1000;
  157.                         break;

  158.                     case Y_DOT:
  159.                         pi.y_dot = pi.keyValue.getDoubleValue() * 1000;
  160.                         break;

  161.                     case Z_DOT:
  162.                         pi.z_dot = pi.keyValue.getDoubleValue() * 1000;
  163.                         break;

  164.                     case MAN_EPOCH_IGNITION:
  165.                         if (pi.maneuver != null) {
  166.                             file.addManeuver(pi.maneuver);
  167.                         }
  168.                         pi.maneuver = new OPMFile.Maneuver();
  169.                         pi.maneuver.setEpochIgnition(parseDate(pi.keyValue.getValue(), file.getMetaData().getTimeSystem()));
  170.                         if (!pi.commentTmp.isEmpty()) {
  171.                             pi.maneuver.setComment(pi.commentTmp);
  172.                             pi.commentTmp.clear();
  173.                         }
  174.                         break;

  175.                     case MAN_DURATION:
  176.                         pi.maneuver.setDuration(pi.keyValue.getDoubleValue());
  177.                         break;

  178.                     case MAN_DELTA_MASS:
  179.                         pi.maneuver.setDeltaMass(pi.keyValue.getDoubleValue());
  180.                         break;

  181.                     case MAN_REF_FRAME:
  182.                         final CCSDSFrame manFrame = parseCCSDSFrame(pi.keyValue.getValue());
  183.                         if (manFrame.isLof()) {
  184.                             pi.maneuver.setRefLofType(manFrame.getLofType());
  185.                         } else {
  186.                             pi.maneuver.setRefFrame(manFrame.getFrame(getConventions(), isSimpleEOP()));
  187.                         }
  188.                         break;

  189.                     case MAN_DV_1:
  190.                         pi.maneuver.setdV(new Vector3D(pi.keyValue.getDoubleValue() * 1000,
  191.                                                        pi.maneuver.getDV().getY(),
  192.                                                        pi.maneuver.getDV().getZ()));
  193.                         break;

  194.                     case MAN_DV_2:
  195.                         pi.maneuver.setdV(new Vector3D(pi.maneuver.getDV().getX(),
  196.                                                        pi.keyValue.getDoubleValue() * 1000,
  197.                                                        pi.maneuver.getDV().getZ()));
  198.                         break;

  199.                     case MAN_DV_3:
  200.                         pi.maneuver.setdV(new Vector3D(pi.maneuver.getDV().getX(),
  201.                                                        pi.maneuver.getDV().getY(),
  202.                                                        pi.keyValue.getDoubleValue() * 1000));
  203.                         break;

  204.                     default:
  205.                         boolean parsed = false;
  206.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  207.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  208.                         parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  209.                         parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  210.                         if (!parsed) {
  211.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  212.                         }
  213.                 }

  214.             }

  215.             file.setPosition(new Vector3D(pi.x, pi.y, pi.z));
  216.             file.setVelocity(new Vector3D(pi.x_dot, pi.y_dot, pi.z_dot));
  217.             if (pi.maneuver != null) {
  218.                 file.addManeuver(pi.maneuver);
  219.             }
  220.             reader.close();
  221.             return file;
  222.         } catch (IOException ioe) {
  223.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  224.         }
  225.     }

  226.     /** Private class used to stock OPM parsing info.
  227.      * @author sports
  228.      */
  229.     private static class ParseInfo {

  230.         /** OPM file being read. */
  231.         private OPMFile file;

  232.         /** Name of the file. */
  233.         private String fileName;

  234.         /** Current line number. */
  235.         private int lineNumber;

  236.         /** Key value of the line being read. */
  237.         private KeyValue keyValue;

  238.         /** Stored comments. */
  239.         private List<String> commentTmp;

  240.         /** First component of position vector. */
  241.         private double x;

  242.         /** Second component of position vector. */
  243.         private double y;

  244.         /** Third component of position vector. */
  245.         private double z;

  246.         /** First component of velocity vector. */
  247.         private double x_dot;

  248.         /** Second component of velocity vector. */
  249.         private double y_dot;

  250.         /** Third component of velocity vector. */
  251.         private double z_dot;

  252.         /** Current maneuver. */
  253.         private OPMFile.Maneuver maneuver;

  254.         /** Create a new {@link ParseInfo} object. */
  255.         protected ParseInfo() {
  256.             file       = new OPMFile();
  257.             lineNumber = 0;
  258.             commentTmp = new ArrayList<String>();
  259.             maneuver   = null;
  260.         }
  261.     }
  262. }