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

  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;

  21. import org.hipparchus.fitting.PolynomialCurveFitter;
  22. import org.hipparchus.fitting.WeightedObservedPoint;
  23. import org.hipparchus.util.FastMath;
  24. import org.orekit.files.rinex.observation.ObservationData;
  25. import org.orekit.files.rinex.observation.ObservationDataSet;
  26. import org.orekit.gnss.GnssSignal;
  27. import org.orekit.gnss.MeasurementType;
  28. import org.orekit.gnss.SatelliteSystem;
  29. import org.orekit.time.AbsoluteDate;

  30. /**
  31.  * Phase minus code cycle slip detectors.
  32.  * The detector is based the algorithm given in <a
  33.  * href="https://gssc.esa.int/navipedia/index.php/Examples_of_single_frequency_Cycle-Slip_Detectors">
  34.  * Examples of single frequency Cycle-Slip Detectors</a> by Zornoza and M. Hernández-Pajares. Within this class
  35.  * a polynomial is used to smooth the data. We consider a cycle_slip occurring if the current measurement is  too
  36.  * far from the one predicted with the polynomial (algorithm 1 on Navipedia).
  37.  * <p>
  38.  * For building the detector, one should give a threshold and a gap time limit.
  39.  * After construction of the detectors, one can have access to a List of CycleData. Each CycleDate represents
  40.  * a link between the station (define by the RINEX file) and a satellite at a specific frequency. For each cycle data,
  41.  * one has access to the begin and end of availability, and a sorted set which contains all the date at which
  42.  * cycle-slip have been detected
  43.  * </p>
  44.  * @author David Soulard
  45.  * @since 10.2
  46.  */
  47. public class PhaseMinusCodeCycleSlipDetector extends AbstractCycleSlipDetector {

  48.     /** Order of the polynomial used for fitting. */
  49.     private final int order;

  50.     /** Threshold above which cycle-slip occurs. */
  51.     private final double threshold;

  52.     /** Polynomial single frequency cycle-slip detector Constructor.
  53.      * @param dt time gap threshold between two consecutive measurement (if time between two consecutive measurement is greater than dt, a cycle slip is declared)
  54.      * @param threshold threshold above which cycle-slip occurs
  55.      * @param n number of measurement before starting
  56.      * @param order polynomial order
  57.      */
  58.     public PhaseMinusCodeCycleSlipDetector(final double dt, final double threshold,
  59.                                            final int n, final int order) {
  60.         super(dt, n);
  61.         this.threshold = threshold;
  62.         this.order     = order;
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     protected void manageData(final ObservationDataSet observation) {

  67.         // Extract observation data
  68.         final SatelliteSystem system = observation.getSatellite().getSystem();
  69.         final int             prn    = observation.getSatellite().getPRN();
  70.         final AbsoluteDate    date   = observation.getDate();

  71.         // Initialize list of measurements
  72.         final List<ObservationData> pseudoRanges = new ArrayList<>();
  73.         final List<ObservationData> phases       = new ArrayList<>();

  74.         // Loop on observation data to fill lists
  75.         for (final ObservationData od : observation.getObservationData()) {
  76.             if (!Double.isNaN(od.getValue())) {
  77.                 if (od.getObservationType().getMeasurementType() == MeasurementType.PSEUDO_RANGE) {
  78.                     pseudoRanges.add(od);
  79.                 } else if (od.getObservationType().getMeasurementType() == MeasurementType.CARRIER_PHASE) {
  80.                     phases.add(od);
  81.                 }
  82.             }
  83.         }

  84.         // Loop on phase measurements
  85.         for (final ObservationData phase : phases) {
  86.             // Loop on range measurement
  87.             for (final ObservationData pseudoRange : pseudoRanges) {
  88.                 // Change unit of phase measurement
  89.                 final double wavelength = phase.getObservationType().getSignal(system).getWavelength();
  90.                 final ObservationData phaseInMeters = new ObservationData(phase.getObservationType(),
  91.                                                                           wavelength * phase.getValue(),
  92.                                                                           phase.getLossOfLockIndicator(),
  93.                                                                           phase.getSignalStrength());

  94.                 // Check if measurement frequencies are the same
  95.                 if (phase.getObservationType().getSignal(system) == pseudoRange.getObservationType().getSignal(system)) {
  96.                     // Phase minus Code combination
  97.                     final PhaseMinusCodeCombination phaseMinusCode = MeasurementCombinationFactory.getPhaseMinusCodeCombination(system);
  98.                     final CombinedObservationData cod = phaseMinusCode.combine(phaseInMeters, pseudoRange);
  99.                     final String nameSat = setName(prn, observation.getSatellite().getSystem());

  100.                     // Check for cycle-slip detection
  101.                     final boolean slip = cycleSlipDetection(nameSat, date, cod.getValue(), phase.getObservationType().getSignal(system));
  102.                     if (!slip) {
  103.                         // Update cycle slip data
  104.                         cycleSlipDataSet(nameSat, date, cod.getValue(), phase.getObservationType().getSignal(system));
  105.                     }
  106.                 }
  107.             }
  108.         }

  109.     }

  110.     /**
  111.      * Compute if there is a cycle slip at a specific date.
  112.      * @param nameSat name of the satellite, on the predefined format (e.g. GPS - 07 for satellite 7 of GPS constellation)
  113.      * @param currentDate the date at which we check if a cycle-slip occurs
  114.      * @param phaseMinusCode phase measurement minus code measurement
  115.      * @param signal signal used
  116.      * @return true if a cycle slip has been detected.
  117.      */
  118.     private boolean cycleSlipDetection(final String nameSat, final AbsoluteDate currentDate,
  119.                                        final double phaseMinusCode, final GnssSignal signal) {

  120.         // Access the cycle slip results to know if a cycle-slip already occurred
  121.         final List<CycleSlipDetectorResults>          data  = getResults();
  122.         final List<Map<GnssSignal, DataForDetection>> stuff = getStuffReference();

  123.         // If a cycle-slip already occurred
  124.         if (data != null) {

  125.             // Loop on cycle-slip results
  126.             for (CycleSlipDetectorResults resultPmC : data) {

  127.                 // Found the right cycle data
  128.                 if (resultPmC.getSatelliteName().compareTo(nameSat) == 0 && resultPmC.getCycleSlipMap().containsKey(signal)) {
  129.                     final Map<GnssSignal, DataForDetection> values = stuff.get(data.indexOf(resultPmC));
  130.                     final DataForDetection v = values.get(signal);

  131.                     // Check the time gap condition
  132.                     if (FastMath.abs(currentDate.durationFrom(v.getFiguresReference()[v.getWrite()].getDate())) > getMaxTimeBeetween2Measurement()) {
  133.                         resultPmC.addCycleSlipDate(signal, currentDate);
  134.                         v.resetFigures( new SlipComputationData[getMinMeasurementNumber()], phaseMinusCode, currentDate);
  135.                         resultPmC.setDate(signal, currentDate);
  136.                         return true;
  137.                     }

  138.                     // Compute the fitting polynomial if there are enough measurement since last cycle-slip
  139.                     if (v.getCanBeComputed() >= getMinMeasurementNumber()) {
  140.                         final List<WeightedObservedPoint> xy = new ArrayList<>();
  141.                         for (int i = 0; i < getMinMeasurementNumber(); i++) {
  142.                             final SlipComputationData current = v.getFiguresReference()[i];
  143.                             xy.add(new WeightedObservedPoint(1.0, current.getDate().durationFrom(currentDate),
  144.                                                                  current.getValue()));
  145.                         }

  146.                         final PolynomialCurveFitter fitting = PolynomialCurveFitter.create(order);
  147.                         // Check if there is a cycle_slip
  148.                         if (FastMath.abs(fitting.fit(xy)[0] - phaseMinusCode) > threshold) {
  149.                             resultPmC.addCycleSlipDate(signal, currentDate);
  150.                             v.resetFigures( new SlipComputationData[getMinMeasurementNumber()], phaseMinusCode, currentDate);
  151.                             resultPmC.setDate(signal, currentDate);
  152.                             return true;
  153.                         }

  154.                     } else {
  155.                         break;
  156.                     }

  157.                 }

  158.             }

  159.         }

  160.         // No cycle-slip
  161.         return false;
  162.     }

  163. }