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);
}
}
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);
}
}
}
Aggregations