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.gnss;
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.GroundReceiverCommonParametersWithDerivatives;
25  import org.orekit.estimation.measurements.GroundReceiverCommonParametersWithoutDerivatives;
26  import org.orekit.estimation.measurements.GroundReceiverMeasurement;
27  import org.orekit.estimation.measurements.GroundStation;
28  import org.orekit.estimation.measurements.ObservableSatellite;
29  import org.orekit.propagation.SpacecraftState;
30  import org.orekit.time.AbsoluteDate;
31  import org.orekit.utils.Constants;
32  import org.orekit.utils.ParameterDriver;
33  import org.orekit.utils.TimeSpanMap.Span;
34  import org.orekit.utils.TimeStampedPVCoordinates;
35  
36  /** Class modeling a phase measurement from a ground station.
37   * <p>
38   * The measurement is considered to be a signal emitted from
39   * a spacecraft and received on a ground station.
40   * Its value is the number of cycles between emission and
41   * reception. The motion of both the station and the
42   * spacecraft during the signal flight time are taken into
43   * account. The date of the measurement corresponds to the
44   * reception on ground of the emitted signal.
45   * </p>
46   * @author Thierry Ceolin
47   * @author Luc Maisonobe
48   * @author Maxime Journot
49   * @since 9.2
50   */
51  public class Phase extends GroundReceiverMeasurement<Phase> {
52  
53      /** Type of the measurement. */
54      public static final String MEASUREMENT_TYPE = "Phase";
55  
56      /** Driver for ambiguity. */
57      private final AmbiguityDriver ambiguityDriver;
58  
59      /** Wavelength of the phase observed value [m]. */
60      private final double wavelength;
61  
62      /** Simple constructor.
63       * @param station ground station from which measurement is performed
64       * @param date date of the measurement
65       * @param phase observed value (cycles)
66       * @param wavelength phase observed value wavelength (m)
67       * @param sigma theoretical standard deviation
68       * @param baseWeight base weight
69       * @param satellite satellite related to this measurement
70       * @param cache from which ambiguity drive should come
71       * @since 12.1
72       */
73      public Phase(final GroundStation station, final AbsoluteDate date,
74                   final double phase, final double wavelength, final double sigma,
75                   final double baseWeight, final ObservableSatellite satellite,
76                   final AmbiguityCache cache) {
77          super(station, false, date, phase, sigma, baseWeight, satellite);
78          ambiguityDriver = cache.getAmbiguity(satellite.getName(),
79                                               station.getBaseFrame().getName(),
80                                               wavelength);
81          addParameterDriver(ambiguityDriver);
82          this.wavelength = wavelength;
83      }
84  
85      /** Get the wavelength.
86       * @return wavelength (m)
87       */
88      public double getWavelength() {
89          return wavelength;
90      }
91  
92      /** Get the driver for phase ambiguity.
93       * @return the driver for phase ambiguity
94       * @since 10.3
95       */
96      public AmbiguityDriver getAmbiguityDriver() {
97          return ambiguityDriver;
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     protected EstimatedMeasurementBase<Phase> theoreticalEvaluationWithoutDerivatives(final int iteration,
103                                                                                       final int evaluation,
104                                                                                       final SpacecraftState[] states) {
105 
106         final GroundReceiverCommonParametersWithoutDerivatives common = computeCommonParametersWithout(states[0]);
107 
108         // prepare the evaluation
109         final EstimatedMeasurementBase<Phase> estimated =
110                         new EstimatedMeasurementBase<>(this, iteration, evaluation,
111                                                        new SpacecraftState[] {
112                                                            common.getTransitState()
113                                                        }, new TimeStampedPVCoordinates[] {
114                                                            common.getTransitPV(),
115                                                            common.getStationDownlink()
116                                                        });
117 
118         // Clock offsets
119         final ObservableSatellite satellite = getSatellites().get(0);
120         final double              dts       = satellite.getClockOffsetDriver().getValue(common.getState().getDate());
121         final double              dtg       = getStation().getClockOffsetDriver().getValue(getDate());
122 
123         // Phase value
124         final double cOverLambda = Constants.SPEED_OF_LIGHT / wavelength;
125         final double ambiguity   = ambiguityDriver.getValue(common.getState().getDate());
126         final double phase       = (common.getTauD() + dtg - dts) * cOverLambda + ambiguity;
127 
128         estimated.setEstimatedValue(phase);
129 
130         return estimated;
131 
132     }
133 
134     /** {@inheritDoc} */
135     @Override
136     protected EstimatedMeasurement<Phase> theoreticalEvaluation(final int iteration,
137                                                                 final int evaluation,
138                                                                 final SpacecraftState[] states) {
139 
140         final SpacecraftState state = states[0];
141 
142         // Phase derivatives are computed with respect to spacecraft state in inertial frame
143         // and station parameters
144         // ----------------------
145         //
146         // Parameters:
147         //  - 0..2 - Position of the spacecraft in inertial frame
148         //  - 3..5 - Velocity of the spacecraft in inertial frame
149         //  - 6..n - station parameters (ambiguity, clock offset, station offsets, pole, prime meridian...)
150         final GroundReceiverCommonParametersWithDerivatives common = computeCommonParametersWithDerivatives(state);
151         final int nbParams = common.getTauD().getFreeParameters();
152 
153         // prepare the evaluation
154         final EstimatedMeasurement<Phase> estimated =
155                         new EstimatedMeasurement<>(this, iteration, evaluation,
156                                                    new SpacecraftState[] {
157                                                        common.getTransitState()
158                                                    }, new TimeStampedPVCoordinates[] {
159                                                        common.getTransitPV().toTimeStampedPVCoordinates(),
160                                                        common.getStationDownlink().toTimeStampedPVCoordinates()
161                                                    });
162 
163         // Clock offsets
164         final ObservableSatellite satellite = getSatellites().get(0);
165         final Gradient            dts       = satellite.getClockOffsetDriver().getValue(nbParams, common.getIndices(), state.getDate());
166         final Gradient            dtg       = getStation().getClockOffsetDriver().getValue(nbParams, common.getIndices(), getDate());
167 
168         // Phase value
169         final double   cOverLambda = Constants.SPEED_OF_LIGHT / wavelength;
170         final Gradient ambiguity   = ambiguityDriver.getValue(nbParams, common.getIndices(), state.getDate());
171         final Gradient phase       = common.getTauD().add(dtg).subtract(dts).multiply(cOverLambda).add(ambiguity);
172 
173         estimated.setEstimatedValue(phase.getValue());
174 
175         // Phase first order derivatives with respect to state
176         final double[] derivatives = phase.getGradient();
177         estimated.setStateDerivatives(0, Arrays.copyOfRange(derivatives, 0, 6));
178 
179         // Set first order derivatives with respect to parameters
180         for (final ParameterDriver driver : getParametersDrivers()) {
181             for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
182 
183                 final Integer index = common.getIndices().get(span.getData());
184                 if (index != null) {
185                     estimated.setParameterDerivatives(driver, span.getStart(), derivatives[index]);
186                 }
187             }
188         }
189 
190         return estimated;
191 
192     }
193 
194 }