PhaseTroposphericDelayModifier.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.Field;
  22. import org.hipparchus.analysis.differentiation.Gradient;
  23. import org.orekit.attitudes.FrameAlignedProvider;
  24. import org.orekit.estimation.measurements.EstimatedMeasurement;
  25. import org.orekit.estimation.measurements.EstimatedMeasurementBase;
  26. import org.orekit.estimation.measurements.EstimationModifier;
  27. import org.orekit.estimation.measurements.GroundStation;
  28. import org.orekit.estimation.measurements.gnss.Phase;
  29. import org.orekit.models.earth.troposphere.TroposphericModel;
  30. import org.orekit.propagation.FieldSpacecraftState;
  31. import org.orekit.propagation.SpacecraftState;
  32. import org.orekit.utils.Differentiation;
  33. import org.orekit.utils.FieldTrackingCoordinates;
  34. import org.orekit.utils.ParameterDriver;
  35. import org.orekit.utils.ParameterFunction;
  36. import org.orekit.utils.TimeSpanMap.Span;
  37. import org.orekit.utils.TrackingCoordinates;

  38. /**
  39.  * Class modifying theoretical phase measurement with tropospheric delay.
  40.  * The effect of tropospheric correction on the phase is directly computed
  41.  * through the computation of the tropospheric delay.
  42.  * @author David Soulard
  43.  * @author Bryan Cazabonne
  44.  * @since 10.2
  45.  */
  46. public class PhaseTroposphericDelayModifier implements EstimationModifier<Phase> {

  47.     /** Tropospheric delay model. */
  48.     private final TroposphericModel tropoModel;

  49.     /** Constructor.
  50.      *
  51.      * @param model  Tropospheric delay model appropriate for the current range measurement method.
  52.      * @deprecated as of 12.1, replaced by {@link #PhaseTroposphericDelayModifier(TroposphericModel)}
  53.      */
  54.     @Deprecated
  55.     public PhaseTroposphericDelayModifier(final org.orekit.models.earth.troposphere.DiscreteTroposphericModel model) {
  56.         this(new org.orekit.models.earth.troposphere.TroposphericModelAdapter(model));
  57.     }

  58.     /** Constructor.
  59.      *
  60.      * @param model  Tropospheric delay model appropriate for the current range measurement method.
  61.      * @since 12.1
  62.      */
  63.     public PhaseTroposphericDelayModifier(final TroposphericModel model) {
  64.         tropoModel = model;
  65.     }

  66.     /** Compute the measurement error due to Troposphere.
  67.      * @param station station
  68.      * @param state spacecraft state
  69.      * @param wavelength wavelength of the signal
  70.      * @return the measurement error due to Troposphere
  71.      */
  72.     private double phaseErrorTroposphericModel(final GroundStation station, final SpacecraftState state, final double wavelength) {

  73.         // tracking
  74.         final TrackingCoordinates trackingCoordinates =
  75.                         station.getBaseFrame().getTrackingCoordinates(state.getPosition(), state.getFrame(), state.getDate());

  76.         // only consider measures above the horizon
  77.         if (trackingCoordinates.getElevation() > 0) {
  78.             // delay in meters
  79.             final double delay = tropoModel.pathDelay(trackingCoordinates,
  80.                                                       station.getOffsetGeodeticPoint(state.getDate()),
  81.                                                       station.getPressureTemperatureHumidity(state.getDate()),
  82.                                                       tropoModel.getParameters(state.getDate()), state.getDate()).
  83.                                  getDelay();

  84.             return delay / wavelength;
  85.         }

  86.         return 0;
  87.     }

  88.     /** Compute the measurement error due to Troposphere.
  89.      * @param <T> type of the element
  90.      * @param station station
  91.      * @param state spacecraft state
  92.      * @param parameters tropospheric model parameters
  93.      * @param wavelength of the measurements
  94.      * @return the measurement error due to Troposphere
  95.      */
  96.     private <T extends CalculusFieldElement<T>> T phaseErrorTroposphericModel(final GroundStation station,
  97.                                                                           final FieldSpacecraftState<T> state,
  98.                                                                           final T[] parameters, final double wavelength) {

  99.         // Field
  100.         final Field<T> field = state.getDate().getField();
  101.         final T zero         = field.getZero();

  102.         // tracking
  103.         final FieldTrackingCoordinates<T> trackingCoordinates =
  104.                         station.getBaseFrame().getTrackingCoordinates(state.getPosition(), state.getFrame(), state.getDate());


  105.         // only consider measures above the horizon
  106.         if (trackingCoordinates.getElevation().getReal() > 0) {
  107.             // delay in meters
  108.             final T delay = tropoModel.pathDelay(trackingCoordinates,
  109.                                                  station.getOffsetGeodeticPoint(state.getDate()),
  110.                                                  station.getPressureTemperatureHumidity(state.getDate()),
  111.                                                  parameters, state.getDate()).
  112.                             getDelay();

  113.             return delay.divide(wavelength);
  114.         }

  115.         return zero;
  116.     }

  117.     /** Compute the Jacobian of the delay term wrt state using
  118.     * automatic differentiation.
  119.     *
  120.     * @param derivatives tropospheric delay derivatives
  121.     *
  122.     * @return Jacobian of the delay wrt state
  123.     */
  124.     private double[][] phaseErrorJacobianState(final double[] derivatives) {
  125.         final double[][] finiteDifferencesJacobian = new double[1][6];
  126.         System.arraycopy(derivatives, 0, finiteDifferencesJacobian[0], 0, 6);
  127.         return finiteDifferencesJacobian;
  128.     }

  129.     /** Compute the derivative of the delay term wrt parameters.
  130.      *
  131.      * @param station ground station
  132.      * @param driver driver for the station offset parameter
  133.      * @param state spacecraft state
  134.      * @param wavelength wavelength of the signal
  135.      * @return derivative of the delay wrt station offset parameter
  136.      */
  137.     private double phaseErrorParameterDerivative(final GroundStation station,
  138.                                                  final ParameterDriver driver,
  139.                                                  final SpacecraftState state,
  140.                                                  final double wavelength) {
  141.         final ParameterFunction rangeError = (parameterDriver, date) -> phaseErrorTroposphericModel(station, state, wavelength);
  142.         final ParameterFunction phaseErrorDerivative =
  143.                         Differentiation.differentiate(rangeError, 3, 10.0 * driver.getScale());
  144.         return phaseErrorDerivative.value(driver, state.getDate());

  145.     }

  146.     /** Compute the derivative of the delay term wrt parameters using
  147.     * automatic differentiation.
  148.     *
  149.     * @param derivatives tropospheric delay derivatives
  150.     * @param freeStateParameters dimension of the state.
  151.     * @return derivative of the delay wrt tropospheric model parameters
  152.     */
  153.     private double[] phaseErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
  154.         // 0 ... freeStateParameters - 1   -> derivatives of the delay wrt state
  155.         // freeStateParameters ... n       -> derivatives of the delay wrt tropospheric parameters
  156.         return Arrays.copyOfRange(derivatives, freeStateParameters, derivatives.length);
  157.     }

  158.     /** {@inheritDoc} */
  159.     @Override
  160.     public List<ParameterDriver> getParametersDrivers() {
  161.         return tropoModel.getParametersDrivers();
  162.     }

  163.     /** {@inheritDoc} */
  164.     @Override
  165.     public void modifyWithoutDerivatives(final EstimatedMeasurementBase<Phase> estimated) {

  166.         final Phase           measurement = estimated.getObservedMeasurement();
  167.         final GroundStation   station     = measurement.getStation();
  168.         final SpacecraftState state       = estimated.getStates()[0];

  169.         // Update estimated value taking into account the tropospheric delay.
  170.         // The tropospheric delay is directly added to the phase.
  171.         final double[] newValue = estimated.getEstimatedValue();
  172.         final double delay = phaseErrorTroposphericModel(station, state, measurement.getWavelength());
  173.         newValue[0] = newValue[0] + delay;
  174.         estimated.modifyEstimatedValue(this, newValue);

  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public void modify(final EstimatedMeasurement<Phase> estimated) {
  179.         final Phase           measurement = estimated.getObservedMeasurement();
  180.         final GroundStation   station     = measurement.getStation();
  181.         final SpacecraftState state       = estimated.getStates()[0];

  182.         // update estimated derivatives with Jacobian of the measure wrt state
  183.         final ModifierGradientConverter converter = new ModifierGradientConverter(state, 6, new FrameAlignedProvider(state.getFrame()));
  184.         final FieldSpacecraftState<Gradient> gState = converter.getState(tropoModel);
  185.         final Gradient[] gParameters = converter.getParametersAtStateDate(gState, tropoModel);
  186.         final Gradient gDelay = phaseErrorTroposphericModel(station, gState, gParameters, measurement.getWavelength());
  187.         final double[] derivatives = gDelay.getGradient();

  188.         // Update state derivatives
  189.         final double[][] djac = phaseErrorJacobianState(derivatives);
  190.         final double[][] stateDerivatives = estimated.getStateDerivatives(0);
  191.         for (int irow = 0; irow < stateDerivatives.length; ++irow) {
  192.             for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
  193.                 stateDerivatives[irow][jcol] += djac[irow][jcol];
  194.             }
  195.         }
  196.         estimated.setStateDerivatives(0, stateDerivatives);


  197.         // Update tropospheric parameter derivatives
  198.         int index = 0;
  199.         for (final ParameterDriver driver : getParametersDrivers()) {
  200.             if (driver.isSelected()) {
  201.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {

  202.                     // update estimated derivatives with derivative of the modification wrt tropospheric parameters
  203.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  204.                     final double[] dDelaydP    = phaseErrorParameterDerivative(derivatives, converter.getFreeStateParameters());
  205.                     parameterDerivative += dDelaydP[index];
  206.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  207.                     index = index + 1;
  208.                 }
  209.             }
  210.         }

  211.         // Update station parameter derivatives
  212.         for (final ParameterDriver driver : Arrays.asList(station.getClockOffsetDriver(),
  213.                                                           station.getEastOffsetDriver(),
  214.                                                           station.getNorthOffsetDriver(),
  215.                                                           station.getZenithOffsetDriver())) {
  216.             if (driver.isSelected()) {
  217.                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  218.                     // update estimated derivatives with derivative of the modification wrt station parameters
  219.                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
  220.                     parameterDerivative += phaseErrorParameterDerivative(station, driver, state, measurement.getWavelength());
  221.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
  222.                 }
  223.             }
  224.         }

  225.         // Update estimated value taking into account the tropospheric delay.
  226.         modifyWithoutDerivatives(estimated);

  227.     }

  228. }