1   /* Copyright Luc Maisonobe
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.files.sp3;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.hipparchus.analysis.interpolation.HermiteInterpolator;
24  import org.orekit.attitudes.AttitudeProvider;
25  import org.orekit.attitudes.FrameAlignedProvider;
26  import org.orekit.files.general.EphemerisFile;
27  import org.orekit.files.general.EphemerisSegmentPropagator;
28  import org.orekit.frames.Frame;
29  import org.orekit.propagation.BoundedPropagator;
30  import org.orekit.propagation.SpacecraftState;
31  import org.orekit.time.AbsoluteDate;
32  import org.orekit.time.ClockModel;
33  import org.orekit.time.ClockOffset;
34  import org.orekit.time.SampledClockModel;
35  import org.orekit.utils.CartesianDerivativesFilter;
36  import org.orekit.utils.SortedListTrimmer;
37  
38  /** One segment of an {@link SP3Ephemeris}.
39   * @author Thomas Neidhart
40   * @author Evan Ward
41   * @author Luc Maisonobe
42   * @since 12.0
43   */
44  public class SP3Segment implements EphemerisFile.EphemerisSegment<SP3Coordinate> {
45  
46      /** Standard gravitational parameter in m³ / s². */
47      private final double mu;
48  
49      /** Reference frame. */
50      private final Frame frame;
51  
52      /** Number of points to use for interpolation. */
53      private final int interpolationSamples;
54  
55      /** Available derivatives. */
56      private final CartesianDerivativesFilter filter;
57  
58      /** Ephemeris Data. */
59      private final List<SP3Coordinate> coordinates;
60  
61      /** Simple constructor.
62       * @param mu standard gravitational parameter to use for creating
63       * {@link org.orekit.orbits.Orbit Orbits} from the ephemeris data.
64       * @param frame reference frame
65       * @param interpolationSamples number of points to use for interpolation
66       * @param filter available derivatives
67       */
68      public SP3Segment(final double mu, final Frame frame,
69                        final int interpolationSamples, final CartesianDerivativesFilter filter) {
70          this.mu                   = mu;
71          this.frame                = frame;
72          this.interpolationSamples = interpolationSamples;
73          this.filter               = filter;
74          this.coordinates          = new ArrayList<>();
75      }
76  
77      /** Extract the clock model.
78       * @return extracted clock model
79       * @since 12.1
80       */
81      public ClockModel extractClockModel() {
82          final List<ClockOffset> sample = new ArrayList<>(coordinates.size());
83          coordinates.forEach(c -> {
84              final AbsoluteDate date   = c.getDate();
85              final double       offset = c.getClockCorrection();
86              final double       rate   = filter.getMaxOrder() > 0 ? c.getClockRateChange() : Double.NaN;
87              sample.add(new ClockOffset(date, offset, rate, Double.NaN));
88          });
89          return new SampledClockModel(sample, interpolationSamples);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public double getMu() {
95          return mu;
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public AbsoluteDate getStart() {
101         return coordinates.get(0).getDate();
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public AbsoluteDate getStop() {
107         return coordinates.get(coordinates.size() - 1).getDate();
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public Frame getFrame() {
113         return frame;
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public int getInterpolationSamples() {
119         return interpolationSamples;
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public CartesianDerivativesFilter getAvailableDerivatives() {
125         return filter;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public List<SP3Coordinate> getCoordinates() {
131         return Collections.unmodifiableList(this.coordinates);
132     }
133 
134     /** Adds a new P/V coordinate.
135      * @param coord the P/V coordinate of the satellite
136      */
137     public void addCoordinate(final SP3Coordinate coord) {
138         coordinates.add(coord);
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public BoundedPropagator getPropagator() {
144         return new PropagatorWithClock(new FrameAlignedProvider(getInertialFrame()));
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public BoundedPropagator getPropagator(final AttitudeProvider attitudeProvider) {
150         return new PropagatorWithClock(attitudeProvider);
151     }
152 
153     /** Propagator including clock.
154      * @since 12.1
155      */
156     private class PropagatorWithClock extends EphemerisSegmentPropagator<SP3Coordinate> {
157 
158         /** Trimmer for coordinates list. */
159         private final SortedListTrimmer trimmer;
160 
161         /** Simple constructor.
162          * @param attitudeProvider attitude porovider
163          */
164         PropagatorWithClock(final AttitudeProvider attitudeProvider) {
165             super(SP3Segment.this, attitudeProvider);
166             this.trimmer = new SortedListTrimmer(getInterpolationSamples());
167         }
168 
169         /** {@inheritDoc} */
170         @Override
171         protected SpacecraftState updateAdditionalStates(final SpacecraftState original) {
172 
173             final HermiteInterpolator interpolator = new HermiteInterpolator();
174 
175             // Fill interpolator with sample
176             trimmer.
177                 getNeighborsSubList(original.getDate(), coordinates).
178                 forEach(c -> {
179                     final double deltaT = c.getDate().durationFrom(original.getDate());
180                     if (filter.getMaxOrder() < 1) {
181                         // we use only clock offset
182                         interpolator.addSamplePoint(deltaT,
183                                                     new double[] { c.getClockCorrection() });
184                     } else {
185                         // we use both clock offset and clock rate
186                         interpolator.addSamplePoint(deltaT,
187                                                     new double[] { c.getClockCorrection() },
188                                                     new double[] { c.getClockRateChange() });
189                     }
190                 });
191 
192             // perform interpolation (we get derivatives even if we used only clock offset)
193             final double[][] derivatives = interpolator.derivatives(0.0, 1);
194 
195             // add the clock offset and its first derivative
196             return super.updateAdditionalStates(original).
197                 addAdditionalState(SP3Utils.CLOCK_ADDITIONAL_STATE, derivatives[0]).
198                 addAdditionalStateDerivative(SP3Utils.CLOCK_ADDITIONAL_STATE, derivatives[1]);
199 
200         }
201 
202     }
203 
204 }