TurnAroundRangeIonosphericDelayModifier.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.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.     /** Compute the measurement error due to ionosphere.
  69.      * @param station station
  70.      * @param state spacecraft state
  71.      * @return the measurement error due to ionosphere
  72.      */
  73.     private double rangeErrorIonosphericModel(final GroundStation station,
  74.                                               final SpacecraftState state) {
  75.         // Base frame associated with the station
  76.         final TopocentricFrame baseFrame = station.getBaseFrame();
  77.         // Delay in meters
  78.         return ionoModel.pathDelay(state, baseFrame, frequency, ionoModel.getParameters(state.getDate()));
  79.     }

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

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


  107.     /** Compute the derivative of the delay term wrt parameters.
  108.      *
  109.      * @param station ground station
  110.      * @param driver driver for the station offset parameter
  111.      * @param state spacecraft state
  112.      * @return derivative of the delay wrt station offset parameter
  113.      */
  114.     private double rangeErrorParameterDerivative(final GroundStation station,
  115.                                                  final ParameterDriver driver,
  116.                                                  final SpacecraftState state) {

  117.         final ParameterFunction rangeError = new ParameterFunction() {
  118.             /** {@inheritDoc} */
  119.             @Override
  120.             public double value(final ParameterDriver parameterDriver, final AbsoluteDate date) {
  121.                 return rangeErrorIonosphericModel(station, state);
  122.             }
  123.         };

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

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

  127.     }

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

  140.     /** {@inheritDoc} */
  141.     @Override
  142.     public List<ParameterDriver> getParametersDrivers() {
  143.         return ionoModel.getParametersDrivers();
  144.     }

  145.     @Override
  146.     public void modifyWithoutDerivatives(final EstimatedMeasurementBase<TurnAroundRange> estimated) {

  147.         final TurnAroundRange measurement      = estimated.getObservedMeasurement();
  148.         final GroundStation   primaryStation   = measurement.getPrimaryStation();
  149.         final GroundStation   secondaryStation = measurement.getSecondaryStation();
  150.         final SpacecraftState state            = estimated.getStates()[0];

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

  158.     }

  159.     @Override
  160.     public void modify(final EstimatedMeasurement<TurnAroundRange> estimated) {
  161.         final TurnAroundRange measurement      = estimated.getObservedMeasurement();
  162.         final GroundStation   primaryStation   = measurement.getPrimaryStation();
  163.         final GroundStation   secondaryStation = measurement.getSecondaryStation();
  164.         final SpacecraftState state            = estimated.getStates()[0];

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

  174.         final double[][] primaryDjac      = rangeErrorJacobianState(primaryDerivatives);
  175.         final double[][] secondaryDjac    = rangeErrorJacobianState(secondaryDerivatives);
  176.         final double[][] stateDerivatives = estimated.getStateDerivatives(0);
  177.         for (int irow = 0; irow < stateDerivatives.length; ++irow) {
  178.             for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
  179.                 stateDerivatives[irow][jcol] += primaryDjac[irow][jcol] + secondaryDjac[irow][jcol];
  180.             }
  181.         }
  182.         estimated.setStateDerivatives(0, stateDerivatives);

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

  195.         }

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

  208.         }

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

  216.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  217.                     parameterDerivative += rangeErrorParameterDerivative(primaryStation, driver, state);
  218.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  219.                 }
  220.             }
  221.         }

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

  228.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  229.                     parameterDerivative += rangeErrorParameterDerivative(secondaryStation, driver, state);
  230.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  231.                 }
  232.             }
  233.         }

  234.         // Update estimated value taking into account the ionospheric delay.
  235.         // The ionospheric delay is directly added to the TurnAroundRange.
  236.         final double[] newValue = estimated.getEstimatedValue();
  237.         newValue[0] = newValue[0] + primaryGDelay.getReal() + secondaryGDelay.getReal();
  238.         estimated.modifyEstimatedValue(this, newValue);
  239.     }

  240. }