LazyLoadedTimeScales.java

  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. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.orekit.data.DataProvidersManager;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.frames.EOPHistory;
  25. import org.orekit.frames.LazyLoadedEop;
  26. import org.orekit.utils.IERSConventions;

  27. /**
  28.  * An implementation of {@link TimeScales} that loads auxiliary data, leap seconds and
  29.  * UT1-UTC, when it is first accessed. The list of loaders may be modified before the
  30.  * first data access.
  31.  *
  32.  * @author Luc Maisonobe
  33.  * @author Evan Ward
  34.  * @see TimeScalesFactory
  35.  * @since 10.1
  36.  */
  37. public class LazyLoadedTimeScales extends AbstractTimeScales {

  38.     /** Source of EOP data. */
  39.     private final LazyLoadedEop lazyLoadedEop;

  40.     /** UTCTAI offsets loaders. */
  41.     private final List<UTCTAIOffsetsLoader> loaders = new ArrayList<>();
  42.     /** Universal Time Coordinate scale. */
  43.     private UTCScale utc = null;

  44.     /** International Atomic Time scale. */
  45.     private TAIScale tai = null;

  46.     /** Terrestrial Time scale. */
  47.     private TTScale tt = null;

  48.     /** Galileo System Time scale. */
  49.     private GalileoScale gst = null;

  50.     /** GLObal NAvigation Satellite System scale. */
  51.     private GLONASSScale glonass = null;

  52.     /** Quasi-Zenith Satellite System scale. */
  53.     private QZSSScale qzss = null;

  54.     /** Global Positioning System scale. */
  55.     private GPSScale gps = null;

  56.     /** Geocentric Coordinate Time scale. */
  57.     private TCGScale tcg = null;

  58.     /** Barycentric Dynamic Time scale. */
  59.     private TDBScale tdb = null;

  60.     /** Barycentric Coordinate Time scale. */
  61.     private TCBScale tcb = null;

  62.     /** IRNSS System Time scale. */
  63.     private IRNSSScale irnss = null;

  64.     /** BDS System Time scale. */
  65.     private BDTScale bds = null;

  66.     /**
  67.      * Create a new set of time scales with the given sources of auxiliary data. This
  68.      * constructor uses the same {@link DataProvidersManager} for the default EOP loaders
  69.      * and the default leap second loaders.
  70.      *
  71.      * @param lazyLoadedEop loads Earth Orientation Parameters for {@link
  72.      *                      #getUT1(IERSConventions, boolean)}.
  73.      */
  74.     public LazyLoadedTimeScales(final LazyLoadedEop lazyLoadedEop) {
  75.         this.lazyLoadedEop = lazyLoadedEop;
  76.     }

  77.     /**
  78.      * Add a loader for UTC-TAI offsets history files.
  79.      *
  80.      * @param loader custom loader to add
  81.      * @see TAIUTCDatFilesLoader
  82.      * @see UTCTAIHistoryFilesLoader
  83.      * @see UTCTAIBulletinAFilesLoader
  84.      * @see #getUTC()
  85.      * @see #clearUTCTAIOffsetsLoaders()
  86.      * @since 7.1
  87.      */
  88.     public void addUTCTAIOffsetsLoader(final UTCTAIOffsetsLoader loader) {
  89.         synchronized (this) {
  90.             loaders.add(loader);
  91.         }
  92.     }

  93.     /**
  94.      * Add the default loaders for UTC-TAI offsets history files (both IERS and USNO).
  95.      * <p>
  96.      * The default loaders are {@link TAIUTCDatFilesLoader} that looks for a file named
  97.      * {@code tai-utc.dat} that must be in USNO format, {@link
  98.      * UTCTAIHistoryFilesLoader} that looks for a file named {@code UTC-TAI.history} that
  99.      * must be in the IERS format and {@link AGILeapSecondFilesLoader} that looks for a
  100.      * files named {@code LeapSecond.dat} that must be in AGI format. The {@link
  101.      * UTCTAIBulletinAFilesLoader} is<em>not</em> added by default as it is not recommended.
  102.      * USNO warned us that the TAI-UTC data present in bulletin A was for convenience only
  103.      * and was not reliable, there have been errors in several bulletins regarding these data.
  104.      * </p>
  105.      *
  106.      * @see <a href="http://maia.usno.navy.mil/ser7/tai-utc.dat">USNO tai-utc.dat
  107.      * file</a>
  108.      * @see <a href="http://hpiers.obspm.fr/eoppc/bul/bulc/UTC-TAI.history">IERS
  109.      * UTC-TAI.history file</a>
  110.      * @see TAIUTCDatFilesLoader
  111.      * @see UTCTAIHistoryFilesLoader
  112.      * @see AGILeapSecondFilesLoader
  113.      * @see #getUTC()
  114.      * @see #clearUTCTAIOffsetsLoaders()
  115.      * @since 7.1
  116.      */
  117.     public void addDefaultUTCTAIOffsetsLoaders() {
  118.         synchronized (this) {
  119.             final DataProvidersManager dataProvidersManager =
  120.                     lazyLoadedEop.getDataProvidersManager();
  121.             addUTCTAIOffsetsLoader(new TAIUTCDatFilesLoader(TAIUTCDatFilesLoader.DEFAULT_SUPPORTED_NAMES, dataProvidersManager));
  122.             addUTCTAIOffsetsLoader(new UTCTAIHistoryFilesLoader(dataProvidersManager));
  123.             addUTCTAIOffsetsLoader(new AGILeapSecondFilesLoader(AGILeapSecondFilesLoader.DEFAULT_SUPPORTED_NAMES, dataProvidersManager));
  124.         }
  125.     }

  126.     /**
  127.      * Clear loaders for UTC-TAI offsets history files.
  128.      *
  129.      * @see #getUTC()
  130.      * @see #addUTCTAIOffsetsLoader(UTCTAIOffsetsLoader)
  131.      * @see #addDefaultUTCTAIOffsetsLoaders()
  132.      * @since 7.1
  133.      */
  134.     public void clearUTCTAIOffsetsLoaders() {
  135.         synchronized (this) {
  136.             loaders.clear();
  137.         }
  138.     }

  139.     @Override
  140.     public TAIScale getTAI() {
  141.         synchronized (this) {

  142.             if (tai == null) {
  143.                 tai = new TAIScale();
  144.             }

  145.             return tai;

  146.         }
  147.     }

  148.     @Override
  149.     public UTCScale getUTC() {
  150.         synchronized (this) {
  151.             if (utc == null) {
  152.                 List<OffsetModel> entries = Collections.emptyList();
  153.                 if (loaders.isEmpty()) {
  154.                     addDefaultUTCTAIOffsetsLoaders();
  155.                 }
  156.                 for (UTCTAIOffsetsLoader loader : loaders) {
  157.                     entries = loader.loadOffsets();
  158.                     if (!entries.isEmpty()) {
  159.                         break;
  160.                     }
  161.                 }
  162.                 if (entries.isEmpty()) {
  163.                     throw new OrekitException(OrekitMessages.NO_IERS_UTC_TAI_HISTORY_DATA_LOADED);
  164.                 }
  165.                 utc = new UTCScale(getTAI(), entries);
  166.             }

  167.             return utc;
  168.         }
  169.     }

  170.     @Override
  171.     public UT1Scale getUT1(final IERSConventions conventions, final boolean simpleEOP) {
  172.         // synchronized to maintain the same semantics as Orekit 10.0
  173.         synchronized (this) {
  174.             return super.getUT1(conventions, simpleEOP);
  175.         }
  176.     }

  177.     @Override
  178.     protected EOPHistory getEopHistory(final IERSConventions conventions,
  179.                                        final boolean simpleEOP) {
  180.         return lazyLoadedEop.getEOPHistory(conventions, simpleEOP, this);
  181.     }

  182.     // need to make this public for compatibility. Provides access to UT1 constructor.
  183.     /** {@inheritDoc} */
  184.     @Override
  185.     public UT1Scale getUT1(final EOPHistory history) {
  186.         return super.getUT1(history);
  187.     }

  188.     @Override
  189.     public TTScale getTT() {
  190.         synchronized (this) {

  191.             if (tt == null) {
  192.                 tt = new TTScale();
  193.             }

  194.             return tt;

  195.         }
  196.     }

  197.     @Override
  198.     public GalileoScale getGST() {
  199.         synchronized (this) {

  200.             if (gst == null) {
  201.                 gst = new GalileoScale();
  202.             }

  203.             return gst;

  204.         }
  205.     }

  206.     @Override
  207.     public GLONASSScale getGLONASS() {
  208.         synchronized (this) {

  209.             if (glonass == null) {
  210.                 glonass = new GLONASSScale(getUTC());
  211.             }

  212.             return glonass;

  213.         }
  214.     }

  215.     @Override
  216.     public QZSSScale getQZSS() {
  217.         synchronized (this) {

  218.             if (qzss == null) {
  219.                 qzss = new QZSSScale();
  220.             }

  221.             return qzss;

  222.         }
  223.     }

  224.     @Override
  225.     public GPSScale getGPS() {
  226.         synchronized (this) {

  227.             if (gps == null) {
  228.                 gps = new GPSScale();
  229.             }

  230.             return gps;

  231.         }
  232.     }

  233.     @Override
  234.     public TCGScale getTCG() {
  235.         synchronized (this) {

  236.             if (tcg == null) {
  237.                 tcg = new TCGScale(getTT(), getTAI());
  238.             }

  239.             return tcg;

  240.         }
  241.     }

  242.     @Override
  243.     public TDBScale getTDB() {
  244.         synchronized (this) {

  245.             if (tdb == null) {
  246.                 tdb = new TDBScale(getTT(), getJ2000Epoch());
  247.             }

  248.             return tdb;

  249.         }
  250.     }

  251.     @Override
  252.     public TCBScale getTCB() {
  253.         synchronized (this) {

  254.             if (tcb == null) {
  255.                 tcb = new TCBScale(getTDB(), getTAI());
  256.             }

  257.             return tcb;

  258.         }
  259.     }

  260.     @Override
  261.     public GMSTScale getGMST(final IERSConventions conventions, final boolean simpleEOP) {
  262.         // synchronized to maintain the same semantics as Orekit 10.0
  263.         synchronized (this) {
  264.             return super.getGMST(conventions, simpleEOP);
  265.         }
  266.     }

  267.     @Override
  268.     public IRNSSScale getIRNSS() {
  269.         synchronized (this) {

  270.             if (irnss == null) {
  271.                 irnss = new IRNSSScale();
  272.             }

  273.             return irnss;

  274.         }
  275.     }

  276.     @Override
  277.     public BDTScale getBDT() {
  278.         synchronized (this) {

  279.             if (bds == null) {
  280.                 bds = new BDTScale();
  281.             }

  282.             return bds;

  283.         }
  284.     }

  285. }