SatelliteClockScale.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 org.hipparchus.CalculusFieldElement;
  19. import org.orekit.utils.Constants;

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

  25.     /** Serializable UID. */
  26.     private static final long serialVersionUID = 20210309L;

  27.     /** Name of the scale. */
  28.     private final String name;

  29.     /** Reference epoch. */
  30.     private final AbsoluteDate epoch;

  31.     /** Reference epoch. */
  32.     private final DateTimeComponents epochDT;

  33.     /** Offset from TAI at epoch. */
  34.     private final double offsetAtEpoch;

  35.     /** Clock count at epoch. */
  36.     private final double countAtEpoch;

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

  39.     /** Clock rate. */
  40.     private final double rate;

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

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public double offsetFromTAI(final AbsoluteDate date) {
  87.         return offsetAtEpoch + drift * date.durationFrom(epoch);
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public double offsetToTAI(final DateComponents date, final TimeComponents time) {
  92.         final double delta          = Constants.JULIAN_DAY * (date.getJ2000Day() - epochDT.getDate().getJ2000Day()) +
  93.                                       time.getSecondsInUTCDay() - epochDT.getTime().getSecondsInUTCDay();
  94.         final double timeSinceEpoch = (delta - countAtEpoch) / rate;
  95.         return -(offsetAtEpoch + drift * timeSinceEpoch);
  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);
  115.     }

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

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

  124. }