1   /* Copyright 2002-2024 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.time;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.util.FastMath;
21  import org.orekit.utils.Constants;
22  
23  /** Barycentric Dynamic Time.
24   * <p>Time used to take account of time dilation when calculating orbits of planets,
25   * asteroids, comets and interplanetary spacecraft in the Solar system. It was based
26   * on a Dynamical time scale but was not well defined and not rigorously correct as
27   * a relativistic time scale. It was subsequently deprecated in favour of
28   * Barycentric Coordinate Time (TCB), but at the 2006 General Assembly of the
29   * International Astronomical Union TDB was rehabilitated by making it a specific
30   * fixed linear transformation of TCB.</p>
31   * <p>By convention, TDB = TT + 0.001658 sin(g) + 0.000014 sin(2g)seconds
32   * where g = 357.53 + 0.9856003 (JD - 2451545) degrees.</p>
33   * @author Aude Privat
34   */
35  public class TDBScale implements TimeScale {
36  
37      /** Serializable UID. */
38      private static final long serialVersionUID = 20131209L;
39  
40      /** Constant term for g angle. */
41      private static final double G0 = 357.53;
42  
43      /** Slope term for g angle. */
44      private static final double G1 = 0.9856003;
45  
46      /** Factor for sin(g). */
47      private static final double SIN_G_FACTOR = 0.001658;
48  
49      /** Factor for sin(2g). */
50      private static final double SIN_2G_FACTOR = 0.000014;
51  
52      /** TT time scale. */
53      private final TimeScale tt;
54  
55      /** Reference Epoch. */
56      private final AbsoluteDate j2000Epoch;
57  
58      /**
59       * Package private constructor for the factory.
60       *
61       * @param tt         TT time scale.
62       * @param j2000Epoch reference date for this time scale.
63       */
64      TDBScale(final TimeScale tt, final AbsoluteDate j2000Epoch) {
65          this.tt = tt;
66          this.j2000Epoch = j2000Epoch;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public double offsetFromTAI(final AbsoluteDate date) {
72          final double dtDays = date.durationFrom(j2000Epoch) / Constants.JULIAN_DAY;
73          final double g = FastMath.toRadians(G0 + G1 * dtDays);
74          return tt.offsetFromTAI(date) + (SIN_G_FACTOR * FastMath.sin(g) + SIN_2G_FACTOR * FastMath.sin(2 * g));
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public <T extends CalculusFieldElement<T>> T offsetFromTAI(final FieldAbsoluteDate<T> date) {
80          final T dtDays = date.durationFrom(j2000Epoch).divide(Constants.JULIAN_DAY);
81          final T g = dtDays.multiply(G1).add(G0).multiply(dtDays.getPi().divide(180));
82          return tt.offsetFromTAI(date).
83                          add(g.sin().multiply(SIN_G_FACTOR).add(g.multiply(2).sin().multiply(SIN_2G_FACTOR)));
84      }
85  
86      /** {@inheritDoc} */
87      public String getName() {
88          return "TDB";
89      }
90  
91      /** {@inheritDoc} */
92      public String toString() {
93          return getName();
94      }
95  
96  }