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  
18  package org.orekit.estimation.measurements;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.hipparchus.Field;
25  import org.hipparchus.analysis.differentiation.Gradient;
26  import org.hipparchus.util.FastMath;
27  import org.orekit.orbits.CartesianOrbit;
28  import org.orekit.orbits.FieldCartesianOrbit;
29  import org.orekit.propagation.SpacecraftState;
30  import org.orekit.time.AbsoluteDate;
31  import org.orekit.time.clocks.ClockModel;
32  import org.orekit.time.clocks.PolynomialClockModel;
33  import org.orekit.utils.AbsolutePVCoordinates;
34  import org.orekit.utils.FieldAbsolutePVCoordinates;
35  import org.orekit.utils.FieldPVCoordinatesProvider;
36  import org.orekit.utils.PVCoordinatesProvider;
37  import org.orekit.utils.ParameterDriver;
38  import org.orekit.utils.TimeStampedFieldPVCoordinates;
39  import org.orekit.utils.TimeStampedPVCoordinates;
40  
41  /** Abstract class underlying both observed and observing measurement
42   * objects.  Contains the ClockModel and the ability to store a
43   * master list of all parameter drivers associated with the object.
44   *
45   * @author Brianna Aubin
46   * @since 14.0
47   */
48  public abstract class AbstractParticipant implements MeasurementParticipant {
49  
50      /** Checkstyle is annoying sometimes. */
51      private static final String CLOCK_STRING = "-clock";
52  
53      /** Clock offset scaling factor.
54       * <p>
55       * We use a power of 2 to avoid numeric noise introduction
56       * in the multiplications/divisions sequences.
57       * </p>
58       */
59      private static final double CLOCK_OFFSET_SCALE = FastMath.scalb(1.0, -10);
60  
61      /** Stores clock model. */
62      private final ClockModel clockModel;
63  
64      /** Stores list of all ParameterDriver values. */
65      private final List<ParameterDriver> parameterDrivers = new ArrayList<>();
66  
67      /** Name of the satellite. */
68      private final String name;
69  
70      /** Simple constructor.
71       * @param name name of MeasurementObject
72       */
73      protected AbstractParticipant(final String name) {
74          this(name, createEmptyPolynomialClock(name));
75      }
76  
77      /** Simple constructor.
78       * @param name name of MeasurementObject
79       * @param clock clock belonging to MeasurementObject
80       */
81      protected AbstractParticipant(final String name, final ClockModel clock) {
82  
83          // Initialize member variables
84          this.name = name;
85          this.clockModel = clock;
86  
87          for (ParameterDriver parameter: clock.getParametersDrivers()) {
88              parameterDrivers.add(parameter);
89          }
90      }
91  
92      /** Get the MeasurementObject name.
93       * @return name for the object
94       */
95      @Override
96      public final String getName() {
97          return name;
98      }
99  
100     /** Creates a polynomial clock with zero displacement.
101      * @param name name of object that is holding the clock
102      * @return new polynomial clock model
103      */
104     protected static PolynomialClockModel createEmptyPolynomialClock(final String name) {
105         return new PolynomialClockModel(new ParameterDriver(name + CLOCK_STRING + BIAS_SUFFIX,
106                                                     0.0, CLOCK_OFFSET_SCALE,
107                                                     Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY),
108                                            new ParameterDriver(name + CLOCK_STRING + DRIFT_SUFFIX,
109                                                     0.0, CLOCK_OFFSET_SCALE,
110                                                     Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY),
111                                            new ParameterDriver(name + CLOCK_STRING + ACCELERATION_SUFFIX,
112                                                     0.0, CLOCK_OFFSET_SCALE,
113                                                     Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));
114     }
115 
116     /** Get the clock model valid at some date.
117      * @return clock model
118      */
119     @Override
120     public final ClockModel getClockModel() {
121         return clockModel;
122     }
123 
124     /** Get model parameters.
125      * @return model parameters, will throw an
126      * exception if one PDriver has several values driven. If
127      * it's the case (if at least 1 PDriver of the model has several values
128      * driven) the method {@link org.orekit.utils.ParameterDriversProvider#getParameters(AbsoluteDate)} must be used.
129      */
130     @Override
131     public List<ParameterDriver> getParametersDrivers() {
132         return Collections.unmodifiableList(parameterDrivers);
133     }
134 
135     /**
136      * Add a single parameter.
137      * @param parameterDriver parameter being added to the MeasurementObject
138      */
139     protected final void addParameterDriver(final ParameterDriver parameterDriver) {
140         parameterDrivers.add(parameterDriver);
141     }
142 
143     /**
144      * Create PV provider from position-velocity-acceleration vector and template state.
145      * @param templateState template state
146      * @param pvCoordinates position-velocity-acceleration
147      * @return position-velocity-acceleration provider
148      */
149     public static PVCoordinatesProvider extractPVCoordinatesProvider(final SpacecraftState templateState,
150                                                                      final TimeStampedPVCoordinates pvCoordinates) {
151         if (templateState.isOrbitDefined()) {
152             final CartesianOrbit cartesianOrbit = new CartesianOrbit(pvCoordinates, templateState.getFrame(),
153                     templateState.getOrbit().getMu());
154             return templateState.getOrbit().getType().convertType(cartesianOrbit);
155         } else {
156             return new AbsolutePVCoordinates(templateState.getFrame(), pvCoordinates);
157         }
158     }
159 
160     /**
161      * Create PV provider from position-velocity-acceleration vector and template state.
162      *
163      * @param templateState template state
164      * @param pvCoordinates position-velocity-acceleration
165      * @return position-velocity-acceleration provider
166      */
167     public static FieldPVCoordinatesProvider<Gradient> extractFieldPVCoordinatesProvider(final SpacecraftState templateState,
168                                                                                          final TimeStampedFieldPVCoordinates<Gradient> pvCoordinates) {
169         final Field<Gradient> field = pvCoordinates.getDate().getField();
170         if (templateState.isOrbitDefined()) {
171             final FieldCartesianOrbit<Gradient> cartesianOrbit = new FieldCartesianOrbit<>(pvCoordinates, templateState.getFrame(),
172                     field.getZero().newInstance(templateState.getOrbit().getMu()));
173             return templateState.getOrbit().getType().convertType(cartesianOrbit);
174         } else {
175             return new FieldAbsolutePVCoordinates<>(templateState.getFrame(), pvCoordinates);
176         }
177     }
178 }