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.propagation.analytical.tle.generation;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.orekit.annotation.DefaultDataContext;
21  import org.orekit.data.DataContext;
22  import org.orekit.frames.Frame;
23  import org.orekit.orbits.FieldKeplerianOrbit;
24  import org.orekit.orbits.KeplerianOrbit;
25  import org.orekit.orbits.OrbitType;
26  import org.orekit.propagation.FieldSpacecraftState;
27  import org.orekit.propagation.SpacecraftState;
28  import org.orekit.propagation.analytical.tle.FieldTLE;
29  import org.orekit.propagation.analytical.tle.TLE;
30  import org.orekit.propagation.conversion.osc2mean.FixedPointConverter;
31  import org.orekit.propagation.conversion.osc2mean.TLETheory;
32  import org.orekit.time.TimeScale;
33  import org.orekit.utils.ParameterDriver;
34  
35  /**
36   * Fixed Point method to reverse SGP4 and SDP4 propagation algorithm
37   * and generate a usable TLE from a spacecraft state.
38   * <p>
39   * Using this algorithm, the B* value is not computed. In other words,
40   * the B* value from the template TLE is set to the generated one.
41   * </p>
42   * @author Thomas Paulet
43   * @author Bryan Cazabonne
44   * @since 12.0
45   */
46  public class FixedPointTleGenerationAlgorithm implements TleGenerationAlgorithm {
47  
48      /** Default value for epsilon. */
49      public static final double EPSILON_DEFAULT = 1.0e-10;
50  
51      /** Default value for maxIterations. */
52      public static final int MAX_ITERATIONS_DEFAULT = 100;
53  
54      /** Default value for scale. */
55      public static final double SCALE_DEFAULT = 1.0;
56  
57      /** Osculating to mean orbit converter. */
58      private final FixedPointConverter converter;
59  
60      /** UTC time scale. */
61      private final TimeScale utc;
62  
63      /**
64       * Default constructor.
65       * <p>
66       * Uses the {@link DataContext#getDefault() default data context}
67       * as well as {@link #EPSILON_DEFAULT}, {@link #MAX_ITERATIONS_DEFAULT},
68       * {@link #SCALE_DEFAULT} for method convergence.
69       * </p>
70       */
71      @DefaultDataContext
72      public FixedPointTleGenerationAlgorithm() {
73          this(EPSILON_DEFAULT, MAX_ITERATIONS_DEFAULT, SCALE_DEFAULT);
74      }
75  
76      /**
77       * Constructor.
78       * <p>
79       * Uses the {@link DataContext#getDefault() default data context}.
80       * </p>
81       * @param epsilon used to compute threshold for convergence check
82       * @param maxIterations maximum number of iterations for convergence
83       * @param scale scale factor of the Fixed Point algorithm
84       */
85      @DefaultDataContext
86      public FixedPointTleGenerationAlgorithm(final double epsilon,
87                                              final int maxIterations,
88                                              final double scale) {
89          this(epsilon, maxIterations, scale,
90               DataContext.getDefault().getTimeScales().getUTC(),
91               DataContext.getDefault().getFrames().getTEME());
92      }
93  
94      /**
95       * Constructor.
96       * @param epsilon used to compute threshold for convergence check
97       * @param maxIterations maximum number of iterations for convergence
98       * @param scale scale factor of the Fixed Point algorithm
99       * @param utc UTC time scale
100      * @param teme TEME frame
101      */
102     public FixedPointTleGenerationAlgorithm(final double epsilon,
103                                             final int maxIterations,
104                                             final double scale,
105                                             final TimeScale utc,
106                                             final Frame teme) {
107         this.converter = new FixedPointConverter(new TLETheory(utc, teme),
108                                                  epsilon,
109                                                  maxIterations,
110                                                  scale);
111         this.utc       = utc;
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     /** {@inheritDoc} */
130     @Override
131     public <T extends CalculusFieldElement<T>> FieldTLE<T> generate(final FieldSpacecraftState<T> state,
132                                                                     final FieldTLE<T> templateTLE) {
133         final T bStar = state.getMass().getField().getZero().newInstance(templateTLE.getBStar());
134         final FieldKeplerianOrbit<T> mean = (FieldKeplerianOrbit<T>) OrbitType.KEPLERIAN.convertType(converter.convertToMean(state.getOrbit()));
135         final FieldTLE<T> tle = TleGenerationUtil.newTLE(mean, templateTLE, bStar, utc);
136         // reset estimated parameters from template to generated tle
137         for (final ParameterDriver templateDrivers : templateTLE.getParametersDrivers()) {
138             if (templateDrivers.isSelected()) {
139                 // set to selected for the new TLE
140                 tle.getParameterDriver(templateDrivers.getName()).setSelected(true);
141             }
142         }
143         return tle;
144     }
145 
146 }