Ephemeris.java

  1. /* Copyright 2002-2022 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.propagation.analytical;

  18. import java.io.Serializable;
  19. import java.util.List;
  20. import java.util.stream.Collectors;

  21. import org.hipparchus.exception.LocalizedCoreFormats;
  22. import org.hipparchus.exception.MathIllegalArgumentException;
  23. import org.hipparchus.util.FastMath;
  24. import org.orekit.attitudes.Attitude;
  25. import org.orekit.attitudes.AttitudeProvider;
  26. import org.orekit.attitudes.InertialProvider;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.frames.Frame;
  30. import org.orekit.orbits.Orbit;
  31. import org.orekit.propagation.BoundedPropagator;
  32. import org.orekit.propagation.SpacecraftState;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.utils.DoubleArrayDictionary;
  35. import org.orekit.utils.ImmutableTimeStampedCache;
  36. import org.orekit.utils.PVCoordinatesProvider;
  37. import org.orekit.utils.TimeStampedPVCoordinates;

  38. /** This class is designed to accept and handle tabulated orbital entries.
  39.  * Tabulated entries are classified and then extrapolated in way to obtain
  40.  * continuous output, with accuracy and computation methods configured by the user.
  41.  *
  42.  * @author Fabien Maussion
  43.  * @author Véronique Pommier-Maurussane
  44.  * @author Luc Maisonobe
  45.  */
  46. public class Ephemeris extends AbstractAnalyticalPropagator implements BoundedPropagator {

  47.     /** Default extrapolation time threshold: 1ms.
  48.      * @since 9.0
  49.      **/
  50.     public static final double DEFAULT_EXTRAPOLATION_THRESHOLD_SEC = 1e-3;

  51.      /** First date in range. */
  52.     private final AbsoluteDate minDate;

  53.     /** Last date in range. */
  54.     private final AbsoluteDate maxDate;

  55.     /** The extrapolation threshold beyond which the propagation will fail. **/
  56.     private final double extrapolationThreshold;

  57.     /** Reference frame. */
  58.     private final Frame frame;

  59.     /** Names of the additional states. */
  60.     private final String[] additional;

  61.     /** Local PV Provider used for computing attitude. **/
  62.     private LocalPVProvider pvProvider;

  63.     /** Thread-safe cache. */
  64.     private final transient ImmutableTimeStampedCache<SpacecraftState> cache;

  65.     /** Constructor with tabulated states.
  66.      * <p>
  67.      * This constructor allows extrapolating outside of the states time span
  68.      * by up to the 1ms {@link #DEFAULT_EXTRAPOLATION_THRESHOLD_SEC default
  69.      * extrapolation threshold}.
  70.      * </p>
  71.      *
  72.      * @param states tabulates states
  73.      * @param interpolationPoints number of points to use in interpolation
  74.           * @exception MathIllegalArgumentException if the number of states is smaller than
  75.      * the number of points to use in interpolation
  76.      * @see #Ephemeris(List, int, double)
  77.      * @see #Ephemeris(List, int, double, AttitudeProvider)
  78.      */
  79.     public Ephemeris(final List<SpacecraftState> states, final int interpolationPoints)
  80.         throws MathIllegalArgumentException {
  81.         this(states, interpolationPoints, DEFAULT_EXTRAPOLATION_THRESHOLD_SEC);
  82.     }

  83.     /** Constructor with tabulated states.
  84.      *
  85.      * @param states tabulates states
  86.      * @param interpolationPoints number of points to use in interpolation
  87.      * @param extrapolationThreshold the largest time difference in seconds between
  88.      * the start or stop boundary of the ephemeris bounds to be doing extrapolation
  89.      * @exception MathIllegalArgumentException if the number of states is smaller than
  90.      * the number of points to use in interpolation
  91.      * @since 9.0
  92.      * @see #Ephemeris(List, int, double, AttitudeProvider)
  93.      */
  94.     public Ephemeris(final List<SpacecraftState> states, final int interpolationPoints,
  95.                      final double extrapolationThreshold)
  96.         throws MathIllegalArgumentException {
  97.         this(states, interpolationPoints, extrapolationThreshold,
  98.                 // if states is empty an exception will be thrown in the other constructor
  99.                 states.isEmpty() ? null : InertialProvider.of(states.get(0).getFrame()));
  100.     }

  101.     /** Constructor with tabulated states.
  102.      * @param states tabulates states
  103.      * @param interpolationPoints number of points to use in interpolation
  104.      * @param extrapolationThreshold the largest time difference in seconds between
  105.      * the start or stop boundary of the ephemeris bounds to be doing extrapolation
  106.      * @param attitudeProvider attitude law to use.
  107.      * @exception MathIllegalArgumentException if the number of states is smaller than
  108.      * the number of points to use in interpolation
  109.      * @since 10.1
  110.      */
  111.     public Ephemeris(final List<SpacecraftState> states,
  112.                      final int interpolationPoints,
  113.                      final double extrapolationThreshold,
  114.                      final AttitudeProvider attitudeProvider)
  115.         throws MathIllegalArgumentException {

  116.         super(attitudeProvider);

  117.         if (states.size() < interpolationPoints) {
  118.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INSUFFICIENT_DIMENSION,
  119.                                                    states.size(), interpolationPoints);
  120.         }

  121.         final SpacecraftState s0 = states.get(0);
  122.         minDate = s0.getDate();
  123.         maxDate = states.get(states.size() - 1).getDate();
  124.         frame = s0.getFrame();

  125.         final List<DoubleArrayDictionary.Entry> as = s0.getAdditionalStatesValues().getData();
  126.         additional = new String[as.size()];
  127.         for (int i = 0; i < additional.length; ++i) {
  128.             additional[i] = as.get(i).getKey();
  129.         }

  130.         // check all states handle the same additional states
  131.         for (final SpacecraftState state : states) {
  132.             s0.ensureCompatibleAdditionalStates(state);
  133.         }

  134.         pvProvider = new LocalPVProvider(states, interpolationPoints, extrapolationThreshold);

  135.         // user needs to explicitly set attitude provider if they want to use one
  136.         setAttitudeProvider(null);

  137.         // set up cache
  138.         cache = new ImmutableTimeStampedCache<SpacecraftState>(interpolationPoints, states);

  139.         this.extrapolationThreshold = extrapolationThreshold;
  140.     }

  141.     /** Get the first date of the range.
  142.      * @return the first date of the range
  143.      */
  144.     public AbsoluteDate getMinDate() {
  145.         return minDate;
  146.     }

  147.     /** Get the last date of the range.
  148.      * @return the last date of the range
  149.      */
  150.     public AbsoluteDate getMaxDate() {
  151.         return maxDate;
  152.     }

  153.     /** Get the maximum timespan outside of the stored ephemeris that is allowed
  154.      * for extrapolation.
  155.      * @return the extrapolation threshold in seconds
  156.      */
  157.     public double getExtrapolationThreshold() {
  158.         return extrapolationThreshold;
  159.     }

  160.     @Override
  161.     public Frame getFrame() {
  162.         return frame;
  163.     }

  164.     @Override
  165.     /** {@inheritDoc} */
  166.     public SpacecraftState basicPropagate(final AbsoluteDate date) {
  167.         final SpacecraftState evaluatedState;

  168.         final AbsoluteDate central;
  169.         if (date.compareTo(minDate) < 0 && FastMath.abs(date.durationFrom(minDate)) <= extrapolationThreshold) {
  170.             // avoid TimeStampedCacheException as we are still within the tolerance before minDate
  171.             central = minDate;
  172.         } else if (date.compareTo(maxDate) > 0 && FastMath.abs(date.durationFrom(maxDate)) <= extrapolationThreshold) {
  173.             // avoid TimeStampedCacheException as we are still within the tolerance after maxDate
  174.             central = maxDate;
  175.         } else {
  176.             central = date;
  177.         }
  178.         final List<SpacecraftState> neighbors = cache.getNeighbors(central).collect(Collectors.toList());
  179.         evaluatedState = neighbors.get(0).interpolate(date, neighbors);

  180.         final AttitudeProvider attitudeProvider = getAttitudeProvider();

  181.         if (attitudeProvider == null) {
  182.             return evaluatedState;
  183.         } else {
  184.             pvProvider.setCurrentState(evaluatedState);
  185.             final Attitude calculatedAttitude = attitudeProvider.getAttitude(pvProvider, date,
  186.                                                                              evaluatedState.getFrame());

  187.             // Verify if orbit is defined
  188.             if (evaluatedState.isOrbitDefined()) {
  189.                 return new SpacecraftState(evaluatedState.getOrbit(), calculatedAttitude, evaluatedState.getMass(),
  190.                                            evaluatedState.getAdditionalStatesValues(), evaluatedState.getAdditionalStatesDerivatives());
  191.             } else {
  192.                 return new SpacecraftState(evaluatedState.getAbsPVA(), calculatedAttitude, evaluatedState.getMass(),
  193.                                            evaluatedState.getAdditionalStatesValues(), evaluatedState.getAdditionalStatesDerivatives());
  194.             }

  195.         }
  196.     }

  197.     /** {@inheritDoc} */
  198.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  199.         return basicPropagate(date).getOrbit();
  200.     }

  201.     /** {@inheritDoc} */
  202.     protected double getMass(final AbsoluteDate date) {
  203.         return basicPropagate(date).getMass();
  204.     }

  205.     /** {@inheritDoc} */
  206.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame f) {
  207.         return propagate(date).getPVCoordinates(f);
  208.     }

  209.     /** Try (and fail) to reset the initial state.
  210.      * <p>
  211.      * This method always throws an exception, as ephemerides cannot be reset.
  212.      * </p>
  213.      * @param state new initial state to consider
  214.      */
  215.     public void resetInitialState(final SpacecraftState state) {
  216.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  217.     }

  218.     /** {@inheritDoc} */
  219.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  220.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  221.     }

  222.     /** {@inheritDoc} */
  223.     public SpacecraftState getInitialState() {
  224.         return basicPropagate(getMinDate());
  225.     }

  226.     /** {@inheritDoc} */
  227.     @Override
  228.     public boolean isAdditionalStateManaged(final String name) {

  229.         // the additional state may be managed by a specific provider in the base class
  230.         if (super.isAdditionalStateManaged(name)) {
  231.             return true;
  232.         }

  233.         // the additional state may be managed in the states sample
  234.         for (final String a : additional) {
  235.             if (a.equals(name)) {
  236.                 return true;
  237.             }
  238.         }

  239.         return false;

  240.     }

  241.     /** {@inheritDoc} */
  242.     @Override
  243.     public String[] getManagedAdditionalStates() {
  244.         final String[] upperManaged = super.getManagedAdditionalStates();
  245.         final String[] managed = new String[upperManaged.length + additional.length];
  246.         System.arraycopy(upperManaged, 0, managed, 0, upperManaged.length);
  247.         System.arraycopy(additional, 0, managed, upperManaged.length, additional.length);
  248.         return managed;
  249.     }

  250.     /** Internal PVCoordinatesProvider for attitude computation. */
  251.     private static class LocalPVProvider implements PVCoordinatesProvider, Serializable {

  252.         /** Serializable UID. */
  253.         private static final long serialVersionUID = 20160115L;

  254.         /** Current state. */
  255.         private SpacecraftState currentState;

  256.         /** List of spacecraft states. */
  257.         private List<SpacecraftState> states;

  258.         /** Interpolation points number. */
  259.         private int interpolationPoints;

  260.         /** Extrapolation threshold. */
  261.         private double extrapolationThreshold;

  262.         /** Constructor.
  263.          * @param states list of spacecraft states
  264.          * @param interpolationPoints interpolation points number
  265.          * @param extrapolationThreshold extrapolation threshold value
  266.          */
  267.         LocalPVProvider(final List<SpacecraftState> states, final int interpolationPoints,
  268.                      final double extrapolationThreshold) {

  269.             this.states = states;
  270.             this.interpolationPoints = interpolationPoints;
  271.             this.extrapolationThreshold = extrapolationThreshold;
  272.         }

  273.         /** Get the current state.
  274.          * @return current state
  275.          */
  276.         public SpacecraftState getCurrentState() {
  277.             return currentState;
  278.         }

  279.         /** Set the current state.
  280.          * @param state state to set
  281.          */
  282.         public void setCurrentState(final SpacecraftState state) {
  283.             this.currentState = state;
  284.         }

  285.         /** {@inheritDoc} */
  286.         public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame f) {
  287.             final double dt = getCurrentState().getDate().durationFrom(date);
  288.             final double closeEnoughTimeInSec = 1e-9;

  289.             if (FastMath.abs(dt) > closeEnoughTimeInSec) {

  290.                 // used in case of attitude transition, the attitude computed is not at the current date.
  291.                 final Ephemeris ephemeris = new Ephemeris(states, interpolationPoints, extrapolationThreshold, null);
  292.                 return ephemeris.getPVCoordinates(date, f);
  293.             }

  294.             return currentState.getPVCoordinates(f);

  295.         }

  296.     }

  297. }