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.ndm.adm.AttitudeEndpoints;
24  import org.orekit.files.ccsds.section.AbstractWriter;
25  import org.orekit.files.ccsds.utils.generation.Generator;
26  import org.orekit.utils.units.Unit;
27  
28  /** Writer for spin stabilized data.
29   * @author Luc Maisonobe
30   * @since 11.0
31   */
32  class SpinStabilizedWriter extends AbstractWriter {
33  
34      /** Format version.
35       * @since 12.0
36       */
37      private final double formatVersion;
38  
39      /** Spin stabilized block. */
40      private final SpinStabilized spinStabilized;
41  
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 spinStabilized spin stabilized data to write
47       */
48      SpinStabilizedWriter(final double formatVersion, final String xmlTag, final String kvnTag,
49                           final SpinStabilized spinStabilized) {
50          super(xmlTag, kvnTag);
51          this.formatVersion  = formatVersion;
52          this.spinStabilized = spinStabilized;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      protected void writeContent(final Generator generator) throws IOException {
58  
59          generator.writeComments(spinStabilized.getComments());
60  
61          // endpoints
62          if (formatVersion < 2.0) {
63              generator.writeEntry(SpinStabilizedKey.SPIN_FRAME_A.name(), spinStabilized.getEndpoints().getFrameA().getName(), null, true);
64              generator.writeEntry(SpinStabilizedKey.SPIN_FRAME_B.name(), spinStabilized.getEndpoints().getFrameB().getName(), null, true);
65              generator.writeEntry(SpinStabilizedKey.SPIN_DIR.name(),
66                                   spinStabilized.getEndpoints().isA2b() ? AttitudeEndpoints.A2B : AttitudeEndpoints.B2A,
67                                                                         null, true);
68          } else {
69              generator.writeEntry(SpinStabilizedKey.REF_FRAME_A.name(), spinStabilized.getEndpoints().getFrameA().getName(), null, true);
70              generator.writeEntry(SpinStabilizedKey.REF_FRAME_B.name(), spinStabilized.getEndpoints().getFrameB().getName(), null, true);
71          }
72  
73          // spin
74          generator.writeEntry(SpinStabilizedKey.SPIN_ALPHA.name(),     spinStabilized.getSpinAlpha(), Unit.DEGREE,        true);
75          generator.writeEntry(SpinStabilizedKey.SPIN_DELTA.name(),     spinStabilized.getSpinDelta(), Unit.DEGREE,        true);
76          generator.writeEntry(SpinStabilizedKey.SPIN_ANGLE.name(),     spinStabilized.getSpinAngle(), Unit.DEGREE,        true);
77          generator.writeEntry(SpinStabilizedKey.SPIN_ANGLE_VEL.name(), spinStabilized.getSpinAngleVel(), Units.DEG_PER_S, true);
78  
79          if (spinStabilized.hasMomentum()) {
80              // momentum
81              generator.writeEntry(SpinStabilizedKey.MOMENTUM_ALPHA.name(), spinStabilized.getMomentumAlpha(), Unit.DEGREE,     true);
82              generator.writeEntry(SpinStabilizedKey.MOMENTUM_DELTA.name(), spinStabilized.getMomentumDelta(), Unit.DEGREE,     true);
83              generator.writeEntry(SpinStabilizedKey.NUTATION_VEL.name(),   spinStabilized.getNutationVel(),   Units.DEG_PER_S, true);
84          } else if (spinStabilized.hasNutation()) {
85              // nutation
86              generator.writeEntry(SpinStabilizedKey.NUTATION.name(),       spinStabilized.getNutation(),       Unit.DEGREE, true);
87              generator.writeEntry(SpinStabilizedKey.NUTATION_PER.name(),   spinStabilized.getNutationPeriod(), Unit.SECOND, true);
88              generator.writeEntry(SpinStabilizedKey.NUTATION_PHASE.name(), spinStabilized.getNutationPhase(),  Unit.DEGREE, true);
89          }
90  
91      }
92  
93  }