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.         for (final ParameterDriver parameterDriver : supportedParameters) {
  76.             this.supportedParameters.add(parameterDriver);
  77.         }

  78.         this.date       = date;
  79.         this.observed   = new double[] {
  80.             observed
  81.         };
  82.         this.sigma      = new double[] {
  83.             sigma
  84.         };
  85.         this.baseWeight = new double[] {
  86.             baseWeight
  87.         };

  88.         this.satellites = satellites;

  89.         this.modifiers = new ArrayList<EstimationModifier<T>>();
  90.         setEnabled(true);

  91.     }

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

  107.         this.date       = date;
  108.         this.observed   = observed.clone();
  109.         this.sigma      = sigma.clone();
  110.         this.baseWeight = baseWeight.clone();

  111.         this.satellites = satellites;

  112.         this.modifiers = new ArrayList<EstimationModifier<T>>();
  113.         setEnabled(true);

  114.     }

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

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public List<ParameterDriver> getParametersDrivers() {
  125.         return Collections.unmodifiableList(supportedParameters);
  126.     }

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     public void setEnabled(final boolean enabled) {
  130.         this.enabled = enabled;
  131.     }

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     public boolean isEnabled() {
  135.         return enabled;
  136.     }

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     public int getDimension() {
  140.         return observed.length;
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public double[] getTheoreticalStandardDeviation() {
  145.         return sigma.clone();
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public double[] getBaseWeight() {
  150.         return baseWeight.clone();
  151.     }

  152.     /** {@inheritDoc} */
  153.     @Override
  154.     public List<ObservableSatellite> getSatellites() {
  155.         return satellites;
  156.     }

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

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

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

  173.         // apply the modifiers
  174.         for (final EstimationModifier<T> modifier : modifiers) {
  175.             modifier.modify(estimation);
  176.         }

  177.         return estimation;

  178.     }

  179.     /** {@inheritDoc} */
  180.     @Override
  181.     public AbsoluteDate getDate() {
  182.         return date;
  183.     }

  184.     /** {@inheritDoc} */
  185.     @Override
  186.     public double[] getObservedValue() {
  187.         return observed.clone();
  188.     }

  189.     /** {@inheritDoc} */
  190.     @Override
  191.     public void addModifier(final EstimationModifier<T> modifier) {

  192.         // combine the measurement parameters and the modifier parameters
  193.         supportedParameters.addAll(modifier.getParametersDrivers());

  194.         modifiers.add(modifier);

  195.     }

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

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

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

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

  226.         return delay;

  227.     }

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

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

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

  254.         return delay;

  255.     }

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

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

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

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

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

  296.     }

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

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

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

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

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

  336.     }

  337. }