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 org.orekit.files.ccsds.ndm.adm.AdmMetadataKey;
20  import org.orekit.files.ccsds.ndm.adm.AdmParser;
21  import org.orekit.files.ccsds.ndm.adm.AttitudeType;
22  import org.orekit.files.ccsds.utils.ContextBinding;
23  import org.orekit.files.ccsds.utils.lexical.ParseToken;
24  import org.orekit.files.ccsds.utils.lexical.TokenType;
25  
26  
27  /** Keys for {@link AemMetadata AEM container} entries.
28   * <p>
29   * Additional container are also listed in {@link AdmMetadataKey}.
30   * </p>
31   * @author Bryan Cazabonne
32   * @since 11.0
33   */
34  public enum AemMetadataKey {
35  
36      /** First reference frame. */
37      REF_FRAME_A((token, context, container) -> token.processAsFrame(container.getEndpoints()::setFrameA, context, true, true, true)),
38  
39      /** Second reference frame. */
40      REF_FRAME_B((token, context, container) -> {
41          if (token.getType() == TokenType.ENTRY) {
42              container.checkNotNull(container.getEndpoints().getFrameA(), REF_FRAME_A);
43              final boolean aIsSpaceraftBody = container.getEndpoints().getFrameA().asSpacecraftBodyFrame() != null;
44              return token.processAsFrame(container.getEndpoints()::setFrameB, context,
45                                          aIsSpaceraftBody, aIsSpaceraftBody, !aIsSpaceraftBody);
46          }
47          return true;
48      }),
49  
50      /** Rotation direction entry. */
51      ATTITUDE_DIR((token, context, container) -> {
52          if (token.getType() == TokenType.ENTRY) {
53              container.getEndpoints().setA2b(token.getContentAsUppercaseCharacter() == 'A');
54          }
55          return true;
56      }),
57  
58      /** Start time entry. */
59      START_TIME((token, context, container) -> token.processAsDate(container::setStartTime, context)),
60  
61      /** Stop time entry. */
62      STOP_TIME((token, context, container) -> token.processAsDate(container::setStopTime, context)),
63  
64      /** Useable start time entry. */
65      USEABLE_START_TIME((token, context, container) -> token.processAsDate(container::setUseableStartTime, context)),
66  
67      /** Useable stop time entry. */
68      USEABLE_STOP_TIME((token, context, container) -> token.processAsDate(container::setUseableStopTime, context)),
69  
70      /** Format of the data line entry. */
71      ATTITUDE_TYPE((token, context, container) -> {
72          if (token.getType() == TokenType.ENTRY) {
73              try {
74                  container.setAttitudeType(AttitudeType.parseType(token.getContentAsNormalizedString()));
75                  return true;
76              } catch (IllegalArgumentException iae) {
77                  return false;
78              }
79          }
80          return true;
81      }),
82  
83      /** Placement of the scalar component in quaternion entry. */
84      QUATERNION_TYPE((token, context, container) -> {
85          if (token.getType() == TokenType.ENTRY) {
86              container.setIsFirst("FIRST".equals(token.getContentAsUppercaseString()));
87          }
88          return true;
89      }),
90  
91      /** Rotation order entry for Euler angles. */
92      EULER_ROT_SEQ((token, context, container) -> AdmParser.processRotationOrder(token, container::setEulerRotSeq)),
93  
94      /** Reference frame for Euler rates. */
95      RATE_FRAME((token, context, container) -> {
96          if (token.getType() == TokenType.ENTRY) {
97              final String content = token.getContentAsUppercaseString();
98              final char   suffix  = content.charAt(content.length() - 1);
99              container.setRateFrameIsA(suffix == 'A');
100         }
101         return true;
102     }),
103 
104     /** Interpolation method in ephemeris. */
105     INTERPOLATION_METHOD((token, context, container) -> token.processAsUppercaseString(container::setInterpolationMethod)),
106 
107     /** Interpolation degree in ephemeris. */
108     INTERPOLATION_DEGREE((token, context, container) -> token.processAsInteger(container::setInterpolationDegree));
109 
110     /** Processing method. */
111     private final TokenProcessor processor;
112 
113     /** Simple constructor.
114      * @param processor processing method
115      */
116     AemMetadataKey(final TokenProcessor processor) {
117         this.processor = processor;
118     }
119 
120     /** Process an token.
121      * @param token token to process
122      * @param context context binding
123      * @param container container to fill
124      * @return true of token was accepted
125      */
126     public boolean process(final ParseToken token, final ContextBinding context, final AemMetadata container) {
127         return processor.process(token, context, container);
128     }
129 
130     /** Interface for processing one token. */
131     interface TokenProcessor {
132         /** Process one token.
133          * @param token token to process
134          * @param context context binding
135          * @param container container to fill
136          * @return true of token was accepted
137          */
138         boolean process(ParseToken token, ContextBinding context, AemMetadata container);
139     }
140 
141 }