GNSSDate.java

  1. /* Copyright 2002-2020 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.time;

  18. import java.io.Serializable;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.concurrent.atomic.AtomicReference;

  23. import org.hipparchus.util.FastMath;
  24. import org.orekit.annotation.DefaultDataContext;
  25. import org.orekit.data.DataContext;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.frames.EOPEntry;
  29. import org.orekit.gnss.SatelliteSystem;
  30. import org.orekit.utils.Constants;
  31. import org.orekit.utils.IERSConventions;

  32. /** Container for date in GNSS form.
  33.  * <p> This class can be used to handle {@link SatelliteSystem#GPS GPS},
  34.  * {@link SatelliteSystem#GALILEO Galileo}, {@link SatelliteSystem#BEIDOU BeiDou}
  35.  * and {@link SatelliteSystem#QZSS QZSS} dates. </p>
  36.  * @author Luc Maisonobe (original code)
  37.  * @author Bryan Cazabonne (generalization to all GNSS constellations)
  38.  * @see AbsoluteDate
  39.  */
  40. public class GNSSDate implements Serializable, TimeStamped {

  41.     /** Serializable UID. */
  42.     private static final long serialVersionUID = 201902141L;

  43.     /** Duration of a week in days. */
  44.     private static final int WEEK_D = 7;

  45.     /** Duration of a week in seconds. */
  46.     private static final double WEEK_S = WEEK_D * Constants.JULIAN_DAY;

  47.     /** Conversion factor from seconds to milliseconds. */
  48.     private static final double S_TO_MS = 1000.0;

  49.     /** Reference date for ensuring continuity across GNSS week rollover.
  50.      * @since 9.3.1
  51.      */
  52.     private static AtomicReference<DateComponents> rolloverReference = new AtomicReference<DateComponents>(null);

  53.     /** Week number since the GNSS reference epoch. */
  54.     private final int weekNumber;

  55.     /** Number of milliseconds since week start. */
  56.     private final double milliInWeek;

  57.     /** Satellite system to consider. */
  58.     private final SatelliteSystem system;

  59.     /** Corresponding date. */
  60.     private final transient AbsoluteDate date;

  61.     /** Build an instance corresponding to a GNSS date.
  62.      * <p>
  63.      * GNSS dates are provided as a week number starting at
  64.      * the GNSS reference epoch and as a number of milliseconds
  65.      * since week start.
  66.      * </p>
  67.      * <p>
  68.      * Many interfaces provide week number modulo the constellation week cycle. In order to cope with
  69.      * this, when the week number is smaller than the week cycle, this constructor assumes a modulo operation
  70.      * has been performed and it will fix the week number according to the reference date set up for
  71.      * handling rollover (see {@link #setRolloverReference(DateComponents) setRolloverReference(reference)}).
  72.      * If the week number is equal to the week cycle or larger, it will be used without any correction.
  73.      * </p>
  74.      *
  75.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  76.      *
  77.      * @param weekNumber week number
  78.      * @param milliInWeek number of milliseconds since week start
  79.      * @param system satellite system to consider
  80.      * @see #GNSSDate(int, double, SatelliteSystem, TimeScales)
  81.      */
  82.     @DefaultDataContext
  83.     public GNSSDate(final int weekNumber, final double milliInWeek,
  84.                     final SatelliteSystem system) {
  85.         this(weekNumber, milliInWeek, system, DataContext.getDefault().getTimeScales());
  86.     }

  87.     /**
  88.      * Build an instance corresponding to a GNSS date.
  89.      * <p>
  90.      * GNSS dates are provided as a week number starting at the GNSS reference epoch and
  91.      * as a number of milliseconds since week start.
  92.      * </p>
  93.      * <p>
  94.      * Many interfaces provide week number modulo the constellation week cycle. In order
  95.      * to cope with this, when the week number is smaller than the week cycle, this
  96.      * constructor assumes a modulo operation has been performed and it will fix the week
  97.      * number according to the reference date set up for handling rollover (see {@link
  98.      * #setRolloverReference(DateComponents) setRolloverReference(reference)}). If the
  99.      * week number is equal to the week cycle or larger, it will be used without any
  100.      * correction.
  101.      * </p>
  102.      *
  103.      * @param weekNumber  week number
  104.      * @param milliInWeek number of milliseconds since week start
  105.      * @param system      satellite system to consider
  106.      * @param timeScales  the set of time scales. Used to retrieve the appropriate time
  107.      *                    scale for the given {@code system}.
  108.      * @since 10.1
  109.      */
  110.     public GNSSDate(final int weekNumber,
  111.                     final double milliInWeek,
  112.                     final SatelliteSystem system,
  113.                     final TimeScales timeScales) {

  114.         final int day = (int) FastMath.floor(milliInWeek / (Constants.JULIAN_DAY * S_TO_MS));
  115.         final double secondsInDay = milliInWeek / S_TO_MS - day * Constants.JULIAN_DAY;

  116.         int w = weekNumber;
  117.         DateComponents dc = new DateComponents(getWeekReferenceDateComponents(system), weekNumber * 7 + day);
  118.         final int cycleW = GNSSDateType.getRollOverWeek(system);
  119.         if (weekNumber < cycleW) {

  120.             DateComponents reference = rolloverReference.get();
  121.             if (reference == null) {
  122.                 // lazy setting of a default reference, using end of EOP entries
  123.                 final UT1Scale       ut1       = timeScales.getUT1(IERSConventions.IERS_2010, true);
  124.                 final List<EOPEntry> eop       = ut1.getEOPHistory().getEntries();
  125.                 final int            lastMJD   = eop.get(eop.size() - 1).getMjd();
  126.                 reference = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, lastMJD);
  127.                 rolloverReference.compareAndSet(null, reference);
  128.             }

  129.             // fix GNSS week rollover
  130.             final int cycleD = WEEK_D * cycleW;
  131.             while (dc.getJ2000Day() < reference.getJ2000Day() - cycleD / 2) {
  132.                 dc = new DateComponents(dc, cycleD);
  133.                 w += cycleW;
  134.             }

  135.         }

  136.         this.weekNumber  = w;
  137.         this.milliInWeek = milliInWeek;
  138.         this.system      = system;

  139.         date = new AbsoluteDate(dc, new TimeComponents(secondsInDay), getTimeScale(system, timeScales));

  140.     }

  141.     /** Build an instance from an absolute date.
  142.      *
  143.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  144.      *
  145.      * @param date absolute date to consider
  146.      * @param system satellite system to consider
  147.      * @see #GNSSDate(AbsoluteDate, SatelliteSystem, TimeScales)
  148.      */
  149.     @DefaultDataContext
  150.     public GNSSDate(final AbsoluteDate date, final SatelliteSystem system) {
  151.         this(date, system, DataContext.getDefault().getTimeScales());
  152.     }

  153.     /**
  154.      * Build an instance from an absolute date.
  155.      *
  156.      * @param date       absolute date to consider
  157.      * @param system     satellite system to consider
  158.      * @param timeScales the set of time scales. Used to retrieve the appropriate time
  159.      *                   scale for the given {@code system}.
  160.      * @since 10.1
  161.      */
  162.     public GNSSDate(final AbsoluteDate date,
  163.                     final SatelliteSystem system,
  164.                     final TimeScales timeScales) {

  165.         this.system = system;
  166.         final AbsoluteDate epoch = getWeekReferenceAbsoluteDate(system, timeScales);
  167.         this.weekNumber  = (int) FastMath.floor(date.durationFrom(epoch) / WEEK_S);
  168.         final AbsoluteDate weekStart = new AbsoluteDate(epoch, WEEK_S * weekNumber);
  169.         this.milliInWeek = date.durationFrom(weekStart) * S_TO_MS;
  170.         this.date        = date;

  171.     }

  172.     /** Set a reference date for ensuring continuity across GNSS week rollover.
  173.      * <p>
  174.      * Instance created using the {@link #GNSSDate(int, double, SatelliteSystem) GNSSDate(weekNumber, milliInWeek, system)}
  175.      * constructor and with a week number between 0 and the constellation week cycle (cycleW) after this method has been called will
  176.      * fix the week number to ensure they correspond to dates between {@code reference - cycleW / 2 weeks}
  177.      * and {@code reference + cycleW / 2 weeks}.
  178.      * </p>
  179.      * <p>
  180.      * If this method is never called, a default reference date for rollover will be set using
  181.      * the date of the last known EOP entry retrieved from {@link UT1Scale#getEOPHistory() UT1}
  182.      * time scale.
  183.      * </p>
  184.      * @param reference reference date for GNSS week rollover
  185.      * @see #getRolloverReference()
  186.      * @see #GNSSDate(int, double, SatelliteSystem)
  187.      * @since 9.3.1
  188.      */
  189.     public static void setRolloverReference(final DateComponents reference) {
  190.         rolloverReference.set(reference);
  191.     }

  192.     /** Get the reference date ensuring continuity across GNSS week rollover.
  193.      * @return reference reference date for GNSS week rollover
  194.      * @see #setRolloverReference(DateComponents)
  195.      * @see #GNSSDate(int, double, SatelliteSystem)
  196.      * @since 9.3.1
  197.      */
  198.     public static DateComponents getRolloverReference() {
  199.         return rolloverReference.get();
  200.     }

  201.     /** Get the week number since the GNSS reference epoch.
  202.      * <p>
  203.      * The week number returned here has been fixed for GNSS week rollover, i.e.
  204.      * it may be larger than the corresponding week cycle of the constellation.
  205.      * </p>
  206.      * @return week number since since the GNSS reference epoch
  207.      */
  208.     public int getWeekNumber() {
  209.         return weekNumber;
  210.     }

  211.     /** Get the number of milliseconds since week start.
  212.      * @return number of milliseconds since week start
  213.      */
  214.     public double getMilliInWeek() {
  215.         return milliInWeek;
  216.     }

  217.     /** {@inheritDoc} */
  218.     @Override
  219.     public AbsoluteDate getDate() {
  220.         return date;
  221.     }

  222.     /** Get the time scale related to the given satellite system.
  223.      * @param satellite satellite system
  224.      * @param timeScales set of time scales.
  225.      * @return the time scale
  226.      */
  227.     private TimeScale getTimeScale(final SatelliteSystem satellite,
  228.                                    final TimeScales timeScales) {
  229.         switch (satellite) {
  230.             case GPS     : return timeScales.getGPS();
  231.             case GALILEO : return timeScales.getGST();
  232.             case QZSS    : return timeScales.getQZSS();
  233.             case BEIDOU  : return timeScales.getBDT();
  234.             case IRNSS   : return timeScales.getIRNSS();
  235.             case SBAS    : return timeScales.getGPS();
  236.             default      : throw new OrekitException(OrekitMessages.INVALID_SATELLITE_SYSTEM, satellite);
  237.         }
  238.     }

  239.     /** Get the reference epoch of the week number for the given satellite system.
  240.      * <p> Returned parameter is an AbsoluteDate. </p>
  241.      * @param satellite satellite system
  242.      * @param timeScales set of time scales.
  243.      * @return the reference epoch
  244.      */
  245.     private AbsoluteDate getWeekReferenceAbsoluteDate(final SatelliteSystem satellite,
  246.                                                       final TimeScales timeScales) {
  247.         switch (satellite) {
  248.             case GPS     : return timeScales.getGpsEpoch();
  249.             case GALILEO : return timeScales.getGalileoEpoch();
  250.             case QZSS    : return timeScales.getQzssEpoch();
  251.             case BEIDOU  : return timeScales.getBeidouEpoch();
  252.             case IRNSS   : return timeScales.getIrnssEpoch();
  253.             case SBAS    : return timeScales.getGpsEpoch();
  254.             default      : throw new OrekitException(OrekitMessages.INVALID_SATELLITE_SYSTEM, satellite);
  255.         }
  256.     }

  257.     /** Get the reference epoch of the week number for the given satellite system.
  258.      * <p> Returned parameter is a DateComponents. </p>
  259.      * @param satellite satellite system
  260.      * @return the reference epoch
  261.      */
  262.     private DateComponents getWeekReferenceDateComponents(final SatelliteSystem satellite) {
  263.         switch (satellite) {
  264.             case GPS     : return DateComponents.GPS_EPOCH;
  265.             case GALILEO : return DateComponents.GALILEO_EPOCH;
  266.             case QZSS    : return DateComponents.QZSS_EPOCH;
  267.             case BEIDOU  : return DateComponents.BEIDOU_EPOCH;
  268.             case IRNSS   : return DateComponents.IRNSS_EPOCH;
  269.             case SBAS    : return DateComponents.GPS_EPOCH;
  270.             default      : throw new OrekitException(OrekitMessages.INVALID_SATELLITE_SYSTEM, satellite);
  271.         }
  272.     }

  273.     /** Replace the instance with a data transfer object for serialization.
  274.      * @return data transfer object that will be serialized
  275.      */
  276.     @DefaultDataContext
  277.     private Object writeReplace() {
  278.         return new DataTransferObject(weekNumber, milliInWeek, system);
  279.     }

  280.     /** Internal class used only for serialization. */
  281.     @DefaultDataContext
  282.     private static class DataTransferObject implements Serializable {

  283.         /** Serializable UID. */
  284.         private static final long serialVersionUID = 201902141L;

  285.         /** Week number since the GNSS reference epoch. */
  286.         private final int weekNumber;

  287.         /** Number of milliseconds since week start. */
  288.         private final double milliInWeek;

  289.         /** Satellite system to consider. */
  290.         private final SatelliteSystem system;

  291.         /** Simple constructor.
  292.          * @param weekNumber week number since the GNSS reference epoch
  293.          * @param milliInWeek number of milliseconds since week start
  294.          * @param system satellite system to consider
  295.          */
  296.         DataTransferObject(final int weekNumber, final double milliInWeek,
  297.                            final SatelliteSystem system) {
  298.             this.weekNumber  = weekNumber;
  299.             this.milliInWeek = milliInWeek;
  300.             this.system      = system;
  301.         }

  302.         /** Replace the deserialized data transfer object with a {@link GNSSDate}.
  303.          * @return replacement {@link GNSSDate}
  304.          */
  305.         private Object readResolve() {
  306.             return new GNSSDate(weekNumber, milliInWeek, system);
  307.         }

  308.     }

  309.     /** Enumerate for GNSS data. */
  310.     private enum GNSSDateType {

  311.         /** GPS. */
  312.         GPS(SatelliteSystem.GPS, 1024),

  313.         /** Galileo. */
  314.         GALILEO(SatelliteSystem.GALILEO, 4096),

  315.         /** QZSS. */
  316.         QZSS(SatelliteSystem.QZSS, 1024),

  317.         /** BeiDou. */
  318.         BEIDOU(SatelliteSystem.BEIDOU, 8192),

  319.         /** IRNSS. */
  320.         IRNSS(SatelliteSystem.IRNSS, 1024),

  321.         /** SBAS. */
  322.         SBAS(SatelliteSystem.SBAS, 1024);

  323.         /** Map for the number of week in one GNSS rollover cycle. */
  324.         private static final Map<SatelliteSystem, Integer> CYCLE_MAP = new HashMap<SatelliteSystem, Integer>();
  325.         static {
  326.             for (final GNSSDateType type : values()) {
  327.                 final int             val       = type.getRollOverCycle();
  328.                 final SatelliteSystem satellite = type.getSatelliteSystem();
  329.                 CYCLE_MAP.put(satellite, val);
  330.             }
  331.         }

  332.         /** Number of week in one rollover cycle. */
  333.         private final int numberOfWeek;

  334.         /** Satellite system. */
  335.         private final SatelliteSystem satelliteSystem;

  336.         /**
  337.          * Build a new instance.
  338.          *
  339.          * @param system satellite system
  340.          * @param rollover number of week in one rollover cycle
  341.          */
  342.         GNSSDateType(final SatelliteSystem system, final int rollover) {
  343.             this.satelliteSystem = system;
  344.             this.numberOfWeek    = rollover;
  345.         }

  346.         /** Get the number of week in one rollover cycle.
  347.          * @return  the number of week in one rollover cycle
  348.          */
  349.         private int getRollOverCycle() {
  350.             return numberOfWeek;
  351.         }

  352.         /** Get the satellite system.
  353.          * @return the satellite system
  354.          */
  355.         private SatelliteSystem getSatelliteSystem() {
  356.             return satelliteSystem;
  357.         }

  358.         /** Get the number of week in one rollover cycle for the given satellite system.
  359.          *
  360.          * @param satellite satellite system
  361.          * @return the number of week in one rollover cycle for the given satellite system
  362.          */
  363.         private static int getRollOverWeek(final SatelliteSystem satellite) {
  364.             return CYCLE_MAP.get(satellite);
  365.         }

  366.     }
  367. }