Search in sources :

Example 1 with Segment

use of org.orekit.files.ccsds.StreamingOemWriter.Segment in project Orekit by CS-SI.

the class StreamingOemWriterTest method testWriteOemStepHandler.

/**
 * Check reading and writing an OEM both with and without using the step handler
 * methods.
 *
 * @throws Exception on error.
 */
@Test
public void testWriteOemStepHandler() throws Exception {
    // setup
    TimeScale utc = TimeScalesFactory.getUTC();
    List<String> files = Arrays.asList("/ccsds/OEMExample5.txt", "/ccsds/OEMExample4.txt");
    for (String ex : files) {
        InputStream inEntry = getClass().getResourceAsStream(ex);
        OEMParser parser = new OEMParser().withMu(CelestialBodyFactory.getEarth().getGM()).withConventions(IERSConventions.IERS_2010);
        OEMFile oemFile = parser.parse(inEntry, "OEMExample.txt");
        OemSatelliteEphemeris satellite = oemFile.getSatellites().values().iterator().next();
        EphemeridesBlock ephemerisBlock = satellite.getSegments().get(0);
        Frame frame = ephemerisBlock.getFrame();
        double step = ephemerisBlock.getStopTime().durationFrom(ephemerisBlock.getStartTime()) / (ephemerisBlock.getCoordinates().size() - 1);
        String originator = oemFile.getOriginator();
        EphemeridesBlock block = oemFile.getEphemeridesBlocks().get(0);
        String objectName = block.getMetaData().getObjectName();
        String objectID = block.getMetaData().getObjectID();
        Map<Keyword, String> metadata = new LinkedHashMap<>();
        metadata.put(Keyword.ORIGINATOR, originator);
        metadata.put(Keyword.OBJECT_NAME, "will be overwritten");
        metadata.put(Keyword.OBJECT_ID, objectID);
        Map<Keyword, String> segmentData = new LinkedHashMap<>();
        segmentData.put(Keyword.OBJECT_NAME, objectName);
        // check using the Propagator / StepHandler interface
        StringBuilder buffer = new StringBuilder();
        StreamingOemWriter writer = new StreamingOemWriter(buffer, utc, metadata);
        writer.writeHeader();
        Segment segment = writer.newSegment(frame, segmentData);
        BoundedPropagator propagator = satellite.getPropagator();
        propagator.setMasterMode(step, segment);
        propagator.propagate(propagator.getMinDate(), propagator.getMaxDate());
        // verify
        BufferedReader reader = new BufferedReader(new StringReader(buffer.toString()));
        OEMFile generatedOemFile = parser.parse(reader, "buffer");
        compareOemFiles(oemFile, generatedOemFile, 1e-7, 1e-7);
        // check calling the methods directly
        buffer = new StringBuilder();
        writer = new StreamingOemWriter(buffer, utc, metadata);
        writer.writeHeader();
        // set start and stop date manually
        segmentData.put(Keyword.START_TIME, StreamingOemWriter.dateToString(block.getStart().getComponents(utc)));
        segmentData.put(Keyword.STOP_TIME, StreamingOemWriter.dateToString(block.getStop().getComponents(utc)));
        segment = writer.newSegment(frame, segmentData);
        segment.writeMetadata();
        for (TimeStampedPVCoordinates coordinate : block.getCoordinates()) {
            segment.writeEphemerisLine(coordinate);
        }
        // verify
        reader = new BufferedReader(new StringReader(buffer.toString()));
        generatedOemFile = parser.parse(reader, "buffer");
        compareOemFiles(oemFile, generatedOemFile, 1e-7, 1e-7);
    }
}
Also used : Frame(org.orekit.frames.Frame) InputStream(java.io.InputStream) EphemeridesBlock(org.orekit.files.ccsds.OEMFile.EphemeridesBlock) TimeStampedPVCoordinates(org.orekit.utils.TimeStampedPVCoordinates) TimeScale(org.orekit.time.TimeScale) Segment(org.orekit.files.ccsds.StreamingOemWriter.Segment) LinkedHashMap(java.util.LinkedHashMap) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) OemSatelliteEphemeris(org.orekit.files.ccsds.OEMFile.OemSatelliteEphemeris) BoundedPropagator(org.orekit.propagation.BoundedPropagator) Test(org.junit.Test)

Example 2 with Segment

use of org.orekit.files.ccsds.StreamingOemWriter.Segment in project Orekit by CS-SI.

the class OEMWriter method write.

/**
 * {@inheritDoc}
 */
