OEMWriter.java

  1. /* Copyright 2016 Applied Defense Solutions (ADS)
  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.  * ADS 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.IOException;
  19. import java.util.LinkedHashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.orekit.errors.OrekitIllegalArgumentException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.files.ccsds.OEMFile.CovarianceMatrix;
  25. import org.orekit.files.ccsds.OEMFile.EphemeridesBlock;
  26. import org.orekit.files.ccsds.StreamingOemWriter.Segment;
  27. import org.orekit.files.general.EphemerisFile;
  28. import org.orekit.files.general.EphemerisFile.EphemerisSegment;
  29. import org.orekit.files.general.EphemerisFileWriter;
  30. import org.orekit.time.TimeScale;
  31. import org.orekit.utils.TimeStampedPVCoordinates;

  32. /**
  33.  * An OEM Writer class that can take in a general {@link EphemerisFile} object
  34.  * and export it as a valid OEM file.
  35.  *
  36.  * @author Hank Grabowski
  37.  * @author Evan Ward
  38.  * @since 9.0
  39.  * @see <a href="https://public.ccsds.org/Pubs/502x0b2c1.pdf">CCSDS 502.0-B-2 Orbit Data
  40.  *      Messages</a>
  41.  * @see <a href="https://public.ccsds.org/Pubs/500x0g3.pdf">CCSDS 500.0-G-3 Navigation
  42.  *      Data Definitions and Conventions</a>
  43.  * @see StreamingOemWriter
  44.  */
  45. public class OEMWriter implements EphemerisFileWriter {

  46.     /** Version number implemented. **/
  47.     public static final String CCSDS_OEM_VERS = "2.0";

  48.     /** Default interpolation method if the user specifies none. **/
  49.     public static final InterpolationMethod DEFAULT_INTERPOLATION_METHOD = InterpolationMethod.LAGRANGE;

  50.     /** Default originator field value if user specifies none. **/
  51.     public static final String DEFAULT_ORIGINATOR = "OREKIT";

  52.     /**
  53.      * The space object ID we want to export, or null if we will process
  54.      * whichever space object is in an {@link EphemerisFile} with only one space
  55.      * object in it.
  56.      */
  57.     private final InterpolationMethod interpolationMethod;

  58.     /** Originator name, usually the organization and/or country. **/
  59.     private final String originator;

  60.     /**
  61.      * Space object ID, usually an official international designator such as
  62.      * "1998-067A".
  63.      **/
  64.     private final String spaceObjectId;

  65.     /** Space object name, usually a common name for an object like "ISS". **/
  66.     private final String spaceObjectName;

  67.     /**
  68.      * Standard default constructor that creates a writer with default
  69.      * configurations.
  70.      */
  71.     public OEMWriter() {
  72.         this(DEFAULT_INTERPOLATION_METHOD, DEFAULT_ORIGINATOR, null, null);
  73.     }

  74.     /**
  75.      * Constructor used to create a new OEM writer configured with the necessary
  76.      * parameters to successfully fill in all required fields that aren't part
  77.      * of a standard @{link EphemerisFile} object.
  78.      *
  79.      * @param interpolationMethod
  80.      *            The interpolation method to specify in the OEM file
  81.      * @param originator
  82.      *            The originator field string
  83.      * @param spaceObjectId
  84.      *            The spacecraft ID
  85.      * @param spaceObjectName
  86.      *            The space object common name
  87.      */
  88.     public OEMWriter(final InterpolationMethod interpolationMethod, final String originator, final String spaceObjectId,
  89.             final String spaceObjectName) {
  90.         this.interpolationMethod = interpolationMethod;
  91.         this.originator = originator;
  92.         this.spaceObjectId = spaceObjectId;
  93.         this.spaceObjectName = spaceObjectName;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public void write(final Appendable writer, final EphemerisFile ephemerisFile)
  98.             throws IOException {

  99.         if (writer == null) {
  100.             throw new OrekitIllegalArgumentException(OrekitMessages.NULL_ARGUMENT, "writer");
  101.         }

  102.         if (ephemerisFile == null) {
  103.             return;
  104.         }

  105.         final String idToProcess;
  106.         if (spaceObjectId != null) {
  107.             if (ephemerisFile.getSatellites().containsKey(spaceObjectId)) {
  108.                 idToProcess = spaceObjectId;
  109.             } else {
  110.                 throw new OrekitIllegalArgumentException(OrekitMessages.VALUE_NOT_FOUND, spaceObjectId, "ephemerisFile");
  111.             }
  112.         } else if (ephemerisFile.getSatellites().keySet().size() == 1) {
  113.             idToProcess = ephemerisFile.getSatellites().keySet().iterator().next();
  114.         } else {
  115.             throw new OrekitIllegalArgumentException(OrekitMessages.EPHEMERIS_FILE_NO_MULTI_SUPPORT);
  116.         }

  117.         // Get satellite and ephemeris segments to output.
  118.         final EphemerisFile.SatelliteEphemeris satEphem = ephemerisFile.getSatellites().get(idToProcess);
  119.         final List<? extends EphemerisSegment> segments = satEphem.getSegments();
  120.         if (segments.isEmpty()) {
  121.             // no data -> no output
  122.             return;
  123.         }
  124.         final EphemerisSegment firstSegment = segments.get(0);

  125.         final String objectName = this.spaceObjectName == null ?
  126.                 idToProcess : this.spaceObjectName;
  127.         // Only one time scale per OEM file, see Section 5.2.4.5
  128.         final TimeScale timeScale = firstSegment.getTimeScale();
  129.         // metadata that is constant for the whole OEM file
  130.         final Map<Keyword, String> metadata = new LinkedHashMap<>();
  131.         metadata.put(Keyword.TIME_SYSTEM, firstSegment.getTimeScaleString());
  132.         metadata.put(Keyword.ORIGINATOR, this.originator);
  133.         // Only one object in an OEM file, see Section 2.1
  134.         metadata.put(Keyword.OBJECT_ID, idToProcess);
  135.         metadata.put(Keyword.OBJECT_NAME, objectName);
  136.         metadata.put(Keyword.INTERPOLATION, this.interpolationMethod.toString());
  137.         final StreamingOemWriter oemWriter =
  138.                 new StreamingOemWriter(writer, timeScale, metadata);
  139.         oemWriter.writeHeader();

  140.         for (final EphemerisSegment segment : segments) {
  141.             // segment specific metadata
  142.             metadata.clear();
  143.             metadata.put(Keyword.CENTER_NAME, segment.getFrameCenterString());
  144.             metadata.put(Keyword.REF_FRAME, segment.getFrameString());
  145.             metadata.put(Keyword.START_TIME, segment.getStart().toString(timeScale));
  146.             metadata.put(Keyword.STOP_TIME, segment.getStop().toString(timeScale));
  147.             metadata.put(Keyword.INTERPOLATION_DEGREE,
  148.                     String.valueOf(segment.getInterpolationSamples() - 1));

  149.             final Segment segmentWriter = oemWriter.newSegment(null, metadata);
  150.             segmentWriter.writeMetadata();
  151.             for (final TimeStampedPVCoordinates coordinates : segment.getCoordinates()) {
  152.                 segmentWriter.writeEphemerisLine(coordinates);
  153.             }

  154.             if (segment instanceof EphemeridesBlock) {
  155.                 final EphemeridesBlock curr_ephem_block = (EphemeridesBlock) segment;
  156.                 final List<CovarianceMatrix> covarianceMatrices = curr_ephem_block.getCovarianceMatrices();
  157.                 if (!covarianceMatrices.isEmpty()) {
  158.                     segmentWriter.writeCovarianceMatrices(covarianceMatrices);
  159.                 }
  160.             }
  161.         }
  162.     }

  163.     /** OEM interpolation method. See Table 5-3. */
  164.     public enum InterpolationMethod {
  165.         /** Hermite interpolation. */
  166.         HERMITE,
  167.         /** Lagrange interpolation. */
  168.         LAGRANGE,
  169.         /** Linear interpolation. */
  170.         LINEAR
  171.     }

  172. }