PhaseIonosphericDelayModifier.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.  * The ASF 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.gnss.Phase;
  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.utils.Constants;
  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. /**
  38.  * Class modifying theoretical phase measurement with ionospheric delay.
  39.  * The effect of ionospheric correction on the phase is directly computed
  40.  * through the computation of the ionospheric delay.
  41.  * @author David Soulard
  42.  * @author Bryan Cazabonne
  43.  * @since 10.2
  44.  */
  45. public class PhaseIonosphericDelayModifier implements EstimationModifier<Phase> {

  46.     /** Ionospheric delay model. */
  47.     private final IonosphericModel ionoModel;

  48.     /** Frequency [Hz]. */
  49.     private final double frequency;

  50.     /** Constructor.
  51.      *
  52.      * @param model  Ionospheric delay model appropriate for the current range measurement method.
  53.      * @param freq frequency of the signal in Hz
  54.      */
  55.     public PhaseIonosphericDelayModifier(final IonosphericModel model,
  56.                                          final double freq) {
  57.         ionoModel = model;
  58.         frequency = freq;
  59.     }

  60.     /** Compute the measurement error due to ionosphere.
  61.      * @param station station
  62.      * @param state spacecraft state
  63.      * @return the measurement error due to ionosphere
  64.      */
  65.     private double phaseErrorIonosphericModel(final GroundStation station,
  66.                                               final SpacecraftState state) {

  67.         // Base frame associated with the station
  68.         final TopocentricFrame baseFrame = station.getBaseFrame();
  69.         final double wavelength  = Constants.SPEED_OF_LIGHT / frequency;
  70.         // delay in meters
  71.         final double delay = ionoModel.pathDelay(state, baseFrame, frequency, ionoModel.getParameters(state.getDate()));
  72.         return delay / wavelength;
  73.     }

  74.     /** Compute the measurement error due to ionosphere.
  75.      * @param <T> type of the element
  76.      * @param station station
  77.      * @param state spacecraft state
  78.      * @param parameters ionospheric model parameters at state date
  79.      * @return the measurement error due to ionosphere
  80.      */
  81.     private <T extends CalculusFieldElement<T>> T phaseErrorIonosphericModel(final GroundStation station,
  82.                                                                              final FieldSpacecraftState<T> state,
  83.                                                                              final T[] parameters) {

  84.         // Base frame associated with the station
  85.         final TopocentricFrame baseFrame = station.getBaseFrame();
  86.         final double wavelength  = Constants.SPEED_OF_LIGHT / frequency;
  87.         // delay in meters
  88.         final T delay = ionoModel.pathDelay(state, baseFrame, frequency, parameters);
  89.         return delay.divide(wavelength);
  90.     }

  91.     /** Compute the Jacobian of the delay term wrt state using
  92.     * automatic differentiation.
  93.     *
  94.     * @param derivatives ionospheric delay derivatives
  95.     * @param freeStateParameters dimension of the state.
  96.     *
  97.     * @return Jacobian of the delay wrt state
  98.     */
  99.     private double[][] phaseErrorJacobianState(final double[] derivatives, final int freeStateParameters) {
  100.         final double[][] finiteDifferencesJacobian = new double[1][6];
  101.         for (int i = 0; i < freeStateParameters; i++) {
  102.             finiteDifferencesJacobian[0][i] = derivatives[i];
  103.         }
  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 phaseErrorParameterDerivative(final GroundStation station,
  114.                                                  final ParameterDriver driver,
  115.                                                  final SpacecraftState state) {
  116.         final ParameterFunction phaseError = (parameterDriver, date) -> phaseErrorIonosphericModel(station, state);
  117.         final ParameterFunction phaseErrorDerivative =
  118.                         Differentiation.differentiate(phaseError, 3, 10.0 * driver.getScale());
  119.         return phaseErrorDerivative.value(driver, state.getDate());

  120.     }

  121.     /** Compute the derivative of the delay term wrt parameters using
  122.     * automatic differentiation.
  123.     *
  124.     * @param derivatives ionospheric delay derivatives
  125.     * @param freeStateParameters dimension of the state.
  126.     * @return derivative of the delay wrt ionospheric model parameters
  127.     */
  128.     private double[] phaseErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
  129.         // 0 ... freeStateParameters - 1 -> derivatives of the delay wrt state
  130.         // freeStateParameters ... n     -> derivatives of the delay wrt ionospheric parameters
  131.         final int dim = derivatives.length - freeStateParameters;
  132.         final double[] phaseError = new double[dim];

  133.         for (int i = 0; i < dim; i++) {
  134.             phaseError[i] = derivatives[freeStateParameters + i];
  135.         }

  136.         return phaseError;
  137.     }

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

  143.     @Override
  144.     public void modifyWithoutDerivatives(final EstimatedMeasurementBase<Phase> estimated) {

  145.         final Phase           measurement = estimated.getObservedMeasurement();
  146.         final GroundStation   station     = measurement.getStation();
  147.         final SpacecraftState state       = estimated.getStates()[0];

  148.         // Update estimated value taking into account the ionospheric delay.
  149.         // The ionospheric delay is directly subtracted to the phase.
  150.         final double[] newValue = estimated.getEstimatedValue();
  151.         final double delay = phaseErrorIonosphericModel(station, state);
  152.         newValue[0] = newValue[0] - delay;
  153.         estimated.setEstimatedValue(newValue);

  154.     }

  155.     @Override
  156.     public void modify(final EstimatedMeasurement<Phase> estimated) {

  157.         final Phase           measurement = estimated.getObservedMeasurement();
  158.         final GroundStation   station     = measurement.getStation();
  159.         final SpacecraftState state       = estimated.getStates()[0];

  160.         // Compute ionospheric delay (the division by the wavelength is performed)
  161.         final ModifierGradientConverter converter =
  162.                         new ModifierGradientConverter(state, 6, new FrameAlignedProvider(state.getFrame()));
  163.         final FieldSpacecraftState<Gradient> gState = converter.getState(ionoModel);
  164.         final Gradient[] gParameters = converter.getParametersAtStateDate(gState, ionoModel);
  165.         final Gradient gDelay = phaseErrorIonosphericModel(station, gState, gParameters);
  166.         final double[] derivatives = gDelay.getGradient();

  167.         // Update state derivatives
  168.         final double[][] djac = phaseErrorJacobianState(derivatives, converter.getFreeStateParameters());
  169.         final double[][] stateDerivatives = estimated.getStateDerivatives(0);
  170.         for (int irow = 0; irow < stateDerivatives.length; ++irow) {
  171.             for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
  172.                 stateDerivatives[irow][jcol] -= djac[irow][jcol];
  173.             }
  174.         }
  175.         estimated.setStateDerivatives(0, stateDerivatives);

  176.         // Update ionospheric parameter derivatives
  177.         int index = 0;
  178.         for (final ParameterDriver driver : getParametersDrivers()) {
  179.             if (driver.isSelected()) {
  180.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  181.                     // update estimated derivatives with derivative of the modification wrt ionospheric parameters
  182.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  183.                     final double[] dDelaydP    = phaseErrorParameterDerivative(derivatives, converter.getFreeStateParameters());
  184.                     parameterDerivative -= dDelaydP[index];
  185.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  186.                     index = index + 1;
  187.                 }
  188.             }

  189.         }

  190.         // Update station parameter derivatives
  191.         for (final ParameterDriver driver : Arrays.asList(station.getClockOffsetDriver(),
  192.                                                           station.getEastOffsetDriver(),
  193.                                                           station.getNorthOffsetDriver(),
  194.                                                           station.getZenithOffsetDriver())) {
  195.             if (driver.isSelected()) {
  196.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  197.                     // update estimated derivatives with derivative of the modification wrt station parameters
  198.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  199.                     parameterDerivative -= phaseErrorParameterDerivative(station, driver, state);
  200.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  201.                 }
  202.             }
  203.         }

  204.         // Update estimated value taking into account the ionospheric delay.
  205.         modifyWithoutDerivatives(estimated);

  206.     }

  207. }