ODMParser.java

  1. /* Copyright 2002-2019 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.files.ccsds;

  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.List;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;

  24. import org.hipparchus.util.FastMath;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitMessages;
  27. import org.orekit.time.AbsoluteDate;
  28. import org.orekit.time.TimeScalesFactory;
  29. import org.orekit.utils.IERSConventions;

  30. /**
  31.  * Base class for all CCSDS Orbit Data Message parsers.
  32.  *
  33.  * <p> This base class is immutable, and hence thread safe. When parts must be
  34.  * changed, such as reference date for Mission Elapsed Time or Mission Relative
  35.  * Time time systems, or the gravitational coefficient or the IERS conventions,
  36.  * the various {@code withXxx} methods must be called, which create a new
  37.  * immutable instance with the new parameters. This is a combination of the <a
  38.  * href="https://en.wikipedia.org/wiki/Builder_pattern">builder design
  39.  * pattern</a> and a <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
  40.  * interface</a>.
  41.  *
  42.  * @author Luc Maisonobe
  43.  * @since 6.1
  44.  */
  45. public abstract class ODMParser {

  46.     /** Pattern for international designator. */
  47.     private static final Pattern INTERNATIONAL_DESIGNATOR = Pattern.compile("(\\p{Digit}{4})-(\\p{Digit}{3})(\\p{Upper}{1,3})");

  48.     /** Reference date for Mission Elapsed Time or Mission Relative Time time systems. */
  49.     private final AbsoluteDate missionReferenceDate;

  50.     /** Gravitational coefficient. */
  51.     private final  double mu;

  52.     /** IERS Conventions. */
  53.     private final  IERSConventions conventions;

  54.     /** Indicator for simple or accurate EOP interpolation. */
  55.     private final  boolean simpleEOP;

  56.     /** Launch Year. */
  57.     private int launchYear;

  58.     /** Launch number. */
  59.     private int launchNumber;

  60.     /** Piece of launch (from "A" to "ZZZ"). */
  61.     private String launchPiece;

  62.     /** Complete constructor.
  63.      * @param missionReferenceDate reference date for Mission Elapsed Time or Mission Relative Time time systems
  64.      * @param mu gravitational coefficient
  65.      * @param conventions IERS Conventions
  66.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  67.      * @param launchYear launch year for TLEs
  68.      * @param launchNumber launch number for TLEs
  69.      * @param launchPiece piece of launch (from "A" to "ZZZ") for TLEs
  70.      */
  71.     protected ODMParser(final AbsoluteDate missionReferenceDate, final double mu,
  72.                         final IERSConventions conventions, final boolean simpleEOP,
  73.                         final int launchYear, final int launchNumber, final String launchPiece) {
  74.         this.missionReferenceDate = missionReferenceDate;
  75.         this.mu                   = mu;
  76.         this.conventions          = conventions;
  77.         this.simpleEOP            = simpleEOP;
  78.         this.launchYear           = launchYear;
  79.         this.launchNumber         = launchNumber;
  80.         this.launchPiece          = launchPiece;
  81.     }

  82.     /** Set initial date.
  83.      * @param newMissionReferenceDate mission reference date to use while parsing
  84.      * @return a new instance, with mission reference date replaced
  85.      * @see #getMissionReferenceDate()
  86.      */
  87.     public abstract ODMParser withMissionReferenceDate(AbsoluteDate newMissionReferenceDate);

  88.     /** Get initial date.
  89.      * @return mission reference date to use while parsing
  90.      * @see #withMissionReferenceDate(AbsoluteDate)
  91.      */
  92.     public AbsoluteDate getMissionReferenceDate() {
  93.         return missionReferenceDate;
  94.     }

  95.     /** Set gravitational coefficient.
  96.      * @param newMu gravitational coefficient to use while parsing
  97.      * @return a new instance, with gravitational coefficient date replaced
  98.      * @see #getMu()
  99.      */
  100.     public abstract ODMParser withMu(double newMu);

  101.     /** Get gravitational coefficient.
  102.      * @return gravitational coefficient to use while parsing
  103.      * @see #withMu(double)
  104.      */
  105.     public double getMu() {
  106.         return mu;
  107.     }

  108.     /** Set IERS conventions.
  109.      * @param newConventions IERS conventions to use while parsing
  110.      * @return a new instance, with IERS conventions replaced
  111.      * @see #getConventions()
  112.      */
  113.     public abstract ODMParser withConventions(IERSConventions newConventions);

  114.     /** Get IERS conventions.
  115.      * @return IERS conventions to use while parsing
  116.      * @see #withConventions(IERSConventions)
  117.      */
  118.     public IERSConventions getConventions() {
  119.         return conventions;
  120.     }

  121.     /** Set EOP interpolation method.
  122.      * @param newSimpleEOP if true, tidal effects are ignored when interpolating EOP
  123.      * @return a new instance, with EOP interpolation method replaced
  124.      * @see #isSimpleEOP()
  125.      */
  126.     public abstract ODMParser withSimpleEOP(boolean newSimpleEOP);

  127.     /** Get EOP interpolation method.
  128.      * @return true if tidal effects are ignored when interpolating EOP
  129.      * @see #withSimpleEOP(boolean)
  130.      */
  131.     public boolean isSimpleEOP() {
  132.         return simpleEOP;
  133.     }

  134.     /** Set international designator.
  135.      * <p>
  136.      * This method may be used to ensure the launch year number and pieces are
  137.      * correctly set if they are not present in the CCSDS file header in the
  138.      * OBJECT_ID in the form YYYY-NNN-P{PP}. If they are already in the header,
  139.      * they will be parsed automatically regardless of this method being called
  140.      * or not (i.e. header information override information set here).
  141.      * </p>
  142.      * @param newLaunchYear launch year
  143.      * @param newLaunchNumber launch number
  144.      * @param newLaunchPiece piece of launch (from "A" to "ZZZ")
  145.      * @return a new instance, with TLE settings replaced
  146.      */
  147.     public abstract ODMParser withInternationalDesignator(int newLaunchYear,
  148.                                                           int newLaunchNumber,
  149.                                                           String newLaunchPiece);

  150.     /** Get the launch year.
  151.      * @return launch year
  152.      */
  153.     public int getLaunchYear() {
  154.         return launchYear;
  155.     }

  156.     /** Get the launch number.
  157.      * @return launch number
  158.      */
  159.     public int getLaunchNumber() {
  160.         return launchNumber;
  161.     }

  162.     /** Get the piece of launch.
  163.      * @return piece of launch
  164.      */
  165.     public String getLaunchPiece() {
  166.         return launchPiece;
  167.     }

  168.     /** Parse a CCSDS Orbit Data Message.
  169.      * @param fileName name of the file containing the message
  170.      * @return parsed orbit
  171.      */
  172.     public ODMFile parse(final String fileName) {
  173.         try (InputStream stream = new FileInputStream(fileName)) {
  174.             return parse(stream, fileName);
  175.         } catch (IOException e) {
  176.             throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_FILE, fileName);
  177.         }
  178.     }

  179.     /** Parse a CCSDS Orbit Data Message.
  180.      * @param stream stream containing message
  181.      * @return parsed orbit
  182.      */
  183.     public ODMFile parse(final InputStream stream) {
  184.         return parse(stream, "<unknown>");
  185.     }

  186.     /** Parse a CCSDS Orbit Data Message.
  187.      * @param stream stream containing message
  188.      * @param fileName name of the file containing the message (for error messages)
  189.      * @return parsed orbit
  190.      */
  191.     public abstract ODMFile parse(InputStream stream, String fileName);

  192.     /** Parse a comment line.
  193.      * @param keyValue key=value pair containing the comment
  194.      * @param comment placeholder where the current comment line should be added
  195.      * @return true if the line was a comment line and was parsed
  196.      */
  197.     protected boolean parseComment(final KeyValue keyValue, final List<String> comment) {
  198.         if (keyValue.getKeyword() == Keyword.COMMENT) {
  199.             comment.add(keyValue.getValue());
  200.             return true;
  201.         } else {
  202.             return false;
  203.         }
  204.     }

  205.     /** Parse an entry from the header.
  206.      * @param keyValue key = value pair
  207.      * @param odmFile instance to update with parsed entry
  208.      * @param comment previous comment lines, will be emptied if used by the keyword
  209.      * @return true if the keyword was a header keyword and has been parsed
  210.      */
  211.     protected boolean parseHeaderEntry(final KeyValue keyValue,
  212.                                        final ODMFile odmFile, final List<String> comment) {
  213.         switch (keyValue.getKeyword()) {

  214.             case CREATION_DATE:
  215.                 if (!comment.isEmpty()) {
  216.                     odmFile.setHeaderComment(comment);
  217.                     comment.clear();
  218.                 }
  219.                 odmFile.setCreationDate(new AbsoluteDate(keyValue.getValue(), TimeScalesFactory.getUTC()));
  220.                 return true;

  221.             case ORIGINATOR:
  222.                 odmFile.setOriginator(keyValue.getValue());
  223.                 return true;

  224.             default:
  225.                 return false;

  226.         }

  227.     }

  228.     /** Parse a meta-data key = value entry.
  229.      * @param keyValue key = value pair
  230.      * @param metaData instance to update with parsed entry
  231.      * @param comment previous comment lines, will be emptied if used by the keyword
  232.      * @return true if the keyword was a meta-data keyword and has been parsed
  233.      */
  234.     protected boolean parseMetaDataEntry(final KeyValue keyValue,
  235.                                          final ODMMetaData metaData, final List<String> comment) {
  236.         switch (keyValue.getKeyword()) {
  237.             case OBJECT_NAME:
  238.                 if (!comment.isEmpty()) {
  239.                     metaData.setComment(comment);
  240.                     comment.clear();
  241.                 }
  242.                 metaData.setObjectName(keyValue.getValue());
  243.                 return true;

  244.             case OBJECT_ID: {
  245.                 metaData.setObjectID(keyValue.getValue());
  246.                 final Matcher matcher = INTERNATIONAL_DESIGNATOR.matcher(keyValue.getValue());
  247.                 if (matcher.matches()) {
  248.                     metaData.setLaunchYear(Integer.parseInt(matcher.group(1)));
  249.                     metaData.setLaunchNumber(Integer.parseInt(matcher.group(2)));
  250.                     metaData.setLaunchPiece(matcher.group(3));
  251.                 }
  252.                 return true;
  253.             }

  254.             case CENTER_NAME:
  255.                 metaData.setCenterName(keyValue.getValue());
  256.                 final String canonicalValue;
  257.                 if (keyValue.getValue().equals("SOLAR SYSTEM BARYCENTER") || keyValue.getValue().equals("SSB")) {
  258.                     canonicalValue = "SOLAR_SYSTEM_BARYCENTER";
  259.                 } else if (keyValue.getValue().equals("EARTH MOON BARYCENTER") || keyValue.getValue().equals("EARTH-MOON BARYCENTER") ||
  260.                         keyValue.getValue().equals("EARTH BARYCENTER") || keyValue.getValue().equals("EMB")) {
  261.                     canonicalValue = "EARTH_MOON";
  262.                 } else {
  263.                     canonicalValue = keyValue.getValue();
  264.                 }
  265.                 for (final CenterName c : CenterName.values()) {
  266.                     if (c.name().equals(canonicalValue)) {
  267.                         metaData.setHasCreatableBody(true);
  268.                         metaData.setCenterBody(c.getCelestialBody());
  269.                         metaData.getODMFile().setMuCreated(c.getCelestialBody().getGM());
  270.                     }
  271.                 }
  272.                 return true;

  273.             case REF_FRAME:
  274.                 metaData.setFrameString(keyValue.getValue());
  275.                 metaData.setRefFrame(parseCCSDSFrame(keyValue.getValue()).getFrame(getConventions(), isSimpleEOP()));
  276.                 return true;

  277.             case REF_FRAME_EPOCH:
  278.                 metaData.setFrameEpochString(keyValue.getValue());
  279.                 return true;

  280.             case TIME_SYSTEM:
  281.                 if (!CcsdsTimeScale.contains(keyValue.getValue())) {
  282.                     throw new OrekitException(
  283.                             OrekitMessages.CCSDS_TIME_SYSTEM_NOT_IMPLEMENTED,
  284.                             keyValue.getValue());
  285.                 }
  286.                 final CcsdsTimeScale timeSystem =
  287.                         CcsdsTimeScale.valueOf(keyValue.getValue());
  288.                 metaData.setTimeSystem(timeSystem);
  289.                 if (metaData.getFrameEpochString() != null) {
  290.                     metaData.setFrameEpoch(parseDate(metaData.getFrameEpochString(), timeSystem));
  291.                 }
  292.                 return true;

  293.             default:
  294.                 return false;
  295.         }
  296.     }

  297.     /** Parse a general state data key = value entry.
  298.      * @param keyValue key = value pair
  299.      * @param general instance to update with parsed entry
  300.      * @param comment previous comment lines, will be emptied if used by the keyword
  301.      * @return true if the keyword was a meta-data keyword and has been parsed
  302.      */
  303.     protected boolean parseGeneralStateDataEntry(final KeyValue keyValue,
  304.                                                  final OGMFile general, final List<String> comment) {
  305.         switch (keyValue.getKeyword()) {

  306.             case EPOCH:
  307.                 general.setEpochComment(comment);
  308.                 comment.clear();
  309.                 general.setEpoch(parseDate(keyValue.getValue(), general.getMetaData().getTimeSystem()));
  310.                 return true;

  311.             case SEMI_MAJOR_AXIS:
  312.                 general.setKeplerianElementsComment(comment);
  313.                 comment.clear();
  314.                 general.setA(keyValue.getDoubleValue() * 1000);
  315.                 general.setHasKeplerianElements(true);
  316.                 return true;

  317.             case ECCENTRICITY:
  318.                 general.setE(keyValue.getDoubleValue());
  319.                 return true;

  320.             case INCLINATION:
  321.                 general.setI(FastMath.toRadians(keyValue.getDoubleValue()));
  322.                 return true;

  323.             case RA_OF_ASC_NODE:
  324.                 general.setRaan(FastMath.toRadians(keyValue.getDoubleValue()));
  325.                 return true;

  326.             case ARG_OF_PERICENTER:
  327.                 general.setPa(FastMath.toRadians(keyValue.getDoubleValue()));
  328.                 return true;

  329.             case TRUE_ANOMALY:
  330.                 general.setAnomalyType("TRUE");
  331.                 general.setAnomaly(FastMath.toRadians(keyValue.getDoubleValue()));
  332.                 return true;

  333.             case MEAN_ANOMALY:
  334.                 general.setAnomalyType("MEAN");
  335.                 general.setAnomaly(FastMath.toRadians(keyValue.getDoubleValue()));
  336.                 return true;

  337.             case GM:
  338.                 general.setMuParsed(keyValue.getDoubleValue() * 1e9);
  339.                 return true;

  340.             case MASS:
  341.                 comment.addAll(0, general.getSpacecraftComment());
  342.                 general.setSpacecraftComment(comment);
  343.                 comment.clear();
  344.                 general.setMass(keyValue.getDoubleValue());
  345.                 return true;

  346.             case SOLAR_RAD_AREA:
  347.                 comment.addAll(0, general.getSpacecraftComment());
  348.                 general.setSpacecraftComment(comment);
  349.                 comment.clear();
  350.                 general.setSolarRadArea(keyValue.getDoubleValue());
  351.                 return true;

  352.             case SOLAR_RAD_COEFF:
  353.                 comment.addAll(0, general.getSpacecraftComment());
  354.                 general.setSpacecraftComment(comment);
  355.                 comment.clear();
  356.                 general.setSolarRadCoeff(keyValue.getDoubleValue());
  357.                 return true;

  358.             case DRAG_AREA:
  359.                 comment.addAll(0, general.getSpacecraftComment());
  360.                 general.setSpacecraftComment(comment);
  361.                 comment.clear();
  362.                 general.setDragArea(keyValue.getDoubleValue());
  363.                 return true;

  364.             case DRAG_COEFF:
  365.                 comment.addAll(0, general.getSpacecraftComment());
  366.                 general.setSpacecraftComment(comment);
  367.                 comment.clear();
  368.                 general.setDragCoeff(keyValue.getDoubleValue());
  369.                 return true;

  370.             case COV_REF_FRAME:
  371.                 general.setCovarianceComment(comment);
  372.                 comment.clear();
  373.                 final CCSDSFrame covFrame = parseCCSDSFrame(keyValue.getValue());
  374.                 if (covFrame.isLof()) {
  375.                     general.setCovRefLofType(covFrame.getLofType());
  376.                 } else {
  377.                     general.setCovRefFrame(covFrame.getFrame(getConventions(), isSimpleEOP()));
  378.                 }
  379.                 return true;

  380.             case CX_X:
  381.                 general.createCovarianceMatrix();
  382.                 general.setCovarianceMatrixEntry(0, 0, keyValue.getDoubleValue() * 1.0e6);
  383.                 return true;

  384.             case CY_X:
  385.                 general.setCovarianceMatrixEntry(0, 1, keyValue.getDoubleValue() * 1.0e6);
  386.                 return true;

  387.             case CY_Y:
  388.                 general.setCovarianceMatrixEntry(1, 1, keyValue.getDoubleValue() * 1.0e6);
  389.                 return true;

  390.             case CZ_X:
  391.                 general.setCovarianceMatrixEntry(0, 2, keyValue.getDoubleValue() * 1.0e6);
  392.                 return true;

  393.             case CZ_Y:
  394.                 general.setCovarianceMatrixEntry(1, 2, keyValue.getDoubleValue() * 1.0e6);
  395.                 return true;

  396.             case CZ_Z:
  397.                 general.setCovarianceMatrixEntry(2, 2, keyValue.getDoubleValue() * 1.0e6);
  398.                 return true;

  399.             case CX_DOT_X:
  400.                 general.setCovarianceMatrixEntry(0, 3, keyValue.getDoubleValue() * 1.0e6);
  401.                 return true;

  402.             case CX_DOT_Y:
  403.                 general.setCovarianceMatrixEntry(1, 3, keyValue.getDoubleValue() * 1.0e6);
  404.                 return true;

  405.             case CX_DOT_Z:
  406.                 general.setCovarianceMatrixEntry(2, 3, keyValue.getDoubleValue() * 1.0e6);
  407.                 return true;

  408.             case CX_DOT_X_DOT:
  409.                 general.setCovarianceMatrixEntry(3, 3, keyValue.getDoubleValue() * 1.0e6);
  410.                 return true;

  411.             case CY_DOT_X:
  412.                 general.setCovarianceMatrixEntry(0, 4, keyValue.getDoubleValue() * 1.0e6);
  413.                 return true;

  414.             case CY_DOT_Y:
  415.                 general.setCovarianceMatrixEntry(1, 4, keyValue.getDoubleValue() * 1.0e6);
  416.                 return true;

  417.             case CY_DOT_Z:
  418.                 general.setCovarianceMatrixEntry(2, 4, keyValue.getDoubleValue() * 1.0e6);
  419.                 return true;

  420.             case CY_DOT_X_DOT:
  421.                 general.setCovarianceMatrixEntry(3, 4, keyValue.getDoubleValue() * 1.0e6);
  422.                 return true;

  423.             case CY_DOT_Y_DOT:
  424.                 general.setCovarianceMatrixEntry(4, 4, keyValue.getDoubleValue() * 1.0e6);
  425.                 return true;

  426.             case CZ_DOT_X:
  427.                 general.setCovarianceMatrixEntry(0, 5, keyValue.getDoubleValue() * 1.0e6);
  428.                 return true;

  429.             case CZ_DOT_Y:
  430.                 general.setCovarianceMatrixEntry(1, 5, keyValue.getDoubleValue() * 1.0e6);
  431.                 return true;

  432.             case CZ_DOT_Z:
  433.                 general.setCovarianceMatrixEntry(2, 5, keyValue.getDoubleValue() * 1.0e6);
  434.                 return true;

  435.             case CZ_DOT_X_DOT:
  436.                 general.setCovarianceMatrixEntry(3, 5, keyValue.getDoubleValue() * 1.0e6);
  437.                 return true;

  438.             case CZ_DOT_Y_DOT:
  439.                 general.setCovarianceMatrixEntry(4, 5, keyValue.getDoubleValue() * 1.0e6);
  440.                 return true;

  441.             case CZ_DOT_Z_DOT:
  442.                 general.setCovarianceMatrixEntry(5, 5, keyValue.getDoubleValue() * 1.0e6);
  443.                 return true;

  444.             case USER_DEFINED_X:
  445.                 general.setUserDefinedParameters(keyValue.getKey(), keyValue.getValue());
  446.                 return true;

  447.             default:
  448.                 return false;
  449.         }
  450.     }

  451.     /** Parse a CCSDS frame.
  452.      * @param frameName name of the frame, as the value of a CCSDS key=value line
  453.      * @return CCSDS frame corresponding to the name
  454.      */
  455.     protected CCSDSFrame parseCCSDSFrame(final String frameName) {
  456.         return CCSDSFrame.valueOf(frameName.replaceAll("-", ""));
  457.     }

  458.     /** Parse a date.
  459.      * @param date date to parse, as the value of a CCSDS key=value line
  460.      * @param timeSystem time system to use
  461.      * @return parsed date
  462.      */
  463.     protected AbsoluteDate parseDate(final String date, final CcsdsTimeScale timeSystem) {
  464.         return timeSystem.parseDate(date, conventions, missionReferenceDate);
  465.     }

  466. }