OPMParser.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.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.     public OPMFile parse(final InputStream stream, final String fileName) throws OrekitException {

  114.         try {
  115.             final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
  116.             // initialize internal data structures
  117.             final ParseInfo pi = new ParseInfo();
  118.             pi.fileName = fileName;
  119.             final OPMFile file = pi.file;

  120.             // set the additional data that has been configured prior the parsing by the user.
  121.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  122.             pi.file.setMuSet(getMu());
  123.             pi.file.setConventions(getConventions());
  124.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  125.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  126.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  127.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  128.                 ++pi.lineNumber;
  129.                 if (line.trim().length() == 0) {
  130.                     continue;
  131.                 }
  132.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  133.                 if (pi.keyValue.getKeyword() == null) {
  134.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  135.                 }
  136.                 switch (pi.keyValue.getKeyword()) {

  137.                 case CCSDS_OPM_VERS:
  138.                     file.setFormatVersion(pi.keyValue.getDoubleValue());
  139.                     break;

  140.                 case X:
  141.                     pi.x = pi.keyValue.getDoubleValue() * 1000;
  142.                     break;

  143.                 case Y:
  144.                     pi.y = pi.keyValue.getDoubleValue() * 1000;
  145.                     break;

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

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

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

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

  158.                 case MAN_EPOCH_IGNITION:
  159.                     if (pi.maneuver != null) {
  160.                         file.addManeuver(pi.maneuver);
  161.                     }
  162.                     pi.maneuver = new OPMFile.Maneuver();
  163.                     pi.maneuver.setEpochIgnition(parseDate(pi.keyValue.getValue(), file.getTimeSystem()));
  164.                     if (!pi.commentTmp.isEmpty()) {
  165.                         pi.maneuver.setComment(pi.commentTmp);
  166.                         pi.commentTmp.clear();
  167.                     }
  168.                     break;

  169.                 case MAN_DURATION:
  170.                     pi.maneuver.setDuration(pi.keyValue.getDoubleValue());
  171.                     break;

  172.                 case MAN_DELTA_MASS:
  173.                     pi.maneuver.setDeltaMass(pi.keyValue.getDoubleValue());
  174.                     break;

  175.                 case MAN_REF_FRAME:
  176.                     final CCSDSFrame manFrame = parseCCSDSFrame(pi.keyValue.getValue());
  177.                     if (manFrame.isLof()) {
  178.                         pi.maneuver.setRefLofType(manFrame.getLofType());
  179.                     } else {
  180.                         pi.maneuver.setRefFrame(manFrame.getFrame(getConventions(), isSimpleEOP()));
  181.                     }
  182.                     break;

  183.                 case MAN_DV_1:
  184.                     pi.maneuver.setdV(new Vector3D(pi.keyValue.getDoubleValue() * 1000,
  185.                                                    pi.maneuver.getDV().getY(),
  186.                                                    pi.maneuver.getDV().getZ()));
  187.                     break;

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

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

  198.                 default:
  199.                     boolean parsed = false;
  200.                     parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  201.                     parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  202.                     parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  203.                     parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  204.                     if (!parsed) {
  205.                         throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  206.                     }
  207.                 }

  208.             }

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

  220.     /** Private class used to stock OPM parsing info.
  221.      * @author sports
  222.      */
  223.     private static class ParseInfo {

  224.         /** OPM file being read. */
  225.         private OPMFile file;

  226.         /** Name of the file. */
  227.         private String fileName;

  228.         /** Current line number. */
  229.         private int lineNumber;

  230.         /** Key value of the line being read. */
  231.         private KeyValue keyValue;

  232.         /** Stored comments. */
  233.         private List<String> commentTmp;

  234.         /** First component of position vector. */
  235.         private double x;

  236.         /** Second component of position vector. */
  237.         private double y;

  238.         /** Third component of position vector. */
  239.         private double z;

  240.         /** First component of velocity vector. */
  241.         private double x_dot;

  242.         /** Second component of velocity vector. */
  243.         private double y_dot;

  244.         /** Third component of velocity vector. */
  245.         private double z_dot;

  246.         /** Current maneuver. */
  247.         private OPMFile.Maneuver maneuver;

  248.         /** Create a new {@link ParseInfo} object. */
  249.         protected ParseInfo() {
  250.             file       = new OPMFile();
  251.             lineNumber = 0;
  252.             commentTmp = new ArrayList<String>();
  253.             maneuver   = null;
  254.         }
  255.     }
  256. }