1   /* Copyright 2022-2025 Luc Maisonobe
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  
18  package org.orekit.files.ccsds.ndm.adm.acm;
19  
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.hipparchus.linear.DiagonalMatrix;
24  import org.orekit.files.ccsds.definitions.TimeConverter;
25  import org.orekit.files.ccsds.section.AbstractWriter;
26  import org.orekit.files.ccsds.utils.FileFormat;
27  import org.orekit.files.ccsds.utils.generation.Generator;
28  import org.orekit.utils.units.Unit;
29  
30  /** Writer for covariance history data.
31   * @author Luc Maisonobe
32   * @since 12.0
33   */
34  class AttitudeCovarianceHistoryWriter extends AbstractWriter {
35  
36      /** Covariance history block. */
37      private final AttitudeCovarianceHistory history;
38  
39      /** Converter for dates. */
40      private final TimeConverter timeConverter;
41  
42      /** Create a writer.
43       * @param covarianceHistory covariance history to write
44       * @param timeConverter converter for dates
45       */
46      AttitudeCovarianceHistoryWriter(final AttitudeCovarianceHistory covarianceHistory,
47                                      final TimeConverter timeConverter) {
48          super(AcmDataSubStructureKey.cov.name(), AcmDataSubStructureKey.COV.name());
49          this.history       = covarianceHistory;
50          this.timeConverter = timeConverter;
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      protected void writeContent(final Generator generator) throws IOException {
56  
57          // covariance history block
58          final AttitudeCovarianceHistoryMetadata metadata = history.getMetadata();
59          generator.writeComments(metadata.getComments());
60  
61          // identifiers
62          generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_ID.name(),       metadata.getCovID(),      null, false);
63          generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_PREV_ID.name(),  metadata.getCovPrevID(),  null, false);
64          generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_BASIS.name(),    metadata.getCovBasis(),   null, false);
65          generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_BASIS_ID.name(), metadata.getCovBasisID(), null, false);
66  
67          // references
68          if (metadata.getCovReferenceFrame() != null) {
69              generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_REF_FRAME.name(), metadata.getCovReferenceFrame().getName(), null, false);
70          }
71  
72          // elements
73          generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_TYPE.name(), metadata.getCovType(), false);
74  
75          // data
76          final List<Unit> units = metadata.getCovType().getUnits();
77          for (final AttitudeCovariance covariance : history.getCovariances()) {
78              final DiagonalMatrix matrix = covariance.getMatrix();
79              final StringBuilder  line   = new StringBuilder();
80              line.append(generator.dateToString(timeConverter, covariance.getDate()));
81              for (int k = 0; k < units.size(); ++k) {
82                  line.append(' ');
83                  line.append(generator.doubleToString(units.get(k).fromSI(matrix.getEntry(k, k))));
84              }
85              if (generator.getFormat() == FileFormat.XML) {
86                  generator.writeEntry(Acm.COV_LINE, line.toString(), null, true);
87              } else {
88                  generator.writeRawData(line);
89                  generator.newLine();
90              }
91  
92          }
93  
94      }
95  
96  }