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.conversion;
18  
19  import org.orekit.annotation.DefaultDataContext;
20  import org.orekit.attitudes.AttitudeProvider;
21  import org.orekit.attitudes.FrameAlignedProvider;
22  import org.orekit.data.DataContext;
23  import org.orekit.frames.Frame;
24  import org.orekit.orbits.Orbit;
25  import org.orekit.orbits.PositionAngleType;
26  import org.orekit.propagation.Propagator;
27  import org.orekit.propagation.SpacecraftState;
28  import org.orekit.propagation.analytical.tle.TLE;
29  import org.orekit.propagation.analytical.tle.TLEPropagator;
30  import org.orekit.propagation.analytical.tle.generation.TleGenerationAlgorithm;
31  import org.orekit.utils.ParameterDriver;
32  
33  import java.util.List;
34  
35  /** Builder for TLEPropagator.
36   * @author Pascal Parraud
37   * @author Thomas Paulet
38   * @since 6.0
39   */
40  public class TLEPropagatorBuilder extends AbstractAnalyticalPropagatorBuilder<TLEPropagator> {
41  
42      /** Data context used to access frames and time scales. */
43      private final DataContext dataContext;
44  
45      /** Template TLE. */
46      private final TLE templateTLE;
47  
48      /** TLE generation algorithm. */
49      private final TleGenerationAlgorithm generationAlgorithm;
50  
51      /** Build a new instance. This constructor uses the {@link DataContext#getDefault()
52       * default data context}.
53       * <p>
54       * The template TLE is used as a model to {@link
55       * #createInitialOrbit() create initial orbit}. It defines the
56       * inertial frame, the central attraction coefficient, orbit type, satellite number,
57       * classification, .... and is also used together with the {@code positionScale} to
58       * convert from the {@link ParameterDriver#setNormalizedValue(double) normalized}
59       * parameters used by the callers of this builder to the real orbital parameters.
60       * </p>
61       * @param templateTLE reference TLE from which real orbits will be built
62       * @param positionAngleType position angle type to use
63       * @param positionScale scaling factor used for orbital parameters normalization
64       * (typically set to the expected standard deviation of the position)
65       * @param generationAlgorithm TLE generation algorithm
66       * @since 12.0
67       * @see #TLEPropagatorBuilder(TLE, PositionAngleType, double, DataContext, TleGenerationAlgorithm)
68       * @see #TLEPropagatorBuilder(TLE, PositionAngleType, double, DataContext, TleGenerationAlgorithm, AttitudeProvider)
69       */
70      @DefaultDataContext
71      public TLEPropagatorBuilder(final TLE templateTLE, final PositionAngleType positionAngleType,
72                                  final double positionScale, final TleGenerationAlgorithm generationAlgorithm) {
73          this(templateTLE, positionAngleType, positionScale, DataContext.getDefault(), generationAlgorithm);
74      }
75  
76      /** Build a new instance.
77       * <p>
78       * The template TLE is used as a model to {@link
79       * #createInitialOrbit() create initial orbit}. It defines the
80       * inertial frame, the central attraction coefficient, orbit type, satellite number,
81       * classification, .... and is also used together with the {@code positionScale} to
82       * convert from the {@link ParameterDriver#setNormalizedValue(double) normalized}
83       * parameters used by the callers of this builder to the real orbital parameters.
84       * The default attitude provider is aligned with the orbit's inertial frame.
85       * </p>
86       * @param templateTLE reference TLE from which real orbits will be built
87       * @param positionAngleType position angle type to use
88       * @param positionScale scaling factor used for orbital parameters normalization
89       * (typically set to the expected standard deviation of the position)
90       * @param dataContext used to access frames and time scales.
91       * @param generationAlgorithm TLE generation algorithm
92       * @since 12.0
93       * @see #TLEPropagatorBuilder(TLE, PositionAngleType, double, DataContext, TleGenerationAlgorithm, AttitudeProvider)
94       */
95      public TLEPropagatorBuilder(final TLE templateTLE, final PositionAngleType positionAngleType,
96                                  final double positionScale, final DataContext dataContext,
97                                  final TleGenerationAlgorithm generationAlgorithm) {
98          this(templateTLE, positionAngleType, positionScale, dataContext, generationAlgorithm, FrameAlignedProvider.of(dataContext.getFrames().getTEME()));
99      }
100 
101     /** Build a new instance.
102      * <p>
103      * The template TLE is used as a model to {@link
104      * #createInitialOrbit() create initial orbit}. It defines the
105      * inertial frame, the central attraction coefficient, orbit type, satellite number,
106      * classification, .... and is also used together with the {@code positionScale} to
107      * convert from the {@link ParameterDriver#setNormalizedValue(double) normalized}
108      * parameters used by the callers of this builder to the real orbital parameters.
109      * </p>
110      * @param templateTLE reference TLE from which real orbits will be built
111      * @param positionAngleType position angle type to use
112      * @param positionScale scaling factor used for orbital parameters normalization
113      * (typically set to the expected standard deviation of the position)
114      * @param dataContext used to access frames and time scales.
115      * @param generationAlgorithm TLE generation algorithm
116      * @param attitudeProvider attitude law to use
117      * @since 12.2
118      */
119     public TLEPropagatorBuilder(final TLE templateTLE, final PositionAngleType positionAngleType,
120                                 final double positionScale, final DataContext dataContext,
121                                 final TleGenerationAlgorithm generationAlgorithm, final AttitudeProvider attitudeProvider) {
122         super(TLEPropagator.selectExtrapolator(templateTLE, dataContext.getFrames().getTEME(), attitudeProvider).getInitialState().getOrbit(),
123               positionAngleType, positionScale, false, attitudeProvider, Propagator.DEFAULT_MASS);
124 
125         // Supported parameters: Bstar
126         addSupportedParameters(templateTLE.getParametersDrivers());
127 
128         this.templateTLE         = templateTLE;
129         this.dataContext         = dataContext;
130         this.generationAlgorithm = generationAlgorithm;
131     }
132 
133 
134     /** {@inheritDoc} */
135     @Override
136     public TLEPropagator buildPropagator(final double[] normalizedParameters) {
137 
138         // create the orbit
139         setParameters(normalizedParameters);
140         final Orbit           orbit = createInitialOrbit();
141         final SpacecraftState state = new SpacecraftState(orbit);
142         final Frame           teme  = dataContext.getFrames().getTEME();
143 
144         // TLE related to the orbit
145         final TLE tle = generationAlgorithm.generate(state, templateTLE);
146         final List<ParameterDriver> drivers = templateTLE.getParametersDrivers();
147         for (int index = 0; index < drivers.size(); index++) {
148             if (drivers.get(index).isSelected()) {
149                 tle.getParametersDrivers().get(index).setSelected(true);
150             }
151         }
152 
153         // propagator
154         final TLEPropagator propagator = TLEPropagator.selectExtrapolator(tle, getAttitudeProvider(), getMass(), teme);
155         getImpulseManeuvers().forEach(propagator::addEventDetector);
156         return propagator;
157     }
158 
159     /** Getter for the template TLE.
160      * @return the template TLE
161      */
162     public TLE getTemplateTLE() {
163         return templateTLE;
164     }
165 }