1   /* Copyright 2002-2021 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  
18  package org.orekit.files.ccsds.ndm.odm;
19  
20  import java.util.function.Supplier;
21  
22  import org.hipparchus.linear.MatrixUtils;
23  import org.hipparchus.linear.RealMatrix;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  import org.orekit.files.ccsds.definitions.FrameFacade;
27  import org.orekit.files.ccsds.section.CommentsContainer;
28  import org.orekit.files.ccsds.section.Data;
29  import org.orekit.time.AbsoluteDate;
30  
31  /** Container for OPM/OMM/OCM Cartesian covariance matrix.
32   * @author sports
33   * @since 6.1
34   */
35  public class CartesianCovariance extends CommentsContainer implements Data {
36  
37      /** Labels for matrix row/columns. */
38      private static final String[] LABELS = {
39          "X", "Y", "Z", "X_DOT", "Y_DOT", "Z_DOT"
40      };
41  
42      /** Supplier for default reference frame. */
43      private final Supplier<FrameFacade> defaultFrameSupplier;
44  
45      /** Matrix epoch. */
46      private AbsoluteDate epoch;
47  
48      /** Reference frame in which data are given. */
49      private FrameFacade referenceFrame;
50  
51      /** Position/Velocity covariance matrix. */
52      private RealMatrix covarianceMatrix;
53  
54      /** Create an empty data set.
55       * @param defaultFrameSupplier supplier for default reference frame
56       * if no frame is specified in the CCSDS message
57       */
58      public CartesianCovariance(final Supplier<FrameFacade> defaultFrameSupplier) {
59          this.defaultFrameSupplier = defaultFrameSupplier;
60          covarianceMatrix = MatrixUtils.createRealMatrix(6, 6);
61          for (int i = 0; i < covarianceMatrix.getRowDimension(); ++i) {
62              for (int j = 0; j <= i; ++j) {
63                  covarianceMatrix.setEntry(i, j, Double.NaN);
64              }
65          }
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public void validate(final double version) {
71          super.validate(version);
72          checkNotNull(epoch, CartesianCovarianceKey.EPOCH);
73          for (int i = 0; i < covarianceMatrix.getRowDimension(); ++i) {
74              for (int j = 0; j <= i; ++j) {
75                  if (Double.isNaN(covarianceMatrix.getEntry(i, j))) {
76                      throw new OrekitException(OrekitMessages.CCSDS_MISSING_KEYWORD,
77                                                "C" + LABELS[i] + "_" + LABELS[j]);
78                  }
79              }
80          }
81      }
82  
83      /** Get matrix epoch.
84       * @return matrix epoch
85       */
86      public AbsoluteDate getEpoch() {
87          return epoch;
88      }
89  
90      /** Set matrix epoch.
91       * @param epoch matrix epoch
92       */
93      public void setEpoch(final AbsoluteDate epoch) {
94          refuseFurtherComments();
95          this.epoch = epoch;
96      }
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 
108     /** Set the reference frame in which data are given.
109      * @param referenceFrame the reference frame to be set
110      */
111     public void setReferenceFrame(final FrameFacade referenceFrame) {
112         refuseFurtherComments();
113         this.referenceFrame = referenceFrame;
114     }
115 
116     /** Get the Position/Velocity covariance matrix.
117      * @return the Position/Velocity covariance matrix
118      */
119     public RealMatrix getCovarianceMatrix() {
120         return covarianceMatrix;
121     }
122 
123     /** Set an entry in the Position/Velocity covariance matrix.
124      * <p>
125      * Both m(j, k) and m(k, j) are set.
126      * </p>
127      * @param j row index (must be between 0 and 5 (inclusive)
128      * @param k column index (must be between 0 and 5 (inclusive)
129      * @param entry value of the matrix entry
130      */
131     public void setCovarianceMatrixEntry(final int j, final int k, final double entry) {
132         refuseFurtherComments();
133         covarianceMatrix.setEntry(j, k, entry);
134         covarianceMatrix.setEntry(k, j, entry);
135     }
136 
137 }