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          return ut1Map.computeIfAbsent(
82              new Pair<>(conventions, simpleEOP),
83              k -> getUT1(getEopHistory(conventions, simpleEOP)));
84      }
85  
86      @Override
87      public GMSTScale getGMST(final IERSConventions conventions, final boolean simpleEOP) {
88          return gmstMap.computeIfAbsent(
89              new Pair<>(conventions, simpleEOP),
90              k -> new GMSTScale(getUT1(conventions, simpleEOP)));
91      }
92  
93      @Override
94      public AbsoluteDate getJulianEpoch() {
95          return new AbsoluteDate(DateComponents.JULIAN_EPOCH, TimeComponents.H12, this.getTT());
96      }
97  
98      @Override
99      public AbsoluteDate getModifiedJulianEpoch() {
100         return new AbsoluteDate(DateComponents.MODIFIED_JULIAN_EPOCH, TimeComponents.H00, this.getTT());
101     }
102 
103     @Override
104     public AbsoluteDate getFiftiesEpoch() {
105         return new AbsoluteDate(DateComponents.FIFTIES_EPOCH, TimeComponents.H00, this.getTT());
106     }
107 
108     @Override
109     public AbsoluteDate getCcsdsEpoch() {
110         return new AbsoluteDate(DateComponents.CCSDS_EPOCH, TimeComponents.H00, this.getTAI());
111     }
112 
113     @Override
114     public AbsoluteDate getGalileoEpoch() {
115         return new AbsoluteDate(DateComponents.GALILEO_EPOCH, TimeComponents.H00, this.getGST());
116     }
117 
118     @Override
119     public AbsoluteDate getGpsEpoch() {
120         return new AbsoluteDate(DateComponents.GPS_EPOCH, TimeComponents.H00, this.getGPS());
121     }
122 
123     @Override
124     public AbsoluteDate getQzssEpoch() {
125         return new AbsoluteDate(DateComponents.QZSS_EPOCH, TimeComponents.H00, this.getQZSS());
126     }
127 
128     @Override
129     public AbsoluteDate getIrnssEpoch() {
130         return new AbsoluteDate(DateComponents.IRNSS_EPOCH, TimeComponents.H00, this.getIRNSS());
131     }
132 
133     @Override
134     public AbsoluteDate getBeidouEpoch() {
135         return new AbsoluteDate(DateComponents.BEIDOU_EPOCH, TimeComponents.H00, this.getBDT());
136     }
137 
138     @Override
139     public AbsoluteDate getGlonassEpoch() {
140         return new AbsoluteDate(DateComponents.GLONASS_EPOCH,
141                 new TimeComponents(29.0), this.getTAI()).shiftedBy(-10800.0);
142     }
143 
144     @Override
145     public AbsoluteDate getJ2000Epoch() {
146         return new AbsoluteDate(DateComponents.J2000_EPOCH, TimeComponents.H12, this.getTT());
147     }
148 
149     @Override
150     public AbsoluteDate getJavaEpoch() {
151         return new AbsoluteDate(DateComponents.JAVA_EPOCH, this.getTAI()).shiftedBy(8.000082);
152     }
153 
154     @Override
155     public AbsoluteDate getPastInfinity() {
156         return getJavaEpoch().shiftedBy(Double.NEGATIVE_INFINITY);
157     }
158 
159     @Override
160     public AbsoluteDate getFutureInfinity() {
161         return getJavaEpoch().shiftedBy(Double.POSITIVE_INFINITY);
162     }
163 
164     @Override
165     public AbsoluteDate createJulianEpoch(final double julianEpoch) {
166         return new AbsoluteDate(getJ2000Epoch(),
167                 Constants.JULIAN_YEAR * (julianEpoch - 2000.0));
168     }
169 
170     @Override
171     public AbsoluteDate createBesselianEpoch(final double besselianEpoch) {
172         return new AbsoluteDate(getJ2000Epoch(),
173                 MathArrays.linearCombination(
174                         Constants.BESSELIAN_YEAR, besselianEpoch - 1900,
175                         Constants.JULIAN_DAY, -36525,
176                         Constants.JULIAN_DAY, 0.31352));
177     }
178 
179 }