AdmParser.java

  1. /* Copyright 2002-2022 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.ndm.adm;

  18. import java.util.Map;

  19. import org.hipparchus.geometry.euclidean.threed.RotationOrder;
  20. import org.orekit.data.DataContext;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.files.ccsds.ndm.NdmConstituent;
  24. import org.orekit.files.ccsds.ndm.ParsedUnitsBehavior;
  25. import org.orekit.files.ccsds.utils.lexical.ParseToken;
  26. import org.orekit.files.ccsds.utils.lexical.TokenType;
  27. import org.orekit.files.ccsds.utils.lexical.XmlTokenBuilder;
  28. import org.orekit.files.ccsds.utils.parsing.AbstractConstituentParser;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.IERSConventions;

  31. /**
  32.  * Base class for Attitude Data Message parsers.
  33.  * <p>
  34.  * Note than starting with Orekit 11.0, CCSDS message parsers are
  35.  * mutable objects that gather the data being parsed, until the
  36.  * message is complete and the {@link #parseMessage(org.orekit.data.DataSource)
  37.  * parseMessage} method has returned. This implies that parsers
  38.  * should <em>not</em> be used in a multi-thread context. The recommended
  39.  * way to use parsers is to either dedicate one parser for each message
  40.  * and drop it afterwards, or to use a single-thread loop.
  41.  * </p>
  42.  * @param <T> type of the file
  43.  * @param <P> type of the parser
  44.  * @author Luc Maisonobe
  45.  * @since 11.0
  46.  */
  47. public abstract class AdmParser<T extends NdmConstituent<?, ?>, P extends AbstractConstituentParser<T, ?>>
  48.     extends AbstractConstituentParser<T, P> {

  49.     /** Index rotation element name. */
  50.     private static final String ROTATION_1 = "rotation1";

  51.     /** Index rotation element name. */
  52.     private static final String ROTATION_2 = "rotation2";

  53.     /** Index rotation element name. */
  54.     private static final String ROTATION_3 = "rotation3";

  55.     /** Reference date for Mission Elapsed Time or Mission Relative Time time systems. */
  56.     private final AbsoluteDate missionReferenceDate;

  57.     /** Complete constructor.
  58.      * @param root root element for XML files
  59.      * @param formatVersionKey key for format version
  60.      * @param conventions IERS Conventions
  61.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  62.      * @param dataContext used to retrieve frames, time scales, etc.
  63.      * @param missionReferenceDate reference date for Mission Elapsed Time or Mission Relative Time time systems
  64.      * (may be null if time system is absolute)
  65.      * @param parsedUnitsBehavior behavior to adopt for handling parsed units
  66.      */
  67.     protected AdmParser(final String root, final String formatVersionKey, final IERSConventions conventions,
  68.                         final boolean simpleEOP, final DataContext dataContext,
  69.                         final AbsoluteDate missionReferenceDate, final ParsedUnitsBehavior parsedUnitsBehavior) {
  70.         super(root, formatVersionKey, conventions, simpleEOP, dataContext, parsedUnitsBehavior);
  71.         this.missionReferenceDate = missionReferenceDate;
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public Map<String, XmlTokenBuilder> getSpecialXmlElementsBuilders() {

  76.         final Map<String, XmlTokenBuilder> builders = super.getSpecialXmlElementsBuilders();

  77.         // special handling of rotation elements
  78.         builders.put(ROTATION_1, new RotationXmlTokenBuilder());
  79.         builders.put(ROTATION_2, new RotationXmlTokenBuilder());
  80.         builders.put(ROTATION_3, new RotationXmlTokenBuilder());

  81.         return builders;

  82.     }

  83.     /**
  84.      * Get reference date for Mission Elapsed Time and Mission Relative Time time systems.
  85.      * @return the reference date
  86.      */
  87.     public AbsoluteDate getMissionReferenceDate() {
  88.         return missionReferenceDate;
  89.     }

  90.     /** Process a CCSDS Euler angles sequence as a {@link RotationOrder}.
  91.      * @param sequence Euler angles sequence token
  92.      * @param consumer consumer of the rotation order
  93.      * @return always return {@code true}
  94.      */
  95.     public static boolean processRotationOrder(final ParseToken sequence,
  96.                                                final RotationOrderConsumer consumer) {
  97.         if (sequence.getType() == TokenType.ENTRY) {
  98.             try {
  99.                 consumer.accept(RotationOrder.valueOf(sequence.getContentAsUppercaseString().
  100.                                                       replace('1', 'X').
  101.                                                       replace('2', 'Y').
  102.                                                       replace('3', 'Z')));
  103.             } catch (IllegalArgumentException iae) {
  104.                 throw new OrekitException(OrekitMessages.CCSDS_INVALID_ROTATION_SEQUENCE,
  105.                                           sequence.getContentAsUppercaseString(),
  106.                                           sequence.getLineNumber(), sequence.getFileName());
  107.             }
  108.         }
  109.         return true;
  110.     }

  111.     /** Interface representing instance methods that consume otation order values. */
  112.     public interface RotationOrderConsumer {
  113.         /** Consume a data.
  114.          * @param value value to consume
  115.          */
  116.         void accept(RotationOrder value);
  117.     }

  118. }