TurnAroundRangeIonosphericDelayModifier.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.estimation.measurements.modifiers;

  18. import java.util.Arrays;
  19. import java.util.List;

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.hipparchus.analysis.differentiation.Gradient;
  22. import org.orekit.attitudes.FrameAlignedProvider;
  23. import org.orekit.estimation.measurements.EstimatedMeasurement;
  24. import org.orekit.estimation.measurements.EstimatedMeasurementBase;
  25. import org.orekit.estimation.measurements.EstimationModifier;
  26. import org.orekit.estimation.measurements.GroundStation;
  27. import org.orekit.estimation.measurements.TurnAroundRange;
  28. import org.orekit.frames.TopocentricFrame;
  29. import org.orekit.models.earth.ionosphere.IonosphericModel;
  30. import org.orekit.propagation.FieldSpacecraftState;
  31. import org.orekit.propagation.SpacecraftState;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.Differentiation;
  34. import org.orekit.utils.ParameterDriver;
  35. import org.orekit.utils.ParameterFunction;
  36. import org.orekit.utils.TimeSpanMap.Span;

  37. /** Class modifying theoretical TurnAroundRange measurement with ionospheric delay.
  38.  * <p>
  39.  * The effect of ionospheric correction on the TurnAroundRange is directly computed
  40.  * through the computation of the ionospheric delay.
  41.  * </p>
  42.  * <p>
  43.  * The ionospheric delay depends on the frequency of the signal (GNSS, VLBI, ...).
  44.  * For optical measurements (e.g. SLR), the ray is not affected by ionosphere charged particles.
  45.  * </p>
  46.  * <p>
  47.  * Since 10.0, state derivatives and ionospheric parameters derivates are computed
  48.  * using automatic differentiation.
  49.  * </p>
  50.  * @author Maxime Journot
  51.  * @since 9.0
  52.  */
  53. public class TurnAroundRangeIonosphericDelayModifier implements EstimationModifier<TurnAroundRange> {

  54.     /** Ionospheric delay model. */
  55.     private final IonosphericModel ionoModel;

  56.     /** Frequency [Hz]. */
  57.     private final double frequency;

  58.     /** Constructor.
  59.      *
  60.      * @param model  Ionospheric delay model appropriate for the current TurnAroundRange measurement method.
  61.      * @param freq frequency of the signal in Hz
  62.      */
  63.     public TurnAroundRangeIonosphericDelayModifier(final IonosphericModel model,
  64.                                                    final double freq) {
  65.         ionoModel = model;
  66.         frequency = freq;
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public String getEffectName() {
  71.         return "ionosphere";
  72.     }

  73.     /** Compute the measurement error due to ionosphere.
  74.      * @param station station
  75.      * @param state spacecraft state
  76.      * @return the measurement error due to ionosphere
  77.      */
  78.     private double rangeErrorIonosphericModel(final GroundStation station,
  79.                                               final SpacecraftState state) {
  80.         // Base frame associated with the station
  81.         final TopocentricFrame baseFrame = station.getBaseFrame();
  82.         // Delay in meters
  83.         return ionoModel.pathDelay(state, baseFrame, frequency, ionoModel.getParameters(state.getDate()));
  84.     }

  85.     /** Compute the measurement error due to ionosphere.
  86.      * @param <T> type of the elements
  87.      * @param station station
  88.      * @param state spacecraft state
  89.      * @param parameters ionospheric model parameters
  90.      * @return the measurement error due to ionosphere
  91.      */
  92.     private <T extends CalculusFieldElement<T>> T rangeErrorIonosphericModel(final GroundStation station,
  93.                                                                              final FieldSpacecraftState<T> state,
  94.                                                                              final T[] parameters) {
  95.         // Base frame associated with the station
  96.         final TopocentricFrame baseFrame = station.getBaseFrame();
  97.         // Delay in meters
  98.         return ionoModel.pathDelay(state, baseFrame, frequency, parameters);
  99.     }

  100.     /** Compute the Jacobian of the delay term wrt state using
  101.     * automatic differentiation.
  102.     *
  103.     * @param derivatives ionospheric delay derivatives
  104.     *
  105.     * @return Jacobian of the delay wrt state
  106.     */
  107.     private double[][] rangeErrorJacobianState(final double[] derivatives) {
  108.         final double[][] finiteDifferencesJacobian = new double[1][6];
  109.         System.arraycopy(derivatives, 0, finiteDifferencesJacobian[0], 0, 6);
  110.         return finiteDifferencesJacobian;
  111.     }


  112.     /** Compute the derivative of the delay term wrt parameters.
  113.      *
  114.      * @param station ground station
  115.      * @param driver driver for the station offset parameter
  116.      * @param state spacecraft state
  117.      * @return derivative of the delay wrt station offset parameter
  118.      */
  119.     private double rangeErrorParameterDerivative(final GroundStation station,
  120.                                                  final ParameterDriver driver,
  121.                                                  final SpacecraftState state) {

  122.         final ParameterFunction rangeError = new ParameterFunction() {
  123.             /** {@inheritDoc} */
  124.             @Override
  125.             public double value(final ParameterDriver parameterDriver, final AbsoluteDate date) {
  126.                 return rangeErrorIonosphericModel(station, state);
  127.             }
  128.         };

  129.         final ParameterFunction rangeErrorDerivative =
  130.                         Differentiation.differentiate(rangeError, 3, 10.0 * driver.getScale());

  131.         return rangeErrorDerivative.value(driver, state.getDate());

  132.     }

  133.     /** Compute the derivative of the delay term wrt parameters using
  134.     * automatic differentiation.
  135.     *
  136.     * @param derivatives ionospheric delay derivatives
  137.     * @param freeStateParameters dimension of the state.
  138.     * @return derivative of the delay wrt ionospheric model parameters
  139.     */
  140.     private double[] rangeErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
  141.         // 0 ... freeStateParameters - 1 -> derivatives of the delay wrt state
  142.         // freeStateParameters ... n     -> derivatives of the delay wrt ionospheric parameters
  143.         return Arrays.copyOfRange(derivatives, freeStateParameters, derivatives.length);
  144.     }

  145.     /** {@inheritDoc} */
  146.     @Override
  147.     public List<ParameterDriver> getParametersDrivers() {
  148.         return ionoModel.getParametersDrivers();
  149.     }

  150.     @Override
  151.     public void modifyWithoutDerivatives(final EstimatedMeasurementBase<TurnAroundRange> estimated) {

  152.         final TurnAroundRange measurement      = estimated.getObservedMeasurement();
  153.         final GroundStation   primaryStation   = measurement.getPrimaryStation();
  154.         final GroundStation   secondaryStation = measurement.getSecondaryStation();
  155.         final SpacecraftState state            = estimated.getStates()[0];

  156.         // Update estimated value taking into account the ionospheric delay.
  157.         // The ionospheric delay is directly added to the TurnAroundRange.
  158.         final double[] newValue     = estimated.getEstimatedValue();
  159.         final double primaryDelay   = rangeErrorIonosphericModel(primaryStation, state);
  160.         final double secondaryDelay = rangeErrorIonosphericModel(secondaryStation, state);
  161.         newValue[0] = newValue[0] + primaryDelay + secondaryDelay;
  162.         estimated.modifyEstimatedValue(this, newValue);

  163.     }

  164.     @Override
  165.     public void modify(final EstimatedMeasurement<TurnAroundRange> estimated) {
  166.         final TurnAroundRange measurement      = estimated.getObservedMeasurement();
  167.         final GroundStation   primaryStation   = measurement.getPrimaryStation();
  168.         final GroundStation   secondaryStation = measurement.getSecondaryStation();
  169.         final SpacecraftState state            = estimated.getStates()[0];

  170.         // Update estimated derivatives with Jacobian of the measure wrt state
  171.         final ModifierGradientConverter converter =
  172.                 new ModifierGradientConverter(state, 6, new FrameAlignedProvider(state.getFrame()));
  173.         final FieldSpacecraftState<Gradient> gState = converter.getState(ionoModel);
  174.         final Gradient[] gParameters        = converter.getParametersAtStateDate(gState, ionoModel);
  175.         final Gradient primaryGDelay        = rangeErrorIonosphericModel(primaryStation, gState, gParameters);
  176.         final Gradient secondaryGDelay      = rangeErrorIonosphericModel(secondaryStation, gState, gParameters);
  177.         final double[] primaryDerivatives   = primaryGDelay.getGradient();
  178.         final double[] secondaryDerivatives = secondaryGDelay.getGradient();

  179.         final double[][] primaryDjac      = rangeErrorJacobianState(primaryDerivatives);
  180.         final double[][] secondaryDjac    = rangeErrorJacobianState(secondaryDerivatives);
  181.         final double[][] stateDerivatives = estimated.getStateDerivatives(0);
  182.         for (int irow = 0; irow < stateDerivatives.length; ++irow) {
  183.             for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
  184.                 stateDerivatives[irow][jcol] += primaryDjac[irow][jcol] + secondaryDjac[irow][jcol];
  185.             }
  186.         }
  187.         estimated.setStateDerivatives(0, stateDerivatives);

  188.         int indexPrimary = 0;
  189.         for (final ParameterDriver driver : getParametersDrivers()) {
  190.             if (driver.isSelected()) {
  191.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  192.                     // update estimated derivatives with derivative of the modification wrt ionospheric parameters
  193.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  194.                     final double[] derivatives = rangeErrorParameterDerivative(primaryDerivatives, converter.getFreeStateParameters());
  195.                     parameterDerivative += derivatives[indexPrimary];
  196.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  197.                     indexPrimary += 1;
  198.                 }
  199.             }

  200.         }

  201.         int indexSecondary = 0;
  202.         for (final ParameterDriver driver : getParametersDrivers()) {
  203.             if (driver.isSelected()) {
  204.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  205.                     // update estimated derivatives with derivative of the modification wrt ionospheric parameters
  206.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  207.                     final double[] derivatives = rangeErrorParameterDerivative(secondaryDerivatives, converter.getFreeStateParameters());
  208.                     parameterDerivative += derivatives[indexSecondary];
  209.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  210.                     indexSecondary += 1;
  211.                 }
  212.             }

  213.         }

  214.         // Update derivatives with respect to primary station position
  215.         for (final ParameterDriver driver : Arrays.asList(primaryStation.getClockOffsetDriver(),
  216.                                                           primaryStation.getEastOffsetDriver(),
  217.                                                           primaryStation.getNorthOffsetDriver(),
  218.                                                           primaryStation.getZenithOffsetDriver())) {
  219.             if (driver.isSelected()) {
  220.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {

  221.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  222.                     parameterDerivative += rangeErrorParameterDerivative(primaryStation, driver, state);
  223.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  224.                 }
  225.             }
  226.         }

  227.         // Update derivatives with respect to secondary station position
  228.         for (final ParameterDriver driver : Arrays.asList(secondaryStation.getEastOffsetDriver(),
  229.                                                           secondaryStation.getNorthOffsetDriver(),
  230.                                                           secondaryStation.getZenithOffsetDriver())) {
  231.             if (driver.isSelected()) {
  232.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {

  233.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  234.                     parameterDerivative += rangeErrorParameterDerivative(secondaryStation, driver, state);
  235.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  236.                 }
  237.             }
  238.         }

  239.         // Update estimated value taking into account the ionospheric delay.
  240.         // The ionospheric delay is directly added to the TurnAroundRange.
  241.         final double[] newValue = estimated.getEstimatedValue();
  242.         newValue[0] = newValue[0] + primaryGDelay.getReal() + secondaryGDelay.getReal();
  243.         estimated.modifyEstimatedValue(this, newValue);
  244.     }

  245. }