1   /* Contributed in the public domain.
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 java.util.concurrent.ConcurrentHashMap;
20  import java.util.concurrent.ConcurrentMap;
21  
22  import org.hipparchus.util.MathArrays;
23  import org.hipparchus.util.Pair;
24  import org.orekit.frames.EOPHistory;
25  import org.orekit.utils.Constants;
26  import org.orekit.utils.IERSConventions;
27  
28  /**
29   * Abstract base class for {@link TimeScales} that implements some common functionality.
30   *
31   * @author Evan Ward
32   * @author Luc Maisonobe
33   * @since 10.1
34   */
35  public abstract class AbstractTimeScales implements TimeScales {
36  
37      /** GMST time scales. */
38      private final ConcurrentMap<Pair<IERSConventions, Boolean>, GMSTScale> gmstMap;
39      /** UT1 time scales. */
40      private final ConcurrentMap<Pair<IERSConventions, Boolean>, UT1Scale> ut1Map;
41  
42      /** Simple constructor. */
43      public AbstractTimeScales() {
44          final int n = IERSConventions.values().length;
45          gmstMap = new ConcurrentHashMap<>(n * 2);
46          ut1Map = new ConcurrentHashMap<>(n * 2);
47      }
48  
49      /**
50       * Get the Universal Time 1 scale.
51       * <p>
52       * As this method allow associating any history with the time scale, it may involve
53       * large data sets. So this method does <em>not</em> cache the resulting {@link
54       * UT1Scale UT1Scale} instance, a new instance will be returned each time. In order to
55       * avoid wasting memory, calling {@link #getUT1(IERSConventions, boolean)} with the
56       * single enumerate corresponding to the conventions may be a better solution. This
57       * method is made available only for expert use.
58       * </p>
59       *
60       * @param history EOP parameters providing dUT1 (may be null if no correction is
61       *                desired)
62       * @return Universal Time 1 scale
63       * @see #getUT1(IERSConventions, boolean)
64       */
65      protected UT1Scale getUT1(final EOPHistory history) {
66          return new UT1Scale(history, getUTC());
67      }
68  
69      /**
70       * Get the EOP history for the given conventions.
71       *
72       * @param conventions to use in computing the EOP history.
73       * @param simpleEOP   whether to ignore some small tidal effects.
74       * @return EOP history.
75       */
76      protected abstract EOPHistory getEopHistory(IERSConventions conventions,
77                                                  boolean simpleEOP);
78  
79      @Override
80      public UT1Scale getUT1(final IERSConventions conventions, final boolean simpleEOP) {
81          final Pair<IERSConventions, Boolean> key = new Pair<>(conventions, simpleEOP);
82          synchronized (this) {
83              return ut1Map.computeIfAbsent(key, k -> getUT1(getEopHistory(conventions, simpleEOP)));
84          }
85      }
86  
87      @Override
88      public GMSTScale getGMST(final IERSConventions conventions, final boolean simpleEOP) {
89          final Pair<IERSConventions, Boolean> key = new Pair<>(conventions, simpleEOP);
90          synchronized (this) {
91              return gmstMap.computeIfAbsent(key, k -> new GMSTScale(getUT1(conventions, simpleEOP)));
92          }
93      }
94  
95      @Override
96      public AbsoluteDate getJulianEpoch() {
97          return new AbsoluteDate(DateComponents.JULIAN_EPOCH, TimeComponents.H12, this.getTT());
98      }
99  
100     @Override
101     public AbsoluteDate getModifiedJulianEpoch() {
102         return new AbsoluteDate(DateComponents.MODIFIED_JULIAN_EPOCH, TimeComponents.H00, this.getTT());
103     }
104 
105     @Override
106     public AbsoluteDate getFiftiesEpoch() {
107         return new AbsoluteDate(DateComponents.FIFTIES_EPOCH, TimeComponents.H00, this.getTT());
108     }
109 
110     @Override
111     public AbsoluteDate getCcsdsEpoch() {
112         return new AbsoluteDate(DateComponents.CCSDS_EPOCH, TimeComponents.H00, this.getTAI());
113     }
114 
115     @Override
116     public AbsoluteDate getGalileoEpoch() {
117         return new AbsoluteDate(DateComponents.GALILEO_EPOCH, TimeComponents.H00, this.getGST());
118     }
119 
120     @Override
121     public AbsoluteDate getGpsEpoch() {
122         return new AbsoluteDate(DateComponents.GPS_EPOCH, TimeComponents.H00, this.getGPS());
123     }
124 
125     @Override
126     public AbsoluteDate getQzssEpoch() {
127         return new AbsoluteDate(DateComponents.QZSS_EPOCH, TimeComponents.H00, this.getQZSS());
128     }
129 
130     @Override
131     public AbsoluteDate getIrnssEpoch() {
132         return new AbsoluteDate(DateComponents.IRNSS_EPOCH, TimeComponents.H00, this.getIRNSS());
133     }
134 
135     @Override
136     public AbsoluteDate getBeidouEpoch() {
137         return new AbsoluteDate(DateComponents.BEIDOU_EPOCH, TimeComponents.H00, this.getBDT());
138     }
139 
140     @Override
141     public AbsoluteDate getGlonassEpoch() {
142         return new AbsoluteDate(DateComponents.GLONASS_EPOCH,
143                 new TimeComponents(29.0), this.getTAI()).shiftedBy(-10800.0);
144     }
145 
146     @Override
147     public AbsoluteDate getJ2000Epoch() {
148         return new AbsoluteDate(DateComponents.J2000_EPOCH, TimeComponents.H12, this.getTT());
149     }
150 
151     @Override
152     public AbsoluteDate getJavaEpoch() {
153         return new AbsoluteDate(DateComponents.JAVA_EPOCH, this.getTAI()).shiftedBy(8.000082);
154     }
155 
156     @Override
157     public AbsoluteDate getPastInfinity() {
158         return getJavaEpoch().shiftedBy(Double.NEGATIVE_INFINITY);
159     }
160 
161     @Override
162     public AbsoluteDate getFutureInfinity() {
163         return getJavaEpoch().shiftedBy(Double.POSITIVE_INFINITY);
164     }
165 
166     @Override
167     public AbsoluteDate createJulianEpoch(final double julianEpoch) {
168         return new AbsoluteDate(getJ2000Epoch(),
169                 Constants.JULIAN_YEAR * (julianEpoch - 2000.0));
170     }
171 
172     @Override
173     public AbsoluteDate createBesselianEpoch(final double besselianEpoch) {
174         return new AbsoluteDate(getJ2000Epoch(),
175                 MathArrays.linearCombination(
176                         Constants.BESSELIAN_YEAR, besselianEpoch - 1900,
177                         Constants.JULIAN_DAY, -36525,
178                         Constants.JULIAN_DAY, 0.31352));
179     }
180 
181 }