Ephemeris.java

  1. /* Copyright 2002-2020 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.annotation.DefaultDataContext;
  26. import org.orekit.attitudes.Attitude;
  27. import org.orekit.attitudes.AttitudeProvider;
  28. import org.orekit.data.DataContext;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.frames.Frame;
  32. import org.orekit.orbits.Orbit;
  33. import org.orekit.propagation.BoundedPropagator;
  34. import org.orekit.propagation.Propagator;
  35. import org.orekit.propagation.SpacecraftState;
  36. import org.orekit.time.AbsoluteDate;
  37. import org.orekit.utils.ImmutableTimeStampedCache;
  38. import org.orekit.utils.PVCoordinatesProvider;
  39. import org.orekit.utils.TimeStampedPVCoordinates;

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

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

  53.      /** First date in range. */
  54.     private final AbsoluteDate minDate;

  55.     /** Last date in range. */
  56.     private final AbsoluteDate maxDate;

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

  59.     /** Reference frame. */
  60.     private final Frame frame;

  61.     /** Names of the additional states. */
  62.     private final String[] additional;

  63.     /** Local PV Provider used for computing attitude. **/
  64.     private LocalPVProvider pvProvider;

  65.     /** Thread-safe cache. */
  66.     private final transient ImmutableTimeStampedCache<SpacecraftState> cache;

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

  88.     /** Constructor with tabulated states.
  89.      *
  90.      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  91.      *
  92.      * @param states tabulates states
  93.      * @param interpolationPoints number of points to use in interpolation
  94.      * @param extrapolationThreshold the largest time difference in seconds between
  95.      * the start or stop boundary of the ephemeris bounds to be doing extrapolation
  96.      * @exception MathIllegalArgumentException if the number of states is smaller than
  97.      * the number of points to use in interpolation
  98.      * @since 9.0
  99.      * @see #Ephemeris(List, int, double, AttitudeProvider)
  100.      */
  101.     @DefaultDataContext
  102.     public Ephemeris(final List<SpacecraftState> states, final int interpolationPoints,
  103.                      final double extrapolationThreshold)
  104.         throws MathIllegalArgumentException {
  105.         this(states, interpolationPoints, extrapolationThreshold,
  106.                 Propagator.getDefaultLaw(DataContext.getDefault().getFrames()));
  107.     }

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

  123.         super(attitudeProvider);

  124.         if (states.size() < interpolationPoints) {
  125.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INSUFFICIENT_DIMENSION,
  126.                                                    states.size(), interpolationPoints);
  127.         }

  128.         final SpacecraftState s0 = states.get(0);
  129.         minDate = s0.getDate();
  130.         maxDate = states.get(states.size() - 1).getDate();
  131.         frame = s0.getFrame();

  132.         final Set<String> names0 = s0.getAdditionalStates().keySet();
  133.         additional = names0.toArray(new String[names0.size()]);

  134.         // check all states handle the same additional states
  135.         for (final SpacecraftState state : states) {
  136.             s0.ensureCompatibleAdditionalStates(state);
  137.         }

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

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

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

  143.         this.extrapolationThreshold = extrapolationThreshold;
  144.     }

  145.     /** Get the first date of the range.
  146.      * @return the first date of the range
  147.      */
  148.     public AbsoluteDate getMinDate() {
  149.         return minDate;
  150.     }

  151.     /** Get the last date of the range.
  152.      * @return the last date of the range
  153.      */
  154.     public AbsoluteDate getMaxDate() {
  155.         return maxDate;
  156.     }

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

  164.     @Override
  165.     public Frame getFrame() {
  166.         return frame;
  167.     }

  168.     @Override
  169.     /** {@inheritDoc} */
  170.     public SpacecraftState basicPropagate(final AbsoluteDate date) {
  171.         final SpacecraftState evaluatedState;

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

  184.         final AttitudeProvider attitudeProvider = getAttitudeProvider();

  185.         if (attitudeProvider == null) {
  186.             return evaluatedState;
  187.         } else {
  188.             pvProvider.setCurrentState(evaluatedState);
  189.             final Attitude calculatedAttitude = attitudeProvider.getAttitude(pvProvider, date,
  190.                                                                              evaluatedState.getFrame());

  191.             // Verify if orbit is defined
  192.             if (evaluatedState.isOrbitDefined()) {
  193.                 return new SpacecraftState(evaluatedState.getOrbit(), calculatedAttitude,
  194.                                            evaluatedState.getMass(), evaluatedState.getAdditionalStates());
  195.             } else {
  196.                 return new SpacecraftState(evaluatedState.getAbsPVA(), calculatedAttitude,
  197.                                            evaluatedState.getMass(),  evaluatedState.getAdditionalStates());
  198.             }

  199.         }
  200.     }

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

  205.     /** {@inheritDoc} */
  206.     protected double getMass(final AbsoluteDate date) {
  207.         return basicPropagate(date).getMass();
  208.     }

  209.     /** {@inheritDoc} */
  210.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame f) {
  211.         return propagate(date).getPVCoordinates(f);
  212.     }

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

  222.     /** {@inheritDoc} */
  223.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  224.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  225.     }

  226.     /** {@inheritDoc} */
  227.     public SpacecraftState getInitialState() {
  228.         return basicPropagate(getMinDate());
  229.     }

  230.     /** {@inheritDoc} */
  231.     @Override
  232.     public boolean isAdditionalStateManaged(final String name) {

  233.         // the additional state may be managed by a specific provider in the base class
  234.         if (super.isAdditionalStateManaged(name)) {
  235.             return true;
  236.         }

  237.         // the additional state may be managed in the states sample
  238.         for (final String a : additional) {
  239.             if (a.equals(name)) {
  240.                 return true;
  241.             }
  242.         }

  243.         return false;

  244.     }

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

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

  256.         /** Serializable UID. */
  257.         private static final long serialVersionUID = 20160115L;

  258.         /** Current state. */
  259.         private SpacecraftState currentState;

  260.         /** List of spacecraft states. */
  261.         private List<SpacecraftState> states;

  262.         /** Interpolation points number. */
  263.         private int interpolationPoints;

  264.         /** Extrapolation threshold. */
  265.         private double extrapolationThreshold;

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

  273.             this.states = states;
  274.             this.interpolationPoints = interpolationPoints;
  275.             this.extrapolationThreshold = extrapolationThreshold;
  276.         }

  277.         /** Get the current state.
  278.          * @return current state
  279.          */
  280.         public SpacecraftState getCurrentState() {
  281.             return currentState;
  282.         }

  283.         /** Set the current state.
  284.          * @param state state to set
  285.          */
  286.         public void setCurrentState(final SpacecraftState state) {
  287.             this.currentState = state;
  288.         }

  289.         /** {@inheritDoc} */
  290.         public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame f) {
  291.             final double dt = getCurrentState().getDate().durationFrom(date);
  292.             final double closeEnoughTimeInSec = 1e-9;

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

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

  298.             return currentState.getPVCoordinates(f);

  299.         }

  300.     }

  301. }