1   /* Contributed in the public domain.
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  
19  import java.util.Collection;
20  import java.util.Map.Entry;
21  import java.util.NavigableMap;
22  import java.util.TreeMap;
23  
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.TimeStampedPVCoordinates;
35  
36  /**
37   * A {@link BoundedPropagator} that covers a larger time span from several constituent
38   * propagators that cover shorter time spans.
39   *
40   * @author Evan Ward
41   * @see #AggregateBoundedPropagator(Collection)
42   */
43  public class AggregateBoundedPropagator extends AbstractAnalyticalPropagator
44          implements BoundedPropagator {
45  
46      /** Constituent propagators. */
47      private final NavigableMap<AbsoluteDate, BoundedPropagator> propagators;
48  
49      /**
50       * Create a propagator by concatenating several {@link BoundedPropagator}s.
51       *
52       * @param propagators that provide the backing data for this instance. There must be
53       *                    at least one propagator in the collection. If there are gaps
54       *                    between the {@link BoundedPropagator#getMaxDate()} of one
55       *                    propagator and the {@link BoundedPropagator#getMinDate()} of the
56       *                    next propagator an exception may be thrown by any method of this
57       *                    class at any time. If there are overlaps between the the {@link
58       *                    BoundedPropagator#getMaxDate()} of one propagator and the {@link
59       *                    BoundedPropagator#getMinDate()} of the next propagator then the
60       *                    propagator with the latest {@link BoundedPropagator#getMinDate()}
61       *                    is used.
62       */
63      public AggregateBoundedPropagator(
64              final Collection<? extends BoundedPropagator> propagators) {
65          super(defaultAttitude(propagators));
66          this.propagators = new TreeMap<>();
67          for (final BoundedPropagator propagator : propagators) {
68              this.propagators.put(propagator.getMinDate(), propagator);
69          }
70          super.resetInitialState(
71                  this.propagators.firstEntry().getValue().getInitialState());
72      }
73  
74      /**
75       * Helper function for the constructor.
76       * @param propagators to consider.
77       * @return attitude provider.
78       */
79      private static AttitudeProvider defaultAttitude(
80              final Collection<? extends BoundedPropagator> propagators) {
81          // this check is needed here because it can't be before the super() call in the
82          // constructor.
83          if (propagators.isEmpty()) {
84              throw new OrekitException(OrekitMessages.NOT_ENOUGH_PROPAGATORS);
85          }
86          return new InertialProvider(propagators.iterator().next().getFrame());
87      }
88  
89      @Override
90      protected SpacecraftState basicPropagate(final AbsoluteDate date) {
91          // #589 override this method for a performance benefit,
92          // getPropagator(date).propagate(date) is only called once
93  
94          // do propagation
95          final SpacecraftState state = getPropagator(date).propagate(date);
96  
97          // evaluate attitude
98          final Attitude attitude =
99                  getAttitudeProvider().getAttitude(this, date, state.getFrame());
100 
101         // build raw state
102         if (state.isOrbitDefined()) {
103             return new SpacecraftState(
104                     state.getOrbit(), attitude, state.getMass(),
105                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
106         } else {
107             return new SpacecraftState(
108                     state.getAbsPVA(), attitude, state.getMass(),
109                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
110         }
111     }
112 
113     @Override
114     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date,
115                                                      final Frame frame) {
116         return getPropagator(date).getPVCoordinates(date, frame);
117     }
118 
119     @Override
120     protected Orbit propagateOrbit(final AbsoluteDate date) {
121         return getPropagator(date).propagate(date).getOrbit();
122     }
123 
124     @Override
125     public AbsoluteDate getMinDate() {
126         return propagators.firstEntry().getValue().getMinDate();
127     }
128 
129     @Override
130     public AbsoluteDate getMaxDate() {
131         return propagators.lastEntry().getValue().getMaxDate();
132     }
133 
134     @Override
135     protected double getMass(final AbsoluteDate date) {
136         return getPropagator(date).propagate(date).getMass();
137     }
138 
139     @Override
140     public SpacecraftState getInitialState() {
141         return propagators.firstEntry().getValue().getInitialState();
142     }
143 
144     @Override
145     protected void resetIntermediateState(final SpacecraftState state,
146                                           final boolean forward) {
147         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
148     }
149 
150     @Override
151     public void resetInitialState(final SpacecraftState state) {
152         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
153     }
154 
155     /**
156      * Get the propagator to use for the given date.
157      *
158      * @param date of query
159      * @return propagator to use on date.
160      */
161     private BoundedPropagator getPropagator(final AbsoluteDate date) {
162         final Entry<AbsoluteDate, BoundedPropagator> entry = propagators.floorEntry(date);
163         if (entry != null) {
164             return entry.getValue();
165         } else {
166             // let the first propagator throw the exception
167             return propagators.firstEntry().getValue();
168         }
169     }
170 
171 }