1   /* Copyright 2002-2025 Mark Rutten
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    * Mark Rutten 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.propagation.analytical.tle.generation;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer;
21  import org.hipparchus.optim.nonlinear.vector.leastsquares.LevenbergMarquardtOptimizer;
22  import org.orekit.annotation.DefaultDataContext;
23  import org.orekit.data.DataContext;
24  import org.orekit.frames.Frame;
25  import org.orekit.orbits.KeplerianOrbit;
26  import org.orekit.orbits.OrbitType;
27  import org.orekit.propagation.FieldSpacecraftState;
28  import org.orekit.propagation.SpacecraftState;
29  import org.orekit.propagation.analytical.tle.FieldTLE;
30  import org.orekit.propagation.analytical.tle.TLE;
31  import org.orekit.propagation.conversion.osc2mean.LeastSquaresConverter;
32  import org.orekit.propagation.conversion.osc2mean.TLETheory;
33  import org.orekit.time.TimeScale;
34  import org.orekit.utils.ParameterDriver;
35  
36  /**
37   * Least squares method to generate a usable TLE from a spacecraft state.
38   *
39   * @author Mark Rutten
40   * @since 12.0
41   */
42  public class LeastSquaresTleGenerationAlgorithm implements TleGenerationAlgorithm {
43  
44      /** Default value for maximum number of iterations.*/
45      public static final int DEFAULT_MAX_ITERATIONS = 1000;
46  
47      /** Osculating to mean orbit converter. */
48      private final LeastSquaresConverter converter;
49  
50      /** UTC time scale. */
51      private final TimeScale utc;
52  
53      /**
54       * Default constructor.
55       * <p>Uses:
56       * <ul>
57       * <li>the {@link DataContext#getDefault() default data context}</li>
58       * <li>{@link #DEFAULT_MAX_ITERATIONS}</li>
59       * <li>the {@link LevenbergMarquardtOptimizer}</li>
60       * </ul>
61       */
62      @DefaultDataContext
63      public LeastSquaresTleGenerationAlgorithm() {
64          this(DEFAULT_MAX_ITERATIONS);
65      }
66  
67      /**
68       * Default constructor.
69       * <p>Uses:
70       * <ul>
71       * <li>the {@link DataContext#getDefault() default data context}</li>
72       * <li>the {@link LevenbergMarquardtOptimizer}</li>
73       * </ul>
74       * @param maxIterations maximum number of iterations for convergence
75       */
76      @DefaultDataContext
77      public LeastSquaresTleGenerationAlgorithm(final int maxIterations) {
78          this(maxIterations, DataContext.getDefault().getTimeScales().getUTC(),
79               DataContext.getDefault().getFrames().getTEME());
80      }
81  
82      /**
83       * Constructor.
84       * <p>Uses the {@link LevenbergMarquardtOptimizer}.</p>
85       * @param maxIterations maximum number of iterations for convergence
86       * @param utc  UTC time scale
87       * @param teme TEME frame
88       */
89      public LeastSquaresTleGenerationAlgorithm(final int maxIterations,
90                                                final TimeScale utc,
91                                                final Frame teme) {
92          this(utc, teme, new LeastSquaresConverter(new TLETheory(utc, teme),
93                                                    new LevenbergMarquardtOptimizer(),
94                                                    LeastSquaresConverter.DEFAULT_THRESHOLD,
95                                                    maxIterations));
96      }
97  
98      /**
99       * Constructor.
100      * <p>Enables to select the {@link LeastSquaresOptimizer optimizer}
101      * for the {@link LeastSquaresConverter least-squares converter}.</p>
102      * @param utc  UTC time scale
103      * @param teme TEME frame
104      * @param converter osculating to mean orbit converter using a least-squares algorithm
105      */
106     public LeastSquaresTleGenerationAlgorithm(final TimeScale utc,
107                                               final Frame teme,
108                                               final LeastSquaresConverter converter) {
109         this.utc       = utc;
110         this.converter = converter;
111         converter.setMeanTheory(new TLETheory(utc, teme));
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public TLE generate(final SpacecraftState state, final TLE templateTLE) {
117         final KeplerianOrbit mean = (KeplerianOrbit) OrbitType.KEPLERIAN.convertType(converter.convertToMean(state.getOrbit()));
118         final TLE tle = TleGenerationUtil.newTLE(mean, templateTLE, templateTLE.getBStar(mean.getDate()), utc);
119         // reset estimated parameters from template to generated tle
120         for (final ParameterDriver templateDrivers : templateTLE.getParametersDrivers()) {
121             if (templateDrivers.isSelected()) {
122                 // set to selected for the new TLE
123                 tle.getParameterDriver(templateDrivers.getName()).setSelected(true);
124             }
125         }
126         return tle;
127     }
128 
129     /**
130      * Get the Root Mean Square of the TLE estimation.
131      * <p>
132      * Be careful that the RMS is updated each time the
133      * {@link LeastSquaresTleGenerationAlgorithm#generate(SpacecraftState, TLE)}
134      * method is called.
135      * </p>
136      * @return the RMS
137      */
138     public double getRms() {
139         return converter.getRMS();
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     public <T extends CalculusFieldElement<T>> FieldTLE<T> generate(final FieldSpacecraftState<T> state,
145                                                                     final FieldTLE<T> templateTLE) {
146         throw new UnsupportedOperationException();
147     }
148 }