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.modifiers;
18  
19  import java.util.Arrays;
20  
21  import org.hipparchus.analysis.differentiation.Gradient;
22  import org.orekit.estimation.measurements.EstimatedMeasurement;
23  import org.orekit.estimation.measurements.EstimatedMeasurementBase;
24  import org.orekit.estimation.measurements.EstimationModifier;
25  import org.orekit.estimation.measurements.GroundStation;
26  import org.orekit.estimation.measurements.ObservedMeasurement;
27  import org.orekit.propagation.FieldSpacecraftState;
28  import org.orekit.propagation.SpacecraftState;
29  import org.orekit.propagation.integration.AbstractGradientConverter;
30  import org.orekit.utils.Differentiation;
31  import org.orekit.utils.ParameterDriver;
32  import org.orekit.utils.ParameterDriversProvider;
33  import org.orekit.utils.TimeSpanMap.Span;
34  
35  /** Utility class modifying theoretical range-rate measurement.
36   * @author Joris Olympio
37   * @since 11.2
38   */
39  public class RangeRateModifierUtil {
40  
41      /** Private constructor for utility class.*/
42      private RangeRateModifierUtil() {
43          // not used
44      }
45  
46      /** Apply a modifier to an estimated measurement.
47       * @param <T> type of the measurement
48       * @param estimated estimated measurement to modify
49       * @param station ground station
50       * @param modelEffect model effect
51       * @param modifier applied modifier
52       * @since 12.1
53       */
54      public static <T extends ObservedMeasurement<T>> void modifyWithoutDerivatives(final EstimatedMeasurementBase<T> estimated,
55                                                                                     final GroundStation station,
56                                                                                     final ParametricModelEffect modelEffect,
57                                                                                     final EstimationModifier<T> modifier) {
58  
59          final SpacecraftState state    = estimated.getStates()[0];
60  
61          // update estimated value taking into account the delay. The  delay is directly added to the range.
62          final double[] newValue = estimated.getEstimatedValue();
63          final double delay = modelEffect.evaluate(station, state);
64          newValue[0] = newValue[0] + delay;
65          estimated.modifyEstimatedValue(modifier, newValue);
66  
67      }
68  
69      /** Apply a modifier to an estimated measurement.
70       * @param <T> type of the measurement
71       * @param estimated estimated measurement to modify
72       * @param station ground station
73       * @param converter gradient converter
74       * @param parametricModel parametric modifier model
75       * @param modelEffect model effect
76       * @param modelEffectGradient model effect gradient
77       * @param modifier applied modifier
78       * @since 12.1
79       */
80      public static <T extends ObservedMeasurement<T>> void modify(final EstimatedMeasurement<T> estimated,
81                                                                   final ParameterDriversProvider parametricModel,
82                                                                   final AbstractGradientConverter converter,
83                                                                   final GroundStation station,
84                                                                   final ParametricModelEffect modelEffect,
85                                                                   final ParametricModelEffectGradient modelEffectGradient,
86                                                                   final EstimationModifier<T> modifier) {
87  
88          final SpacecraftState state = estimated.getStates()[0];
89  
90          // update estimated derivatives with Jacobian of the measure wrt state
91          final FieldSpacecraftState<Gradient> gState = converter.getState(parametricModel);
92          final Gradient[] gParameters = converter.getParameters(gState, parametricModel);
93          final Gradient gDelay = modelEffectGradient.evaluate(station, gState, gParameters);
94          final double[] derivatives = gDelay.getGradient();
95  
96          // update estimated derivatives with Jacobian of the measure wrt state
97          final double[][] stateDerivatives = estimated.getStateDerivatives(0);
98          for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
99              stateDerivatives[0][jcol] += derivatives[jcol];
100         }
101         estimated.setStateDerivatives(0, stateDerivatives);
102 
103         int index = 0;
104         for (final ParameterDriver driver : parametricModel.getParametersDrivers()) {
105             if (driver.isSelected()) {
106                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
107 
108                     // update estimated derivatives with derivative of the modification wrt modifier parameters
109                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
110                     parameterDerivative += derivatives[index + converter.getFreeStateParameters()];
111                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
112                     index = index + 1;
113                 }
114             }
115 
116         }
117 
118         for (final ParameterDriver driver : Arrays.asList(station.getClockOffsetDriver(),
119                                                           station.getEastOffsetDriver(),
120                                                           station.getNorthOffsetDriver(),
121                                                           station.getZenithOffsetDriver())) {
122             if (driver.isSelected()) {
123                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
124 
125                     // update estimated derivatives with derivative of the modification wrt station parameters
126                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
127                     parameterDerivative += Differentiation.differentiate((d, t) -> modelEffect.evaluate(station, state),
128                                                                          3, 10.0 * driver.getScale()).value(driver, state.getDate());
129                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
130                 }
131             }
132         }
133 
134         // update estimated value taking into account the delay.
135         modifyWithoutDerivatives(estimated, station, modelEffect, modifier);
136 
137     }
138 
139 }