EventBasedScheduler.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.estimation.measurements.generation;

  18. import org.orekit.estimation.measurements.EstimatedMeasurementBase;
  19. import org.orekit.estimation.measurements.ObservedMeasurement;
  20. import org.orekit.propagation.Propagator;
  21. import org.orekit.propagation.SpacecraftState;
  22. import org.orekit.propagation.events.DetectorModifier;
  23. import org.orekit.propagation.events.EventDetector;
  24. import org.orekit.propagation.events.handlers.EventHandler;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.time.DatesSelector;
  27. import org.orekit.utils.TimeSpanMap;

  28. import java.util.function.Predicate;

  29. /** {@link Scheduler} based on {@link EventDetector} for generating measurements sequences.
  30.  * <p>
  31.  * Event-based schedulers generate measurements following a repetitive pattern when the
  32.  * a {@link EventDetector detector} provided at construction is in a {@link SignSemantic
  33.  * measurement feasible} state. It is important that the sign of the g function of the underlying
  34.  * event detector is not arbitrary, but has a semantic meaning, e.g. in or out,
  35.  * true or false. This class works well with event detectors that detect entry to or exit
  36.  * from a region, e.g. {@link org.orekit.propagation.events.EclipseDetector EclipseDetector},
  37.  * {@link org.orekit.propagation.events.ElevationDetector ElevationDetector}, {@link
  38.  * org.orekit.propagation.events.LatitudeCrossingDetector LatitudeCrossingDetector}. Using this
  39.  * scheduler with detectors that are not based on entry to or exit from a region, e.g. {@link
  40.  * org.orekit.propagation.events.DateDetector DateDetector}, {@link
  41.  * org.orekit.propagation.events.LongitudeCrossingDetector LongitudeCrossingDetector}, will likely
  42.  * lead to unexpected results.
  43.  * </p>
  44.  * <p>
  45.  * The repetitive pattern can be either a continuous stream of measurements separated by
  46.  * a constant step (for example one measurement every 60s), or several sequences of measurements
  47.  * at high rate up to a maximum number, with a rest period between sequences (for example
  48.  * sequences of up to 256 measurements every 100ms with 300s between each sequence).
  49.  * </p>
  50.  * @param <T> the type of the measurement
  51.  * @author Luc Maisonobe
  52.  * @since 9.3
  53.  */
  54. public class EventBasedScheduler<T extends ObservedMeasurement<T>> extends AbstractScheduler<T> {

  55.     /** Semantic of the detector g function sign to use. */
  56.     private final SignSemantic signSemantic;

  57.     /** Feasibility status. */
  58.     private TimeSpanMap<Boolean> feasibility;

  59.     /** Propagation direction. */
  60.     private boolean forward;

  61.     /** Simple constructor.
  62.      * <p>
  63.      * The event detector instance should <em>not</em> be already bound to the propagator.
  64.      * It will be wrapped in an {@link DetectorModifier adapter} in order to manage time
  65.      * ranges when measurements are feasible. The wrapping adapter will be automatically
  66.      * {@link Propagator#addEventDetector(EventDetector) added} to the propagator by this
  67.      * constructor.
  68.      * </p>
  69.      * <p>
  70.      * BEWARE! Dates selectors often store internally the last selected dates, so they are not
  71.      * reusable across several {@link EventBasedScheduler instances}. A separate selector
  72.      * should be used for each scheduler.
  73.      * </p>
  74.      * <p>
  75.      * This constructor calls {@link #EventBasedScheduler(MeasurementBuilder, DatesSelector,
  76.      * Predicate, Propagator, EventDetector, SignSemantic)} whith the predicate set to accept
  77.      * all generated measurements.
  78.      * </p>
  79.      * @param builder builder for individual measurements
  80.      * @param selector selector for dates (beware that selectors are generally not
  81.      * reusable across several {@link EventBasedScheduler instances}, each selector should
  82.      * be dedicated to one scheduler
  83.      * @param propagator propagator associated with this scheduler
  84.      * @param detector detector for checking measurements feasibility
  85.      * @param signSemantic semantic of the detector g function sign to use
  86.      */
  87.     public EventBasedScheduler(final MeasurementBuilder<T> builder, final DatesSelector selector,
  88.                                final Propagator propagator,
  89.                                final EventDetector detector, final SignSemantic signSemantic) {
  90.         this(builder, selector, e -> true, propagator, detector, signSemantic);
  91.     }

  92.     /** Simple constructor.
  93.      * <p>
  94.      * The event detector instance should <em>not</em> be already bound to the propagator.
  95.      * It will be wrapped in an {@link DetectorModifier adapter} in order to manage time
  96.      * ranges when measurements are feasible. The wrapping adapter will be automatically
  97.      * {@link Propagator#addEventDetector(EventDetector) added} to the propagator by this
  98.      * constructor.
  99.      * </p>
  100.      * <p>
  101.      * BEWARE! Dates selectors often store internally the last selected dates, so they are not
  102.      * reusable across several {@link EventBasedScheduler instances}. A separate selector
  103.      * should be used for each scheduler.
  104.      * </p>
  105.      * @param builder builder for individual measurements
  106.      * @param selector selector for dates (beware that selectors are generally not
  107.      * reusable across several {@link EventBasedScheduler instances}, each selector should
  108.      * be dedicated to one scheduler
  109.      * @param filter predicate for a posteriori filtering of generated measurements
  110.      *               (measurements are accepted if the predicates evaluates to {@code true})
  111.      * @param propagator propagator associated with this scheduler
  112.      * @param detector detector for checking measurements feasibility
  113.      * @param signSemantic semantic of the detector g function sign to use
  114.      * @since 13.0
  115.      */
  116.     public EventBasedScheduler(final MeasurementBuilder<T> builder, final DatesSelector selector,
  117.                                final Predicate<EstimatedMeasurementBase<T>> filter, final Propagator propagator,
  118.                                final EventDetector detector, final SignSemantic signSemantic) {
  119.         super(builder, selector, filter);
  120.         this.signSemantic = signSemantic;
  121.         this.feasibility  = new TimeSpanMap<>(Boolean.FALSE);
  122.         this.forward      = true;
  123.         propagator.addEventDetector(new FeasibilityModifier(detector));
  124.     }

  125.     /** {@inheritDoc} */
  126.     @Override
  127.     public boolean measurementIsFeasible(final AbsoluteDate date) {
  128.         return feasibility.get(date);
  129.     }

  130.     /** Adapter for managing feasibility status changes. */
  131.     private class FeasibilityModifier implements DetectorModifier {

  132.         /** Wrapped event detector. */
  133.         private final EventDetector eventDetector;

  134.         /** Build an adaptor wrapping an existing detector.
  135.          * @param eventDetector detector to wrap
  136.          */
  137.         FeasibilityModifier(final EventDetector eventDetector) {
  138.             this.eventDetector = eventDetector;
  139.         }

  140.         /** {@inheritDoc} */
  141.         @Override
  142.         public EventDetector getDetector() {
  143.             return eventDetector;
  144.         }

  145.         /** {@inheritDoc} */
  146.         @Override
  147.         public void init(final SpacecraftState s0, final AbsoluteDate t) {
  148.             DetectorModifier.super.init(s0, t);
  149.             forward     = t.compareTo(s0.getDate()) > 0;
  150.             feasibility = new TimeSpanMap<>(signSemantic.measurementIsFeasible(g(s0)));
  151.         }

  152.         /** {@inheritDoc} */
  153.         @Override
  154.         public EventHandler getHandler() {

  155.             final EventDetector rawDetector = getDetector();
  156.             final EventHandler  rawHandler  = rawDetector.getHandler();

  157.             return (state, detector, increasing) -> {

  158.                 // find the feasibility status AFTER the current date
  159.                 final boolean statusAfter = signSemantic.measurementIsFeasible(increasing ? +1 : -1);

  160.                 // store either status or its opposite according to propagation direction
  161.                 if (forward) {
  162.                     // forward propagation
  163.                     feasibility.addValidAfter(statusAfter, state.getDate(), false);
  164.                 } else {
  165.                     // backward propagation
  166.                     feasibility.addValidBefore(!statusAfter, state.getDate(), false);
  167.                 }

  168.                 // delegate to wrapped detector
  169.                 return rawHandler.eventOccurred(state, rawDetector, increasing);

  170.             };

  171.         }

  172.     }

  173. }