1   /* Copyright 2002-2019 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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  
19  import java.io.Serializable;
20  
21  import org.hipparchus.util.FastMath;
22  import org.orekit.time.AbsoluteDate;
23  import org.orekit.time.TimeStamped;
24  
25  /**
26   * Holder for time span of noon/night turns.
27   *
28   * <p>
29   * The boundaries estimated are updated as
30   * new points close to the span are evaluated.
31   * </p>
32   *
33   * @author Luc Maisonobe
34   * @since 9.3
35   */
36  class TurnSpan implements Serializable, TimeStamped {
37  
38      /** Serializable UID. */
39      private static final long serialVersionUID = 20180626L;
40  
41      /** Margin in seconds after turn end. */
42      private final double endMargin;
43  
44      /** Best estimate of the start of the turn. */
45      private AbsoluteDate start;
46  
47      /** Best estimate of the end of the turn, excluding margin. */
48      private AbsoluteDate end;
49  
50      /** Best estimate of the end of the turn, including margin. */
51      private AbsoluteDate endPlusMargin;
52  
53      /** Time between start and its estimation date. */
54      private double startProjection;
55  
56      /** Time between end and its estimation date. */
57      private double endProjection;
58  
59      /** Turn duration. */
60      private double duration;
61  
62      /** Simple constructor.
63       * @param start estimate of the start of the turn
64       * @param end estimate of the end of the turn, excluding margin
65       * @param estimationDate date at which turn boundaries have been estimated
66       * @param endMargin margin in seconds after turn end
67       */
68      TurnSpan(final AbsoluteDateAbsoluteDate">AbsoluteDate start, final AbsoluteDate end,
69               final AbsoluteDate estimationDate, final double endMargin) {
70          this.endMargin       = endMargin;
71          this.start           = start;
72          this.end             = end;
73          this.endPlusMargin   = end.shiftedBy(endMargin);
74          this.startProjection = FastMath.abs(start.durationFrom(estimationDate));
75          this.endProjection   = FastMath.abs(endPlusMargin.durationFrom(estimationDate));
76          this.duration        = end.durationFrom(start);
77      }
78  
79      /** Update the estimate of the turn start.
80       * <p>
81       * Start boundary is updated only if it is estimated
82       * from a time closer to the boundary than the previous estimate.
83       * </p>
84       * @param newStart new estimate of the start of the turn
85       * @param estimationDate date at which turn start has been estimated
86       */
87      public void updateStart(final AbsoluteDateluteDate">AbsoluteDate newStart,  final AbsoluteDate estimationDate) {
88  
89          // update the start date if this estimate is closer than the previous one
90          final double newStartProjection = FastMath.abs(newStart.durationFrom(estimationDate));
91          if (newStartProjection <= startProjection) {
92              this.start           = newStart;
93              this.startProjection = newStartProjection;
94              this.duration        = end.durationFrom(start);
95          }
96  
97      }
98  
99      /** Update the estimate of the turn end.
100      * <p>
101      * end boundary is updated only if it is estimated
102      * from a time closer to the boundary than the previous estimate.
103      * </p>
104      * @param newEnd new estimate of the end of the turn
105      * @param estimationDate date at which turn end has been estimated
106      */
107     public void updateEnd(final AbsoluteDatebsoluteDate">AbsoluteDate newEnd, final AbsoluteDate estimationDate) {
108 
109         // update the end date if this estimate is closer than the previous one
110         final double newEndProjection = FastMath.abs(newEnd.durationFrom(estimationDate));
111         if (newEndProjection <= endProjection) {
112             this.end             = newEnd;
113             this.endPlusMargin   = newEnd.shiftedBy(endMargin);
114             this.endProjection   = newEndProjection;
115             this.duration        = end.durationFrom(start);
116         }
117 
118     }
119 
120     /** {@inheritDoc}
121      * <p>
122      * The global date of the turn is the turn end, including margin
123      * </p>
124      */
125     @Override
126     public AbsoluteDate getDate() {
127         return endPlusMargin;
128     }
129 
130     /** Get turn duration.
131      * @return turn duration
132      */
133     public double getTurnDuration() {
134         return duration;
135     }
136 
137     /** Get turn start date.
138      * @return turn start date
139      */
140     public AbsoluteDate getTurnStartDate() {
141         return start;
142     }
143 
144     /** Get turn end date (without margin).
145      * @return turn end date (without margin)
146      */
147     public AbsoluteDate getTurnEndDate() {
148         return end;
149     }
150 
151     /** Check if a date is within range.
152      * @param date date to check
153      * @return true if date is within range extended by end margin,
154      * both start and end + margin dates are included
155      */
156     public boolean inTurnTimeRange(final AbsoluteDate date) {
157         return date.durationFrom(start) >= 0 && date.durationFrom(endPlusMargin) <= 0;
158     }
159 
160 }