FieldStateCovariance.java

  1. /* Copyright 2002-2024 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.propagation;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.linear.Array2DRowFieldMatrix;
  21. import org.hipparchus.linear.BlockRealMatrix;
  22. import org.hipparchus.linear.FieldMatrix;
  23. import org.hipparchus.linear.MatrixUtils;
  24. import org.hipparchus.util.MathArrays;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitMessages;
  27. import org.orekit.frames.FieldTransform;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.frames.LOF;
  30. import org.orekit.orbits.FieldOrbit;
  31. import org.orekit.orbits.OrbitType;
  32. import org.orekit.orbits.PositionAngleType;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.time.FieldAbsoluteDate;
  35. import org.orekit.time.FieldTimeStamped;
  36. import org.orekit.utils.CartesianDerivativesFilter;

  37. /**
  38.  * This class is the representation of a covariance matrix at a given date.
  39.  * <p>
  40.  * Currently, the covariance only represents the orbital elements.
  41.  * <p>
  42.  * It is possible to change the covariance frame by using the {@link #changeCovarianceFrame(FieldOrbit, Frame)} or
  43.  * {@link #changeCovarianceFrame(FieldOrbit, LOF)} method. These methods are based on Equations (18) and (20) of
  44.  * <i>Covariance Transformations for Satellite Flight Dynamics Operations</i> by David A. SVallado.
  45.  * <p>
  46.  * Finally, covariance orbit type can be changed using the
  47.  * {@link #changeCovarianceType(FieldOrbit, OrbitType, PositionAngleType) changeCovarianceType(FieldOrbit, OrbitType,
  48.  * PositionAngle)} method.
  49.  * </p>
  50.  *
  51.  * @author Bryan Cazabonne
  52.  * @author Vincent Cucchietti
  53.  * @since 12.0
  54.  * @param <T> type of the field elements
  55.  */
  56. public class FieldStateCovariance<T extends CalculusFieldElement<T>> implements FieldTimeStamped<T> {

  57.     /** State dimension. */
  58.     private static final int STATE_DIMENSION = 6;

  59.     /** Default position angle for covariance expressed in cartesian elements. */
  60.     private static final PositionAngleType DEFAULT_POSITION_ANGLE = PositionAngleType.TRUE;

  61.     /** Orbital covariance [6x6]. */
  62.     private final FieldMatrix<T> orbitalCovariance;

  63.     /** Covariance frame (can be null if LOF is defined). */
  64.     private final Frame frame;

  65.     /** Covariance LOF type (can be null if frame is defined). */
  66.     private final LOF lof;

  67.     /** Covariance epoch. */
  68.     private final FieldAbsoluteDate<T> epoch;

  69.     /** Covariance orbit type. */
  70.     private final OrbitType orbitType;

  71.     /** Covariance position angle type (not used if orbitType is CARTESIAN). */
  72.     private final PositionAngleType angleType;

  73.     /**
  74.      * Constructor.
  75.      *
  76.      * @param orbitalCovariance 6x6 orbital parameters covariance
  77.      * @param epoch epoch of the covariance
  78.      * @param lof covariance LOF type
  79.      */
  80.     public FieldStateCovariance(final FieldMatrix<T> orbitalCovariance, final FieldAbsoluteDate<T> epoch,
  81.                                 final LOF lof) {
  82.         this(orbitalCovariance, epoch, null, lof, OrbitType.CARTESIAN, DEFAULT_POSITION_ANGLE);
  83.     }

  84.     /**
  85.      * Constructor.
  86.      *
  87.      * @param orbitalCovariance 6x6 orbital parameters covariance
  88.      * @param epoch epoch of the covariance
  89.      * @param covarianceFrame covariance frame (inertial or Earth fixed)
  90.      * @param orbitType orbit type of the covariance (CARTESIAN if covarianceFrame is not pseudo-inertial)
  91.      * @param angleType position angle type of the covariance (not used if orbitType is CARTESIAN)
  92.      */
  93.     public FieldStateCovariance(final FieldMatrix<T> orbitalCovariance, final FieldAbsoluteDate<T> epoch,
  94.                                 final Frame covarianceFrame,
  95.                                 final OrbitType orbitType, final PositionAngleType angleType) {
  96.         this(orbitalCovariance, epoch, covarianceFrame, null, orbitType, angleType);
  97.     }

  98.     /**
  99.      * Private constructor.
  100.      *
  101.      * @param orbitalCovariance 6x6 orbital parameters covariance
  102.      * @param epoch epoch of the covariance
  103.      * @param covarianceFrame covariance frame (inertial or Earth fixed)
  104.      * @param lof covariance LOF type
  105.      * @param orbitType orbit type of the covariance
  106.      * @param angleType position angle type of the covariance (not used if orbitType is CARTESIAN)
  107.      */
  108.     private FieldStateCovariance(final FieldMatrix<T> orbitalCovariance, final FieldAbsoluteDate<T> epoch,
  109.                                  final Frame covarianceFrame, final LOF lof,
  110.                                  final OrbitType orbitType, final PositionAngleType angleType) {

  111.         StateCovariance.checkFrameAndOrbitTypeConsistency(covarianceFrame, orbitType);

  112.         this.orbitalCovariance = orbitalCovariance;
  113.         this.epoch             = epoch;
  114.         this.frame             = covarianceFrame;
  115.         this.lof               = lof;
  116.         this.orbitType         = orbitType;
  117.         this.angleType         = angleType;

  118.     }

  119.     /** {@inheritDoc}. */
  120.     @Override
  121.     public FieldAbsoluteDate<T> getDate() {
  122.         return epoch;
  123.     }

  124.     /**
  125.      * Get the covariance matrix.
  126.      *
  127.      * @return the covariance matrix
  128.      */
  129.     public FieldMatrix<T> getMatrix() {
  130.         return orbitalCovariance;
  131.     }

  132.     /**
  133.      * Get the covariance orbit type.
  134.      *
  135.      * @return the covariance orbit type
  136.      */
  137.     public OrbitType getOrbitType() {
  138.         return orbitType;
  139.     }

  140.     /**
  141.      * Get the covariance angle type.
  142.      *
  143.      * @return the covariance angle type
  144.      */
  145.     public PositionAngleType getPositionAngleType() {
  146.         return angleType;
  147.     }

  148.     /**
  149.      * Get the covariance frame.
  150.      *
  151.      * @return the covariance frame (can be null)
  152.      *
  153.      * @see #getLOF()
  154.      */
  155.     public Frame getFrame() {
  156.         return frame;
  157.     }

  158.     /**
  159.      * Get the covariance LOF type.
  160.      *
  161.      * @return the covariance LOF type (can be null)
  162.      *
  163.      * @see #getFrame()
  164.      */
  165.     public LOF getLOF() {
  166.         return lof;
  167.     }

  168.     /**
  169.      * Get the covariance matrix in another orbit type.
  170.      * <p>
  171.      * The covariance orbit type <b>cannot</b> be changed if the covariance
  172.      * matrix is expressed in a {@link LOF local orbital frame} or a
  173.      * non-pseudo inertial frame.
  174.      * <p>
  175.      * As this type change uses the jacobian matrix of the transformation, it introduces a linear approximation.
  176.      * Hence, the current covariance matrix <b>will not exactly match</b> the new linearized case and the
  177.      * distribution will not follow a generalized Gaussian distribution anymore.
  178.      * <p>
  179.      * This is based on equation (1) to (6) from "Vallado, D. A. (2004). Covariance transformations for satellite flight
  180.      * dynamics operations."
  181.      *
  182.      * @param orbit orbit to which the covariance matrix should correspond
  183.      * @param outOrbitType target orbit type of the state covariance matrix
  184.      * @param outAngleType target position angle type of the state covariance matrix
  185.      *
  186.      * @return a new covariance state, expressed in the target orbit type with the target position angle
  187.      *
  188.      * @see #changeCovarianceFrame(FieldOrbit, Frame)
  189.      */
  190.     public FieldStateCovariance<T> changeCovarianceType(final FieldOrbit<T> orbit, final OrbitType outOrbitType,
  191.                                                         final PositionAngleType outAngleType) {

  192.         // Handle case where the covariance is already expressed in the output type
  193.         if (outOrbitType == orbitType && (outAngleType == angleType || outOrbitType == OrbitType.CARTESIAN)) {
  194.             if (lof == null) {
  195.                 return new FieldStateCovariance<>(orbitalCovariance, epoch, frame, orbitType, angleType);
  196.             }
  197.             else {
  198.                 return new FieldStateCovariance<>(orbitalCovariance, epoch, lof);
  199.             }
  200.         }

  201.         // Check if the covariance is expressed in a celestial body frame
  202.         if (frame != null) {

  203.             // Check if the covariance is defined in an inertial frame
  204.             if (frame.isPseudoInertial()) {
  205.                 return changeTypeAndCreate(orbit, epoch, frame, orbitType, angleType, outOrbitType, outAngleType,
  206.                                            orbitalCovariance);
  207.             }

  208.             // The covariance is not defined in an inertial frame. The orbit type cannot be changed
  209.             throw new OrekitException(OrekitMessages.CANNOT_CHANGE_COVARIANCE_TYPE_IF_DEFINED_IN_NON_INERTIAL_FRAME);

  210.         }

  211.         // The covariance is not expressed in a celestial body frame. The orbit type cannot be changed
  212.         throw new OrekitException(OrekitMessages.CANNOT_CHANGE_COVARIANCE_TYPE_IF_DEFINED_IN_LOF);

  213.     }

  214.     /**
  215.      * Get the covariance in a given local orbital frame.
  216.      * <p>
  217.      * Changing the covariance frame is a linear process, this method does not introduce approximation unless a change
  218.      * in covariance orbit type is required.
  219.      * <p>
  220.      * This is based on equation (18) to (20) "from Vallado, D. A. (2004). Covariance transformations for satellite
  221.      * flight dynamics operations."
  222.      *
  223.      * @param orbit orbit to which the covariance matrix should correspond
  224.      * @param lofOut output local orbital frame
  225.      *
  226.      * @return a new covariance state, expressed in the output local orbital frame
  227.      */
  228.     public FieldStateCovariance<T> changeCovarianceFrame(final FieldOrbit<T> orbit, final LOF lofOut) {

  229.         // Verify current covariance frame
  230.         if (lof != null) {

  231.             // Change the covariance local orbital frame
  232.             return changeFrameAndCreate(orbit, epoch, lof, lofOut, orbitalCovariance);

  233.         } else {

  234.             // Covariance is expressed in celestial body frame
  235.             return changeFrameAndCreate(orbit, epoch, frame, lofOut, orbitalCovariance, orbitType, angleType);

  236.         }

  237.     }

  238.     /**
  239.      * Get the covariance in the output frame.
  240.      * <p>
  241.      * Changing the covariance frame is a linear process, this method does not introduce approximation unless a change
  242.      * in covariance orbit type is required.
  243.      * <p>
  244.      * This is based on equation (18) to (20) "from Vallado, D. A. (2004). Covariance transformations for satellite
  245.      * flight dynamics operations."
  246.      *
  247.      * @param orbit orbit to which the covariance matrix should correspond
  248.      * @param frameOut output frame
  249.      *
  250.      * @return a new covariance state, expressed in the output frame
  251.      */
  252.     public FieldStateCovariance<T> changeCovarianceFrame(final FieldOrbit<T> orbit, final Frame frameOut) {

  253.         // Verify current covariance frame
  254.         if (lof != null) {

  255.             // Covariance is expressed in local orbital frame
  256.             return changeFrameAndCreate(orbit, epoch, lof, frameOut, orbitalCovariance);

  257.         } else {

  258.             // Change covariance frame
  259.             return changeFrameAndCreate(orbit, epoch, frame, frameOut, orbitalCovariance, orbitType, angleType);

  260.         }

  261.     }

  262.     /**
  263.      * Get a time-shifted covariance matrix.
  264.      * <p>
  265.      * The shifting model is a linearized, Keplerian one. In other words, it is based on a state transition matrix that
  266.      * is computed assuming Keplerian motion.
  267.      * <p>
  268.      * Shifting is <em>not</em> intended as a replacement for proper covariance propagation, but should be sufficient
  269.      * for small time shifts or coarse accuracy.
  270.      *
  271.      * @param field to which the elements belong
  272.      * @param orbit orbit to which the covariance matrix should correspond
  273.      * @param dt time shift in seconds
  274.      *
  275.      * @return a new covariance state, shifted with respect to the instance
  276.      */
  277.     public FieldStateCovariance<T> shiftedBy(final Field<T> field, final FieldOrbit<T> orbit, final T dt) {

  278.         // Shifted orbit
  279.         final FieldOrbit<T> shifted = orbit.shiftedBy(dt);

  280.         // State covariance expressed in celestial body frame
  281.         if (frame != null) {

  282.             // State covariance expressed in a pseudo-inertial frame
  283.             if (frame.isPseudoInertial()) {

  284.                 // Compute STM
  285.                 final FieldMatrix<T> stm = getStm(field, orbit, dt);

  286.                 // Convert covariance in STM type (i.e., Equinoctial elements)
  287.                 final FieldStateCovariance<T> inStmType = changeTypeAndCreate(orbit, epoch, frame, orbitType, angleType,
  288.                                                                               OrbitType.EQUINOCTIAL, PositionAngleType.MEAN,
  289.                                                                               orbitalCovariance);

  290.                 // Shift covariance by applying the STM
  291.                 final FieldMatrix<T> shiftedCov = stm.multiply(inStmType.getMatrix().multiplyTransposed(stm));

  292.                 // Restore the initial covariance type
  293.                 return changeTypeAndCreate(shifted, shifted.getDate(), frame,
  294.                                            OrbitType.EQUINOCTIAL, PositionAngleType.MEAN,
  295.                                            orbitType, angleType, shiftedCov);
  296.             }

  297.             // State covariance expressed in a non pseudo-inertial frame
  298.             else {

  299.                 // Convert state covariance to orbit pseudo-inertial frame
  300.                 final FieldStateCovariance<T> inOrbitFrame = changeCovarianceFrame(orbit, orbit.getFrame());

  301.                 // Shift the state covariance
  302.                 final FieldStateCovariance<T> shiftedCovariance = inOrbitFrame.shiftedBy(field, orbit, dt);

  303.                 // Restore the initial covariance frame
  304.                 return shiftedCovariance.changeCovarianceFrame(shifted, frame);
  305.             }
  306.         }

  307.         // State covariance expressed in a commonly used local orbital frame (LOF)
  308.         else {

  309.             // Convert state covariance to orbit pseudo-inertial frame
  310.             final FieldStateCovariance<T> inOrbitFrame = changeCovarianceFrame(orbit, orbit.getFrame());

  311.             // Shift the state covariance
  312.             final FieldStateCovariance<T> shiftedCovariance = inOrbitFrame.shiftedBy(field, orbit, dt);

  313.             // Restore the initial covariance frame
  314.             return shiftedCovariance.changeCovarianceFrame(shifted, lof);
  315.         }

  316.     }

  317.     /** Get new state covariance instance.
  318.      * @return new state covariance instance.
  319.      */
  320.     public StateCovariance toStateCovariance() {

  321.         // Extract data
  322.         final T[][] data      = orbitalCovariance.getData();
  323.         final int   rowDim    = data.length;
  324.         final int   columnDim = data.length;

  325.         // Convert to non-field version
  326.         final double[][] realData = new double[rowDim][columnDim];
  327.         for (int i = 0; i < rowDim; i++) {
  328.             for (int j = 0; j < columnDim; j++) {
  329.                 realData[i][j] = data[i][j].getReal();
  330.             }
  331.         }
  332.         final BlockRealMatrix realMatrix = new BlockRealMatrix(realData);
  333.         final AbsoluteDate    realDate   = epoch.toAbsoluteDate();

  334.         // Return new state covariance according to current state covariance definition
  335.         if (frame == null) {
  336.             return new StateCovariance(realMatrix, realDate, lof);
  337.         }
  338.         else {
  339.             return new StateCovariance(realMatrix, realDate, frame, orbitType, angleType);
  340.         }
  341.     }

  342.     /**
  343.      * Create a covariance matrix in another orbit type.
  344.      * <p>
  345.      * As this type change uses the jacobian matrix of the transformation, it introduces a linear approximation.
  346.      * Hence, the input covariance matrix <b>will not exactly match</b> the new linearized case and the
  347.      * distribution will not follow a generalized Gaussian distribution anymore.
  348.      * <p>
  349.      * This is based on equation (1) to (6) from "Vallado, D. A. (2004). Covariance transformations for satellite flight
  350.      * dynamics operations."
  351.      *
  352.      * @param orbit orbit to which the covariance matrix should correspond
  353.      * @param date covariance epoch
  354.      * @param covFrame covariance frame
  355.      * @param inOrbitType initial orbit type of the state covariance matrix
  356.      * @param inAngleType initial position angle type of the state covariance matrix
  357.      * @param outOrbitType target orbit type of the state covariance matrix
  358.      * @param outAngleType target position angle type of the state covariance matrix
  359.      * @param inputCov input covariance
  360.      *
  361.      * @return the covariance expressed in the target orbit type with the target position angle
  362.      */
  363.     private FieldStateCovariance<T> changeTypeAndCreate(final FieldOrbit<T> orbit,
  364.                                                         final FieldAbsoluteDate<T> date,
  365.                                                         final Frame covFrame,
  366.                                                         final OrbitType inOrbitType,
  367.                                                         final PositionAngleType inAngleType,
  368.                                                         final OrbitType outOrbitType,
  369.                                                         final PositionAngleType outAngleType,
  370.                                                         final FieldMatrix<T> inputCov) {

  371.         // Notations:
  372.         // I: Input orbit type
  373.         // O: Output orbit type
  374.         // C: Cartesian parameters

  375.         // Extract field
  376.         final Field<T> field = date.getField();

  377.         // Compute dOutputdCartesian
  378.         final T[][]         aOC               = MathArrays.buildArray(field, STATE_DIMENSION, STATE_DIMENSION);
  379.         final FieldOrbit<T> orbitInOutputType = outOrbitType.convertType(orbit);
  380.         orbitInOutputType.getJacobianWrtCartesian(outAngleType, aOC);
  381.         final FieldMatrix<T> dOdC = new Array2DRowFieldMatrix<>(aOC, false);

  382.         // Compute dCartesiandInput
  383.         final T[][]         aCI              = MathArrays.buildArray(field, STATE_DIMENSION, STATE_DIMENSION);
  384.         final FieldOrbit<T> orbitInInputType = inOrbitType.convertType(orbit);
  385.         orbitInInputType.getJacobianWrtParameters(inAngleType, aCI);
  386.         final FieldMatrix<T> dCdI = new Array2DRowFieldMatrix<>(aCI, false);

  387.         // Compute dOutputdInput
  388.         final FieldMatrix<T> dOdI = dOdC.multiply(dCdI);

  389.         // Conversion of the state covariance matrix in target orbit type
  390.         final FieldMatrix<T> outputCov = dOdI.multiply(inputCov.multiplyTransposed(dOdI));

  391.         // Return the converted covariance
  392.         return new FieldStateCovariance<>(outputCov, date, covFrame, outOrbitType, outAngleType);

  393.     }

  394.     /**
  395.      * Create a covariance matrix from a {@link LOF local orbital frame} to another
  396.      * {@link LOF local orbital frame}.
  397.      * <p>
  398.      * Changing the covariance frame is a linear process, this method does not introduce approximation.
  399.      * <p>
  400.      * The transformation is based on Equation (18) to (20) of "Covariance Transformations for Satellite Flight Dynamics
  401.      * Operations" by David A. Vallado.
  402.      *
  403.      * @param orbit orbit to which the covariance matrix should correspond
  404.      * @param date covariance epoch
  405.      * @param lofIn the local orbital frame in which the input covariance matrix is expressed
  406.      * @param lofOut the target local orbital frame
  407.      * @param inputCartesianCov input covariance {@code CARTESIAN})
  408.      *
  409.      * @return the covariance matrix expressed in the target commonly used local orbital frame in cartesian elements
  410.      */
  411.     private FieldStateCovariance<T> changeFrameAndCreate(final FieldOrbit<T> orbit,
  412.                                                          final FieldAbsoluteDate<T> date,
  413.                                                          final LOF lofIn,
  414.                                                          final LOF lofOut,
  415.                                                          final FieldMatrix<T> inputCartesianCov) {

  416.         // Builds the matrix to perform covariance transformation
  417.         final FieldMatrix<T> jacobianFromLofInToLofOut =
  418.                 getJacobian(date.getField(),
  419.                             LOF.transformFromLOFInToLOFOut(lofIn, lofOut, date, orbit.getPVCoordinates()));

  420.         // Get the Cartesian covariance matrix converted to frameOut
  421.         final FieldMatrix<T> cartesianCovarianceOut =
  422.                 jacobianFromLofInToLofOut.multiply(inputCartesianCov.multiplyTransposed(jacobianFromLofInToLofOut));

  423.         // Output converted covariance
  424.         return new FieldStateCovariance<>(cartesianCovarianceOut, date, lofOut);

  425.     }

  426.     /**
  427.      * Convert the covariance matrix from a {@link Frame frame} to a {@link LOF local orbital frame}.
  428.      * <p>
  429.      * Changing the covariance frame is a linear process, this method does not introduce approximation unless a change
  430.      * in covariance orbit type is required.
  431.      * <p>
  432.      * The transformation is based on Equation (18) to (20) of "Covariance Transformations for Satellite Flight Dynamics
  433.      * Operations" by David A. Vallado.
  434.      * <p>
  435.      * As the frame transformation must be performed with the covariance expressed in Cartesian elements, both the orbit
  436.      * and position angle types of the input covariance must be provided.
  437.      * <p>
  438.      * <b>The output covariance matrix will provided in Cartesian orbit type and not converted back to
  439.      * its original expression (if input different from Cartesian elements).</b>
  440.      *
  441.      * @param orbit orbit to which the covariance matrix should correspond
  442.      * @param date covariance epoch
  443.      * @param frameIn the frame in which the input covariance matrix is expressed. In case the frame is <b>not</b>
  444.      * pseudo-inertial, the input covariance matrix is expected to be expressed in <b>Cartesian elements</b>.
  445.      * @param lofOut the target local orbital frame
  446.      * @param inputCov input covariance
  447.      * @param covOrbitType orbit type of the covariance matrix (used if frameIn is pseudo-inertial)
  448.      * @param covAngleType position angle type of the covariance matrix (used if frameIn is pseudo-inertial) (not used
  449.      * if covOrbitType equals {@code CARTESIAN})
  450.      * @return the covariance matrix expressed in the target local orbital frame in Cartesian elements
  451.      * @throws OrekitException if input frame is <b>not</b> pseudo-inertial <b>and</b> the input covariance is
  452.      * <b>not</b> expressed in Cartesian elements.
  453.      */
  454.     private FieldStateCovariance<T> changeFrameAndCreate(final FieldOrbit<T> orbit,
  455.                                                          final FieldAbsoluteDate<T> date,
  456.                                                          final Frame frameIn,
  457.                                                          final LOF lofOut,
  458.                                                          final FieldMatrix<T> inputCov,
  459.                                                          final OrbitType covOrbitType,
  460.                                                          final PositionAngleType covAngleType) {

  461.         // Input frame is inertial
  462.         if (frameIn.isPseudoInertial()) {

  463.             // Convert input matrix to Cartesian parameters in input frame
  464.             final FieldMatrix<T> cartesianCovarianceIn =
  465.                     changeTypeAndCreate(orbit, date, frameIn, covOrbitType, covAngleType,
  466.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN,
  467.                                         inputCov).getMatrix();

  468.             // Builds the matrix to perform covariance transformation
  469.             final FieldMatrix<T> jacobianFromFrameInToLofOut =
  470.                     getJacobian(date.getField(), lofOut.transformFromInertial(date, orbit.getPVCoordinates(frameIn)));

  471.             // Get the Cartesian covariance matrix converted to frameOut
  472.             final FieldMatrix<T> cartesianCovarianceOut =
  473.                     jacobianFromFrameInToLofOut.multiply(
  474.                             cartesianCovarianceIn.multiplyTransposed(jacobianFromFrameInToLofOut));

  475.             // Return converted covariance matrix expressed in cartesian elements
  476.             return new FieldStateCovariance<>(cartesianCovarianceOut, date, lofOut);

  477.         }

  478.         // Input frame is not inertial so the covariance matrix is expected to be in cartesian elements
  479.         else {

  480.             // Method checkInputConsistency already verify that input covariance is defined in CARTESIAN type
  481.             final Frame orbitInertialFrame = orbit.getFrame();

  482.             // Compute rotation matrix from frameIn to orbit inertial frame
  483.             final FieldMatrix<T> cartesianCovarianceInOrbitFrame =
  484.                     changeFrameAndCreate(orbit, date, frameIn, orbitInertialFrame, inputCov,
  485.                                          OrbitType.CARTESIAN, PositionAngleType.MEAN).getMatrix();

  486.             // Convert from orbit inertial frame to lofOut
  487.             return changeFrameAndCreate(orbit, date, orbitInertialFrame, lofOut, cartesianCovarianceInOrbitFrame,
  488.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN);

  489.         }

  490.     }

  491.     /**
  492.      * Convert the covariance matrix from a {@link LOF  local orbital frame} to a {@link Frame frame}.
  493.      * <p>
  494.      * Changing the covariance frame is a linear process, this method does not introduce approximation.
  495.      * <p>
  496.      * The transformation is based on Equation (18) to (20) of "Covariance Transformations for Satellite Flight Dynamics
  497.      * Operations" by David A. Vallado.
  498.      * <p>
  499.      * The <u>input</u> covariance matrix is necessarily provided in <b>Cartesian orbit type</b>.
  500.      * <p>
  501.      * The <u>output</u> covariance matrix will be expressed in <b>Cartesian elements</b>.
  502.      *
  503.      * @param orbit orbit to which the covariance matrix should correspond
  504.      * @param date covariance epoch
  505.      * @param lofIn the local orbital frame in which the input covariance matrix is expressed
  506.      * @param frameOut the target frame
  507.      * @param inputCartesianCov input covariance ({@code CARTESIAN})
  508.      *
  509.      * @return the covariance matrix expressed in the target frame in cartesian elements
  510.      */
  511.     private FieldStateCovariance<T> changeFrameAndCreate(final FieldOrbit<T> orbit,
  512.                                                          final FieldAbsoluteDate<T> date,
  513.                                                          final LOF lofIn,
  514.                                                          final Frame frameOut,
  515.                                                          final FieldMatrix<T> inputCartesianCov) {

  516.         // Output frame is pseudo-inertial
  517.         if (frameOut.isPseudoInertial()) {

  518.             // Builds the matrix to perform covariance transformation
  519.             final FieldMatrix<T> jacobianFromLofInToFrameOut =
  520.                     getJacobian(date.getField(),
  521.                                 lofIn.transformFromInertial(date, orbit.getPVCoordinates(frameOut)).getInverse());

  522.             // Transform covariance
  523.             final FieldMatrix<T> transformedCovariance =
  524.                     jacobianFromLofInToFrameOut.multiply(
  525.                             inputCartesianCov.multiplyTransposed(jacobianFromLofInToFrameOut));

  526.             // Get the Cartesian covariance matrix converted to frameOut
  527.             return new FieldStateCovariance<>(transformedCovariance, date, frameOut, OrbitType.CARTESIAN,
  528.                                               DEFAULT_POSITION_ANGLE);

  529.         }

  530.         // Output frame is not pseudo-inertial
  531.         else {

  532.             // Builds the matrix to perform covariance transformation
  533.             final FieldMatrix<T> jacobianFromLofInToOrbitFrame =
  534.                     getJacobian(date.getField(),
  535.                                 lofIn.transformFromInertial(date, orbit.getPVCoordinates()).getInverse());

  536.             // Get the Cartesian covariance matrix converted to orbit inertial frame
  537.             final FieldMatrix<T> cartesianCovarianceInOrbitFrame = jacobianFromLofInToOrbitFrame.multiply(
  538.                     inputCartesianCov.multiplyTransposed(jacobianFromLofInToOrbitFrame));

  539.             // Get the Cartesian covariance matrix converted to frameOut
  540.             return changeFrameAndCreate(orbit, date, orbit.getFrame(), frameOut, cartesianCovarianceInOrbitFrame,
  541.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN);
  542.         }

  543.     }

  544.     /**
  545.      * Get the covariance matrix in another frame.
  546.      * <p>
  547.      * The transformation is based on Equation (18) of "Covariance Transformations for Satellite Flight Dynamics
  548.      * Operations" by David A. Vallado.
  549.      * <p>
  550.      * Changing the covariance frame is a linear process, this method does not introduce approximation unless a change
  551.      * in covariance orbit type is required.
  552.      * <p>
  553.      * As the frame transformation must be performed with the covariance expressed in Cartesian elements, both the orbit
  554.      * and position angle types of the input covariance must be provided.
  555.      * <p>
  556.      * In case the <u>input</u> frame is <b>not</b> pseudo-inertial, the <u>input</u> covariance matrix is necessarily
  557.      * expressed in <b>Cartesian elements</b>.
  558.      * <p>
  559.      * In case the <u>output</u> frame is <b>not</b> pseudo-inertial, the <u>output</u> covariance matrix will be
  560.      * expressed in <b>Cartesian elements</b>.
  561.      *
  562.      * @param orbit orbit to which the covariance matrix should correspond
  563.      * @param date covariance epoch
  564.      * @param frameIn the frame in which the input covariance matrix is expressed
  565.      * @param frameOut the target frame
  566.      * @param inputCov input covariance
  567.      * @param covOrbitType orbit type of the covariance matrix (used if frameIn is pseudo-inertial)
  568.      * @param covAngleType position angle type of the covariance matrix (used if frameIn is pseudo-inertial) (<b>not</b>
  569.      * used if covOrbitType equals {@code CARTESIAN})
  570.      * @return the covariance matrix expressed in the target frame
  571.      *
  572.      * @throws OrekitException if input frame is <b>not</b> pseudo-inertial <b>and</b> the input covariance is
  573.      * <b>not</b> expressed in cartesian elements.
  574.      */
  575.     private FieldStateCovariance<T> changeFrameAndCreate(final FieldOrbit<T> orbit,
  576.                                                          final FieldAbsoluteDate<T> date,
  577.                                                          final Frame frameIn,
  578.                                                          final Frame frameOut,
  579.                                                          final FieldMatrix<T> inputCov,
  580.                                                          final OrbitType covOrbitType,
  581.                                                          final PositionAngleType covAngleType) {

  582.         // Get the transform from the covariance frame to the output frame
  583.         final FieldTransform<T> inToOut = frameIn.getTransformTo(frameOut, orbit.getDate());

  584.         // Matrix to perform the covariance transformation
  585.         final FieldMatrix<T> j = getJacobian(date.getField(), inToOut);

  586.         // Input frame pseudo-inertial
  587.         if (frameIn.isPseudoInertial()) {

  588.             // Convert input matrix to Cartesian parameters in input frame
  589.             final FieldMatrix<T> cartesianCovarianceIn =
  590.                     changeTypeAndCreate(orbit, date, frameIn, covOrbitType, covAngleType,
  591.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN,
  592.                                         inputCov).getMatrix();

  593.             // Get the Cartesian covariance matrix converted to frameOut
  594.             final FieldMatrix<T> cartesianCovarianceOut = j.multiply(cartesianCovarianceIn.multiplyTransposed(j));

  595.             // Output frame is pseudo-inertial
  596.             if (frameOut.isPseudoInertial()) {

  597.                 // Convert output Cartesian matrix to initial orbit type and position angle
  598.                 return changeTypeAndCreate(orbit, date, frameOut, OrbitType.CARTESIAN, PositionAngleType.MEAN,
  599.                                            covOrbitType, covAngleType, cartesianCovarianceOut);

  600.             }

  601.             // Output frame is not pseudo-inertial
  602.             else {

  603.                 // Output Cartesian matrix
  604.                 return new FieldStateCovariance<>(cartesianCovarianceOut, date, frameOut, OrbitType.CARTESIAN,
  605.                                                   DEFAULT_POSITION_ANGLE);

  606.             }
  607.         }

  608.         // Input frame is not pseudo-inertial
  609.         else {

  610.             // Method checkInputConsistency already verify that input covariance is defined in CARTESIAN type

  611.             // Convert covariance matrix to frameOut
  612.             final FieldMatrix<T> covInFrameOut = j.multiply(inputCov.multiplyTransposed(j));

  613.             // Output the Cartesian covariance matrix converted to frameOut
  614.             return new FieldStateCovariance<>(covInFrameOut, date, frameOut, OrbitType.CARTESIAN, DEFAULT_POSITION_ANGLE);

  615.         }

  616.     }

  617.     /**
  618.      * Builds the matrix to perform covariance frame transformation.
  619.      *
  620.      * @param field to which the elements belong
  621.      * @param transform input transformation
  622.      *
  623.      * @return the matrix to perform the covariance frame transformation
  624.      */
  625.     private FieldMatrix<T> getJacobian(final Field<T> field, final FieldTransform<T> transform) {
  626.         // Get the Jacobian of the transform
  627.         final T[][] jacobian = MathArrays.buildArray(field, STATE_DIMENSION, STATE_DIMENSION);
  628.         transform.getJacobian(CartesianDerivativesFilter.USE_PV, jacobian);
  629.         // Return
  630.         return new Array2DRowFieldMatrix<>(jacobian, false);

  631.     }

  632.     /**
  633.      * Get the state transition matrix considering Keplerian contribution only.
  634.      *
  635.      * @param field to which the elements belong
  636.      * @param initialOrbit orbit to which the initial covariance matrix should correspond
  637.      * @param dt time difference between the two orbits
  638.      *
  639.      * @return the state transition matrix used to shift the covariance matrix
  640.      */
  641.     private FieldMatrix<T> getStm(final Field<T> field, final FieldOrbit<T> initialOrbit, final T dt) {

  642.         // initialize the STM
  643.         final FieldMatrix<T> stm = MatrixUtils.createFieldIdentityMatrix(field, STATE_DIMENSION);

  644.         // State transition matrix using Keplerian contribution only
  645.         final T mu           = initialOrbit.getMu();
  646.         final T sma          = initialOrbit.getA();
  647.         final T contribution = mu.divide(sma.pow(5)).sqrt().multiply(dt).multiply(-1.5);
  648.         stm.setEntry(5, 0, contribution);

  649.         // Return
  650.         return stm;

  651.     }

  652. }