1   /* Copyright 2002-2025 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.oem;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.orekit.data.DataContext;
26  import org.orekit.errors.OrekitException;
27  import org.orekit.errors.OrekitMessages;
28  import org.orekit.files.ccsds.definitions.TimeSystem;
29  import org.orekit.files.ccsds.ndm.NdmConstituent;
30  import org.orekit.files.ccsds.ndm.odm.OdmHeader;
31  import org.orekit.files.general.EphemerisFile;
32  import org.orekit.utils.IERSConventions;
33  import org.orekit.utils.TimeStampedPVCoordinates;
34  
35  /** This class stores all the information of the OEM File parsed by OEMParser.
36   * <p>
37   * It contains the header and a list of Ephemerides Blocks each containing
38   * metadata, a list of ephemerides data lines and optional covariance matrices
39   * (and their metadata).
40   * </p>
41   * @author sports
42   * @author Evan Ward
43   * @since 6.1
44   */
45  public class Oem extends NdmConstituent<OdmHeader, OemSegment>
46      implements EphemerisFile<TimeStampedPVCoordinates, OemSegment> {
47  
48      /** Root element for XML files. */
49      public static final String ROOT = "oem";
50  
51      /** Key for format version. */
52      public static final String FORMAT_VERSION_KEY = "CCSDS_OEM_VERS";
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
63       */
64      public Oem(final OdmHeader header, final List<OemSegment> 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      /** {@inheritDoc} */
72      @Override
73      public Map<String, OemSatelliteEphemeris> getSatellites() {
74          final Map<String, List<OemSegment>> byId = new HashMap<>();
75          for (final OemSegment segment : getSegments()) {
76              final String id = segment.getMetadata().getObjectID();
77              byId.putIfAbsent(id, new ArrayList<>());
78              byId.get(id).add(segment);
79          }
80          final Map<String, OemSatelliteEphemeris> ret = new HashMap<>();
81          for (final Map.Entry<String, List<OemSegment>> entry : byId.entrySet()) {
82              ret.put(entry.getKey(), new OemSatelliteEphemeris(entry.getKey(), mu, entry.getValue()));
83          }
84          return ret;
85      }
86  
87      /** Check that, according to the CCSDS standard, every OEMBlock has the same time system.
88       */
89      public void checkTimeSystems() {
90          TimeSystem referenceTimeSystem = null;
91          for (final OemSegment segment : getSegments()) {
92              final TimeSystem timeSystem = segment.getMetadata().getTimeSystem();
93              if (referenceTimeSystem == null) {
94                  referenceTimeSystem = timeSystem;
95              } else if (!referenceTimeSystem.equals(timeSystem)) {
96                  throw new OrekitException(OrekitMessages.CCSDS_INCONSISTENT_TIME_SYSTEMS,
97                                            referenceTimeSystem.name(), timeSystem.name());
98              }
99          }
100     }
101 
102 }