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 java.util.concurrent.atomic.AtomicReference;

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

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

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

  41.     /** UTCTAI offsets loaders. */
  42.     private final List<UTCTAIOffsetsLoader> loaders = new ArrayList<>();

  43.     /** Universal Time Coordinate scale. */
  44.     private AtomicReference<UTCScale> utc = new AtomicReference<>();

  45.     /** International Atomic Time scale. */
  46.     private AtomicReference<TAIScale> tai = new AtomicReference<>();

  47.     /** Terrestrial Time scale. */
  48.     private AtomicReference<TTScale> tt = new AtomicReference<>();

  49.     /** Galileo System Time scale. */
  50.     private AtomicReference<GalileoScale> gst = new AtomicReference<>();

  51.     /** GLObal NAvigation Satellite System scale. */
  52.     private AtomicReference<GLONASSScale> glonass = new AtomicReference<>();

  53.     /** Quasi-Zenith Satellite System scale. */
  54.     private AtomicReference<QZSSScale> qzss = new AtomicReference<>();

  55.     /** Global Positioning System scale. */
  56.     private AtomicReference<GPSScale> gps = new AtomicReference<>();

  57.     /** Geocentric Coordinate Time scale. */
  58.     private AtomicReference<TCGScale> tcg = new AtomicReference<>();

  59.     /** Barycentric Dynamic Time scale. */
  60.     private AtomicReference<TDBScale> tdb = new AtomicReference<>();

  61.     /** Barycentric Coordinate Time scale. */
  62.     private AtomicReference<TCBScale> tcb = new AtomicReference<>();

  63.     /** IRNSS System Time scale. */
  64.     private AtomicReference<IRNSSScale> irnss = new AtomicReference<>();

  65.     /** BDS System Time scale. */
  66.     private AtomicReference<BDTScale> bds = new AtomicReference<>();

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

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

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

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

  140.     @Override
  141.     public TAIScale getTAI() {

  142.         TAIScale refTai = tai.get();
  143.         if (refTai == null) {
  144.             tai.compareAndSet(null, new TAIScale());
  145.             refTai = tai.get();
  146.         }

  147.         return refTai;

  148.     }

  149.     @Override
  150.     public UTCScale getUTC() {

  151.         UTCScale refUtc = utc.get();
  152.         if (refUtc == null) {
  153.             List<OffsetModel> entries = Collections.emptyList();
  154.             if (loaders.isEmpty()) {
  155.                 addDefaultUTCTAIOffsetsLoaders();
  156.             }
  157.             for (UTCTAIOffsetsLoader loader : loaders) {
  158.                 entries = loader.loadOffsets();
  159.                 if (!entries.isEmpty()) {
  160.                     break;
  161.                 }
  162.             }
  163.             if (entries.isEmpty()) {
  164.                 throw new OrekitException(OrekitMessages.NO_IERS_UTC_TAI_HISTORY_DATA_LOADED);
  165.             }
  166.             utc.compareAndSet(null, new UTCScale(getTAI(), entries));
  167.             refUtc = utc.get();
  168.         }

  169.         return refUtc;

  170.     }

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

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

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

  189.     @Override
  190.     public TTScale getTT() {

  191.         TTScale refTt = tt.get();
  192.         if (refTt == null) {
  193.             tt.compareAndSet(null, new TTScale());
  194.             refTt = tt.get();
  195.         }

  196.         return refTt;

  197.     }

  198.     @Override
  199.     public GalileoScale getGST() {

  200.         GalileoScale refGst = gst.get();
  201.         if (refGst == null) {
  202.             gst.compareAndSet(null, new GalileoScale());
  203.             refGst = gst.get();
  204.         }

  205.         return refGst;

  206.     }

  207.     @Override
  208.     public GLONASSScale getGLONASS() {

  209.         GLONASSScale refGlonass = glonass.get();
  210.         if (refGlonass == null) {
  211.             glonass.compareAndSet(null, new GLONASSScale(getUTC()));
  212.             refGlonass = glonass.get();
  213.         }

  214.         return refGlonass;

  215.     }

  216.     @Override
  217.     public QZSSScale getQZSS() {

  218.         QZSSScale refQzss = qzss.get();
  219.         if (refQzss == null) {
  220.             qzss.compareAndSet(null, new QZSSScale());
  221.             refQzss = qzss.get();
  222.         }

  223.         return refQzss;

  224.     }

  225.     @Override
  226.     public GPSScale getGPS() {

  227.         GPSScale refGps = gps.get();
  228.         if (refGps == null) {
  229.             gps.compareAndSet(null, new GPSScale());
  230.             refGps = gps.get();
  231.         }

  232.         return refGps;

  233.     }

  234.     @Override
  235.     public TCGScale getTCG() {

  236.         TCGScale refTcg = tcg.get();
  237.         if (refTcg == null) {
  238.             tcg.compareAndSet(null, new TCGScale(getTT(), getTAI()));
  239.             refTcg = tcg.get();
  240.         }

  241.         return refTcg;

  242.     }

  243.     @Override
  244.     public TDBScale getTDB() {

  245.         TDBScale refTdb = tdb.get();
  246.         if (refTdb == null) {
  247.             tdb.compareAndSet(null, new TDBScale(getTT(), getJ2000Epoch()));
  248.             refTdb = tdb.get();
  249.         }

  250.         return refTdb;

  251.     }

  252.     @Override
  253.     public TCBScale getTCB() {

  254.         TCBScale refTcb = tcb.get();
  255.         if (refTcb == null) {
  256.             tcb.compareAndSet(null, new TCBScale(getTDB(), getTAI()));
  257.             refTcb = tcb.get();
  258.         }

  259.         return refTcb;

  260.     }

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

  268.     @Override
  269.     public IRNSSScale getIRNSS() {

  270.         IRNSSScale refIrnss = irnss.get();
  271.         if (refIrnss == null) {
  272.             irnss.compareAndSet(null, new IRNSSScale());
  273.             refIrnss = irnss.get();
  274.         }

  275.         return refIrnss;

  276.     }

  277.     @Override
  278.     public BDTScale getBDT() {

  279.         BDTScale refBds = bds.get();
  280.         if (refBds == null) {
  281.             bds.compareAndSet(null, new BDTScale());
  282.             refBds = bds.get();
  283.         }

  284.         return refBds;

  285.     }

  286. }