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.orekit.files.ccsds.definitions.Units;
23  import org.orekit.files.ccsds.ndm.adm.AttitudeEndoints;
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 Euler data.
29   * @author Luc Maisonobe
30   * @since 11.0
31   */
32  class EulerWriter extends AbstractWriter {
33  
34      /** Suffix for angles. */
35      private static String ANGLE = "_ANGLE";
36  
37      /** Suffix for rates. */
38      private static String RATE = "_RATE";
39  
40      /** Euler block. */
41      private final Euler euler;
42  
43      /** Create a writer.
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 euler Euler data to write
47       */
48      EulerWriter(final String xmlTag, final String kvnTag,
49                          final Euler euler) {
50          super(xmlTag, kvnTag);
51          this.euler = euler;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      protected void writeContent(final Generator generator) throws IOException {
57  
58          generator.writeComments(euler.getComments());
59  
60          // endpoints
61          generator.writeEntry(EulerKey.EULER_FRAME_A.name(), euler.getEndpoints().getFrameA().getName(), null, true);
62          generator.writeEntry(EulerKey.EULER_FRAME_B.name(), euler.getEndpoints().getFrameB().getName(), null, true);
63          generator.writeEntry(EulerKey.EULER_DIR.name(),
64                               euler.getEndpoints().isA2b() ? AttitudeEndoints.A2B : AttitudeEndoints.B2A,
65                               null, true);
66  
67          // angles
68          final String   seq    = euler.getEulerRotSeq().name();
69          final double[] angles = euler.getRotationAngles();
70          generator.writeEntry(EulerKey.EULER_ROT_SEQ.name(),
71                               seq.replace('X', '1').replace('Y', '2').replace('Z', '3'),
72                               null, true);
73          generator.writeEntry(EulerKey.RATE_FRAME.name(),
74                               euler.rateFrameIsA() ? EulerKey.EULER_FRAME_A.name() : EulerKey.EULER_FRAME_B.name(),
75                               null, true);
76  
77          // if we don't have rates, at least we need angles
78          // (we may have only rates, as orientation is already given by mandatory quaternion)
79          final boolean needsAngles = !euler.hasRates();
80          generator.writeEntry(seq.charAt(0) + ANGLE, angles[0], Unit.DEGREE, needsAngles);
81          generator.writeEntry(seq.charAt(1) + ANGLE, angles[1], Unit.DEGREE, needsAngles);
82          generator.writeEntry(seq.charAt(2) + ANGLE, angles[2], Unit.DEGREE, needsAngles);
83  
84          // rates
85          if (euler.hasRates()) {
86              final double[] rates = euler.getRotationRates();
87              generator.writeEntry(seq.charAt(0) + RATE, rates[0], Units.DEG_PER_S, true);
88              generator.writeEntry(seq.charAt(1) + RATE, rates[1], Units.DEG_PER_S, true);
89              generator.writeEntry(seq.charAt(2) + RATE, rates[2], Units.DEG_PER_S, true);
90          }
91  
92      }
93  
94  }