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  package org.orekit.files.ccsds.ndm.adm.aem;
18  
19  import java.util.Arrays;
20  
21  import org.orekit.files.ccsds.ndm.adm.AttitudeType;
22  import org.orekit.time.AbsoluteDate;
23  import org.orekit.utils.TimeStampedAngularCoordinates;
24  
25  /** Container for one attitude entry.
26   * @author Luc Maisonobe
27   * @since 11.0
28   */
29  class AttitudeEntry {
30  
31      /** Metadata used to interpret the data fields. */
32      private final AemMetadata metadata;
33  
34      /** Epoch. */
35      private AbsoluteDate epoch;
36  
37      /** Attitude components. */
38      private double[] components;
39  
40      /** Build an uninitialized entry.
41       * @param metadata metadata used to interpret the data fields
42       */
43      AttitudeEntry(final AemMetadata metadata) {
44          this.metadata   = metadata;
45          this.components = new double[8];
46          Arrays.fill(components, Double.NaN);
47      }
48  
49      /** Get the metadata.
50       * @return metadata
51       */
52      public AemMetadata getMetadata() {
53          return metadata;
54      }
55  
56      /** Set epoch.
57       * @param epoch epoch to set
58       */
59      public void setEpoch(final AbsoluteDate epoch) {
60          this.epoch = epoch;
61      }
62  
63      /** Set one component.
64       * @param i index of the component
65       * @param value value of the component
66       */
67      public void setComponent(final int i, final double value) {
68          components[i] = value;
69      }
70  
71      /** Set one angle.
72       * @param axis axis label
73       * @param value value of the angle
74       */
75      public void setAngle(final char axis, final double value) {
76          if (metadata.getEulerRotSeq() != null) {
77              final String seq = metadata.getEulerRotSeq().name();
78              if (seq.charAt(0) == axis && Double.isNaN(components[0])) {
79                  components[0] = value;
80              } else if (seq.charAt(1) == axis && Double.isNaN(components[1])) {
81                  components[1] = value;
82              } else if (seq.charAt(2) == axis && Double.isNaN(components[2])) {
83                  components[2] = value;
84              }
85          }
86      }
87  
88      /** Set one rate.
89       * @param axis axis label
90       * @param value value of the rate
91       */
92      public void setRate(final char axis, final double value) {
93          if (metadata.getEulerRotSeq() != null) {
94              final String seq   = metadata.getEulerRotSeq().name();
95              final int    first = metadata.getAttitudeType() == AttitudeType.QUATERNION_RATE ? 4 : 3;
96              if (seq.charAt(0) == axis && Double.isNaN(components[first])) {
97                  components[first] = value;
98              } else if (seq.charAt(1) == axis && Double.isNaN(components[first + 1])) {
99                  components[first + 1] = value;
100             } else if (seq.charAt(2) == axis && Double.isNaN(components[first + 2])) {
101                 components[first + 2] = value;
102             }
103         }
104     }
105 
106     /** Get the angular coordinates entry.
107      * @return angular coordinates entry
108      */
109     public TimeStampedAngularCoordinates getCoordinates() {
110         return metadata.getAttitudeType().build(metadata.isFirst(),
111                                                 metadata.getEndpoints().isExternal2SpacecraftBody(),
112                                                 metadata.getEulerRotSeq(), metadata.isSpacecraftBodyRate(),
113                                                 epoch, components);
114     }
115 
116 }