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

  25. import org.apache.commons.math3.exception.util.DummyLocalizable;
  26. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  27. import org.apache.commons.math3.linear.MatrixUtils;
  28. import org.apache.commons.math3.linear.RealMatrix;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.files.general.OrbitFileParser;
  32. import org.orekit.frames.Frame;
  33. import org.orekit.frames.LOFType;
  34. import org.orekit.orbits.CartesianOrbit;
  35. import org.orekit.time.AbsoluteDate;
  36. import org.orekit.utils.IERSConventions;
  37. import org.orekit.utils.PVCoordinates;

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

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

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

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

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

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

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

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

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

  120.     /** {@inheritDoc} */
  121.     public OEMFile parse(final InputStream stream, final String fileName) throws OrekitException {

  122.         try {

  123.             final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
  124.             // initialize internal data structures
  125.             final ParseInfo pi = new ParseInfo();
  126.             pi.fileName = fileName;
  127.             final OEMFile file = pi.file;

  128.             // set the additional data that has been configured prior the parsing by the user.
  129.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  130.             pi.file.setMuSet(getMu());
  131.             pi.file.setConventions(getConventions());

  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_OEM_VERS:
  143.                     file.setFormatVersion(pi.keyValue.getDoubleValue());
  144.                     break;

  145.                 case META_START:
  146.                     file.addEphemeridesBlock();
  147.                     pi.lastEphemeridesBlock = file.getEphemeridesBlocks().get(file.getEphemeridesBlocks().size() - 1);
  148.                     pi.lastEphemeridesBlock.getMetaData().setLaunchYear(getLaunchYear());
  149.                     pi.lastEphemeridesBlock.getMetaData().setLaunchNumber(getLaunchNumber());
  150.                     pi.lastEphemeridesBlock.getMetaData().setLaunchPiece(getLaunchPiece());
  151.                     break;

  152.                 case START_TIME:
  153.                     pi.lastEphemeridesBlock.setStartTime(parseDate(pi.keyValue.getValue(),
  154.                                                                    pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  155.                     break;

  156.                 case USEABLE_START_TIME:
  157.                     pi.lastEphemeridesBlock.setUseableStartTime(parseDate(pi.keyValue.getValue(),
  158.                                                                           pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  159.                     break;

  160.                 case USEABLE_STOP_TIME:
  161.                     pi.lastEphemeridesBlock.setUseableStopTime(parseDate(pi.keyValue.getValue(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  162.                     break;

  163.                 case STOP_TIME:
  164.                     pi.lastEphemeridesBlock.setStopTime(parseDate(pi.keyValue.getValue(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem()));
  165.                     break;

  166.                 case INTERPOLATION:
  167.                     pi.lastEphemeridesBlock.setInterpolationMethod(pi.keyValue.getValue());
  168.                     break;

  169.                 case INTERPOLATION_DEGREE:
  170.                     pi.lastEphemeridesBlock.setInterpolationDegree(Integer .parseInt(pi.keyValue.getValue()));
  171.                     break;

  172.                 case META_STOP:
  173.                     file.setMuUsed();
  174.                     parseEphemeridesDataLines(reader, pi);
  175.                     break;

  176.                 case COVARIANCE_START:
  177.                     parseCovarianceDataLines(reader, pi);
  178.                     break;

  179.                 default:
  180.                     boolean parsed = false;
  181.                     parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  182.                     parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  183.                     if (pi.lastEphemeridesBlock != null) {
  184.                         parsed = parsed || parseMetaDataEntry(pi.keyValue,
  185.                                                               pi.lastEphemeridesBlock.getMetaData(), pi.commentTmp);
  186.                     }
  187.                     if (!parsed) {
  188.                         throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  189.                     }
  190.                 }
  191.             }
  192.             file.checkTimeSystems();
  193.             return file;
  194.         } catch (IOException ioe) {
  195.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  196.         }
  197.     }

  198.     /**
  199.      * Parse an ephemeris data line and add its content to the ephemerides
  200.      * block.
  201.      *
  202.      * @param reader the reader
  203.      * @param pi the parser info
  204.      * @exception IOException if an error occurs while reading from the stream
  205.      * @exception OrekitException if a date cannot be parsed
  206.      */
  207.     private void parseEphemeridesDataLines(final BufferedReader reader,  final ParseInfo pi)
  208.         throws OrekitException, IOException {

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

  210.             ++pi.lineNumber;
  211.             if (line.trim().length() > 0) {
  212.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  213.                 if (pi.keyValue.getKeyword() == null) {
  214.                     try {
  215.                         final Scanner sc = new Scanner(line);
  216.                         final AbsoluteDate date = parseDate(sc.next(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem());
  217.                         final Vector3D position = new Vector3D(Double.parseDouble(sc.next()) * 1000,
  218.                                                                Double.parseDouble(sc.next()) * 1000,
  219.                                                                Double.parseDouble(sc.next()) * 1000);
  220.                         final Vector3D velocity = new Vector3D(Double.parseDouble(sc.next()) * 1000,
  221.                                                                Double.parseDouble(sc.next()) * 1000,
  222.                                                                Double.parseDouble(sc.next()) * 1000);
  223.                         final CartesianOrbit orbit =
  224.                                 new CartesianOrbit(new PVCoordinates(position, velocity),
  225.                                                    pi.lastEphemeridesBlock.getMetaData().getFrame(),
  226.                                                    date, pi.file.getMuUsed());
  227.                         Vector3D acceleration = null;
  228.                         if (sc.hasNext()) {
  229.                             acceleration = new Vector3D(Double.parseDouble(sc.next()) * 1000,
  230.                                                         Double.parseDouble(sc.next()) * 1000,
  231.                                                         Double.parseDouble(sc.next()) * 1000);
  232.                         }
  233.                         final OEMFile.EphemeridesDataLine epDataLine =
  234.                                 new OEMFile.EphemeridesDataLine(orbit, acceleration);
  235.                         pi.lastEphemeridesBlock.getEphemeridesDataLines().add(epDataLine);
  236.                     } catch (NumberFormatException nfe) {
  237.                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  238.                                                   pi.lineNumber, pi.fileName, line);
  239.                     }
  240.                 } else {
  241.                     switch (pi.keyValue.getKeyword()) {
  242.                     case META_START:
  243.                         pi.lastEphemeridesBlock.setEphemeridesDataLinesComment(pi.commentTmp);
  244.                         pi.commentTmp.clear();
  245.                         pi.lineNumber--;
  246.                         reader.reset();
  247.                         return;
  248.                     case COVARIANCE_START:
  249.                         pi.lastEphemeridesBlock.setEphemeridesDataLinesComment(pi.commentTmp);
  250.                         pi.commentTmp.clear();
  251.                         pi.lineNumber--;
  252.                         reader.reset();
  253.                         return;
  254.                     case COMMENT:
  255.                         pi.commentTmp.add(pi.keyValue.getValue());
  256.                         break;
  257.                     default :
  258.                         throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  259.                     }
  260.                 }
  261.             }
  262.             reader.mark(300);

  263.         }
  264.     }

  265.     /**
  266.      * Parse the covariance data lines, create a set of CovarianceMatrix objects
  267.      * and add them in the covarianceMatrices list of the ephemerides block.
  268.      *
  269.      * @param reader the reader
  270.      * @param pi the parser info
  271.      * @throws IOException if an error occurs while reading from the stream
  272.      * @throws OrekitException if the frame cannot be retrieved
  273.      */
  274.     private void parseCovarianceDataLines(final BufferedReader reader, final ParseInfo pi)
  275.         throws IOException, OrekitException  {
  276.         int i = 0;
  277.         for (String line = reader.readLine(); line != null; line = reader.readLine()) {

  278.             ++pi.lineNumber;
  279.             if (line.trim().length() == 0) {
  280.                 continue;
  281.             }
  282.             pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  283.             if (pi.keyValue.getKeyword() == null) {
  284.                 final Scanner sc = new Scanner(line);
  285.                 for (int j = 0; j < i + 1; j++) {
  286.                     try {
  287.                         pi.lastMatrix.addToEntry(i, j, Double.parseDouble(sc.next()));
  288.                     } catch (NumberFormatException nfe) {
  289.                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  290.                                                   pi.lineNumber, pi.fileName, line);
  291.                     }
  292.                     if (j != i) {
  293.                         pi.lastMatrix.addToEntry(j, i, pi.lastMatrix.getEntry(i, j));
  294.                     }
  295.                 }
  296.                 if (i == 5) {
  297.                     final OEMFile.CovarianceMatrix cm =
  298.                             new OEMFile.CovarianceMatrix(pi.epoch, pi.covRefLofType, pi.covRefFrame, pi.lastMatrix);
  299.                     pi.lastEphemeridesBlock.getCovarianceMatrices().add(cm);
  300.                 }
  301.                 i++;
  302.             } else {
  303.                 switch (pi.keyValue.getKeyword()) {
  304.                 case EPOCH :
  305.                     i                = 0;
  306.                     pi.covRefLofType = null;
  307.                     pi.covRefFrame   = null;
  308.                     pi.lastMatrix    = MatrixUtils.createRealMatrix(6, 6);
  309.                     pi.epoch         = parseDate(pi.keyValue.getValue(), pi.lastEphemeridesBlock.getMetaData().getTimeSystem());
  310.                     break;
  311.                 case COV_REF_FRAME :
  312.                     final CCSDSFrame frame = parseCCSDSFrame(pi.keyValue.getValue());
  313.                     if (frame.isLof()) {
  314.                         pi.covRefLofType = frame.getLofType();
  315.                         pi.covRefFrame   = null;
  316.                     } else {
  317.                         pi.covRefLofType = null;
  318.                         pi.covRefFrame   = frame.getFrame(getConventions(), isSimpleEOP());
  319.                     }
  320.                     break;
  321.                 case COVARIANCE_STOP :
  322.                     return;
  323.                 default :
  324.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  325.                 }
  326.             }
  327.         }
  328.     }

  329.     /** Private class used to stock OEM parsing info.
  330.      * @author sports
  331.      */
  332.     private static class ParseInfo {

  333.         /** Ephemerides block being parsed. */
  334.         private OEMFile.EphemeridesBlock lastEphemeridesBlock;

  335.         /** Name of the file. */
  336.         private String fileName;

  337.         /** Current line number. */
  338.         private int lineNumber;

  339.         /** OEM file being read. */
  340.         private OEMFile file;

  341.         /** Key value of the line being read. */
  342.         private KeyValue keyValue;

  343.         /** Stored epoch. */
  344.         private AbsoluteDate epoch;

  345.         /** Covariance reference type of Local Orbital Frame. */
  346.         private LOFType covRefLofType;

  347.         /** Covariance reference frame. */
  348.         private Frame covRefFrame;
  349.         /** Stored matrix. */
  350.         private RealMatrix lastMatrix;

  351.         /** Stored comments. */
  352.         private List<String> commentTmp;

  353.         /** Create a new {@link ParseInfo} object. */
  354.         protected ParseInfo() {
  355.             lineNumber = 0;
  356.             file = new OEMFile();
  357.             commentTmp = new ArrayList<String>();
  358.         }
  359.     }
  360. }