1 /* Copyright 2002-2021 CS GROUP
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 * CS 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.ilrs;
18
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.orekit.errors.OrekitIllegalArgumentException;
23 import org.orekit.errors.OrekitMessages;
24 import org.orekit.files.general.EphemerisFile;
25 import org.orekit.files.general.EphemerisFile.SatelliteEphemeris;
26 import org.orekit.files.general.EphemerisFileWriter;
27 import org.orekit.files.ilrs.StreamingCpfWriter.Segment;
28 import org.orekit.time.TimeScale;
29 import org.orekit.utils.TimeStampedPVCoordinates;
30
31 /**
32 * An CPF Writer class that can take in a general {@link EphemerisFile} object
33 * and export it as a valid CPF file.
34 * <p>
35 * It supports both 1.0 and 2.0 versions
36 * <p>
37 * <b>Note:</b> By default, only required header keys are wrote (H1 and H2).
38 * Furthermore, only position data can be written.
39 * Other keys (i.e. in header and other types of ephemeris entries) are simply ignored.
40 * Contributions are welcome to support more fields in the format.
41 * @author Bryan Cazabonne
42 * @since 10.3
43 * @see <a href="https://ilrs.gsfc.nasa.gov/docs/2006/cpf_1.01.pdf">1.0 file format</a>
44 * @see <a href="https://ilrs.gsfc.nasa.gov/docs/2018/cpf_2.00h-1.pdf">2.0 file format</a>
45 */
46 public class CPFWriter implements EphemerisFileWriter {
47
48 /** Container for header data. */
49 private final CPFHeader header;
50
51 /** Time scale for dates. */
52 private final TimeScale timescale;
53
54 /**
55 * Constructor.
56 * @param header container for header data
57 * @param timescale time scale for dates
58 */
59 public CPFWriter(final CPFHeader header, final TimeScale timescale) {
60 this.header = header;
61 this.timescale = timescale;
62 }
63
64
65 /** {@inheritDoc} */
66 @Override
67 public <C extends TimeStampedPVCoordinates, S extends EphemerisFile.EphemerisSegment<C>>
68 void write(final Appendable writer, final EphemerisFile<C, S> ephemerisFile)
69 throws IOException {
70
71 // Verify if writer is not a null object
72 if (writer == null) {
73 throw new OrekitIllegalArgumentException(OrekitMessages.NULL_ARGUMENT, "writer");
74 }
75
76 // Verify if the populated ephemeris file to serialize into the buffer is not null
77 if (ephemerisFile == null) {
78 return;
79 }
80
81 // Get satellite and ephemeris segments to output.
82 final SatelliteEphemeris<C, S> satEphem = ephemerisFile.getSatellites().get(header.getIlrsSatelliteId());
83 final List<S> segments = satEphem.getSegments();
84
85 // Writer
86 final StreamingCpfWriter cpfWriter =
87 new StreamingCpfWriter(writer, timescale, header);
88 // Write header
89 cpfWriter.writeHeader();
90
91 // Loop on ephemeris segments
92 for (final S segment : segments) {
93 final Segment segmentWriter = cpfWriter.newSegment(header.getRefFrame());
94 // Loop on coordinates
95 for (final TimeStampedPVCoordinates coordinates : segment.getCoordinates()) {
96 segmentWriter.writeEphemerisLine(coordinates);
97 }
98 }
99
100 // Write end of file
101 cpfWriter.writeEndOfFile();
102
103 }
104
105 }