TLEPropagator.java

  1. /* Copyright 2002-2013 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  19. import org.apache.commons.math3.util.FastMath;
  20. import org.apache.commons.math3.util.MathUtils;
  21. import org.orekit.attitudes.AttitudeProvider;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.errors.PropagationException;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.frames.FramesFactory;
  27. import org.orekit.orbits.CartesianOrbit;
  28. import org.orekit.orbits.Orbit;
  29. import org.orekit.propagation.SpacecraftState;
  30. import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.PVCoordinates;


  33. /** This class provides elements to propagate TLE's.
  34.  * <p>
  35.  * The models used are SGP4 and SDP4, initially proposed by NORAD as the unique convenient
  36.  * propagator for TLE's. Inputs and outputs of this propagator are only suited for
  37.  * NORAD two lines elements sets, since it uses estimations and mean values appropriate
  38.  * for TLE's only.
  39.  * </p>
  40.  * <p>
  41.  * Deep- or near- space propagator is selected internally according to NORAD recommendations
  42.  * so that the user has not to worry about the used computation methods. One instance is created
  43.  * for each TLE (this instance can only be get using {@link #selectExtrapolator(TLE)} method,
  44.  * and can compute {@link PVCoordinates position and velocity coordinates} at any
  45.  * time. Maximum accuracy is guaranteed in a 24h range period before and after the provided
  46.  * TLE epoch (of course this accuracy is not really measurable nor predictable: according to
  47.  * <a href="http://www.celestrak.com/">CelesTrak</a>, the precision is close to one kilometer
  48.  * and error won't probably rise above 2 km).
  49.  * </p>
  50.  * <p>This implementation is largely inspired from the paper and source code <a
  51.  * href="http://www.celestrak.com/publications/AIAA/2006-6753/">Revisiting Spacetrack
  52.  * Report #3</a> and is fully compliant with its results and tests cases.</p>
  53.  * @author Felix R. Hoots, Ronald L. Roehrich, December 1980 (original fortran)
  54.  * @author David A. Vallado, Paul Crawford, Richard Hujsak, T.S. Kelso (C++ translation and improvements)
  55.  * @author Fabien Maussion (java translation)
  56.  * @see TLE
  57.  */
  58. public abstract class TLEPropagator extends AbstractAnalyticalPropagator {

  59.     // CHECKSTYLE: stop VisibilityModifierCheck

  60.     /** Initial state. */
  61.     protected final TLE tle;

  62.     /** final RAAN. */
  63.     protected double xnode;

  64.     /** final semi major axis. */
  65.     protected double a;

  66.     /** final eccentricity. */
  67.     protected double e;

  68.     /** final inclination. */
  69.     protected double i;

  70.     /** final perigee argument. */
  71.     protected double omega;

  72.     /** L from SPTRCK #3. */
  73.     protected double xl;

  74.     /** original recovered semi major axis. */
  75.     protected double a0dp;

  76.     /** original recovered mean motion. */
  77.     protected double xn0dp;

  78.     /** cosinus original inclination. */
  79.     protected double cosi0;

  80.     /** cos io squared. */
  81.     protected double theta2;

  82.     /** sinus original inclination. */
  83.     protected double sini0;

  84.     /** common parameter for mean anomaly (M) computation. */
  85.     protected double xmdot;

  86.     /** common parameter for perigee argument (omega) computation. */
  87.     protected double omgdot;

  88.     /** common parameter for raan (OMEGA) computation. */
  89.     protected double xnodot;

  90.     /** original eccentricity squared. */
  91.     protected double e0sq;
  92.     /** 1 - e2. */
  93.     protected double beta02;

  94.     /** sqrt (1 - e2). */
  95.     protected double beta0;

  96.     /** perigee, expressed in KM and ALTITUDE. */
  97.     protected double perige;

  98.     /** eta squared. */
  99.     protected double etasq;

  100.     /** original eccentricity * eta. */
  101.     protected double eeta;

  102.     /** s* new value for the contant s. */
  103.     protected double s4;

  104.     /** tsi from SPTRCK #3. */
  105.     protected double tsi;

  106.     /** eta from SPTRCK #3. */
  107.     protected double eta;

  108.     /** coef for SGP C3 computation. */
  109.     protected double coef;

  110.     /** coef for SGP C5 computation. */
  111.     protected double coef1;

  112.     /** C1 from SPTRCK #3. */
  113.     protected double c1;

  114.     /** C2 from SPTRCK #3. */
  115.     protected double c2;

  116.     /** C4 from SPTRCK #3. */
  117.     protected double c4;

  118.     /** common parameter for raan (OMEGA) computation. */
  119.     protected double xnodcf;

  120.     /** 3/2 * C1. */
  121.     protected double t2cof;

  122.     // CHECKSTYLE: resume VisibilityModifierCheck

  123.     /** TLE frame. */
  124.     private final Frame teme;

  125.     /** Spacecraft mass (kg). */
  126.     private final double mass;

  127.     /** Protected constructor for derived classes.
  128.      * @param initialTLE the unique TLE to propagate
  129.      * @param attitudeProvider provider for attitude computation
  130.      * @param mass spacecraft mass (kg)
  131.      * @exception OrekitException if some specific error occurs
  132.      */
  133.     protected TLEPropagator(final TLE initialTLE, final AttitudeProvider attitudeProvider,
  134.                             final double mass)
  135.         throws OrekitException {
  136.         super(attitudeProvider);
  137.         setStartDate(initialTLE.getDate());
  138.         this.tle  = initialTLE;
  139.         this.teme = FramesFactory.getTEME();
  140.         this.mass = mass;
  141.         initializeCommons();
  142.         sxpInitialize();
  143.         // set the initial state
  144.         super.resetInitialState(new SpacecraftState(propagateOrbit(initialTLE.getDate())));
  145.     }

  146.     /** Selects the extrapolator to use with the selected TLE.
  147.      * @param tle the TLE to propagate.
  148.      * @return the correct propagator.
  149.      * @exception OrekitException if the underlying model cannot be initialized
  150.      */
  151.     public static TLEPropagator selectExtrapolator(final TLE tle) throws OrekitException {
  152.         return selectExtrapolator(tle, DEFAULT_LAW, DEFAULT_MASS);
  153.     }

  154.     /** Selects the extrapolator to use with the selected TLE.
  155.      * @param tle the TLE to propagate.
  156.      * @param attitudeProvider provider for attitude computation
  157.      * @param mass spacecraft mass (kg)
  158.      * @return the correct propagator.
  159.      * @exception OrekitException if the underlying model cannot be initialized
  160.      */
  161.     public static TLEPropagator selectExtrapolator(final TLE tle, final AttitudeProvider attitudeProvider,
  162.                                                    final double mass) throws OrekitException {

  163.         final double a1 = FastMath.pow( TLEConstants.XKE / (tle.getMeanMotion() * 60.0), TLEConstants.TWO_THIRD);
  164.         final double cosi0 = FastMath.cos(tle.getI());
  165.         final double temp = TLEConstants.CK2 * 1.5 * (3 * cosi0 * cosi0 - 1.0) *
  166.                             FastMath.pow(1.0 - tle.getE() * tle.getE(), -1.5);
  167.         final double delta1 = temp / (a1 * a1);
  168.         final double a0 = a1 * (1.0 - delta1 * (TLEConstants.ONE_THIRD + delta1 * (delta1 * 134.0 / 81.0 + 1.0)));
  169.         final double delta0 = temp / (a0 * a0);

  170.         // recover original mean motion :
  171.         final double xn0dp = tle.getMeanMotion() * 60.0 / (delta0 + 1.0);

  172.         // Period >= 225 minutes is deep space
  173.         if (MathUtils.TWO_PI / (xn0dp * TLEConstants.MINUTES_PER_DAY) >= (1.0 / 6.4)) {
  174.             return new DeepSDP4(tle, attitudeProvider, mass);
  175.         } else {
  176.             return new SGP4(tle, attitudeProvider, mass);
  177.         }
  178.     }

  179.     /** Get the Earth gravity coefficient used for TLE propagation.
  180.      * @return the Earth gravity coefficient.
  181.      */
  182.     public static double getMU() {
  183.         return TLEConstants.MU;
  184.     }

  185.     /** Get the extrapolated position and velocity from an initial TLE.
  186.      * @param date the final date
  187.      * @return the final PVCoordinates
  188.      * @exception OrekitException if propagation cannot be performed at given date
  189.      */
  190.     public PVCoordinates getPVCoordinates(final AbsoluteDate date)
  191.         throws OrekitException {

  192.         sxpPropagate(date.durationFrom(tle.getDate()) / 60.0);

  193.         // Compute PV with previous calculated parameters
  194.         return computePVCoordinates();
  195.     }

  196.     /** Computation of the first commons parameters.
  197.      */
  198.     private void initializeCommons() {

  199.         final double a1 = FastMath.pow(TLEConstants.XKE / (tle.getMeanMotion() * 60.0), TLEConstants.TWO_THIRD);
  200.         cosi0 = FastMath.cos(tle.getI());
  201.         theta2 = cosi0 * cosi0;
  202.         final double x3thm1 = 3.0 * theta2 - 1.0;
  203.         e0sq = tle.getE() * tle.getE();
  204.         beta02 = 1.0 - e0sq;
  205.         beta0 = FastMath.sqrt(beta02);
  206.         final double tval = TLEConstants.CK2 * 1.5 * x3thm1 / (beta0 * beta02);
  207.         final double delta1 = tval / (a1 * a1);
  208.         final double a0 = a1 * (1.0 - delta1 * (TLEConstants.ONE_THIRD + delta1 * (1.0 + 134.0 / 81.0 * delta1)));
  209.         final double delta0 = tval / (a0 * a0);

  210.         // recover original mean motion and semi-major axis :
  211.         xn0dp = tle.getMeanMotion() * 60.0 / (delta0 + 1.0);
  212.         a0dp = a0 / (1.0 - delta0);

  213.         // Values of s and qms2t :
  214.         s4 = TLEConstants.S;  // unmodified value for s
  215.         double q0ms24 = TLEConstants.QOMS2T; // unmodified value for q0ms2T

  216.         perige = (a0dp * (1 - tle.getE()) - TLEConstants.NORMALIZED_EQUATORIAL_RADIUS) * TLEConstants.EARTH_RADIUS; // perige

  217.         //  For perigee below 156 km, the values of s and qoms2t are changed :
  218.         if (perige < 156.0) {
  219.             if (perige <= 98.0) {
  220.                 s4 = 20.0;
  221.             } else {
  222.                 s4 = perige - 78.0;
  223.             }
  224.             final double temp_val = (120.0 - s4) * TLEConstants.NORMALIZED_EQUATORIAL_RADIUS / TLEConstants.EARTH_RADIUS;
  225.             final double temp_val_squared = temp_val * temp_val;
  226.             q0ms24 = temp_val_squared * temp_val_squared;
  227.             s4 = s4 / TLEConstants.EARTH_RADIUS + TLEConstants.NORMALIZED_EQUATORIAL_RADIUS; // new value for q0ms2T and s
  228.         }

  229.         final double pinv = 1.0 / (a0dp * beta02);
  230.         final double pinvsq = pinv * pinv;
  231.         tsi = 1.0 / (a0dp - s4);
  232.         eta = a0dp * tle.getE() * tsi;
  233.         etasq = eta * eta;
  234.         eeta = tle.getE() * eta;

  235.         final double psisq = FastMath.abs(1.0 - etasq); // abs because pow 3.5 needs positive value
  236.         final double tsi_squared = tsi * tsi;
  237.         coef = q0ms24 * tsi_squared * tsi_squared;
  238.         coef1 = coef / FastMath.pow(psisq, 3.5);

  239.         // C2 and C1 coefficients computation :
  240.         c2 = coef1 * xn0dp * (a0dp * (1.0 + 1.5 * etasq + eeta * (4.0 + etasq)) +
  241.              0.75 * TLEConstants.CK2 * tsi / psisq * x3thm1 * (8.0 + 3.0 * etasq * (8.0 + etasq)));
  242.         c1 = tle.getBStar() * c2;
  243.         sini0 = FastMath.sin(tle.getI());

  244.         final double x1mth2 = 1.0 - theta2;

  245.         // C4 coefficient computation :
  246.         c4 = 2.0 * xn0dp * coef1 * a0dp * beta02 * (eta * (2.0 + 0.5 * etasq) +
  247.              tle.getE() * (0.5 + 2.0 * etasq) -
  248.              2 * TLEConstants.CK2 * tsi / (a0dp * psisq) *
  249.              (-3.0 * x3thm1 * (1.0 - 2.0 * eeta + etasq * (1.5 - 0.5 * eeta)) +
  250.               0.75 * x1mth2 * (2.0 * etasq - eeta * (1.0 + etasq)) * FastMath.cos(2.0 * tle.getPerigeeArgument())));

  251.         final double theta4 = theta2 * theta2;
  252.         final double temp1 = 3 * TLEConstants.CK2 * pinvsq * xn0dp;
  253.         final double temp2 = temp1 * TLEConstants.CK2 * pinvsq;
  254.         final double temp3 = 1.25 * TLEConstants.CK4 * pinvsq * pinvsq * xn0dp;

  255.         // atmospheric and gravitation coefs :(Mdf and OMEGAdf)
  256.         xmdot = xn0dp +
  257.                 0.5 * temp1 * beta0 * x3thm1 +
  258.                 0.0625 * temp2 * beta0 * (13.0 - 78.0 * theta2 + 137.0 * theta4);

  259.         final double x1m5th = 1.0 - 5.0 * theta2;

  260.         omgdot = -0.5 * temp1 * x1m5th +
  261.                  0.0625 * temp2 * (7.0 - 114.0 * theta2 + 395.0 * theta4) +
  262.                  temp3 * (3.0 - 36.0 * theta2 + 49.0 * theta4);

  263.         final double xhdot1 = -temp1 * cosi0;

  264.         xnodot = xhdot1 + (0.5 * temp2 * (4.0 - 19.0 * theta2) + 2.0 * temp3 * (3.0 - 7.0 * theta2)) * cosi0;
  265.         xnodcf = 3.5 * beta02 * xhdot1 * c1;
  266.         t2cof = 1.5 * c1;

  267.     }

  268.     /** Retrieves the position and velocity.
  269.      * @return the computed PVCoordinates.
  270.      * @exception OrekitException if current orbit is out of supported range
  271.      * (too large eccentricity, too low perigee ...)
  272.      */
  273.     private PVCoordinates computePVCoordinates() throws OrekitException {

  274.         // Long period periodics
  275.         final double axn = e * FastMath.cos(omega);
  276.         double temp = 1.0 / (a * (1.0 - e * e));
  277.         final double xlcof = 0.125 * TLEConstants.A3OVK2 * sini0 * (3.0 + 5.0 * cosi0) / (1.0 + cosi0);
  278.         final double aycof = 0.25 * TLEConstants.A3OVK2 * sini0;
  279.         final double xll = temp * xlcof * axn;
  280.         final double aynl = temp * aycof;
  281.         final double xlt = xl + xll;
  282.         final double ayn = e * FastMath.sin(omega) + aynl;
  283.         final double elsq = axn * axn + ayn * ayn;
  284.         final double capu = MathUtils.normalizeAngle(xlt - xnode, FastMath.PI);
  285.         double epw = capu;
  286.         double ecosE = 0;
  287.         double esinE = 0;
  288.         double sinEPW = 0;
  289.         double cosEPW = 0;

  290.         // Dundee changes:  items dependent on cosio get recomputed:
  291.         final double cosi0Sq = cosi0 * cosi0;
  292.         final double x3thm1 = 3.0 * cosi0Sq - 1.0;
  293.         final double x1mth2 = 1.0 - cosi0Sq;
  294.         final double x7thm1 = 7.0 * cosi0Sq - 1.0;

  295.         if (e > (1 - 1e-6)) {
  296.             throw new OrekitException(OrekitMessages.TOO_LARGE_ECCENTRICITY_FOR_PROPAGATION_MODEL, e);
  297.         }

  298.         // Solve Kepler's' Equation.
  299.         final double newtonRaphsonEpsilon = 1e-12;
  300.         for (int j = 0; j < 10; j++) {

  301.             boolean doSecondOrderNewtonRaphson = true;

  302.             sinEPW = FastMath.sin( epw);
  303.             cosEPW = FastMath.cos( epw);
  304.             ecosE = axn * cosEPW + ayn * sinEPW;
  305.             esinE = axn * sinEPW - ayn * cosEPW;
  306.             final double f = capu - epw + esinE;
  307.             if (FastMath.abs(f) < newtonRaphsonEpsilon) {
  308.                 break;
  309.             }
  310.             final double fdot = 1.0 - ecosE;
  311.             double delta_epw = f / fdot;
  312.             if (j == 0) {
  313.                 final double maxNewtonRaphson = 1.25 * FastMath.abs(e);
  314.                 doSecondOrderNewtonRaphson = false;
  315.                 if (delta_epw > maxNewtonRaphson) {
  316.                     delta_epw = maxNewtonRaphson;
  317.                 } else if (delta_epw < -maxNewtonRaphson) {
  318.                     delta_epw = -maxNewtonRaphson;
  319.                 } else {
  320.                     doSecondOrderNewtonRaphson = true;
  321.                 }
  322.             }
  323.             if (doSecondOrderNewtonRaphson) {
  324.                 delta_epw = f / (fdot + 0.5 * esinE * delta_epw);
  325.             }
  326.             epw += delta_epw;
  327.         }

  328.         // Short period preliminary quantities
  329.         temp = 1.0 - elsq;
  330.         final double pl = a * temp;
  331.         final double r = a * (1.0 - ecosE);
  332.         double temp2 = a / r;
  333.         final double betal = FastMath.sqrt(temp);
  334.         temp = esinE / (1.0 + betal);
  335.         final double cosu = temp2 * (cosEPW - axn + ayn * temp);
  336.         final double sinu = temp2 * (sinEPW - ayn - axn * temp);
  337.         final double u = FastMath.atan2(sinu, cosu);
  338.         final double sin2u = 2.0 * sinu * cosu;
  339.         final double cos2u = 2.0 * cosu * cosu - 1.0;
  340.         final double temp1 = TLEConstants.CK2 / pl;
  341.         temp2 = temp1 / pl;

  342.         // Update for short periodics
  343.         final double rk = r * (1.0 - 1.5 * temp2 * betal * x3thm1) + 0.5 * temp1 * x1mth2 * cos2u;
  344.         final double uk = u - 0.25 * temp2 * x7thm1 * sin2u;
  345.         final double xnodek = xnode + 1.5 * temp2 * cosi0 * sin2u;
  346.         final double xinck = i + 1.5 * temp2 * cosi0 * sini0 * cos2u;

  347.         // Orientation vectors
  348.         final double sinuk = FastMath.sin(uk);
  349.         final double cosuk = FastMath.cos(uk);
  350.         final double sinik = FastMath.sin(xinck);
  351.         final double cosik = FastMath.cos(xinck);
  352.         final double sinnok = FastMath.sin(xnodek);
  353.         final double cosnok = FastMath.cos(xnodek);
  354.         final double xmx = -sinnok * cosik;
  355.         final double xmy = cosnok * cosik;
  356.         final double ux = xmx * sinuk + cosnok * cosuk;
  357.         final double uy = xmy * sinuk + sinnok * cosuk;
  358.         final double uz = sinik * sinuk;

  359.         // Position and velocity
  360.         final double cr = 1000 * rk * TLEConstants.EARTH_RADIUS;
  361.         final Vector3D pos = new Vector3D(cr * ux, cr * uy, cr * uz);

  362.         final double rdot   = TLEConstants.XKE * FastMath.sqrt(a) * esinE / r;
  363.         final double rfdot  = TLEConstants.XKE * FastMath.sqrt(pl) / r;
  364.         final double xn     = TLEConstants.XKE / (a * FastMath.sqrt(a));
  365.         final double rdotk  = rdot - xn * temp1 * x1mth2 * sin2u;
  366.         final double rfdotk = rfdot + xn * temp1 * (x1mth2 * cos2u + 1.5 * x3thm1);
  367.         final double vx     = xmx * cosuk - cosnok * sinuk;
  368.         final double vy     = xmy * cosuk - sinnok * sinuk;
  369.         final double vz     = sinik * cosuk;

  370.         final double cv = 1000.0 * TLEConstants.EARTH_RADIUS / 60.0;
  371.         final Vector3D vel = new Vector3D(cv * (rdotk * ux + rfdotk * vx),
  372.                                           cv * (rdotk * uy + rfdotk * vy),
  373.                                           cv * (rdotk * uz + rfdotk * vz));

  374.         return new PVCoordinates(pos, vel);

  375.     }

  376.     /** Initialization proper to each propagator (SGP or SDP).
  377.      * @exception OrekitException if some specific error occurs
  378.      */
  379.     protected abstract void sxpInitialize() throws OrekitException;

  380.     /** Propagation proper to each propagator (SGP or SDP).
  381.      * @param t the offset from initial epoch (min)
  382.      * @exception OrekitException if current state cannot be propagated
  383.      */
  384.     protected abstract void sxpPropagate(double t) throws OrekitException;

  385.     /** {@inheritDoc} */
  386.     public void resetInitialState(final SpacecraftState state)
  387.         throws PropagationException {
  388.         throw new PropagationException(OrekitMessages.NON_RESETABLE_STATE);
  389.     }

  390.     /** {@inheritDoc} */
  391.     protected double getMass(final AbsoluteDate date) {
  392.         return mass;
  393.     }

  394.     /** {@inheritDoc} */
  395.     protected Orbit propagateOrbit(final AbsoluteDate date) throws PropagationException {
  396.         try {
  397.             return new CartesianOrbit(getPVCoordinates(date), teme, date, TLEConstants.MU);
  398.         } catch (OrekitException oe) {
  399.             throw new PropagationException(oe);
  400.         }
  401.     }

  402.     /** Get the underlying TLE.
  403.      * @return underlying TLE
  404.      */
  405.     public TLE getTLE() {
  406.         return tle;
  407.     }

  408.     /** {@inheritDoc} */
  409.     public Frame getFrame() {
  410.         return teme;
  411.     }

  412. }