1   /* Copyright 2002-2019 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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.Field;
23  import org.hipparchus.RealFieldElement;
24  import org.hipparchus.analysis.differentiation.DerivativeStructure;
25  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.orekit.estimation.measurements.EstimatedMeasurement;
28  import org.orekit.estimation.measurements.EstimationModifier;
29  import org.orekit.estimation.measurements.GroundStation;
30  import org.orekit.estimation.measurements.TurnAroundRange;
31  import org.orekit.models.earth.DiscreteTroposphericModel;
32  import org.orekit.models.earth.TroposphericModel;
33  import org.orekit.orbits.OrbitType;
34  import org.orekit.orbits.PositionAngle;
35  import org.orekit.propagation.FieldSpacecraftState;
36  import org.orekit.propagation.Propagator;
37  import org.orekit.propagation.SpacecraftState;
38  import org.orekit.utils.Differentiation;
39  import org.orekit.utils.ParameterDriver;
40  import org.orekit.utils.ParameterFunction;
41  import org.orekit.utils.StateFunction;
42  
43  /** Class modifying theoretical turn-around TurnAroundRange measurement with tropospheric delay.
44   * The effect of tropospheric correction on the TurnAroundRange is directly computed
45   * through the computation of the tropospheric delay.
46   *
47   * In general, for GNSS, VLBI, ... there is hardly any frequency dependence in the delay.
48   * For SLR techniques however, the frequency dependence is sensitive.
49   *
50   * @author Maxime Journot
51   * @since 9.0
52   */
53  public class TurnAroundRangeTroposphericDelayModifier implements EstimationModifier<TurnAroundRange> {
54  
55      /** Tropospheric delay model. */
56      private final DiscreteTroposphericModel tropoModel;
57  
58      /** Constructor.
59       *
60       * @param model  Tropospheric delay model appropriate for the current TurnAroundRange measurement method.
61       */
62      public TurnAroundRangeTroposphericDelayModifier(final DiscreteTroposphericModel model) {
63          tropoModel = model;
64      }
65  
66      /** Get the station height above mean sea level.
67       *
68       * @param station  ground station (or measuring station)
69       * @return the measuring station height above sea level, m
70       */
71      private double getStationHeightAMSL(final GroundStation station) {
72          // FIXME height should be computed with respect to geoid WGS84+GUND = EGM2008 for example
73          final double height = station.getBaseFrame().getPoint().getAltitude();
74          return height;
75      }
76  
77      /** Get the station height above mean sea level.
78      * @param <T> type of the elements
79      * @param field field of the elements
80      * @param station  ground station (or measuring station)
81      * @return the measuring station height above sea level, m
82      */
83      private <T extends RealFieldElement<T>> T getStationHeightAMSL(final Field<T> field,
84                                                                     final GroundStation station) {
85          // FIXME heigth should be computed with respect to geoid WGS84+GUND = EGM2008 for example
86          final T height = station.getBaseFrame().getPoint(field).getAltitude();
87          return height;
88      }
89  
90      /** Compute the measurement error due to Troposphere.
91       * @param station station
92       * @param state spacecraft state
93       * @return the measurement error due to Troposphere
94       */
95      private double rangeErrorTroposphericModel(final GroundStation station, final SpacecraftState state) {
96          //
97          final Vector3D position = state.getPVCoordinates().getPosition();
98  
99          // elevation
100         final double elevation = station.getBaseFrame().getElevation(position,
101                                                                      state.getFrame(),
102                                                                      state.getDate());
103 
104         // only consider measures above the horizon
105         if (elevation > 0) {
106             // altitude AMSL in meters
107             final double height = getStationHeightAMSL(station);
108 
109             // Delay in meters
110             final double delay = tropoModel.pathDelay(elevation, height, tropoModel.getParameters(), state.getDate());
111 
112             return delay;
113         }
114 
115         return 0;
116     }
117 
118     /** Compute the measurement error due to Troposphere.
119      * @param <T> type of the element
120      * @param station station
121      * @param state spacecraft state
122      * @param parameters tropospheric model parameters
123      * @return the measurement error due to Troposphere
124      */
125     private <T extends RealFieldElement<T>> T rangeErrorTroposphericModel(final GroundStation station,
126                                                                           final FieldSpacecraftState<T> state,
127                                                                           final T[] parameters) {
128         // Field
129         final Field<T> field = state.getDate().getField();
130         final T zero         = field.getZero();
131 
132         //
133         final FieldVector3D<T> position = state.getPVCoordinates().getPosition();
134         final T dsElevation             = station.getBaseFrame().getElevation(position,
135                                                                               state.getFrame(),
136                                                                               state.getDate());
137 
138         // only consider measures above the horizon
139         if (dsElevation.getReal() > 0) {
140             // altitude AMSL in meters
141             final T height = getStationHeightAMSL(field, station);
142 
143             // Delay in meters
144             final T delay = tropoModel.pathDelay(dsElevation, height, parameters, state.getDate());
145 
146             return delay;
147         }
148 
149         return zero;
150     }
151 
152     /** Compute the Jacobian of the delay term wrt state.
153     *
154     * @param station station
155     * @param refstate reference spacecraft state
156     *
157     * @return Jacobian of the delay wrt state
158     */
159     private double[][] rangeErrorJacobianState(final GroundStation station, final SpacecraftState refstate) {
160         final double[][] finiteDifferencesJacobian =
161                         Differentiation.differentiate(new StateFunction() {
162                             public double[] value(final SpacecraftState state) {
163                                 // evaluate target's elevation with a changed target position
164                                 final double value = rangeErrorTroposphericModel(station, state);
165 
166                                 return new double[] {value };
167                             }
168                         }, 1, Propagator.DEFAULT_LAW, OrbitType.CARTESIAN,
169                         PositionAngle.TRUE, 15.0, 3).value(refstate);
170 
171         return finiteDifferencesJacobian;
172     }
173 
174     /** Compute the Jacobian of the delay term wrt state using
175     * automatic differentiation.
176     *
177     * @param derivatives tropospheric delay derivatives
178     * @param freeStateParameters dimension of the state.
179     *
180     * @return Jacobian of the delay wrt state
181     */
182     private double[][] rangeErrorJacobianState(final double[] derivatives, final int freeStateParameters) {
183         final double[][] finiteDifferencesJacobian = new double[1][6];
184         for (int i = 0; i < freeStateParameters; i++) {
185             // First element is the value of the delay
186             finiteDifferencesJacobian[0][i] = derivatives[i + 1];
187         }
188         return finiteDifferencesJacobian;
189     }
190 
191 
192     /** Compute the derivative of the delay term wrt parameters.
193      *
194      * @param station ground station
195      * @param driver driver for the station offset parameter
196      * @param state spacecraft state
197      * @return derivative of the delay wrt station offset parameter
198      */
199     private double rangeErrorParameterDerivative(final GroundStation station,
200                                                  final ParameterDriver driver,
201                                                  final SpacecraftState state) {
202 
203         final ParameterFunctionParameterFunction">ParameterFunction rangeError = new ParameterFunction() {
204             /** {@inheritDoc} */
205             @Override
206             public double value(final ParameterDriver parameterDriver) {
207                 return rangeErrorTroposphericModel(station, state);
208             }
209         };
210 
211         final ParameterFunction rangeErrorDerivative = Differentiation.differentiate(rangeError, 3, 10.0 * driver.getScale());
212 
213         return rangeErrorDerivative.value(driver);
214 
215     }
216 
217     /** Compute the derivative of the delay term wrt parameters using
218     * automatic differentiation.
219     *
220     * @param derivatives tropospheric delay derivatives
221     * @param freeStateParameters dimension of the state.
222     * @return derivative of the delay wrt tropospheric model parameters
223     */
224     private double[] rangeErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
225         // 0                               -> value of the delay
226         // 1 ... freeStateParameters       -> derivatives of the delay wrt state
227         // freeStateParameters + 1 ... n   -> derivatives of the delay wrt tropospheric parameters
228         final int dim = derivatives.length - 1 - freeStateParameters;
229         final double[] rangeError = new double[dim];
230 
231         for (int i = 0; i < dim; i++) {
232             rangeError[i] = derivatives[1 + freeStateParameters + i];
233         }
234 
235         return rangeError;
236     }
237 
238     /** {@inheritDoc} */
239     @Override
240     public List<ParameterDriver> getParametersDrivers() {
241         return tropoModel.getParametersDrivers();
242     }
243 
244     /** {@inheritDoc} */
245     @Override
246     public void modify(final EstimatedMeasurement<TurnAroundRange> estimated) {
247         final TurnAroundRange measurement   = estimated.getObservedMeasurement();
248         final GroundStation   masterStation = measurement.getMasterStation();
249         final GroundStation   slaveStation  = measurement.getSlaveStation();
250         final SpacecraftState state         = estimated.getStates()[0];
251 
252         final double[] oldValue = estimated.getEstimatedValue();
253 
254         // Update estimated derivatives with Jacobian of the measure wrt state
255         final TroposphericDSConverterTroposphericDSConverter.html#TroposphericDSConverter">TroposphericDSConverter converter = new TroposphericDSConverter(state, 6, Propagator.DEFAULT_LAW);
256         final FieldSpacecraftState<DerivativeStructure> dsState = converter.getState(tropoModel);
257         final DerivativeStructure[] dsParameters = converter.getParameters(dsState, tropoModel);
258         final DerivativeStructure masterDSDelay = rangeErrorTroposphericModel(masterStation, dsState, dsParameters);
259         final DerivativeStructure slaveDSDelay = rangeErrorTroposphericModel(slaveStation, dsState, dsParameters);
260         final double[] masterDerivatives = masterDSDelay.getAllDerivatives();
261         final double[] slaveDerivatives  = masterDSDelay.getAllDerivatives();
262 
263         double[][] masterDjac = new double[1][6];
264         double[][] slaveDjac  = new double[1][6];
265         // This implementation will disappear when the implementations of TroposphericModel
266         // will directly be implementations of DiscreteTroposphericModel
267         if (tropoModel instanceof TroposphericModel) {
268             masterDjac = rangeErrorJacobianState(masterStation, state);
269             slaveDjac  = rangeErrorJacobianState(slaveStation, state);
270         } else {
271             masterDjac = rangeErrorJacobianState(masterDerivatives, converter.getFreeStateParameters());
272             slaveDjac  = rangeErrorJacobianState(slaveDerivatives, converter.getFreeStateParameters());
273         }
274         final double[][] stateDerivatives = estimated.getStateDerivatives(0);
275         for (int irow = 0; irow < stateDerivatives.length; ++irow) {
276             for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
277                 stateDerivatives[irow][jcol] += masterDjac[irow][jcol] + slaveDjac[irow][jcol];
278             }
279         }
280         estimated.setStateDerivatives(0, stateDerivatives);
281 
282         int indexMaster = 0;
283         for (final ParameterDriver driver : getParametersDrivers()) {
284             if (driver.isSelected()) {
285                 // update estimated derivatives with derivative of the modification wrt tropospheric parameters
286                 double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
287                 final double[] derivatives = rangeErrorParameterDerivative(masterDerivatives, converter.getFreeStateParameters());
288                 parameterDerivative += derivatives[indexMaster];
289                 estimated.setParameterDerivatives(driver, parameterDerivative);
290                 indexMaster += 1;
291             }
292 
293         }
294 
295         int indexSlave = 0;
296         for (final ParameterDriver driver : getParametersDrivers()) {
297             if (driver.isSelected()) {
298                 // update estimated derivatives with derivative of the modification wrt tropospheric parameters
299                 double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
300                 final double[] derivatives = rangeErrorParameterDerivative(slaveDerivatives, converter.getFreeStateParameters());
301                 parameterDerivative += derivatives[indexSlave];
302                 estimated.setParameterDerivatives(driver, parameterDerivative);
303                 indexSlave += 1;
304             }
305 
306         }
307 
308         // Update derivatives with respect to master station position
309         for (final ParameterDriver driver : Arrays.asList(masterStation.getClockOffsetDriver(),
310                                                           masterStation.getEastOffsetDriver(),
311                                                           masterStation.getNorthOffsetDriver(),
312                                                           masterStation.getZenithOffsetDriver())) {
313             if (driver.isSelected()) {
314                 double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
315                 parameterDerivative += rangeErrorParameterDerivative(masterStation, driver, state);
316                 estimated.setParameterDerivatives(driver, parameterDerivative);
317             }
318         }
319 
320         // Update derivatives with respect to slave station position
321         for (final ParameterDriver driver : Arrays.asList(slaveStation.getEastOffsetDriver(),
322                                                           slaveStation.getNorthOffsetDriver(),
323                                                           slaveStation.getZenithOffsetDriver())) {
324             if (driver.isSelected()) {
325                 double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
326                 parameterDerivative += rangeErrorParameterDerivative(slaveStation, driver, state);
327                 estimated.setParameterDerivatives(driver, parameterDerivative);
328             }
329         }
330 
331         // Update estimated value taking into account the tropospheric delay.
332         // The tropospheric delay is directly added to the TurnAroundRange.
333         final double[] newValue = oldValue.clone();
334         newValue[0] = newValue[0] + masterDSDelay.getReal() + slaveDSDelay.getReal();
335         estimated.setEstimatedValue(newValue);
336 
337     }
338 
339 }