SatelliteClockScale.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.time;

  18. import org.hipparchus.CalculusFieldElement;

  19. /** Scale for on-board clock.
  20.  * @author Luc Maisonobe
  21.  * @since 11.0
  22.  */
  23. public class SatelliteClockScale implements TimeScale {

  24.     /** Name of the scale. */
  25.     private final String name;

  26.     /** Reference epoch. */
  27.     private final AbsoluteDate epoch;

  28.     /** Reference epoch. */
  29.     private final DateTimeComponents epochDT;

  30.     /** Offset from TAI at epoch. */
  31.     private final TimeOffset offsetAtEpoch;

  32.     /** Clock count at epoch. */
  33.     private final double countAtEpoch;

  34.     /** Clock drift (i.e. clock count per SI second minus 1.0). */
  35.     private final double drift;

  36.     /** Clock rate. */
  37.     private final double rate;

  38.     /** Create a linear model for satellite clock.
  39.      * <p>
  40.      * Beware that we specify the model using its drift with respect to
  41.      * flow of time. For a perfect clock without any drift, the clock
  42.      * count would be one tick every SI second. A clock that is fast, say
  43.      * for example it generates 1000001 ticks every 1000000 SI second, would
  44.      * have a rate of 1.000001 tick per SI second and hence a drift of
  45.      * 1.0e-6 tick per SI second. In this constructor we use the drift
  46.      * (1.0e-6 in the previous example) rather than the rate (1.000001
  47.      * in the previous example) to specify the clock. The rationale is
  48.      * that for clocks that are intended to be used for representing absolute
  49.      * time, the drift is expected to be small (much smaller that 1.0e-6
  50.      * for a good clock), so using drift is numerically more stable than
  51.      * using rate and risking catastrophic cancellation when subtracting
  52.      * 1.0 in the internal computation.
  53.      * </p>
  54.      * <p>
  55.      * Despite what is explained in the previous paragraph, this class can
  56.      * handle spacecraft clocks that are not intended to be synchronized with
  57.      * SI seconds, for example clocks that ticks at 10 Hz. In such cases the
  58.      * drift would need to be set at 10.0 - 1.0 = 9.0, which is not intuitive.
  59.      * For these clocks, the methods {@link #countAtDate(AbsoluteDate)} and
  60.      * {@link #dateAtCount(double)} and perhaps {@link #offsetFromTAI(AbsoluteDate)}
  61.      * are still useful, whereas {@link #offsetToTAI(DateComponents, TimeComponents)}
  62.      * is probably not really meaningful.
  63.      * </p>
  64.      * @param name of the scale
  65.      * @param epoch reference epoch
  66.      * @param epochScale time scale in which the {@code epoch} was defined
  67.      * @param countAtEpoch clock count at {@code epoch}
  68.      * @param drift clock drift rate (i.e. clock count change per SI second minus 1.0)
  69.      */
  70.     public SatelliteClockScale(final String name,
  71.                                final AbsoluteDate epoch, final TimeScale epochScale,
  72.                                final double countAtEpoch, final double drift) {
  73.         this.name          = name;
  74.         this.epoch         = epoch;
  75.         this.epochDT       = epoch.getComponents(epochScale);
  76.         this.offsetAtEpoch = epochScale.offsetFromTAI(epoch).add(new TimeOffset(countAtEpoch));
  77.         this.countAtEpoch  = countAtEpoch;
  78.         this.drift         = drift;
  79.         this.rate          = 1.0 + drift;
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public TimeOffset offsetFromTAI(final AbsoluteDate date) {
  84.         return offsetAtEpoch.add(new TimeOffset(drift * date.durationFrom(epoch)));
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public TimeOffset offsetToTAI(final DateComponents date, final TimeComponents time) {
  89.         final long      deltaDate      = (date.getJ2000Day() - epochDT.getDate().getJ2000Day()) *
  90.                                          TimeOffset.DAY.getSeconds();
  91.         final TimeOffset deltaTime      = time.getSplitSecondsInUTCDay().
  92.                                          subtract(epochDT.getTime().getSplitSecondsInUTCDay());
  93.         final double    delta          = deltaDate + deltaTime.toDouble();
  94.         final double    timeSinceEpoch = (delta - countAtEpoch) / rate;
  95.         return offsetAtEpoch.add(new TimeOffset(drift * timeSinceEpoch)).negate();
  96.     }

  97.     /** Compute date corresponding to some clock count.
  98.      * @param count clock count
  99.      * @return date at {@code count}
  100.      */
  101.     public AbsoluteDate dateAtCount(final double count) {
  102.         return epoch.shiftedBy((count - countAtEpoch) / rate);
  103.     }

  104.     /** Compute clock count corresponding to some date.
  105.      * @param date date
  106.      * @return clock count at {@code date}
  107.      */
  108.     public double countAtDate(final AbsoluteDate date) {
  109.         return countAtEpoch + rate * date.durationFrom(epoch);
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     public <T extends CalculusFieldElement<T>> T offsetFromTAI(final FieldAbsoluteDate<T> date) {
  114.         return date.durationFrom(epoch).multiply(drift).add(offsetAtEpoch.toDouble());
  115.     }

  116.     /** {@inheritDoc} */
  117.     public String getName() {
  118.         return name;
  119.     }

  120.     /** {@inheritDoc} */
  121.     public String toString() {
  122.         return getName();
  123.     }

  124. }