1   /* Copyright 2002-2021 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  
18  package org.orekit.files.ccsds.ndm.adm.apm;
19  
20  import java.io.IOException;
21  
22  import org.hipparchus.complex.Quaternion;
23  import org.orekit.files.ccsds.definitions.TimeConverter;
24  import org.orekit.files.ccsds.ndm.adm.AttitudeEndoints;
25  import org.orekit.files.ccsds.section.AbstractWriter;
26  import org.orekit.files.ccsds.utils.generation.Generator;
27  import org.orekit.utils.units.Unit;
28  
29  /** Writer for quaternion data.
30   * @author Luc Maisonobe
31   * @since 11.0
32   */
33  class ApmQuaternionWriter extends AbstractWriter {
34  
35      /** Quaternion block. */
36      private final ApmQuaternion quaternion;
37  
38      /** Converter for dates. */
39      private final TimeConverter timeConverter;
40  
41      /** Create a writer.
42       * @param xmlTag name of the XML tag surrounding the section
43       * @param kvnTag name of the KVN tag surrounding the section (may be null)
44       * @param quaternion quaternion to write
45       * @param timeConverter converter for dates
46       */
47      ApmQuaternionWriter(final String xmlTag, final String kvnTag,
48                          final ApmQuaternion quaternion, final TimeConverter timeConverter) {
49          super(xmlTag, kvnTag);
50          this.quaternion    = quaternion;
51          this.timeConverter = timeConverter;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      protected void writeContent(final Generator generator) throws IOException {
57  
58          generator.writeEntry(ApmQuaternionKey.EPOCH.name(), timeConverter, quaternion.getEpoch(), true);
59  
60          // endpoints
61          generator.writeEntry(ApmQuaternionKey.Q_FRAME_A.name(), quaternion.getEndpoints().getFrameA().getName(), null, true);
62          generator.writeEntry(ApmQuaternionKey.Q_FRAME_B.name(), quaternion.getEndpoints().getFrameB().getName(), null, true);
63          generator.writeEntry(ApmQuaternionKey.Q_DIR.name(),
64                               quaternion.getEndpoints().isA2b() ? AttitudeEndoints.A2B : AttitudeEndoints.B2A,
65                               null, true);
66  
67          // quaternion
68          final Quaternion q = quaternion.getQuaternion();
69          generator.writeEntry(ApmQuaternionKey.Q1.name(), q.getQ1(), Unit.ONE, true);
70          generator.writeEntry(ApmQuaternionKey.Q2.name(), q.getQ2(), Unit.ONE, true);
71          generator.writeEntry(ApmQuaternionKey.Q3.name(), q.getQ3(), Unit.ONE, true);
72          generator.writeEntry(ApmQuaternionKey.QC.name(), q.getQ0(), Unit.ONE, true);
73  
74          // quaternion derivative
75          if (quaternion.hasRates()) {
76              final Quaternion qDot = quaternion.getQuaternionDot();
77              generator.writeEntry(ApmQuaternionKey.Q1_DOT.name(), qDot.getQ1(), Unit.ONE, true);
78              generator.writeEntry(ApmQuaternionKey.Q2_DOT.name(), qDot.getQ2(), Unit.ONE, true);
79              generator.writeEntry(ApmQuaternionKey.Q3_DOT.name(), qDot.getQ3(), Unit.ONE, true);
80              generator.writeEntry(ApmQuaternionKey.QC_DOT.name(), qDot.getQ0(), Unit.ONE, true);
81          }
82  
83      }
84  
85  }