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;
18  
19  import java.util.Arrays;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.hipparchus.Field;
24  import org.hipparchus.analysis.differentiation.DSFactory;
25  import org.hipparchus.analysis.differentiation.DerivativeStructure;
26  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
27  import org.orekit.frames.FieldTransform;
28  import org.orekit.propagation.SpacecraftState;
29  import org.orekit.time.AbsoluteDate;
30  import org.orekit.time.FieldAbsoluteDate;
31  import org.orekit.utils.ParameterDriver;
32  import org.orekit.utils.TimeStampedFieldPVCoordinates;
33  import org.orekit.utils.TimeStampedPVCoordinates;
34  
35  /** Class modeling one-way or two-way range rate measurement between two vehicles.
36   * One-way range rate (or Doppler) measurements generally apply to specific satellites
37   * (e.g. GNSS, DORIS), where a signal is transmitted from a satellite to a
38   * measuring station.
39   * Two-way range rate measurements are applicable to any system. The signal is
40   * transmitted to the (non-spinning) satellite and returned by a transponder
41   * (or reflected back)to the same measuring station.
42   * The Doppler measurement can be obtained by multiplying the velocity by (fe/c), where
43   * fe is the emission frequency.
44   *
45   * @author Thierry Ceolin
46   * @author Joris Olympio
47   * @since 8.0
48   */
49  public class RangeRate extends AbstractMeasurement<RangeRate> {
50  
51      /** Ground station from which measurement is performed. */
52      private final GroundStation station;
53  
54      /** Flag indicating whether it is a two-way measurement. */
55      private final boolean twoway;
56  
57      /** Simple constructor.
58       * <p>
59       * This constructor uses 0 as the index of the propagator related
60       * to this measurement, thus being well suited for mono-satellite
61       * orbit determination.
62       * </p>
63       * @param station ground station from which measurement is performed
64       * @param date date of the measurement
65       * @param rangeRate observed value, m/s
66       * @param sigma theoretical standard deviation
67       * @param baseWeight base weight
68       * @param twoway if true, this is a two-way measurement
69       * @deprecated since 9.3 replaced by {@link #RangeRate(GroundStation, AbsoluteDate,
70       * double, double, double, boolean, ObservableSatellite)}
71       */
72      @Deprecated
73      public RangeRate(final GroundStation station, final AbsoluteDate date,
74                       final double rangeRate,
75                       final double sigma,
76                       final double baseWeight,
77                       final boolean twoway) {
78          this(station, date, rangeRate, sigma, baseWeight, twoway, new ObservableSatellite(0));
79      }
80  
81      /** Simple constructor.
82       * @param station ground station from which measurement is performed
83       * @param date date of the measurement
84       * @param rangeRate observed value, m/s
85       * @param sigma theoretical standard deviation
86       * @param baseWeight base weight
87       * @param twoway if true, this is a two-way measurement
88       * @param propagatorIndex index of the propagator related to this measurement
89       * @since 9.0
90       * @deprecated since 9.3 replaced by {@link #RangeRate(GroundStation, AbsoluteDate,
91       * double, double, double, boolean, ObservableSatellite)}
92       */
93      @Deprecated
94      public RangeRate(final GroundStation station, final AbsoluteDate date,
95                       final double rangeRate,
96                       final double sigma,
97                       final double baseWeight,
98                       final boolean twoway,
99                       final int propagatorIndex) {
100         this(station, date, rangeRate, sigma, baseWeight, twoway, new ObservableSatellite(propagatorIndex));
101     }
102 
103     /** Simple constructor.
104      * @param station ground station from which measurement is performed
105      * @param date date of the measurement
106      * @param rangeRate observed value, m/s
107      * @param sigma theoretical standard deviation
108      * @param baseWeight base weight
109      * @param twoway if true, this is a two-way measurement
110      * @param satellite satellite related to this measurement
111      * @since 9.3
112      */
113     public RangeRate(final GroundStation station, final AbsoluteDate date,
114                      final double rangeRate, final double sigma, final double baseWeight,
115                      final boolean twoway, final ObservableSatellite satellite) {
116         super(date, rangeRate, sigma, baseWeight, Arrays.asList(satellite));
117         addParameterDriver(station.getClockOffsetDriver());
118         addParameterDriver(station.getEastOffsetDriver());
119         addParameterDriver(station.getNorthOffsetDriver());
120         addParameterDriver(station.getZenithOffsetDriver());
121         addParameterDriver(station.getPrimeMeridianOffsetDriver());
122         addParameterDriver(station.getPrimeMeridianDriftDriver());
123         addParameterDriver(station.getPolarOffsetXDriver());
124         addParameterDriver(station.getPolarDriftXDriver());
125         addParameterDriver(station.getPolarOffsetYDriver());
126         addParameterDriver(station.getPolarDriftYDriver());
127         this.station = station;
128         this.twoway  = twoway;
129     }
130 
131     /** Check if the instance represents a two-way measurement.
132      * @return true if the instance represents a two-way measurement
133      */
134     public boolean isTwoWay() {
135         return twoway;
136     }
137 
138     /** Get the ground station from which measurement is performed.
139      * @return ground station from which measurement is performed
140      */
141     public GroundStation getStation() {
142         return station;
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     protected EstimatedMeasurement<RangeRate> theoreticalEvaluation(final int iteration, final int evaluation,
148                                                                     final SpacecraftState[] states) {
149 
150         final ObservableSatellite satellite = getSatellites().get(0);
151         final SpacecraftState     state     = states[satellite.getPropagatorIndex()];
152 
153         // Range-rate derivatives are computed with respect to spacecraft state in inertial frame
154         // and station position in station's offset frame
155         // -------
156         //
157         // Parameters:
158         //  - 0..2 - Position of the spacecraft in inertial frame
159         //  - 3..5 - Velocity of the spacecraft in inertial frame
160         //  - 6..n - station parameters (clock offset, station offsets, pole, prime meridian...)
161         int nbParams = 6;
162         final Map<String, Integer> indices = new HashMap<>();
163         for (ParameterDriver driver : getParametersDrivers()) {
164             if (driver.isSelected()) {
165                 indices.put(driver.getName(), nbParams++);
166             }
167         }
168         final DSFactory factory = new DSFactory(nbParams, 1);
169         final Field<DerivativeStructure> field = factory.getDerivativeField();
170         final FieldVector3D<DerivativeStructure> zero = FieldVector3D.getZero(field);
171 
172         // Coordinates of the spacecraft expressed as a derivative structure
173         final TimeStampedFieldPVCoordinates<DerivativeStructure> pvaDS = getCoordinates(state, 0, factory);
174 
175         // transform between station and inertial frame, expressed as a derivative structure
176         // The components of station's position in offset frame are the 3 last derivative parameters
177         final FieldTransform<DerivativeStructure> offsetToInertialDownlink =
178                         station.getOffsetToInertial(state.getFrame(), getDate(), factory, indices);
179         final FieldAbsoluteDate<DerivativeStructure> downlinkDateDS =
180                         offsetToInertialDownlink.getFieldDate();
181 
182         // Station position in inertial frame at end of the downlink leg
183         final TimeStampedFieldPVCoordinates<DerivativeStructure> stationDownlink =
184                         offsetToInertialDownlink.transformPVCoordinates(new TimeStampedFieldPVCoordinates<>(downlinkDateDS,
185                                                                                                             zero, zero, zero));
186 
187         // Compute propagation times
188         // (if state has already been set up to pre-compensate propagation delay,
189         //  we will have delta == tauD and transitState will be the same as state)
190 
191         // Downlink delay
192         final DerivativeStructure tauD = signalTimeOfFlight(pvaDS, stationDownlink.getPosition(), downlinkDateDS);
193 
194         // Transit state
195         final DerivativeStructure   delta        = downlinkDateDS.durationFrom(state.getDate());
196         final DerivativeStructure   deltaMTauD   = tauD.negate().add(delta);
197         final SpacecraftState       transitState = state.shiftedBy(deltaMTauD.getValue());
198 
199         // Transit state (re)computed with derivative structures
200         final TimeStampedFieldPVCoordinates<DerivativeStructure> transitPV = pvaDS.shiftedBy(deltaMTauD);
201 
202         // one-way (downlink) range-rate
203         final EstimatedMeasurement<RangeRate> evalOneWay1 =
204                         oneWayTheoreticalEvaluation(iteration, evaluation, true,
205                                                     stationDownlink, transitPV, transitState, indices);
206         final EstimatedMeasurement<RangeRate> estimated;
207         if (twoway) {
208             // one-way (uplink) light time correction
209             final FieldTransform<DerivativeStructure> offsetToInertialApproxUplink =
210                             station.getOffsetToInertial(state.getFrame(),
211                                                         downlinkDateDS.shiftedBy(tauD.multiply(-2)), factory, indices);
212             final FieldAbsoluteDate<DerivativeStructure> approxUplinkDateDS =
213                             offsetToInertialApproxUplink.getFieldDate();
214 
215             final TimeStampedFieldPVCoordinates<DerivativeStructure> stationApproxUplink =
216                             offsetToInertialApproxUplink.transformPVCoordinates(new TimeStampedFieldPVCoordinates<>(approxUplinkDateDS,
217                                                                                                                     zero, zero, zero));
218 
219             final DerivativeStructure tauU = signalTimeOfFlight(stationApproxUplink, transitPV.getPosition(), transitPV.getDate());
220 
221             final TimeStampedFieldPVCoordinates<DerivativeStructure> stationUplink =
222                             stationApproxUplink.shiftedBy(transitPV.getDate().durationFrom(approxUplinkDateDS).subtract(tauU));
223 
224             final EstimatedMeasurement<RangeRate> evalOneWay2 =
225                             oneWayTheoreticalEvaluation(iteration, evaluation, false,
226                                                         stationUplink, transitPV, transitState, indices);
227 
228             // combine uplink and downlink values
229             estimated = new EstimatedMeasurement<>(this, iteration, evaluation,
230                                                    evalOneWay1.getStates(),
231                                                    new TimeStampedPVCoordinates[] {
232                                                        evalOneWay2.getParticipants()[0],
233                                                        evalOneWay1.getParticipants()[0],
234                                                        evalOneWay1.getParticipants()[1]
235                                                    });
236             estimated.setEstimatedValue(0.5 * (evalOneWay1.getEstimatedValue()[0] + evalOneWay2.getEstimatedValue()[0]));
237 
238             // combine uplink and downlink partial derivatives with respect to state
239             final double[][] sd1 = evalOneWay1.getStateDerivatives(0);
240             final double[][] sd2 = evalOneWay2.getStateDerivatives(0);
241             final double[][] sd = new double[sd1.length][sd1[0].length];
242             for (int i = 0; i < sd.length; ++i) {
243                 for (int j = 0; j < sd[0].length; ++j) {
244                     sd[i][j] = 0.5 * (sd1[i][j] + sd2[i][j]);
245                 }
246             }
247             estimated.setStateDerivatives(0, sd);
248 
249             // combine uplink and downlink partial derivatives with respect to parameters
250             evalOneWay1.getDerivativesDrivers().forEach(driver -> {
251                 final double[] pd1 = evalOneWay1.getParameterDerivatives(driver);
252                 final double[] pd2 = evalOneWay2.getParameterDerivatives(driver);
253                 final double[] pd = new double[pd1.length];
254                 for (int i = 0; i < pd.length; ++i) {
255                     pd[i] = 0.5 * (pd1[i] + pd2[i]);
256                 }
257                 estimated.setParameterDerivatives(driver, pd);
258             });
259 
260         } else {
261             estimated = evalOneWay1;
262         }
263 
264         return estimated;
265 
266     }
267 
268     /** Evaluate measurement in one-way.
269      * @param iteration iteration number
270      * @param evaluation evaluations counter
271      * @param downlink indicator for downlink leg
272      * @param stationPV station coordinates when signal is at station
273      * @param transitPV spacecraft coordinates at onboard signal transit
274      * @param transitState orbital state at onboard signal transit
275      * @param indices indices of the estimated parameters in derivatives computations
276      * @return theoretical value
277      * @see #evaluate(SpacecraftStatet)
278      */
279     private EstimatedMeasurement<RangeRate> oneWayTheoreticalEvaluation(final int iteration, final int evaluation, final boolean downlink,
280                                                                         final TimeStampedFieldPVCoordinates<DerivativeStructure> stationPV,
281                                                                         final TimeStampedFieldPVCoordinates<DerivativeStructure> transitPV,
282                                                                         final SpacecraftState transitState,
283                                                                         final Map<String, Integer> indices) {
284 
285         // prepare the evaluation
286         final EstimatedMeasurement<RangeRate> estimated =
287                         new EstimatedMeasurement<RangeRate>(this, iteration, evaluation,
288                                                             new SpacecraftState[] {
289                                                                 transitState
290                                                             }, new TimeStampedPVCoordinates[] {
291                                                                 (downlink ? transitPV : stationPV).toTimeStampedPVCoordinates(),
292                                                                 (downlink ? stationPV : transitPV).toTimeStampedPVCoordinates()
293                                                             });
294 
295         // range rate value
296         final FieldVector3D<DerivativeStructure> stationPosition  = stationPV.getPosition();
297         final FieldVector3D<DerivativeStructure> relativePosition = stationPosition.subtract(transitPV.getPosition());
298 
299         final FieldVector3D<DerivativeStructure> stationVelocity  = stationPV.getVelocity();
300         final FieldVector3D<DerivativeStructure> relativeVelocity = stationVelocity.subtract(transitPV.getVelocity());
301 
302         // radial direction
303         final FieldVector3D<DerivativeStructure> lineOfSight      = relativePosition.normalize();
304 
305         // range rate
306         final DerivativeStructure rangeRate = FieldVector3D.dotProduct(relativeVelocity, lineOfSight);
307 
308         estimated.setEstimatedValue(rangeRate.getValue());
309 
310         // compute partial derivatives of (rr) with respect to spacecraft state Cartesian coordinates
311         final double[] derivatives = rangeRate.getAllDerivatives();
312         estimated.setStateDerivatives(0, Arrays.copyOfRange(derivatives, 1, 7));
313 
314         // set partial derivatives with respect to parameters
315         // (beware element at index 0 is the value, not a derivative)
316         for (final ParameterDriver driver : getParametersDrivers()) {
317             final Integer index = indices.get(driver.getName());
318             if (index != null) {
319                 estimated.setParameterDerivatives(driver, derivatives[index + 1]);
320             }
321         }
322 
323         return estimated;
324 
325     }
326 
327 }