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.cdm;
18  
19  import org.orekit.errors.OrekitException;
20  import org.orekit.errors.OrekitMessages;
21  import org.orekit.files.ccsds.section.CommentsContainer;
22  
23  import java.util.Arrays;
24  
25  /**
26   * Container for Sigma/Eigenvectors Covariance data.
27   * <p>
28   * Beware that the Orekit getters and setters all rely on SI units. The parsers
29   * and writers take care of converting these SI units into CCSDS mandatory units.
30   * The {@link org.orekit.utils.units.Unit Unit} class provides useful
31   * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
32   * {@link org.orekit.utils.units.Unit#toSI(double) toSI} methods in case the callers
33   * already use CCSDS units instead of the API SI units. The general-purpose
34   * {@link org.orekit.utils.units.Unit Unit} class (without an 's') and the
35   * CCSDS-specific {@link org.orekit.files.ccsds.definitions.Units Units} class
36   * (with an 's') also provide some predefined units. These predefined units and the
37   * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
38   * {@link org.orekit.utils.units.Unit#toSI(double) toSI} conversion methods are indeed
39   * what the parsers and writers use for the conversions.
40   * </p>
41   * <p>
42   * The positional covariance one-sigma dispersions corresponding to the major,
43   * intermediate and minor eigenvalues, followed by the associated eigenvectors.
44   * The data is presented on a single line (12 values separated by spaces).
45   * (Condition: Mandatory if {@code ALT_COV_TYPE = CSIG3EIGVEC3})
46   * </p>
47   */
48  public class SigmaEigenvectorsCovariance extends CommentsContainer {
49  
50      /** Sigma/Eigenvectors Covariance covariance matrix. */
51      private double[] csig3eigvec3;
52  
53      /** Flag indicating whether the alternate covariance type set in the CDM Object metadata section is Sigma/Eigenvectors Covariance. */
54      private final boolean altCovFlag;
55  
56      /** Simple constructor.
57       * <p> The Sigma/Eigenvectors Covariance data is only provided if {@link CdmMetadataKey#ALT_COV_TYPE} is {@link AltCovarianceType#CSIG3EIGVEC3}, otherwise
58       * its terms will return NaN. </p>
59       * @param altCovFlag Flag indicating whether the alternate covariance type set in the CDM Object metadata section is Sigma/Eigenvectors Covariance.
60       */
61      public SigmaEigenvectorsCovariance(final boolean altCovFlag) {
62          this.altCovFlag = altCovFlag;
63          csig3eigvec3 = new double[12];
64  
65          Arrays.fill(csig3eigvec3, Double.NaN);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public void validate(final double version) {
71          super.validate(version);
72  
73          // Conditional on ALT_COV_TYPE = Sigma/Eigenvectors Covariance
74          if (!isAltCovFlagSet()) {
75              throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD, CdmMetadataKey.ALT_COV_TYPE);
76          }
77  
78          // We only check values that are mandatory in a cdm file
79          for (int i = 0; i < getCsig3eigvec3().length; i++) {
80              checkNotNaN(getCsig3eigvec3()[i], SigmaEigenvectorsCovarianceKey.CSIG3EIGVEC3.name());
81          }
82      }
83  
84  
85      /**
86       * Get the Sigma/Eigenvectors Covariance data.
87       * <p> The Sigma/Eigenvectors Covariance data is only provided if {@link CdmMetadataKey#ALT_COV_TYPE} is {@link AltCovarianceType#CSIG3EIGVEC3}, otherwise
88       * its terms will return NaN. </p>
89       * @return covarianceData the covariance data in the Sigma/Eigenvectors format.
90       */
91      public double[] getCsig3eigvec3() {
92          return csig3eigvec3 == null ? null : csig3eigvec3.clone();
93      }
94  
95      /**
96       * Set the Sigma/Eigenvectors Covariance data.
97       * @param csig3eigvec3 the covariance data in the Sigma/Eigenvectors format.
98       */
99      public void setCsig3eigvec3(final double[] csig3eigvec3) {
100         refuseFurtherComments();
101 
102         // Conditional on ALT_COV_TYPE = Sigma/Eigenvectors Covariance
103         if (!isAltCovFlagSet()) {
104             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD, CdmMetadataKey.ALT_COV_TYPE);
105         }
106 
107         this.csig3eigvec3 = csig3eigvec3 == null ? null : csig3eigvec3.clone();
108     }
109 
110     /** Get the flag indicating whether the alternate covariance type set in the CDM Object metadata section is Sigma/Eigenvectors Covariance.
111      * @return the altCovFlag
112      */
113     public boolean isAltCovFlagSet() {
114         return altCovFlag;
115     }
116 
117 }