1   /* Copyright 2025-2026 Hawkeye 360 (HE360)
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;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.hipparchus.analysis.differentiation.Gradient;
24  import org.hipparchus.analysis.differentiation.GradientField;
25  import org.orekit.frames.FieldTransform;
26  import org.orekit.frames.Frame;
27  import org.orekit.frames.Transform;
28  import org.orekit.propagation.SpacecraftState;
29  import org.orekit.time.AbsoluteDate;
30  import org.orekit.time.FieldAbsoluteDate;
31  import org.orekit.time.clocks.ClockOffset;
32  import org.orekit.time.clocks.FieldClockOffset;
33  import org.orekit.time.clocks.QuadraticFieldClockModel;
34  import org.orekit.utils.FieldPVCoordinatesProvider;
35  import org.orekit.utils.PVCoordinatesProvider;
36  import org.orekit.utils.ParameterDriver;
37  import org.orekit.utils.TimeSpanMap.Span;
38  
39  /** Abstract interface that contains those methods necessary
40   *  for both space and ground-based satellite observers.
41   *
42   * @author Brianna Aubin
43   * @since 14.0
44   */
45  public interface Observer extends MeasurementParticipant {
46  
47      /** Return the PVCoordinatesProvider.
48       * @return pos/vel coordinates provider
49       */
50      PVCoordinatesProvider getPVCoordinatesProvider();
51  
52      /** Return the FieldPVCoordinatesProvider.
53       * @param freeParameters number of estimated parameters
54       * @param parameterIndices indices of the estimated parameters in derivatives computations, must be driver
55       * @return pos/vel coordinates provider for values with Gradient field
56       */
57      FieldPVCoordinatesProvider<Gradient> getFieldPVCoordinatesProvider(int freeParameters,
58                                                                         Map<String, Integer> parameterIndices);
59  
60      /** Get the transform between offset frame and inertial frame.
61       * <p>
62       * The offset frame takes the <em>current</em> position offset,
63       * polar motion and the meridian shift into account. The frame
64       * returned is disconnected from later changes in the parameters.
65       * When the {@link ParameterDriver parameters} managing these
66       * offsets are changed, the method must be called again to retrieve
67       * a new offset frame.
68       * </p>
69       * @param inertial inertial frame to transform to
70       * @param date date of the transform
71       * @param clockOffsetAlreadyApplied if true, the specified {@code date} is as read
72       * by the ground station clock (i.e. clock offset <em>not</em> compensated), if false,
73       * the specified {@code date} was already compensated and is a physical absolute date
74       * @return transform between offset frame and inertial frame, at <em>real</em> measurement
75       * date (i.e. with clock, Earth and station offsets applied)
76       */
77      Transform getOffsetToInertial(Frame inertial, AbsoluteDate date, boolean clockOffsetAlreadyApplied);
78  
79      /** Get the transform between offset frame and inertial frame with derivatives.
80       * <p>
81       * As the East and North vectors are not well defined at pole, the derivatives
82       * of these two vectors diverge to infinity as we get closer to the pole.
83       * So this method should not be used for stations less than 0.0001 degree from
84       * either poles.
85       * </p>
86       * @param inertial inertial frame to transform to
87       * @param clockDate date of the transform, clock offset and its derivatives already compensated
88       * @param freeParameters total number of free parameters in the gradient
89       * @param indices indices of the estimated parameters in derivatives computations, must be driver
90       * span name in map, not driver name or will not give right results (see {@link ParameterDriver#getValue(int, Map)})
91       * @return transform between offset frame and inertial frame, at specified date
92       */
93      default FieldTransform<Gradient> getOffsetToInertial(final Frame inertial,
94                                                           final AbsoluteDate clockDate,
95                                                           final int freeParameters,
96                                                           final Map<String, Integer> indices) {
97          // take clock offset into account
98          final Gradient offset = getFieldOffsetValue(freeParameters, clockDate, indices);
99          final FieldAbsoluteDate<Gradient> offsetCompensatedDate = new FieldAbsoluteDate<>(clockDate, offset.negate());
100 
101         return getOffsetToInertial(inertial, offsetCompensatedDate, freeParameters, indices);
102     }
103 
104     /** Get the transform between offset frame and inertial frame with derivatives.
105      * <p>
106      * As the East and North vectors are not well defined at pole, the derivatives
107      * of these two vectors diverge to infinity as we get closer to the pole.
108      * So this method should not be used for stations less than 0.0001 degree from
109      * either poles.
110      * </p>
111      * @param inertial inertial frame to transform to
112      * @param offsetCompensatedDate date of the transform, clock offset and its derivatives already compensated
113      * @param freeParameters total number of free parameters in the gradient
114      * @param indices indices of the estimated parameters in derivatives computations, must be driver
115      * span name in map, not driver name or will not give right results (see {@link ParameterDriver#getValue(int, Map)})
116      * @return transform between offset frame and inertial frame, at specified date
117      */
118     FieldTransform<Gradient> getOffsetToInertial(Frame inertial, FieldAbsoluteDate<Gradient> offsetCompensatedDate,
119                                                  int freeParameters, Map<String, Integer> indices);
120 
121     /** Create a map of the free parameter values.
122      * @param states list of ObservableSatellite measurement states
123      * @param parameterDrivers list of all parameter values for the measurement
124      * @return map of the free parameter values
125      */
126     static Map<String, Integer> getParameterIndices(final SpacecraftState[] states,
127                                                     final List<ParameterDriver> parameterDrivers) {
128 
129         // measurement derivatives are computed with respect to spacecraft state in inertial frame
130         // Parameters:
131         //  - 6k..6k+2 - Position of spacecraft k (counting k from 0 to nbSat-1) in inertial frame
132         //  - 6k+3..6k+5 - Velocity of spacecraft k (counting k from 0 to nbSat-1) in inertial frame
133         //  - 6nbSat..n - measurements parameters (clock offset, etc)
134         int nbParams = 6 * states.length;
135         final Map<String, Integer> paramIndices = new HashMap<>();
136         for (ParameterDriver measurementDriver : parameterDrivers) {
137             if (measurementDriver.isSelected()) {
138                 for (Span<String> span = measurementDriver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
139                     paramIndices.put(span.getData(), nbParams++);
140                 }
141             }
142         }
143         return paramIndices;
144     }
145 
146     /**
147      * Compute actual date taking into account clock offset.
148      * @param date date as registered by observer
149      * @return corrected date
150      */
151     default AbsoluteDate getCorrectedReceptionDate(final AbsoluteDate date) {
152         final ClockOffset localClock = getQuadraticClockModel().getOffset(date);
153         return date.shiftedBy(-localClock.getBias());
154     }
155 
156     /**
157      * Compute actual date taking into account clock offset.
158      * @param date date as registered by observer
159      * @param nbParams number of independent variables for automatic differentiation
160      * @param paramIndices mapping between parameter name and variable index
161      * @return corrected date
162      */
163     default FieldAbsoluteDate<Gradient> getCorrectedReceptionDateField(final AbsoluteDate date,
164                                                                        final int nbParams,
165                                                                        final Map<String, Integer> paramIndices) {
166         final QuadraticFieldClockModel<Gradient> quadraticClockModel = getQuadraticFieldClock(nbParams, date, paramIndices);
167         final GradientField field = GradientField.getField(nbParams);
168         final FieldAbsoluteDate<Gradient> fieldDate = new FieldAbsoluteDate<>(field, date);
169         final FieldClockOffset<Gradient> localClock = quadraticClockModel.getOffset(fieldDate);
170         return fieldDate.shiftedBy(localClock.getBias().negate());
171     }
172 
173 }