TimeSystem.java

  1. /* Copyright 2002-2025 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.gnss;

  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.function.Function;

  21. import org.orekit.errors.OrekitIllegalArgumentException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.time.TimeScale;
  24. import org.orekit.time.TimeScales;

  25. /** Enumerate for the time systems used in navigation files.
  26.  *
  27.  * @author Thomas Neidhart
  28.  * @author Evan Ward
  29.  * @author Thomas Paulet
  30.  * @since 11.0
  31.  */
  32. public enum TimeSystem {

  33.     /** Global Positioning System. */
  34.     GPS("GPS", "GP", "G", TimeScales::getGPS),

  35.     /** GLONASS. */
  36.     GLONASS("GLO", "GL", "R", TimeScales::getGLONASS),

  37.     /** GALILEO. */
  38.     GALILEO("GAL", "GA", "E", TimeScales::getGST),

  39.     /** International Atomic Time. */
  40.     TAI("TAI", null, null, TimeScales::getTAI),

  41.     /** Coordinated Universal Time. */
  42.     UTC("UTC", "UT", null, TimeScales::getUTC),

  43.     /** Quasi-Zenith System. */
  44.     QZSS("QZS", "QZ", "J", TimeScales::getQZSS),

  45.     /** Beidou. */
  46.     BEIDOU("BDT", "BD", "C", TimeScales::getBDT),

  47.     /** NavIC. */
  48.     NAVIC("IRN", "IR", "I", TimeScales::getNavIC),

  49.     /** SBAS.
  50.      * @since 12.0
  51.      */
  52.     SBAS(null, "SB", "S", TimeScales::getUTC),

  53.     /** GMT (should only be used in RUN BY / DATE entries).
  54.      * @since 12.0
  55.      */
  56.     GMT("GMT", null, null, TimeScales::getUTC),

  57.     /** Unknown (should only be used in RUN BY / DATE entries). */
  58.     UNKNOWN("LCL", null, null, TimeScales::getGPS);

  59.     /** Parsing key map. */
  60.     private static final Map<String, TimeSystem> KEYS_MAP = new HashMap<>();

  61.     /** Parsing two letters code map.
  62.      * @since 12.0
  63.      */
  64.     private static final Map<String, TimeSystem> TLC_MAP = new HashMap<>();

  65.     /** Parsing one letters code map.
  66.      * @since 12.0
  67.      */
  68.     private static final Map<String, TimeSystem> OLC_MAP = new HashMap<>();

  69.     static {
  70.         for (final TimeSystem timeSystem : values()) {
  71.             if (timeSystem.key != null) {
  72.                 KEYS_MAP.put(timeSystem.key, timeSystem);
  73.             }
  74.             if (timeSystem.twoLettersCode != null) {
  75.                 TLC_MAP.put(timeSystem.twoLettersCode, timeSystem);
  76.             }
  77.             if (timeSystem.oneLetterCode != null) {
  78.                 OLC_MAP.put(timeSystem.oneLetterCode, timeSystem);
  79.             }
  80.         }
  81.     }

  82.     /** Key for the system. */
  83.     private final String key;

  84.     /** Two-letters code.
  85.      * @since 12.0
  86.      */
  87.     private final String twoLettersCode;

  88.     /** One-letter code.
  89.      * @since 12.0
  90.      */
  91.     private final String oneLetterCode;

  92.     /** Time scale provider.
  93.      * @since 12.0
  94.      */
  95.     private final Function<TimeScales, TimeScale> timeScaleProvider;

  96.     /** Simple constructor.
  97.      * @param key key letter (may be null)
  98.      * @param twoLettersCode two letters code (may be null)
  99.      * @param oneLetterCode one letter code (may be null)
  100.      * @param timeScaleProvider time scale provider
  101.      */
  102.     TimeSystem(final String key, final String twoLettersCode, final String oneLetterCode,
  103.                final Function<TimeScales, TimeScale> timeScaleProvider) {
  104.         this.key               = key;
  105.         this.twoLettersCode    = twoLettersCode;
  106.         this.oneLetterCode     = oneLetterCode;
  107.         this.timeScaleProvider = timeScaleProvider;
  108.     }

  109.     /** Get the 3 letters key of the time system.
  110.      * @return 3 letters key
  111.      * @since 12.0
  112.      */
  113.     public String getKey() {
  114.         return key;
  115.     }

  116.     /** Get the two letters code.
  117.      * @return two letters code (may be null for non-GNSS time systems)
  118.      * @since 12.2
  119.      */
  120.     public String getTwoLettersCode() {
  121.         return twoLettersCode;
  122.     }

  123.     /** Get the one letter code.
  124.      * @return one letter code (may be null for non-GNSS time systems)
  125.      * @since 12.2
  126.      */
  127.     public String getOneLetterCode() {
  128.         return oneLetterCode;
  129.     }

  130.     /** Parse a string to get the time system.
  131.      * <p>
  132.      * The string must be the time system.
  133.      * </p>
  134.      * @param s string to parse
  135.      * @return the time system
  136.      * @exception OrekitIllegalArgumentException if the string does not correspond to a time system key
  137.      */
  138.     public static TimeSystem parseTimeSystem(final String s)
  139.         throws OrekitIllegalArgumentException {
  140.         final TimeSystem timeSystem = KEYS_MAP.get(s);
  141.         if (timeSystem == null) {
  142.             throw new OrekitIllegalArgumentException(OrekitMessages.UNKNOWN_TIME_SYSTEM, s);
  143.         }
  144.         return timeSystem;
  145.     }

  146.     /** Parse a string to get the time system.
  147.      * <p>
  148.      * The string must be the two letters code of the time system.
  149.      * </p>
  150.      * @param code string to parse
  151.      * @return the time system
  152.      * @exception OrekitIllegalArgumentException if the string does not correspond to a time system key
  153.      */
  154.     public static TimeSystem parseTwoLettersCode(final String code)
  155.         throws OrekitIllegalArgumentException {
  156.         final TimeSystem timeSystem = TLC_MAP.get(code);
  157.         if (timeSystem == null) {
  158.             throw new OrekitIllegalArgumentException(OrekitMessages.UNKNOWN_TIME_SYSTEM, code);
  159.         }
  160.         return timeSystem;
  161.     }

  162.     /** Parse a string to get the time system.
  163.      * <p>
  164.      * The string must be the one letters code of the time system.
  165.      * The one letter code is the RINEX GNSS system flag.
  166.      * </p>
  167.      * @param code string to parse
  168.      * @return the time system
  169.      * @exception OrekitIllegalArgumentException if the string does not correspond to a time system key
  170.      */
  171.     public static TimeSystem parseOneLetterCode(final String code)
  172.         throws OrekitIllegalArgumentException {
  173.         final TimeSystem timeSystem = OLC_MAP.get(code);
  174.         if (timeSystem == null) {
  175.             throw new OrekitIllegalArgumentException(OrekitMessages.UNKNOWN_TIME_SYSTEM, code);
  176.         }
  177.         return timeSystem;
  178.     }

  179.     /** Get the time scale corresponding to time system.
  180.      * @param timeScales the set of time scales to use
  181.      * @return the time scale corresponding to time system in the set of time scales
  182.      */
  183.     public TimeScale getTimeScale(final TimeScales timeScales) {
  184.         return timeScaleProvider.apply(timeScales);
  185.     }

  186. }