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.     @Override
  112.     public APMParser withMissionReferenceDate(final AbsoluteDate newMissionReferenceDate) {
  113.         return new APMParser(newMissionReferenceDate, getMu(), getConventions(), isSimpleEOP(),
  114.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  115.                              getDataContext());
  116.     }

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

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

  131.     /** {@inheritDoc} */
  132.     @Override
  133.     public APMParser withSimpleEOP(final boolean newSimpleEOP) {
  134.         return new APMParser(getMissionReferenceDate(), getMu(), getConventions(), newSimpleEOP,
  135.                              getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  136.                              getDataContext());
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     public APMParser withInternationalDesignator(final int newLaunchYear,
  141.                                                  final int newLaunchNumber,
  142.                                                  final String newLaunchPiece) {
  143.         return new APMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  144.                              newLaunchYear, newLaunchNumber, newLaunchPiece,
  145.                              getDataContext());
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public APMParser withDataContext(final DataContext dataContext) {
  150.         return new APMParser(getMissionReferenceDate(), getMu(), getConventions(), isSimpleEOP(),
  151.                 getLaunchYear(), getLaunchNumber(), getLaunchPiece(),
  152.                 dataContext);
  153.     }

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

  159.     /** {@inheritDoc} */
  160.     @Override
  161.     public APMFile parse(final InputStream stream) {
  162.         return (APMFile) super.parse(stream);
  163.     }

  164.     /** {@inheritDoc} */
  165.     @Override
  166.     public APMFile parse(final InputStream stream, final String fileName) {
  167.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {

  168.             // initialize internal data structures
  169.             final ParseInfo pi = new ParseInfo();
  170.             pi.fileName = fileName;
  171.             final APMFile file = pi.file;

  172.             // set the additional data that has been configured prior the parsing by the user.
  173.             pi.file.setMissionReferenceDate(getMissionReferenceDate());
  174.             pi.file.setMu(getMu());
  175.             pi.file.setConventions(getConventions());
  176.             pi.file.setDataContext(getDataContext());
  177.             pi.file.getMetaData().setLaunchYear(getLaunchYear());
  178.             pi.file.getMetaData().setLaunchNumber(getLaunchNumber());
  179.             pi.file.getMetaData().setLaunchPiece(getLaunchPiece());

  180.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  181.                 ++pi.lineNumber;
  182.                 if (line.trim().length() == 0) {
  183.                     // Blank line
  184.                     continue;
  185.                 }
  186.                 pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
  187.                 if (pi.keyValue.getKeyword() == null) {
  188.                     // Unexpected keyword. An exception is thrown.
  189.                     throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  190.                 }

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

  192.                     case CCSDS_APM_VERS:
  193.                         file.setFormatVersion(pi.keyValue.getDoubleValue());
  194.                         break;

  195.                     case Q_FRAME_A:
  196.                         file.setQuaternionFrameAString(pi.keyValue.getValue());
  197.                         break;

  198.                     case Q_FRAME_B:
  199.                         file.setQuaternionFrameBString(pi.keyValue.getValue());
  200.                         break;

  201.                     case Q_DIR:
  202.                         file.setAttitudeQuaternionDirection(pi.keyValue.getValue());
  203.                         break;

  204.                     case QC:
  205.                         pi.q0 = pi.keyValue.getDoubleValue();
  206.                         break;

  207.                     case Q1:
  208.                         pi.q1 = pi.keyValue.getDoubleValue();
  209.                         break;

  210.                     case Q2:
  211.                         pi.q2 = pi.keyValue.getDoubleValue();
  212.                         break;

  213.                     case Q3:
  214.                         pi.q3 = pi.keyValue.getDoubleValue();
  215.                         break;

  216.                     case MAN_EPOCH_START:
  217.                         if (pi.maneuver != null) {
  218.                             file.addManeuver(pi.maneuver);
  219.                         }
  220.                         pi.maneuver = new APMFile.APMManeuver();
  221.                         pi.maneuver.setEpochStart(parseDate(pi.keyValue.getValue(), file.getMetaData().getTimeSystem()));
  222.                         if (!pi.commentTmp.isEmpty()) {
  223.                             pi.maneuver.setComment(pi.commentTmp);
  224.                             pi.commentTmp.clear();
  225.                         }
  226.                         break;

  227.                     case MAN_DURATION:
  228.                         pi.maneuver.setDuration(pi.keyValue.getDoubleValue());
  229.                         break;

  230.                     case MAN_REF_FRAME:
  231.                         pi.maneuver.setRefFrameString(pi.keyValue.getValue());
  232.                         break;

  233.                     case MAN_TOR_1:
  234.                         pi.maneuver.setTorque(new Vector3D(pi.keyValue.getDoubleValue(),
  235.                                                            pi.maneuver.getTorque().getY(),
  236.                                                            pi.maneuver.getTorque().getZ()));
  237.                         break;

  238.                     case MAN_TOR_2:
  239.                         pi.maneuver.setTorque(new Vector3D(pi.maneuver.getTorque().getX(),
  240.                                                            pi.keyValue.getDoubleValue(),
  241.                                                            pi.maneuver.getTorque().getZ()));
  242.                         break;

  243.                     case MAN_TOR_3:
  244.                         pi.maneuver.setTorque(new Vector3D(pi.maneuver.getTorque().getX(),
  245.                                                            pi.maneuver.getTorque().getY(),
  246.                                                            pi.keyValue.getDoubleValue()));
  247.                         break;

  248.                     default:
  249.                         boolean parsed = false;
  250.                         parsed = parsed || parseComment(pi.keyValue, pi.commentTmp);
  251.                         parsed = parsed || parseHeaderEntry(pi.keyValue, file, pi.commentTmp);
  252.                         parsed = parsed || parseMetaDataEntry(pi.keyValue, file.getMetaData(), pi.commentTmp);
  253.                         parsed = parsed || parseGeneralStateDataEntry(pi.keyValue, file, pi.commentTmp);
  254.                         if (!parsed) {
  255.                             throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber, pi.fileName, line);
  256.                         }

  257.                 }

  258.             }

  259.             file.setQuaternion(new Quaternion(pi.q0, pi.q1, pi.q2, pi.q3));
  260.             if (pi.maneuver != null) {
  261.                 file.addManeuver(pi.maneuver);
  262.             }
  263.             return file;

  264.         } catch (IOException ioe) {
  265.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  266.         }
  267.     }

  268.     /** Private class used to stock APM parsing info. */
  269.     private static class ParseInfo {

  270.         /** APM file being read. */
  271.         private APMFile file;

  272.         /** Name of the file. */
  273.         private String fileName;

  274.         /** Current line number. */
  275.         private int lineNumber;

  276.         /** Key value of the line being read. */
  277.         private KeyValue keyValue;

  278.         /** Stored comments. */
  279.         private List<String> commentTmp;

  280.         /** Scalar coordinate of the quaternion. */
  281.         private double q0;

  282.         /** First component of the quaternion vector. */
  283.         private double q1;

  284.         /** Second component of the quaternion vector.. */
  285.         private double q2;

  286.         /** Third component of the quaternion vector.. */
  287.         private double q3;

  288.         /** Current maneuver. */
  289.         private APMFile.APMManeuver maneuver;

  290.         /** Create a new {@link ParseInfo} object. */
  291.         protected ParseInfo() {
  292.             file       = new APMFile();
  293.             lineNumber = 0;
  294.             commentTmp = new ArrayList<String>();
  295.             maneuver   = null;
  296.         }
  297.     }

  298. }