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.Constants;
32  import org.orekit.utils.ParameterDriver;
33  import org.orekit.utils.TimeStampedFieldPVCoordinates;
34  import org.orekit.utils.TimeStampedPVCoordinates;
35  
36  /** Class modeling a range measurement from a ground station.
37   * <p>
38   * For one-way measurements, a signal is emitted by the satellite
39   * and received by the ground station. The measurement value is the
40   * elapsed time between emission and reception multiplied by c where
41   * c is the speed of light.
42   * </p>
43   * <p>
44   * For two-way measurements, the measurement is considered to be a signal
45   * emitted from a ground station, reflected on spacecraft, and received
46   * on the same ground station. Its value is the elapsed time between
47   * emission and reception multiplied by c/2 where c is the speed of light.
48   * </p>
49   * <p>
50   * The motion of both the station and the spacecraft during the signal
51   * flight time are taken into account. The date of the measurement
52   * corresponds to the reception on ground of the emitted or reflected signal.
53   * </p>
54   * <p>
55   * The clock offsets of both the ground station and the satellite are taken
56   * into account. These offsets correspond to the values that must be subtracted
57   * from station (resp. satellite) reading of time to compute the real physical
58   * date. These offsets have two effects:
59   * </p>
60   * <ul>
61   *   <li>as measurement date is evaluated at reception time, the real physical date
62   *   of the measurement is the observed date to which the receiving ground station
63   *   clock offset is subtracted</li>
64   *   <li>as range is evaluated using the total signal time of flight, for one-way
65   *   measurements the observed range is the real physical signal time of flight to
66   *   which (Δtg - Δts) ⨉ c is added, where Δtg (resp. Δts) is the clock offset for the
67   *   receiving ground station (resp. emitting satellite). A similar effect exists in
68   *   two-way measurements but it is computed as (Δtg - Δtg) ⨉ c / 2 as the same ground
69   *   station clock is used for initial emission and final reception and therefore it evaluates
70   *   to zero.</li>
71   * </ul>
72   * <p>
73   * @author Thierry Ceolin
74   * @author Luc Maisonobe
75   * @author Maxime Journot
76   * @since 8.0
77   */
78  public class Range extends AbstractMeasurement<Range> {
79  
80      /** Ground station from which measurement is performed. */
81      private final GroundStation station;
82  
83      /** Flag indicating whether it is a two-way measurement. */
84      private final boolean twoway;
85  
86      /** Simple constructor.
87       * <p>
88       * This constructor uses 0 as the index of the propagator related
89       * to this measurement, thus being well suited for mono-satellite
90       * orbit determination.
91       * </p>
92       * @param station ground station from which measurement is performed
93       * @param date date of the measurement
94       * @param range observed value
95       * @param sigma theoretical standard deviation
96       * @param baseWeight base weight
97       * @deprecated as of 9.3, replaced by {@link #Range(GroundStation, boolean, AbsoluteDate,
98       * double, double, double, ObservableSatellite)}
99       */
100     @Deprecated
101     public Range(final GroundStation station, final AbsoluteDate date,
102                  final double range, final double sigma, final double baseWeight) {
103         this(station, true, date, range, sigma, baseWeight, new ObservableSatellite(0));
104     }
105 
106     /** Simple constructor.
107      * <p>
108      * This constructor uses 0 as the index of the propagator related
109      * to this measurement, thus being well suited for mono-satellite
110      * orbit determination.
111      * </p>
112      * @param station ground station from which measurement is performed
113      * @param date date of the measurement
114      * @param range observed value
115      * @param sigma theoretical standard deviation
116      * @param baseWeight base weight
117      * @param twoWay flag indicating whether it is a two-way measurement
118      * @deprecated as of 9.3, replaced by {@link #Range(GroundStation, boolean, AbsoluteDate,
119      * double, double, double, ObservableSatellite)}
120      */
121     @Deprecated
122     public Range(final GroundStation station, final AbsoluteDate date, final double range,
123                  final double sigma, final double baseWeight, final boolean twoWay) {
124         this(station, twoWay, date, range, sigma, baseWeight, new ObservableSatellite(0));
125     }
126 
127     /** Simple constructor.
128      * @param station ground station from which measurement is performed
129      * @param date date of the measurement
130      * @param range observed value
131      * @param sigma theoretical standard deviation
132      * @param baseWeight base weight
133      * @param propagatorIndex index of the propagator related to this measurement
134      * @deprecated as of 9.3, replaced by {@link #Range(GroundStation, boolean, AbsoluteDate,
135      * double, double, double, ObservableSatellite)}
136      */
137     @Deprecated
138     public Range(final GroundStation station, final AbsoluteDate date,
139                  final double range, final double sigma, final double baseWeight,
140                  final int propagatorIndex) {
141         this(station, true, date, range, sigma, baseWeight, new ObservableSatellite(0));
142     }
143 
144     /** Simple constructor.
145      * @param station ground station from which measurement is performed
146      * @param twoWay flag indicating whether it is a two-way measurement
147      * @param date date of the measurement
148      * @param range observed value
149      * @param sigma theoretical standard deviation
150      * @param baseWeight base weight
151      * @param propagatorIndex index of the propagator related to this measurement
152      * @since 9.0
153      * @deprecated as of 9.3, replaced by {@link #Range(GroundStation, boolean, AbsoluteDate,
154      * double, double, double, ObservableSatellite)}
155      */
156     @Deprecated
157     public Range(final GroundStation station, final boolean twoWay, final AbsoluteDate date,
158                  final double range, final double sigma, final double baseWeight,
159                  final int propagatorIndex) {
160         this(station, twoWay, date, range, sigma, baseWeight, new ObservableSatellite(propagatorIndex));
161     }
162 
163     /** Simple constructor.
164      * @param station ground station from which measurement is performed
165      * @param twoWay flag indicating whether it is a two-way measurement
166      * @param date date of the measurement
167      * @param range observed value
168      * @param sigma theoretical standard deviation
169      * @param baseWeight base weight
170      * @param satellite satellite related to this measurement
171      * @since 9.3
172      */
173     public Range(final GroundStation station, final boolean twoWay, final AbsoluteDate date,
174                  final double range, final double sigma, final double baseWeight,
175                  final ObservableSatellite satellite) {
176         super(date, range, sigma, baseWeight, Arrays.asList(satellite));
177         addParameterDriver(station.getClockOffsetDriver());
178         addParameterDriver(station.getEastOffsetDriver());
179         addParameterDriver(station.getNorthOffsetDriver());
180         addParameterDriver(station.getZenithOffsetDriver());
181         addParameterDriver(station.getPrimeMeridianOffsetDriver());
182         addParameterDriver(station.getPrimeMeridianDriftDriver());
183         addParameterDriver(station.getPolarOffsetXDriver());
184         addParameterDriver(station.getPolarDriftXDriver());
185         addParameterDriver(station.getPolarOffsetYDriver());
186         addParameterDriver(station.getPolarDriftYDriver());
187         if (!twoWay) {
188             // for one way measurements, the satellite clock offset affects the measurement
189             addParameterDriver(satellite.getClockOffsetDriver());
190         }
191         this.station = station;
192         this.twoway = twoWay;
193     }
194 
195     /** Get the ground station from which measurement is performed.
196      * @return ground station from which measurement is performed
197      */
198     public GroundStation getStation() {
199         return station;
200     }
201 
202     /** Check if the instance represents a two-way measurement.
203      * @return true if the instance represents a two-way measurement
204      */
205     public boolean isTwoWay() {
206         return twoway;
207     }
208 
209     /** {@inheritDoc} */
210     @Override
211     protected EstimatedMeasurement<Range> theoreticalEvaluation(final int iteration,
212                                                                 final int evaluation,
213                                                                 final SpacecraftState[] states) {
214 
215         final ObservableSatellite satellite = getSatellites().get(0);
216         final SpacecraftState     state     = states[satellite.getPropagatorIndex()];
217 
218         // Range derivatives are computed with respect to spacecraft state in inertial frame
219         // and station parameters
220         // ----------------------
221         //
222         // Parameters:
223         //  - 0..2 - Position of the spacecraft in inertial frame
224         //  - 3..5 - Velocity of the spacecraft in inertial frame
225         //  - 6..n - measurements parameters (clock offset, station offsets, pole, prime meridian, sat clock offset...)
226         int nbParams = 6;
227         final Map<String, Integer> indices = new HashMap<>();
228         for (ParameterDriver driver : getParametersDrivers()) {
229             if (driver.isSelected()) {
230                 indices.put(driver.getName(), nbParams++);
231             }
232         }
233         final DSFactory                          factory = new DSFactory(nbParams, 1);
234         final Field<DerivativeStructure>         field   = factory.getDerivativeField();
235         final FieldVector3D<DerivativeStructure> zero    = FieldVector3D.getZero(field);
236 
237         // Coordinates of the spacecraft expressed as a derivative structure
238         final TimeStampedFieldPVCoordinates<DerivativeStructure> pvaDS = getCoordinates(state, 0, factory);
239 
240         // transform between station and inertial frame, expressed as a derivative structure
241         // The components of station's position in offset frame are the 3 last derivative parameters
242         final FieldTransform<DerivativeStructure> offsetToInertialDownlink =
243                         station.getOffsetToInertial(state.getFrame(), getDate(), factory, indices);
244         final FieldAbsoluteDate<DerivativeStructure> downlinkDateDS = offsetToInertialDownlink.getFieldDate();
245 
246         // Station position in inertial frame at end of the downlink leg
247         final TimeStampedFieldPVCoordinates<DerivativeStructure> stationDownlink =
248                         offsetToInertialDownlink.transformPVCoordinates(new TimeStampedFieldPVCoordinates<>(downlinkDateDS,
249                                                                                                             zero, zero, zero));
250 
251         // Compute propagation times
252         // (if state has already been set up to pre-compensate propagation delay,
253         //  we will have delta == tauD and transitState will be the same as state)
254 
255         // Downlink delay
256         final DerivativeStructure tauD = signalTimeOfFlight(pvaDS, stationDownlink.getPosition(), downlinkDateDS);
257 
258         // Transit state & Transit state (re)computed with derivative structures
259         final DerivativeStructure   delta        = downlinkDateDS.durationFrom(state.getDate());
260         final DerivativeStructure   deltaMTauD   = tauD.negate().add(delta);
261         final SpacecraftState       transitState = state.shiftedBy(deltaMTauD.getValue());
262         final TimeStampedFieldPVCoordinates<DerivativeStructure> transitStateDS = pvaDS.shiftedBy(deltaMTauD);
263 
264         // prepare the evaluation
265         final EstimatedMeasurement<Range> estimated;
266         final DerivativeStructure range;
267 
268         if (twoway) {
269 
270             // Station at transit state date (derivatives of tauD taken into account)
271             final TimeStampedFieldPVCoordinates<DerivativeStructure> stationAtTransitDate =
272                             stationDownlink.shiftedBy(tauD.negate());
273             // Uplink delay
274             final DerivativeStructure tauU =
275                             signalTimeOfFlight(stationAtTransitDate, transitStateDS.getPosition(), transitStateDS.getDate());
276             final TimeStampedFieldPVCoordinates<DerivativeStructure> stationUplink =
277                             stationDownlink.shiftedBy(-tauD.getValue() - tauU.getValue());
278 
279             // Prepare the evaluation
280             estimated = new EstimatedMeasurement<Range>(this, iteration, evaluation,
281                                                             new SpacecraftState[] {
282                                                                 transitState
283                                                             }, new TimeStampedPVCoordinates[] {
284                                                                 stationUplink.toTimeStampedPVCoordinates(),
285                                                                 transitStateDS.toTimeStampedPVCoordinates(),
286                                                                 stationDownlink.toTimeStampedPVCoordinates()
287                                                             });
288 
289             // Range value
290             final double              cOver2 = 0.5 * Constants.SPEED_OF_LIGHT;
291             final DerivativeStructure tau    = tauD.add(tauU);
292             range                            = tau.multiply(cOver2);
293 
294         } else {
295 
296             estimated = new EstimatedMeasurement<Range>(this, iteration, evaluation,
297                             new SpacecraftState[] {
298                                 transitState
299                             }, new TimeStampedPVCoordinates[] {
300                                 transitStateDS.toTimeStampedPVCoordinates(),
301                                 stationDownlink.toTimeStampedPVCoordinates()
302                             });
303 
304             // Clock offsets
305             final DerivativeStructure dtg = station.getClockOffsetDriver().getValue(factory, indices);
306             final DerivativeStructure dts = satellite.getClockOffsetDriver().getValue(factory, indices);
307 
308             // Range value
309             range = tauD.add(dtg).subtract(dts).multiply(Constants.SPEED_OF_LIGHT);
310 
311         }
312 
313         estimated.setEstimatedValue(range.getValue());
314 
315         // Range partial derivatives with respect to state
316         final double[] derivatives = range.getAllDerivatives();
317         estimated.setStateDerivatives(0, Arrays.copyOfRange(derivatives, 1, 7));
318 
319         // set partial derivatives with respect to parameters
320         // (beware element at index 0 is the value, not a derivative)
321         for (final ParameterDriver driver : getParametersDrivers()) {
322             final Integer index = indices.get(driver.getName());
323             if (index != null) {
324                 estimated.setParameterDerivatives(driver, derivatives[index + 1]);
325             }
326         }
327 
328         return estimated;
329 
330     }
331 
332 }