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(), state.getAdditionalStates());
105         } else {
106             return new SpacecraftState(
107                     state.getAbsPVA(), attitude, state.getMass(), state.getAdditionalStates());
108         }
109     }
110 
111     @Override
112     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date,
113                                                      final Frame frame) {
114         return getPropagator(date).getPVCoordinates(date, frame);
115     }
116 
117     @Override
118     protected Orbit propagateOrbit(final AbsoluteDate date) {
119         return getPropagator(date).propagate(date).getOrbit();
120     }
121 
122     @Override
123     public AbsoluteDate getMinDate() {
124         return propagators.firstEntry().getValue().getMinDate();
125     }
126 
127     @Override
128     public AbsoluteDate getMaxDate() {
129         return propagators.lastEntry().getValue().getMaxDate();
130     }
131 
132     @Override
133     protected double getMass(final AbsoluteDate date) {
134         return getPropagator(date).propagate(date).getMass();
135     }
136 
137     @Override
138     public SpacecraftState getInitialState() {
139         return propagators.firstEntry().getValue().getInitialState();
140     }
141 
142     @Override
143     protected void resetIntermediateState(final SpacecraftState state,
144                                           final boolean forward) {
145         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
146     }
147 
148     @Override
149     public void resetInitialState(final SpacecraftState state) {
150         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
151     }
152 
153     /**
154      * Get the propagator to use for the given date.
155      *
156      * @param date of query
157      * @return propagator to use on date.
158      */
159     private BoundedPropagator getPropagator(final AbsoluteDate date) {
160         final Entry<AbsoluteDate, BoundedPropagator> entry = propagators.floorEntry(date);
161         if (entry != null) {
162             return entry.getValue();
163         } else {
164             // let the first propagator throw the exception
165             return propagators.firstEntry().getValue();
166         }
167     }
168 
169 }