EstimatedMeasurement.java

  1. /* Copyright 2002-2025 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.IdentityHashMap;
  19. import java.util.Map;
  20. import java.util.stream.Stream;

  21. import org.orekit.errors.OrekitIllegalArgumentException;
  22. import org.orekit.errors.OrekitIllegalStateException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.propagation.SpacecraftState;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.utils.ParameterDriver;
  27. import org.orekit.utils.TimeSpanMap;
  28. import org.orekit.utils.TimeStampedPVCoordinates;
  29. import org.orekit.utils.TimeSpanMap.Span;

  30. /** Class holding an estimated theoretical value associated to an {@link ObservedMeasurement observed measurement}.
  31.  * @param <T> the type of the measurement
  32.  * @author Luc Maisonobe
  33.  * @since 8.0
  34.  */
  35. public class EstimatedMeasurement<T extends ObservedMeasurement<T>> extends EstimatedMeasurementBase<T> {

  36.     /** Partial derivatives with respect to states. */
  37.     private double[][][] stateDerivatives;

  38.     /** Partial derivatives with respect to parameters. */
  39.     private final Map<ParameterDriver, TimeSpanMap<double[]>> parametersDerivatives;

  40.     /** Simple constructor.
  41.      * @param observedMeasurement associated observed measurement
  42.      * @param iteration iteration number
  43.      * @param count evaluations counter
  44.      * @param states states of the spacecrafts
  45.      * @param participants coordinates of the participants in signal travel order
  46.      * in inertial frame
  47.      */
  48.     public EstimatedMeasurement(final T observedMeasurement,
  49.                                 final int iteration, final int count,
  50.                                 final SpacecraftState[] states,
  51.                                 final TimeStampedPVCoordinates[] participants) {
  52.         super(observedMeasurement, iteration, count, states, participants);
  53.         this.stateDerivatives      = new double[states.length][][];
  54.         this.parametersDerivatives = new IdentityHashMap<>();
  55.     }

  56.     /** Constructor from measurement base.
  57.      * @param estimatedMeasurementBase estimated measurement base
  58.      * @since 12.1.3
  59.      */
  60.     public EstimatedMeasurement(final EstimatedMeasurementBase<T> estimatedMeasurementBase) {
  61.         this(estimatedMeasurementBase.getObservedMeasurement(), estimatedMeasurementBase.getIteration(),
  62.             estimatedMeasurementBase.getCount(), estimatedMeasurementBase.getStates(),
  63.             estimatedMeasurementBase.getParticipants());
  64.         this.setEstimatedValue(estimatedMeasurementBase.getEstimatedValue());
  65.         this.setStatus(estimatedMeasurementBase.getStatus());
  66.     }

  67.     /** Get state size.
  68.      * <p>
  69.      * Warning, the {@link #setStateDerivatives(int, double[][])}
  70.      * method must have been called before this method is called.
  71.      * </p>
  72.      * @return state size
  73.      * @since 10.1
  74.      */
  75.     public int getStateSize() {
  76.         return stateDerivatives[0][0].length;
  77.     }

  78.     /** Get the partial derivatives of the {@link #getEstimatedValue()
  79.      * simulated measurement} with respect to state Cartesian coordinates.
  80.      * @param index index of the state, according to the {@code states}
  81.      * passed at construction
  82.      * @return partial derivatives of the simulated value (array of size
  83.      * {@link ObservedMeasurement#getDimension() dimension} x 6)
  84.      */
  85.     public double[][] getStateDerivatives(final int index) {
  86.         final double[][] sd = new double[getObservedMeasurement().getDimension()][];
  87.         for (int i = 0; i < getObservedMeasurement().getDimension(); ++i) {
  88.             sd[i] = stateDerivatives[index][i].clone();
  89.         }
  90.         return sd;
  91.     }

  92.     /** Set the partial derivatives of the {@link #getEstimatedValue()
  93.      * simulated measurement} with respect to state Cartesian coordinates.
  94.      * @param index index of the state, according to the {@code states}
  95.      * passed at construction
  96.      * @param derivatives partial derivatives with respect to state
  97.      */
  98.     public void setStateDerivatives(final int index, final double[]... derivatives) {
  99.         this.stateDerivatives[index] = new double[getObservedMeasurement().getDimension()][];
  100.         for (int i = 0; i < getObservedMeasurement().getDimension(); ++i) {
  101.             this.stateDerivatives[index][i] = derivatives[i].clone();
  102.         }
  103.     }

  104.     /** Get all the drivers with set derivatives.
  105.      * @return all the drivers with set derivatives
  106.      * @since 9.0
  107.      */
  108.     public Stream<ParameterDriver> getDerivativesDrivers() {
  109.         return parametersDerivatives.entrySet().stream().map(Map.Entry::getKey);
  110.     }

  111.     /** Get the partial derivatives of the {@link #getEstimatedValue()
  112.      * simulated measurement} with respect to a parameter.
  113.      * @param driver name of the span of the driver for the parameter for which
  114.      * the derivative wants to be known.
  115.      * @return partial derivatives of the simulated value
  116.      * @exception OrekitIllegalArgumentException if parameter is unknown or
  117.      * OrekitIllegalStateException if this function is used on a PDriver having several
  118.      * values driven, in this case the method
  119.      * {@link #getParameterDerivatives(ParameterDriver, AbsoluteDate)} must be called
  120.      */
  121.     public double[] getParameterDerivatives(final ParameterDriver driver)
  122.         throws OrekitIllegalArgumentException {
  123.         if (driver.getNbOfValues() == 1) {
  124.             final TimeSpanMap<double[]> p = parametersDerivatives.get(driver);
  125.             if (p == null) {
  126.                 final StringBuilder builder = new StringBuilder();
  127.                 for (final Map.Entry<ParameterDriver, TimeSpanMap<double[]>> entry : parametersDerivatives.entrySet()) {
  128.                     if (builder.length() > 0) {
  129.                         builder.append(",  ");
  130.                     }
  131.                     builder.append(entry.getKey());
  132.                 }
  133.                 throw new OrekitIllegalArgumentException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  134.                                                          driver,
  135.                                                          builder.length() > 0 ? builder.toString() : " <none>");
  136.             }
  137.             return p.get(AbsoluteDate.ARBITRARY_EPOCH);
  138.         } else {
  139.             throw new OrekitIllegalStateException(OrekitMessages.PARAMETER_WITH_SEVERAL_ESTIMATED_VALUES, driver.getName(), "getParameterDerivatives(driver, date)");
  140.         }
  141.     }

  142.     /** Get the partial derivatives of the {@link #getEstimatedValue()
  143.      * simulated measurement} with respect to a parameter.
  144.      * @param driver name of the span of the driver for the parameter for which
  145.      * the derivative wants to be known.
  146.      * @param date date at which the parameter derivatives wants to be known
  147.      * @return partial derivatives of the simulated value
  148.      * @exception OrekitIllegalArgumentException if parameter is unknown
  149.      */
  150.     public double[] getParameterDerivatives(final ParameterDriver driver, final AbsoluteDate date)
  151.         throws OrekitIllegalArgumentException {
  152.         final TimeSpanMap<double[]> p = parametersDerivatives.get(driver);
  153.         if (p == null) {
  154.             final StringBuilder builder = new StringBuilder();
  155.             for (final Map.Entry<ParameterDriver, TimeSpanMap<double[]>> entry : parametersDerivatives.entrySet()) {
  156.                 if (builder.length() > 0) {
  157.                     builder.append(", ");
  158.                 }
  159.                 builder.append(entry.getKey());
  160.             }
  161.             throw new OrekitIllegalArgumentException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  162.                                                      driver,
  163.                                                      builder.length() > 0 ? builder.toString() : "<none>");
  164.         }
  165.         return p.get(date);
  166.     }

  167.     /** Set the partial derivatives of the {@link #getEstimatedValue()
  168.      * simulated measurement} with respect to parameter.
  169.      * @param driver name of the span of the driver for the parameter for which
  170.      * the derivative wants to be known.
  171.      * @param date date at which the parameterDerivative wants to be set
  172.      * @param parameterDerivatives partial derivatives with respect to parameter
  173.      */
  174.     public void setParameterDerivatives(final ParameterDriver driver, final AbsoluteDate date, final double... parameterDerivatives) {
  175.         if (!parametersDerivatives.containsKey(driver) || parametersDerivatives.get(driver) == null) {
  176.             final TimeSpanMap<double[]> derivativeSpanMap = new TimeSpanMap<>(parameterDerivatives);
  177.             final TimeSpanMap<String> driverNameSpan = driver.getNamesSpanMap();
  178.             for (Span<String> span = driverNameSpan.getSpan(driverNameSpan.getFirstSpan().getEnd()); span != null; span = span.next()) {
  179.                 derivativeSpanMap.addValidAfter(parameterDerivatives, span.getStart(), false);
  180.             }
  181.             parametersDerivatives.put(driver, derivativeSpanMap);

  182.         } else {

  183.             AbsoluteDate dateToAddAfter = driver.getNamesSpanMap().getSpan(date).getStart();
  184.             if (dateToAddAfter.equals(AbsoluteDate.PAST_INFINITY)) {
  185.                 dateToAddAfter = driver.getNamesSpanMap().getSpan(date).getEnd();
  186.                 parametersDerivatives.get(driver).addValidBefore(parameterDerivatives, dateToAddAfter, false);
  187.             } else {
  188.                 parametersDerivatives.get(driver).addValidAfter(parameterDerivatives, dateToAddAfter, false);
  189.             }

  190.         }

  191.     }

  192.     /** Set the partial derivatives of the {@link #getEstimatedValue()
  193.      * simulated measurement} with respect to parameter.
  194.      * @param driver driver for the parameter
  195.      * @param parameterDerivativesMap partial derivatives with respect to parameter
  196.      */
  197.     public void setParameterDerivatives(final ParameterDriver driver, final TimeSpanMap<double[]> parameterDerivativesMap) {
  198.         parametersDerivatives.put(driver, parameterDerivativesMap);
  199.     }

  200. }