Position.java

  1. /* Copyright 2002-2020 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.estimation.measurements;

  18. import java.util.Arrays;

  19. import org.hipparchus.exception.LocalizedCoreFormats;
  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.FastMath;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.propagation.SpacecraftState;
  24. import org.orekit.time.AbsoluteDate;
  25. import org.orekit.utils.TimeStampedPVCoordinates;

  26. /** Class modeling a position only measurement.
  27.  * <p>
  28.  * For position-velocity measurement see {@link PV}.
  29.  * </p>
  30.  * @see PV
  31.  * @author Luc Maisonobe
  32.  * @since 9.3
  33.  */
  34. public class Position extends AbstractMeasurement<Position> {

  35.     /** Identity matrix, for states derivatives. */
  36.     private static final double[][] IDENTITY = new double[][] {
  37.         {
  38.             1, 0, 0, 0, 0, 0
  39.         }, {
  40.             0, 1, 0, 0, 0, 0
  41.         }, {
  42.             0, 0, 1, 0, 0, 0
  43.         }
  44.     };

  45.     /** Covariance matrix of the position only measurement (size 3x3). */
  46.     private final double[][] covarianceMatrix;

  47.     /** Constructor with one double for the standard deviation.
  48.      * <p>The double is the position's standard deviation, common to the 3 position's components.</p>
  49.      * <p>
  50.      * The measurement must be in the orbit propagation frame.
  51.      * </p>
  52.      * @param date date of the measurement
  53.      * @param position position
  54.      * @param sigmaPosition theoretical standard deviation on position components
  55.      * @param baseWeight base weight
  56.      * @param satellite satellite related to this measurement
  57.      * @since 9.3
  58.      */
  59.     public Position(final AbsoluteDate date, final Vector3D position,
  60.                     final double sigmaPosition, final double baseWeight,
  61.                     final ObservableSatellite satellite) {
  62.         this(date, position,
  63.              new double[] {
  64.                  sigmaPosition,
  65.                  sigmaPosition,
  66.                  sigmaPosition
  67.              }, baseWeight, satellite);
  68.     }

  69.     /** Constructor with one vector for the standard deviation.
  70.      * <p>The 3-sized vector represents the square root of the diagonal elements of the covariance matrix.</p>
  71.      * <p>The measurement must be in the orbit propagation frame.</p>
  72.      * @param date date of the measurement
  73.      * @param position position
  74.      * @param sigmaPosition 3-sized vector of the standard deviations of the position
  75.      * @param baseWeight base weight
  76.      * @param satellite satellite related to this measurement
  77.      * @since 9.3
  78.      */
  79.     public Position(final AbsoluteDate date, final Vector3D position,
  80.                     final double[] sigmaPosition, final double baseWeight, final ObservableSatellite satellite) {
  81.         this(date, position, buildPvCovarianceMatrix(sigmaPosition), baseWeight, satellite);
  82.     }

  83.     /** Constructor with full covariance matrix and all inputs.
  84.      * <p>The fact that the covariance matrix is symmetric and positive definite is not checked.</p>
  85.      * <p>The measurement must be in the orbit propagation frame.</p>
  86.      * @param date date of the measurement
  87.      * @param position position
  88.      * @param covarianceMatrix 3x3 covariance matrix of the position only measurement
  89.      * @param baseWeight base weight
  90.      * @param satellite satellite related to this measurement
  91.      * @since 9.3
  92.      */
  93.     public Position(final AbsoluteDate date, final Vector3D position,
  94.                     final double[][] covarianceMatrix, final double baseWeight,
  95.                     final ObservableSatellite satellite) {
  96.         super(date,
  97.               new double[] {
  98.                   position.getX(), position.getY(), position.getZ()
  99.               }, extractSigmas(covarianceMatrix),
  100.               new double[] {
  101.                   baseWeight, baseWeight, baseWeight
  102.               }, Arrays.asList(satellite));
  103.         this.covarianceMatrix = covarianceMatrix.clone();
  104.     }

  105.     /** Get the position.
  106.      * @return position
  107.      */
  108.     public Vector3D getPosition() {
  109.         final double[] pv = getObservedValue();
  110.         return new Vector3D(pv[0], pv[1], pv[2]);
  111.     }

  112.     /** Get the covariance matrix.
  113.      * @return the covariance matrix
  114.      */
  115.     public double[][] getCovarianceMatrix() {
  116.         return covarianceMatrix.clone();
  117.     }

  118.     /** Get the correlation coefficients matrix.
  119.      * <p>This is the 3x3 matrix M such that:
  120.      * <p>Mij = Pij/(σi.σj)
  121.      * <p>Where:
  122.      * <ul>
  123.      * <li>P is the covariance matrix
  124.      * <li>σi is the i-th standard deviation (σi² = Pii)
  125.      * </ul>
  126.      * @return the correlation coefficient matrix (3x3)
  127.      */
  128.     public double[][] getCorrelationCoefficientsMatrix() {

  129.         // Get the standard deviations
  130.         final double[] sigmas = getTheoreticalStandardDeviation();

  131.         // Initialize the correlation coefficients matric to the covariance matrix
  132.         final double[][] corrCoefMatrix = new double[sigmas.length][sigmas.length];

  133.         // Divide by the standard deviations
  134.         for (int i = 0; i < sigmas.length; i++) {
  135.             for (int j = 0; j < sigmas.length; j++) {
  136.                 corrCoefMatrix[i][j] = covarianceMatrix[i][j] / (sigmas[i] * sigmas[j]);
  137.             }
  138.         }
  139.         return corrCoefMatrix;
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     protected EstimatedMeasurement<Position> theoreticalEvaluation(final int iteration, final int evaluation,
  144.                                                                    final SpacecraftState[] states) {

  145.         // PV value
  146.         final TimeStampedPVCoordinates pv = states[0].getPVCoordinates();

  147.         // prepare the evaluation
  148.         final EstimatedMeasurement<Position> estimated =
  149.                         new EstimatedMeasurement<>(this, iteration, evaluation, states,
  150.                                                    new TimeStampedPVCoordinates[] {
  151.                                                        pv
  152.                                                    });

  153.         estimated.setEstimatedValue(new double[] {
  154.             pv.getPosition().getX(), pv.getPosition().getY(), pv.getPosition().getZ()
  155.         });

  156.         // partial derivatives with respect to state
  157.         estimated.setStateDerivatives(0, IDENTITY);

  158.         return estimated;
  159.     }

  160.     /** Extract standard deviations from a 3x3 position covariance matrix.
  161.      * Check the size of the position covariance matrix first.
  162.      * @param pCovarianceMatrix the 3x" position covariance matrix
  163.      * @return the standard deviations (3-sized vector), they are
  164.      * the square roots of the diagonal elements of the covariance matrix in input.
  165.      */
  166.     private static double[] extractSigmas(final double[][] pCovarianceMatrix) {

  167.         // Check the size of the covariance matrix, should be 3x3
  168.         if (pCovarianceMatrix.length != 3 || pCovarianceMatrix[0].length != 3) {
  169.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH_2x2,
  170.                                       pCovarianceMatrix.length, pCovarianceMatrix[0],
  171.                                       3, 3);
  172.         }

  173.         // Extract the standard deviations (square roots of the diagonal elements)
  174.         final double[] sigmas = new double[3];
  175.         for (int i = 0; i < sigmas.length; i++) {
  176.             sigmas[i] = FastMath.sqrt(pCovarianceMatrix[i][i]);
  177.         }
  178.         return sigmas;
  179.     }

  180.     /** Build a 3x3 position covariance matrix from a 3-sized vector (position standard deviations).
  181.      * Check the size of the vector first.
  182.      * @param sigmaP 3-sized vector with position standard deviations
  183.      * @return the 3x3 position covariance matrix
  184.      */
  185.     private static double[][] buildPvCovarianceMatrix(final double[] sigmaP) {
  186.         // Check the size of the vector first
  187.         if (sigmaP.length != 3) {
  188.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH, sigmaP.length, 3);

  189.         }

  190.         // Build the 3x3 position covariance matrix
  191.         final double[][] pvCovarianceMatrix = new double[3][3];
  192.         for (int i = 0; i < sigmaP.length; i++) {
  193.             pvCovarianceMatrix[i][i] =  sigmaP[i] * sigmaP[i];
  194.         }
  195.         return pvCovarianceMatrix;
  196.     }

  197. }