GroundMaskElevationDetector.java

  1. /* Copyright 2002-2013 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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. import java.util.Arrays;
  19. import java.util.Comparator;

  20. import org.apache.commons.math3.util.FastMath;
  21. import org.apache.commons.math3.util.MathUtils;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.frames.TopocentricFrame;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.propagation.events.handlers.EventHandler;
  27. import org.orekit.propagation.events.handlers.StopOnDecreasing;

  28. /** Finder for satellite azimuth-elevation events with respect to a mask.
  29.  * <p>This class finds elevation events (i.e. satellite raising and setting) with
  30.  * respect to an azimuth-elevation mask.</p>
  31.  * <p>An azimuth-elevation mask defines the physical horizon for a local point,
  32.  * origin of some topocentric frame.</p>
  33.  * <p>Azimuth is defined according to {@link TopocentricFrame#getAzimuth(org.apache.commons.math3.geometry.euclidean.threed.Vector3D, org.orekit.frames.Frame, org.orekit.time.AbsoluteDate) getAzimuth}.
  34.  *  Elevation is defined according to {@link TopocentricFrame#getElevation(org.apache.commons.math3.geometry.euclidean.threed.Vector3D, org.orekit.frames.Frame, org.orekit.time.AbsoluteDate) getElevation}.</p>
  35.  * <p>The azimuth elevation mask must be supplied as a twodimensional array with
  36.  *  multiples lines of pairs of azimuth-elevation angles. First row will be filled with
  37.  *  azimuth values, second row with elevation values, as in the following snippet:
  38.  *  <pre>
  39.  *    double [][] mask = {
  40.  *                        {FastMathFastMath.toRadians(0),   FastMath.toRadians(10)},
  41.  *                        {FastMathFastMath.toRadians(45),  FastMath.toRadians(8)},
  42.  *                        {FastMathFastMath.toRadians(90),  FastMath.toRadians(6)},
  43.  *                        {FastMathFastMath.toRadians(135), FastMath.toRadians(4)},
  44.  *                        {FastMathFastMath.toRadians(180), FastMath.toRadians(5)},
  45.  *                        {FastMathFastMath.toRadians(225), FastMath.toRadians(6)},
  46.  *                        {FastMathFastMath.toRadians(270), FastMath.toRadians(8)},
  47.  *                        {FastMathFastMath.toRadians(315), FastMath.toRadians(9)}
  48.  *                       };
  49.  *  </pre>
  50.  * </p>
  51.  * <p>No assumption is made on azimuth values and ordering. The only restraint is
  52.  * that only one elevation value can be associated to identical azimuths modulo 2PI.</p>
  53.  * <p>The default implementation behavior is to {@link
  54.  * org.orekit.propagation.events.handlers.EventHandler.Action#CONTINUE continue}
  55.  * propagation at raising and to {@link
  56.  * org.orekit.propagation.events.handlers.EventHandler.Action#STOP stop} propagation
  57.  * at setting. This can be changed by calling {@link #withHandler(EventHandler)}
  58.  * after construction.</p>
  59.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  60.  * @author Pascal Parraud
  61.  * @deprecated as of 6.1 replaced by {@link ElevationDetector}
  62.  */
  63. @Deprecated
  64. public class GroundMaskElevationDetector extends AbstractReconfigurableDetector<GroundMaskElevationDetector> {

  65.     /** Serializable UID. */
  66.     private static final long serialVersionUID = 20131118L;

  67.     /** Azimuth-elevation mask. */
  68.     private final double[][] azelmask;

  69.     /** Topocentric frame in which azimuth and elevation should be evaluated. */
  70.     private final TopocentricFrame topo;

  71.     /** Build a new azimuth-elevation detector.
  72.      *  <p>This simple constructor takes default values for maximal checking
  73.      *   interval ({@link #DEFAULT_MAXCHECK}) and convergence threshold
  74.      *   ({@link #DEFAULT_THRESHOLD}).</p>
  75.      * @param azimelev azimuth-elevation mask (rad)
  76.      * @param topo topocentric frame in which elevation should be evaluated
  77.      * @exception IllegalArgumentException if azimuth-elevation mask is not supported
  78.      */
  79.     public GroundMaskElevationDetector(final double[][] azimelev, final TopocentricFrame topo) {
  80.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, azimelev, topo);
  81.     }

  82.     /** Build a new azimuth-elevation detector.
  83.      * <p>This constructor takes default value for convergence threshold
  84.      * ({@link #DEFAULT_THRESHOLD}).</p>
  85.      * <p>The maximal interval between elevation checks should
  86.      * be smaller than the half duration of the minimal pass to handle,
  87.      * otherwise some short passes could be missed.</p>
  88.      * @param maxCheck maximal checking interval (s)
  89.      * @param azimelev azimuth-elevation mask (rad)
  90.      * @param topo topocentric frame in which elevation should be evaluated
  91.      * @exception IllegalArgumentException if azimuth-elevation mask is not supported
  92.      */
  93.     public GroundMaskElevationDetector(final double maxCheck,
  94.                                        final double[][] azimelev,
  95.                                        final TopocentricFrame topo) {
  96.         this(maxCheck, DEFAULT_THRESHOLD, azimelev, topo);
  97.     }

  98.     /** Build a new azimuth-elevation detector.
  99.      * <p>The maximal interval between elevation checks should
  100.      * be smaller than the half duration of the minimal pass to handle,
  101.      * otherwise some short passes could be missed.</p>
  102.      * @param maxCheck maximal checking interval (s)
  103.      * @param threshold convergence threshold (s)
  104.      * @param azimelev azimuth-elevation mask (rad)
  105.      * @param topo topocentric frame in which elevation should be evaluated
  106.      * @exception IllegalArgumentException if azimuth-elevation mask is not supported
  107.      */
  108.     public GroundMaskElevationDetector(final double maxCheck, final double threshold,
  109.                                        final double[][] azimelev, final TopocentricFrame topo) {
  110.         this(maxCheck, threshold, DEFAULT_MAX_ITER,
  111.              new StopOnDecreasing<GroundMaskElevationDetector>(),
  112.              azimelev, topo);
  113.     }

  114.     /** Private constructor with full parameters.
  115.      * <p>
  116.      * This constructor is private as users are expected to use the builder
  117.      * API with the various {@code withXxx()} methods to set up the instance
  118.      * in a readable manner without using a huge amount of parameters.
  119.      * </p>
  120.      * @param maxCheck maximum checking interval (s)
  121.      * @param threshold convergence threshold (s)
  122.      * @param maxIter maximum number of iterations in the event time search
  123.      * @param handler event handler to call at event occurrences
  124.      * @param azimelev azimuth-elevation mask (rad)
  125.      * @param topo topocentric frame in which elevation should be evaluated
  126.      * @since 6.1
  127.      */
  128.     private GroundMaskElevationDetector(final double maxCheck, final double threshold,
  129.                                         final int maxIter, final EventHandler<GroundMaskElevationDetector> handler,
  130.                                         final double[][] azimelev,
  131.                                         final TopocentricFrame topo) {
  132.         super(maxCheck, threshold, maxIter, handler);
  133.         this.azelmask = checkMask(azimelev);
  134.         this.topo     = topo;
  135.     }

  136.     /** {@inheritDoc} */
  137.     @Override
  138.     protected GroundMaskElevationDetector create(final double newMaxCheck, final double newThreshold,
  139.                                                  final int newMaxIter, final EventHandler<GroundMaskElevationDetector> newHandler) {
  140.         return new GroundMaskElevationDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  141.                                                azelmask, topo);
  142.     }

  143.     /** Get the topocentric frame.
  144.      * @return the topocentric frame
  145.      */
  146.     public TopocentricFrame getTopocentricFrame() {
  147.         return topo;
  148.     }

  149.     /** Compute the value of the switching function.
  150.      * This function measures the difference between the current elevation and the
  151.      * elevation for current azimuth interpolated from azimuth-elevation mask.
  152.      * @param s the current state information: date, kinematics, attitude
  153.      * @return value of the switching function
  154.      * @exception OrekitException if some specific error occurs
  155.      */
  156.     public double g(final SpacecraftState s) throws OrekitException {
  157.         final double azimuth = topo.getAzimuth(s.getPVCoordinates().getPosition(), s.getFrame(), s.getDate());
  158.         return topo.getElevation(s.getPVCoordinates().getPosition(), s.getFrame(), s.getDate()) - getElevation(azimuth);
  159.     }

  160.     /** Get the interpolated elevation for a given azimuth according to the mask.
  161.      * @param azimuth azimuth (rad)
  162.      * @return elevation angle (rad)
  163.      */
  164.     public double getElevation(final double azimuth) {
  165.         double elevation = 0.0;
  166.         boolean fin = false;
  167.         for (int i = 1; i < azelmask.length & !fin; i++) {
  168.             if (azimuth <= azelmask[i][0]) {
  169.                 fin = true;
  170.                 final double azd = azelmask[i - 1][0];
  171.                 final double azf = azelmask[i][0];
  172.                 final double eld = azelmask[i - 1][1];
  173.                 final double elf = azelmask[i][1];
  174.                 elevation = eld + (azimuth - azd) * (elf - eld) / (azf - azd);
  175.             }
  176.         }
  177.         return elevation;
  178.     }

  179.     /** Checking and ordering the azimuth-elevation tabulation.
  180.      * @param azimelev azimuth-elevation tabulation to be checked and ordered
  181.      * @return ordered azimuth-elevation tabulation ordered
  182.      */
  183.     private static double[][] checkMask(final double[][] azimelev) {

  184.         /* Copy of the given mask */
  185.         final double[][] mask = new double[azimelev.length + 2][azimelev[0].length];
  186.         for (int i = 0; i < azimelev.length; i++) {
  187.             System.arraycopy(azimelev[i], 0, mask[i + 1], 0, azimelev[i].length);
  188.             /* Reducing azimuth between 0 and 2*Pi */
  189.             mask[i + 1][0] = MathUtils.normalizeAngle(mask[i + 1][0], FastMath.PI);
  190.         }

  191.         /* Sorting the mask with respect to azimuth */
  192.         Arrays.sort(mask, 1, mask.length - 1, new Comparator<double[]>() {
  193.             public int compare(final double[] d1, final double[] d2) {
  194.                 return Double.compare(d1[0], d2[0]);
  195.             }
  196.         });

  197.         /* Extending the mask in order to cover [0, 2PI] in azimuth */
  198.         mask[0][0] = mask[mask.length - 2][0] - MathUtils.TWO_PI;
  199.         mask[0][1] = mask[mask.length - 2][1];
  200.         mask[mask.length - 1][0] = mask[1][0] + MathUtils.TWO_PI;
  201.         mask[mask.length - 1][1] = mask[1][1];

  202.         /* Checking the sorted mask: same azimuth modulo 2PI must have same elevation */
  203.         for (int i = 1; i < mask.length; i++) {
  204.             if (Double.compare(mask[i - 1][0], mask[i][0]) == 0) {
  205.                 if (Double.compare(mask[i - 1][1], mask[i][1]) != 0) {
  206.                     throw OrekitException.createIllegalArgumentException(OrekitMessages.UNEXPECTED_TWO_ELEVATION_VALUES_FOR_ONE_AZIMUTH, mask[i - 1][1], mask[i][1], mask[i][0]);
  207.                 }
  208.             }
  209.         }

  210.         return mask;
  211.     }

  212. }