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

  18. import org.hipparchus.util.FastMath;
  19. import org.orekit.time.AbsoluteDate;
  20. import org.orekit.time.TimeStamped;

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

  33.     /** Margin in seconds after turn end. */
  34.     private final double endMargin;

  35.     /** Best estimate of the start of the turn. */
  36.     private AbsoluteDate start;

  37.     /** Best estimate of the end of the turn, excluding margin. */
  38.     private AbsoluteDate end;

  39.     /** Best estimate of the end of the turn, including margin. */
  40.     private AbsoluteDate endPlusMargin;

  41.     /** Time between start and its estimation date. */
  42.     private double startProjection;

  43.     /** Time between end and its estimation date. */
  44.     private double endProjection;

  45.     /** Turn duration. */
  46.     private double duration;

  47.     /** Simple constructor.
  48.      * @param start estimate of the start of the turn
  49.      * @param end estimate of the end of the turn, excluding margin
  50.      * @param estimationDate date at which turn boundaries have been estimated
  51.      * @param endMargin margin in seconds after turn end
  52.      */
  53.     TurnSpan(final AbsoluteDate start, final AbsoluteDate end,
  54.              final AbsoluteDate estimationDate, final double endMargin) {
  55.         this.endMargin       = endMargin;
  56.         this.start           = start;
  57.         this.end             = end;
  58.         this.endPlusMargin   = end.shiftedBy(endMargin);
  59.         this.startProjection = FastMath.abs(start.durationFrom(estimationDate));
  60.         this.endProjection   = FastMath.abs(endPlusMargin.durationFrom(estimationDate));
  61.         this.duration        = end.durationFrom(start);
  62.     }

  63.     /** Update the estimate of the turn start.
  64.      * <p>
  65.      * Start boundary is updated only if it is estimated
  66.      * from a time closer to the boundary than the previous estimate.
  67.      * </p>
  68.      * @param newStart new estimate of the start of the turn
  69.      * @param estimationDate date at which turn start has been estimated
  70.      */
  71.     public void updateStart(final AbsoluteDate newStart,  final AbsoluteDate estimationDate) {

  72.         // update the start date if this estimate is closer than the previous one
  73.         final double newStartProjection = FastMath.abs(newStart.durationFrom(estimationDate));
  74.         if (newStartProjection <= startProjection) {
  75.             this.start           = newStart;
  76.             this.startProjection = newStartProjection;
  77.             this.duration        = end.durationFrom(start);
  78.         }

  79.     }

  80.     /** Update the estimate of the turn end.
  81.      * <p>
  82.      * end boundary is updated only if it is estimated
  83.      * from a time closer to the boundary than the previous estimate.
  84.      * </p>
  85.      * @param newEnd new estimate of the end of the turn
  86.      * @param estimationDate date at which turn end has been estimated
  87.      */
  88.     public void updateEnd(final AbsoluteDate newEnd, final AbsoluteDate estimationDate) {

  89.         // update the end date if this estimate is closer than the previous one
  90.         final double newEndProjection = FastMath.abs(newEnd.durationFrom(estimationDate));
  91.         if (newEndProjection <= endProjection) {
  92.             this.end             = newEnd;
  93.             this.endPlusMargin   = newEnd.shiftedBy(endMargin);
  94.             this.endProjection   = newEndProjection;
  95.             this.duration        = end.durationFrom(start);
  96.         }

  97.     }

  98.     /** {@inheritDoc}
  99.      * <p>
  100.      * The global date of the turn is the turn end, including margin
  101.      * </p>
  102.      */
  103.     @Override
  104.     public AbsoluteDate getDate() {
  105.         return endPlusMargin;
  106.     }

  107.     /** Get turn duration.
  108.      * @return turn duration
  109.      */
  110.     public double getTurnDuration() {
  111.         return duration;
  112.     }

  113.     /** Get turn start date.
  114.      * @return turn start date
  115.      */
  116.     public AbsoluteDate getTurnStartDate() {
  117.         return start;
  118.     }

  119.     /** Get turn end date (without margin).
  120.      * @return turn end date (without margin)
  121.      */
  122.     public AbsoluteDate getTurnEndDate() {
  123.         return end;
  124.     }

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

  133. }