OPMParser.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.geometry.euclidean.threed.Vector3D;
  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 OPM (Orbit Parameter Message).
  34.  * @author sports
  35.  * @since 6.1
  36.  */
  37. public class OPMParser 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.      *
  67.      * <p>This method uses the {@link DataContext#getDefault() default data context}. See
  68.      * {@link #withDataContext(DataContext)}.
  69.      */
  70.     @DefaultDataContext
  71.     public OPMParser() {
  72.         this(DataContext.getDefault());
  73.     }

  74.     /** Constructor with data context.
  75.      * <p>
  76.      * This class is immutable, and hence thread safe. When parts
  77.      * must be changed, such as reference date for Mission Elapsed Time or
  78.      * Mission Relative Time time systems, or the gravitational coefficient or
  79.      * the IERS conventions, the various {@code withXxx} methods must be called,
  80.      * which create a new immutable instance with the new parameters. This
  81.      * is a combination of the
  82.      * <a href="https://en.wikipedia.org/wiki/Builder_pattern">builder design
  83.      * pattern</a> and a
  84.      * <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
  85.      * interface</a>.
  86.      * </p>
  87.      * <p>
  88.      * The initial date for Mission Elapsed Time and Mission Relative Time time systems is not set here.
  89.      * If such time systems are used, it must be initialized before parsing by calling {@link
  90.      * #withMissionReferenceDate(AbsoluteDate)}.
  91.      * </p>
  92.      * <p>
  93.      * The gravitational coefficient is not set here. If it is needed in order
  94.      * to parse Cartesian orbits where the value is not set in the CCSDS file, it must
  95.      * be initialized before parsing by calling {@link #withMu(double)}.
  96.      * </p>
  97.      * <p>
  98.      * The IERS conventions to use is not set here. If it is needed in order to
  99.      * parse some reference frames or UT1 time scale, it must be initialized before
  100.      * parsing by calling {@link #withConventions(IERSConventions)}.
  101.      * </p>
  102.      *
  103.      * @param dataContext used by the parser.
  104.      *
  105.      * @see #OPMParser()
  106.      * @see #withDataContext(DataContext)
  107.      * @since 10.1
  108.      */
  109.     public OPMParser(final DataContext dataContext) {
  110.         this(AbsoluteDate.FUTURE_INFINITY, Double.NaN, null, true, 0, 0, "", dataContext);
  111.     }

  112.     /** Complete constructor.
  113.      * @param missionReferenceDate reference date for Mission Elapsed Time or Mission Relative Time time systems
  114.      * @param mu gravitational coefficient
  115.      * @param conventions IERS Conventions
  116.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  117.      * @param launchYear launch year for TLEs
  118.      * @param launchNumber launch number for TLEs
  119.      * @param launchPiece piece of launch (from "A" to "ZZZ") for TLEs
  120.      * @param dataContext used to retrieve frames, time scales, etc.
  121.      */
  122.     private OPMParser(final AbsoluteDate missionReferenceDate, final double mu,
  123.                       final IERSConventions conventions, final boolean simpleEOP,
  124.                       final int launchYear, final int launchNumber,
  125.                       final String launchPiece, final DataContext dataContext) {
  126.         super(missionReferenceDate, mu, conventions, simpleEOP, launchYear, launchNumber,
  127.                 launchPiece, dataContext);
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public OPMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  132.         return new OPMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  133.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     public OPMParser withMu(final double newMu) {
  138.         return new OPMParser(getMissionReferenceDate(), newMu, getConventions(), isSimpleEOP(),
  139.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     public OPMParser withConventions(final IERSConventions newConventions) {
  144.         return new OPMParser(getMissionReferenceDate(), getMu(), newConventions, isSimpleEOP(),
  145.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public OPMParser withSimpleEOP(final boolean newSimpleEOP) {
  150.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  151.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  152.     }

  153.     /** {@inheritDoc} */
  154.     @Override
  155.     public OPMParser withInternationalDesignator(final int newLaunchYear,
  156.                                                  final int newLaunchNumber,
  157.                                                  final String newLaunchPiece) {
  158.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  159.                              newLaunchYear, newLaunchNumber, newLaunchPiece, getDataContext());
  160.     }

  161.     /** {@inheritDoc} */
  162.     @Override
  163.     public OPMParser withDataContext(final DataContext newDataContext) {
  164.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  165.                 getLaunchYear(), getLaunchNumber(), getLaunchPiece(), newDataContext);
  166.     }

  167.     /** {@inheritDoc} */
  168.     @Override
  169.     public OPMFile parse(final String fileName) {
  170.         return (OPMFile) super.parse(fileName);
  171.     }

  172.     /** {@inheritDoc} */
  173.     @Override
  174.     public OPMFile parse(final InputStream stream) {
  175.         return (OPMFile) super.parse(stream);
  176.     }

  177.     /** {@inheritDoc} */
  178.     @Override
  179.     public OPMFile parse(final InputStream stream, final String fileName) {

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

  181.             // initialize internal data structures
  182.             final ParseInfo pi = new ParseInfo();
  183.             pi.fileName = fileName;
  184.             final OPMFile file = pi.file;

  185.             // set the additional data that has been configured prior the parsing by the user.
  186.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  187.             pi.file.setMuSet(getMu());
  188.             pi.file.setConventions(getConventions());
  189.             pi.file.setDataContext(getDataContext());
  190.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  191.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  192.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  193.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  194.                 ++pi.lineNumber;
  195.                 if (line.trim().length() == 0) {
  196.                     continue;
  197.                 }
  198.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  199.                 if (pi.keyValue.getKeyword() == null) {
  200.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  201.                 }
  202.                 switch (pi.keyValue.getKeyword()) {

  203.                     case CCSDS_OPM_VERS:
  204.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  205.                         break;

  206.                     case X:
  207.                         pi.x = pi.keyValue.getDoubleValue() * 1000;
  208.                         break;

  209.                     case Y:
  210.                         pi.y = pi.keyValue.getDoubleValue() * 1000;
  211.                         break;

  212.                     case Z:
  213.                         pi.z = pi.keyValue.getDoubleValue() * 1000;
  214.                         break;

  215.                     case X_DOT:
  216.                         pi.x_dot = pi.keyValue.getDoubleValue() * 1000;
  217.                         break;

  218.                     case Y_DOT:
  219.                         pi.y_dot = pi.keyValue.getDoubleValue() * 1000;
  220.                         break;

  221.                     case Z_DOT:
  222.                         pi.z_dot = pi.keyValue.getDoubleValue() * 1000;
  223.                         break;

  224.                     case MAN_EPOCH_IGNITION:
  225.                         if (pi.maneuver != null) {
  226.                             file.addManeuver(pi.maneuver);
  227.                         }
  228.                         pi.maneuver = new OPMFile.Maneuver();
  229.                         pi.maneuver.setEpochIgnition(parseDate(pi.keyValue.getValue(), file.getMetaData().getTimeSystem()));
  230.                         if (!pi.commentTmp.isEmpty()) {
  231.                             pi.maneuver.setComment(pi.commentTmp);
  232.                             pi.commentTmp.clear();
  233.                         }
  234.                         break;

  235.                     case MAN_DURATION:
  236.                         pi.maneuver.setDuration(pi.keyValue.getDoubleValue());
  237.                         break;

  238.                     case MAN_DELTA_MASS:
  239.                         pi.maneuver.setDeltaMass(pi.keyValue.getDoubleValue());
  240.                         break;

  241.                     case MAN_REF_FRAME:
  242.                         final CCSDSFrame manFrame = parseCCSDSFrame(pi.keyValue.getValue());
  243.                         if (manFrame.isLof()) {
  244.                             pi.maneuver.setRefLofType(manFrame.getLofType());
  245.                         } else {
  246.                             pi.maneuver.setRefFrame(manFrame.getFrame(
  247.                                     getConventions(),
  248.                                     isSimpleEOP(),
  249.                                     getDataContext()));
  250.                         }
  251.                         break;

  252.                     case MAN_DV_1:
  253.                         pi.maneuver.setdV(new Vector3D(pi.keyValue.getDoubleValue() * 1000,
  254.                                                        pi.maneuver.getDV().getY(),
  255.                                                        pi.maneuver.getDV().getZ()));
  256.                         break;

  257.                     case MAN_DV_2:
  258.                         pi.maneuver.setdV(new Vector3D(pi.maneuver.getDV().getX(),
  259.                                                        pi.keyValue.getDoubleValue() * 1000,
  260.                                                        pi.maneuver.getDV().getZ()));
  261.                         break;

  262.                     case MAN_DV_3:
  263.                         pi.maneuver.setdV(new Vector3D(pi.maneuver.getDV().getX(),
  264.                                                        pi.maneuver.getDV().getY(),
  265.                                                        pi.keyValue.getDoubleValue() * 1000));
  266.                         break;

  267.                     default:
  268.                         boolean parsed = false;
  269.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  270.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  271.                         parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  272.                         parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  273.                         if (!parsed) {
  274.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  275.                         }
  276.                 }

  277.             }

  278.             file.setPosition(new Vector3D(pi.x, pi.y, pi.z));
  279.             file.setVelocity(new Vector3D(pi.x_dot, pi.y_dot, pi.z_dot));
  280.             if (pi.maneuver != null) {
  281.                 file.addManeuver(pi.maneuver);
  282.             }
  283.             return file;
  284.         } catch (IOException ioe) {
  285.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  286.         }
  287.     }

  288.     /** Private class used to stock OPM parsing info.
  289.      * @author sports
  290.      */
  291.     private static class ParseInfo {

  292.         /** OPM file being read. */
  293.         private OPMFile file;

  294.         /** Name of the file. */
  295.         private String fileName;

  296.         /** Current line number. */
  297.         private int lineNumber;

  298.         /** Key value of the line being read. */
  299.         private KeyValue keyValue;

  300.         /** Stored comments. */
  301.         private List<String> commentTmp;

  302.         /** First component of position vector. */
  303.         private double x;

  304.         /** Second component of position vector. */
  305.         private double y;

  306.         /** Third component of position vector. */
  307.         private double z;

  308.         /** First component of velocity vector. */
  309.         private double x_dot;

  310.         /** Second component of velocity vector. */
  311.         private double y_dot;

  312.         /** Third component of velocity vector. */
  313.         private double z_dot;

  314.         /** Current maneuver. */
  315.         private OPMFile.Maneuver maneuver;

  316.         /** Create a new {@link ParseInfo} object. */
  317.         protected ParseInfo() {
  318.             file       = new OPMFile();
  319.             lineNumber = 0;
  320.             commentTmp = new ArrayList<String>();
  321.             maneuver   = null;
  322.         }
  323.     }
  324. }