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

  51.     /** Ionospheric delay model. */
  52.     private final IonosphericModel ionoModel;

  53.     /** Frequency [Hz]. */
  54.     private final double frequency;

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

  65.     /** Compute the measurement error due to ionosphere.
  66.      * @param station station
  67.      * @param state spacecraft state
  68.      * @return the measurement error due to ionosphere
  69.      */
  70.     private double rangeErrorIonosphericModel(final GroundStation station,
  71.                                               final SpacecraftState state) {
  72.         // Base frame associated with the station
  73.         final TopocentricFrame baseFrame = station.getBaseFrame();
  74.         // Delay in meters
  75.         final double delay = ionoModel.pathDelay(state, baseFrame, frequency, ionoModel.getParameters(state.getDate()));
  76.         return delay;
  77.     }

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

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


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

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

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

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

  126.     }

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

  139.         for (int i = 0; i < dim; i++) {
  140.             rangeError[i] = derivatives[freeStateParameters + i];
  141.         }

  142.         return rangeError;
  143.     }

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

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

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

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

  162.     }

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

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

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

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

  199.         }

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

  212.         }

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

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

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

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

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

  244. }