EventBasedScheduler.java

  1. /* Copyright 2002-2022 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 java.util.List;
  19. import java.util.SortedSet;
  20. import java.util.TreeSet;

  21. import org.hipparchus.ode.events.Action;
  22. import org.orekit.estimation.measurements.ObservedMeasurement;
  23. import org.orekit.propagation.Propagator;
  24. import org.orekit.propagation.SpacecraftState;
  25. import org.orekit.propagation.events.AdapterDetector;
  26. import org.orekit.propagation.events.EventDetector;
  27. import org.orekit.propagation.sampling.OrekitStepInterpolator;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.time.DatesSelector;
  30. import org.orekit.utils.TimeSpanMap;


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

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

  59.     /** Feasibility status. */
  60.     private TimeSpanMap<Boolean> feasibility;

  61.     /** Propagation direction. */
  62.     private boolean forward;

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

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public SortedSet<T> generate(final List<OrekitStepInterpolator> interpolators) {

  96.         // select dates in the current step, using arbitrarily interpolator 0
  97.         // as all interpolators cover the same range
  98.         final List<AbsoluteDate> dates = getSelector().selectDates(interpolators.get(0).getPreviousState().getDate(),
  99.                                                                    interpolators.get(0).getCurrentState().getDate());

  100.         // generate measurements when feasible
  101.         final SortedSet<T> measurements = new TreeSet<>();
  102.         for (final AbsoluteDate date : dates) {
  103.             if (feasibility.get(date)) {
  104.                 // a measurement is feasible at this date

  105.                 // interpolate states at measurement date
  106.                 final SpacecraftState[] states = new SpacecraftState[interpolators.size()];
  107.                 for (int i = 0; i < states.length; ++i) {
  108.                     states[i] = interpolators.get(i).getInterpolatedState(date);
  109.                 }

  110.                 // generate measurement
  111.                 measurements.add(getBuilder().build(states));

  112.             }
  113.         }

  114.         return measurements;

  115.     }

  116.     /** Adapter for managing feasibility status changes. */
  117.     private class FeasibilityAdapter extends AdapterDetector {

  118.         /** Build an adaptor wrapping an existing detector.
  119.          * @param detector detector to wrap
  120.          */
  121.         FeasibilityAdapter(final EventDetector detector) {
  122.             super(detector);
  123.         }

  124.         /** {@inheritDoc} */
  125.         @Override
  126.         public void init(final SpacecraftState s0, final AbsoluteDate t) {
  127.             super.init(s0, t);
  128.             forward     = t.compareTo(s0.getDate()) > 0;
  129.             feasibility = new TimeSpanMap<Boolean>(signSemantic.measurementIsFeasible(g(s0)));
  130.         }

  131.         /** {@inheritDoc} */
  132.         @Override
  133.         public Action eventOccurred(final SpacecraftState s, final boolean increasing) {

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

  136.             // store either status or its opposite according to propagation direction
  137.             if (forward) {
  138.                 // forward propagation
  139.                 feasibility.addValidAfter(statusAfter, s.getDate(), false);
  140.             } else {
  141.                 // backward propagation
  142.                 feasibility.addValidBefore(!statusAfter, s.getDate(), false);
  143.             }

  144.             // delegate to wrapped detector
  145.             return super.eventOccurred(s, increasing);

  146.         }

  147.     }

  148. }