1   /* Copyright 2002-2026 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.tle;
18  
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
24  import org.hipparchus.util.FastMath;
25  import org.hipparchus.util.MathArrays;
26  import org.hipparchus.util.MathUtils;
27  import org.hipparchus.util.Pair;
28  import org.orekit.annotation.DefaultDataContext;
29  import org.orekit.attitudes.AttitudeProvider;
30  import org.orekit.attitudes.FieldAttitude;
31  import org.orekit.attitudes.FrameAlignedProvider;
32  import org.orekit.data.DataContext;
33  import org.orekit.errors.OrekitException;
34  import org.orekit.errors.OrekitMessages;
35  import org.orekit.frames.Frame;
36  import org.orekit.orbits.FieldCartesianOrbit;
37  import org.orekit.orbits.FieldOrbit;
38  import org.orekit.propagation.FieldSpacecraftState;
39  import org.orekit.propagation.analytical.FieldAbstractAnalyticalPropagator;
40  import org.orekit.propagation.analytical.tle.generation.TleGenerationAlgorithm;
41  import org.orekit.time.FieldAbsoluteDate;
42  import org.orekit.time.TimeInterval;
43  import org.orekit.time.TimeScale;
44  import org.orekit.utils.FieldPVCoordinates;
45  import org.orekit.utils.PVCoordinates;
46  import org.orekit.utils.drivers.ParameterDriver;
47  import org.orekit.utils.TimeSpanMap;
48  
49  
50  /** This class provides elements to propagate TLE's.
51   * <p>
52   * The models used are SGP4 and SDP4, initially proposed by NORAD as the unique convenient
53   * propagator for TLE's. Inputs and outputs of this propagator are only suited for
54   * NORAD two lines elements sets, since it uses estimations and mean values appropriate
55   * for TLE's only.
56   * </p>
57   * <p>
58   * Deep- or near- space propagator is selected internally according to NORAD recommendations
59   * so that the user has not to worry about the used computation methods. One instance is created
60   * for each TLE (this instance can only be get using {@link #selectExtrapolator(FieldTLE)} method,
61   * and can compute {@link PVCoordinates position and velocity coordinates} at any
62   * time. Maximum accuracy is guaranteed in a 24h range period before and after the provided
63   * TLE epoch (of course this accuracy is not really measurable nor predictable: according to
64   * <a href="https://www.celestrak.com/">CelesTrak</a>, the precision is close to one kilometer
65   * and error won't probably rise above 2 km).
66   * </p>
67   * <p>This implementation is largely inspired from the paper and source code <a
68   * href="https://www.celestrak.com/publications/AIAA/2006-6753/">Revisiting Spacetrack
69   * Report #3</a> and is fully compliant with its results and tests cases.</p>
70   * @author Felix R. Hoots, Ronald L. Roehrich, December 1980 (original fortran)
71   * @author David A. Vallado, Paul Crawford, Richard Hujsak, T.S. Kelso (C++ translation and improvements)
72   * @author Fabien Maussion (java translation)
73   * @author Thomas Paulet (field translation)
74   * @since 11.0
75   * @see FieldTLE
76   * @param <T> type of the field elements
77   */
78  public abstract class FieldTLEPropagator<T extends CalculusFieldElement<T>> extends FieldAbstractAnalyticalPropagator<T> {
79  
80      // CHECKSTYLE: stop VisibilityModifier check
81  
82      /** Initial state. */
83      protected FieldTLE<T> tle;
84  
85      /** UTC time scale. */
86      protected final TimeScale utc;
87  
88      /** final RAAN. */
89      protected T xnode;
90  
91      /** final semi major axis. */
92      protected T a;
93  
94      /** final eccentricity. */
95      protected T e;
96  
97      /** final inclination. */
98      protected T i;
99  
100     /** final periapsis argument. */
101     protected T omega;
102 
103     /** L from SPTRCK #3. */
104     protected T xl;
105 
106     /** original recovered semi major axis. */
107     protected T a0dp;
108 
109     /** original recovered mean motion. */
110     protected T xn0dp;
111 
112     /** cosinus original inclination. */
113     protected T cosi0;
114 
115     /** cos io squared. */
116     protected T theta2;
117 
118     /** sinus original inclination. */
119     protected T sini0;
120 
121     /** common parameter for mean anomaly (M) computation. */
122     protected T xmdot;
123 
124     /** common parameter for periapsis argument (omega) computation. */
125     protected T omgdot;
126 
127     /** common parameter for raan (OMEGA) computation. */
128     protected T xnodot;
129 
130     /** original eccentricity squared. */
131     protected T e0sq;
132     /** 1 - e2. */
133     protected T beta02;
134 
135     /** sqrt (1 - e2). */
136     protected T beta0;
137 
138     /** periapsis, expressed in KM and ALTITUDE. */
139     protected T perige;
140 
141     /** eta squared. */
142     protected T etasq;
143 
144     /** original eccentricity * eta. */
145     protected T eeta;
146 
147     /** s* new value for the contant s. */
148     protected T s4;
149 
150     /** tsi from SPTRCK #3. */
151     protected T tsi;
152 
153     /** eta from SPTRCK #3. */
154     protected T eta;
155 
156     /** coef for SGP C3 computation. */
157     protected T coef;
158 
159     /** coef for SGP C5 computation. */
160     protected T coef1;
161 
162     /** C1 from SPTRCK #3. */
163     protected T c1;
164 
165     /** C2 from SPTRCK #3. */
166     protected T c2;
167 
168     /** C4 from SPTRCK #3. */
169     protected T c4;
170 
171     /** common parameter for raan (OMEGA) computation. */
172     protected T xnodcf;
173 
174     /** 3/2 * C1. */
175     protected T t2cof;
176 
177     // CHECKSTYLE: resume VisibilityModifier check
178 
179     /** TLE frame. */
180     private final Frame teme;
181 
182     /** All TLEs and masses. */
183     private TimeSpanMap<Pair<FieldTLE<T>, T>> tlesAndMasses;
184 
185     /** Driver for the ballistic parameter.
186      * @since 14.0
187      */
188     private final ParameterDriver bStarDriver;
189 
190     /** TLE generation algorithm used when resetting TLE from state. */
191     private TleGenerationAlgorithm generationAlgorithm;
192 
193     /** Protected constructor for derived classes.
194      *
195      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
196      *
197      * @param initialTLE the unique TLE to propagate
198      * @param attitudeProvider provider for attitude computation
199      * @param mass spacecraft mass (kg)
200      * @see #FieldTLEPropagator(FieldTLE, AttitudeProvider, CalculusFieldElement, Frame)
201      * @since 14.0
202      */
203     @DefaultDataContext
204     protected FieldTLEPropagator(final FieldTLE<T> initialTLE, final AttitudeProvider attitudeProvider, final T mass) {
205         this(initialTLE, attitudeProvider, mass, DataContext.getDefault().getFrames().getTEME());
206     }
207 
208     /** Protected constructor for derived classes.
209      * @param initialTLE the unique TLE to propagate
210      * @param attitudeProvider provider for attitude computation
211      * @param mass spacecraft mass (kg)
212      * @param teme the TEME frame to use for propagation.
213      * @since 14.0
214      */
215     protected FieldTLEPropagator(final FieldTLE<T> initialTLE, final AttitudeProvider attitudeProvider, final T mass,
216                                  final Frame teme) {
217         super(initialTLE.getE().getField(), attitudeProvider);
218         setStartDate(initialTLE.getDate());
219         this.utc           = initialTLE.getUtc();
220         initializeTle(initialTLE);
221         this.teme          = teme;
222         this.tlesAndMasses = new TimeSpanMap<>(new Pair<>(tle, mass));
223         this.bStarDriver   = new ParameterDriver(TleGenerationAlgorithm.B_STAR,
224                                                  initialTLE.getBStar().getReal(),
225                                                  TleGenerationAlgorithm.B_STAR_SCALE,
226                                                  Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
227                                                  TimeInterval.UNLIMITED);
228         this.generationAlgorithm = TLEPropagator.getDefaultTleGenerationAlgorithm(initialTLE.toTLE(), this.utc, teme);
229 
230         initializeCommons(tle.getBStar());
231         sxpInitialize(tle.getBStar());
232         // set the initial state
233         final T[] parameters = MathArrays.buildArray(initialTLE.getDate().getField(), 1);
234         parameters[0] = initialTLE.getBStar();
235         final FieldOrbit<T> orbit = propagateOrbit(initialTLE.getDate(), parameters);
236         final FieldAttitude<T> attitude = attitudeProvider.getAttitude(orbit, orbit.getDate(), orbit.getFrame());
237         super.resetInitialState(new FieldSpacecraftState<>(orbit, attitude).withMass(mass));
238     }
239 
240     /** Selects the extrapolator to use with the selected TLE.
241      *
242      * <p>This method uses the {@link DataContext#getDefault() default data context}.
243      *
244      * @param tle the TLE to propagate.
245      * @return the correct propagator.
246      * @param <T> elements type
247      * @see #selectExtrapolator(FieldTLE, Frame)
248      * @since 14.0
249      */
250     @DefaultDataContext
251     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(final FieldTLE<T> tle) {
252         return selectExtrapolator(tle, DataContext.getDefault().getFrames().getTEME());
253     }
254 
255     /** Selects the extrapolator to use with the selected TLE.
256      *
257      *<p>This method uses the {@link DataContext#getDefault() default data context}.
258      *
259      * @param tle the TLE to propagate.
260      * @param teme TEME frame.
261      * @return the correct propagator.
262      * @param <T> elements type
263      * @since 14.0
264      */
265     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(final FieldTLE<T> tle,
266                                                                                                final Frame teme) {
267         return selectExtrapolator(tle, FrameAlignedProvider.of(teme),
268                                   tle.getE().getField().getZero().newInstance(DEFAULT_MASS), teme);
269     }
270 
271     /** Selects the extrapolator to use with the selected TLE.
272      *
273      * <p>This method uses the {@link DataContext#getDefault() default data context}.
274      *
275      * @param tle the TLE to propagate.
276      * @param attitudeProvider provider for attitude computation
277      * @param mass spacecraft mass (kg)
278      * @return the correct propagator.
279      * @param <T> elements type
280      * @see #selectExtrapolator(FieldTLE, AttitudeProvider, CalculusFieldElement, Frame)
281      * @since 14.0
282      */
283     @DefaultDataContext
284     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(final FieldTLE<T> tle,
285                                                                                                final AttitudeProvider attitudeProvider,
286                                                                                                final T mass) {
287         return selectExtrapolator(tle, attitudeProvider, mass,
288                                   DataContext.getDefault().getFrames().getTEME());
289     }
290 
291     /** Selects the extrapolator to use with the selected TLE.
292      *
293      * @param tle the TLE to propagate.
294      * @param attitudeProvider provider for attitude computation
295      * @param mass spacecraft mass (kg)
296      * @param teme the TEME frame to use for propagation.
297      * @return the correct propagator.
298      * @param <T> elements type
299      * @since 14.0
300      */
301     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(
302             final FieldTLE<T> tle,
303             final AttitudeProvider attitudeProvider,
304             final T mass,
305             final Frame teme) {
306 
307         final T xkeOverN = tle.getMeanMotion().multiply(60.0).reciprocal().multiply(TLEConstants.XKE);
308         final T a1 = xkeOverN.multiply(xkeOverN).cbrt();
309         final T cosi0 = FastMath.cos(tle.getI());
310         final T temp1 = cosi0.multiply(cosi0.multiply(3.0)).subtract(1.0).multiply(1.5 * TLEConstants.CK2);
311         final T oMe2  =  tle.getE().multiply(tle.getE()).negate().add(1.0);
312         final T temp = temp1.divide(oMe2.multiply(oMe2.sqrt()));
313         final T delta1 = temp.divide(a1.multiply(a1));
314         final T a0 = a1.multiply(delta1.multiply(delta1.multiply(
315                         delta1.multiply(134.0 / 81.0).add(1.0)).add(TLEConstants.ONE_THIRD)).negate().add(1.0));
316         final T delta0 = temp.divide(a0.multiply(a0));
317 
318         // recover original mean motion :
319         final T xn0dp = tle.getMeanMotion().multiply(60.0).divide(delta0.add(1.0));
320 
321         final FieldTLEPropagator<T> propagator;
322         // Period >= 225 minutes is deep space
323         if (MathUtils.TWO_PI / (xn0dp.multiply(TLEConstants.MINUTES_PER_DAY).getReal()) >= (1.0 / 6.4)) {
324             propagator = new FieldDeepSDP4<>(tle, attitudeProvider, mass, teme);
325         } else {
326             propagator = new FieldSGP4<>(tle, attitudeProvider, mass, teme);
327         }
328 
329         return propagator;
330 
331     }
332 
333     /** Get the Earth gravity coefficient used for TLE propagation.
334      * @return the Earth gravity coefficient.
335      */
336     public static double getMU() {
337         return TLEConstants.MU;
338     }
339 
340     /** Get the extrapolated position and velocity from an initial TLE.
341      * @param date the final date
342      * @param bStar value of the ballistic coefficient to use for propagation
343      * @return the final PVCoordinates
344      */
345     public FieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> date, final T bStar) {
346 
347         sxpPropagate(date.durationFrom(tle.getDate()).divide(60.0), bStar);
348 
349         // Compute PV with previous calculated parameters
350         return computePVCoordinates();
351     }
352 
353     /** Computation of the first commons parameters.
354      * @param bStar value of the ballistic coefficient to use for propagation
355      */
356     private void initializeCommons(final T bStar) {
357 
358         final T zero = tle.getDate().getField().getZero();
359         final T xkeOverN = tle.getMeanMotion().multiply(60.0).reciprocal().multiply(TLEConstants.XKE);
360         final T a1 = xkeOverN.multiply(xkeOverN).cbrt();
361         cosi0 = FastMath.cos(tle.getI());
362         theta2 = cosi0.multiply(cosi0);
363         final T x3thm1 = theta2.multiply(3.0).subtract(1.0);
364         e0sq = tle.getE().square();
365         beta02 = e0sq.negate().add(1.0);
366         beta0 = FastMath.sqrt(beta02);
367         final T tval = x3thm1.multiply(1.5 * TLEConstants.CK2).divide(beta0.multiply(beta02));
368         final T delta1 = tval.divide(a1.multiply(a1));
369         final T a0 = a1.multiply(delta1.multiply(
370                      delta1.multiply(134.0 / 81.0).add(1.0).multiply(delta1).add(TLEConstants.ONE_THIRD)).negate().add(1.0));
371         final T delta0 = tval.divide(a0.multiply(a0));
372 
373         // recover original mean motion and semi-major axis :
374         xn0dp = tle.getMeanMotion().multiply(60.0).divide(delta0.add(1.0));
375         a0dp = a0.divide(delta0.negate().add(1.0));
376 
377         // Values of s and qms2t :
378         s4 = zero.newInstance(TLEConstants.S);  // unmodified value for s
379         T q0ms24 = zero.newInstance(TLEConstants.QOMS2T); // unmodified value for q0ms2T
380 
381         perige = a0dp.multiply(tle.getE().negate().add(1.0)).subtract(TLEConstants.NORMALIZED_EQUATORIAL_RADIUS).multiply(
382                 TLEConstants.EARTH_RADIUS); // perige
383 
384         //  For periapsis below 156 km, the values of s and qoms2t are changed :
385         if (perige.getReal() < 156.0) {
386             if (perige.getReal() <= 98.0) {
387                 s4 = zero.newInstance(20.0);
388             } else {
389                 s4 = perige.subtract(78.0);
390             }
391             final T temp_val = s4.negate().add(120.0).multiply(TLEConstants.NORMALIZED_EQUATORIAL_RADIUS / TLEConstants.EARTH_RADIUS);
392             final T temp_val_squared = temp_val.multiply(temp_val);
393             q0ms24 = temp_val_squared.square();
394             s4 = s4.divide(TLEConstants.EARTH_RADIUS).add(TLEConstants.NORMALIZED_EQUATORIAL_RADIUS); // new value for q0ms2T and s
395         }
396 
397         final T pinv = a0dp.multiply(beta02).reciprocal();
398         final T pinvsq = pinv.square();
399         tsi = a0dp.subtract(s4).reciprocal();
400         eta = a0dp.multiply(tle.getE()).multiply(tsi);
401         etasq = eta.square();
402         eeta = tle.getE().multiply(eta);
403 
404         final T psisq = etasq.negate().add(1.0).abs(); // abs because pow 3.5 needs positive value
405         final T tsi_squared = tsi.multiply(tsi);
406         coef = q0ms24.multiply(tsi_squared.square());
407         coef1 = coef.divide(psisq.pow(3.5));
408 
409         // C2 and C1 coefficients computation :
410         c2 = coef1.multiply(xn0dp).multiply(a0dp.multiply(
411                 etasq.multiply(1.5).add(eeta.multiply(etasq.add(4.0))).add(1.0)).add(
412                 tsi.divide(psisq).multiply(x3thm1).multiply(0.75 * TLEConstants.CK2).multiply(
413                         etasq.multiply(etasq.add(8.0)).multiply(3.0).add(8.0))));
414         c1 = bStar.multiply(c2);
415         sini0 = FastMath.sin(tle.getI());
416 
417         final T x1mth2 = theta2.negate().add(1.0);
418 
419         // C4 coefficient computation :
420         c4 = xn0dp.multiply(coef1).multiply(a0dp).multiply(2.0).multiply(beta02).multiply(
421                 eta.multiply(etasq.multiply(0.5).add(2.0)).add(tle.getE().multiply(etasq.multiply(2.0).add(0.5))).subtract(
422                         tsi.divide(a0dp.multiply(psisq)).multiply(2 * TLEConstants.CK2).multiply(
423                                 x3thm1.multiply(-3).multiply(etasq.multiply(eeta.multiply(-0.5).add(1.5)).add(eeta.multiply(-2.0)).add(1.0)).add(
424                                         x1mth2.multiply(0.75).multiply(etasq.multiply(2.0).subtract(eeta.multiply(etasq.add(1.0)))).multiply(FastMath.cos(tle.getPeriapsisArgument().multiply(2.0)))))));
425 
426         final T theta4 = theta2.multiply(theta2);
427         final T temp1  = pinvsq.multiply(xn0dp).multiply(3 * TLEConstants.CK2);
428         final T temp2  = temp1.multiply(pinvsq).multiply(TLEConstants.CK2);
429         final T temp3  = pinvsq.multiply(pinvsq).multiply(xn0dp).multiply(1.25 * TLEConstants.CK4);
430 
431         // atmospheric and gravitation coefs :(Mdf and OMEGAdf)
432         xmdot = xn0dp.add(
433                 temp1.multiply(0.5).multiply(beta0).multiply(x3thm1)).add(
434                 temp2.multiply(0.0625).multiply(beta0).multiply(
435                         theta2.multiply(78.0).negate().add(13.0).add(theta4.multiply(137.0))));
436 
437         final T x1m5th = theta2.multiply(5.0).negate().add(1.0);
438 
439         omgdot = temp1.multiply(-0.5).multiply(x1m5th).add(
440                 temp2.multiply(0.0625).multiply(theta2.multiply(114.0).negate().add(
441                         theta4.multiply(395.0)).add(7.0))).add(
442                 temp3.multiply(theta2.multiply(36.0).negate().add(theta4.multiply(49.0)).add(3.0)));
443 
444         final T xhdot1 = temp1.negate().multiply(cosi0);
445 
446         xnodot = xhdot1.add(temp2.multiply(0.5).multiply(theta2.multiply(19.0).negate().add(4.0)).add(
447                 temp3.multiply(2.0).multiply(theta2.multiply(7.0).negate().add(3.0))).multiply(cosi0));
448         xnodcf = beta02.multiply(xhdot1).multiply(c1).multiply(3.5);
449         t2cof = c1.multiply(1.5);
450 
451     }
452 
453     /** Retrieves the position and velocity.
454      * @return the computed PVCoordinates.
455      */
456     private FieldPVCoordinates<T> computePVCoordinates() {
457 
458         final T zero = tle.getDate().getField().getZero();
459         // Long period periodics
460         final T axn = e.multiply(FastMath.cos(omega));
461         T temp = a.multiply(e.multiply(e).negate().add(1.0)).reciprocal();
462         final T xlcof = sini0.multiply(0.125 * TLEConstants.A3OVK2).multiply(
463                 cosi0.multiply(5.0).add(3.0).divide(cosi0.add(1.0)));
464         final T aycof = sini0.multiply(0.25 * TLEConstants.A3OVK2);
465         final T xll   = temp.multiply(xlcof).multiply(axn);
466         final T aynl  = temp.multiply(aycof);
467         final T xlt   = xl.add(xll);
468         final T ayn   = e.multiply(FastMath.sin(omega)).add(aynl);
469         final T elsq  = axn.square().add(ayn.square());
470         final T capu  = MathUtils.normalizeAngle(xlt.subtract(xnode), zero.getPi());
471         T epw    = capu;
472         T ecosE  = zero;
473         T esinE  = zero;
474         T sinEPW = zero;
475         T cosEPW = zero;
476 
477         // Dundee changes:  items dependent on cosio get recomputed:
478         final T cosi0Sq = cosi0.square();
479         final T x3thm1  = cosi0Sq.multiply(3.0).subtract(1.0);
480         final T x1mth2  = cosi0Sq.negate().add(1.0);
481         final T x7thm1  = cosi0Sq.multiply(7.0).subtract(1.0);
482 
483         if (e.getReal() > (1 - 1e-6)) {
484             throw new OrekitException(OrekitMessages.TOO_LARGE_ECCENTRICITY_FOR_PROPAGATION_MODEL, e.getReal());
485         }
486 
487         // Solve Kepler's' Equation.
488         final double newtonRaphsonEpsilon = 1e-12;
489         for (int j = 0; j < 10; j++) {
490 
491             boolean doSecondOrderNewtonRaphson = true;
492 
493             sinEPW = FastMath.sin( epw);
494             cosEPW = FastMath.cos( epw);
495             ecosE  = axn.multiply(cosEPW).add(ayn.multiply(sinEPW));
496             esinE  = axn.multiply(sinEPW).subtract(ayn.multiply(cosEPW));
497             final T f = capu.subtract(epw).add(esinE);
498             if (FastMath.abs(f.getReal()) < newtonRaphsonEpsilon) {
499                 break;
500             }
501             final T fdot = ecosE.negate().add(1.0);
502             T delta_epw = f.divide(fdot);
503             if (j == 0) {
504                 final T maxNewtonRaphson = e.abs().multiply(1.25);
505                 doSecondOrderNewtonRaphson = false;
506                 if (delta_epw.getReal() > maxNewtonRaphson.getReal()) {
507                     delta_epw = maxNewtonRaphson;
508                 } else if (delta_epw.getReal() < -maxNewtonRaphson.getReal()) {
509                     delta_epw = maxNewtonRaphson.negate();
510                 } else {
511                     doSecondOrderNewtonRaphson = true;
512                 }
513             }
514             if (doSecondOrderNewtonRaphson) {
515                 delta_epw = f.divide(fdot.add(esinE.multiply(0.5).multiply(delta_epw)));
516             }
517             epw = epw.add(delta_epw);
518         }
519 
520         // Short period preliminary quantities
521         temp = elsq.negate().add(1.0);
522         final T pl = a.multiply(temp);
523         final T r  = a.multiply(ecosE.negate().add(1.0));
524         T temp2 = a.divide(r);
525         final T betal = FastMath.sqrt(temp);
526         temp = esinE.divide(betal.add(1.0));
527         final T cosu  = temp2.multiply(cosEPW.subtract(axn).add(ayn.multiply(temp)));
528         final T sinu  = temp2.multiply(sinEPW.subtract(ayn).subtract(axn.multiply(temp)));
529         final T u     = FastMath.atan2(sinu, cosu);
530         final T sin2u = sinu.multiply(cosu).multiply(2.0);
531         final T cos2u = cosu.multiply(cosu).multiply(2.0).subtract(1.0);
532         final T temp1 = pl.reciprocal().multiply(TLEConstants.CK2);
533         temp2         = temp1.divide(pl);
534 
535         // Update for short periodics
536         final T rk = r.multiply(temp2.multiply(betal).multiply(x3thm1).multiply(-1.5).add(1.0)).add(
537                      temp1.multiply(x1mth2).multiply(cos2u).multiply(0.5));
538         final T uk = u.subtract(temp2.multiply(x7thm1).multiply(sin2u).multiply(0.25));
539         final T xnodek = xnode.add(temp2.multiply(cosi0).multiply(sin2u).multiply(1.5));
540         final T xinck = i.add(temp2.multiply(cosi0).multiply(sini0).multiply(cos2u).multiply(1.5));
541 
542         // Orientation vectors
543         final T sinuk  = FastMath.sin(uk);
544         final T cosuk  = FastMath.cos(uk);
545         final T sinik  = FastMath.sin(xinck);
546         final T cosik  = FastMath.cos(xinck);
547         final T sinnok = FastMath.sin(xnodek);
548         final T cosnok = FastMath.cos(xnodek);
549         final T xmx    = sinnok.negate().multiply(cosik);
550         final T xmy    = cosnok.multiply(cosik);
551         final T ux     = xmx.multiply(sinuk).add(cosnok.multiply(cosuk));
552         final T uy     = xmy.multiply(sinuk).add(sinnok.multiply(cosuk));
553         final T uz     = sinik.multiply(sinuk);
554 
555         // Position and velocity
556         final T cr = rk.multiply(1000 * TLEConstants.EARTH_RADIUS);
557         final FieldVector3D<T> pos = new FieldVector3D<>(cr.multiply(ux), cr.multiply(uy), cr.multiply(uz));
558 
559         final T sqrtA  = FastMath.sqrt(a);
560         final T rdot   = sqrtA.multiply(esinE.divide(r)).multiply(TLEConstants.XKE);
561         final T rfdot  = FastMath.sqrt(pl).divide(r).multiply(TLEConstants.XKE);
562         final T xn     = a.multiply(sqrtA).reciprocal().multiply(TLEConstants.XKE);
563         final T rdotk  = rdot.subtract(xn.multiply(temp1).multiply(x1mth2).multiply(sin2u));
564         final T rfdotk = rfdot.add(xn.multiply(temp1).multiply(x1mth2.multiply(cos2u).add(x3thm1.multiply(1.5))));
565         final T vx     = xmx.multiply(cosuk).subtract(cosnok.multiply(sinuk));
566         final T vy     = xmy.multiply(cosuk).subtract(sinnok.multiply(sinuk));
567         final T vz     = sinik.multiply(cosuk);
568 
569         final double cv = 1000.0 * TLEConstants.EARTH_RADIUS / 60.0;
570         final FieldVector3D<T> vel = new FieldVector3D<>(rdotk.multiply(ux).add(rfdotk.multiply(vx)).multiply(cv),
571                                                           rdotk.multiply(uy).add(rfdotk.multiply(vy)).multiply(cv),
572                                                           rdotk.multiply(uz).add(rfdotk.multiply(vz)).multiply(cv));
573         return new FieldPVCoordinates<>(pos, vel);
574 
575     }
576 
577     /** {@inheritDoc} */
578     @Override
579     public List<ParameterDriver> getParametersDrivers() {
580         return Collections.singletonList(bStarDriver);
581     }
582 
583     /** Initialization proper to each propagator (SGP or SDP).
584      * @param bStar value of the ballistic coefficient to use for propagation
585      */
586     protected abstract void sxpInitialize(T bStar);
587 
588     /** Propagation proper to each propagator (SGP or SDP).
589      * @param t the offset from initial epoch (min)
590      * @param bStar value of the ballistic coefficient to use for propagation
591      */
592     protected abstract void sxpPropagate(T t, T bStar);
593 
594     /** {@inheritDoc}
595      * <p>
596      * For TLE propagator, calling this method is only recommended
597      * for covariance propagation when the new <code>state</code>
598      * differs from the previous one by only adding the additional
599      * state containing the derivatives.
600      * </p>
601      */
602     public void resetInitialState(final FieldSpacecraftState<T> state) {
603         super.resetInitialState(state);
604         resetTle(state);
605         tlesAndMasses = new TimeSpanMap<>(new Pair<>(tle, state.getMass()));
606     }
607 
608     /** {@inheritDoc} */
609     protected void resetIntermediateState(final FieldSpacecraftState<T> state, final boolean forward) {
610         resetTle(state);
611         final Pair<FieldTLE<T>, T> tleAndMass = new Pair<>(tle, state.getMass());
612         if (forward) {
613             tlesAndMasses.addValidAfter(tleAndMass, state.getDate().toAbsoluteDate(), false);
614         } else {
615             tlesAndMasses.addValidBefore(tleAndMass, state.getDate().toAbsoluteDate(), false);
616         }
617         stateChanged(state);
618     }
619 
620     /** Set the TLE generation algorithm used when resetting TLE from state.
621      * @param tleGenerationAlgorithm TLE generation algorithm
622      * @since 14.0
623      */
624     public void setTleGenerationAlgorithm(final TleGenerationAlgorithm tleGenerationAlgorithm) {
625         this.generationAlgorithm = tleGenerationAlgorithm;
626     }
627 
628     /** Reset internal TLE from a SpacecraftState.
629      * @param state spacecraft state on which to base new TLE
630      */
631     private void resetTle(final FieldSpacecraftState<T> state) {
632         final FieldTLE<T> newTle = generationAlgorithm.generate(state, tle);
633         initializeTle(newTle);
634     }
635 
636     /** Initialize internal TLE.
637      * @param newTle tle to replace current one
638      */
639     private void initializeTle(final FieldTLE<T> newTle) {
640         tle = newTle;
641         initializeCommons(newTle.getBStar());
642         sxpInitialize(newTle.getBStar());
643     }
644 
645     /** {@inheritDoc} */
646     protected T getMass(final FieldAbsoluteDate<T> date) {
647         return tlesAndMasses.get(date.toAbsoluteDate()).getValue();
648     }
649 
650     /** {@inheritDoc} */
651     public FieldOrbit<T> propagateOrbit(final FieldAbsoluteDate<T> date, final T[] parameters) {
652         final FieldTLE<T> closestTle = tlesAndMasses.get(date.toAbsoluteDate()).getKey();
653         if (parameters[0] != closestTle.getBStar() || !tle.equals(closestTle)) {
654             initializeTle(closestTle);
655         }
656         final T mu = date.getField().getZero().newInstance(TLEConstants.MU);
657         return new FieldCartesianOrbit<>(getPVCoordinates(date, parameters[0]), teme, date, mu);
658     }
659 
660     /** Get the underlying TLE.
661      * If there has been calls to #resetInitialState or #resetIntermediateState,
662      * it will not be the same as given to the constructor.
663      * @return underlying TLE
664      */
665     public FieldTLE<T> getTLE() {
666         return tle;
667     }
668 
669     /** {@inheritDoc} */
670     public Frame getFrame() {
671         return teme;
672     }
673 
674 }