UTCTAIOffset.java

  1. /* Copyright 2002-2024 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 org.hipparchus.CalculusFieldElement;
  20. import org.orekit.utils.Constants;

  21. /** Offset between {@link UTCScale UTC} and  {@link TAIScale TAI} time scales.
  22.  * <p>The {@link UTCScale UTC} and  {@link TAIScale TAI} time scales are two
  23.  * scales offset with respect to each other. The {@link TAIScale TAI} scale is
  24.  * continuous whereas the {@link UTCScale UTC} includes some discontinuity when
  25.  * leap seconds are introduced by the <a href="http://www.iers.org/">International
  26.  * Earth Rotation Service</a> (IERS).</p>
  27.  * <p>This class represents the offset between the two scales that is
  28.  * valid between two leap seconds occurrences. It handles both the linear offsets
  29.  * used from 1961-01-01 to 1971-12-31 and the constant integer offsets used since
  30.  * 1972-01-01.</p>
  31.  * @author Luc Maisonobe
  32.  * @see UTCScale
  33.  * @see UTCTAIHistoryFilesLoader
  34.  */
  35. public class UTCTAIOffset implements TimeStamped, Serializable {

  36.     /** Serializable UID. */
  37.     private static final long serialVersionUID = 4742190573136348054L;

  38.     /** Leap date. */
  39.     private final AbsoluteDate leapDate;

  40.     /** Leap date in Modified Julian Day. */
  41.     private final int leapDateMJD;

  42.     /** Offset start of validity date. */
  43.     private final AbsoluteDate validityStart;

  44.     /** Reference date for the slope multiplication as Modified Julian Day. */
  45.     private final int mjdRef;

  46.     /** Reference date for the slope multiplication. */
  47.     private final AbsoluteDate reference;

  48.     /** Value of the leap at offset validity start (in seconds). */
  49.     private final double leap;

  50.     /** Offset at validity start in seconds (TAI minus UTC). */
  51.     private final double offset;

  52.     /** Offset slope in seconds per UTC second (TAI minus UTC / dUTC). */
  53.     private final double slopeUTC;

  54.     /** Offset slope in seconds per TAI second (TAI minus UTC / dTAI). */
  55.     private final double slopeTAI;

  56.     /** Simple constructor for a linear model.
  57.      * @param leapDate leap date
  58.      * @param leapDateMJD leap date in Modified Julian Day
  59.      * @param leap value of the leap at offset validity start (in seconds)
  60.      * @param offset offset in seconds (TAI minus UTC)
  61.      * @param mjdRef reference date for the slope multiplication as Modified Julian Day
  62.      * @param slope offset slope in seconds per UTC second (TAI minus UTC / dUTC)
  63.      * @param reference date for slope computations.
  64.      */
  65.     UTCTAIOffset(final AbsoluteDate leapDate, final int leapDateMJD,
  66.                  final double leap, final double offset,
  67.                  final int mjdRef, final double slope, final AbsoluteDate reference) {
  68.         this.leapDate      = leapDate;
  69.         this.leapDateMJD   = leapDateMJD;
  70.         this.validityStart = leapDate.shiftedBy(leap);
  71.         this.mjdRef        = mjdRef;
  72.         this.reference     = reference;
  73.         this.leap          = leap;
  74.         this.offset        = offset;
  75.         this.slopeUTC      = slope;
  76.         this.slopeTAI      = slope / (1 + slope);
  77.     }

  78.     /** Get the date of the start of the leap.
  79.      * @return date of the start of the leap
  80.      * @see #getValidityStart()
  81.      */
  82.     public AbsoluteDate getDate() {
  83.         return leapDate;
  84.     }

  85.     /** Get the date of the start of the leap as Modified Julian Day.
  86.      * @return date of the start of the leap as Modified Julian Day
  87.      */
  88.     public int getMJD() {
  89.         return leapDateMJD;
  90.     }

  91.     /** Get the start time of validity for this offset.
  92.      * <p>The start of the validity of the offset is {@link #getLeap()}
  93.      * seconds after the start of the leap itself.</p>
  94.      * @return start of validity date
  95.      * @see #getDate()
  96.      */
  97.     public AbsoluteDate getValidityStart() {
  98.         return validityStart;
  99.     }

  100.     /** Get the value of the leap at offset validity start (in seconds).
  101.      * @return value of the leap at offset validity start (in seconds)
  102.      */
  103.     public double getLeap() {
  104.         return leap;
  105.     }

  106.     /** Get the TAI - UTC offset in seconds.
  107.      * @param date date at which the offset is requested
  108.      * @return TAI - UTC offset in seconds.
  109.      */
  110.     public double getOffset(final AbsoluteDate date) {
  111.         if (slopeTAI == 0) {
  112.             // we use an if statement here so the offset computation returns
  113.             // a finite value when date is AbsoluteDate.FUTURE_INFINITY
  114.             // without this if statement, the multiplication between an
  115.             // infinite duration and a zero slope would induce a NaN offset
  116.             return offset;
  117.         } else {
  118.             return offset + date.durationFrom(reference) * slopeTAI;
  119.         }
  120.     }

  121.     /** Get the TAI - UTC offset in seconds.
  122.      * @param date date at which the offset is requested
  123.      * @param <T> type of the filed elements
  124.      * @return TAI - UTC offset in seconds.
  125.      * @since 9.0
  126.      */
  127.     public <T extends CalculusFieldElement<T>> T getOffset(final FieldAbsoluteDate<T> date) {
  128.         if (slopeTAI == 0) {
  129.             // we use an if statement here so the offset computation returns
  130.             // a finite value when date is FieldAbsoluteDate.getFutureInfinity(field)
  131.             // without this if statement, the multiplication between an
  132.             // infinite duration and a zero slope would induce a NaN offset
  133.             return date.getField().getZero().add(offset);
  134.         } else {
  135.             return date.durationFrom(reference).multiply(slopeTAI).add(offset);
  136.         }
  137.     }

  138.     /** Get the TAI - UTC offset in seconds.
  139.      * @param date date components (in UTC) at which the offset is requested
  140.      * @param time time components (in UTC) at which the offset is requested
  141.      * @return TAI - UTC offset in seconds.
  142.      */
  143.     public double getOffset(final DateComponents date, final TimeComponents time) {
  144.         final int    days     = date.getMJD() - mjdRef;
  145.         final double fraction = time.getSecondsInUTCDay();
  146.         return offset + days * (slopeUTC * Constants.JULIAN_DAY) + fraction * slopeUTC;
  147.     }

  148. }