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