OPMParser.java

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

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

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

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

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

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

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

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

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

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

  119.         try {
  120.             final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
  121.             // initialize internal data structures
  122.             final ParseInfo pi = new ParseInfo();
  123.             pi.fileName = fileName;
  124.             final OPMFile file = pi.file;

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

  132.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  133.                 ++pi.lineNumber;
  134.                 if (line.trim().length() == 0) {
  135.                     continue;
  136.                 }
  137.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  138.                 if (pi.keyValue.getKeyword() == null) {
  139.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  140.                 }
  141.                 switch (pi.keyValue.getKeyword()) {

  142.                     case CCSDS_OPM_VERS:
  143.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  144.                         break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  213.             }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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