TLE.java

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  88.     /** Pattern for line 1. */
  89.     private static final Pattern LINE_1_PATTERN =
  90.         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})) " +
  91.                         "[ +-][ 0-9]{5}[+-][ 0-9] [ +-][ 0-9]{5}[+-][ 0-9] [ 0-9] [ 0-9]{4}[ 0-9]");

  92.     /** Pattern for line 2. */
  93.     private static final Pattern LINE_2_PATTERN =
  94.         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} " +
  95.                         "[ 0-9]{3}[.][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{2}[.][ 0-9]{13}[ 0-9]");

  96.     /** International symbols for parsing. */
  97.     private static final DecimalFormatSymbols SYMBOLS =
  98.         new DecimalFormatSymbols(Locale.US);

  99.     /** Serializable UID. */
  100.     private static final long serialVersionUID = -1596648022319057689L;

  101.     /** The satellite number. */
  102.     private final int satelliteNumber;

  103.     /** Classification (U for unclassified). */
  104.     private final char classification;

  105.     /** Launch year. */
  106.     private final int launchYear;

  107.     /** Launch number. */
  108.     private final int launchNumber;

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

  111.     /** Type of ephemeris. */
  112.     private final int ephemerisType;

  113.     /** Element number. */
  114.     private final int elementNumber;

  115.     /** the TLE current date. */
  116.     private final AbsoluteDate epoch;

  117.     /** Mean motion (rad/s). */
  118.     private final double meanMotion;

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

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

  123.     /** Eccentricity. */
  124.     private final double eccentricity;

  125.     /** Inclination (rad). */
  126.     private final double inclination;

  127.     /** Argument of perigee (rad). */
  128.     private final double pa;

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

  131.     /** Mean anomaly (rad). */
  132.     private final double meanAnomaly;

  133.     /** Revolution number at epoch. */
  134.     private final int revolutionNumberAtEpoch;

  135.     /** First line. */
  136.     private String line1;

  137.     /** Second line. */
  138.     private String line2;

  139.     /** The UTC scale. */
  140.     private final TimeScale utc;

  141.     /** Driver for ballistic coefficient parameter. */
  142.     private final transient ParameterDriver bStarParameterDriver;


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

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

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

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

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

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

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

  205.         // save the lines
  206.         this.line1 = line1;
  207.         this.line2 = line2;
  208.         this.utc = utc;

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

  213.     }

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

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

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

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

  332.         // Checking inclination range
  333.         this.inclination = i;

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

  336.         // Checking eccentricity range
  337.         this.eccentricity = e;

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

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

  342.         this.revolutionNumberAtEpoch = revolutionNumberAtEpoch;


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

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

  351.     }

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

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

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

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

  381.         final StringBuilder buffer = new StringBuilder();

  382.         buffer.append('1');

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

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

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

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

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

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

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

  413.         buffer.append(' ');
  414.         buffer.append(ephemerisType);

  415.         buffer.append(' ');
  416.         buffer.append(ParseUtils.addPadding("elementNumber", elementNumber, ' ', 4, true, satelliteNumber));

  417.         buffer.append(checksum(buffer));

  418.         line1 = buffer.toString();

  419.     }

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

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

  448.     }

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

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

  455.         buffer.append('2');

  456.         buffer.append(' ');
  457.         buffer.append(ParseUtils.buildSatelliteNumber(satelliteNumber, "satelliteNumber-2"));

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

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

  471.         buffer.append(checksum(buffer));

  472.         line2 = buffer.toString();

  473.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  590.     /** Get a string representation of this TLE set.
  591.      * <p>The representation is simply the two lines separated by the
  592.      * platform line separator.</p>
  593.      * @return string representation of this TLE set
  594.      */
  595.     public String toString() {
  596.         return getLine1() + System.getProperty("line.separator") + getLine2();
  597.     }

  598.     /**
  599.      * Convert Spacecraft State into TLE.
  600.      *
  601.      * @param state Spacecraft State to convert into TLE
  602.      * @param templateTLE first guess used to get identification and estimate new TLE
  603.      * @param generationAlgorithm TLE generation algorithm
  604.      * @return a generated TLE
  605.      * @since 12.0
  606.      */
  607.     public static TLE stateToTLE(final SpacecraftState state, final TLE templateTLE,
  608.                                  final TleGenerationAlgorithm generationAlgorithm) {
  609.         return generationAlgorithm.generate(state, templateTLE);
  610.     }

  611.     /** Check the lines format validity.
  612.      * @param line1 the first element
  613.      * @param line2 the second element
  614.      * @return true if format is recognized (non null lines, 69 characters length,
  615.      * line content), false if not
  616.      */
  617.     public static boolean isFormatOK(final String line1, final String line2) {

  618.         if (line1 == null || line1.length() != 69 ||
  619.             line2 == null || line2.length() != 69) {
  620.             return false;
  621.         }

  622.         if (!(LINE_1_PATTERN.matcher(line1).matches() &&
  623.               LINE_2_PATTERN.matcher(line2).matches())) {
  624.             return false;
  625.         }

  626.         // check sums
  627.         final int checksum1 = checksum(line1);
  628.         if (Integer.parseInt(line1.substring(68)) != (checksum1 % 10)) {
  629.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  630.                                       1, Integer.toString(checksum1 % 10), line1.substring(68), line1);
  631.         }

  632.         final int checksum2 = checksum(line2);
  633.         if (Integer.parseInt(line2.substring(68)) != (checksum2 % 10)) {
  634.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  635.                                       2, Integer.toString(checksum2 % 10), line2.substring(68), line2);
  636.         }

  637.         return true;

  638.     }

  639.     /** Compute the checksum of the first 68 characters of a line.
  640.      * @param line line to check
  641.      * @return checksum
  642.      */
  643.     private static int checksum(final CharSequence line) {
  644.         int sum = 0;
  645.         for (int j = 0; j < 68; j++) {
  646.             final char c = line.charAt(j);
  647.             if (Character.isDigit(c)) {
  648.                 sum += Character.digit(c, 10);
  649.             } else if (c == '-') {
  650.                 ++sum;
  651.             }
  652.         }
  653.         return sum % 10;
  654.     }

  655.     /** Check if this tle equals the provided tle.
  656.      * <p>Due to the difference in precision between object and string
  657.      * representations of TLE, it is possible for this method to return false
  658.      * even if string representations returned by {@link #toString()}
  659.      * are equal.</p>
  660.      * @param o other tle
  661.      * @return true if this tle equals the provided tle
  662.      */
  663.     @Override
  664.     public boolean equals(final Object o) {
  665.         if (o == this) {
  666.             return true;
  667.         }
  668.         if (!(o instanceof TLE)) {
  669.             return false;
  670.         }
  671.         final TLE tle = (TLE) o;
  672.         return satelliteNumber == tle.satelliteNumber &&
  673.                 classification == tle.classification &&
  674.                 launchYear == tle.launchYear &&
  675.                 launchNumber == tle.launchNumber &&
  676.                 Objects.equals(launchPiece, tle.launchPiece) &&
  677.                 ephemerisType == tle.ephemerisType &&
  678.                 elementNumber == tle.elementNumber &&
  679.                 Objects.equals(epoch, tle.epoch) &&
  680.                 meanMotion == tle.meanMotion &&
  681.                 meanMotionFirstDerivative == tle.meanMotionFirstDerivative &&
  682.                 meanMotionSecondDerivative == tle.meanMotionSecondDerivative &&
  683.                 eccentricity == tle.eccentricity &&
  684.                 inclination == tle.inclination &&
  685.                 pa == tle.pa &&
  686.                 raan == tle.raan &&
  687.                 meanAnomaly == tle.meanAnomaly &&
  688.                 revolutionNumberAtEpoch == tle.revolutionNumberAtEpoch &&
  689.                 getBStar() == tle.getBStar();
  690.     }

  691.     /** Get a hashcode for this tle.
  692.      * @return hashcode
  693.      */
  694.     @Override
  695.     public int hashCode() {
  696.         return Objects.hash(satelliteNumber,
  697.                 classification,
  698.                 launchYear,
  699.                 launchNumber,
  700.                 launchPiece,
  701.                 ephemerisType,
  702.                 elementNumber,
  703.                 epoch,
  704.                 meanMotion,
  705.                 meanMotionFirstDerivative,
  706.                 meanMotionSecondDerivative,
  707.                 eccentricity,
  708.                 inclination,
  709.                 pa,
  710.                 raan,
  711.                 meanAnomaly,
  712.                 revolutionNumberAtEpoch,
  713.                 getBStar());
  714.     }

  715.     /** Get the drivers for TLE propagation SGP4 and SDP4.
  716.      * @return drivers for SGP4 and SDP4 model parameters
  717.      */
  718.     public List<ParameterDriver> getParametersDrivers() {
  719.         return Collections.singletonList(bStarParameterDriver);
  720.     }

  721.     /** Replace the instance with a data transfer object for serialization.
  722.      * @return data transfer object that will be serialized
  723.      */
  724.     private Object writeReplace() {
  725.         return new DataTransferObject(line1, line2, utc);
  726.     }

  727.     /** Internal class used only for serialization. */
  728.     private static class DataTransferObject implements Serializable {

  729.         /** Serializable UID. */
  730.         private static final long serialVersionUID = -1596648022319057689L;

  731.         /** First line. */
  732.         private String line1;

  733.         /** Second line. */
  734.         private String line2;

  735.         /** The UTC scale. */
  736.         private final TimeScale utc;

  737.         /** Simple constructor.
  738.          * @param line1 the first element (69 char String)
  739.          * @param line2 the second element (69 char String)
  740.          * @param utc the UTC time scale
  741.          */
  742.         DataTransferObject(final String line1, final String line2, final TimeScale utc) {
  743.             this.line1 = line1;
  744.             this.line2 = line2;
  745.             this.utc   = utc;
  746.         }

  747.         /** Replace the deserialized data transfer object with a {@link TLE}.
  748.          * @return replacement {@link TLE}
  749.          */
  750.         private Object readResolve() {
  751.             return new TLE(line1, line2, utc);
  752.         }

  753.     }

  754. }