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.     /** The interpolation method for ephemeris data. */
  53.     private final InterpolationMethod interpolationMethod;

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

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

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

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

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

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

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

  98.         if (ephemerisFile == null) {
  99.             return;
  100.         }

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

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

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

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

  145.             final Segment segmentWriter = oemWriter.newSegment(null, metadata);
  146.             segmentWriter.writeMetadata();
  147.             for (final TimeStampedPVCoordinates coordinates : segment.getCoordinates()) {
  148.                 segmentWriter.writeEphemerisLine(coordinates);
  149.             }

  150.             if (segment instanceof EphemeridesBlock) {
  151.                 final EphemeridesBlock curr_ephem_block = (EphemeridesBlock) segment;
  152.                 final List<CovarianceMatrix> covarianceMatrices = curr_ephem_block.getCovarianceMatrices();
  153.                 if (!covarianceMatrices.isEmpty()) {
  154.                     segmentWriter.writeCovarianceMatrices(covarianceMatrices);
  155.                 }
  156.             }
  157.         }
  158.     }

  159.     /** OEM interpolation method. See Table 5-3. */
  160.     public enum InterpolationMethod {
  161.         /** Hermite interpolation. */
  162.         HERMITE,
  163.         /** Lagrange interpolation. */
  164.         LAGRANGE,
  165.         /** Linear interpolation. */
  166.         LINEAR
  167.     }

  168. }