OEMWriter.java

  1. /* Copyright 2016 Applied Defense Solutions (ADS)
  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.  * 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.StreamingOemWriter.Segment;
  25. import org.orekit.files.general.EphemerisFile;
  26. import org.orekit.files.general.EphemerisFile.EphemerisSegment;
  27. import org.orekit.files.general.EphemerisFileWriter;
  28. import org.orekit.time.TimeScale;
  29. import org.orekit.utils.TimeStampedPVCoordinates;

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

  44.     /** Version number implemented. **/
  45.     public static final String CCSDS_OEM_VERS = "2.0";

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

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

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

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

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

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

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

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

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

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

  100.         if (ephemerisFile == null) {
  101.             return;
  102.         }

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

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

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

  138.         for (final EphemerisSegment segment : segments) {
  139.             // segment specific metadata
  140.             metadata.clear();
  141.             metadata.put(Keyword.CENTER_NAME, segment.getFrameCenterString());
  142.             metadata.put(Keyword.REF_FRAME, segment.getFrameString());
  143.             metadata.put(Keyword.START_TIME, segment.getStart().toString(timeScale));
  144.             metadata.put(Keyword.STOP_TIME, segment.getStop().toString(timeScale));
  145.             metadata.put(Keyword.INTERPOLATION_DEGREE,
  146.                     String.valueOf(segment.getInterpolationSamples() - 1));
  147.             final Segment segmentWriter = oemWriter.newSegment(null, metadata);
  148.             segmentWriter.writeMetadata();
  149.             for (final TimeStampedPVCoordinates coordinates : segment.getCoordinates()) {
  150.                 segmentWriter.writeEphemerisLine(coordinates);
  151.             }
  152.         }
  153.     }

  154.     /** OEM interpolation method. See Table 5-3. */
  155.     public enum InterpolationMethod {
  156.         /** Hermite interpolation. */
  157.         HERMITE,
  158.         /** Lagrange interpolation. */
  159.         LAGRANGE,
  160.         /** Linear interpolation. */
  161.         LINEAR
  162.     }

  163. }