TLE.java

  1. /* Copyright 2002-2020 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.Locale;
  22. import java.util.Objects;
  23. import java.util.regex.Pattern;

  24. import org.hipparchus.util.ArithmeticUtils;
  25. import org.hipparchus.util.FastMath;
  26. import org.hipparchus.util.MathUtils;
  27. import org.orekit.annotation.DefaultDataContext;
  28. import org.orekit.data.DataContext;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitInternalError;
  31. import org.orekit.errors.OrekitMessages;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.time.DateComponents;
  34. import org.orekit.time.DateTimeComponents;
  35. import org.orekit.time.TimeComponents;
  36. import org.orekit.time.TimeScale;
  37. import org.orekit.time.TimeStamped;

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

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

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

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

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

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

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

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

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

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

  73.     /** Pattern for line 1. */
  74.     private static final Pattern LINE_1_PATTERN =
  75.         Pattern.compile("1 [ 0-9]{5}[A-Z] [ 0-9]{5}[ A-Z]{3} [ 0-9]{5}[.][ 0-9]{8} (?:(?:[ 0+-][.][ 0-9]{8})|(?: [ +-][.][ 0-9]{7})) " +
  76.                         "[ +-][ 0-9]{5}[+-][ 0-9] [ +-][ 0-9]{5}[+-][ 0-9] [ 0-9] [ 0-9]{4}[ 0-9]");

  77.     /** Pattern for line 2. */
  78.     private static final Pattern LINE_2_PATTERN =
  79.         Pattern.compile("2 [ 0-9]{5} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{7} " +
  80.                         "[ 0-9]{3}[.][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{2}[.][ 0-9]{13}[ 0-9]");

  81.     /** International symbols for parsing. */
  82.     private static final DecimalFormatSymbols SYMBOLS =
  83.         new DecimalFormatSymbols(Locale.US);

  84.     /** Serializable UID. */
  85.     private static final long serialVersionUID = -1596648022319057689L;

  86.     /** The satellite number. */
  87.     private final int satelliteNumber;

  88.     /** Classification (U for unclassified). */
  89.     private final char classification;

  90.     /** Launch year. */
  91.     private final int launchYear;

  92.     /** Launch number. */
  93.     private final int launchNumber;

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

  96.     /** Type of ephemeris. */
  97.     private final int ephemerisType;

  98.     /** Element number. */
  99.     private final int elementNumber;

  100.     /** the TLE current date. */
  101.     private final AbsoluteDate epoch;

  102.     /** Mean motion (rad/s). */
  103.     private final double meanMotion;

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

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

  108.     /** Eccentricity. */
  109.     private final double eccentricity;

  110.     /** Inclination (rad). */
  111.     private final double inclination;

  112.     /** Argument of perigee (rad). */
  113.     private final double pa;

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

  116.     /** Mean anomaly (rad). */
  117.     private final double meanAnomaly;

  118.     /** Revolution number at epoch. */
  119.     private final int revolutionNumberAtEpoch;

  120.     /** Ballistic coefficient. */
  121.     private final double bStar;

  122.     /** First line. */
  123.     private String line1;

  124.     /** Second line. */
  125.     private String line2;

  126.     /** The UTC scale. */
  127.     private final TimeScale utc;

  128.     /** Simple constructor from unparsed two lines. This constructor uses the {@link
  129.      * DataContext#getDefault() default data context}.
  130.      *
  131.      * <p>The static method {@link #isFormatOK(String, String)} should be called
  132.      * before trying to build this object.<p>
  133.      * @param line1 the first element (69 char String)
  134.      * @param line2 the second element (69 char String)
  135.      * @see #TLE(String, String, TimeScale)
  136.      */
  137.     @DefaultDataContext
  138.     public TLE(final String line1, final String line2) {
  139.         this(line1, line2, DataContext.getDefault().getTimeScales().getUTC());
  140.     }

  141.     /** Simple constructor from unparsed two lines using the given time scale as UTC.
  142.      *
  143.      * <p>The static method {@link #isFormatOK(String, String)} should be called
  144.      * before trying to build this object.<p>
  145.      * @param line1 the first element (69 char String)
  146.      * @param line2 the second element (69 char String)
  147.      * @param utc the UTC time scale.
  148.      * @since 10.1
  149.      */
  150.     public TLE(final String line1, final String line2, final TimeScale utc) {

  151.         // identification
  152.         satelliteNumber = parseInteger(line1, 2, 5);
  153.         final int satNum2 = parseInteger(line2, 2, 5);
  154.         if (satelliteNumber != satNum2) {
  155.             throw new OrekitException(OrekitMessages.TLE_LINES_DO_NOT_REFER_TO_SAME_OBJECT,
  156.                                       line1, line2);
  157.         }
  158.         classification  = line1.charAt(7);
  159.         launchYear      = parseYear(line1, 9);
  160.         launchNumber    = parseInteger(line1, 11, 3);
  161.         launchPiece     = line1.substring(14, 17).trim();
  162.         ephemerisType   = parseInteger(line1, 62, 1);
  163.         elementNumber   = parseInteger(line1, 64, 4);

  164.         // Date format transform (nota: 27/31250 == 86400/100000000)
  165.         final int    year      = parseYear(line1, 18);
  166.         final int    dayInYear = parseInteger(line1, 20, 3);
  167.         final long   df        = 27l * parseInteger(line1, 24, 8);
  168.         final int    secondsA  = (int) (df / 31250l);
  169.         final double secondsB  = (df % 31250l) / 31250.0;
  170.         epoch = new AbsoluteDate(new DateComponents(year, dayInYear),
  171.                                  new TimeComponents(secondsA, secondsB),
  172.                                  utc);

  173.         // mean motion development
  174.         // converted from rev/day, 2 * rev/day^2 and 6 * rev/day^3 to rad/s, rad/s^2 and rad/s^3
  175.         meanMotion                 = parseDouble(line2, 52, 11) * FastMath.PI / 43200.0;
  176.         meanMotionFirstDerivative  = parseDouble(line1, 33, 10) * FastMath.PI / 1.86624e9;
  177.         meanMotionSecondDerivative = Double.parseDouble((line1.substring(44, 45) + '.' +
  178.                                                          line1.substring(45, 50) + 'e' +
  179.                                                          line1.substring(50, 52)).replace(' ', '0')) *
  180.                                      FastMath.PI / 5.3747712e13;

  181.         eccentricity = Double.parseDouble("." + line2.substring(26, 33).replace(' ', '0'));
  182.         inclination  = FastMath.toRadians(parseDouble(line2, 8, 8));
  183.         pa           = FastMath.toRadians(parseDouble(line2, 34, 8));
  184.         raan         = FastMath.toRadians(Double.parseDouble(line2.substring(17, 25).replace(' ', '0')));
  185.         meanAnomaly  = FastMath.toRadians(parseDouble(line2, 43, 8));

  186.         revolutionNumberAtEpoch = parseInteger(line2, 63, 5);
  187.         bStar = Double.parseDouble((line1.substring(53, 54) + '.' +
  188.                                     line1.substring(54, 59) + 'e' +
  189.                                     line1.substring(59, 61)).replace(' ', '0'));

  190.         // save the lines
  191.         this.line1 = line1;
  192.         this.line2 = line2;
  193.         this.utc = utc;

  194.     }

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

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

  299.         // identification
  300.         this.satelliteNumber = satelliteNumber;
  301.         this.classification  = classification;
  302.         this.launchYear      = launchYear;
  303.         this.launchNumber    = launchNumber;
  304.         this.launchPiece     = launchPiece;
  305.         this.ephemerisType   = ephemerisType;
  306.         this.elementNumber   = elementNumber;

  307.         // orbital parameters
  308.         this.epoch = epoch;
  309.         // Checking mean motion range
  310.         checkParameterRangeInclusive(MEAN_MOTION, meanMotion, 0.0, Double.POSITIVE_INFINITY);
  311.         this.meanMotion = meanMotion;
  312.         this.meanMotionFirstDerivative = meanMotionFirstDerivative;
  313.         this.meanMotionSecondDerivative = meanMotionSecondDerivative;

  314.         // Checking inclination range
  315.         checkParameterRangeInclusive(INCLINATION, i, 0, FastMath.PI);
  316.         this.inclination = i;

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

  319.         // Checking eccentricity range
  320.         checkParameterRangeInclusive(ECCENTRICITY, e, 0.0, 1.0);
  321.         this.eccentricity = e;

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

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

  326.         this.revolutionNumberAtEpoch = revolutionNumberAtEpoch;
  327.         this.bStar = bStar;

  328.         // don't build the line until really needed
  329.         this.line1 = null;
  330.         this.line2 = null;
  331.         this.utc = utc;

  332.     }

  333.     /**
  334.      * Get the UTC time scale used to create this TLE.
  335.      *
  336.      * @return UTC time scale.
  337.      */
  338.     TimeScale getUtc() {
  339.         return utc;
  340.     }

  341.     /** Get the first line.
  342.      * @return first line
  343.      */
  344.     public String getLine1() {
  345.         if (line1 == null) {
  346.             buildLine1();
  347.         }
  348.         return line1;
  349.     }

  350.     /** Get the second line.
  351.      * @return second line
  352.      */
  353.     public String getLine2() {
  354.         if (line2 == null) {
  355.             buildLine2();
  356.         }
  357.         return line2;
  358.     }

  359.     /** Build the line 1 from the parsed elements.
  360.      */
  361.     private void buildLine1() {

  362.         final StringBuffer buffer = new StringBuffer();

  363.         buffer.append('1');

  364.         buffer.append(' ');
  365.         buffer.append(addPadding("satelliteNumber-1", satelliteNumber, '0', 5, true));
  366.         buffer.append(classification);

  367.         buffer.append(' ');
  368.         buffer.append(addPadding("launchYear",   launchYear % 100, '0', 2, true));
  369.         buffer.append(addPadding("launchNumber", launchNumber, '0', 3, true));
  370.         buffer.append(addPadding("launchPiece",  launchPiece, ' ', 3, false));

  371.         buffer.append(' ');
  372.         final DateTimeComponents dtc = epoch.getComponents(utc);
  373.         buffer.append(addPadding("year", dtc.getDate().getYear() % 100, '0', 2, true));
  374.         buffer.append(addPadding("day",  dtc.getDate().getDayOfYear(),  '0', 3, true));
  375.         buffer.append('.');
  376.         // nota: 31250/27 == 100000000/86400
  377.         final int fraction = (int) FastMath.rint(31250 * dtc.getTime().getSecondsInUTCDay() / 27.0);
  378.         buffer.append(addPadding("fraction", fraction,  '0', 8, true));

  379.         buffer.append(' ');
  380.         final double n1 = meanMotionFirstDerivative * 1.86624e9 / FastMath.PI;
  381.         final String sn1 = addPadding("meanMotionFirstDerivative",
  382.                                       new DecimalFormat(".00000000", SYMBOLS).format(n1), ' ', 10, true);
  383.         buffer.append(sn1);

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

  387.         buffer.append(' ');
  388.         buffer.append(formatExponentMarkerFree("B*", bStar, 5, ' ', 8, true));

  389.         buffer.append(' ');
  390.         buffer.append(ephemerisType);

  391.         buffer.append(' ');
  392.         buffer.append(addPadding("elementNumber", elementNumber, ' ', 4, true));

  393.         buffer.append(Integer.toString(checksum(buffer)));

  394.         line1 = buffer.toString();

  395.     }

  396.     /** Format a real number without 'e' exponent marker.
  397.      * @param name parameter name
  398.      * @param d number to format
  399.      * @param mantissaSize size of the mantissa (not counting initial '-' or ' ' for sign)
  400.      * @param c padding character
  401.      * @param size desired size
  402.      * @param rightJustified if true, the resulting string is
  403.      * right justified (i.e. space are added to the left)
  404.      * @return formatted and padded number
  405.      */
  406.     private String formatExponentMarkerFree(final String name, final double d, final int mantissaSize,
  407.                                             final char c, final int size, final boolean rightJustified) {
  408.         final double dAbs = FastMath.abs(d);
  409.         int exponent = (dAbs < 1.0e-9) ? -9 : (int) FastMath.ceil(FastMath.log10(dAbs));
  410.         long mantissa = FastMath.round(dAbs * FastMath.pow(10.0, mantissaSize - exponent));
  411.         if (mantissa == 0) {
  412.             exponent = 0;
  413.         } else if (mantissa > (ArithmeticUtils.pow(10, mantissaSize) - 1)) {
  414.             // rare case: if d has a single digit like d = 1.0e-4 with mantissaSize = 5
  415.             // the above computation finds exponent = -4 and mantissa = 100000 which
  416.             // doesn't fit in a 5 digits string
  417.             exponent++;
  418.             mantissa = FastMath.round(dAbs * FastMath.pow(10.0, mantissaSize - exponent));
  419.         }
  420.         final String sMantissa = addPadding(name, (int) mantissa, '0', mantissaSize, true);
  421.         final String sExponent = Integer.toString(FastMath.abs(exponent));
  422.         final String formatted = (d <  0 ? '-' : ' ') + sMantissa + (exponent <= 0 ? '-' : '+') + sExponent;

  423.         return addPadding(name, formatted, c, size, rightJustified);

  424.     }

  425.     /** Build the line 2 from the parsed elements.
  426.      */
  427.     private void buildLine2() {

  428.         final StringBuffer buffer = new StringBuffer();
  429.         final DecimalFormat f34   = new DecimalFormat("##0.0000", SYMBOLS);
  430.         final DecimalFormat f211  = new DecimalFormat("#0.00000000", SYMBOLS);

  431.         buffer.append('2');

  432.         buffer.append(' ');
  433.         buffer.append(addPadding("satelliteNumber-2", satelliteNumber, '0', 5, true));

  434.         buffer.append(' ');
  435.         buffer.append(addPadding(INCLINATION, f34.format(FastMath.toDegrees(inclination)), ' ', 8, true));
  436.         buffer.append(' ');
  437.         buffer.append(addPadding("raan", f34.format(FastMath.toDegrees(raan)), ' ', 8, true));
  438.         buffer.append(' ');
  439.         buffer.append(addPadding(ECCENTRICITY, (int) FastMath.rint(eccentricity * 1.0e7), '0', 7, true));
  440.         buffer.append(' ');
  441.         buffer.append(addPadding("pa", f34.format(FastMath.toDegrees(pa)), ' ', 8, true));
  442.         buffer.append(' ');
  443.         buffer.append(addPadding("meanAnomaly", f34.format(FastMath.toDegrees(meanAnomaly)), ' ', 8, true));

  444.         buffer.append(' ');
  445.         buffer.append(addPadding(MEAN_MOTION, f211.format(meanMotion * 43200.0 / FastMath.PI), ' ', 11, true));
  446.         buffer.append(addPadding("revolutionNumberAtEpoch", revolutionNumberAtEpoch, ' ', 5, true));

  447.         buffer.append(Integer.toString(checksum(buffer)));

  448.         line2 = buffer.toString();

  449.     }

  450.     /** Add padding characters before an integer.
  451.      * @param name parameter name
  452.      * @param k integer to pad
  453.      * @param c padding character
  454.      * @param size desired size
  455.      * @param rightJustified if true, the resulting string is
  456.      * right justified (i.e. space are added to the left)
  457.      * @return padded string
  458.      */
  459.     private String addPadding(final String name, final int k, final char c,
  460.                               final int size, final boolean rightJustified) {
  461.         return addPadding(name, Integer.toString(k), c, size, rightJustified);
  462.     }

  463.     /** Add padding characters to a string.
  464.      * @param name parameter name
  465.      * @param string string to pad
  466.      * @param c padding character
  467.      * @param size desired size
  468.      * @param rightJustified if true, the resulting string is
  469.      * right justified (i.e. space are added to the left)
  470.      * @return padded string
  471.      */
  472.     private String addPadding(final String name, final String string, final char c,
  473.                               final int size, final boolean rightJustified) {

  474.         if (string.length() > size) {
  475.             throw new OrekitException(OrekitMessages.TLE_INVALID_PARAMETER,
  476.                                       satelliteNumber, name, string);
  477.         }

  478.         final StringBuffer padding = new StringBuffer();
  479.         for (int i = 0; i < size; ++i) {
  480.             padding.append(c);
  481.         }

  482.         if (rightJustified) {
  483.             final String concatenated = padding + string;
  484.             final int l = concatenated.length();
  485.             return concatenated.substring(l - size, l);
  486.         }

  487.         return (string + padding).substring(0, size);

  488.     }

  489.     /** Parse a double.
  490.      * @param line line to parse
  491.      * @param start start index of the first character
  492.      * @param length length of the string
  493.      * @return value of the double
  494.      */
  495.     private double parseDouble(final String line, final int start, final int length) {
  496.         final String field = line.substring(start, start + length).trim();
  497.         return field.length() > 0 ? Double.parseDouble(field.replace(' ', '0')) : 0;
  498.     }

  499.     /** Parse an integer.
  500.      * @param line line to parse
  501.      * @param start start index of the first character
  502.      * @param length length of the string
  503.      * @return value of the integer
  504.      */
  505.     private int parseInteger(final String line, final int start, final int length) {
  506.         final String field = line.substring(start, start + length).trim();
  507.         return field.length() > 0 ? Integer.parseInt(field.replace(' ', '0')) : 0;
  508.     }

  509.     /** Parse a year written on 2 digits.
  510.      * @param line line to parse
  511.      * @param start start index of the first character
  512.      * @return value of the year
  513.      */
  514.     private int parseYear(final String line, final int start) {
  515.         final int year = 2000 + parseInteger(line, start, 2);
  516.         return (year > 2056) ? (year - 100) : year;
  517.     }

  518.     /** Get the satellite id.
  519.      * @return the satellite number
  520.      */
  521.     public int getSatelliteNumber() {
  522.         return satelliteNumber;
  523.     }

  524.     /** Get the classification.
  525.      * @return classification
  526.      */
  527.     public char getClassification() {
  528.         return classification;
  529.     }

  530.     /** Get the launch year.
  531.      * @return the launch year
  532.      */
  533.     public int getLaunchYear() {
  534.         return launchYear;
  535.     }

  536.     /** Get the launch number.
  537.      * @return the launch number
  538.      */
  539.     public int getLaunchNumber() {
  540.         return launchNumber;
  541.     }

  542.     /** Get the launch piece.
  543.      * @return the launch piece
  544.      */
  545.     public String getLaunchPiece() {
  546.         return launchPiece;
  547.     }

  548.     /** Get the type of ephemeris.
  549.      * @return the ephemeris type (one of {@link #DEFAULT}, {@link #SGP},
  550.      * {@link #SGP4}, {@link #SGP8}, {@link #SDP4}, {@link #SDP8})
  551.      */
  552.     public int getEphemerisType() {
  553.         return ephemerisType;
  554.     }

  555.     /** Get the element number.
  556.      * @return the element number
  557.      */
  558.     public int getElementNumber() {
  559.         return elementNumber;
  560.     }

  561.     /** Get the TLE current date.
  562.      * @return the epoch
  563.      */
  564.     public AbsoluteDate getDate() {
  565.         return epoch;
  566.     }

  567.     /** Get the mean motion.
  568.      * @return the mean motion (rad/s)
  569.      */
  570.     public double getMeanMotion() {
  571.         return meanMotion;
  572.     }

  573.     /** Get the mean motion first derivative.
  574.      * @return the mean motion first derivative (rad/s²)
  575.      */
  576.     public double getMeanMotionFirstDerivative() {
  577.         return meanMotionFirstDerivative;
  578.     }

  579.     /** Get the mean motion second derivative.
  580.      * @return the mean motion second derivative (rad/s³)
  581.      */
  582.     public double getMeanMotionSecondDerivative() {
  583.         return meanMotionSecondDerivative;
  584.     }

  585.     /** Get the eccentricity.
  586.      * @return the eccentricity
  587.      */
  588.     public double getE() {
  589.         return eccentricity;
  590.     }

  591.     /** Get the inclination.
  592.      * @return the inclination (rad)
  593.      */
  594.     public double getI() {
  595.         return inclination;
  596.     }

  597.     /** Get the argument of perigee.
  598.      * @return omega (rad)
  599.      */
  600.     public double getPerigeeArgument() {
  601.         return pa;
  602.     }

  603.     /** Get Right Ascension of the Ascending node.
  604.      * @return the raan (rad)
  605.      */
  606.     public double getRaan() {
  607.         return raan;
  608.     }

  609.     /** Get the mean anomaly.
  610.      * @return the mean anomaly (rad)
  611.      */
  612.     public double getMeanAnomaly() {
  613.         return meanAnomaly;
  614.     }

  615.     /** Get the revolution number.
  616.      * @return the revolutionNumberAtEpoch
  617.      */
  618.     public int getRevolutionNumberAtEpoch() {
  619.         return revolutionNumberAtEpoch;
  620.     }

  621.     /** Get the ballistic coefficient.
  622.      * @return bStar
  623.      */
  624.     public double getBStar() {
  625.         return bStar;
  626.     }

  627.     /** Get a string representation of this TLE set.
  628.      * <p>The representation is simply the two lines separated by the
  629.      * platform line separator.</p>
  630.      * @return string representation of this TLE set
  631.      */
  632.     public String toString() {
  633.         try {
  634.             return getLine1() + System.getProperty("line.separator") + getLine2();
  635.         } catch (OrekitException oe) {
  636.             throw new OrekitInternalError(oe);
  637.         }
  638.     }

  639.     /** Check the lines format validity.
  640.      * @param line1 the first element
  641.      * @param line2 the second element
  642.      * @return true if format is recognized (non null lines, 69 characters length,
  643.      * line content), false if not
  644.      */
  645.     public static boolean isFormatOK(final String line1, final String line2) {

  646.         if (line1 == null || line1.length() != 69 ||
  647.             line2 == null || line2.length() != 69) {
  648.             return false;
  649.         }

  650.         if (!(LINE_1_PATTERN.matcher(line1).matches() &&
  651.               LINE_2_PATTERN.matcher(line2).matches())) {
  652.             return false;
  653.         }

  654.         // check sums
  655.         final int checksum1 = checksum(line1);
  656.         if (Integer.parseInt(line1.substring(68)) != (checksum1 % 10)) {
  657.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  658.                                       1, Integer.toString(checksum1 % 10), line1.substring(68), line1);
  659.         }

  660.         final int checksum2 = checksum(line2);
  661.         if (Integer.parseInt(line2.substring(68)) != (checksum2 % 10)) {
  662.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  663.                                       2, Integer.toString(checksum2 % 10), line2.substring(68), line2);
  664.         }

  665.         return true;

  666.     }

  667.     /** Compute the checksum of the first 68 characters of a line.
  668.      * @param line line to check
  669.      * @return checksum
  670.      */
  671.     private static int checksum(final CharSequence line) {
  672.         int sum = 0;
  673.         for (int j = 0; j < 68; j++) {
  674.             final char c = line.charAt(j);
  675.             if (Character.isDigit(c)) {
  676.                 sum += Character.digit(c, 10);
  677.             } else if (c == '-') {
  678.                 ++sum;
  679.             }
  680.         }
  681.         return sum % 10;
  682.     }

  683.     /**
  684.      * <p>
  685.      * Check if the given parameter is within an acceptable range.
  686.      * The bounds are inclusive: an exception is raised when either of those conditions are met:
  687.      * <ul>
  688.      *     <li>The parameter is strictly greater than upperBound</li>
  689.      *     <li>The parameter is strictly lower than lowerBound</li>
  690.      * </ul>
  691.      * </p>
  692.      * <p>
  693.      * In either of these cases, an OrekitException is raised with a TLE_INVALID_PARAMETER_RANGE
  694.      * message, for instance:
  695.      * <pre>
  696.      *   "invalid TLE parameter eccentricity: 42.0 not in range [0.0, 1.0]"
  697.      * </pre>
  698.      * </p>
  699.      * @param parameterName name of the parameter
  700.      * @param parameter value of the parameter
  701.      * @param lowerBound lower bound of the acceptable range (inclusive)
  702.      * @param upperBound upper bound of the acceptable range (inclusive)
  703.      */
  704.     private void checkParameterRangeInclusive(final String parameterName, final double parameter,
  705.             final double lowerBound,
  706.             final double upperBound) {
  707.         if ((parameter < lowerBound) || (parameter > upperBound)) {
  708.             throw new OrekitException(OrekitMessages.TLE_INVALID_PARAMETER_RANGE, parameterName,
  709.                     parameter,
  710.                     lowerBound, upperBound);
  711.         }
  712.     }

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

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

  773. }