1   /* Copyright 2002-2021 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  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.analysis.differentiation.Gradient;
24  import org.orekit.attitudes.InertialProvider;
25  import org.orekit.estimation.measurements.EstimatedMeasurement;
26  import org.orekit.estimation.measurements.EstimationModifier;
27  import org.orekit.estimation.measurements.GroundStation;
28  import org.orekit.estimation.measurements.Range;
29  import org.orekit.frames.TopocentricFrame;
30  import org.orekit.models.earth.ionosphere.IonosphericModel;
31  import org.orekit.propagation.FieldSpacecraftState;
32  import org.orekit.propagation.SpacecraftState;
33  import org.orekit.utils.Differentiation;
34  import org.orekit.utils.ParameterDriver;
35  import org.orekit.utils.ParameterFunction;
36  
37  /** Class modifying theoretical range measurement with ionospheric delay.
38   * The effect of ionospheric correction on the range is directly computed
39   * through the computation of the ionospheric delay.
40   *
41   * The ionospheric delay depends on the frequency of the signal (GNSS, VLBI, ...).
42   * For optical measurements (e.g. SLR), the ray is not affected by ionosphere charged particles.
43   * <p>
44   * Since 10.0, state derivatives and ionospheric parameters derivates are computed
45   * using automatic differentiation.
46   * </p>
47   * @author Maxime Journot
48   * @author Joris Olympio
49   * @since 8.0
50   */
51  public class RangeIonosphericDelayModifier implements EstimationModifier<Range> {
52  
53      /** Ionospheric delay model. */
54      private final IonosphericModel ionoModel;
55  
56      /** Frequency [Hz]. */
57      private final double frequency;
58  
59      /** Constructor.
60       *
61       * @param model Ionospheric delay model appropriate for the current range measurement method.
62       * @param freq frequency of the signal in Hz
63       */
64      public RangeIonosphericDelayModifier(final IonosphericModel model,
65                                           final double freq) {
66          ionoModel = model;
67          frequency = freq;
68      }
69  
70      /** Compute the measurement error due to ionosphere.
71       * @param station station
72       * @param state spacecraft state
73       * @return the measurement error due to ionosphere
74       */
75      private double rangeErrorIonosphericModel(final GroundStation station,
76                                                final SpacecraftState state) {
77          // Base frame associated with the station
78          final TopocentricFrame baseFrame = station.getBaseFrame();
79          // delay in meters
80          final double delay = ionoModel.pathDelay(state, baseFrame, frequency, ionoModel.getParameters());
81          return delay;
82      }
83  
84      /** Compute the measurement error due to ionosphere.
85       * @param <T> type of the element
86       * @param station station
87       * @param state spacecraft state
88       * @param parameters ionospheric model parameters
89       * @return the measurement error due to ionosphere
90       */
91      private <T extends CalculusFieldElement<T>> T rangeErrorIonosphericModel(final GroundStation station,
92                                                                           final FieldSpacecraftState<T> state,
93                                                                           final T[] parameters) {
94           // Base frame associated with the station
95          final TopocentricFrame baseFrame = station.getBaseFrame();
96          // delay in meters
97          final T delay = ionoModel.pathDelay(state, baseFrame, frequency, parameters);
98          return delay;
99      }
100 
101     /** Compute the Jacobian of the delay term wrt state using
102     * automatic differentiation.
103     *
104     * @param derivatives ionospheric delay derivatives
105     *
106     * @return Jacobian of the delay wrt state
107     */
108     private double[][] rangeErrorJacobianState(final double[] derivatives) {
109         final double[][] finiteDifferencesJacobian = new double[1][6];
110         System.arraycopy(derivatives, 0, finiteDifferencesJacobian[0], 0, 6);
111         return finiteDifferencesJacobian;
112     }
113 
114 
115     /** Compute the derivative of the delay term wrt parameters.
116     *
117     * @param station ground station
118     * @param driver driver for the station offset parameter
119     * @param state spacecraft state
120     * @param delay current ionospheric delay
121     * @return derivative of the delay wrt station offset parameter
122     */
123     private double rangeErrorParameterDerivative(final GroundStation station,
124                                                  final ParameterDriver driver,
125                                                  final SpacecraftState state,
126                                                  final double delay) {
127 
128         final ParameterFunction rangeError = new ParameterFunction() {
129             /** {@inheritDoc} */
130             @Override
131             public double value(final ParameterDriver parameterDriver) {
132                 return rangeErrorIonosphericModel(station, state);
133             }
134         };
135 
136         final ParameterFunction rangeErrorDerivative =
137                        Differentiation.differentiate(rangeError, 3, 10.0 * driver.getScale());
138 
139         return rangeErrorDerivative.value(driver);
140 
141     }
142 
143     /** Compute the derivative of the delay term wrt parameters using
144     * automatic differentiation.
145     *
146     * @param derivatives ionospheric delay derivatives
147     * @param freeStateParameters dimension of the state.
148     * @return derivative of the delay wrt ionospheric model parameters
149     */
150     private double[] rangeErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
151         // 0 ... freeStateParameters - 1 -> derivatives of the delay wrt state
152         // freeStateParameters ... n     -> derivatives of the delay wrt ionospheric parameters
153         final int dim = derivatives.length - freeStateParameters;
154         final double[] rangeError = new double[dim];
155 
156         for (int i = 0; i < dim; i++) {
157             rangeError[i] = derivatives[freeStateParameters + i];
158         }
159 
160         return rangeError;
161     }
162 
163     /** {@inheritDoc} */
164     @Override
165     public List<ParameterDriver> getParametersDrivers() {
166         return ionoModel.getParametersDrivers();
167     }
168 
169     @Override
170     public void modify(final EstimatedMeasurement<Range> estimated) {
171         final Range           measurement = estimated.getObservedMeasurement();
172         final GroundStation   station     = measurement.getStation();
173         final SpacecraftState state       = estimated.getStates()[0];
174 
175         final double[] oldValue = estimated.getEstimatedValue();
176 
177         // update estimated derivatives with Jacobian of the measure wrt state
178         final IonosphericGradientConverter converter =
179                 new IonosphericGradientConverter(state, 6, new InertialProvider(state.getFrame()));
180         final FieldSpacecraftState<Gradient> gState = converter.getState(ionoModel);
181         final Gradient[] gParameters = converter.getParameters(gState, ionoModel);
182         final Gradient gDelay = rangeErrorIonosphericModel(station, gState, gParameters);
183         final double[] derivatives = gDelay.getGradient();
184 
185         final double[][] djac = rangeErrorJacobianState(derivatives);
186 
187         final double[][] stateDerivatives = estimated.getStateDerivatives(0);
188         for (int irow = 0; irow < stateDerivatives.length; ++irow) {
189             for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
190                 stateDerivatives[irow][jcol] += djac[irow][jcol];
191             }
192         }
193         estimated.setStateDerivatives(0, stateDerivatives);
194 
195         int index = 0;
196         for (final ParameterDriver driver : getParametersDrivers()) {
197             if (driver.isSelected()) {
198                 // update estimated derivatives with derivative of the modification wrt ionospheric parameters
199                 double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
200                 final double[] dDelaydP    = rangeErrorParameterDerivative(derivatives, converter.getFreeStateParameters());
201                 parameterDerivative += dDelaydP[index];
202                 estimated.setParameterDerivatives(driver, parameterDerivative);
203                 index = index + 1;
204             }
205 
206         }
207 
208         for (final ParameterDriver driver : Arrays.asList(station.getClockOffsetDriver(),
209                                                           station.getEastOffsetDriver(),
210                                                           station.getNorthOffsetDriver(),
211                                                           station.getZenithOffsetDriver())) {
212             if (driver.isSelected()) {
213                 // update estimated derivatives with derivative of the modification wrt station parameters
214                 double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
215                 parameterDerivative += rangeErrorParameterDerivative(station, driver, state, gDelay.getValue());
216                 estimated.setParameterDerivatives(driver, parameterDerivative);
217             }
218         }
219 
220         // update estimated value taking into account the ionospheric delay.
221         // The ionospheric delay is directly added to the range.
222         final double[] newValue = oldValue.clone();
223         newValue[0] = newValue[0] + gDelay.getValue();
224         estimated.setEstimatedValue(newValue);
225 
226     }
227 
228 }