AbstractMeasurement.java

  1. /* Copyright 2002-2020 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;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.hipparchus.RealFieldElement;
  22. import org.hipparchus.analysis.differentiation.DSFactory;
  23. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  24. import org.hipparchus.analysis.differentiation.Gradient;
  25. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.util.FastMath;
  28. import org.orekit.propagation.SpacecraftState;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.time.FieldAbsoluteDate;
  31. import org.orekit.utils.Constants;
  32. import org.orekit.utils.ParameterDriver;
  33. import org.orekit.utils.TimeStampedFieldPVCoordinates;
  34. import org.orekit.utils.TimeStampedPVCoordinates;

  35. /** Abstract class handling measurements boilerplate.
  36.  * @param <T> the type of the measurement
  37.  * @author Luc Maisonobe
  38.  * @since 8.0
  39.  */
  40. public abstract class AbstractMeasurement<T extends ObservedMeasurement<T>>
  41.     implements ObservedMeasurement<T> {

  42.     /** List of the supported parameters. */
  43.     private final List<ParameterDriver> supportedParameters;

  44.     /** Satellites related to this measurement.
  45.      * @since 9.3
  46.      */
  47.     private final List<ObservableSatellite> satellites;

  48.     /** Date of the measurement. */
  49.     private final AbsoluteDate date;

  50.     /** Observed value. */
  51.     private final double[] observed;

  52.     /** Theoretical standard deviation. */
  53.     private final double[] sigma;

  54.     /** Base weight. */
  55.     private final double[] baseWeight;

  56.     /** Modifiers that apply to the measurement.*/
  57.     private final List<EstimationModifier<T>> modifiers;

  58.     /** Enabling status. */
  59.     private boolean enabled;

  60.     /** Simple constructor for mono-dimensional measurements.
  61.      * <p>
  62.      * At construction, a measurement is enabled.
  63.      * </p>
  64.      * @param date date of the measurement
  65.      * @param observed observed value
  66.      * @param sigma theoretical standard deviation
  67.      * @param baseWeight base weight
  68.      * @param satellites satellites related to this measurement
  69.      * @since 9.3
  70.      */
  71.     protected AbstractMeasurement(final AbsoluteDate date, final double observed,
  72.                                   final double sigma, final double baseWeight,
  73.                                   final List<ObservableSatellite> satellites) {

  74.         this.supportedParameters = new ArrayList<ParameterDriver>();

  75.         this.date       = date;
  76.         this.observed   = new double[] {
  77.             observed
  78.         };
  79.         this.sigma      = new double[] {
  80.             sigma
  81.         };
  82.         this.baseWeight = new double[] {
  83.             baseWeight
  84.         };

  85.         this.satellites = satellites;

  86.         this.modifiers = new ArrayList<EstimationModifier<T>>();
  87.         setEnabled(true);

  88.     }

  89.     /** Simple constructor, for multi-dimensional measurements.
  90.      * <p>
  91.      * At construction, a measurement is enabled.
  92.      * </p>
  93.      * @param date date of the measurement
  94.      * @param observed observed value
  95.      * @param sigma theoretical standard deviation
  96.      * @param baseWeight base weight
  97.      * @param satellites satellites related to this measurement
  98.      * @since 9.3
  99.      */
  100.     protected AbstractMeasurement(final AbsoluteDate date, final double[] observed,
  101.                                   final double[] sigma, final double[] baseWeight,
  102.                                   final List<ObservableSatellite> satellites) {
  103.         this.supportedParameters = new ArrayList<ParameterDriver>();

  104.         this.date       = date;
  105.         this.observed   = observed.clone();
  106.         this.sigma      = sigma.clone();
  107.         this.baseWeight = baseWeight.clone();

  108.         this.satellites = satellites;

  109.         this.modifiers = new ArrayList<EstimationModifier<T>>();
  110.         setEnabled(true);

  111.     }

  112.     /** Add a parameter driver.
  113.      * @param driver parameter driver to add
  114.      * @since 9.3
  115.      */
  116.     protected void addParameterDriver(final ParameterDriver driver) {
  117.         supportedParameters.add(driver);
  118.     }

  119.     /** {@inheritDoc} */
  120.     @Override
  121.     public List<ParameterDriver> getParametersDrivers() {
  122.         return Collections.unmodifiableList(supportedParameters);
  123.     }

  124.     /** {@inheritDoc} */
  125.     @Override
  126.     public void setEnabled(final boolean enabled) {
  127.         this.enabled = enabled;
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public boolean isEnabled() {
  132.         return enabled;
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     public int getDimension() {
  137.         return observed.length;
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public double[] getTheoreticalStandardDeviation() {
  142.         return sigma.clone();
  143.     }

  144.     /** {@inheritDoc} */
  145.     @Override
  146.     public double[] getBaseWeight() {
  147.         return baseWeight.clone();
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     public List<ObservableSatellite> getSatellites() {
  152.         return satellites;
  153.     }

  154.     /** Estimate the theoretical value.
  155.      * <p>
  156.      * The theoretical value does not have <em>any</em> modifiers applied.
  157.      * </p>
  158.      * @param iteration iteration number
  159.      * @param evaluation evaluation number
  160.      * @param states orbital states at measurement date
  161.      * @return theoretical value
  162.      * @see #estimate(int, int, SpacecraftState[])
  163.      */
  164.     protected abstract EstimatedMeasurement<T> theoreticalEvaluation(int iteration, int evaluation, SpacecraftState[] states);

  165.     /** {@inheritDoc} */
  166.     @Override
  167.     public EstimatedMeasurement<T> estimate(final int iteration, final int evaluation, final SpacecraftState[] states) {

  168.         // compute the theoretical value
  169.         final EstimatedMeasurement<T> estimation = theoreticalEvaluation(iteration, evaluation, states);

  170.         // apply the modifiers
  171.         for (final EstimationModifier<T> modifier : modifiers) {
  172.             modifier.modify(estimation);
  173.         }

  174.         return estimation;

  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public AbsoluteDate getDate() {
  179.         return date;
  180.     }

  181.     /** {@inheritDoc} */
  182.     @Override
  183.     public double[] getObservedValue() {
  184.         return observed.clone();
  185.     }

  186.     /** {@inheritDoc} */
  187.     @Override
  188.     public void addModifier(final EstimationModifier<T> modifier) {

  189.         // combine the measurement parameters and the modifier parameters
  190.         supportedParameters.addAll(modifier.getParametersDrivers());

  191.         modifiers.add(modifier);

  192.     }

  193.     /** {@inheritDoc} */
  194.     @Override
  195.     public List<EstimationModifier<T>> getModifiers() {
  196.         return Collections.unmodifiableList(modifiers);
  197.     }

  198.     /** Compute propagation delay on a link leg (typically downlink or uplink).
  199.      * @param adjustableEmitterPV position/velocity of emitter that may be adjusted
  200.      * @param receiverPosition fixed position of receiver at {@code signalArrivalDate},
  201.      * in the same frame as {@code adjustableEmitterPV}
  202.      * @param signalArrivalDate date at which the signal arrives to receiver
  203.      * @return <em>positive</em> delay between signal emission and signal reception dates
  204.      */
  205.     public static double signalTimeOfFlight(final TimeStampedPVCoordinates adjustableEmitterPV,
  206.                                             final Vector3D receiverPosition,
  207.                                             final AbsoluteDate signalArrivalDate) {

  208.         // initialize emission date search loop assuming the state is already correct
  209.         // this will be true for all but the first orbit determination iteration,
  210.         // and even for the first iteration the loop will converge very fast
  211.         final double offset = signalArrivalDate.durationFrom(adjustableEmitterPV.getDate());
  212.         double delay = offset;

  213.         // search signal transit date, computing the signal travel in inertial frame
  214.         final double cReciprocal = 1.0 / Constants.SPEED_OF_LIGHT;
  215.         double delta;
  216.         int count = 0;
  217.         do {
  218.             final double previous   = delay;
  219.             final Vector3D transitP = adjustableEmitterPV.shiftedBy(offset - delay).getPosition();
  220.             delay                   = receiverPosition.distance(transitP) * cReciprocal;
  221.             delta                   = FastMath.abs(delay - previous);
  222.         } while (count++ < 10 && delta >= 2 * FastMath.ulp(delay));

  223.         return delay;

  224.     }

  225.     /** Compute propagation delay on a link leg (typically downlink or uplink).
  226.      * @param adjustableEmitterPV position/velocity of emitter that may be adjusted
  227.      * @param receiverPosition fixed position of receiver at {@code signalArrivalDate},
  228.      * in the same frame as {@code adjustableEmitterPV}
  229.      * @param signalArrivalDate date at which the signal arrives to receiver
  230.      * @return <em>positive</em> delay between signal emission and signal reception dates
  231.      * @param <T> the type of the components
  232.      */
  233.     public static <T extends RealFieldElement<T>> T signalTimeOfFlight(final TimeStampedFieldPVCoordinates<T> adjustableEmitterPV,
  234.                                                                        final FieldVector3D<T> receiverPosition,
  235.                                                                        final FieldAbsoluteDate<T> signalArrivalDate) {

  236.         // Initialize emission date search loop assuming the emitter PV is almost correct
  237.         // this will be true for all but the first orbit determination iteration,
  238.         // and even for the first iteration the loop will converge extremely fast
  239.         final T offset = signalArrivalDate.durationFrom(adjustableEmitterPV.getDate());
  240.         T delay = offset;

  241.         // search signal transit date, computing the signal travel in the frame shared by emitter and receiver
  242.         final double cReciprocal = 1.0 / Constants.SPEED_OF_LIGHT;
  243.         double delta;
  244.         int count = 0;
  245.         do {
  246.             final double previous           = delay.getReal();
  247.             final FieldVector3D<T> transitP = adjustableEmitterPV.shiftedBy(delay.negate().add(offset)).getPosition();
  248.             delay                           = receiverPosition.distance(transitP).multiply(cReciprocal);
  249.             delta                           = FastMath.abs(delay.getReal() - previous);
  250.         } while (count++ < 10 && delta >= 2 * FastMath.ulp(delay.getReal()));

  251.         return delay;

  252.     }

  253.     /** Get Cartesian coordinates as derivatives.
  254.      * <p>
  255.      * The position will correspond to variables {@code firstDerivative},
  256.      * {@code firstDerivative + 1} and {@code firstDerivative + 2}.
  257.      * The velocity will correspond to variables {@code firstDerivative + 3},
  258.      * {@code firstDerivative + 4} and {@code firstDerivative + 5}.
  259.      * The acceleration will correspond to constants.
  260.      * </p>
  261.      * @param state state of the satellite considered
  262.      * @param firstDerivative index of the first derivative
  263.      * @param factory factory for building the derivatives
  264.      * @return Cartesian coordinates as derivatives
  265.      * @deprecated as of 10.2, replaced by {@link #getCoordinates(SpacecraftState, int, int)}
  266.      */
  267.     @Deprecated
  268.     public static TimeStampedFieldPVCoordinates<DerivativeStructure> getCoordinates(final SpacecraftState state,
  269.                                                                                     final int firstDerivative,
  270.                                                                                     final DSFactory factory) {

  271.         // Position of the satellite expressed as a derivative structure
  272.         // The components of the position are the 3 first derivative parameters
  273.         final Vector3D p = state.getPVCoordinates().getPosition();
  274.         final FieldVector3D<DerivativeStructure> pDS =
  275.                         new FieldVector3D<>(factory.variable(firstDerivative + 0, p.getX()),
  276.                                             factory.variable(firstDerivative + 1, p.getY()),
  277.                                             factory.variable(firstDerivative + 2, p.getZ()));

  278.         // Velocity of the satellite expressed as a derivative structure
  279.         // The components of the velocity are the 3 second derivative parameters
  280.         final Vector3D v = state.getPVCoordinates().getVelocity();
  281.         final FieldVector3D<DerivativeStructure> vDS =
  282.                         new FieldVector3D<>(factory.variable(firstDerivative + 3, v.getX()),
  283.                                             factory.variable(firstDerivative + 4, v.getY()),
  284.                                             factory.variable(firstDerivative + 5, v.getZ()));

  285.         // Acceleration of the satellite
  286.         // The components of the acceleration are not derivative parameters
  287.         final Vector3D a = state.getPVCoordinates().getAcceleration();
  288.         final FieldVector3D<DerivativeStructure> aDS =
  289.                         new FieldVector3D<>(factory.constant(a.getX()),
  290.                                             factory.constant(a.getY()),
  291.                                             factory.constant(a.getZ()));

  292.         return new TimeStampedFieldPVCoordinates<>(state.getDate(), pDS, vDS, aDS);

  293.     }

  294.     /** Get Cartesian coordinates as derivatives.
  295.      * <p>
  296.      * The position will correspond to variables {@code firstDerivative},
  297.      * {@code firstDerivative + 1} and {@code firstDerivative + 2}.
  298.      * The velocity will correspond to variables {@code firstDerivative + 3},
  299.      * {@code firstDerivative + 4} and {@code firstDerivative + 5}.
  300.      * The acceleration will correspond to constants.
  301.      * </p>
  302.      * @param state state of the satellite considered
  303.      * @param firstDerivative index of the first derivative
  304.      * @param freeParameters total number of free parameters in the gradient
  305.      * @return Cartesian coordinates as derivatives
  306.      * @since 10.2
  307.      */
  308.     public static TimeStampedFieldPVCoordinates<Gradient> getCoordinates(final SpacecraftState state,
  309.                                                                          final int firstDerivative,
  310.                                                                          final int freeParameters) {

  311.         // Position of the satellite expressed as a gradient
  312.         // The components of the position are the 3 first derivative parameters
  313.         final Vector3D p = state.getPVCoordinates().getPosition();
  314.         final FieldVector3D<Gradient> pDS =
  315.                         new FieldVector3D<>(Gradient.variable(freeParameters, firstDerivative + 0, p.getX()),
  316.                                             Gradient.variable(freeParameters, firstDerivative + 1, p.getY()),
  317.                                             Gradient.variable(freeParameters, firstDerivative + 2, p.getZ()));

  318.         // Velocity of the satellite expressed as a gradient
  319.         // The components of the velocity are the 3 second derivative parameters
  320.         final Vector3D v = state.getPVCoordinates().getVelocity();
  321.         final FieldVector3D<Gradient> vDS =
  322.                         new FieldVector3D<>(Gradient.variable(freeParameters, firstDerivative + 3, v.getX()),
  323.                                             Gradient.variable(freeParameters, firstDerivative + 4, v.getY()),
  324.                                             Gradient.variable(freeParameters, firstDerivative + 5, v.getZ()));

  325.         // Acceleration of the satellite
  326.         // The components of the acceleration are not derivative parameters
  327.         final Vector3D a = state.getPVCoordinates().getAcceleration();
  328.         final FieldVector3D<Gradient> aDS =
  329.                         new FieldVector3D<>(Gradient.constant(freeParameters, a.getX()),
  330.                                             Gradient.constant(freeParameters, a.getY()),
  331.                                             Gradient.constant(freeParameters, a.getZ()));

  332.         return new TimeStampedFieldPVCoordinates<>(state.getDate(), pDS, vDS, aDS);

  333.     }

  334. }