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.models.earth.troposphere;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.util.MathArrays;
24  import org.orekit.annotation.DefaultDataContext;
25  import org.orekit.bodies.FieldGeodeticPoint;
26  import org.orekit.bodies.GeodeticPoint;
27  import org.orekit.time.AbsoluteDate;
28  import org.orekit.time.FieldAbsoluteDate;
29  import org.orekit.time.TimeScale;
30  import org.orekit.time.TimeScalesFactory;
31  import org.orekit.utils.FieldTrackingCoordinates;
32  import org.orekit.utils.ParameterDriver;
33  import org.orekit.utils.ParameterDriversProvider;
34  import org.orekit.utils.TimeSpanMap;
35  import org.orekit.utils.TimeSpanMap.Span;
36  import org.orekit.utils.TrackingCoordinates;
37  
38  /**
39   * Time span estimated tropospheric model.
40   * <p>
41   * This class is closely related to {@link org.orekit.models.earth.troposphere EstimatedModel} class.<br>
42   * The difference is that it has a {@link TimeSpanMap} of {@link EstimatedModel} objects as attribute. <br>
43   * The idea behind this model is to allow the user to design a tropospheric model that can see its physical parameters
44   * (total zenith delay) change with time, at dates chosen by the user. <br>
45   * </p>
46   * @author Bryan Cazabonne
47   * @since 10.2
48   */
49  public class TimeSpanEstimatedModel implements TroposphericModel {
50  
51      /** Prefix for dates before in the tropospheric parameter drivers' name. */
52      public static final String DATE_BEFORE = " - Before ";
53  
54      /** Prefix for dates after in the tropospheric parameter drivers' name. */
55      public static final String DATE_AFTER = " - After ";
56  
57      /** Time scale for transition dates. */
58      private final TimeScale timeScale;
59  
60      /** It contains all the models use for the whole period of measurements. */
61      private final TimeSpanMap<EstimatedModel> troposphericModelMap;
62  
63      /**
64       * Constructor with default UTC time scale.
65       * @param model the initial model which going to be used for all the models initialization.
66       */
67      @DefaultDataContext
68      public TimeSpanEstimatedModel(final EstimatedModel model) {
69          this(model, TimeScalesFactory.getUTC());
70      }
71  
72      /**
73       * Constructor with default UTC time scale.
74       * @param model the initial model which going to be used for all the models initialization.
75       * @param timeScale  timeScale Time scale used for the default names of the tropospheric parameter drivers
76       */
77      public TimeSpanEstimatedModel(final EstimatedModel model, final TimeScale timeScale) {
78          this.troposphericModelMap = new TimeSpanMap<>(model);
79          this.timeScale            = timeScale;
80      }
81  
82      /** {@inheritDoc}
83       * <p>
84       * All the parameter drivers of all Estimated models are returned in an array.
85       * Models are ordered chronologically.
86       * </p>
87       */
88      @Override
89      public List<ParameterDriver> getParametersDrivers() {
90  
91          // Get all transitions from the TimeSpanMap
92          final List<ParameterDriver> listTroposphericParameterDrivers = new ArrayList<>();
93  
94          // Loop on the spans
95          for (Span<EstimatedModel> span = getFirstSpan(); span != null; span = span.next()) {
96              // Add all the parameter drivers of each span
97              for (ParameterDriver tropoDriver : span.getData().getParametersDrivers()) {
98                  // Add the driver only if the name does not exist already
99                  if (!ParameterDriversProvider.findByName(listTroposphericParameterDrivers, tropoDriver.getName())) {
100                     listTroposphericParameterDrivers.add(tropoDriver);
101                 }
102             }
103         }
104 
105         // Return an array of parameter drivers with no duplicated name
106         return listTroposphericParameterDrivers;
107 
108     }
109 
110     /** Add an EstimatedTroposphericModel entry valid before a limit date.<br>
111      * Using <code>addTroposphericValidBefore(entry, t)</code> will make <code>entry</code>
112      * valid in ]-∞, t[ (note the open bracket).
113      * @param model EstimatedTroposphericModel entry
114      * @param latestValidityDate date before which the entry is valid
115      * (must be different from <b>all</b> dates already used for transitions)
116      */
117     public void addTroposphericModelValidBefore(final EstimatedModel model, final AbsoluteDate latestValidityDate) {
118         troposphericModelMap.addValidBefore(changeTroposphericParameterDriversNames(model,
119                                                                                     latestValidityDate,
120                                                                                     DATE_BEFORE),
121                                             latestValidityDate, false);
122     }
123 
124     /** Add a EstimatedTroposphericModel entry valid after a limit date.<br>
125      * Using <code>addTroposphericModelValidAfter(entry, t)</code> will make <code>entry</code>
126      * valid in [t, +∞[ (note the closed bracket).
127      * @param model EstimatedTroposphericModel entry
128      * @param earliestValidityDate date after which the entry is valid
129      * (must be different from <b>all</b> dates already used for transitions)
130      */
131     public void addTroposphericModelValidAfter(final EstimatedModel model, final AbsoluteDate earliestValidityDate) {
132         troposphericModelMap.addValidAfter(changeTroposphericParameterDriversNames(model,
133                                                                                    earliestValidityDate,
134                                                                                    DATE_AFTER),
135                                            earliestValidityDate, false);
136     }
137 
138     /** Get the {@link EstimatedModel} model valid at a date.
139      * @param date the date of validity
140      * @return the EstimatedTroposphericModel model valid at date
141      */
142     public EstimatedModel getTroposphericModel(final AbsoluteDate date) {
143         return troposphericModelMap.get(date);
144     }
145 
146     /** Get the first {@link Span time span} of the tropospheric model time span map.
147      * @return the first {@link Span time span} of the tropospheric model time span map
148      * @since 11.1
149      */
150     public Span<EstimatedModel> getFirstSpan() {
151         return troposphericModelMap.getFirstSpan();
152     }
153 
154     /** Extract the proper parameter drivers' values from the array in input of the
155      * {@link #pathDelay(TrackingCoordinates, GeodeticPoint, double[], AbsoluteDate) pathDelay}  method.
156      *  Parameters are filtered given an input date.
157      * @param parameters the input parameters array
158      * @param date the date
159      * @return the parameters given the date
160      */
161     public double[] extractParameters(final double[] parameters, final AbsoluteDate date) {
162 
163         // Get the tropospheric parameter drivers of the date
164         final List<ParameterDriver> troposphericParameterDriver = getTroposphericModel(date).getParametersDrivers();
165 
166         // Find out the indexes of the parameters in the whole array of parameters
167         final List<ParameterDriver> allTroposphericParameters = getParametersDrivers();
168         final double[] outParameters = new double[troposphericParameterDriver.size()];
169         int index = 0;
170         for (int i = 0; i < allTroposphericParameters.size(); i++) {
171             final String driverName = allTroposphericParameters.get(i).getName();
172             for (ParameterDriver tropoDriver : troposphericParameterDriver) {
173                 if (tropoDriver.getName().equals(driverName)) {
174                     outParameters[index++] = parameters[i];
175                 }
176             }
177         }
178         return outParameters;
179     }
180 
181     /** Extract the proper parameter drivers' values from the array in input of the
182      * {@link #pathDelay(TrackingCoordinates, GeodeticPoint, double[], AbsoluteDate) pathDelay}  method.
183      *  Parameters are filtered given an input date.
184      * @param parameters the input parameters array
185      * @param date the date
186      * @param <T> extends CalculusFieldElements
187      * @return the parameters given the date
188      */
189     public <T extends CalculusFieldElement<T>> T[] extractParameters(final T[] parameters,
190                                                                  final FieldAbsoluteDate<T> date) {
191 
192         // Get the tropospheric parameter drivers of the date
193         final List<ParameterDriver> troposphericParameterDriver = getTroposphericModel(date.toAbsoluteDate()).getParametersDrivers();
194 
195         // Find out the indexes of the parameters in the whole array of parameters
196         final List<ParameterDriver> allTroposphericParameters = getParametersDrivers();
197         final T[] outParameters = MathArrays.buildArray(date.getField(), troposphericParameterDriver.size());
198         int index = 0;
199         for (int i = 0; i < allTroposphericParameters.size(); i++) {
200             final String driverName = allTroposphericParameters.get(i).getName();
201             for (ParameterDriver tropoDriver : troposphericParameterDriver) {
202                 if (tropoDriver.getName().equals(driverName)) {
203                     outParameters[index++] = parameters[i];
204                 }
205             }
206         }
207         return outParameters;
208     }
209 
210     /** {@inheritDoc} */
211     @Override
212     public TroposphericDelay pathDelay(final TrackingCoordinates trackingCoordinates,
213                                        final GeodeticPoint point,
214                                        final double[] parameters, final AbsoluteDate date) {
215         // Extract the proper parameters valid at date from the input array
216         final double[] extractedParameters = extractParameters(parameters, date);
217         // Compute and return the path delay
218         return getTroposphericModel(date).pathDelay(trackingCoordinates, point, extractedParameters, date);
219     }
220 
221     /** {@inheritDoc} */
222     @Override
223     public <T extends CalculusFieldElement<T>> FieldTroposphericDelay<T> pathDelay(final FieldTrackingCoordinates<T> trackingCoordinates,
224                                                                                    final  FieldGeodeticPoint<T> point,
225                                                                                    final T[] parameters, final FieldAbsoluteDate<T> date) {
226         // Extract the proper parameters valid at date from the input array
227         final T[] extractedParameters = extractParameters(parameters, date);
228         // Compute and return the path delay
229         return getTroposphericModel(date.toAbsoluteDate()).pathDelay(trackingCoordinates, point,
230                                                                      extractedParameters, date);
231     }
232 
233     /** Change the parameter drivers names of a {@link EstimatedModel} model, if needed.
234      * <p>
235      * This is done to avoid that several parameter drivers have the same name.<br>
236      * It is done only if the user hasn't modify the EstimatedTroposphericModel parameter drivers default names.
237      * </p>
238      * @param troposphericModel the EstimatedTroposphericModel model
239      * @param date the date used in the parameter driver's name
240      * @param datePrefix the date prefix used in the parameter driver's name
241      * @return the EstimatedTroposphericModel with its drivers' names changed
242      */
243     private EstimatedModel changeTroposphericParameterDriversNames(final EstimatedModel troposphericModel,
244                                                                    final AbsoluteDate date,
245                                                                    final String datePrefix) {
246         // Loop on the parameter drivers of the EstimatedTroposphericModel model
247         for (ParameterDriver driver: troposphericModel.getParametersDrivers()) {
248             final String driverName = driver.getName();
249 
250             // If the name is the default name for EstimatedTroposphericModel parameter drivers
251             // Modify the name to add the prefix and the date
252             if (driverName.equals(EstimatedModel.TOTAL_ZENITH_DELAY)) {
253                 driver.setName(driverName + datePrefix + date.toString(timeScale));
254             }
255         }
256         return troposphericModel;
257     }
258 
259 }