TLE.java

  1. /* Copyright 2002-2025 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. import java.text.DecimalFormat;
  19. import java.text.DecimalFormatSymbols;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import java.util.Objects;
  24. import java.util.regex.Pattern;

  25. import org.hipparchus.util.ArithmeticUtils;
  26. import org.hipparchus.util.FastMath;
  27. import org.hipparchus.util.MathUtils;
  28. import org.orekit.annotation.DefaultDataContext;
  29. import org.orekit.data.DataContext;
  30. import org.orekit.errors.OrekitException;
  31. import org.orekit.errors.OrekitMessages;
  32. import org.orekit.orbits.KeplerianOrbit;
  33. import org.orekit.orbits.OrbitType;
  34. import org.orekit.propagation.SpacecraftState;
  35. import org.orekit.propagation.analytical.tle.generation.TleGenerationAlgorithm;
  36. import org.orekit.propagation.analytical.tle.generation.TleGenerationUtil;
  37. import org.orekit.propagation.conversion.osc2mean.OsculatingToMeanConverter;
  38. import org.orekit.propagation.conversion.osc2mean.TLETheory;
  39. import org.orekit.time.AbsoluteDate;
  40. import org.orekit.time.DateComponents;
  41. import org.orekit.time.DateTimeComponents;
  42. import org.orekit.time.TimeComponents;
  43. import org.orekit.time.TimeScale;
  44. import org.orekit.time.TimeStamped;
  45. import org.orekit.utils.Constants;
  46. import org.orekit.utils.ParameterDriver;
  47. import org.orekit.utils.ParameterDriversProvider;

  48. /** This class is a container for a single set of TLE data.
  49.  *
  50.  * <p>TLE sets can be built either by providing directly the two lines, in
  51.  * which case parsing is performed internally or by providing the already
  52.  * parsed elements.</p>
  53.  * <p>TLE are not transparently convertible to {@link org.orekit.orbits.Orbit Orbit}
  54.  * instances. They are significant only with respect to their dedicated {@link
  55.  * TLEPropagator propagator}, which also computes position and velocity coordinates.
  56.  * Any attempt to directly use orbital parameters like {@link #getE() eccentricity},
  57.  * {@link #getI() inclination}, etc. without any reference to the {@link TLEPropagator
  58.  * TLE propagator} is prone to errors.</p>
  59.  * <p>More information on the TLE format can be found on the
  60.  * <a href="https://www.celestrak.com/">CelesTrak website.</a></p>
  61.  * @author Fabien Maussion
  62.  * @author Luc Maisonobe
  63.  */
  64. public class TLE implements TimeStamped, ParameterDriversProvider {

  65.     /** Identifier for SGP type of ephemeris. */
  66.     public static final int SGP = 1;

  67.     /** Identifier for SGP4 type of ephemeris. */
  68.     public static final int SGP4 = 2;

  69.     /** Identifier for SDP4 type of ephemeris. */
  70.     public static final int SDP4 = 3;

  71.     /** Identifier for SGP8 type of ephemeris. */
  72.     public static final int SGP8 = 4;

  73.     /** Identifier for SDP8 type of ephemeris. */
  74.     public static final int SDP8 = 5;

  75.     /** Identifier for default type of ephemeris (SGP4/SDP4). */
  76.     public static final int DEFAULT = 0;

  77.     /** Parameter name for B* coefficient. */
  78.     public static final String B_STAR = "BSTAR";

  79.     /** B* scaling factor.
  80.      * <p>
  81.      * We use a power of 2 to avoid numeric noise introduction
  82.      * in the multiplications/divisions sequences.
  83.      * </p>
  84.      */
  85.     private static final double B_STAR_SCALE = FastMath.scalb(1.0, -20);

  86.     /** Name of the mean motion parameter. */
  87.     private static final String MEAN_MOTION = "meanMotion";

  88.     /** Name of the inclination parameter. */
  89.     private static final String INCLINATION = "inclination";

  90.     /** Name of the eccentricity parameter. */
  91.     private static final String ECCENTRICITY = "eccentricity";

  92.     /** Pattern for line 1. */
  93.     private static final Pattern LINE_1_PATTERN =
  94.         Pattern.compile("1 [ 0-9A-Z&&[^IO]][ 0-9]{4}[A-Z] [ 0-9]{5}[ A-Z]{3} [ 0-9]{5}[.][ 0-9]{8} (?:(?:[ 0+-][.][ 0-9]{8})|(?: [ +-][.][ 0-9]{7})) " +
  95.                         "[ +-][ 0-9]{5}[+-][ 0-9] [ +-][ 0-9]{5}[+-][ 0-9] [ 0-9] [ 0-9]{4}[ 0-9]");

  96.     /** Pattern for line 2. */
  97.     private static final Pattern LINE_2_PATTERN =
  98.         Pattern.compile("2 [ 0-9A-Z&&[^IO]][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{7} " +
  99.                         "[ 0-9]{3}[.][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{2}[.][ 0-9]{13}[ 0-9]");

  100.     /** International symbols for parsing. */
  101.     private static final DecimalFormatSymbols SYMBOLS =
  102.         new DecimalFormatSymbols(Locale.US);

  103.     /** The satellite number. */
  104.     private final int satelliteNumber;

  105.     /** Classification (U for unclassified). */
  106.     private final char classification;

  107.     /** Launch year. */
  108.     private final int launchYear;

  109.     /** Launch number. */
  110.     private final int launchNumber;

  111.     /** Piece of launch (from "A" to "ZZZ"). */
  112.     private final String launchPiece;

  113.     /** Type of ephemeris. */
  114.     private final int ephemerisType;

  115.     /** Element number. */
  116.     private final int elementNumber;

  117.     /** the TLE current date. */
  118.     private final AbsoluteDate epoch;

  119.     /** Mean motion (rad/s). */
  120.     private final double meanMotion;

  121.     /** Mean motion first derivative (rad/s²). */
  122.     private final double meanMotionFirstDerivative;

  123.     /** Mean motion second derivative (rad/s³). */
  124.     private final double meanMotionSecondDerivative;

  125.     /** Eccentricity. */
  126.     private final double eccentricity;

  127.     /** Inclination (rad). */
  128.     private final double inclination;

  129.     /** Argument of perigee (rad). */
  130.     private final double pa;

  131.     /** Right Ascension of the Ascending node (rad). */
  132.     private final double raan;

  133.     /** Mean anomaly (rad). */
  134.     private final double meanAnomaly;

  135.     /** Revolution number at epoch. */
  136.     private final int revolutionNumberAtEpoch;

  137.     /** First line. */
  138.     private String line1;

  139.     /** Second line. */
  140.     private String line2;

  141.     /** The UTC scale. */
  142.     private final TimeScale utc;

  143.     /** Driver for ballistic coefficient parameter. */
  144.     private final transient ParameterDriver bStarParameterDriver;


  145.     /** Simple constructor from unparsed two lines. This constructor uses the {@link
  146.      * DataContext#getDefault() default data context}.
  147.      *
  148.      * <p>The static method {@link #isFormatOK(String, String)} should be called
  149.      * before trying to build this object.</p>
  150.      * @param line1 the first element (69 char String)
  151.      * @param line2 the second element (69 char String)
  152.      * @see #TLE(String, String, TimeScale)
  153.      */
  154.     @DefaultDataContext
  155.     public TLE(final String line1, final String line2) {
  156.         this(line1, line2, DataContext.getDefault().getTimeScales().getUTC());
  157.     }

  158.     /** Simple constructor from unparsed two lines using the given time scale as UTC.
  159.      *
  160.      * <p>The static method {@link #isFormatOK(String, String)} should be called
  161.      * before trying to build this object.</p>
  162.      * @param line1 the first element (69 char String)
  163.      * @param line2 the second element (69 char String)
  164.      * @param utc the UTC time scale.
  165.      * @since 10.1
  166.      */
  167.     public TLE(final String line1, final String line2, final TimeScale utc) {

  168.         // identification
  169.         satelliteNumber = ParseUtils.parseSatelliteNumber(line1, 2, 5);
  170.         final int satNum2 = ParseUtils.parseSatelliteNumber(line2, 2, 5);
  171.         if (satelliteNumber != satNum2) {
  172.             throw new OrekitException(OrekitMessages.TLE_LINES_DO_NOT_REFER_TO_SAME_OBJECT,
  173.                                       line1, line2);
  174.         }
  175.         classification  = line1.charAt(7);
  176.         launchYear      = ParseUtils.parseYear(line1, 9);
  177.         launchNumber    = ParseUtils.parseInteger(line1, 11, 3);
  178.         launchPiece     = line1.substring(14, 17).trim();
  179.         ephemerisType   = ParseUtils.parseInteger(line1, 62, 1);
  180.         elementNumber   = ParseUtils.parseInteger(line1, 64, 4);

  181.         // Date format transform (nota: 27/31250 == 86400/100000000)
  182.         final int    year      = ParseUtils.parseYear(line1, 18);
  183.         final int    dayInYear = ParseUtils.parseInteger(line1, 20, 3);
  184.         final long   df        = 27l * ParseUtils.parseInteger(line1, 24, 8);
  185.         final int    secondsA  = (int) (df / 31250l);
  186.         final double secondsB  = (df % 31250l) / 31250.0;
  187.         epoch = new AbsoluteDate(new DateComponents(year, dayInYear),
  188.                                  new TimeComponents(secondsA, secondsB),
  189.                                  utc);

  190.         // mean motion development
  191.         // converted from rev/day, 2 * rev/day^2 and 6 * rev/day^3 to rad/s, rad/s^2 and rad/s^3
  192.         meanMotion                 = ParseUtils.parseDouble(line2, 52, 11) * FastMath.PI / 43200.0;
  193.         meanMotionFirstDerivative  = ParseUtils.parseDouble(line1, 33, 10) * FastMath.PI / 1.86624e9;
  194.         meanMotionSecondDerivative = Double.parseDouble((line1.substring(44, 45) + '.' +
  195.                                                          line1.substring(45, 50) + 'e' +
  196.                                                          line1.substring(50, 52)).replace(' ', '0')) *
  197.                                      FastMath.PI / 5.3747712e13;

  198.         eccentricity = Double.parseDouble("." + line2.substring(26, 33).replace(' ', '0'));
  199.         inclination  = FastMath.toRadians(ParseUtils.parseDouble(line2, 8, 8));
  200.         pa           = FastMath.toRadians(ParseUtils.parseDouble(line2, 34, 8));
  201.         raan         = FastMath.toRadians(Double.parseDouble(line2.substring(17, 25).replace(' ', '0')));
  202.         meanAnomaly  = FastMath.toRadians(ParseUtils.parseDouble(line2, 43, 8));

  203.         revolutionNumberAtEpoch = ParseUtils.parseInteger(line2, 63, 5);
  204.         final double bStarValue = Double.parseDouble((line1.substring(53, 54) + '.' +
  205.                                     line1.substring(54, 59) + 'e' +
  206.                                     line1.substring(59, 61)).replace(' ', '0'));

  207.         // save the lines
  208.         this.line1 = line1;
  209.         this.line2 = line2;
  210.         this.utc = utc;

  211.         // create model parameter drivers
  212.         this.bStarParameterDriver = new ParameterDriver(B_STAR, bStarValue, B_STAR_SCALE,
  213.                                                         Double.NEGATIVE_INFINITY,
  214.                                                         Double.POSITIVE_INFINITY);

  215.     }

  216.     /**
  217.      * <p>
  218.      * Simple constructor from already parsed elements. This constructor uses the
  219.      * {@link DataContext#getDefault() default data context}.
  220.      * </p>
  221.      *
  222.      * <p>
  223.      * The mean anomaly, the right ascension of ascending node Ω and the argument of
  224.      * perigee ω are normalized into the [0, 2π] interval as they can be negative.
  225.      * After that, a range check is performed on some of the orbital elements:
  226.      *
  227.      * <pre>
  228.      *     meanMotion &gt;= 0
  229.      *     0 &lt;= i &lt;= π
  230.      *     0 &lt;= Ω &lt;= 2π
  231.      *     0 &lt;= e &lt;= 1
  232.      *     0 &lt;= ω &lt;= 2π
  233.      *     0 &lt;= meanAnomaly &lt;= 2π
  234.      * </pre>
  235.      *
  236.      * @param satelliteNumber satellite number
  237.      * @param classification classification (U for unclassified)
  238.      * @param launchYear launch year (all digits)
  239.      * @param launchNumber launch number
  240.      * @param launchPiece launch piece (3 char String)
  241.      * @param ephemerisType type of ephemeris
  242.      * @param elementNumber element number
  243.      * @param epoch elements epoch
  244.      * @param meanMotion mean motion (rad/s)
  245.      * @param meanMotionFirstDerivative mean motion first derivative (rad/s²)
  246.      * @param meanMotionSecondDerivative mean motion second derivative (rad/s³)
  247.      * @param e eccentricity
  248.      * @param i inclination (rad)
  249.      * @param pa argument of perigee (rad)
  250.      * @param raan right ascension of ascending node (rad)
  251.      * @param meanAnomaly mean anomaly (rad)
  252.      * @param revolutionNumberAtEpoch revolution number at epoch
  253.      * @param bStar ballistic coefficient
  254.      * @see #TLE(int, char, int, int, String, int, int, AbsoluteDate, double, double,
  255.      * double, double, double, double, double, double, int, double, TimeScale)
  256.      */
  257.     @DefaultDataContext
  258.     public TLE(final int satelliteNumber, final char classification,
  259.                final int launchYear, final int launchNumber, final String launchPiece,
  260.                final int ephemerisType, final int elementNumber, final AbsoluteDate epoch,
  261.                final double meanMotion, final double meanMotionFirstDerivative,
  262.                final double meanMotionSecondDerivative, final double e, final double i,
  263.                final double pa, final double raan, final double meanAnomaly,
  264.                final int revolutionNumberAtEpoch, final double bStar) {
  265.         this(satelliteNumber, classification, launchYear, launchNumber, launchPiece,
  266.                 ephemerisType, elementNumber, epoch, meanMotion,
  267.                 meanMotionFirstDerivative, meanMotionSecondDerivative, e, i, pa, raan,
  268.                 meanAnomaly, revolutionNumberAtEpoch, bStar,
  269.                 DataContext.getDefault().getTimeScales().getUTC());
  270.     }

  271.     /**
  272.      * <p>
  273.      * Simple constructor from already parsed elements using the given time scale as
  274.      * UTC.
  275.      * </p>
  276.      *
  277.      * <p>
  278.      * The mean anomaly, the right ascension of ascending node Ω and the argument of
  279.      * perigee ω are normalized into the [0, 2π] interval as they can be negative.
  280.      * After that, a range check is performed on some of the orbital elements:
  281.      *
  282.      * <pre>
  283.      *     meanMotion &gt;= 0
  284.      *     0 &lt;= i &lt;= π
  285.      *     0 &lt;= Ω &lt;= 2π
  286.      *     0 &lt;= e &lt;= 1
  287.      *     0 &lt;= ω &lt;= 2π
  288.      *     0 &lt;= meanAnomaly &lt;= 2π
  289.      * </pre>
  290.      *
  291.      * @param satelliteNumber satellite number
  292.      * @param classification classification (U for unclassified)
  293.      * @param launchYear launch year (all digits)
  294.      * @param launchNumber launch number
  295.      * @param launchPiece launch piece (3 char String)
  296.      * @param ephemerisType type of ephemeris
  297.      * @param elementNumber element number
  298.      * @param epoch elements epoch
  299.      * @param meanMotion mean motion (rad/s)
  300.      * @param meanMotionFirstDerivative mean motion first derivative (rad/s²)
  301.      * @param meanMotionSecondDerivative mean motion second derivative (rad/s³)
  302.      * @param e eccentricity
  303.      * @param i inclination (rad)
  304.      * @param pa argument of perigee (rad)
  305.      * @param raan right ascension of ascending node (rad)
  306.      * @param meanAnomaly mean anomaly (rad)
  307.      * @param revolutionNumberAtEpoch revolution number at epoch
  308.      * @param bStar ballistic coefficient
  309.      * @param utc the UTC time scale.
  310.      * @since 10.1
  311.      */
  312.     public TLE(final int satelliteNumber, final char classification,
  313.                final int launchYear, final int launchNumber, final String launchPiece,
  314.                final int ephemerisType, final int elementNumber, final AbsoluteDate epoch,
  315.                final double meanMotion, final double meanMotionFirstDerivative,
  316.                final double meanMotionSecondDerivative, final double e, final double i,
  317.                final double pa, final double raan, final double meanAnomaly,
  318.                final int revolutionNumberAtEpoch, final double bStar,
  319.                final TimeScale utc) {

  320.         // identification
  321.         this.satelliteNumber = satelliteNumber;
  322.         this.classification  = classification;
  323.         this.launchYear      = launchYear;
  324.         this.launchNumber    = launchNumber;
  325.         this.launchPiece     = launchPiece;
  326.         this.ephemerisType   = ephemerisType;
  327.         this.elementNumber   = elementNumber;

  328.         // orbital parameters
  329.         this.epoch = epoch;
  330.         // Checking mean motion range
  331.         this.meanMotion = meanMotion;
  332.         this.meanMotionFirstDerivative = meanMotionFirstDerivative;
  333.         this.meanMotionSecondDerivative = meanMotionSecondDerivative;

  334.         // Checking inclination range
  335.         this.inclination = i;

  336.         // Normalizing RAAN in [0,2pi] interval
  337.         this.raan = MathUtils.normalizeAngle(raan, FastMath.PI);

  338.         // Checking eccentricity range
  339.         this.eccentricity = e;

  340.         // Normalizing PA in [0,2pi] interval
  341.         this.pa = MathUtils.normalizeAngle(pa, FastMath.PI);

  342.         // Normalizing mean anomaly in [0,2pi] interval
  343.         this.meanAnomaly = MathUtils.normalizeAngle(meanAnomaly, FastMath.PI);

  344.         this.revolutionNumberAtEpoch = revolutionNumberAtEpoch;


  345.         // don't build the line until really needed
  346.         this.line1 = null;
  347.         this.line2 = null;
  348.         this.utc = utc;

  349.         // create model parameter drivers
  350.         this.bStarParameterDriver = new ParameterDriver(B_STAR, bStar, B_STAR_SCALE,
  351.                                                         Double.NEGATIVE_INFINITY,
  352.                                                         Double.POSITIVE_INFINITY);

  353.     }

  354.     /**
  355.      * Get the UTC time scale used to create this TLE.
  356.      *
  357.      * @return UTC time scale.
  358.      */
  359.     public TimeScale getUtc() {
  360.         return utc;
  361.     }

  362.     /** Get the first line.
  363.      * @return first line
  364.      */
  365.     public String getLine1() {
  366.         if (line1 == null) {
  367.             buildLine1();
  368.         }
  369.         return line1;
  370.     }

  371.     /** Get the second line.
  372.      * @return second line
  373.      */
  374.     public String getLine2() {
  375.         if (line2 == null) {
  376.             buildLine2();
  377.         }
  378.         return line2;
  379.     }

  380.     /** Build the line 1 from the parsed elements.
  381.      */
  382.     private void buildLine1() {

  383.         final StringBuilder buffer = new StringBuilder();

  384.         buffer.append('1');

  385.         buffer.append(' ');
  386.         buffer.append(ParseUtils.buildSatelliteNumber(satelliteNumber, "satelliteNumber-1"));
  387.         buffer.append(classification);

  388.         buffer.append(' ');
  389.         buffer.append(ParseUtils.addPadding("launchYear",   launchYear % 100, '0', 2, true, satelliteNumber));
  390.         buffer.append(ParseUtils.addPadding("launchNumber", launchNumber, '0', 3, true, satelliteNumber));
  391.         buffer.append(ParseUtils.addPadding("launchPiece",  launchPiece, ' ', 3, false, satelliteNumber));

  392.         buffer.append(' ');
  393.         DateTimeComponents dtc = epoch.getComponents(utc);
  394.         int fraction = (int) FastMath.rint(31250 * dtc.getTime().getSecondsInUTCDay() / 27.0);
  395.         if (fraction >= 100000000) {
  396.             dtc =  epoch.shiftedBy(Constants.JULIAN_DAY).getComponents(utc);
  397.             fraction -= 100000000;
  398.         }
  399.         buffer.append(ParseUtils.addPadding("year", dtc.getDate().getYear() % 100, '0', 2, true, satelliteNumber));
  400.         buffer.append(ParseUtils.addPadding("day",  dtc.getDate().getDayOfYear(),  '0', 3, true, satelliteNumber));
  401.         buffer.append('.');
  402.         // nota: 31250/27 == 100000000/86400

  403.         buffer.append(ParseUtils.addPadding("fraction", fraction,  '0', 8, true, satelliteNumber));

  404.         buffer.append(' ');
  405.         final double n1 = meanMotionFirstDerivative * 1.86624e9 / FastMath.PI;
  406.         final String sn1 = ParseUtils.addPadding("meanMotionFirstDerivative",
  407.                                                  new DecimalFormat(".00000000", SYMBOLS).format(n1),
  408.                                                  ' ', 10, true, satelliteNumber);
  409.         buffer.append(sn1);

  410.         buffer.append(' ');
  411.         final double n2 = meanMotionSecondDerivative * 5.3747712e13 / FastMath.PI;
  412.         buffer.append(formatExponentMarkerFree("meanMotionSecondDerivative", n2, 5, ' ', 8, true));

  413.         buffer.append(' ');
  414.         buffer.append(formatExponentMarkerFree("B*", getBStar(), 5, ' ', 8, true));

  415.         buffer.append(' ');
  416.         buffer.append(ephemerisType);

  417.         buffer.append(' ');
  418.         buffer.append(ParseUtils.addPadding("elementNumber", elementNumber, ' ', 4, true, satelliteNumber));

  419.         buffer.append(checksum(buffer));

  420.         line1 = buffer.toString();

  421.     }

  422.     /** Format a real number without 'e' exponent marker.
  423.      * @param name parameter name
  424.      * @param d number to format
  425.      * @param mantissaSize size of the mantissa (not counting initial '-' or ' ' for sign)
  426.      * @param c padding character
  427.      * @param size desired size
  428.      * @param rightJustified if true, the resulting string is
  429.      * right justified (i.e. space are added to the left)
  430.      * @return formatted and padded number
  431.      */
  432.     private String formatExponentMarkerFree(final String name, final double d, final int mantissaSize,
  433.                                             final char c, final int size, final boolean rightJustified) {
  434.         final double dAbs = FastMath.abs(d);
  435.         int exponent = (dAbs < 1.0e-9) ? -9 : (int) FastMath.ceil(FastMath.log10(dAbs));
  436.         long mantissa = FastMath.round(dAbs * FastMath.pow(10.0, mantissaSize - exponent));
  437.         if (mantissa == 0) {
  438.             exponent = 0;
  439.         } else if (mantissa > (ArithmeticUtils.pow(10, mantissaSize) - 1)) {
  440.             // rare case: if d has a single digit like d = 1.0e-4 with mantissaSize = 5
  441.             // the above computation finds exponent = -4 and mantissa = 100000 which
  442.             // doesn't fit in a 5 digits string
  443.             exponent++;
  444.             mantissa = FastMath.round(dAbs * FastMath.pow(10.0, mantissaSize - exponent));
  445.         }
  446.         final String sMantissa = ParseUtils.addPadding(name, (int) mantissa, '0', mantissaSize, true, satelliteNumber);
  447.         final String sExponent = Integer.toString(FastMath.abs(exponent));
  448.         final String formatted = (d <  0 ? '-' : ' ') + sMantissa + (exponent <= 0 ? '-' : '+') + sExponent;

  449.         return ParseUtils.addPadding(name, formatted, c, size, rightJustified, satelliteNumber);

  450.     }

  451.     /** Build the line 2 from the parsed elements.
  452.      */
  453.     private void buildLine2() {

  454.         final StringBuilder buffer = new StringBuilder();
  455.         final DecimalFormat f34   = new DecimalFormat("##0.0000", SYMBOLS);
  456.         final DecimalFormat f211  = new DecimalFormat("#0.00000000", SYMBOLS);

  457.         buffer.append('2');

  458.         buffer.append(' ');
  459.         buffer.append(ParseUtils.buildSatelliteNumber(satelliteNumber, "satelliteNumber-2"));

  460.         buffer.append(' ');
  461.         buffer.append(ParseUtils.addPadding(INCLINATION, f34.format(FastMath.toDegrees(inclination)), ' ', 8, true, satelliteNumber));
  462.         buffer.append(' ');
  463.         buffer.append(ParseUtils.addPadding("raan", f34.format(FastMath.toDegrees(raan)), ' ', 8, true, satelliteNumber));
  464.         buffer.append(' ');
  465.         buffer.append(ParseUtils.addPadding(ECCENTRICITY, (int) FastMath.rint(eccentricity * 1.0e7), '0', 7, true, satelliteNumber));
  466.         buffer.append(' ');
  467.         buffer.append(ParseUtils.addPadding("pa", f34.format(FastMath.toDegrees(pa)), ' ', 8, true, satelliteNumber));
  468.         buffer.append(' ');
  469.         buffer.append(ParseUtils.addPadding("meanAnomaly", f34.format(FastMath.toDegrees(meanAnomaly)), ' ', 8, true, satelliteNumber));

  470.         buffer.append(' ');
  471.         buffer.append(ParseUtils.addPadding(MEAN_MOTION, f211.format(meanMotion * 43200.0 / FastMath.PI), ' ', 11, true, satelliteNumber));
  472.         buffer.append(ParseUtils.addPadding("revolutionNumberAtEpoch", revolutionNumberAtEpoch, ' ', 5, true, satelliteNumber));

  473.         buffer.append(checksum(buffer));

  474.         line2 = buffer.toString();

  475.     }

  476.     /** Get the satellite id.
  477.      * @return the satellite number
  478.      */
  479.     public int getSatelliteNumber() {
  480.         return satelliteNumber;
  481.     }

  482.     /** Get the classification.
  483.      * @return classification
  484.      */
  485.     public char getClassification() {
  486.         return classification;
  487.     }

  488.     /** Get the launch year.
  489.      * @return the launch year
  490.      */
  491.     public int getLaunchYear() {
  492.         return launchYear;
  493.     }

  494.     /** Get the launch number.
  495.      * @return the launch number
  496.      */
  497.     public int getLaunchNumber() {
  498.         return launchNumber;
  499.     }

  500.     /** Get the launch piece.
  501.      * @return the launch piece
  502.      */
  503.     public String getLaunchPiece() {
  504.         return launchPiece;
  505.     }

  506.     /** Get the type of ephemeris.
  507.      * @return the ephemeris type (one of {@link #DEFAULT}, {@link #SGP},
  508.      * {@link #SGP4}, {@link #SGP8}, {@link #SDP4}, {@link #SDP8})
  509.      */
  510.     public int getEphemerisType() {
  511.         return ephemerisType;
  512.     }

  513.     /** Get the element number.
  514.      * @return the element number
  515.      */
  516.     public int getElementNumber() {
  517.         return elementNumber;
  518.     }

  519.     /** Get the TLE current date.
  520.      * @return the epoch
  521.      */
  522.     public AbsoluteDate getDate() {
  523.         return epoch;
  524.     }

  525.     /** Get the mean motion.
  526.      * @return the mean motion (rad/s)
  527.      */
  528.     public double getMeanMotion() {
  529.         return meanMotion;
  530.     }

  531.     /** Get the mean motion first derivative.
  532.      * @return the mean motion first derivative (rad/s²)
  533.      */
  534.     public double getMeanMotionFirstDerivative() {
  535.         return meanMotionFirstDerivative;
  536.     }

  537.     /** Get the mean motion second derivative.
  538.      * @return the mean motion second derivative (rad/s³)
  539.      */
  540.     public double getMeanMotionSecondDerivative() {
  541.         return meanMotionSecondDerivative;
  542.     }

  543.     /** Get the eccentricity.
  544.      * @return the eccentricity
  545.      */
  546.     public double getE() {
  547.         return eccentricity;
  548.     }

  549.     /** Get the inclination.
  550.      * @return the inclination (rad)
  551.      */
  552.     public double getI() {
  553.         return inclination;
  554.     }

  555.     /** Get the argument of perigee.
  556.      * @return omega (rad)
  557.      */
  558.     public double getPerigeeArgument() {
  559.         return pa;
  560.     }

  561.     /** Get Right Ascension of the Ascending node.
  562.      * @return the raan (rad)
  563.      */
  564.     public double getRaan() {
  565.         return raan;
  566.     }

  567.     /** Get the mean anomaly.
  568.      * @return the mean anomaly (rad)
  569.      */
  570.     public double getMeanAnomaly() {
  571.         return meanAnomaly;
  572.     }

  573.     /** Get the revolution number.
  574.      * @return the revolutionNumberAtEpoch
  575.      */
  576.     public int getRevolutionNumberAtEpoch() {
  577.         return revolutionNumberAtEpoch;
  578.     }

  579.     /** Get the ballistic coefficient at tle date.
  580.      * @return bStar
  581.      */
  582.     public double getBStar() {
  583.         return bStarParameterDriver.getValue(getDate());
  584.     }

  585.     /** Get the ballistic coefficient at a specific date.
  586.      * @param date at which the ballistic coefficient wants to be known.
  587.      * @return bStar
  588.      */
  589.     public double getBStar(final AbsoluteDate date) {
  590.         return bStarParameterDriver.getValue(date);
  591.     }

  592.     /** Compute the semi-major axis from the mean motion of the TLE and the gravitational parameter from TLEConstants.
  593.      * @return the semi-major axis computed.
  594.      */
  595.     public double computeSemiMajorAxis() {
  596.         return FastMath.cbrt(TLEConstants.MU / (meanMotion * meanMotion));
  597.     }

  598.     /** Get a string representation of this TLE set.
  599.      * <p>The representation is simply the two lines separated by the
  600.      * platform line separator.</p>
  601.      * @return string representation of this TLE set
  602.      */
  603.     public String toString() {
  604.         return getLine1() + System.getProperty("line.separator") + getLine2();
  605.     }

  606.     /**
  607.      * Convert Spacecraft State into TLE.
  608.      *
  609.      * @param state Spacecraft State to convert into TLE
  610.      * @param templateTLE only used to get identifiers like satellite number, launch year, etc. In other words, the keplerian elements contained in the generated TLE are based on the provided state and not the template TLE.
  611.      * @param generationAlgorithm TLE generation algorithm
  612.      * @return a generated TLE
  613.      * @since 12.0
  614.      * @deprecated As of release 13.0, use {@link #stateToTLE(SpacecraftState, TLE, OsculatingToMeanConverter)} instead.
  615.      */
  616.     @Deprecated
  617.     public static TLE stateToTLE(final SpacecraftState state, final TLE templateTLE,
  618.                                  final TleGenerationAlgorithm generationAlgorithm) {
  619.         return generationAlgorithm.generate(state, templateTLE);
  620.     }

  621.     /**
  622.      * Convert Spacecraft State into TLE.
  623.      * <p>
  624.      * Uses the {@link DataContext#getDefault() default data context}.
  625.      * </p>
  626.      * <p>
  627.      * The B* is not calculated. Its value is simply copied from the model to the generated TLE.
  628.      * </p>
  629.      * @param state       Spacecraft State to convert into TLE
  630.      * @param templateTLE only used to get identifiers like satellite number, launch year, etc.
  631.      *                    In other words, the keplerian elements contained in the generated TLE
  632.      *                    are based on the provided state and not the template TLE.
  633.      * @param converter   osculating to mean orbit converter
  634.      * @return a generated TLE
  635.      * @since 13.0
  636.      */
  637.     @DefaultDataContext
  638.     public static TLE stateToTLE(final SpacecraftState state,
  639.                                  final TLE templateTLE,
  640.                                  final OsculatingToMeanConverter converter) {
  641.         return stateToTLE(state, templateTLE, converter, DataContext.getDefault());
  642.     }

  643.     /**
  644.      * Convert Spacecraft State into TLE.
  645.      * <p>
  646.      * The B* is not calculated. Its value is simply copied from the model to the generated TLE.
  647.      * </p>
  648.      * @param state       Spacecraft State to convert into TLE
  649.      * @param templateTLE only used to get identifiers like satellite number, launch year, etc.
  650.      *                    In other words, the keplerian elements contained in the generated TLE
  651.      *                    are based on the provided state and not the template TLE.
  652.      * @param converter   osculating to mean orbit converter
  653.      * @param dataContext data context
  654.      * @return a generated TLE
  655.      * @since 13.0
  656.      */
  657.     public static TLE stateToTLE(final SpacecraftState state,
  658.                                  final TLE templateTLE,
  659.                                  final OsculatingToMeanConverter converter,
  660.                                  final DataContext dataContext) {
  661.         converter.setMeanTheory(new TLETheory(templateTLE, dataContext));
  662.         final KeplerianOrbit mean = (KeplerianOrbit) OrbitType.KEPLERIAN.convertType(converter.convertToMean(state.getOrbit()));
  663.         final TLE tle = TleGenerationUtil.newTLE(mean, templateTLE, templateTLE.getBStar(mean.getDate()),
  664.                                                  dataContext.getTimeScales().getUTC());
  665.         // reset estimated parameters from template to generated tle
  666.         for (final ParameterDriver templateDrivers : templateTLE.getParametersDrivers()) {
  667.             if (templateDrivers.isSelected()) {
  668.                 // set to selected for the new TLE
  669.                 tle.getParameterDriver(templateDrivers.getName()).setSelected(true);
  670.             }
  671.         }
  672.         return tle;
  673.     }

  674.     /** Check the lines format validity.
  675.      * @param line1 the first element
  676.      * @param line2 the second element
  677.      * @return true if format is recognized (non null lines, 69 characters length,
  678.      * line content), false if not
  679.      */
  680.     public static boolean isFormatOK(final String line1, final String line2) {

  681.         if (line1 == null || line1.length() != 69 ||
  682.             line2 == null || line2.length() != 69) {
  683.             return false;
  684.         }

  685.         if (!(LINE_1_PATTERN.matcher(line1).matches() &&
  686.               LINE_2_PATTERN.matcher(line2).matches())) {
  687.             return false;
  688.         }

  689.         // check sums
  690.         final int checksum1 = checksum(line1);
  691.         if (Integer.parseInt(line1.substring(68)) != (checksum1 % 10)) {
  692.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  693.                                       1, Integer.toString(checksum1 % 10), line1.substring(68), line1);
  694.         }

  695.         final int checksum2 = checksum(line2);
  696.         if (Integer.parseInt(line2.substring(68)) != (checksum2 % 10)) {
  697.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  698.                                       2, Integer.toString(checksum2 % 10), line2.substring(68), line2);
  699.         }

  700.         return true;

  701.     }

  702.     /** Compute the checksum of the first 68 characters of a line.
  703.      * @param line line to check
  704.      * @return checksum
  705.      */
  706.     private static int checksum(final CharSequence line) {
  707.         int sum = 0;
  708.         for (int j = 0; j < 68; j++) {
  709.             final char c = line.charAt(j);
  710.             if (Character.isDigit(c)) {
  711.                 sum += Character.digit(c, 10);
  712.             } else if (c == '-') {
  713.                 ++sum;
  714.             }
  715.         }
  716.         return sum % 10;
  717.     }

  718.     /** Check if this tle equals the provided tle.
  719.      * <p>Due to the difference in precision between object and string
  720.      * representations of TLE, it is possible for this method to return false
  721.      * even if string representations returned by {@link #toString()}
  722.      * are equal.</p>
  723.      * @param o other tle
  724.      * @return true if this tle equals the provided tle
  725.      */
  726.     @Override
  727.     public boolean equals(final Object o) {
  728.         if (o == this) {
  729.             return true;
  730.         }
  731.         if (!(o instanceof TLE)) {
  732.             return false;
  733.         }
  734.         final TLE tle = (TLE) o;
  735.         return satelliteNumber == tle.satelliteNumber &&
  736.                 classification == tle.classification &&
  737.                 launchYear == tle.launchYear &&
  738.                 launchNumber == tle.launchNumber &&
  739.                 Objects.equals(launchPiece, tle.launchPiece) &&
  740.                 ephemerisType == tle.ephemerisType &&
  741.                 elementNumber == tle.elementNumber &&
  742.                 Objects.equals(epoch, tle.epoch) &&
  743.                 meanMotion == tle.meanMotion &&
  744.                 meanMotionFirstDerivative == tle.meanMotionFirstDerivative &&
  745.                 meanMotionSecondDerivative == tle.meanMotionSecondDerivative &&
  746.                 eccentricity == tle.eccentricity &&
  747.                 inclination == tle.inclination &&
  748.                 pa == tle.pa &&
  749.                 raan == tle.raan &&
  750.                 meanAnomaly == tle.meanAnomaly &&
  751.                 revolutionNumberAtEpoch == tle.revolutionNumberAtEpoch &&
  752.                 getBStar() == tle.getBStar();
  753.     }

  754.     /** Get a hashcode for this tle.
  755.      * @return hashcode
  756.      */
  757.     @Override
  758.     public int hashCode() {
  759.         return Objects.hash(satelliteNumber,
  760.                 classification,
  761.                 launchYear,
  762.                 launchNumber,
  763.                 launchPiece,
  764.                 ephemerisType,
  765.                 elementNumber,
  766.                 epoch,
  767.                 meanMotion,
  768.                 meanMotionFirstDerivative,
  769.                 meanMotionSecondDerivative,
  770.                 eccentricity,
  771.                 inclination,
  772.                 pa,
  773.                 raan,
  774.                 meanAnomaly,
  775.                 revolutionNumberAtEpoch,
  776.                 getBStar());
  777.     }

  778.     /** Get the drivers for TLE propagation SGP4 and SDP4.
  779.      * @return drivers for SGP4 and SDP4 model parameters
  780.      */
  781.     public List<ParameterDriver> getParametersDrivers() {
  782.         return Collections.singletonList(bStarParameterDriver);
  783.     }

  784. }