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.orekit.files.ccsds.definitions.Units;
23  import org.orekit.files.ccsds.section.AbstractWriter;
24  import org.orekit.files.ccsds.utils.generation.Generator;
25  
26  /** Writer for inertia data.
27   * @author Luc Maisonobe
28   * @since 11.0
29   */
30  class InertiaWriter extends AbstractWriter {
31  
32      /** Format version.
33       * @since 12.0
34       */
35      private final double formatVersion;
36  
37      /** Inertia block. */
38      private final Inertia inertia;
39  
40      /** Create a writer.
41       * @param formatVersion format version
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 spacecraftParameters spacecraft parameters data to write
45       */
46      InertiaWriter(final double formatVersion, final String xmlTag, final String kvnTag,
47                    final Inertia spacecraftParameters) {
48          super(xmlTag, kvnTag);
49          this.formatVersion = formatVersion;
50          this.inertia = spacecraftParameters;
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      protected void writeContent(final Generator generator) throws IOException {
56  
57          generator.writeComments(inertia.getComments());
58  
59          // frame
60          if (inertia.getFrame() != null) {
61              generator.writeEntry(InertiaKey.INERTIA_REF_FRAME.name(),
62                                   inertia.getFrame().getName(),
63                                   null, false);
64          }
65  
66          // inertia matrix
67          if (formatVersion < 2.0) {
68              generator.writeEntry(InertiaKey.I11.name(), inertia.getInertiaMatrix().getEntry(0, 0), Units.KG_M2, true);
69              generator.writeEntry(InertiaKey.I22.name(), inertia.getInertiaMatrix().getEntry(1, 1), Units.KG_M2, true);
70              generator.writeEntry(InertiaKey.I33.name(), inertia.getInertiaMatrix().getEntry(2, 2), Units.KG_M2, true);
71              generator.writeEntry(InertiaKey.I12.name(), inertia.getInertiaMatrix().getEntry(0, 1), Units.KG_M2, true);
72              generator.writeEntry(InertiaKey.I13.name(), inertia.getInertiaMatrix().getEntry(0, 2), Units.KG_M2, true);
73              generator.writeEntry(InertiaKey.I23.name(), inertia.getInertiaMatrix().getEntry(1, 2), Units.KG_M2, true);
74          } else {
75              generator.writeEntry(InertiaKey.IXX.name(), inertia.getInertiaMatrix().getEntry(0, 0), Units.KG_M2, true);
76              generator.writeEntry(InertiaKey.IYY.name(), inertia.getInertiaMatrix().getEntry(1, 1), Units.KG_M2, true);
77              generator.writeEntry(InertiaKey.IZZ.name(), inertia.getInertiaMatrix().getEntry(2, 2), Units.KG_M2, true);
78              generator.writeEntry(InertiaKey.IXY.name(), inertia.getInertiaMatrix().getEntry(0, 1), Units.KG_M2, true);
79              generator.writeEntry(InertiaKey.IXZ.name(), inertia.getInertiaMatrix().getEntry(0, 2), Units.KG_M2, true);
80              generator.writeEntry(InertiaKey.IYZ.name(), inertia.getInertiaMatrix().getEntry(1, 2), Units.KG_M2, true);
81          }
82  
83      }
84  
85  }