1   /* Copyright 2002-2025 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 org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.Utils;
23  import org.orekit.attitudes.FrameAlignedProvider;
24  import org.orekit.data.DataSource;
25  import org.orekit.files.ilrs.CPF.CPFEphemeris;
26  import org.orekit.files.ilrs.StreamingCpfWriter.Segment;
27  import org.orekit.frames.Frame;
28  import org.orekit.propagation.BoundedPropagator;
29  import org.orekit.time.TimeScale;
30  import org.orekit.time.TimeScalesFactory;
31  import org.orekit.utils.TimeStampedPVCoordinates;
32  
33  import java.io.ByteArrayInputStream;
34  import java.nio.charset.StandardCharsets;
35  import java.util.Arrays;
36  import java.util.List;
37  
38  
39  public class StreamingCpfWriterTest {
40  
41      /** Set Orekit data. */
42      @BeforeEach
43      public void setUp() {
44          Utils.setDataRoot("regular-data");
45      }
46  
47      /**
48       * Check reading and writing a CPF both with and without using the step handler
49       * methods.
50       */
51      @Test
52      public void testWriteCpfStepHandler() throws Exception {
53  
54          // Time scale
55          TimeScale utc = TimeScalesFactory.getUTC();
56          // Create a list of files for testing
57          List<String> files =
58                  Arrays.asList("/ilrs/jason3_cpf_180613_16401.cne",
59                                "/ilrs/lageos1_cpf_180613_16401.hts",
60                                "/ilrs/galileo212_cpf_180613_6641.esa");
61          for (final String ex : files) {
62              DataSource source0 = new DataSource(ex, () -> getClass().getResourceAsStream(ex));
63              CPF cpfFile = new CPFParser().parse(source0);
64  
65              CPFEphemeris satellite =
66                              cpfFile.getSatellites().values().iterator().next();
67              Frame frame = satellite.getFrame();
68              double step = satellite.getStop()
69                              .durationFrom(satellite.getStart()) /
70                              (satellite.getCoordinates().size() - 1);
71  
72              Assertions.assertEquals(step, cpfFile.getHeader().getStep(), 0.1);
73              StringBuilder buffer = new StringBuilder();
74              StreamingCpfWriter writer = new StreamingCpfWriter(buffer, utc, cpfFile.getHeader());
75              writer.writeHeader();
76              Segment segment = writer.newSegment(frame);
77              BoundedPropagator propagator = satellite.getPropagator(new FrameAlignedProvider(frame));
78              propagator.setStepHandler(step, segment);
79              propagator.propagate(propagator.getMinDate(), propagator.getMaxDate());
80  
81              final byte[]    bytes1            = buffer.toString().getBytes(StandardCharsets.UTF_8);
82              final DataSource source1           = new DataSource("buffer", () -> new ByteArrayInputStream(bytes1));
83              final CPF   generatedCpfFile1 = new CPFParser().parse(source1);
84              CPFWriterTest.compareCpfFiles(cpfFile, generatedCpfFile1);
85  
86              // check calling the methods directly
87              buffer = new StringBuilder();
88              writer = new StreamingCpfWriter(buffer, utc, cpfFile.getHeader());
89              writer.writeHeader();
90              segment = writer.newSegment(frame);
91              for (TimeStampedPVCoordinates coordinate : satellite.getCoordinates()) {
92                  segment.writeEphemerisLine(coordinate);
93              }
94              writer.writeEndOfFile();
95  
96              // verify
97              final byte[]    bytes2            = buffer.toString().getBytes(StandardCharsets.UTF_8);
98              final DataSource source2           = new DataSource("buffer", () -> new ByteArrayInputStream(bytes2));
99              final CPF   generatedCpfFile2 = new CPFParser().parse(source2);
100             CPFWriterTest.compareCpfFiles(cpfFile, generatedCpfFile2);
101 
102         }
103 
104     }
105 
106 }