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

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

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

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

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

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

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

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

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

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

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

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

  110.         StateCovariance.checkFrameAndOrbitTypeConsistency(covarianceFrame, orbitType);

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

  117.     }

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

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

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

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

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

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

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

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

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

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

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

  209.         }

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

  212.     }

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

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

  230.             // Check specific case where output covariance will be the same
  231.             if (lofOut == lof) {
  232.                 return new FieldStateCovariance<>(orbitalCovariance, epoch, lof);
  233.             }

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

  236.         } else {

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

  239.         }

  240.     }

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

  256.         // Verify current covariance frame
  257.         if (lof != null) {

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

  260.         } else {

  261.             // Check specific case where output covariance will be the same
  262.             if (frame == frameOut) {
  263.                 return new FieldStateCovariance<>(orbitalCovariance, epoch, frame, orbitType, angleType);
  264.             }

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

  267.         }

  268.     }

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

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

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

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

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

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

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

  299.                 // Restore the initial covariance type
  300.                 return changeTypeAndCreate(shifted, shifted.getDate(), frame,
  301.                                            OrbitType.EQUINOCTIAL, PositionAngleType.MEAN,
  302.                                            orbitType, angleType, shiftedCov);
  303.             }

  304.             // State covariance expressed in a non pseudo-inertial frame
  305.             else {

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

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

  310.                 // Restore the initial covariance frame
  311.                 return shiftedCovariance.changeCovarianceFrame(shifted, frame);
  312.             }
  313.         }

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

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

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

  320.             // Restore the initial covariance frame
  321.             return shiftedCovariance.changeCovarianceFrame(shifted, lof);
  322.         }

  323.     }

  324.     /** Get new state covariance instance.
  325.      * @return new state covariance instance.
  326.      */
  327.     public StateCovariance toStateCovariance() {

  328.         // Extract data
  329.         final T[][] data      = orbitalCovariance.getData();
  330.         final int   rowDim    = data.length;
  331.         final int   columnDim = data.length;

  332.         // Convert to non-field version
  333.         final double[][] realData = new double[rowDim][columnDim];
  334.         for (int i = 0; i < rowDim; i++) {
  335.             for (int j = 0; j < columnDim; j++) {
  336.                 realData[i][j] = data[i][j].getReal();
  337.             }
  338.         }
  339.         final BlockRealMatrix realMatrix = new BlockRealMatrix(realData);
  340.         final AbsoluteDate    realDate   = epoch.toAbsoluteDate();

  341.         // Return new state covariance according to current state covariance definition
  342.         if (frame == null) {
  343.             return new StateCovariance(realMatrix, realDate, lof);
  344.         }
  345.         else {
  346.             return new StateCovariance(realMatrix, realDate, frame, orbitType, angleType);
  347.         }
  348.     }

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

  378.         // Check if type change is really necessary, if not then return input covariance
  379.         if (StateCovariance.inputAndOutputOrbitTypesAreCartesian(inOrbitType, outOrbitType) ||
  380.             StateCovariance.inputAndOutputAreIdentical(inOrbitType, inAngleType, outOrbitType, outAngleType)) {
  381.             return new FieldStateCovariance<>(inputCov, date, covFrame, inOrbitType, inAngleType);
  382.         }

  383.         // Notations:
  384.         // I: Input orbit type
  385.         // O: Output orbit type
  386.         // C: Cartesian parameters

  387.         // Extract field
  388.         final Field<T> field = date.getField();

  389.         // Compute dOutputdCartesian
  390.         final T[][]         aOC               = MathArrays.buildArray(field, STATE_DIMENSION, STATE_DIMENSION);
  391.         final FieldOrbit<T> orbitInOutputType = outOrbitType.convertType(orbit);
  392.         orbitInOutputType.getJacobianWrtCartesian(outAngleType, aOC);
  393.         final FieldMatrix<T> dOdC = new Array2DRowFieldMatrix<>(aOC, false);

  394.         // Compute dCartesiandInput
  395.         final T[][]         aCI              = MathArrays.buildArray(field, STATE_DIMENSION, STATE_DIMENSION);
  396.         final FieldOrbit<T> orbitInInputType = inOrbitType.convertType(orbit);
  397.         orbitInInputType.getJacobianWrtParameters(inAngleType, aCI);
  398.         final FieldMatrix<T> dCdI = new Array2DRowFieldMatrix<>(aCI, false);

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

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

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

  405.     }

  406.     /**
  407.      * Create a covariance matrix from a {@link LOF local orbital frame} to another
  408.      * {@link LOF local orbital frame}.
  409.      * <p>
  410.      * Changing the covariance frame is a linear process, this method does not introduce approximation.
  411.      * <p>
  412.      * The transformation is based on Equation (18) to (20) of "Covariance Transformations for Satellite Flight Dynamics
  413.      * Operations" by David A. Vallado.
  414.      *
  415.      * @param orbit orbit to which the covariance matrix should correspond
  416.      * @param date covariance epoch
  417.      * @param lofIn the local orbital frame in which the input covariance matrix is expressed
  418.      * @param lofOut the target local orbital frame
  419.      * @param inputCartesianCov input covariance {@code CARTESIAN})
  420.      *
  421.      * @return the covariance matrix expressed in the target commonly used local orbital frame in cartesian elements
  422.      */
  423.     private FieldStateCovariance<T> changeFrameAndCreate(final FieldOrbit<T> orbit,
  424.                                                          final FieldAbsoluteDate<T> date,
  425.                                                          final LOF lofIn,
  426.                                                          final LOF lofOut,
  427.                                                          final FieldMatrix<T> inputCartesianCov) {

  428.         // Builds the matrix to perform covariance transformation
  429.         final FieldMatrix<T> jacobianFromLofInToLofOut =
  430.                 getJacobian(LOF.transformFromLOFInToLOFOut(lofIn, lofOut, date, orbit.getPVCoordinates()));

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

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

  436.     }

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

  472.         // Input frame is inertial
  473.         if (frameIn.isPseudoInertial()) {

  474.             // Convert input matrix to Cartesian parameters in input frame
  475.             final FieldMatrix<T> cartesianCovarianceIn =
  476.                     changeTypeAndCreate(orbit, date, frameIn, covOrbitType, covAngleType,
  477.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN,
  478.                                         inputCov).getMatrix();

  479.             // Builds the matrix to perform covariance transformation
  480.             final FieldMatrix<T> jacobianFromFrameInToLofOut =
  481.                     getJacobian(lofOut.transformFromInertial(date, orbit.getPVCoordinates(frameIn)));

  482.             // Get the Cartesian covariance matrix converted to frameOut
  483.             final FieldMatrix<T> cartesianCovarianceOut =
  484.                     jacobianFromFrameInToLofOut.multiply(
  485.                             cartesianCovarianceIn.multiplyTransposed(jacobianFromFrameInToLofOut));

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

  488.         }

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

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

  493.             // Compute rotation matrix from frameIn to orbit inertial frame
  494.             final FieldMatrix<T> cartesianCovarianceInOrbitFrame =
  495.                     changeFrameAndCreate(orbit, date, frameIn, orbitInertialFrame, inputCov,
  496.                                          OrbitType.CARTESIAN, PositionAngleType.MEAN).getMatrix();

  497.             // Convert from orbit inertial frame to lofOut
  498.             return changeFrameAndCreate(orbit, date, orbitInertialFrame, lofOut, cartesianCovarianceInOrbitFrame,
  499.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN);

  500.         }

  501.     }

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

  527.         // Output frame is pseudo-inertial
  528.         if (frameOut.isPseudoInertial()) {

  529.             // Builds the matrix to perform covariance transformation
  530.             final FieldMatrix<T> jacobianFromLofInToFrameOut =
  531.                     getJacobian(lofIn.transformFromInertial(date, orbit.getPVCoordinates(frameOut)).getInverse());

  532.             // Transform covariance
  533.             final FieldMatrix<T> transformedCovariance =
  534.                     jacobianFromLofInToFrameOut.multiply(
  535.                             inputCartesianCov.multiplyTransposed(jacobianFromLofInToFrameOut));

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

  539.         }

  540.         // Output frame is not pseudo-inertial
  541.         else {

  542.             // Builds the matrix to perform covariance transformation
  543.             final FieldMatrix<T> jacobianFromLofInToOrbitFrame =
  544.                     getJacobian(lofIn.transformFromInertial(date, orbit.getPVCoordinates()).getInverse());

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

  548.             // Get the Cartesian covariance matrix converted to frameOut
  549.             return changeFrameAndCreate(orbit, date, orbit.getFrame(), frameOut, cartesianCovarianceInOrbitFrame,
  550.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN);
  551.         }

  552.     }

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

  591.         // Get the transform from the covariance frame to the output frame
  592.         final FieldKinematicTransform<T> inToOut = frameIn.getKinematicTransformTo(frameOut, orbit.getDate());

  593.         // Matrix to perform the covariance transformation
  594.         final FieldMatrix<T> j = getJacobian(inToOut);

  595.         // Input frame pseudo-inertial
  596.         if (frameIn.isPseudoInertial()) {

  597.             // Convert input matrix to Cartesian parameters in input frame
  598.             final FieldMatrix<T> cartesianCovarianceIn =
  599.                     changeTypeAndCreate(orbit, date, frameIn, covOrbitType, covAngleType,
  600.                                         OrbitType.CARTESIAN, PositionAngleType.MEAN,
  601.                                         inputCov).getMatrix();

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

  604.             // Output frame is pseudo-inertial
  605.             if (frameOut.isPseudoInertial()) {

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

  609.             }

  610.             // Output frame is not pseudo-inertial
  611.             else {

  612.                 // Output Cartesian matrix
  613.                 return new FieldStateCovariance<>(cartesianCovarianceOut, date, frameOut, OrbitType.CARTESIAN,
  614.                                                   DEFAULT_POSITION_ANGLE);

  615.             }
  616.         }

  617.         // Input frame is not pseudo-inertial
  618.         else {

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

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

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

  624.         }

  625.     }

  626.     /**
  627.      * Builds the matrix to perform covariance frame transformation.
  628.      *
  629.      * @param transform input transformation
  630.      *
  631.      * @return the matrix to perform the covariance frame transformation
  632.      */
  633.     private FieldMatrix<T> getJacobian(final FieldKinematicTransform<T> transform) {
  634.         return MatrixUtils.createFieldMatrix(transform.getPVJacobian());
  635.     }

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

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

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

  653.         // Return
  654.         return stm;

  655.     }

  656. }