CartesianCovariance.java

  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.ccsds.ndm.odm;

  18. import java.util.function.Supplier;

  19. import org.hipparchus.linear.MatrixUtils;
  20. import org.hipparchus.linear.RealMatrix;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.files.ccsds.definitions.FrameFacade;
  24. import org.orekit.files.ccsds.section.CommentsContainer;
  25. import org.orekit.files.ccsds.section.Data;
  26. import org.orekit.time.AbsoluteDate;

  27. /** Container for OPM/OMM/OCM Cartesian covariance matrix.
  28.  * <p>
  29.  * Beware that the Orekit getters and setters all rely on SI units. The parsers
  30.  * and writers take care of converting these SI units into CCSDS mandatory units.
  31.  * The {@link org.orekit.utils.units.Unit Unit} class provides useful
  32.  * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
  33.  * {@link org.orekit.utils.units.Unit#toSI(double) toSI} methods in case the callers
  34.  * already use CCSDS units instead of the API SI units. The general-purpose
  35.  * {@link org.orekit.utils.units.Unit Unit} class (without an 's') and the
  36.  * CCSDS-specific {@link org.orekit.files.ccsds.definitions.Units Units} class
  37.  * (with an 's') also provide some predefined units. These predefined units and the
  38.  * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
  39.  * {@link org.orekit.utils.units.Unit#toSI(double) toSI} conversion methods are indeed
  40.  * what the parsers and writers use for the conversions.
  41.  * </p>
  42.  * @author sports
  43.  * @since 6.1
  44.  */
  45. public class CartesianCovariance extends CommentsContainer implements Data {

  46.     /** Labels for matrix row/columns. */
  47.     private static final String[] LABELS = {
  48.         "X", "Y", "Z", "X_DOT", "Y_DOT", "Z_DOT"
  49.     };

  50.     /** Supplier for default reference frame. */
  51.     private final Supplier<FrameFacade> defaultFrameSupplier;

  52.     /** Matrix epoch. */
  53.     private AbsoluteDate epoch;

  54.     /** Reference frame in which data are given. */
  55.     private FrameFacade referenceFrame;

  56.     /** Position/Velocity covariance matrix. */
  57.     private final RealMatrix covarianceMatrix;

  58.     /** Create an empty data set.
  59.      * @param defaultFrameSupplier supplier for default reference frame
  60.      * if no frame is specified in the CCSDS message
  61.      */
  62.     public CartesianCovariance(final Supplier<FrameFacade> defaultFrameSupplier) {
  63.         this.defaultFrameSupplier = defaultFrameSupplier;
  64.         covarianceMatrix = MatrixUtils.createRealMatrix(6, 6);
  65.         for (int i = 0; i < covarianceMatrix.getRowDimension(); ++i) {
  66.             for (int j = 0; j <= i; ++j) {
  67.                 covarianceMatrix.setEntry(i, j, Double.NaN);
  68.             }
  69.         }
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public void validate(final double version) {
  74.         super.validate(version);
  75.         checkNotNull(epoch, CartesianCovarianceKey.EPOCH.name());
  76.         for (int i = 0; i < covarianceMatrix.getRowDimension(); ++i) {
  77.             for (int j = 0; j <= i; ++j) {
  78.                 if (Double.isNaN(covarianceMatrix.getEntry(i, j))) {
  79.                     throw new OrekitException(OrekitMessages.CCSDS_MISSING_KEYWORD,
  80.                                               "C" + LABELS[i] + "_" + LABELS[j]);
  81.                 }
  82.             }
  83.         }
  84.     }

  85.     /** Get matrix epoch.
  86.      * @return matrix epoch
  87.      */
  88.     public AbsoluteDate getEpoch() {
  89.         return epoch;
  90.     }

  91.     /** Set matrix epoch.
  92.      * @param epoch matrix epoch
  93.      */
  94.     public void setEpoch(final AbsoluteDate epoch) {
  95.         refuseFurtherComments();
  96.         this.epoch = epoch;
  97.     }

  98.     /**
  99.      * Get the reference frame.
  100.      *
  101.      * @return The reference frame specified by the {@code COV_REF_FRAME} keyword
  102.      * or inherited from metadata
  103.      */
  104.     public FrameFacade getReferenceFrame() {
  105.         return referenceFrame == null ? defaultFrameSupplier.get() : referenceFrame;
  106.     }

  107.     /** Set the reference frame in which data are given.
  108.      * @param referenceFrame the reference frame to be set
  109.      */
  110.     public void setReferenceFrame(final FrameFacade referenceFrame) {
  111.         refuseFurtherComments();
  112.         this.referenceFrame = referenceFrame;
  113.     }

  114.     /** Get the Position/Velocity covariance matrix.
  115.      * @return the Position/Velocity covariance matrix
  116.      */
  117.     public RealMatrix getCovarianceMatrix() {
  118.         return covarianceMatrix;
  119.     }

  120.     /** Set an entry in the Position/Velocity covariance matrix.
  121.      * <p>
  122.      * Both m(j, k) and m(k, j) are set.
  123.      * </p>
  124.      * @param j row index (must be between 0 and 5 (inclusive)
  125.      * @param k column index (must be between 0 and 5 (inclusive)
  126.      * @param entry value of the matrix entry
  127.      */
  128.     public void setCovarianceMatrixEntry(final int j, final int k, final double entry) {
  129.         refuseFurtherComments();
  130.         covarianceMatrix.setEntry(j, k, entry);
  131.         covarianceMatrix.setEntry(k, j, entry);
  132.     }

  133. }