EventBasedScheduler.java

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

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


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

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

  55.     /** Feasibility status. */
  56.     private TimeSpanMap<Boolean> feasibility;

  57.     /** Propagation direction. */
  58.     private boolean forward;

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

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public boolean measurementIsFeasible(final AbsoluteDate date) {
  92.         return feasibility.get(date);
  93.     }

  94.     /** Adapter for managing feasibility status changes. */
  95.     private class FeasibilityAdapter extends AdapterDetector {

  96.         /** Build an adaptor wrapping an existing detector.
  97.          * @param detector detector to wrap
  98.          */
  99.         FeasibilityAdapter(final EventDetector detector) {
  100.             super(detector);
  101.         }

  102.         /** {@inheritDoc} */
  103.         @Override
  104.         public void init(final SpacecraftState s0, final AbsoluteDate t) {
  105.             super.init(s0, t);
  106.             forward     = t.compareTo(s0.getDate()) > 0;
  107.             feasibility = new TimeSpanMap<Boolean>(signSemantic.measurementIsFeasible(g(s0)));
  108.         }

  109.         /** {@inheritDoc} */
  110.         @Override
  111.         public EventHandler getHandler() {

  112.             final EventDetector rawDetector = getDetector();
  113.             final EventHandler  rawHandler  = rawDetector.getHandler();

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

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

  117.                 // store either status or its opposite according to propagation direction
  118.                 if (forward) {
  119.                     // forward propagation
  120.                     feasibility.addValidAfter(statusAfter, state.getDate(), false);
  121.                 } else {
  122.                     // backward propagation
  123.                     feasibility.addValidBefore(!statusAfter, state.getDate(), false);
  124.                 }

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

  127.             };

  128.         }

  129.     }

  130. }