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.leastsquares;
18  
19  import java.util.List;
20  
21  import org.orekit.estimation.measurements.EstimatedMeasurement;
22  import org.orekit.estimation.measurements.ObservableSatellite;
23  import org.orekit.estimation.measurements.ObservedMeasurement;
24  import org.orekit.propagation.SpacecraftState;
25  import org.orekit.propagation.sampling.MultiSatStepHandler;
26  import org.orekit.propagation.sampling.OrekitStepInterpolator;
27  import org.orekit.time.AbsoluteDate;
28  
29  /** {@link org.orekit.propagation.sampling.OrekitStepHandler Step handler} picking up
30   * {@link ObservedMeasurement measurements}.
31   * @author Luc Maisonobe
32   * @since 8.0
33   */
34  class MeasurementHandler implements MultiSatStepHandler {
35  
36      /** Least squares model. */
37      private final AbstractBatchLSModel model;
38  
39      /** Underlying measurements. */
40      private final List<PreCompensation> precompensated;
41  
42      /** Number of the next measurement. */
43      private int number;
44  
45      /** Index of the next measurement component in the model. */
46      private int index;
47  
48      /** Simple constructor.
49       * @param model least squares model
50       * @param precompensated underlying measurements
51       */
52      MeasurementHandler(final AbstractBatchLSModel model, final List<PreCompensation> precompensated) {
53          this.model          = model;
54          this.precompensated = precompensated;
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public void init(final List<SpacecraftState> initialStates, final AbsoluteDate target) {
60          number = 0;
61          index  = 0;
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public void handleStep(final List<OrekitStepInterpolator> interpolators) {
67  
68          while (number < precompensated.size()) {
69  
70              // Consider the next measurement to handle
71              final PreCompensation next = precompensated.get(number);
72  
73              // Current state date for interpolator 0
74              final AbsoluteDate currentDate = interpolators.get(0).getCurrentState().getDate();
75              if (model.isForwardPropagation()  && next.getDate().compareTo(currentDate) > 0 ||
76                  !model.isForwardPropagation() && next.getDate().compareTo(currentDate) < 0) {
77                  return;
78              }
79  
80              // get the observed measurement
81              final ObservedMeasurement<?> observed = next.getMeasurement();
82  
83              // estimate the theoretical measurement
84              final SpacecraftState[] states = new SpacecraftState[observed.getSatellites().size()];
85              for (int i = 0; i < states.length; ++i) {
86                  final ObservableSatellite satellite = observed.getSatellites().get(i);
87                  states[i] = interpolators.get(satellite.getPropagatorIndex()).getInterpolatedState(next.getDate());
88              }
89              final EstimatedMeasurement<?> estimated = observed.estimate(model.getIterationsCount(),
90                                                                          model.getEvaluationsCount(),
91                                                                          states);
92  
93              // fetch the evaluated measurement to the estimator
94              model.fetchEvaluatedMeasurement(index, estimated);
95  
96              // prepare handling of next measurement
97              ++number;
98              index += observed.getDimension();
99  
100         }
101 
102     }
103 
104 }