DOPComputer.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.gnss;

  18. import java.util.List;

  19. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  20. import org.hipparchus.linear.MatrixUtils;
  21. import org.hipparchus.linear.RealMatrix;
  22. import org.hipparchus.util.FastMath;
  23. import org.orekit.bodies.GeodeticPoint;
  24. import org.orekit.bodies.OneAxisEllipsoid;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitMessages;
  27. import org.orekit.frames.TopocentricFrame;
  28. import org.orekit.propagation.Propagator;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.ElevationMask;

  31. /**
  32.  * This class aims at computing the dilution of precision.
  33.  *
  34.  * @author Pascal Parraud
  35.  * @since 8.0
  36.  * @see <a href="http://en.wikipedia.org/wiki/Dilution_of_precision_%28GPS%29">Dilution of precision</a>
  37.  */
  38. public class DOPComputer {

  39.     // Constants
  40.     /** Minimum elevation : 0°. */
  41.     public static final double DOP_MIN_ELEVATION = 0.;

  42.     /** Minimum number of propagators for DOP computation. */
  43.     private static final int DOP_MIN_PROPAGATORS = 4;

  44.     // Fields
  45.     /** The location as a topocentric frame. */
  46.     private final TopocentricFrame frame;

  47.     /** Elevation mask used for computation, if defined. */
  48.     private final ElevationMask elevationMask;

  49.     /** Minimum elevation value used if no mask is defined. */
  50.     private final double minElevation;

  51.     /**
  52.      * Constructor for DOP computation.
  53.      *
  54.      * @param frame the topocentric frame linked to the locations where DOP will be computed
  55.      * @param minElev the minimum elevation to consider (rad)
  56.      * @param elevMask the elevation mask to consider
  57.      */
  58.     private DOPComputer(final TopocentricFrame frame, final double minElev, final ElevationMask elevMask) {
  59.         // Set the topocentric frame
  60.         this.frame = frame;
  61.         // Set the min elevation
  62.         this.minElevation = minElev;
  63.         // Set the elevation mask
  64.         this.elevationMask = elevMask;
  65.     }

  66.     /**
  67.      * Creates a DOP computer for one location.
  68.      *
  69.      * <p>A minimum elevation of 0° is taken into account to compute
  70.      * visibility between the location and the GNSS spacecrafts.</p>
  71.      *
  72.      * @param shape the body shape on which the location is defined
  73.      * @param location the point of interest
  74.      * @return a configured DOP computer
  75.      */
  76.     public static DOPComputer create(final OneAxisEllipsoid shape, final GeodeticPoint location) {
  77.         return new DOPComputer(new TopocentricFrame(shape, location, "Location"), DOP_MIN_ELEVATION, null);
  78.     }

  79.     /**
  80.      * Set the minimum elevation.
  81.      *
  82.      * <p>This will override an elevation mask if it has been configured as such previously.</p>
  83.      *
  84.      * @param newMinElevation minimum elevation for visibility (rad)
  85.      * @return a new DOP computer with updated configuration (the instance is not changed)
  86.      *
  87.      * @see #getMinElevation()
  88.      */
  89.     public DOPComputer withMinElevation(final double newMinElevation) {
  90.         return new DOPComputer(frame, newMinElevation, null);
  91.     }

  92.     /**
  93.      * Set the elevation mask.
  94.      *
  95.      * <p>This will override the min elevation if it has been configured as such previously.</p>
  96.      *
  97.      * @param newElevationMask elevation mask to use for the computation
  98.      * @return a new detector with updated configuration (the instance is not changed)
  99.      *
  100.      * @see #getElevationMask()
  101.      */
  102.     public DOPComputer withElevationMask(final ElevationMask newElevationMask) {
  103.         return new DOPComputer(frame, DOP_MIN_ELEVATION, newElevationMask);
  104.     }

  105.     /**
  106.      * Compute the {@link DOP} at a given date for a set of GNSS spacecrafts.
  107.      * <p>Four GNSS spacecraft at least are needed to compute the DOP.
  108.      * If less than 4 propagators are provided, an exception will be thrown.
  109.      * If less than 4 spacecrafts are visible at the date, all DOP values will be
  110.      * set to {@link java.lang.Double#NaN NaN}.</p>
  111.      *
  112.      * @param date the computation date
  113.      * @param gnss the propagators for GNSS spacecraft involved in the DOP computation
  114.      * @return the {@link DOP} at the location
  115.      */
  116.     public DOP compute(final AbsoluteDate date, final List<Propagator> gnss) {

  117.         // Checks the number of provided propagators
  118.         if (gnss.size() < DOP_MIN_PROPAGATORS) {
  119.             throw new OrekitException(OrekitMessages.NOT_ENOUGH_GNSS_FOR_DOP, gnss.size(), DOP_MIN_PROPAGATORS);
  120.         }

  121.         // Initializes DOP values
  122.         double gdop = Double.NaN;
  123.         double pdop = Double.NaN;
  124.         double hdop = Double.NaN;
  125.         double vdop = Double.NaN;
  126.         double tdop = Double.NaN;

  127.         // Loop over the propagators of GNSS orbits
  128.         final double[][] satDir = new double[gnss.size()][4];
  129.         int satNb = 0;
  130.         for (Propagator prop : gnss) {
  131.             final Vector3D pos = prop.getPVCoordinates(date, frame).getPosition();
  132.             final double elev  = frame.getElevation(pos, frame, date);
  133.             final double elMin = (elevationMask != null) ?
  134.                                  elevationMask.getElevation(frame.getAzimuth(pos, frame, date)) :
  135.                                  minElevation;
  136.             // Only visible satellites are considered
  137.             if (elev > elMin) {
  138.                 // Create the rows of the H matrix
  139.                 final Vector3D r = pos.normalize();
  140.                 satDir[satNb][0] = r.getX();
  141.                 satDir[satNb][1] = r.getY();
  142.                 satDir[satNb][2] = r.getZ();
  143.                 satDir[satNb][3] = -1.;
  144.                 satNb++;
  145.             }
  146.         }

  147.         // DOP values are computed only if at least 4 SV are visible from the location
  148.         if (satNb > 3) {
  149.             // Construct matrix H
  150.             final RealMatrix h = MatrixUtils.createRealMatrix(satNb, 4);
  151.             for (int k = 0; k < satNb; k++) {
  152.                 h.setRow(k, satDir[k]);
  153.             }

  154.             // Compute the pseudo-inverse of H
  155.             final RealMatrix hInv = MatrixUtils.inverse(h.transpose().multiply(h));
  156.             final double sx2 = hInv.getEntry(0, 0);
  157.             final double sy2 = hInv.getEntry(1, 1);
  158.             final double sz2 = hInv.getEntry(2, 2);
  159.             final double st2 = hInv.getEntry(3, 3);

  160.             // Extract various DOP : GDOP, PDOP, HDOP, VDOP, TDOP
  161.             gdop = FastMath.sqrt(hInv.getTrace());
  162.             pdop = FastMath.sqrt(sx2 + sy2 + sz2);
  163.             hdop = FastMath.sqrt(sx2 + sy2);
  164.             vdop = FastMath.sqrt(sz2);
  165.             tdop = FastMath.sqrt(st2);
  166.         }

  167.         // Return all the DOP values
  168.         return new DOP(frame.getPoint(), date, satNb, gdop, pdop, hdop, vdop, tdop);
  169.     }

  170.     /**
  171.      * Get the minimum elevation.
  172.      *
  173.      * @return the minimum elevation (rad)
  174.      */
  175.     public double getMinElevation() {
  176.         return minElevation;
  177.     }

  178.     /**
  179.      * Get the elevation mask.
  180.      *
  181.      * @return the elevation mask
  182.      */
  183.     public ElevationMask getElevationMask() {
  184.         return elevationMask;
  185.     }
  186. }