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.propagation.events;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
21  import org.orekit.frames.Frame;
22  import org.orekit.frames.TopocentricFrame;
23  import org.orekit.models.AtmosphericRefractionModel;
24  import org.orekit.propagation.FieldSpacecraftState;
25  import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
26  import org.orekit.propagation.events.handlers.FieldEventHandler;
27  import org.orekit.time.FieldAbsoluteDate;
28  import org.orekit.utils.ExtendedPositionProvider;
29  
30  
31  /** Detector for ground location being at night.
32   * <p>
33   * This detector is mainly useful for scheduling optical measurements
34   * (either passive telescope observation of satellites against the stars background
35   *  or active satellite laser ranging).
36   * </p>
37   * <p>
38   * The {@code g} function of this detector is positive when ground is at night
39   * (i.e. Sun is below dawn/dusk elevation angle).
40   * </p>
41   * @author Luc Maisonobe
42   * @author Romain Serra
43   * @see GroundAtNightDetector
44   * @since 13.1
45   */
46  public class FieldGroundAtNightDetector<T extends CalculusFieldElement<T>>
47          extends FieldAbstractTopocentricDetector<FieldGroundAtNightDetector<T>, T> {
48  
49      /** Provider for Sun position. */
50      private final ExtendedPositionProvider sun;
51  
52      /** Sun elevation below which we consider night is dark enough. */
53      private final T dawnDuskElevation;
54  
55      /** Atmospheric Model used for calculations, if defined. */
56      private final AtmosphericRefractionModel refractionModel;
57  
58      /** Simple constructor.
59       * @param topocentricFrame ground location to check
60       * @param sun provider for Sun position
61       * @param dawnDuskElevation Sun elevation below which we consider night is dark enough (rad)
62       * @param refractionModel reference to refraction model (null if refraction should be ignored)
63       */
64      public FieldGroundAtNightDetector(final TopocentricFrame topocentricFrame, final ExtendedPositionProvider sun,
65                                        final T dawnDuskElevation,
66                                        final AtmosphericRefractionModel refractionModel) {
67          this(topocentricFrame, sun, dawnDuskElevation, refractionModel, new FieldEventDetectionSettings<>(dawnDuskElevation.getField(),
68                  EventDetectionSettings.getDefaultEventDetectionSettings()), new FieldContinueOnEvent<>());
69      }
70  
71      /** Private constructor.
72       * @param topocentricFrame ground location from which measurement is performed
73       * @param sun provider for Sun position
74       * @param dawnDuskElevation Sun elevation below which we consider night is dark enough (rad)
75       * @param refractionModel reference to refraction model (null if refraction should be ignored),
76       * @param detectionSettings event detection settings
77       * @param handler   event handler to call at event occurrences
78       */
79      protected FieldGroundAtNightDetector(final TopocentricFrame topocentricFrame, final ExtendedPositionProvider sun,
80                                           final T dawnDuskElevation,
81                                           final AtmosphericRefractionModel refractionModel,
82                                           final FieldEventDetectionSettings<T> detectionSettings,
83                                           final FieldEventHandler<T> handler) {
84          super(detectionSettings, handler, topocentricFrame);
85          this.sun               = sun;
86          this.dawnDuskElevation = dawnDuskElevation;
87          this.refractionModel   = refractionModel;
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      protected FieldGroundAtNightDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
93                                                     final FieldEventHandler<T> newHandler) {
94          return new FieldGroundAtNightDetector<>(getTopocentricFrame(), sun, dawnDuskElevation, refractionModel,
95                  detectionSettings, newHandler);
96      }
97  
98      @Override
99      public boolean dependsOnTimeOnly() {
100         return true;
101     }
102 
103     /** {@inheritDoc}
104      * <p>
105      * The {@code g} function of this detector is positive when ground is at night
106      * (i.e. Sun is below dawn/dusk elevation angle).
107      * </p>
108      * <p>
109      * This function only depends on date, not on the actual position of the spacecraft.
110      * </p>
111      */
112     @Override
113     public T g(final FieldSpacecraftState<T> state) {
114 
115         final FieldAbsoluteDate<T> date     = state.getDate();
116         final Frame         frame    = state.getFrame();
117         final FieldVector3D<T> position = sun.getPosition(date, frame);
118         final T trueElevation   = getTopocentricFrame().getElevation(position, frame, date);
119 
120         final T calculatedElevation;
121         if (refractionModel != null) {
122             calculatedElevation = trueElevation.add(refractionModel.getRefraction(trueElevation.getReal()));
123         } else {
124             calculatedElevation = trueElevation;
125         }
126 
127         return dawnDuskElevation.subtract(calculatedElevation);
128 
129     }
130 
131 }