SP3Ephemeris.java

  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.sp3;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.orekit.files.general.EphemerisFile;
  22. import org.orekit.frames.Frame;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.time.AggregatedClockModel;
  25. import org.orekit.time.ClockModel;
  26. import org.orekit.utils.CartesianDerivativesFilter;
  27. import org.orekit.utils.TimeSpanMap;

  28. /** Single satellite ephemeris from an {@link SP3 SP3} file.
  29.  * @author Luc Maisonobe
  30.  * @since 12.0
  31.  */
  32. public class SP3Ephemeris implements EphemerisFile.SatelliteEphemeris<SP3Coordinate, SP3Segment> {

  33.     /** Satellite ID. */
  34.     private final String id;

  35.     /** Standard gravitational parameter in m³ / s². */
  36.     private final double mu;

  37.     /** Reference frame. */
  38.     private final Frame frame;

  39.     /** Number of points to use for interpolation. */
  40.     private final int interpolationSamples;

  41.     /** Available derivatives. */
  42.     private final CartesianDerivativesFilter filter;

  43.     /** Segments. */
  44.     private final List<SP3Segment> segments;

  45.     /** Create an ephemeris for a single satellite.
  46.      * @param id of the satellite.
  47.      * @param mu standard gravitational parameter to use for creating
  48.      * {@link org.orekit.orbits.Orbit Orbits} from the ephemeris data.
  49.      * @param frame reference frame
  50.      * @param interpolationSamples number of points to use for interpolation
  51.      * @param filter available derivatives
  52.      */
  53.     public SP3Ephemeris(final String id, final double mu, final Frame frame,
  54.                         final int interpolationSamples, final CartesianDerivativesFilter filter) {
  55.         this.id                   = id;
  56.         this.mu                   = mu;
  57.         this.frame                = frame;
  58.         this.interpolationSamples = interpolationSamples;
  59.         this.filter               = filter;
  60.         this.segments             = new ArrayList<>();
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public String getId() {
  65.         return this.id;
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public double getMu() {
  70.         return mu;
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public List<SP3Segment> getSegments() {
  75.         return Collections.unmodifiableList(segments);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public AbsoluteDate getStart() {
  80.         return segments.isEmpty() ? null : segments.get(0).getStart();
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public AbsoluteDate getStop() {
  85.         return segments.isEmpty() ? null : segments.get(segments.size() - 1).getStop();
  86.     }

  87.     /** Get the reference frame.
  88.      * @return reference frame
  89.      */
  90.     public Frame getFrame() {
  91.         return frame;
  92.     }

  93.     /** Get the number of points to use for interpolation.
  94.      * @return number of points to use for interpolation
  95.      */
  96.     public int getInterpolationSamples() {
  97.         return interpolationSamples;
  98.     }

  99.     /** Get the available derivatives.
  100.      * @return available derivatives
  101.      */
  102.     public CartesianDerivativesFilter getAvailableDerivatives() {
  103.         return filter;
  104.     }

  105.     /** Adds a new P/V coordinate.
  106.      * @param coord the P/V coordinate of the satellite
  107.      * @param maxGap maximum gap between segments
  108.      */
  109.     public void addCoordinate(final SP3Coordinate coord, final double maxGap) {
  110.         final AbsoluteDate lastDate = getStop();
  111.         final SP3Segment segment;
  112.         if (lastDate == null || coord.getDate().durationFrom(lastDate) > maxGap) {
  113.             // we need to create a new segment
  114.             segment = new SP3Segment(mu, frame,  interpolationSamples, filter);
  115.             segments.add(segment);
  116.         } else {
  117.             segment = segments.get(segments.size() - 1);
  118.         }
  119.         segment.addCoordinate(coord);
  120.     }

  121.     /** Extract the clock model.
  122.      * <p>
  123.      *  There are always 2n+1 {@link AggregatedClockModel#getModels()}
  124.      *  underlying clock models when there are n {@link #getSegments() segments}
  125.      *  in the ephemeris. This happens because there are spans with {@code null}
  126.      *  data before the first segment, between all regular segments and after
  127.      *  last segment.
  128.      * </p>
  129.      * @return extracted clock model
  130.      * @since 12.1
  131.      */
  132.     public AggregatedClockModel extractClockModel() {
  133.         // set up the map for all segments clock models
  134.         final TimeSpanMap<ClockModel> models = new TimeSpanMap<>(null);
  135.         segments.forEach(segment -> models.addValidBetween(segment.extractClockModel(),
  136.                                                            segment.getStart(),
  137.                                                            segment.getStop()));
  138.         return new AggregatedClockModel(models);
  139.     }

  140. }