1   /* Copyright 2020-2025 Exotrail
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.averaging.converters;
18  
19  import org.orekit.annotation.DefaultDataContext;
20  import org.orekit.data.DataContext;
21  import org.orekit.frames.Frame;
22  import org.orekit.orbits.Orbit;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.propagation.analytical.tle.TLE;
25  import org.orekit.propagation.analytical.tle.generation.FixedPointTleGenerationAlgorithm;
26  import org.orekit.time.UTCScale;
27  import org.orekit.propagation.conversion.averaging.SGP4OrbitalState;
28  
29  /**
30   * Class for osculating-to-averaged conversion according to "SGP4" theory, meant as the set of
31   * models associated to Two-Line Elements.
32   *
33   * @author Romain Serra
34   * @see org.orekit.propagation.analytical.tle.TLEPropagator
35   * @see SGP4OrbitalState
36   * @since 12.1
37   */
38  public class OsculatingToSGP4Converter
39          extends FixedPointOsculatingToAveragedConverter<SGP4OrbitalState> {
40  
41      /** First line of arbitrary TLE. Should not impact conversion. */
42      private static final String TEMPLATE_LINE_1 = "1 27421U 02021A   02124.48976499 -.00021470  00000-0 -89879-2 0    20";
43      /** Second line of arbitrary TLE. Should not impact conversion. */
44      private static final String TEMPLATE_LINE_2 = "2 27421  98.7490 199.5121 0001333 133.9522 226.1918 14.26113993    62";
45  
46      /** Scale for fixed-point algorithm. */
47      private final double scale;
48      /** UTC time scale. */
49      private final UTCScale utc;
50      /** TEME frame. */
51      private final Frame teme;
52  
53      /**
54       * Constructor with default data context.
55       */
56      @DefaultDataContext
57      public OsculatingToSGP4Converter() {
58          this(DataContext.getDefault());
59      }
60  
61      /**
62       * Constructor with default parameters for fixed-point algorithm.
63       * @param dataContext data context
64       */
65      public OsculatingToSGP4Converter(final DataContext dataContext) {
66          this(DEFAULT_EPSILON, DEFAULT_MAX_ITERATIONS, FixedPointTleGenerationAlgorithm.SCALE_DEFAULT,
67                  dataContext);
68      }
69  
70      /**
71       * Constructor.
72       * @param epsilon convergence threshold
73       * @param maxIterations maximum number of iterations
74       * @param scale scale
75       * @param dataContext data context
76       */
77      public OsculatingToSGP4Converter(final double epsilon, final int maxIterations,
78                                       final double scale, final DataContext dataContext) {
79          super(epsilon, maxIterations);
80          this.scale = scale;
81          this.utc = dataContext.getTimeScales().getUTC();
82          this.teme = dataContext.getFrames().getTEME();
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public SGP4OrbitalState convertToAveraged(final Orbit osculatingOrbit) {
88          final FixedPointTleGenerationAlgorithm fixedPointAlgorithm = new FixedPointTleGenerationAlgorithm(getEpsilon(),
89                  getMaxIterations(), scale, utc, teme);
90          final SpacecraftState osculatingState = new SpacecraftState(osculatingOrbit);
91          final TLE templateTLe = new TLE(TEMPLATE_LINE_1, TEMPLATE_LINE_2, utc);
92          final TLE tle = fixedPointAlgorithm.generate(osculatingState, templateTLe);
93          return SGP4OrbitalState.of(tle, teme);
94      }
95  }