FieldTurnSpan.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.gnss.attitude;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.util.FastMath;
  20. import org.orekit.time.AbsoluteDate;
  21. import org.orekit.time.FieldAbsoluteDate;
  22. import org.orekit.time.TimeStamped;

  23. /**
  24.  * Holder for time span of noon/night turns.
  25.  *
  26.  * <p>
  27.  * The boundaries estimated are updated as
  28.  * new points close to the span are evaluated.
  29.  * </p>
  30.  *
  31.  * @author Luc Maisonobe
  32.  * @since 9.3
  33.  */
  34. class FieldTurnSpan<T extends CalculusFieldElement<T>> implements TimeStamped {

  35.     /** Margin in seconds after turn end. */
  36.     private final double endMargin;

  37.     /** Best estimate of the start of the turn. */
  38.     private FieldAbsoluteDate<T> start;

  39.     /** Best estimate of the start of the turn. */
  40.     private AbsoluteDate startDouble;

  41.     /** Best estimate of the end of the turn, excluding margin. */
  42.     private FieldAbsoluteDate<T> end;

  43.     /** Best estimate of the end of the turn, including margin. */
  44.     private AbsoluteDate endPlusMarginDouble;

  45.     /** Time between start and its estimation date. */
  46.     private double startProjection;

  47.     /** Time between end and its estimation date. */
  48.     private double endProjection;

  49.     /** Turn duration. */
  50.     private T duration;

  51.     /** Simple constructor.
  52.      * @param start estimate of the start of the turn
  53.      * @param end estimate of the end of the turn, excluding margin
  54.      * @param estimationDate date at which turn boundaries have been estimated
  55.      * @param endMargin margin in seconds after turn end
  56.      */
  57.     FieldTurnSpan(final FieldAbsoluteDate<T> start, final FieldAbsoluteDate<T> end,
  58.                   final AbsoluteDate estimationDate, final double endMargin) {
  59.         final FieldAbsoluteDate<T> endPlusMargin = end.shiftedBy(endMargin);
  60.         this.endMargin           = endMargin;
  61.         this.start               = start;
  62.         this.startDouble         = start.toAbsoluteDate();
  63.         this.end                 = end;
  64.         this.endPlusMarginDouble = endPlusMargin.toAbsoluteDate();
  65.         this.startProjection     = FastMath.abs(start.durationFrom(estimationDate).getReal());
  66.         this.endProjection       = FastMath.abs(endPlusMargin.durationFrom(estimationDate).getReal());
  67.         this.duration            = end.durationFrom(start);
  68.     }

  69.     /** Update the estimate of the turn start.
  70.      * <p>
  71.      * Start boundary is updated only if it is estimated
  72.      * from a time closer to the boundary than the previous estimate.
  73.      * </p>
  74.      * @param newStart new estimate of the start of the turn
  75.      * @param estimationDate date at which turn start has been estimated
  76.      */
  77.     public void updateStart(final FieldAbsoluteDate<T> newStart, final AbsoluteDate estimationDate) {

  78.         // update the start date if this estimate is closer than the previous one
  79.         final double newStartProjection = FastMath.abs(newStart.toAbsoluteDate().durationFrom(estimationDate));
  80.         if (newStartProjection <= startProjection) {
  81.             this.start           = newStart;
  82.             this.startDouble     = newStart.toAbsoluteDate();
  83.             this.startProjection = newStartProjection;
  84.             this.duration        = end.durationFrom(start);
  85.         }

  86.     }

  87.     /** Update the estimate of the turn end.
  88.      * <p>
  89.      * End boundary is updated only if it is estimated
  90.      * from a time closer to the boundary than the previous estimate.
  91.      * </p>
  92.      * @param newEnd new estimate of the end of the turn
  93.      * @param estimationDate date at which turn end has been estimated
  94.      */
  95.     public void updateEnd(final FieldAbsoluteDate<T> newEnd, final AbsoluteDate estimationDate) {

  96.        // update the end date if this estimate is closer than the previous one
  97.         final double newEndProjection = FastMath.abs(newEnd.toAbsoluteDate().durationFrom(estimationDate));
  98.         if (newEndProjection <= endProjection) {
  99.             final FieldAbsoluteDate<T> endPlusMargin       = newEnd.shiftedBy(endMargin);
  100.             this.end                 = newEnd;
  101.             this.endPlusMarginDouble = endPlusMargin.toAbsoluteDate();
  102.             this.endProjection       = newEndProjection;
  103.             this.duration            = end.durationFrom(start);
  104.         }

  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     public AbsoluteDate getDate() {
  109.         return endPlusMarginDouble;
  110.     }

  111.     /** Get turn duration.
  112.      * @return turn duration
  113.      */
  114.     public T getTurnDuration() {
  115.         return duration;
  116.     }

  117.     /** Get turn start date.
  118.      * @return turn start date
  119.      */
  120.     public FieldAbsoluteDate<T> getTurnStartDate() {
  121.         return start;
  122.     }

  123.     /** Get turn end date (without margin).
  124.      * @return turn end date (without margin)
  125.      */
  126.     public FieldAbsoluteDate<T> getTurnEndDate() {
  127.         return end;
  128.     }

  129.     /** Check if a date is within range.
  130.      * @param date date to check
  131.      * @return true if date is within range extended by end margin,
  132.      * both start and end + margin dates are included
  133.      */
  134.     public boolean inTurnTimeRange(final AbsoluteDate date) {
  135.         return date.durationFrom(startDouble) >= 0 && date.durationFrom(endPlusMarginDouble) <= 0;
  136.     }

  137. }