SampledClockModel.java

  1. /* Copyright 2022-2025 Thales Alenia Space
  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.hipparchus.Field;
  20. import org.orekit.utils.ImmutableTimeStampedCache;

  21. import java.util.List;
  22. import java.util.stream.Stream;

  23. /** Offset clock model backed up by a sample.
  24.  * @author Luc Maisonobe
  25.  * @since 12.1
  26.  */
  27. public class SampledClockModel implements ClockModel {

  28.     /** sample. */
  29.     private final ImmutableTimeStampedCache<ClockOffset> sample;

  30.     /** Simple constructor.
  31.      * @param sample clock offsets sample
  32.      * @param nbInterpolationPoints number of points to use in interpolation
  33.      */
  34.     public SampledClockModel(final List<ClockOffset> sample, final int nbInterpolationPoints) {
  35.         this.sample = new ImmutableTimeStampedCache<>(nbInterpolationPoints, sample);
  36.     }

  37.     /** Get the clock offsets cache.
  38.      * @return clock offsets cache
  39.      */
  40.     public ImmutableTimeStampedCache<ClockOffset> getCache() {
  41.         return sample;
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     public AbsoluteDate getValidityStart() {
  46.         return sample.getEarliest().getDate();
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public AbsoluteDate getValidityEnd() {
  51.         return sample.getLatest().getDate();
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public ClockOffset getOffset(final AbsoluteDate date) {
  56.         return new ClockOffsetHermiteInterpolator(sample.getMaxNeighborsSize()).
  57.             interpolate(date, sample.getNeighbors(date));
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public <T extends CalculusFieldElement<T>> FieldClockOffset<T> getOffset(final FieldAbsoluteDate<T> date) {

  62.         // convert the neighbors to field
  63.         final Field<T> field = date.getField();
  64.         final T        zero  = field.getZero();
  65.         final Stream<FieldClockOffset<T>> fieldSample =
  66.             sample.
  67.                 getNeighbors(date.toAbsoluteDate()).
  68.                 map(c -> {
  69.                     final FieldAbsoluteDate<T> dateF   = new FieldAbsoluteDate<>(field, c.getDate());
  70.                     final T                    offsetF = zero.newInstance(c.getOffset());
  71.                     final T rateF;
  72.                     final T accelerationF;
  73.                     if (Double.isNaN(c.getRate())) {
  74.                         // no rate available
  75.                         rateF         = null;
  76.                         accelerationF = null;
  77.                     } else {
  78.                         // rate available
  79.                         rateF = zero.newInstance(c.getRate());
  80.                         accelerationF = Double.isNaN(c.getAcceleration()) ?
  81.                                         null :
  82.                                         zero.newInstance(c.getAcceleration());
  83.                     }
  84.                     return new FieldClockOffset<>(dateF, offsetF, rateF, accelerationF);
  85.                 });

  86.         // perform interpolation
  87.         final FieldClockOffsetHermiteInterpolator<T> interpolator =
  88.             new FieldClockOffsetHermiteInterpolator<>(sample.getMaxNeighborsSize());
  89.         return interpolator.interpolate(date, fieldSample);

  90.     }

  91. }