1   /* Copyright 2002-2026 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.odm.omm;
19  
20  import java.util.List;
21  
22  import org.orekit.data.DataContext;
23  import org.orekit.errors.OrekitException;
24  import org.orekit.errors.OrekitMessages;
25  import org.orekit.files.ccsds.ndm.NdmConstituent;
26  import org.orekit.files.ccsds.ndm.odm.KeplerianElements;
27  import org.orekit.files.ccsds.ndm.odm.OdmCommonMetadata;
28  import org.orekit.files.ccsds.ndm.odm.OdmHeader;
29  import org.orekit.files.ccsds.section.Segment;
30  import org.orekit.orbits.CartesianOrbit;
31  import org.orekit.orbits.KeplerianOrbit;
32  import org.orekit.propagation.SpacecraftState;
33  import org.orekit.propagation.analytical.tle.TLE;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.TimeStamped;
36  import org.orekit.utils.IERSConventions;
37  
38  /**
39   * This class gathers the informations present in the Orbital Mean-Elements Message (OMM).
40   * @author sports
41   * @since 6.1
42   */
43  public class Omm extends NdmConstituent<OdmHeader, Segment<OmmMetadata, OmmData>> implements TimeStamped {
44  
45      /** Root element for XML files. */
46      public static final String ROOT = "omm";
47  
48      /** Key for format version. */
49      public static final String FORMAT_VERSION_KEY = "CCSDS_OMM_VERS";
50  
51      /** Default value for missing optional values. */
52      private static final double DEFAULT_MISSING_OPTIONAL_VALUE = 0.0;
53  
54      /** Gravitational coefficient to use for building Cartesian/Keplerian orbits. */
55      private final double mu;
56  
57      /** Simple constructor.
58       * @param header file header
59       * @param segments file segments
60       * @param conventions IERS conventions
61       * @param dataContext used for creating frames, time scales, etc.
62       * @param mu gravitational coefficient to use for building Cartesian/Keplerian orbits
63       */
64      public Omm(final OdmHeader header, final List<Segment<OmmMetadata, OmmData>> segments,
65                 final IERSConventions conventions, final DataContext dataContext,
66                 final double mu) {
67          super(header, segments, conventions, dataContext);
68          this.mu = mu;
69      }
70  
71      /** Get the file metadata.
72       * @return file metadata
73       */
74      public OmmMetadata getMetadata() {
75          return getSegments().getFirst().getMetadata();
76      }
77  
78      /** Get the file data.
79       * @return file data
80       */
81      public OmmData getData() {
82          return getSegments().getFirst().getData();
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public AbsoluteDate getDate() {
88          return getData().getKeplerianElementsBlock().getEpoch();
89      }
90  
91      /** Generate a keplerian orbit.
92       * @return generated orbit
93       */
94      public KeplerianOrbit generateKeplerianOrbit() {
95          return getData().getKeplerianElementsBlock().generateKeplerianOrbit(getMetadata().getFrame(), mu);
96      }
97  
98      /** Generate spacecraft state from the {@link CartesianOrbit} generated by generateCartesianOrbit.
99       *  Raises an exception if OPM doesn't contain spacecraft mass information.
100      * @return the spacecraft state of the OPM
101      */
102     public SpacecraftState generateSpacecraftState() {
103         return new SpacecraftState(generateKeplerianOrbit()).withMass(getData().getMass());
104     }
105 
106     /** Generate TLE from OMM file. Launch Year, Launch Day and Launch Piece are not present in the
107      * OMM file, they have to be set manually by the user with the AdditionalData static class.
108      * @return the tle
109      */
110     public TLE generateTLE() {
111         final OdmCommonMetadata metadata = getMetadata();
112         final KeplerianElements kep = getData().getKeplerianElementsBlock();
113         final OmmTle            tle = getData().getTLEBlock().orElseThrow(() -> new OrekitException(OrekitMessages.CCSDS_MISSING_OPTIONAL_VALUE));
114         return new TLE(tle.getNoradID(), tle.getClassificationType(),
115                        metadata.getLaunchYear(), metadata.getLaunchNumber(), metadata.getLaunchPiece(),
116                        tle.getEphemerisType(), tle.getElementSetNumber(), kep.getEpoch(),
117                        kep.getMeanMotion().orElse(DEFAULT_MISSING_OPTIONAL_VALUE),
118                        tle.getMeanMotionDot().orElse(DEFAULT_MISSING_OPTIONAL_VALUE) / 2,
119                        tle.getMeanMotionDotDot().orElse(DEFAULT_MISSING_OPTIONAL_VALUE) / 6,
120                        kep.getE(), kep.getI(), kep.getPa(), kep.getRaan(),
121                        kep.getAnomaly(), tle.getRevAtEpoch(),
122                        tle.getBStar().orElse(DEFAULT_MISSING_OPTIONAL_VALUE), getDataContext().getTimeScales().getUTC());
123     }
124 
125 }