@Override
public void write(final Appendable writer, final EphemerisFile ephemerisFile) throws OrekitException, IOException {
    if (writer == null) {
        throw new OrekitIllegalArgumentException(OrekitMessages.NULL_ARGUMENT, "writer");
    }
    if (ephemerisFile == null) {
        return;
    }
    final String idToProcess;
    if (spaceObjectId != null) {
        if (ephemerisFile.getSatellites().containsKey(spaceObjectId)) {
            idToProcess = spaceObjectId;
        } else {
            throw new OrekitIllegalArgumentException(OrekitMessages.VALUE_NOT_FOUND, spaceObjectId, "ephemerisFile");
        }
    } else if (ephemerisFile.getSatellites().keySet().size() == 1) {
        idToProcess = ephemerisFile.getSatellites().keySet().iterator().next();
    } else {
        throw new OrekitIllegalArgumentException(OrekitMessages.EPHEMERIS_FILE_NO_MULTI_SUPPORT);
    }
    // Get satellite and ephemeris segments to output.
    final EphemerisFile.SatelliteEphemeris satEphem = ephemerisFile.getSatellites().get(idToProcess);
    final List<? extends EphemerisSegment> segments = satEphem.getSegments();
    if (segments.isEmpty()) {
        // no data -> no output
        return;
    }
    final EphemerisSegment firstSegment = segments.get(0);
    final String objectName = this.spaceObjectName == null ? idToProcess : this.spaceObjectName;
    // Only one time scale per OEM file, see Section 5.2.4.5
    final TimeScale timeScale = firstSegment.getTimeScale();
    // metadata that is constant for the whole OEM file
    final Map<Keyword, String> metadata = new LinkedHashMap<>();
    metadata.put(Keyword.TIME_SYSTEM, firstSegment.getTimeScaleString());
    metadata.put(Keyword.ORIGINATOR, this.originator);
    // Only one object in an OEM file, see Section 2.1
    metadata.put(Keyword.OBJECT_ID, idToProcess);
    metadata.put(Keyword.OBJECT_NAME, objectName);
    metadata.put(Keyword.INTERPOLATION, this.interpolationMethod.toString());
    final StreamingOemWriter oemWriter = new StreamingOemWriter(writer, timeScale, metadata);
    oemWriter.writeHeader();
    for (final EphemerisSegment segment : segments) {
        // segment specific metadata
        metadata.clear();
        metadata.put(Keyword.CENTER_NAME, segment.getFrameCenterString());
        metadata.put(Keyword.REF_FRAME, segment.getFrameString());
        metadata.put(Keyword.START_TIME, segment.getStart().toString(timeScale));
        metadata.put(Keyword.STOP_TIME, segment.getStop().toString(timeScale));
        metadata.put(Keyword.INTERPOLATION_DEGREE, String.valueOf(segment.getInterpolationSamples() - 1));
        final Segment segmentWriter = oemWriter.newSegment(null, metadata);
        segmentWriter.writeMetadata();
        for (final TimeStampedPVCoordinates coordinates : segment.getCoordinates()) {
            segmentWriter.writeEphemerisLine(coordinates);
        }
    }
}
Also used : TimeStampedPVCoordinates(org.orekit.utils.TimeStampedPVCoordinates) TimeScale(org.orekit.time.TimeScale) EphemerisSegment(org.orekit.files.general.EphemerisFile.EphemerisSegment) Segment(org.orekit.files.ccsds.StreamingOemWriter.Segment) OrekitIllegalArgumentException(org.orekit.errors.OrekitIllegalArgumentException) LinkedHashMap(java.util.LinkedHashMap) EphemerisSegment(org.orekit.files.general.EphemerisFile.EphemerisSegment) EphemerisFile(org.orekit.files.general.EphemerisFile)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2 Segment (org.orekit.files.ccsds.StreamingOemWriter.Segment)2 TimeScale (org.orekit.time.TimeScale)2 TimeStampedPVCoordinates (org.orekit.utils.TimeStampedPVCoordinates)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 Test (org.junit.Test)1 OrekitIllegalArgumentException (org.orekit.errors.OrekitIllegalArgumentException)1 EphemeridesBlock (org.orekit.files.ccsds.OEMFile.EphemeridesBlock)1 OemSatelliteEphemeris (org.orekit.files.ccsds.OEMFile.OemSatelliteEphemeris)1 EphemerisFile (org.orekit.files.general.EphemerisFile)1 EphemerisSegment (org.orekit.files.general.EphemerisFile.EphemerisSegment)1 Frame (org.orekit.frames.Frame)1 BoundedPropagator (org.orekit.propagation.BoundedPropagator)1