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 measurement.
36   * @author Maxime Journot
37   * @author Joris Olympio
38   * @since 11.2
39   */
40  public class RangeModifierUtil {
41  
42      /** Private constructor for utility class.*/
43      private RangeModifierUtil() {
44          // not used
45      }
46  
47      /** Apply a modifier to an estimated measurement.
48       * @param <T> type of the measurement
49       * @param estimated estimated measurement to modify
50       * @param station ground station
51       * @param modelEffect model effect
52       * @param modifier applied modifier
53       * @since 12.1
54       */
55      public static <T extends ObservedMeasurement<T>> void modifyWithoutDerivatives(final EstimatedMeasurementBase<T> estimated,
56                                                                                     final GroundStation station,
57                                                                                     final ParametricModelEffect modelEffect,
58                                                                                     final EstimationModifier<T> modifier) {
59  
60          final SpacecraftState state    = estimated.getStates()[0];
61          final double[]        oldValue = estimated.getEstimatedValue();
62  
63          // update estimated value taking into account the delay. The delay is directly added to the range.
64          final double[] newValue = oldValue.clone();
65          final double delay = modelEffect.evaluate(station, state);
66          newValue[0] = newValue[0] + delay;
67          estimated.modifyEstimatedValue(modifier, newValue);
68  
69      }
70  
71      /** Apply a modifier to an estimated measurement.
72       * @param <T> type of the measurement
73       * @param estimated estimated measurement to modify
74       * @param station ground station
75       * @param converter gradient converter
76       * @param parametricModel parametric modifier model
77       * @param modelEffect model effect
78       * @param modelEffectGradient model effect gradient
79       * @param modifier applied modifier
80       */
81      public static <T extends ObservedMeasurement<T>> void modify(final EstimatedMeasurement<T> estimated,
82                                                                   final ParameterDriversProvider parametricModel,
83                                                                   final AbstractGradientConverter converter,
84                                                                   final GroundStation station,
85                                                                   final ParametricModelEffect modelEffect,
86                                                                   final ParametricModelEffectGradient modelEffectGradient,
87                                                                   final EstimationModifier<T> modifier) {
88  
89          final SpacecraftState state = estimated.getStates()[0];
90  
91          // update estimated derivatives with Jacobian of the measure wrt state
92          final FieldSpacecraftState<Gradient> gState = converter.getState(parametricModel);
93          final Gradient[] gParameters = converter.getParameters(gState, parametricModel);
94          final Gradient gDelay = modelEffectGradient.evaluate(station, gState, gParameters);
95          final double[] derivatives = gDelay.getGradient();
96  
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                 // update estimated derivatives with derivative of the modification wrt modifier parameters
107                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
108                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
109                     parameterDerivative += derivatives[index + converter.getFreeStateParameters()];
110                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
111                     index = index + 1;
112                 }
113             }
114 
115         }
116 
117         for (final ParameterDriver driver : Arrays.asList(station.getClockOffsetDriver(),
118                                                           station.getEastOffsetDriver(),
119                                                           station.getNorthOffsetDriver(),
120                                                           station.getZenithOffsetDriver())) {
121             if (driver.isSelected()) {
122                 // update estimated derivatives with derivative of the modification wrt station parameters
123                 for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
124                     double parameterDerivative = estimated.getParameterDerivatives(driver, span.getStart())[0];
125                     parameterDerivative += Differentiation.differentiate((d, t) -> modelEffect.evaluate(station, state),
126                                                                      3, 10.0 * driver.getScale()).value(driver, state.getDate());
127                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative);
128                 }
129             }
130         }
131 
132         // update estimated value taking into account the delay
133         modifyWithoutDerivatives(estimated, station, modelEffect, modifier);
134 
135     }
136 
137 }