EskfMeasurementHandler.java

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

  18. import java.util.List;

  19. import org.hipparchus.exception.MathRuntimeException;
  20. import org.hipparchus.filtering.kalman.ProcessEstimate;
  21. import org.hipparchus.filtering.kalman.extended.ExtendedKalmanFilter;
  22. import org.hipparchus.linear.MatrixUtils;
  23. import org.hipparchus.linear.RealMatrix;
  24. import org.orekit.errors.OrekitException;
  25. import org.orekit.estimation.measurements.ObservedMeasurement;
  26. import org.orekit.estimation.measurements.PV;
  27. import org.orekit.estimation.measurements.Position;
  28. import org.orekit.propagation.SpacecraftState;
  29. import org.orekit.propagation.sampling.OrekitStepHandler;
  30. import org.orekit.propagation.sampling.OrekitStepInterpolator;
  31. import org.orekit.time.AbsoluteDate;

  32. /** {@link org.orekit.propagation.sampling.OrekitStepHandler Step handler} picking up
  33.  * {@link ObservedMeasurement measurements} for the {@link SemiAnalyticalKalmanEstimator}.
  34.  * @author Julie Bayard
  35.  * @author Bryan Cazabonne
  36.  * @author Maxime Journot
  37.  * @since 11.1
  38.  */
  39. public class EskfMeasurementHandler implements OrekitStepHandler {

  40.     /** Least squares model. */
  41.     private final SemiAnalyticalKalmanModel model;

  42.     /** Extended Kalman Filter. */
  43.     private final ExtendedKalmanFilter<MeasurementDecorator> filter;

  44.     /** Underlying measurements. */
  45.     private final List<ObservedMeasurement<?>> observedMeasurements;

  46.     /** Index of the next measurement component in the model. */
  47.     private int index;

  48.     /** Reference date. */
  49.     private AbsoluteDate referenceDate;

  50.     /** Observer to retrieve current estimation info. */
  51.     private KalmanObserver observer;

  52.     /** Simple constructor.
  53.      * @param model semi-analytical kalman model
  54.      * @param filter kalman filter instance
  55.      * @param observedMeasurements list of observed measurements
  56.      * @param referenceDate reference date
  57.      */
  58.     public EskfMeasurementHandler(final SemiAnalyticalKalmanModel model,
  59.                                   final ExtendedKalmanFilter<MeasurementDecorator> filter,
  60.                                   final List<ObservedMeasurement<?>> observedMeasurements,
  61.                                   final AbsoluteDate referenceDate) {
  62.         this.model                = model;
  63.         this.filter               = filter;
  64.         this.observer             = model.getObserver();
  65.         this.observedMeasurements = observedMeasurements;
  66.         this.referenceDate        = referenceDate;
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public void init(final SpacecraftState s0, final AbsoluteDate t) {
  71.         this.index = 0;
  72.         // Initialize short periodic terms.
  73.         model.initializeShortPeriodicTerms(s0);
  74.         model.updateShortPeriods(s0);
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public void handleStep(final OrekitStepInterpolator interpolator) {

  79.         // Current date
  80.         final AbsoluteDate currentDate = interpolator.getCurrentState().getDate();

  81.         // Update the short period terms with the current MEAN state
  82.         model.updateShortPeriods(interpolator.getCurrentState());

  83.         // Process the measurements between previous step and current step
  84.         while (index < observedMeasurements.size() && observedMeasurements.get(index).getDate().compareTo(currentDate) < 0) {

  85.             try {

  86.                 // Update the norminal state with the interpolated parameters
  87.                 model.updateNominalSpacecraftState(interpolator.getInterpolatedState(observedMeasurements.get(index).getDate()));

  88.                 // Process the current observation
  89.                 final ProcessEstimate estimate = filter.estimationStep(decorate(observedMeasurements.get(index)));

  90.                 // Finalize the estimation
  91.                 model.finalizeEstimation(observedMeasurements.get(index), estimate);

  92.                 // Call the observer if the user add one
  93.                 if (observer != null) {
  94.                     observer.evaluationPerformed(model);
  95.                 }

  96.             } catch (MathRuntimeException mrte) {
  97.                 throw new OrekitException(mrte);
  98.             }

  99.             // Increment the measurement index
  100.             index += 1;

  101.         }

  102.         // Reset the initial state of the propagator
  103.         model.finalizeOperationsObservationGrid();

  104.     }

  105.     /** Decorate an observed measurement.
  106.      * <p>
  107.      * The "physical" measurement noise matrix is the covariance matrix of the measurement.
  108.      * Normalizing it consists in applying the following equation: Rn[i,j] =  R[i,j]/σ[i]/σ[j]
  109.      * Thus the normalized measurement noise matrix is the matrix of the correlation coefficients
  110.      * between the different components of the measurement.
  111.      * </p>
  112.      * @param observedMeasurement the measurement
  113.      * @return decorated measurement
  114.      */
  115.     private MeasurementDecorator decorate(final ObservedMeasurement<?> observedMeasurement) {

  116.         // Normalized measurement noise matrix contains 1 on its diagonal and correlation coefficients
  117.         // of the measurement on its non-diagonal elements.
  118.         // Indeed, the "physical" measurement noise matrix is the covariance matrix of the measurement
  119.         // Normalizing it leaves us with the matrix of the correlation coefficients
  120.         final RealMatrix covariance;
  121.         if (observedMeasurement instanceof PV) {
  122.             // For PV measurements we do have a covariance matrix and thus a correlation coefficients matrix
  123.             final PV pv = (PV) observedMeasurement;
  124.             covariance = MatrixUtils.createRealMatrix(pv.getCorrelationCoefficientsMatrix());
  125.         } else if (observedMeasurement instanceof Position) {
  126.             // For Position measurements we do have a covariance matrix and thus a correlation coefficients matrix
  127.             final Position position = (Position) observedMeasurement;
  128.             covariance = MatrixUtils.createRealMatrix(position.getCorrelationCoefficientsMatrix());
  129.         } else {
  130.             // For other measurements we do not have a covariance matrix.
  131.             // Thus the correlation coefficients matrix is an identity matrix.
  132.             covariance = MatrixUtils.createRealIdentityMatrix(observedMeasurement.getDimension());
  133.         }

  134.         return new MeasurementDecorator(observedMeasurement, covariance, referenceDate);

  135.     }

  136. }