APMParser.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.complex.Quaternion;
  26. import org.hipparchus.exception.DummyLocalizable;
  27. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  28. import org.orekit.annotation.DefaultDataContext;
  29. import org.orekit.data.DataContext;
  30. import org.orekit.errors.OrekitException;
  31. import org.orekit.errors.OrekitMessages;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.IERSConventions;

  34. /**
  35.  * A parser for the CCSDS APM (Attitude Parameter Message).
  36.  * @author Bryan Cazabonne
  37.  * @since 10.2
  38.  */
  39. public class APMParser extends ADMParser {

  40.     /** Simple constructor.
  41.      * <p>
  42.      * This class is immutable, and hence thread safe. When parts
  43.      * must be changed, such as reference date for Mission Elapsed Time or
  44.      * Mission Relative Time time systems, or the gravitational coefficient or
  45.      * the IERS conventions, the various {@code withXxx} methods must be called,
  46.      * which create a new immutable instance with the new parameters. This
  47.      * is a combination of the
  48.      * <a href="https://en.wikipedia.org/wiki/Builder_pattern">builder design
  49.      * pattern</a> and a
  50.      * <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
  51.      * interface</a>.
  52.      * </p>
  53.      * <p>
  54.      * The initial date for Mission Elapsed Time and Mission Relative Time time systems is not set here.
  55.      * If such time systems are used, it must be initialized before parsing by calling {@link
  56.      * #withMissionReferenceDate(AbsoluteDate)}.
  57.      * </p>
  58.      *
  59.      * <p>This method uses the {@link DataContext#getDefault() default data context}. See
  60.      * {@link #withDataContext(DataContext)}.
  61.      */
  62.     @DefaultDataContext
  63.     public APMParser() {
  64.         this(DataContext.getDefault());
  65.     }

  66.     /** Constructor with data context.
  67.      * <p>
  68.      * This class is immutable, and hence thread safe. When parts
  69.      * must be changed, such as reference date for Mission Elapsed Time or
  70.      * Mission Relative Time time systems, or the gravitational coefficient or
  71.      * the IERS conventions, the various {@code withXxx} methods must be called,
  72.      * which create a new immutable instance with the new parameters. This
  73.      * is a combination of the
  74.      * <a href="https://en.wikipedia.org/wiki/Builder_pattern">builder design
  75.      * pattern</a> and a
  76.      * <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
  77.      * interface</a>.
  78.      * </p>
  79.      * <p>
  80.      * The initial date for Mission Elapsed Time and Mission Relative Time time systems is not set here.
  81.      * If such time systems are used, it must be initialized before parsing by calling {@link
  82.      * #withMissionReferenceDate(AbsoluteDate)}.
  83.      * </p>
  84.      *
  85.      * @param dataContext used by the parser.
  86.      *
  87.      * @see #APMParser()
  88.      * @see #withDataContext(DataContext)
  89.      */
  90.     public APMParser(final DataContext dataContext) {
  91.         this(AbsoluteDate.FUTURE_INFINITY, Double.NaN, null, true, 0, 0, "", dataContext);
  92.     }

  93.     /** Complete constructor.
  94.      * @param missionReferenceDate reference date for Mission Elapsed Time or Mission Relative Time time systems
  95.      * @param mu gravitational coefficient
  96.      * @param conventions IERS Conventions
  97.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  98.      * @param launchYear launch year for TLEs
  99.      * @param launchNumber launch number for TLEs
  100.      * @param launchPiece piece of launch (from "A" to "ZZZ") for TLEs
  101.      * @param dataContext used to retrieve frames, time scales, etc.
  102.      */
  103.     private APMParser(final AbsoluteDate missionReferenceDate, final double mu,
  104.                       final IERSConventions conventions, final boolean simpleEOP,
  105.                       final int launchYear, final int launchNumber,
  106.                       final String launchPiece, final DataContext dataContext) {
  107.         super(missionReferenceDate, mu, conventions, simpleEOP, launchYear, launchNumber,
  108.                 launchPiece, dataContext);
  109.     }

  110.     /** {@inheritDoc} */
  111.     public APMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  112.         return new APMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  113.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  114.                              getDataContext());
  115.     }

  116.     /** {@inheritDoc} */
  117.     public APMParser withMu(final double newMu) {
  118.         return new APMParser(getMissionReferenceDate(), newMu, getConventions(), isSimpleEOP(),
  119.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  120.                              getDataContext());
  121.     }

  122.     /** {@inheritDoc} */
  123.     public APMParser withConventions(final IERSConventions newConventions) {
  124.         return new APMParser(getMissionReferenceDate(), getMu(), newConventions, isSimpleEOP(),
  125.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  126.                              getDataContext());
  127.     }

  128.     /** {@inheritDoc} */
  129.     public APMParser withSimpleEOP(final boolean newSimpleEOP) {
  130.         return new APMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  131.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  132.                              getDataContext());
  133.     }

  134.     /** {@inheritDoc} */
  135.     public APMParser withInternationalDesignator(final int newLaunchYear,
  136.                                                  final int newLaunchNumber,
  137.                                                  final String newLaunchPiece) {
  138.         return new APMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  139.                              newLaunchYear, newLaunchNumber, newLaunchPiece,
  140.                              getDataContext());
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public APMParser withDataContext(final DataContext dataContext) {
  145.         return new APMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  146.                 getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  147.                 dataContext);
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     public APMFile parse(final String fileName) {
  152.         return (APMFile) super.parse(fileName);
  153.     }

  154.     /** {@inheritDoc} */
  155.     @Override
  156.     public APMFile parse(final InputStream stream) {
  157.         return (APMFile) super.parse(stream);
  158.     }

  159.     /** {@inheritDoc} */
  160.     public APMFile parse(final InputStream stream, final String fileName) {
  161.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {

  162.             // initialize internal data structures
  163.             final ParseInfo pi = new ParseInfo();
  164.             pi.fileName = fileName;
  165.             final APMFile file = pi.file;

  166.             // set the additional data that has been configured prior the parsing by the user.
  167.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  168.             pi.file.setMu(getMu());
  169.             pi.file.setConventions(getConventions());
  170.             pi.file.setDataContext(getDataContext());
  171.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  172.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  173.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  174.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  175.                 ++pi.lineNumber;
  176.                 if (line.trim().length() == 0) {
  177.                     // Blank line
  178.                     continue;
  179.                 }
  180.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  181.                 if (pi.keyValue.getKeyword() == null) {
  182.                     // Unexpected keyword. An exception is thrown.
  183.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  184.                 }

  185.                 switch (pi.keyValue.getKeyword()) {

  186.                     case CCSDS_APM_VERS:
  187.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  188.                         break;

  189.                     case Q_FRAME_A:
  190.                         file.setQuaternionFrameAString(pi.keyValue.getValue());
  191.                         break;

  192.                     case Q_FRAME_B:
  193.                         file.setQuaternionFrameBString(pi.keyValue.getValue());
  194.                         break;

  195.                     case Q_DIR:
  196.                         file.setAttitudeQuaternionDirection(pi.keyValue.getValue());
  197.                         break;

  198.                     case QC:
  199.                         pi.q0 = pi.keyValue.getDoubleValue();
  200.                         break;

  201.                     case Q1:
  202.                         pi.q1 = pi.keyValue.getDoubleValue();
  203.                         break;

  204.                     case Q2:
  205.                         pi.q2 = pi.keyValue.getDoubleValue();
  206.                         break;

  207.                     case Q3:
  208.                         pi.q3 = pi.keyValue.getDoubleValue();
  209.                         break;

  210.                     case MAN_EPOCH_START:
  211.                         if (pi.maneuver != null) {
  212.                             file.addManeuver(pi.maneuver);
  213.                         }
  214.                         pi.maneuver = new APMFile.APMManeuver();
  215.                         pi.maneuver.setEpochStart(parseDate(pi.keyValue.getValue(), file.getMetaData().getTimeSystem()));
  216.                         if (!pi.commentTmp.isEmpty()) {
  217.                             pi.maneuver.setComment(pi.commentTmp);
  218.                             pi.commentTmp.clear();
  219.                         }
  220.                         break;

  221.                     case MAN_DURATION:
  222.                         pi.maneuver.setDuration(pi.keyValue.getDoubleValue());
  223.                         break;

  224.                     case MAN_REF_FRAME:
  225.                         pi.maneuver.setRefFrameString(pi.keyValue.getValue());
  226.                         break;

  227.                     case MAN_TOR_1:
  228.                         pi.maneuver.setTorque(new Vector3D(pi.keyValue.getDoubleValue(),
  229.                                                            pi.maneuver.getTorque().getY(),
  230.                                                            pi.maneuver.getTorque().getZ()));
  231.                         break;

  232.                     case MAN_TOR_2:
  233.                         pi.maneuver.setTorque(new Vector3D(pi.maneuver.getTorque().getX(),
  234.                                                            pi.keyValue.getDoubleValue(),
  235.                                                            pi.maneuver.getTorque().getZ()));
  236.                         break;

  237.                     case MAN_TOR_3:
  238.                         pi.maneuver.setTorque(new Vector3D(pi.maneuver.getTorque().getX(),
  239.                                                            pi.maneuver.getTorque().getY(),
  240.                                                            pi.keyValue.getDoubleValue()));
  241.                         break;

  242.                     default:
  243.                         boolean parsed = false;
  244.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  245.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  246.                         parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  247.                         parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  248.                         if (!parsed) {
  249.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  250.                         }

  251.                 }

  252.             }

  253.             file.setQuaternion(new Quaternion(pi.q0, pi.q1, pi.q2, pi.q3));
  254.             if (pi.maneuver != null) {
  255.                 file.addManeuver(pi.maneuver);
  256.             }
  257.             return file;

  258.         } catch (IOException ioe) {
  259.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  260.         }
  261.     }

  262.     /** Private class used to stock APM parsing info. */
  263.     private static class ParseInfo {

  264.         /** APM file being read. */
  265.         private APMFile file;

  266.         /** Name of the file. */
  267.         private String fileName;

  268.         /** Current line number. */
  269.         private int lineNumber;

  270.         /** Key value of the line being read. */
  271.         private KeyValue keyValue;

  272.         /** Stored comments. */
  273.         private List<String> commentTmp;

  274.         /** Scalar coordinate of the quaternion. */
  275.         private double q0;

  276.         /** First component of the quaternion vector. */
  277.         private double q1;

  278.         /** Second component of the quaternion vector.. */
  279.         private double q2;

  280.         /** Third component of the quaternion vector.. */
  281.         private double q3;

  282.         /** Current maneuver. */
  283.         private APMFile.APMManeuver maneuver;

  284.         /** Create a new {@link ParseInfo} object. */
  285.         protected ParseInfo() {
  286.             file       = new APMFile();
  287.             lineNumber = 0;
  288.             commentTmp = new ArrayList<String>();
  289.             maneuver   = null;
  290.         }
  291.     }

  292. }