OEMParser.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 java.util.Scanner;

  25. import org.hipparchus.exception.DummyLocalizable;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.linear.MatrixUtils;
  28. import org.hipparchus.linear.RealMatrix;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.files.general.EphemerisFileParser;
  32. import org.orekit.frames.Frame;
  33. import org.orekit.frames.LOFType;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.utils.IERSConventions;
  36. import org.orekit.utils.TimeStampedPVCoordinates;

  37. /**
  38.  * A parser for the CCSDS OEM (Orbit Ephemeris Message).
  39.  * @author sports
  40.  * @since 6.1
  41.  */
  42. public class OEMParser extends ODMParser implements EphemerisFileParser {

  43.     /** Simple constructor.
  44.      * <p>
  45.      * This class is immutable, and hence thread safe. When parts
  46.      * must be changed, such as reference date for Mission Elapsed Time or
  47.      * Mission Relative Time time systems, or the gravitational coefficient or
  48.      * the IERS conventions, the various {@code withXxx} methods must be called,
  49.      * which create a new immutable instance with the new parameters. This
  50.      * is a combination of the
  51.      * <a href="https://en.wikipedia.org/wiki/Builder_pattern">builder design
  52.      * pattern</a> and a
  53.      * <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
  54.      * interface</a>.
  55.      * </p>
  56.      * <p>
  57.      * The initial date for Mission Elapsed Time and Mission Relative Time time systems is not set here.
  58.      * If such time systems are used, it must be initialized before parsing by calling {@link
  59.      * #withMissionReferenceDate(AbsoluteDate)}.
  60.      * </p>
  61.      * <p>
  62.      * The gravitational coefficient is not set here. If it is needed in order
  63.      * to parse Cartesian orbits where the value is not set in the CCSDS file, it must
  64.      * be initialized before parsing by calling {@link #withMu(double)}.
  65.      * </p>
  66.      * <p>
  67.      * The IERS conventions to use is not set here. If it is needed in order to
  68.      * parse some reference frames or UT1 time scale, it must be initialized before
  69.      * parsing by calling {@link #withConventions(IERSConventions)}.
  70.      * </p>
  71.      */
  72.     public OEMParser() {
  73.         this(AbsoluteDate.FUTURE_INFINITY, Double.NaN, null, true, 0, 0, "");
  74.     }

  75.     /** Complete constructor.
  76.      * @param missionReferenceDate reference date for Mission Elapsed Time or Mission Relative Time time systems
  77.      * @param mu gravitational coefficient
  78.      * @param conventions IERS Conventions
  79.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  80.      * @param launchYear launch year for TLEs
  81.      * @param launchNumber launch number for TLEs
  82.      * @param launchPiece piece of launch (from "A" to "ZZZ") for TLEs
  83.      */
  84.     private OEMParser(final AbsoluteDate missionReferenceDate, final double mu,
  85.                       final IERSConventions conventions, final boolean simpleEOP,
  86.                       final int launchYear, final int launchNumber, final String launchPiece) {
  87.         super(missionReferenceDate, mu, conventions, simpleEOP, launchYear, launchNumber, launchPiece);
  88.     }

  89.     /** {@inheritDoc} */
  90.     public OEMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  91.         return new OEMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  92.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  93.     }

  94.     /** {@inheritDoc} */
  95.     public OEMParser withMu(final double newMu) {
  96.         return new OEMParser(getMissionReferenceDate(), newMu, getConventions(), isSimpleEOP(),
  97.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  98.     }

  99.     /** {@inheritDoc} */
  100.     public OEMParser withConventions(final IERSConventions newConventions) {
  101.         return new OEMParser(getMissionReferenceDate(), getMu(), newConventions, isSimpleEOP(),
  102.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  103.     }

  104.     /** {@inheritDoc} */
  105.     public OEMParser withSimpleEOP(final boolean newSimpleEOP) {
  106.         return new OEMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  107.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece());
  108.     }

  109.     /** {@inheritDoc} */
  110.     public OEMParser withInternationalDesignator(final int newLaunchYear,
  111.                                                  final int newLaunchNumber,
  112.                                                  final String newLaunchPiece) {
  113.         return new OEMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  114.                              newLaunchYear, newLaunchNumber, newLaunchPiece);
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     public OEMFile parse(final String fileName) throws OrekitException {
  119.         return (OEMFile) super.parse(fileName);
  120.     }

  121.     /** {@inheritDoc} */
  122.     @Override
  123.     public OEMFile parse(final InputStream stream) throws OrekitException {
  124.         return (OEMFile) super.parse(stream);
  125.     }

  126.     /** {@inheritDoc} */
  127.     public OEMFile parse(final InputStream stream, final String fileName) throws OrekitException {
  128.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"))) {
  129.             return parse(reader, fileName);
  130.         } catch (IOException ioe) {
  131.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  132.         }
  133.     }

  134.     @Override
  135.     public OEMFile parse(final BufferedReader reader, final String fileName)
  136.             throws OrekitException {

  137.         try {

  138.             // initialize internal data structures
  139.             final ParseInfo pi = new ParseInfo();
  140.             pi.fileName = fileName;
  141.             final OEMFile file = pi.file;

  142.             // set the additional data that has been configured prior the parsing by the user.
  143.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  144.             pi.file.setMuSet(getMu());
  145.             pi.file.setConventions(getConventions());

  146.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  147.                 ++pi.lineNumber;
  148.                 if (line.trim().length() == 0) {
  149.                     continue;
  150.                 }
  151.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  152.                 if (pi.keyValue.getKeyword() == null) {
  153.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  154.                 }
  155.                 switch (pi.keyValue.getKeyword()) {
  156.                     case CCSDS_OEM_VERS:
  157.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  158.                         break;

  159.                     case META_START:
  160.                         file.addEphemeridesBlock();
  161.                         pi.lastEphemeridesBlock = file.getEphemeridesBlocks().get(file.getEphemeridesBlocks().size() - 1);
  162.                         pi.lastEphemeridesBlock.getMetaData().setLaunchYear(getLaunchYear());
  163.                         pi.lastEphemeridesBlock.getMetaData().setLaunchNumber(getLaunchNumber());
  164.                         pi.lastEphemeridesBlock.getMetaData().setLaunchPiece(getLaunchPiece());
  165.                         break;

  166.                     case START_TIME:
  167.                         pi.lastEphemeridesBlock.setStartTime(parseDate(pi.keyValue.getValue(),
  168.                                                                        pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  169.                         break;

  170.                     case USEABLE_START_TIME:
  171.                         pi.lastEphemeridesBlock.setUseableStartTime(parseDate(pi.keyValue.getValue(),
  172.                                                                               pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  173.                         break;

  174.                     case USEABLE_STOP_TIME:
  175.                         pi.lastEphemeridesBlock.setUseableStopTime(parseDate(pi.keyValue.getValue(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  176.                         break;

  177.                     case STOP_TIME:
  178.                         pi.lastEphemeridesBlock.setStopTime(parseDate(pi.keyValue.getValue(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  179.                         break;

  180.                     case INTERPOLATION:
  181.                         pi.lastEphemeridesBlock.setInterpolationMethod(pi.keyValue.getValue());
  182.                         break;

  183.                     case INTERPOLATION_DEGREE:
  184.                         pi.lastEphemeridesBlock.setInterpolationDegree(Integer .parseInt(pi.keyValue.getValue()));
  185.                         break;

  186.                     case META_STOP:
  187.                         file.setMuUsed();
  188.                         parseEphemeridesDataLines(reader, pi);
  189.                         break;

  190.                     case COVARIANCE_START:
  191.                         parseCovarianceDataLines(reader, pi);
  192.                         break;

  193.                     default:
  194.                         boolean parsed = false;
  195.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  196.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  197.                         if (pi.lastEphemeridesBlock != null) {
  198.                             parsed = parsed || parseMetaDataEntry(pi.keyValue,
  199.                                                                   pi.lastEphemeridesBlock.getMetaData(), pi.commentTmp);
  200.                             if (parsed && pi.keyValue.getKeyword() == Keyword.REF_FRAME_EPOCH) {
  201.                                 pi.lastEphemeridesBlock.setHasRefFrameEpoch(true);
  202.                             }
  203.                         }
  204.                         if (!parsed) {
  205.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  206.                         }
  207.                 }
  208.             }
  209.             file.checkTimeSystems();
  210.             return file;
  211.         } catch (IOException ioe) {
  212.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  213.         }
  214.     }

  215.     /**
  216.      * Parse an ephemeris data line and add its content to the ephemerides
  217.      * block.
  218.      *
  219.      * @param reader the reader
  220.      * @param pi the parser info
  221.      * @exception IOException if an error occurs while reading from the stream
  222.      * @exception OrekitException if a date cannot be parsed
  223.      */
  224.     private void parseEphemeridesDataLines(final BufferedReader reader,  final ParseInfo pi)
  225.         throws OrekitException, IOException {

  226.         for (String line = reader.readLine(); line != null; line = reader.readLine()) {

  227.             ++pi.lineNumber;
  228.             if (line.trim().length() > 0) {
  229.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  230.                 if (pi.keyValue.getKeyword() == null) {
  231.                     Scanner sc = null;
  232.                     try {
  233.                         sc = new Scanner(line);
  234.                         final AbsoluteDate date = parseDate(sc.next(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem());
  235.                         final Vector3D position = new Vector3D(Double.parseDouble(sc.next()) * 1000,
  236.                                                                Double.parseDouble(sc.next()) * 1000,
  237.                                                                Double.parseDouble(sc.next()) * 1000);
  238.                         final Vector3D velocity = new Vector3D(Double.parseDouble(sc.next()) * 1000,
  239.                                                                Double.parseDouble(sc.next()) * 1000,
  240.                                                                Double.parseDouble(sc.next()) * 1000);
  241.                         Vector3D acceleration = Vector3D.NaN;
  242.                         boolean hasAcceleration = false;
  243.                         if (sc.hasNext()) {
  244.                             acceleration = new Vector3D(Double.parseDouble(sc.next()) * 1000,
  245.                                                         Double.parseDouble(sc.next()) * 1000,
  246.                                                         Double.parseDouble(sc.next()) * 1000);
  247.                             hasAcceleration = true;
  248.                         }
  249.                         final TimeStampedPVCoordinates epDataLine;
  250.                         if (hasAcceleration) {
  251.                             epDataLine = new TimeStampedPVCoordinates(date, position, velocity, acceleration);
  252.                         } else {
  253.                             epDataLine = new TimeStampedPVCoordinates(date, position, velocity);
  254.                         }
  255.                         pi.lastEphemeridesBlock.getEphemeridesDataLines().add(epDataLine);
  256.                         pi.lastEphemeridesBlock.updateHasAcceleration(hasAcceleration);
  257.                     } catch (NumberFormatException nfe) {
  258.                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  259.                                                   pi.lineNumber, pi.fileName, line);
  260.                     } finally {
  261.                         if (sc != null) {
  262.                             sc.close();
  263.                         }
  264.                     }
  265.                 } else {
  266.                     switch (pi.keyValue.getKeyword()) {
  267.                         case META_START:
  268.                             pi.lastEphemeridesBlock.setEphemeridesDataLinesComment(pi.commentTmp);
  269.                             pi.commentTmp.clear();
  270.                             pi.lineNumber--;
  271.                             reader.reset();
  272.                             return;
  273.                         case COVARIANCE_START:
  274.                             pi.lastEphemeridesBlock.setEphemeridesDataLinesComment(pi.commentTmp);
  275.                             pi.commentTmp.clear();
  276.                             pi.lineNumber--;
  277.                             reader.reset();
  278.                             return;
  279.                         case COMMENT:
  280.                             pi.commentTmp.add(pi.keyValue.getValue());
  281.                             break;
  282.                         default :
  283.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  284.                     }
  285.                 }
  286.             }
  287.             reader.mark(300);

  288.         }
  289.     }

  290.     /**
  291.      * Parse the covariance data lines, create a set of CovarianceMatrix objects
  292.      * and add them in the covarianceMatrices list of the ephemerides block.
  293.      *
  294.      * @param reader the reader
  295.      * @param pi the parser info
  296.      * @throws IOException if an error occurs while reading from the stream
  297.      * @throws OrekitException if the frame cannot be retrieved
  298.      */
  299.     private void parseCovarianceDataLines(final BufferedReader reader, final ParseInfo pi)
  300.         throws IOException, OrekitException  {
  301.         int i = 0;
  302.         for (String line = reader.readLine(); line != null; line = reader.readLine()) {

  303.             ++pi.lineNumber;
  304.             if (line.trim().length() == 0) {
  305.                 continue;
  306.             }
  307.             pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  308.             if (pi.keyValue.getKeyword() == null) {
  309.                 final Scanner sc = new Scanner(line);
  310.                 for (int j = 0; j < i + 1; j++) {
  311.                     try {
  312.                         pi.lastMatrix.addToEntry(i, j, Double.parseDouble(sc.next()));
  313.                     } catch (NumberFormatException nfe) {
  314.                         sc.close();
  315.                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  316.                                                   pi.lineNumber, pi.fileName, line);
  317.                     }
  318.                     if (j != i) {
  319.                         pi.lastMatrix.addToEntry(j, i, pi.lastMatrix.getEntry(i, j));
  320.                     }
  321.                 }
  322.                 if (i == 5) {
  323.                     final OEMFile.CovarianceMatrix cm =
  324.                             new OEMFile.CovarianceMatrix(pi.epoch, pi.covRefLofType, pi.covRefFrame, pi.lastMatrix);
  325.                     pi.lastEphemeridesBlock.getCovarianceMatrices().add(cm);
  326.                 }
  327.                 i++;
  328.                 if (sc != null) {
  329.                     sc.close();
  330.                 }
  331.             } else {
  332.                 switch (pi.keyValue.getKeyword()) {
  333.                     case EPOCH :
  334.                         i                = 0;
  335.                         pi.covRefLofType = null;
  336.                         pi.covRefFrame   = null;
  337.                         pi.lastMatrix    = MatrixUtils.createRealMatrix(6, 6);
  338.                         pi.epoch         = parseDate(pi.keyValue.getValue(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem());
  339.                         break;
  340.                     case COV_REF_FRAME :
  341.                         final CCSDSFrame frame = parseCCSDSFrame(pi.keyValue.getValue());
  342.                         if (frame.isLof()) {
  343.                             pi.covRefLofType = frame.getLofType();
  344.                             pi.covRefFrame   = null;
  345.                         } else {
  346.                             pi.covRefLofType = null;
  347.                             pi.covRefFrame   = frame.getFrame(getConventions(), isSimpleEOP());
  348.                         }
  349.                         break;
  350.                     case COVARIANCE_STOP :
  351.                         return;
  352.                     default :
  353.                         throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  354.                 }
  355.             }
  356.         }
  357.     }

  358.     /** Private class used to stock OEM parsing info.
  359.      * @author sports
  360.      */
  361.     private static class ParseInfo {

  362.         /** Ephemerides block being parsed. */
  363.         private OEMFile.EphemeridesBlock lastEphemeridesBlock;

  364.         /** Name of the file. */
  365.         private String fileName;

  366.         /** Current line number. */
  367.         private int lineNumber;

  368.         /** OEM file being read. */
  369.         private OEMFile file;

  370.         /** Key value of the line being read. */
  371.         private KeyValue keyValue;

  372.         /** Stored epoch. */
  373.         private AbsoluteDate epoch;

  374.         /** Covariance reference type of Local Orbital Frame. */
  375.         private LOFType covRefLofType;

  376.         /** Covariance reference frame. */
  377.         private Frame covRefFrame;
  378.         /** Stored matrix. */
  379.         private RealMatrix lastMatrix;

  380.         /** Stored comments. */
  381.         private List<String> commentTmp;

  382.         /** Create a new {@link ParseInfo} object. */
  383.         protected ParseInfo() {
  384.             lineNumber = 0;
  385.             file = new OEMFile();
  386.             commentTmp = new ArrayList<String>();
  387.         }
  388.     }
  389. }