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.estimation.measurements.generation;
18  
19  import org.orekit.estimation.measurements.EstimatedMeasurementBase;
20  import org.orekit.estimation.measurements.ObservedMeasurement;
21  import org.orekit.propagation.Propagator;
22  import org.orekit.propagation.SpacecraftState;
23  import org.orekit.propagation.events.DetectorModifier;
24  import org.orekit.propagation.events.EventDetector;
25  import org.orekit.propagation.events.handlers.EventHandler;
26  import org.orekit.time.AbsoluteDate;
27  import org.orekit.time.DatesSelector;
28  import org.orekit.utils.TimeSpanMap;
29  
30  import java.util.function.Predicate;
31  
32  /** {@link Scheduler} based on {@link EventDetector} for generating measurements sequences.
33   * <p>
34   * Event-based schedulers generate measurements following a repetitive pattern when the
35   * a {@link EventDetector detector} provided at construction is in a {@link SignSemantic
36   * measurement feasible} state. It is important that the sign of the g function of the underlying
37   * event detector is not arbitrary, but has a semantic meaning, e.g. in or out,
38   * true or false. This class works well with event detectors that detect entry to or exit
39   * from a region, e.g. {@link org.orekit.propagation.events.EclipseDetector EclipseDetector},
40   * {@link org.orekit.propagation.events.ElevationDetector ElevationDetector}, {@link
41   * org.orekit.propagation.events.LatitudeCrossingDetector LatitudeCrossingDetector}. Using this
42   * scheduler with detectors that are not based on entry to or exit from a region, e.g. {@link
43   * org.orekit.propagation.events.DateDetector DateDetector}, {@link
44   * org.orekit.propagation.events.LongitudeCrossingDetector LongitudeCrossingDetector}, will likely
45   * lead to unexpected results.
46   * </p>
47   * <p>
48   * The repetitive pattern can be either a continuous stream of measurements separated by
49   * a constant step (for example one measurement every 60s), or several sequences of measurements
50   * at high rate up to a maximum number, with a rest period between sequences (for example
51   * sequences of up to 256 measurements every 100ms with 300s between each sequence).
52   * </p>
53   * @param <T> the type of the measurement
54   * @author Luc Maisonobe
55   * @since 9.3
56   */
57  public class EventBasedScheduler<T extends ObservedMeasurement<T>> extends AbstractScheduler<T> {
58  
59      /** Semantic of the detector g function sign to use. */
60      private final SignSemantic signSemantic;
61  
62      /** Feasibility status. */
63      private TimeSpanMap<Boolean> feasibility;
64  
65      /** Propagation direction. */
66      private boolean forward;
67  
68      /** Simple constructor.
69       * <p>
70       * The event detector instance should <em>not</em> be already bound to the propagator.
71       * It will be wrapped in an {@link DetectorModifier adapter} in order to manage time
72       * ranges when measurements are feasible. The wrapping adapter will be automatically
73       * {@link Propagator#addEventDetector(EventDetector) added} to the propagator by this
74       * constructor.
75       * </p>
76       * <p>
77       * BEWARE! Dates selectors often store internally the last selected dates, so they are not
78       * reusable across several {@link EventBasedScheduler instances}. A separate selector
79       * should be used for each scheduler.
80       * </p>
81       * <p>
82       * This constructor calls {@link #EventBasedScheduler(MeasurementBuilder, DatesSelector,
83       * Predicate, Propagator, EventDetector, SignSemantic)} whith the predicate set to accept
84       * all generated measurements.
85       * </p>
86       * @param builder builder for individual measurements
87       * @param selector selector for dates (beware that selectors are generally not
88       * reusable across several {@link EventBasedScheduler instances}, each selector should
89       * be dedicated to one scheduler
90       * @param propagator propagator associated with this scheduler
91       * @param detector detector for checking measurements feasibility
92       * @param signSemantic semantic of the detector g function sign to use
93       */
94      public EventBasedScheduler(final MeasurementBuilder<T> builder, final DatesSelector selector,
95                                 final Propagator propagator,
96                                 final EventDetector detector, final SignSemantic signSemantic) {
97          this(builder, selector, e -> true, propagator, detector, signSemantic);
98      }
99  
100     /** Simple constructor.
101      * <p>
102      * The event detector instance should <em>not</em> be already bound to the propagator.
103      * It will be wrapped in an {@link DetectorModifier adapter} in order to manage time
104      * ranges when measurements are feasible. The wrapping adapter will be automatically
105      * {@link Propagator#addEventDetector(EventDetector) added} to the propagator by this
106      * constructor.
107      * </p>
108      * <p>
109      * BEWARE! Dates selectors often store internally the last selected dates, so they are not
110      * reusable across several {@link EventBasedScheduler instances}. A separate selector
111      * should be used for each scheduler.
112      * </p>
113      * @param builder builder for individual measurements
114      * @param selector selector for dates (beware that selectors are generally not
115      * reusable across several {@link EventBasedScheduler instances}, each selector should
116      * be dedicated to one scheduler
117      * @param filter predicate for a posteriori filtering of generated measurements
118      *               (measurements are accepted if the predicates evaluates to {@code true})
119      * @param propagator propagator associated with this scheduler
120      * @param detector detector for checking measurements feasibility
121      * @param signSemantic semantic of the detector g function sign to use
122      * @since 13.0
123      */
124     public EventBasedScheduler(final MeasurementBuilder<T> builder, final DatesSelector selector,
125                                final Predicate<EstimatedMeasurementBase<T>> filter, final Propagator propagator,
126                                final EventDetector detector, final SignSemantic signSemantic) {
127         super(builder, selector, filter);
128         this.signSemantic = signSemantic;
129         this.feasibility  = new TimeSpanMap<>(Boolean.FALSE);
130         this.forward      = true;
131         propagator.addEventDetector(new FeasibilityModifier(detector));
132     }
133 
134     /** {@inheritDoc} */
135     @Override
136     public boolean measurementIsFeasible(final AbsoluteDate date) {
137         return feasibility.get(date);
138     }
139 
140     /** Adapter for managing feasibility status changes. */
141     private class FeasibilityModifier implements DetectorModifier {
142 
143         /** Wrapped event detector. */
144         private final EventDetector eventDetector;
145 
146         /** Build an adaptor wrapping an existing detector.
147          * @param eventDetector detector to wrap
148          */
149         FeasibilityModifier(final EventDetector eventDetector) {
150             this.eventDetector = eventDetector;
151         }
152 
153         /** {@inheritDoc} */
154         @Override
155         public EventDetector getDetector() {
156             return eventDetector;
157         }
158 
159         /** {@inheritDoc} */
160         @Override
161         public void init(final SpacecraftState s0, final AbsoluteDate t) {
162             DetectorModifier.super.init(s0, t);
163             forward     = t.compareTo(s0.getDate()) > 0;
164             feasibility = new TimeSpanMap<>(signSemantic.measurementIsFeasible(g(s0)));
165         }
166 
167         /** {@inheritDoc} */
168         @Override
169         public EventHandler getHandler() {
170 
171             final EventDetector rawDetector = getDetector();
172             final EventHandler  rawHandler  = rawDetector.getHandler();
173 
174             return (state, detector, increasing) -> {
175 
176                 // find the feasibility status AFTER the current date
177                 final boolean statusAfter = signSemantic.measurementIsFeasible(increasing ? +1 : -1);
178 
179                 // store either status or its opposite according to propagation direction
180                 if (forward) {
181                     // forward propagation
182                     feasibility.addValidAfter(statusAfter, state.getDate(), false);
183                 } else {
184                     // backward propagation
185                     feasibility.addValidBefore(!statusAfter, state.getDate(), false);
186                 }
187 
188                 // delegate to wrapped detector
189                 return rawHandler.eventOccurred(state, rawDetector, increasing);
190 
191             };
192 
193         }
194 
195     }
196 
197 }