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.propagation.events;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.hipparchus.ode.events.Action;
23  import org.hipparchus.util.FastMath;
24  import org.orekit.frames.FieldStaticTransform;
25  import org.orekit.frames.TopocentricFrame;
26  import org.orekit.models.AtmosphericRefractionModel;
27  import org.orekit.propagation.FieldSpacecraftState;
28  import org.orekit.propagation.events.handlers.FieldEventHandler;
29  import org.orekit.propagation.events.handlers.FieldStopOnDecreasing;
30  import org.orekit.utils.ElevationMask;
31  
32  
33  /**
34   * Finder for satellite raising/setting events that allows for the
35   * setting of azimuth and/or elevation bounds or a ground azimuth/elevation
36   * mask input. Each calculation be configured to use atmospheric refraction
37   * as well.
38   * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
39   * propagation at raising and to {@link Action#STOP stop} propagation
40   * at setting. This can be changed by calling
41   * {@link #withHandler(FieldEventHandler)} after construction.</p>
42   * @author Hank Grabowski
43   * @param <T> type of the field elements
44   */
45  public class FieldElevationDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldElevationDetector<T>, T> {
46  
47      /** Elevation mask used for calculations, if defined. */
48      private final ElevationMask elevationMask;
49  
50      /** Minimum elevation value used if mask is not defined. */
51      private final double minElevation;
52  
53      /** Atmospheric Model used for calculations, if defined. */
54      private final AtmosphericRefractionModel refractionModel;
55  
56      /** Topocentric frame in which elevation should be evaluated. */
57      private final TopocentricFrame topo;
58  
59      /**
60       * Creates an instance of Elevation detector based on passed in topocentric frame
61       * and the minimum elevation angle.
62       * <p>
63       * uses default values for maximal checking interval ({@link #DEFAULT_MAXCHECK})
64       * and convergence threshold ({@link #DEFAULT_THRESHOLD}).</p>
65       * @param field type of the elements
66       * @param topo reference to a topocentric model
67       * @see #withConstantElevation(double)
68       * @see #withElevationMask(ElevationMask)
69       * @see #withRefraction(AtmosphericRefractionModel)
70       */
71      public FieldElevationDetector(final Field<T> field, final TopocentricFrame topo) {
72          this(FieldAdaptableInterval.of(DEFAULT_MAXCHECK),
73               field.getZero().newInstance(DEFAULT_THRESHOLD), DEFAULT_MAX_ITER,
74               new FieldStopOnDecreasing<>(),
75               0.0, null, null, topo);
76      }
77  
78      /**
79       * Creates an instance of Elevation detector based on passed in topocentric frame
80       * and overrides of default maximal checking interval and convergence threshold values.
81       * @param maxCheck maximum checking interval (s)
82       * @param threshold maximum divergence threshold (s)
83       * @param topo reference to a topocentric model
84       * @see #withConstantElevation(double)
85       * @see #withElevationMask(ElevationMask)
86       * @see #withRefraction(AtmosphericRefractionModel)
87       */
88      public FieldElevationDetector(final T maxCheck, final T threshold, final TopocentricFrame topo) {
89          this(FieldAdaptableInterval.of(maxCheck.getReal()), threshold, DEFAULT_MAX_ITER,
90               new FieldStopOnDecreasing<>(),
91               0.0, null, null, topo);
92      }
93  
94      /** Protected constructor with full parameters.
95       * <p>
96       * This constructor is not public as users are expected to use the builder
97       * API with the various {@code withXxx()} methods to set up the instance
98       * in a readable manner without using a huge amount of parameters.
99       * </p>
100      * @param maxCheck maximum checking interval
101      * @param threshold convergence threshold (s)
102      * @param maxIter maximum number of iterations in the event time search
103      * @param handler event handler to call at event occurrences
104      * @param minElevation minimum elevation in radians (rad)
105      * @param mask reference to elevation mask
106      * @param refractionModel reference to refraction model
107      * @param topo reference to a topocentric model
108      */
109     protected FieldElevationDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold,
110                                      final int maxIter, final FieldEventHandler<T> handler,
111                                      final double minElevation, final ElevationMask mask,
112                                      final AtmosphericRefractionModel refractionModel,
113                                    final TopocentricFrame topo) {
114         super(maxCheck, threshold, maxIter, handler);
115         this.minElevation    = minElevation;
116         this.elevationMask   = mask;
117         this.refractionModel = refractionModel;
118         this.topo            = topo;
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     protected FieldElevationDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
124                                                final int newMaxIter, final FieldEventHandler<T> newHandler) {
125         return new FieldElevationDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler,
126                                             minElevation, elevationMask, refractionModel, topo);
127     }
128 
129     /**
130      * Returns the currently configured elevation mask.
131      * @return elevation mask
132      * (null if instance has been configured with {@link #withConstantElevation(double)}
133      * @see #withElevationMask(ElevationMask)
134      */
135     public ElevationMask getElevationMask() {
136         return this.elevationMask;
137     }
138 
139     /**
140      * Returns the currently configured minimum valid elevation value.
141      * @return minimum elevation value
142      * ({@code Double.NaN} if instance has been configured with {@link #withElevationMask(ElevationMask)}
143      * @see #withConstantElevation(double)
144      */
145     public double getMinElevation() {
146         return this.minElevation;
147     }
148 
149     /**
150      * Returns the currently configured refraction model.
151      * @return refraction model
152      * @see #withRefraction(AtmosphericRefractionModel)
153      */
154     public AtmosphericRefractionModel getRefractionModel() {
155         return this.refractionModel;
156     }
157 
158     /**
159      * Returns the currently configured topocentric frame definitions.
160      * @return topocentric frame definition
161      */
162     public TopocentricFrame getTopocentricFrame() {
163         return this.topo;
164     }
165 
166     /** Compute the value of the switching function.
167      * This function measures the difference between the current elevation
168      * (and azimuth if necessary) and the reference mask or minimum value.
169      * @param s the current state information: date, kinematics, attitude
170      * @return value of the switching function
171      */
172     @Override
173     public T g(final FieldSpacecraftState<T> s) {
174 
175         final FieldStaticTransform<T> t = s.getFrame().getStaticTransformTo(topo, s.getDate());
176         final FieldVector3D<T> extPointTopo = t.transformPosition(s.getPosition());
177         final T trueElevation = extPointTopo.getDelta();
178 
179         final T calculatedElevation;
180         if (refractionModel != null) {
181             calculatedElevation = trueElevation.add(refractionModel.getRefraction(trueElevation.getReal()));
182         } else {
183             calculatedElevation = trueElevation;
184         }
185 
186         if (elevationMask != null) {
187             final double azimuth = FastMath.atan2(extPointTopo.getY().getReal(), extPointTopo.getX().getReal());
188             return calculatedElevation.subtract(elevationMask.getElevation(azimuth));
189         } else {
190             return calculatedElevation.subtract(minElevation);
191         }
192 
193     }
194 
195     /**
196      * Setup the minimum elevation for detection.
197      * <p>
198      * This will override an elevation mask if it has been configured as such previously.
199      * </p>
200      * @param newMinElevation minimum elevation for visibility in radians (rad)
201      * @return a new detector with updated configuration (the instance is not changed)
202      * @see #getMinElevation()
203      * @since 6.1
204      */
205     public FieldElevationDetector<T> withConstantElevation(final double newMinElevation) {
206         return new FieldElevationDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
207                                             newMinElevation, null, refractionModel, topo);
208     }
209 
210     /**
211      * Setup the elevation mask for detection using the passed in mask object.
212      * @param newElevationMask elevation mask to use for the computation
213      * @return a new detector with updated configuration (the instance is not changed)
214      * @since 6.1
215      * @see #getElevationMask()
216      */
217     public FieldElevationDetector<T> withElevationMask(final ElevationMask newElevationMask) {
218         return new FieldElevationDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
219                                             Double.NaN, newElevationMask, refractionModel, topo);
220     }
221 
222     /**
223      * Setup the elevation detector to use an atmospheric refraction model in its
224      * calculations.
225      * <p>
226      * To disable the refraction when copying an existing elevation
227      * detector, call this method with a null argument.
228      * </p>
229      * @param newRefractionModel refraction model to use for the computation
230      * @return a new detector with updated configuration (the instance is not changed)
231      * @since 6.1
232      * @see #getRefractionModel()
233      */
234     public FieldElevationDetector<T> withRefraction(final AtmosphericRefractionModel newRefractionModel) {
235         return new FieldElevationDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
236                                             minElevation, elevationMask, newRefractionModel, topo);
237     }
238 
239 }