1   /* Copyright 2002-2025 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.AttitudeEndpoints;
25  import org.orekit.files.ccsds.section.AbstractWriter;
26  import org.orekit.files.ccsds.utils.FileFormat;
27  import org.orekit.files.ccsds.utils.generation.Generator;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.utils.units.Unit;
30  
31  /** Writer for quaternion data.
32   * @author Luc Maisonobe
33   * @since 11.0
34   */
35  class ApmQuaternionWriter extends AbstractWriter {
36  
37      /** Format version.
38       * @since 12.0
39       */
40      private final double formatVersion;
41  
42      /** Quaternion block. */
43      private final ApmQuaternion quaternion;
44  
45      /** Quaternion epoch. */
46      private final AbsoluteDate epoch;
47  
48      /** Converter for dates. */
49      private final TimeConverter timeConverter;
50  
51      /** Create a writer.
52       * @param formatVersion format version
53       * @param xmlTag name of the XML tag surrounding the section
54       * @param kvnTag name of the KVN tag surrounding the section (may be null)
55       * @param quaternion quaternion to write
56       * @param epoch quaternion epoch (only for ADM V1)
57       * @param timeConverter converter for dates
58       */
59      ApmQuaternionWriter(final double formatVersion, final String xmlTag, final String kvnTag,
60                          final ApmQuaternion quaternion,
61                          final AbsoluteDate epoch, final TimeConverter timeConverter) {
62          super(xmlTag, kvnTag);
63          this.formatVersion = formatVersion;
64          this.quaternion    = quaternion;
65          this.epoch         = epoch;
66          this.timeConverter = timeConverter;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      protected void writeContent(final Generator generator) throws IOException {
72  
73          generator.writeComments(quaternion.getComments());
74  
75          if (epoch != null) {
76              // epoch is in the quaternion block only in ADM V1
77              generator.writeEntry(ApmQuaternionKey.EPOCH.name(), timeConverter, epoch, false, true);
78          }
79  
80          // endpoints
81          if (formatVersion < 2.0) {
82              generator.writeEntry(ApmQuaternionKey.Q_FRAME_A.name(), quaternion.getEndpoints().getFrameA().getName(), null, true);
83              generator.writeEntry(ApmQuaternionKey.Q_FRAME_B.name(), quaternion.getEndpoints().getFrameB().getName(), null, true);
84              generator.writeEntry(ApmQuaternionKey.Q_DIR.name(),
85                                   quaternion.getEndpoints().isA2b() ? AttitudeEndpoints.A2B : AttitudeEndpoints.B2A,
86                                                                     null, true);
87          } else {
88              generator.writeEntry(ApmQuaternionKey.REF_FRAME_A.name(), quaternion.getEndpoints().getFrameA().getName(), null, true);
89              generator.writeEntry(ApmQuaternionKey.REF_FRAME_B.name(), quaternion.getEndpoints().getFrameB().getName(), null, true);
90          }
91  
92          // quaternion
93          if (generator.getFormat() == FileFormat.XML) {
94              generator.enterSection(ApmQuaternionKey.quaternion.name());
95          }
96          final Quaternion q = quaternion.getQuaternion();
97          generator.writeEntry(ApmQuaternionKey.Q1.name(), q.getQ1(), Unit.ONE, true);
98          generator.writeEntry(ApmQuaternionKey.Q2.name(), q.getQ2(), Unit.ONE, true);
99          generator.writeEntry(ApmQuaternionKey.Q3.name(), q.getQ3(), Unit.ONE, true);
100         generator.writeEntry(ApmQuaternionKey.QC.name(), q.getQ0(), Unit.ONE, true);
101         if (generator.getFormat() == FileFormat.XML) {
102             generator.exitSection();
103         }
104 
105         // quaternion derivative
106         if (quaternion.hasRates()) {
107             if (generator.getFormat() == FileFormat.XML) {
108                 generator.enterSection(formatVersion < 2.0 ?
109                                        ApmQuaternionKey.quaternionRate.name() :
110                                        ApmQuaternionKey.quaternionDot.name());
111             }
112             final Quaternion qDot = quaternion.getQuaternionDot();
113             generator.writeEntry(ApmQuaternionKey.Q1_DOT.name(), qDot.getQ1(), Unit.ONE, true);
114             generator.writeEntry(ApmQuaternionKey.Q2_DOT.name(), qDot.getQ2(), Unit.ONE, true);
115             generator.writeEntry(ApmQuaternionKey.Q3_DOT.name(), qDot.getQ3(), Unit.ONE, true);
116             generator.writeEntry(ApmQuaternionKey.QC_DOT.name(), qDot.getQ0(), Unit.ONE, true);
117             if (generator.getFormat() == FileFormat.XML) {
118                 generator.exitSection();
119             }
120         }
121 
122     }
123 
124 }