TLE.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.propagation.analytical.tle;

  18. import 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.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitInternalError;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.time.DateComponents;
  31. import org.orekit.time.DateTimeComponents;
  32. import org.orekit.time.TimeComponents;
  33. import org.orekit.time.TimeScalesFactory;
  34. import org.orekit.time.TimeStamped;

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

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

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

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

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

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

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

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

  68.     /** Pattern for line 2. */
  69.     private static final Pattern LINE_2_PATTERN =
  70.         Pattern.compile("2 [ 0-9]{5} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{7} " +
  71.                         "[ 0-9]{3}[.][ 0-9]{4} [ 0-9]{3}[.][ 0-9]{4} [ 0-9]{2}[.][ 0-9]{13}[ 0-9]");

  72.     /** International symbols for parsing. */
  73.     private static final DecimalFormatSymbols SYMBOLS =
  74.         new DecimalFormatSymbols(Locale.US);

  75.     /** Serializable UID. */
  76.     private static final long serialVersionUID = -1596648022319057689L;

  77.     /** The satellite number. */
  78.     private final int satelliteNumber;

  79.     /** Classification (U for unclassified). */
  80.     private final char classification;

  81.     /** Launch year. */
  82.     private final int launchYear;

  83.     /** Launch number. */
  84.     private final int launchNumber;

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

  87.     /** Type of ephemeris. */
  88.     private final int ephemerisType;

  89.     /** Element number. */
  90.     private final int elementNumber;

  91.     /** the TLE current date. */
  92.     private final AbsoluteDate epoch;

  93.     /** Mean motion (rad/s). */
  94.     private final double meanMotion;

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

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

  99.     /** Eccentricity. */
  100.     private final double eccentricity;

  101.     /** Inclination (rad). */
  102.     private final double inclination;

  103.     /** Argument of perigee (rad). */
  104.     private final double pa;

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

  107.     /** Mean anomaly (rad). */
  108.     private final double meanAnomaly;

  109.     /** Revolution number at epoch. */
  110.     private final int revolutionNumberAtEpoch;

  111.     /** Ballistic coefficient. */
  112.     private final double bStar;

  113.     /** First line. */
  114.     private String line1;

  115.     /** Second line. */
  116.     private String line2;

  117.     /** Simple constructor from unparsed two lines.
  118.      * <p>The static method {@link #isFormatOK(String, String)} should be called
  119.      * before trying to build this object.<p>
  120.      * @param line1 the first element (69 char String)
  121.      * @param line2 the second element (69 char String)
  122.      * @exception OrekitException if some format error occurs or lines are inconsistent
  123.      */
  124.     public TLE(final String line1, final String line2) throws OrekitException {

  125.         // identification
  126.         satelliteNumber = parseInteger(line1, 2, 5);
  127.         final int satNum2 = parseInteger(line2, 2, 5);
  128.         if (satelliteNumber != satNum2) {
  129.             throw new OrekitException(OrekitMessages.TLE_LINES_DO_NOT_REFER_TO_SAME_OBJECT,
  130.                                       line1, line2);
  131.         }
  132.         classification  = line1.charAt(7);
  133.         launchYear      = parseYear(line1, 9);
  134.         launchNumber    = parseInteger(line1, 11, 3);
  135.         launchPiece     = line1.substring(14, 17).trim();
  136.         ephemerisType   = parseInteger(line1, 62, 1);
  137.         elementNumber   = parseInteger(line1, 64, 4);

  138.         // Date format transform (nota: 27/31250 == 86400/100000000)
  139.         final int    year      = parseYear(line1, 18);
  140.         final int    dayInYear = parseInteger(line1, 20, 3);
  141.         final long   df        = 27l * parseInteger(line1, 24, 8);
  142.         final int    secondsA  = (int) (df / 31250l);
  143.         final double secondsB  = (df % 31250l) / 31250.0;
  144.         epoch = new AbsoluteDate(new DateComponents(year, dayInYear),
  145.                                  new TimeComponents(secondsA, secondsB),
  146.                                  TimeScalesFactory.getUTC());

  147.         // mean motion development
  148.         // converted from rev/day, 2 * rev/day^2 and 6 * rev/day^3 to rad/s, rad/s^2 and rad/s^3
  149.         meanMotion                 = parseDouble(line2, 52, 11) * FastMath.PI / 43200.0;
  150.         meanMotionFirstDerivative  = parseDouble(line1, 33, 10) * FastMath.PI / 1.86624e9;
  151.         meanMotionSecondDerivative = Double.parseDouble((line1.substring(44, 45) + '.' +
  152.                                                          line1.substring(45, 50) + 'e' +
  153.                                                          line1.substring(50, 52)).replace(' ', '0')) *
  154.                                      FastMath.PI / 5.3747712e13;

  155.         eccentricity = Double.parseDouble("." + line2.substring(26, 33).replace(' ', '0'));
  156.         inclination  = FastMath.toRadians(parseDouble(line2, 8, 8));
  157.         pa           = FastMath.toRadians(parseDouble(line2, 34, 8));
  158.         raan         = FastMath.toRadians(Double.parseDouble(line2.substring(17, 25).replace(' ', '0')));
  159.         meanAnomaly  = FastMath.toRadians(parseDouble(line2, 43, 8));

  160.         revolutionNumberAtEpoch = parseInteger(line2, 63, 5);
  161.         bStar = Double.parseDouble((line1.substring(53, 54) + '.' +
  162.                                     line1.substring(54, 59) + 'e' +
  163.                                     line1.substring(59, 61)).replace(' ', '0'));

  164.         // save the lines
  165.         this.line1 = line1;
  166.         this.line2 = line2;

  167.     }

  168.     /** Simple constructor from already parsed elements.
  169.      * @param satelliteNumber satellite number
  170.      * @param classification classification (U for unclassified)
  171.      * @param launchYear launch year (all digits)
  172.      * @param launchNumber launch number
  173.      * @param launchPiece launch piece
  174.      * @param ephemerisType type of ephemeris
  175.      * @param elementNumber element number
  176.      * @param epoch elements epoch
  177.      * @param meanMotion mean motion (rad/s)
  178.      * @param meanMotionFirstDerivative mean motion first derivative (rad/s²)
  179.      * @param meanMotionSecondDerivative mean motion second derivative (rad/s³)
  180.      * @param e eccentricity
  181.      * @param i inclination (rad)
  182.      * @param pa argument of perigee (rad)
  183.      * @param raan right ascension of ascending node (rad)
  184.      * @param meanAnomaly mean anomaly (rad)
  185.      * @param revolutionNumberAtEpoch revolution number at epoch
  186.      * @param bStar ballistic coefficient
  187.      */
  188.     public TLE(final int satelliteNumber, final char classification,
  189.                final int launchYear, final int launchNumber, final String launchPiece,
  190.                final int ephemerisType, final int elementNumber, final AbsoluteDate epoch,
  191.                final double meanMotion, final double meanMotionFirstDerivative,
  192.                final double meanMotionSecondDerivative, final double e, final double i,
  193.                final double pa, final double raan, final double meanAnomaly,
  194.                final int revolutionNumberAtEpoch, final double bStar) {

  195.         // identification
  196.         this.satelliteNumber = satelliteNumber;
  197.         this.classification  = classification;
  198.         this.launchYear      = launchYear;
  199.         this.launchNumber    = launchNumber;
  200.         this.launchPiece     = launchPiece;
  201.         this.ephemerisType   = ephemerisType;
  202.         this.elementNumber   = elementNumber;

  203.         // orbital parameters
  204.         this.epoch                      = epoch;
  205.         this.meanMotion                 = meanMotion;
  206.         this.meanMotionFirstDerivative  = meanMotionFirstDerivative;
  207.         this.meanMotionSecondDerivative = meanMotionSecondDerivative;
  208.         this.inclination                = i;
  209.         this.raan                       = raan;
  210.         this.eccentricity               = e;
  211.         this.pa                         = pa;
  212.         this.meanAnomaly                = meanAnomaly;

  213.         this.revolutionNumberAtEpoch = revolutionNumberAtEpoch;
  214.         this.bStar                   = bStar;

  215.         // don't build the line until really needed
  216.         this.line1 = null;
  217.         this.line2 = null;

  218.     }

  219.     /** Get the first line.
  220.      * @return first line
  221.      * @exception OrekitException if UTC conversion cannot be done or
  222.      * some parameter is too large to fit format
  223.      */
  224.     public String getLine1()
  225.         throws OrekitException {
  226.         if (line1 == null) {
  227.             buildLine1();
  228.         }
  229.         return line1;
  230.     }

  231.     /** Get the second line.
  232.      * @return second line
  233.      * @exception OrekitException if some parameter is too large to fit format
  234.      */
  235.     public String getLine2()
  236.         throws OrekitException {
  237.         if (line2 == null) {
  238.             buildLine2();
  239.         }
  240.         return line2;
  241.     }

  242.     /** Build the line 1 from the parsed elements.
  243.      * @exception OrekitException if UTC conversion cannot be done or
  244.      * some parameter is too large to fit format
  245.      */
  246.     private void buildLine1()
  247.         throws OrekitException {

  248.         final StringBuffer buffer = new StringBuffer();

  249.         buffer.append('1');

  250.         buffer.append(' ');
  251.         buffer.append(addPadding("satelliteNumber-1", satelliteNumber, '0', 5, true));
  252.         buffer.append(classification);

  253.         buffer.append(' ');
  254.         buffer.append(addPadding("launchYear",   launchYear % 100, '0', 2, true));
  255.         buffer.append(addPadding("launchNumber", launchNumber, '0', 3, true));
  256.         buffer.append(addPadding("launchPiece",  launchPiece, ' ', 3, false));

  257.         buffer.append(' ');
  258.         final DateTimeComponents dtc = epoch.getComponents(TimeScalesFactory.getUTC());
  259.         buffer.append(addPadding("year", dtc.getDate().getYear() % 100, '0', 2, true));
  260.         buffer.append(addPadding("day",  dtc.getDate().getDayOfYear(),  '0', 3, true));
  261.         buffer.append('.');
  262.         // nota: 31250/27 == 100000000/86400
  263.         final int fraction = (int) FastMath.rint(31250 * dtc.getTime().getSecondsInUTCDay() / 27.0);
  264.         buffer.append(addPadding("fraction", fraction,  '0', 8, true));

  265.         buffer.append(' ');
  266.         final double n1 = meanMotionFirstDerivative * 1.86624e9 / FastMath.PI;
  267.         final String sn1 = addPadding("meanMotionFirstDerivative",
  268.                                       new DecimalFormat(".00000000", SYMBOLS).format(n1), ' ', 10, true);
  269.         buffer.append(sn1);

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

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

  275.         buffer.append(' ');
  276.         buffer.append(ephemerisType);

  277.         buffer.append(' ');
  278.         buffer.append(addPadding("elementNumber", elementNumber, ' ', 4, true));

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

  280.         line1 = buffer.toString();

  281.     }

  282.     /** Format a real number without 'e' exponent marker.
  283.      * @param name parameter name
  284.      * @param d number to format
  285.      * @param mantissaSize size of the mantissa (not counting initial '-' or ' ' for sign)
  286.      * @param c padding character
  287.      * @param size desired size
  288.      * @param rightJustified if true, the resulting string is
  289.      * right justified (i.e. space are added to the left)
  290.      * @return formatted and padded number
  291.      * @exception OrekitException if parameter is too large to fit format
  292.      */
  293.     private String formatExponentMarkerFree(final String name, final double d, final int mantissaSize,
  294.                                             final char c, final int size, final boolean rightJustified)
  295.         throws OrekitException {
  296.         final double dAbs = FastMath.abs(d);
  297.         int exponent = (dAbs < 1.0e-9) ? -9 : (int) FastMath.ceil(FastMath.log10(dAbs));
  298.         long mantissa = FastMath.round(dAbs * FastMath.pow(10.0, mantissaSize - exponent));
  299.         if (mantissa == 0) {
  300.             exponent = 0;
  301.         } else if (mantissa > (ArithmeticUtils.pow(10, mantissaSize) - 1)) {
  302.             // rare case: if d has a single digit like d = 1.0e-4 with mantissaSize = 5
  303.             // the above computation finds exponent = -4 and mantissa = 100000 which
  304.             // doesn't fit in a 5 digits string
  305.             exponent++;
  306.             mantissa = FastMath.round(dAbs * FastMath.pow(10.0, mantissaSize - exponent));
  307.         }
  308.         final String sMantissa = addPadding(name, (int) mantissa, '0', mantissaSize, true);
  309.         final String sExponent = Integer.toString(FastMath.abs(exponent));
  310.         final String formatted = (d <  0 ? '-' : ' ') + sMantissa + (exponent <= 0 ? '-' : '+') + sExponent;

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

  312.     }

  313.     /** Build the line 2 from the parsed elements.
  314.      * @exception OrekitException if some parameter is too large to fit format
  315.      */
  316.     private void buildLine2() throws OrekitException {

  317.         final StringBuffer buffer = new StringBuffer();
  318.         final DecimalFormat f34   = new DecimalFormat("##0.0000", SYMBOLS);
  319.         final DecimalFormat f211  = new DecimalFormat("#0.00000000", SYMBOLS);

  320.         buffer.append('2');

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

  323.         buffer.append(' ');
  324.         buffer.append(addPadding("inclination", f34.format(FastMath.toDegrees(inclination)), ' ', 8, true));
  325.         buffer.append(' ');
  326.         buffer.append(addPadding("raan", f34.format(FastMath.toDegrees(raan)), ' ', 8, true));
  327.         buffer.append(' ');
  328.         buffer.append(addPadding("eccentricity", (int) FastMath.rint(eccentricity * 1.0e7), '0', 7, true));
  329.         buffer.append(' ');
  330.         buffer.append(addPadding("pa", f34.format(FastMath.toDegrees(pa)), ' ', 8, true));
  331.         buffer.append(' ');
  332.         buffer.append(addPadding("meanAnomaly", f34.format(FastMath.toDegrees(meanAnomaly)), ' ', 8, true));

  333.         buffer.append(' ');
  334.         buffer.append(addPadding("meanMotion", f211.format(meanMotion * 43200.0 / FastMath.PI), ' ', 11, true));
  335.         buffer.append(addPadding("revolutionNumberAtEpoch", revolutionNumberAtEpoch, ' ', 5, true));

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

  337.         line2 = buffer.toString();

  338.     }

  339.     /** Add padding characters before an integer.
  340.      * @param name parameter name
  341.      * @param k integer to pad
  342.      * @param c padding character
  343.      * @param size desired size
  344.      * @param rightJustified if true, the resulting string is
  345.      * right justified (i.e. space are added to the left)
  346.      * @return padded string
  347.      * @exception OrekitException if parameter is too large to fit format
  348.      */
  349.     private String addPadding(final String name, final int k, final char c,
  350.                               final int size, final boolean rightJustified)
  351.         throws OrekitException {
  352.         return addPadding(name, Integer.toString(k), c, size, rightJustified);
  353.     }

  354.     /** Add padding characters to a string.
  355.      * @param name parameter name
  356.      * @param string string to pad
  357.      * @param c padding character
  358.      * @param size desired size
  359.      * @param rightJustified if true, the resulting string is
  360.      * right justified (i.e. space are added to the left)
  361.      * @return padded string
  362.      * @exception OrekitException if parameter is too large to fit format
  363.      */
  364.     private String addPadding(final String name, final String string, final char c,
  365.                               final int size, final boolean rightJustified)
  366.         throws OrekitException {

  367.         if (string.length() > size) {
  368.             throw new OrekitException(OrekitMessages.TLE_INVALID_PARAMETER,
  369.                                       satelliteNumber, name, string);
  370.         }

  371.         final StringBuffer padding = new StringBuffer();
  372.         for (int i = 0; i < size; ++i) {
  373.             padding.append(c);
  374.         }

  375.         if (rightJustified) {
  376.             final String concatenated = padding + string;
  377.             final int l = concatenated.length();
  378.             return concatenated.substring(l - size, l);
  379.         }

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

  381.     }

  382.     /** Parse a double.
  383.      * @param line line to parse
  384.      * @param start start index of the first character
  385.      * @param length length of the string
  386.      * @return value of the double
  387.      */
  388.     private double parseDouble(final String line, final int start, final int length) {
  389.         final String field = line.substring(start, start + length).trim();
  390.         return field.length() > 0 ? Double.parseDouble(field.replace(' ', '0')) : 0;
  391.     }

  392.     /** Parse an integer.
  393.      * @param line line to parse
  394.      * @param start start index of the first character
  395.      * @param length length of the string
  396.      * @return value of the integer
  397.      */
  398.     private int parseInteger(final String line, final int start, final int length) {
  399.         final String field = line.substring(start, start + length).trim();
  400.         return field.length() > 0 ? Integer.parseInt(field.replace(' ', '0')) : 0;
  401.     }

  402.     /** Parse a year written on 2 digits.
  403.      * @param line line to parse
  404.      * @param start start index of the first character
  405.      * @return value of the year
  406.      */
  407.     private int parseYear(final String line, final int start) {
  408.         final int year = 2000 + parseInteger(line, start, 2);
  409.         return (year > 2056) ? (year - 100) : year;
  410.     }

  411.     /** Get the satellite id.
  412.      * @return the satellite number
  413.      */
  414.     public int getSatelliteNumber() {
  415.         return satelliteNumber;
  416.     }

  417.     /** Get the classification.
  418.      * @return classification
  419.      */
  420.     public char getClassification() {
  421.         return classification;
  422.     }

  423.     /** Get the launch year.
  424.      * @return the launch year
  425.      */
  426.     public int getLaunchYear() {
  427.         return launchYear;
  428.     }

  429.     /** Get the launch number.
  430.      * @return the launch number
  431.      */
  432.     public int getLaunchNumber() {
  433.         return launchNumber;
  434.     }

  435.     /** Get the launch piece.
  436.      * @return the launch piece
  437.      */
  438.     public String getLaunchPiece() {
  439.         return launchPiece;
  440.     }

  441.     /** Get the type of ephemeris.
  442.      * @return the ephemeris type (one of {@link #DEFAULT}, {@link #SGP},
  443.      * {@link #SGP4}, {@link #SGP8}, {@link #SDP4}, {@link #SDP8})
  444.      */
  445.     public int getEphemerisType() {
  446.         return ephemerisType;
  447.     }

  448.     /** Get the element number.
  449.      * @return the element number
  450.      */
  451.     public int getElementNumber() {
  452.         return elementNumber;
  453.     }

  454.     /** Get the TLE current date.
  455.      * @return the epoch
  456.      */
  457.     public AbsoluteDate getDate() {
  458.         return epoch;
  459.     }

  460.     /** Get the mean motion.
  461.      * @return the mean motion (rad/s)
  462.      */
  463.     public double getMeanMotion() {
  464.         return meanMotion;
  465.     }

  466.     /** Get the mean motion first derivative.
  467.      * @return the mean motion first derivative (rad/s²)
  468.      */
  469.     public double getMeanMotionFirstDerivative() {
  470.         return meanMotionFirstDerivative;
  471.     }

  472.     /** Get the mean motion second derivative.
  473.      * @return the mean motion second derivative (rad/s³)
  474.      */
  475.     public double getMeanMotionSecondDerivative() {
  476.         return meanMotionSecondDerivative;
  477.     }

  478.     /** Get the eccentricity.
  479.      * @return the eccentricity
  480.      */
  481.     public double getE() {
  482.         return eccentricity;
  483.     }

  484.     /** Get the inclination.
  485.      * @return the inclination (rad)
  486.      */
  487.     public double getI() {
  488.         return inclination;
  489.     }

  490.     /** Get the argument of perigee.
  491.      * @return omega (rad)
  492.      */
  493.     public double getPerigeeArgument() {
  494.         return pa;
  495.     }

  496.     /** Get Right Ascension of the Ascending node.
  497.      * @return the raan (rad)
  498.      */
  499.     public double getRaan() {
  500.         return raan;
  501.     }

  502.     /** Get the mean anomaly.
  503.      * @return the mean anomaly (rad)
  504.      */
  505.     public double getMeanAnomaly() {
  506.         return meanAnomaly;
  507.     }

  508.     /** Get the revolution number.
  509.      * @return the revolutionNumberAtEpoch
  510.      */
  511.     public int getRevolutionNumberAtEpoch() {
  512.         return revolutionNumberAtEpoch;
  513.     }

  514.     /** Get the ballistic coefficient.
  515.      * @return bStar
  516.      */
  517.     public double getBStar() {
  518.         return bStar;
  519.     }

  520.     /** Get a string representation of this TLE set.
  521.      * <p>The representation is simply the two lines separated by the
  522.      * platform line separator.</p>
  523.      * @return string representation of this TLE set
  524.      */
  525.     public String toString() {
  526.         try {
  527.             return getLine1() + System.getProperty("line.separator") + getLine2();
  528.         } catch (OrekitException oe) {
  529.             throw new OrekitInternalError(oe);
  530.         }
  531.     }

  532.     /** Check the lines format validity.
  533.      * @param line1 the first element
  534.      * @param line2 the second element
  535.      * @return true if format is recognized (non null lines, 69 characters length,
  536.      * line content), false if not
  537.      * @exception OrekitException if checksum is not valid
  538.      */
  539.     public static boolean isFormatOK(final String line1, final String line2)
  540.         throws OrekitException {

  541.         if (line1 == null || line1.length() != 69 ||
  542.             line2 == null || line2.length() != 69) {
  543.             return false;
  544.         }

  545.         if (!(LINE_1_PATTERN.matcher(line1).matches() &&
  546.               LINE_2_PATTERN.matcher(line2).matches())) {
  547.             return false;
  548.         }

  549.         // check sums
  550.         final int checksum1 = checksum(line1);
  551.         if (Integer.parseInt(line1.substring(68)) != (checksum1 % 10)) {
  552.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  553.                                       1, line1.substring(68), checksum1 % 10, line1);
  554.         }

  555.         final int checksum2 = checksum(line2);
  556.         if (Integer.parseInt(line2.substring(68)) != (checksum2 % 10)) {
  557.             throw new OrekitException(OrekitMessages.TLE_CHECKSUM_ERROR,
  558.                                       2, line2.substring(68), checksum2 % 10, line2);
  559.         }

  560.         return true;

  561.     }

  562.     /** Compute the checksum of the first 68 characters of a line.
  563.      * @param line line to check
  564.      * @return checksum
  565.      */
  566.     private static int checksum(final CharSequence line) {
  567.         int sum = 0;
  568.         for (int j = 0; j < 68; j++) {
  569.             final char c = line.charAt(j);
  570.             if (Character.isDigit(c)) {
  571.                 sum += Character.digit(c, 10);
  572.             } else if (c == '-') {
  573.                 ++sum;
  574.             }
  575.         }
  576.         return sum % 10;
  577.     }

  578.     /** Check if this tle equals the provided tle.
  579.      * <p>Due to the difference in precision between object and string
  580.      * representations of TLE, it is possible for this method to return false
  581.      * even if string representations returned by {@link #toString()}
  582.      * are equal.</p>
  583.      * @param o other tle
  584.      * @return true if this tle equals the provided tle
  585.      */
  586.     @Override
  587.     public boolean equals(final Object o) {
  588.         if (o == this) {
  589.             return true;
  590.         }
  591.         if (!(o instanceof TLE)) {
  592.             return false;
  593.         }
  594.         final TLE tle = (TLE) o;
  595.         return satelliteNumber == tle.satelliteNumber &&
  596.                 classification == tle.classification &&
  597.                 launchYear == tle.launchYear &&
  598.                 launchNumber == tle.launchNumber &&
  599.                 Objects.equals(launchPiece, tle.launchPiece) &&
  600.                 ephemerisType == tle.ephemerisType &&
  601.                 elementNumber == tle.elementNumber &&
  602.                 Objects.equals(epoch, tle.epoch) &&
  603.                 meanMotion == tle.meanMotion &&
  604.                 meanMotionFirstDerivative == tle.meanMotionFirstDerivative &&
  605.                 meanMotionSecondDerivative == tle.meanMotionSecondDerivative &&
  606.                 eccentricity == tle.eccentricity &&
  607.                 inclination == tle.inclination &&
  608.                 pa == tle.pa &&
  609.                 raan == tle.raan &&
  610.                 meanAnomaly == tle.meanAnomaly &&
  611.                 revolutionNumberAtEpoch == tle.revolutionNumberAtEpoch &&
  612.                 bStar == tle.bStar;
  613.     }

  614.     /** Get a hashcode for this tle.
  615.      * @return hashcode
  616.      */
  617.     @Override
  618.     public int hashCode() {
  619.         return Objects.hash(satelliteNumber,
  620.                 classification,
  621.                 launchYear,
  622.                 launchNumber,
  623.                 launchPiece,
  624.                 ephemerisType,
  625.                 elementNumber,
  626.                 epoch,
  627.                 meanMotion,
  628.                 meanMotionFirstDerivative,
  629.                 meanMotionSecondDerivative,
  630.                 eccentricity,
  631.                 inclination,
  632.                 pa,
  633.                 raan,
  634.                 meanAnomaly,
  635.                 revolutionNumberAtEpoch,
  636.                 bStar);
  637.     }

  638. }