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.     public OPMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  131.         return new OPMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  132.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  133.     }

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

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

  144.     /** {@inheritDoc} */
  145.     public OPMParser withSimpleEOP(final boolean newSimpleEOP) {
  146.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  147.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(), getDataContext());
  148.     }

  149.     /** {@inheritDoc} */
  150.     public OPMParser withInternationalDesignator(final int newLaunchYear,
  151.                                                  final int newLaunchNumber,
  152.                                                  final String newLaunchPiece) {
  153.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  154.                              newLaunchYear, newLaunchNumber, newLaunchPiece, getDataContext());
  155.     }

  156.     @Override
  157.     public OPMParser withDataContext(final DataContext newDataContext) {
  158.         return new OPMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  159.                 getLaunchYear(), getLaunchNumber(), getLaunchPiece(), newDataContext);
  160.     }

  161.     /** {@inheritDoc} */
  162.     @Override
  163.     public OPMFile parse(final String fileName) {
  164.         return (OPMFile) super.parse(fileName);
  165.     }

  166.     /** {@inheritDoc} */
  167.     @Override
  168.     public OPMFile parse(final InputStream stream) {
  169.         return (OPMFile) super.parse(stream);
  170.     }

  171.     /** {@inheritDoc} */
  172.     public OPMFile parse(final InputStream stream, final String fileName) {

  173.         try {
  174.             final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
  175.             // initialize internal data structures
  176.             final ParseInfo pi = new ParseInfo();
  177.             pi.fileName = fileName;
  178.             final OPMFile file = pi.file;

  179.             // set the additional data that has been configured prior the parsing by the user.
  180.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  181.             pi.file.setMuSet(getMu());
  182.             pi.file.setConventions(getConventions());
  183.             pi.file.setDataContext(getDataContext());
  184.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  185.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  186.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  187.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  188.                 ++pi.lineNumber;
  189.                 if (line.trim().length() == 0) {
  190.                     continue;
  191.                 }
  192.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  193.                 if (pi.keyValue.getKeyword() == null) {
  194.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  195.                 }
  196.                 switch (pi.keyValue.getKeyword()) {

  197.                     case CCSDS_OPM_VERS:
  198.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  199.                         break;

  200.                     case X:
  201.                         pi.x = pi.keyValue.getDoubleValue() * 1000;
  202.                         break;

  203.                     case Y:
  204.                         pi.y = pi.keyValue.getDoubleValue() * 1000;
  205.                         break;

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

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

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

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

  218.                     case MAN_EPOCH_IGNITION:
  219.                         if (pi.maneuver != null) {
  220.                             file.addManeuver(pi.maneuver);
  221.                         }
  222.                         pi.maneuver = new OPMFile.Maneuver();
  223.                         pi.maneuver.setEpochIgnition(parseDate(pi.keyValue.getValue(), file.getMetaData().getTimeSystem()));
  224.                         if (!pi.commentTmp.isEmpty()) {
  225.                             pi.maneuver.setComment(pi.commentTmp);
  226.                             pi.commentTmp.clear();
  227.                         }
  228.                         break;

  229.                     case MAN_DURATION:
  230.                         pi.maneuver.setDuration(pi.keyValue.getDoubleValue());
  231.                         break;

  232.                     case MAN_DELTA_MASS:
  233.                         pi.maneuver.setDeltaMass(pi.keyValue.getDoubleValue());
  234.                         break;

  235.                     case MAN_REF_FRAME:
  236.                         final CCSDSFrame manFrame = parseCCSDSFrame(pi.keyValue.getValue());
  237.                         if (manFrame.isLof()) {
  238.                             pi.maneuver.setRefLofType(manFrame.getLofType());
  239.                         } else {
  240.                             pi.maneuver.setRefFrame(manFrame.getFrame(
  241.                                     getConventions(),
  242.                                     isSimpleEOP(),
  243.                                     getDataContext()));
  244.                         }
  245.                         break;

  246.                     case MAN_DV_1:
  247.                         pi.maneuver.setdV(new Vector3D(pi.keyValue.getDoubleValue() * 1000,
  248.                                                        pi.maneuver.getDV().getY(),
  249.                                                        pi.maneuver.getDV().getZ()));
  250.                         break;

  251.                     case MAN_DV_2:
  252.                         pi.maneuver.setdV(new Vector3D(pi.maneuver.getDV().getX(),
  253.                                                        pi.keyValue.getDoubleValue() * 1000,
  254.                                                        pi.maneuver.getDV().getZ()));
  255.                         break;

  256.                     case MAN_DV_3:
  257.                         pi.maneuver.setdV(new Vector3D(pi.maneuver.getDV().getX(),
  258.                                                        pi.maneuver.getDV().getY(),
  259.                                                        pi.keyValue.getDoubleValue() * 1000));
  260.                         break;

  261.                     default:
  262.                         boolean parsed = false;
  263.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  264.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  265.                         parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  266.                         parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  267.                         if (!parsed) {
  268.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  269.                         }
  270.                 }

  271.             }

  272.             file.setPosition(new Vector3D(pi.x, pi.y, pi.z));
  273.             file.setVelocity(new Vector3D(pi.x_dot, pi.y_dot, pi.z_dot));
  274.             if (pi.maneuver != null) {
  275.                 file.addManeuver(pi.maneuver);
  276.             }
  277.             reader.close();
  278.             return file;
  279.         } catch (IOException ioe) {
  280.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  281.         }
  282.     }

  283.     /** Private class used to stock OPM parsing info.
  284.      * @author sports
  285.      */
  286.     private static class ParseInfo {

  287.         /** OPM file being read. */
  288.         private OPMFile file;

  289.         /** Name of the file. */
  290.         private String fileName;

  291.         /** Current line number. */
  292.         private int lineNumber;

  293.         /** Key value of the line being read. */
  294.         private KeyValue keyValue;

  295.         /** Stored comments. */
  296.         private List<String> commentTmp;

  297.         /** First component of position vector. */
  298.         private double x;

  299.         /** Second component of position vector. */
  300.         private double y;

  301.         /** Third component of position vector. */
  302.         private double z;

  303.         /** First component of velocity vector. */
  304.         private double x_dot;

  305.         /** Second component of velocity vector. */
  306.         private double y_dot;

  307.         /** Third component of velocity vector. */
  308.         private double z_dot;

  309.         /** Current maneuver. */
  310.         private OPMFile.Maneuver maneuver;

  311.         /** Create a new {@link ParseInfo} object. */
  312.         protected ParseInfo() {
  313.             file       = new OPMFile();
  314.             lineNumber = 0;
  315.             commentTmp = new ArrayList<String>();
  316.             maneuver   = null;
  317.         }
  318.     }
  319. }