ApmQuaternionWriter.java

  1. /* Copyright 2002-2024 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.apm;

  18. import java.io.IOException;

  19. import org.hipparchus.complex.Quaternion;
  20. import org.orekit.files.ccsds.definitions.TimeConverter;
  21. import org.orekit.files.ccsds.ndm.adm.AttitudeEndpoints;
  22. import org.orekit.files.ccsds.section.AbstractWriter;
  23. import org.orekit.files.ccsds.utils.FileFormat;
  24. import org.orekit.files.ccsds.utils.generation.Generator;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.utils.units.Unit;

  27. /** Writer for quaternion data.
  28.  * @author Luc Maisonobe
  29.  * @since 11.0
  30.  */
  31. class ApmQuaternionWriter extends AbstractWriter {

  32.     /** Format version.
  33.      * @since 12.0
  34.      */
  35.     private final double formatVersion;

  36.     /** Quaternion block. */
  37.     private final ApmQuaternion quaternion;

  38.     /** Quaternion epoch. */
  39.     private final AbsoluteDate epoch;

  40.     /** Converter for dates. */
  41.     private final TimeConverter timeConverter;

  42.     /** Create a writer.
  43.      * @param formatVersion format version
  44.      * @param xmlTag name of the XML tag surrounding the section
  45.      * @param kvnTag name of the KVN tag surrounding the section (may be null)
  46.      * @param quaternion quaternion to write
  47.      * @param epoch quaternion epoch (only for ADM V1)
  48.      * @param timeConverter converter for dates
  49.      */
  50.     ApmQuaternionWriter(final double formatVersion, final String xmlTag, final String kvnTag,
  51.                         final ApmQuaternion quaternion,
  52.                         final AbsoluteDate epoch, final TimeConverter timeConverter) {
  53.         super(xmlTag, kvnTag);
  54.         this.formatVersion = formatVersion;
  55.         this.quaternion    = quaternion;
  56.         this.epoch         = epoch;
  57.         this.timeConverter = timeConverter;
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     protected void writeContent(final Generator generator) throws IOException {

  62.         generator.writeComments(quaternion.getComments());

  63.         if (epoch != null) {
  64.             // epoch is in the quaternion block only in ADM V1
  65.             generator.writeEntry(ApmQuaternionKey.EPOCH.name(), timeConverter, epoch, false, true);
  66.         }

  67.         // endpoints
  68.         if (formatVersion < 2.0) {
  69.             generator.writeEntry(ApmQuaternionKey.Q_FRAME_A.name(), quaternion.getEndpoints().getFrameA().getName(), null, true);
  70.             generator.writeEntry(ApmQuaternionKey.Q_FRAME_B.name(), quaternion.getEndpoints().getFrameB().getName(), null, true);
  71.             generator.writeEntry(ApmQuaternionKey.Q_DIR.name(),
  72.                                  quaternion.getEndpoints().isA2b() ? AttitudeEndpoints.A2B : AttitudeEndpoints.B2A,
  73.                                                                    null, true);
  74.         } else {
  75.             generator.writeEntry(ApmQuaternionKey.REF_FRAME_A.name(), quaternion.getEndpoints().getFrameA().getName(), null, true);
  76.             generator.writeEntry(ApmQuaternionKey.REF_FRAME_B.name(), quaternion.getEndpoints().getFrameB().getName(), null, true);
  77.         }

  78.         // quaternion
  79.         if (generator.getFormat() == FileFormat.XML) {
  80.             generator.enterSection(ApmQuaternionKey.quaternion.name());
  81.         }
  82.         final Quaternion q = quaternion.getQuaternion();
  83.         generator.writeEntry(ApmQuaternionKey.Q1.name(), q.getQ1(), Unit.ONE, true);
  84.         generator.writeEntry(ApmQuaternionKey.Q2.name(), q.getQ2(), Unit.ONE, true);
  85.         generator.writeEntry(ApmQuaternionKey.Q3.name(), q.getQ3(), Unit.ONE, true);
  86.         generator.writeEntry(ApmQuaternionKey.QC.name(), q.getQ0(), Unit.ONE, true);
  87.         if (generator.getFormat() == FileFormat.XML) {
  88.             generator.exitSection();
  89.         }

  90.         // quaternion derivative
  91.         if (quaternion.hasRates()) {
  92.             if (generator.getFormat() == FileFormat.XML) {
  93.                 generator.enterSection(formatVersion < 2.0 ?
  94.                                        ApmQuaternionKey.quaternionRate.name() :
  95.                                        ApmQuaternionKey.quaternionDot.name());
  96.             }
  97.             final Quaternion qDot = quaternion.getQuaternionDot();
  98.             generator.writeEntry(ApmQuaternionKey.Q1_DOT.name(), qDot.getQ1(), Unit.ONE, true);
  99.             generator.writeEntry(ApmQuaternionKey.Q2_DOT.name(), qDot.getQ2(), Unit.ONE, true);
  100.             generator.writeEntry(ApmQuaternionKey.Q3_DOT.name(), qDot.getQ3(), Unit.ONE, true);
  101.             generator.writeEntry(ApmQuaternionKey.QC_DOT.name(), qDot.getQ0(), Unit.ONE, true);
  102.             if (generator.getFormat() == FileFormat.XML) {
  103.                 generator.exitSection();
  104.             }
  105.         }

  106.     }

  107. }