1   /* Copyright 2022-2025 Luc Maisonobe
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.acm;
18  
19  import org.hipparchus.geometry.euclidean.threed.Rotation;
20  import org.orekit.files.ccsds.definitions.Units;
21  import org.orekit.files.ccsds.utils.ContextBinding;
22  import org.orekit.files.ccsds.utils.lexical.ParseToken;
23  import org.orekit.files.ccsds.utils.lexical.TokenType;
24  import org.orekit.utils.units.Unit;
25  
26  
27  /** Keys for {@link AttitudeManeuver attitude maneuver} entries.
28   * @author Luc Maisonobe
29   * @since 12.0
30   */
31  public enum AttitudeManeuverKey {
32  
33      /** Comment entry. */
34      COMMENT((token, context, container) ->
35              token.getType() == TokenType.ENTRY ? container.addComment(token.getContentAsNormalizedString()) : true),
36  
37      /** Maneuver identification number. */
38      MAN_ID((token, context, container) -> token.processAsFreeTextString(container::setID)),
39  
40      /** Identification number of previous maneuver. */
41      MAN_PREV_ID((token, context, container) -> token.processAsFreeTextString(container::setPrevID)),
42  
43      /** Purpose of the maneuver. */
44      MAN_PURPOSE((token, context, container) -> token.processAsFreeTextString(container::setManPurpose)),
45  
46      /** Start time of actual maneuver, relative to t₀. */
47      MAN_BEGIN_TIME((token, context, container) -> token.processAsDouble(Unit.SECOND, context.getParsedUnitsBehavior(),
48                                                                          container::setBeginTime)),
49  
50      /** End time of actual maneuver, relative to t₀. */
51      MAN_END_TIME((token, context, container) -> token.processAsDouble(Unit.SECOND, context.getParsedUnitsBehavior(),
52                                                                        container::setEndTime)),
53  
54      /** Duration. */
55      MAN_DURATION((token, context, container) -> token.processAsDouble(Unit.SECOND, context.getParsedUnitsBehavior(),
56                                                                        container::setDuration)),
57  
58      /** Actuator used. */
59      ACTUATOR_USED((token, context, container) -> token.processAsFreeTextString(container::setActuatorUsed)),
60  
61      /** Target momentum. */
62      TARGET_MOMENTUM((token, context, container) -> token.processAsVector(Units.N_M_S, context.getParsedUnitsBehavior(),
63                                                                           container::setTargetMomentum)),
64  
65      /** Target momentum frame. */
66      TARGET_MOM_FRAME((token, context, container) -> token.processAsFrame(container::setTargetMomFrame, context, true, true, true)),
67  
68      /** Target attitude. */
69      TARGET_ATTITUDE((token, context, container) -> {
70          try {
71              if (token.getType() == TokenType.ENTRY) {
72                  final String[] fields = token.getRawContent().split("\\p{Space}+");
73                  if (fields.length == 4) {
74                      container.setTargetAttitude(new Rotation(Double.parseDouble(fields[3]),
75                                                               Double.parseDouble(fields[0]),
76                                                               Double.parseDouble(fields[1]),
77                                                               Double.parseDouble(fields[2]),
78                                                               true));
79                      return true;
80                  }
81              } else {
82                  return true;
83              }
84          } catch (NumberFormatException nfe) {
85              // ignored, error handled below, together with wrong number of fields
86          }
87          throw token.generateException(null);
88      }),
89  
90      /** Target spin rate. */
91      TARGET_SPINRATE((token, context, container) -> token.processAsDouble(Units.DEG_PER_S, context.getParsedUnitsBehavior(),
92                                                                            container::setTargetSpinRate));
93  
94      /** Processing method. */
95      private final transient TokenProcessor processor;
96  
97      /** Simple constructor.
98       * @param processor processing method
99       */
100     AttitudeManeuverKey(final TokenProcessor processor) {
101         this.processor = processor;
102     }
103 
104     /** Process an token.
105      * @param token token to process
106      * @param context context binding
107      * @param data data to fill
108      * @return true of token was accepted
109      */
110     public boolean process(final ParseToken token, final ContextBinding context, final AttitudeManeuver data) {
111         return processor.process(token, context, data);
112     }
113 
114     /** Interface for processing one token. */
115     interface TokenProcessor {
116         /** Process one token.
117          * @param token token to process
118          * @param context context binding
119          * @param data data to fill
120          * @return true of token was accepted
121          */
122         boolean process(ParseToken token, ContextBinding context, AttitudeManeuver data);
123     }
124 
125 }