Ephemeris.java

  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.propagation.analytical;

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

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.FastMath;
  25. import org.orekit.attitudes.Attitude;
  26. import org.orekit.attitudes.AttitudeProvider;
  27. import org.orekit.attitudes.InertialProvider;
  28. import org.orekit.errors.OrekitException;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.frames.Frame;
  31. import org.orekit.orbits.Orbit;
  32. import org.orekit.propagation.BoundedPropagator;
  33. import org.orekit.propagation.SpacecraftState;
  34. import org.orekit.time.AbsoluteDate;
  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 Set<String> names0 = s0.getAdditionalStates().keySet();
  126.         additional = names0.toArray(new String[names0.size()]);

  127.         // check all states handle the same additional states
  128.         for (final SpacecraftState state : states) {
  129.             s0.ensureCompatibleAdditionalStates(state);
  130.         }

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

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

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

  136.         this.extrapolationThreshold = extrapolationThreshold;
  137.     }

  138.     /** Get the first date of the range.
  139.      * @return the first date of the range
  140.      */
  141.     public AbsoluteDate getMinDate() {
  142.         return minDate;
  143.     }

  144.     /** Get the last date of the range.
  145.      * @return the last date of the range
  146.      */
  147.     public AbsoluteDate getMaxDate() {
  148.         return maxDate;
  149.     }

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

  157.     @Override
  158.     public Frame getFrame() {
  159.         return frame;
  160.     }

  161.     @Override
  162.     /** {@inheritDoc} */
  163.     public SpacecraftState basicPropagate(final AbsoluteDate date) {
  164.         final SpacecraftState evaluatedState;

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

  177.         final AttitudeProvider attitudeProvider = getAttitudeProvider();

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

  184.             // Verify if orbit is defined
  185.             if (evaluatedState.isOrbitDefined()) {
  186.                 return new SpacecraftState(evaluatedState.getOrbit(), calculatedAttitude,
  187.                                            evaluatedState.getMass(), evaluatedState.getAdditionalStates());
  188.             } else {
  189.                 return new SpacecraftState(evaluatedState.getAbsPVA(), calculatedAttitude,
  190.                                            evaluatedState.getMass(),  evaluatedState.getAdditionalStates());
  191.             }

  192.         }
  193.     }

  194.     /** {@inheritDoc} */
  195.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  196.         return basicPropagate(date).getOrbit();
  197.     }

  198.     /** {@inheritDoc} */
  199.     protected double getMass(final AbsoluteDate date) {
  200.         return basicPropagate(date).getMass();
  201.     }

  202.     /** {@inheritDoc} */
  203.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame f) {
  204.         return propagate(date).getPVCoordinates(f);
  205.     }

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

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

  219.     /** {@inheritDoc} */
  220.     public SpacecraftState getInitialState() {
  221.         return basicPropagate(getMinDate());
  222.     }

  223.     /** {@inheritDoc} */
  224.     @Override
  225.     public boolean isAdditionalStateManaged(final String name) {

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

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

  236.         return false;

  237.     }

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

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

  249.         /** Serializable UID. */
  250.         private static final long serialVersionUID = 20160115L;

  251.         /** Current state. */
  252.         private SpacecraftState currentState;

  253.         /** List of spacecraft states. */
  254.         private List<SpacecraftState> states;

  255.         /** Interpolation points number. */
  256.         private int interpolationPoints;

  257.         /** Extrapolation threshold. */
  258.         private double extrapolationThreshold;

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

  266.             this.states = states;
  267.             this.interpolationPoints = interpolationPoints;
  268.             this.extrapolationThreshold = extrapolationThreshold;
  269.         }

  270.         /** Get the current state.
  271.          * @return current state
  272.          */
  273.         public SpacecraftState getCurrentState() {
  274.             return currentState;
  275.         }

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

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

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

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

  291.             return currentState.getPVCoordinates(f);

  292.         }

  293.     }

  294. }