AttitudeCovarianceHistoryWriter.java

  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. package org.orekit.files.ccsds.ndm.adm.acm;

  18. import java.io.IOException;
  19. import java.util.List;

  20. import org.hipparchus.linear.DiagonalMatrix;
  21. import org.orekit.files.ccsds.definitions.TimeConverter;
  22. import org.orekit.files.ccsds.section.AbstractWriter;
  23. import org.orekit.files.ccsds.utils.FileFormat;
  24. import org.orekit.files.ccsds.utils.generation.Generator;
  25. import org.orekit.utils.units.Unit;

  26. /** Writer for covariance history data.
  27.  * @author Luc Maisonobe
  28.  * @since 12.0
  29.  */
  30. class AttitudeCovarianceHistoryWriter extends AbstractWriter {

  31.     /** Covariance history block. */
  32.     private final AttitudeCovarianceHistory history;

  33.     /** Converter for dates. */
  34.     private final TimeConverter timeConverter;

  35.     /** Create a writer.
  36.      * @param covarianceHistory covariance history to write
  37.      * @param timeConverter converter for dates
  38.      */
  39.     AttitudeCovarianceHistoryWriter(final AttitudeCovarianceHistory covarianceHistory,
  40.                                     final TimeConverter timeConverter) {
  41.         super(AcmDataSubStructureKey.cov.name(), AcmDataSubStructureKey.COV.name());
  42.         this.history       = covarianceHistory;
  43.         this.timeConverter = timeConverter;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     protected void writeContent(final Generator generator) throws IOException {

  48.         // covariance history block
  49.         final AttitudeCovarianceHistoryMetadata metadata = history.getMetadata();
  50.         generator.writeComments(metadata.getComments());

  51.         // identifiers
  52.         generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_ID.name(),       metadata.getCovID(),      null, false);
  53.         generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_PREV_ID.name(),  metadata.getCovPrevID(),  null, false);
  54.         generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_BASIS.name(),    metadata.getCovBasis(),   null, false);
  55.         generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_BASIS_ID.name(), metadata.getCovBasisID(), null, false);

  56.         // references
  57.         if (metadata.getCovReferenceFrame() != null) {
  58.             generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_REF_FRAME.name(), metadata.getCovReferenceFrame().getName(), null, false);
  59.         }

  60.         // elements
  61.         generator.writeEntry(AttitudeCovarianceHistoryMetadataKey.COV_TYPE.name(), metadata.getCovType(), false);

  62.         // data
  63.         final List<Unit> units = metadata.getCovType().getUnits();
  64.         for (final AttitudeCovariance covariance : history.getCovariances()) {
  65.             final DiagonalMatrix matrix = covariance.getMatrix();
  66.             final StringBuilder  line   = new StringBuilder();
  67.             line.append(generator.dateToString(timeConverter, covariance.getDate()));
  68.             for (int k = 0; k < units.size(); ++k) {
  69.                 line.append(' ');
  70.                 line.append(generator.doubleToString(units.get(k).fromSI(matrix.getEntry(k, k))));
  71.             }
  72.             if (generator.getFormat() == FileFormat.XML) {
  73.                 generator.writeEntry(Acm.COV_LINE, line.toString(), null, true);
  74.             } else {
  75.                 generator.writeRawData(line);
  76.                 generator.newLine();
  77.             }

  78.         }

  79.     }

  80. }