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  /** 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 = 20240720L;
36  
37      /** Duration of one julian day. */
38      private static final long FULL_DAY = 86400L;
39  
40      /** Duration of an half julian day. */
41      private static final long HALF_DAY = FULL_DAY / 2;
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 TimeOffset 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 TimeOffset offset = new TimeOffset(gmst0h).add(ut1.offsetFromTAI(date));
87  
88          // normalize offset between -43200 and +43200 seconds
89          long clipped = offset.getSeconds() < 0L ?
90                         (offset.getSeconds() - HALF_DAY) % FULL_DAY + HALF_DAY :
91                         (offset.getSeconds() + HALF_DAY) % FULL_DAY - HALF_DAY;
92          if (clipped == HALF_DAY && offset.getAttoSeconds() > 0L) {
93              clipped -= FULL_DAY;
94          }
95          return new TimeOffset(clipped, offset.getAttoSeconds());
96  
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public <T extends CalculusFieldElement<T>> T offsetFromTAI(final FieldAbsoluteDate<T> date) {
102 
103         // julian seconds since reference date
104         final T ts = date.durationFrom(referenceDate);
105 
106         // julian centuries since reference date
107         final T tc = ts.divide(Constants.JULIAN_CENTURY);
108 
109         // GMST at 0h00 UT1 in seconds = offset with respect to UT1
110         final T gmst0h = tc.multiply(C3).add(C2).multiply(tc).add(C1).multiply(tc).add(C0);
111 
112         // offset with respect to TAI
113         final T offset = gmst0h.add(ut1.offsetFromTAI(date));
114 
115         // normalize offset between -43200 and +43200 seconds
116         return offset.subtract(FULL_DAY * FastMath.floor((offset.getReal() + HALF_DAY) / FULL_DAY));
117 
118     }
119 
120     /** {@inheritDoc} */
121     public String getName() {
122         return "GMST";
123     }
124 
125     /** {@inheritDoc} */
126     public String toString() {
127         return getName();
128     }
129 
130 }