AbstractMeasurementBuilder.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.generation;

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

  22. import org.hipparchus.random.CorrelatedRandomVectorGenerator;
  23. import org.orekit.estimation.measurements.EstimatedMeasurementBase;
  24. import org.orekit.estimation.measurements.EstimationModifier;
  25. import org.orekit.estimation.measurements.ObservableSatellite;
  26. import org.orekit.estimation.measurements.ObservedMeasurement;
  27. import org.orekit.propagation.SpacecraftState;
  28. import org.orekit.propagation.sampling.OrekitStepInterpolator;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.ParameterDriver;

  31. /** Base class for {@link MeasurementBuilder measurements builders}.
  32.  * @param <T> the type of the measurement
  33.  * @author Luc Maisonobe
  34.  * @since 9.3
  35.  */
  36. public abstract class AbstractMeasurementBuilder<T extends ObservedMeasurement<T>> implements MeasurementBuilder<T> {

  37.     /** Noise source (may be null). */
  38.     private final CorrelatedRandomVectorGenerator noiseSource;

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

  41.     /** Theoretical standard deviation. */
  42.     private final double[] sigma;

  43.     /** Base weight. */
  44.     private final double[] baseWeight;

  45.     /** Satellites related to this measurement. */
  46.     private final ObservableSatellite[] satellites;

  47.     /** Start of the measurements time span. */
  48.     private AbsoluteDate spanStart;

  49.     /** End of the measurements time span. */
  50.     private AbsoluteDate spanEnd;

  51.     /** Simple constructor.
  52.      * @param noiseSource noise source, may be null for generating perfect measurements
  53.      * @param sigma theoretical standard deviation
  54.      * @param baseWeight base weight
  55.      * @param satellites satellites related to this builder
  56.      */
  57.     protected AbstractMeasurementBuilder(final CorrelatedRandomVectorGenerator noiseSource,
  58.                                          final double sigma, final double baseWeight,
  59.                                          final ObservableSatellite... satellites) {
  60.         this(noiseSource,
  61.              new double[] {
  62.                  sigma
  63.              }, new double[] {
  64.                  baseWeight
  65.              }, satellites);
  66.     }

  67.     /** Simple constructor.
  68.      * @param noiseSource noise source, may be null for generating perfect measurements
  69.      * @param sigma theoretical standard deviation
  70.      * @param baseWeight base weight
  71.      * @param satellites satellites related to this builder
  72.      */
  73.     protected AbstractMeasurementBuilder(final CorrelatedRandomVectorGenerator noiseSource,
  74.                                          final double[] sigma, final double[] baseWeight,
  75.                                          final ObservableSatellite... satellites) {
  76.         this.noiseSource = noiseSource;
  77.         this.modifiers   = new ArrayList<>();
  78.         this.sigma       = sigma.clone();
  79.         this.baseWeight  = baseWeight.clone();
  80.         this.satellites  = satellites.clone();
  81.     }

  82.     /** {@inheritDoc}
  83.      * <p>
  84.      * This implementation stores the time span of the measurements generation.
  85.      * </p>
  86.      */
  87.     @Override
  88.     public void init(final AbsoluteDate start, final AbsoluteDate end) {
  89.         spanStart = start;
  90.         spanEnd   = end;
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     public void addModifier(final EstimationModifier<T> modifier) {
  95.         modifiers.add(modifier);
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     public List<EstimationModifier<T>> getModifiers() {
  100.         return Collections.unmodifiableList(modifiers);
  101.     }

  102.     /** Get the start of the measurements time span.
  103.      * @return start of the measurements time span
  104.      */
  105.     protected AbsoluteDate getStart() {
  106.         return spanStart;
  107.     }

  108.     /** Get the end of the measurements time span.
  109.      * @return end of the measurements time span
  110.      */
  111.     protected AbsoluteDate getEnd() {
  112.         return spanEnd;
  113.     }

  114.     /** Generate a noise vector.
  115.      * @return noise vector (null if we generate perfect measurements)
  116.      */
  117.     protected double[] getNoise() {
  118.         return noiseSource == null ? null : noiseSource.nextVector();
  119.     }

  120.     /** Get the theoretical standard deviation.
  121.      * <p>
  122.      * The theoretical standard deviation is a theoretical value
  123.      * used for normalizing the residuals. It acts as a weighting
  124.      * factor to mix appropriately measurements with different units
  125.      * and different accuracy. The value has the same dimension as
  126.      * the measurement itself (i.e. when a residual is divided by
  127.      * this value, it becomes dimensionless).
  128.      * </p>
  129.      * @return expected standard deviation
  130.      * @see #getBaseWeight()
  131.      */
  132.     protected double[] getTheoreticalStandardDeviation() {
  133.         return sigma.clone();
  134.     }

  135.     /** Get the base weight associated with the measurement
  136.      * <p>
  137.      * The base weight is used on residuals already normalized thanks to
  138.      * {@link #getTheoreticalStandardDeviation()} to increase or
  139.      * decrease relative effect of some measurements with respect to
  140.      * other measurements. It is a dimensionless value, typically between
  141.      * 0 and 1 (but it can really have any non-negative value).
  142.      * </p>
  143.      * @return base weight
  144.      * @see #getTheoreticalStandardDeviation()
  145.      */
  146.     protected double[] getBaseWeight() {
  147.         return baseWeight.clone();
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     public ObservableSatellite[] getSatellites() {
  152.         return satellites.clone();
  153.     }

  154.     /**
  155.      * Build a dummy observed measurement.
  156.      *
  157.      * @param date          measurement date
  158.      * @param interpolators interpolators relevant for this builder
  159.      * @return dummy observed measurement
  160.      * @since 13.0
  161.      */
  162.     protected abstract T buildObserved(AbsoluteDate date,
  163.                                        Map<ObservableSatellite, OrekitStepInterpolator> interpolators);

  164.     /** {@inheritDoc} */
  165.     @Override
  166.     public EstimatedMeasurementBase<T> build(final AbsoluteDate date,
  167.                                              final Map<ObservableSatellite, OrekitStepInterpolator> interpolators) {

  168.         final SpacecraftState[] relevant = new SpacecraftState[satellites.length];
  169.         for (int i = 0; i < relevant.length; ++i) {
  170.             relevant[i] = interpolators.get(satellites[i]).getInterpolatedState(date);
  171.         }

  172.         // create a dummy observed measurement
  173.         final T observed = buildObserved(date, interpolators);
  174.         for (final EstimationModifier<T> modifier : getModifiers()) {
  175.             observed.addModifier(modifier);
  176.         }

  177.         // set a reference date for parameters missing one
  178.         for (final ParameterDriver driver : observed.getParametersDrivers()) {
  179.             if (driver.getReferenceDate() == null) {
  180.                 final AbsoluteDate start = getStart();
  181.                 final AbsoluteDate end   = getEnd();
  182.                 driver.setReferenceDate(start.durationFrom(end) <= 0 ? start : end);
  183.             }
  184.         }

  185.         // estimate the perfect value of the measurement
  186.         final EstimatedMeasurementBase<T> estimated = observed.estimateWithoutDerivatives(relevant);
  187.         final double[] value = estimated.getEstimatedValue();

  188.         // add the noise
  189.         final double[] noise = getNoise();
  190.         if (noise != null) {
  191.             for (int i = 0; i < value.length; ++i) {
  192.                 value[i] += noise[i];
  193.             }
  194.         }

  195.         // update the dummy measurement (which is referenced by the estimated measurement)
  196.         observed.setObservedValue(value);

  197.         return estimated;

  198.     }

  199. }