1   /* Copyright 2002-2023 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  /** Greenwich Mean Sidereal Time.
24   * <p>The Greenwich Mean Sidereal Time is the hour angle between the meridian of Greenwich
25   * and mean equinox of date at 0h UT1.</p>
26   * <p>This is intended to be accessed thanks to {@link TimeScales},
27   * so there is no public constructor.</p>
28   * @author Luc Maisonobe
29   * @see AbsoluteDate
30   * @since 5.1
31   */
32  public class GMSTScale implements TimeScale {
33  
34      /** Serializable UID. */
35      private static final long serialVersionUID = 20131209L;
36  
37      /** Duration of one julian day. */
38      private static final double FULL_DAY = Constants.JULIAN_DAY;
39  
40      /** Duration of an half julian day. */
41      private static final double HALF_DAY = Constants.JULIAN_DAY / 2.0;
42  
43      /** Coefficient for degree 0. */
44      private static final double C0 = 24110.54841;
45  
46      /** Coefficient for degree 1. */
47      private static final double C1 = 8640184.812866;
48  
49      /** Coefficient for degree 2. */
50      private static final double C2 = 0.093104;
51  
52      /** Coefficient for degree 3. */
53      private static final double C3 = -0.0000062;
54  
55      /** Universal Time 1 time scale. */
56      private final UT1Scale     ut1;
57  
58      /** Reference date for GMST. */
59      private final AbsoluteDate referenceDate;
60  
61      // GST 1982: 24110.54841 + 8640184.812866 t + 0.093104 t2 - 6.2e-6 t3
62      // GST 2000: 24110.5493771 + 8639877.3173760 tu + 307.4771600 te + 0.0931118 te2 - 0.0000062 te3 + 0.0000013 te4
63  
64      /** Package private constructor for the factory.
65       * @param ut1 Universal Time 1 scale
66       */
67      GMSTScale(final UT1Scale ut1) {
68          this.ut1           = ut1;
69          this.referenceDate = new AbsoluteDate(2000, 1, 1, 12, 0, 0.0, ut1);
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public double offsetFromTAI(final AbsoluteDate date) {
75  
76          // julian seconds since reference date
77          final double ts = date.durationFrom(referenceDate);
78  
79          // julian centuries since reference date
80          final double tc = ts / Constants.JULIAN_CENTURY;
81  
82          // GMST at 0h00 UT1 in seconds = offset with respect to UT1
83          final double gmst0h = C0 + tc * (C1 + tc * (C2 + tc * C3));
84  
85          // offset with respect to TAI
86          final double offset = gmst0h + ut1.offsetFromTAI(date);
87  
88          // normalize offset between -43200 and +43200 seconds
89          return offset - FULL_DAY * FastMath.floor((offset + HALF_DAY) / FULL_DAY);
90  
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public <T extends CalculusFieldElement<T>> T offsetFromTAI(final FieldAbsoluteDate<T> date) {
96  
97          // julian seconds since reference date
98          final T ts = date.durationFrom(referenceDate);
99  
100         // julian centuries since reference date
101         final T tc = ts.divide(Constants.JULIAN_CENTURY);
102 
103         // GMST at 0h00 UT1 in seconds = offset with respect to UT1
104         final T gmst0h = tc.multiply(C3).add(C2).multiply(tc).add(C1).multiply(tc).add(C0);
105 
106         // offset with respect to TAI
107         final T offset = gmst0h.add(ut1.offsetFromTAI(date));
108 
109         // normalize offset between -43200 and +43200 seconds
110         return offset.subtract(FULL_DAY * FastMath.floor((offset.getReal() + HALF_DAY) / FULL_DAY));
111 
112     }
113 
114     /** {@inheritDoc} */
115     public String getName() {
116         return "GMST";
117     }
118 
119     /** {@inheritDoc} */
120     public String toString() {
121         return getName();
122     }
123 
124 }