RinexLoader.java

  1. /* Copyright 2002-2018 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.gnss;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.hipparchus.exception.DummyLocalizable;
  28. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  29. import org.hipparchus.geometry.euclidean.twod.Vector2D;
  30. import org.hipparchus.util.FastMath;
  31. import org.orekit.data.DataLoader;
  32. import org.orekit.data.DataProvidersManager;
  33. import org.orekit.errors.OrekitException;
  34. import org.orekit.errors.OrekitMessages;
  35. import org.orekit.time.AbsoluteDate;
  36. import org.orekit.time.TimeScale;
  37. import org.orekit.time.TimeScalesFactory;

  38. public class RinexLoader {

  39.     /** Default supported files name pattern for rinex 2 observation files. */
  40.     public static final String DEFAULT_RINEX_2_SUPPORTED_NAMES = "^\\w{4}\\d{3}[0a-x](?:\\d{2})?\\.\\d{2}[oO]$";

  41.     /** Default supported files name pattern for rinex 3 observation files. */
  42.     public static final String DEFAULT_RINEX_3_SUPPORTED_NAMES = "^\\w{9}_\\w{1}_\\d{11}_\\d{2}\\w_\\d{2}\\w{1}_\\w{2}\\.rnx$";

  43.     // CHECKSTYLE: stop JavadocVariable check
  44.     private static final String RINEX_VERSION_TYPE   = "RINEX VERSION / TYPE";
  45.     private static final String COMMENT              = "COMMENT";
  46.     private static final String PGM_RUN_BY_DATE      = "PGM / RUN BY / DATE";
  47.     private static final String MARKER_NAME          = "MARKER NAME";
  48.     private static final String MARKER_NUMBER        = "MARKER NUMBER";
  49.     private static final String MARKER_TYPE          = "MARKER TYPE";
  50.     private static final String OBSERVER_AGENCY      = "OBSERVER / AGENCY";
  51.     private static final String REC_NB_TYPE_VERS     = "REC # / TYPE / VERS";
  52.     private static final String ANT_NB_TYPE          = "ANT # / TYPE";
  53.     private static final String APPROX_POSITION_XYZ  = "APPROX POSITION XYZ";
  54.     private static final String ANTENNA_DELTA_H_E_N  = "ANTENNA: DELTA H/E/N";
  55.     private static final String ANTENNA_DELTA_X_Y_Z  = "ANTENNA: DELTA X/Y/Z";
  56.     private static final String ANTENNA_PHASECENTER  = "ANTENNA: PHASECENTER";
  57.     private static final String ANTENNA_B_SIGHT_XYZ  = "ANTENNA: B.SIGHT XYZ";
  58.     private static final String ANTENNA_ZERODIR_AZI  = "ANTENNA: ZERODIR AZI";
  59.     private static final String ANTENNA_ZERODIR_XYZ  = "ANTENNA: ZERODIR XYZ";
  60.     private static final String NB_OF_SATELLITES     = "# OF SATELLITES";
  61.     private static final String WAVELENGTH_FACT_L1_2 = "WAVELENGTH FACT L1/2";
  62.     private static final String RCV_CLOCK_OFFS_APPL  = "RCV CLOCK OFFS APPL";
  63.     private static final String INTERVAL             = "INTERVAL";
  64.     private static final String TIME_OF_FIRST_OBS    = "TIME OF FIRST OBS";
  65.     private static final String TIME_OF_LAST_OBS     = "TIME OF LAST OBS";
  66.     private static final String LEAP_SECONDS         = "LEAP SECONDS";
  67.     private static final String PRN_NB_OF_OBS        = "PRN / # OF OBS";
  68.     private static final String NB_TYPES_OF_OBSERV   = "# / TYPES OF OBSERV";
  69.     private static final String END_OF_HEADER        = "END OF HEADER";
  70.     private static final String CENTER_OF_MASS_XYZ   = "CENTER OF MASS: XYZ";
  71.     private static final String SIGNAL_STRENGTH_UNIT = "SIGNAL STRENGTH UNIT";
  72.     private static final String SYS_NB_OBS_TYPES     = "SYS / # / OBS TYPES";
  73.     private static final String SYS_DCBS_APPLIED     = "SYS / DCBS APPLIED";
  74.     private static final String SYS_PCVS_APPLIED     = "SYS / PCVS APPLIED";
  75.     private static final String SYS_SCALE_FACTOR     = "SYS / SCALE FACTOR";
  76.     private static final String SYS_PHASE_SHIFT      = "SYS / PHASE SHIFT";
  77.     private static final String GLONASS_SLOT_FRQ_NB  = "GLONASS SLOT / FRQ #";
  78.     private static final String GLONASS_COD_PHS_BIS  = "GLONASS COD/PHS/BIS";

  79.     private static final String GPS                  = "GPS";
  80.     private static final String GAL                  = "GAL";
  81.     private static final String GLO                  = "GLO";
  82.     private static final String QZS                  = "QZS";
  83.     private static final String BDT                  = "BDT";
  84.     private static final String IRN                  = "IRN";
  85.     // CHECKSTYLE: resume JavadocVariable check

  86.     /** Rinex Observations, grouped by file. */
  87.     private final Map<RinexHeader, List<ObservationDataSet>> observations;

  88.     /** Simple constructor.
  89.      * <p>
  90.      * This constructor is used when the rinex files are managed by the
  91.      * global {@link DataProvidersManager DataProvidersManager}.
  92.      * </p>
  93.      * @param supportedNames regular expression for supported files names
  94.      * @exception OrekitException if no rinex file can be read
  95.      */
  96.     public RinexLoader(final String supportedNames)
  97.         throws OrekitException {
  98.         observations = new HashMap<>();
  99.         DataProvidersManager.getInstance().feed(supportedNames, new Parser());
  100.     }

  101.     /** Simple constructor.
  102.      * @param input data input stream
  103.      * @param name name of the file (or zip entry)
  104.      * @exception OrekitException if no rinex file can be read
  105.      */
  106.     public RinexLoader(final InputStream input, final String name)
  107.         throws OrekitException {
  108.         try {
  109.             observations = new HashMap<>();
  110.             new Parser().loadData(input, name);
  111.         } catch (IOException ioe) {
  112.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  113.         }
  114.     }

  115.     /** Add a Rinex header.
  116.      * @param header rinex header to add
  117.      * @return the list into which observations should be added
  118.      */
  119.     private List<ObservationDataSet> addHeader(final RinexHeader header) {
  120.         final List<ObservationDataSet> list = new ArrayList<>();
  121.         observations.put(header, list);
  122.         return list;
  123.     }

  124.     /** Get parsed rinex observations.
  125.      * @return unmodifiable view of parsed rinex observations
  126.      */
  127.     public Map<RinexHeader, List<ObservationDataSet>> getObservations() {
  128.         return Collections.unmodifiableMap(observations);
  129.     }

  130.     /** Parser for rinex files.
  131.      */
  132.     public class Parser implements DataLoader {

  133.         /** Index of label in data lines. */
  134.         private static final int LABEL_START = 60;

  135.         /** File type Accepted (only Observation Data). */
  136.         private static final String FILE_TYPE = "O"; //Only Observation Data files

  137.         /** {@inheritDoc} */
  138.         @Override
  139.         public boolean stillAcceptsData() {
  140.             // we load all rinex files we can find
  141.             return true;
  142.         }

  143.         /** {@inheritDoc} */
  144.         @Override
  145.         public void loadData(final InputStream input, final String name)
  146.             throws IOException, OrekitException {

  147.             try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"))) {

  148.                 // placeholders for parsed data
  149.                 SatelliteSystem                  satelliteSystem        = null;
  150.                 double                           formatVersion          = Double.NaN;
  151.                 boolean                          inRinexVersion         = false;
  152.                 int                              lineNumber             = 0;
  153.                 SatelliteSystem                  obsTypesSystem         = null;
  154.                 String                           markerName             = null;
  155.                 String                           markerNumber           = null;
  156.                 String                           markerType             = null;
  157.                 String                           observerName           = null;
  158.                 String                           agencyName             = null;
  159.                 String                           receiverNumber         = null;
  160.                 String                           receiverType           = null;
  161.                 String                           receiverVersion        = null;
  162.                 String                           antennaNumber          = null;
  163.                 String                           antennaType            = null;
  164.                 Vector3D                         approxPos              = null;
  165.                 Vector3D                         antRefPoint            = null;
  166.                 String                           obsCode                = null;
  167.                 Vector3D                         antPhaseCenter         = null;
  168.                 Vector3D                         antBSight              = null;
  169.                 double                           antAzi                 = Double.NaN;
  170.                 Vector3D                         antZeroDir             = null;
  171.                 Vector3D                         centerMass             = null;
  172.                 double                           antHeight              = Double.NaN;
  173.                 Vector2D                         eccentricities         = Vector2D.ZERO;
  174.                 int                              clkOffset              = -1;
  175.                 int                              nbTypes                = -1;
  176.                 int                              nbSat                  = -1;
  177.                 double                           interval               = Double.NaN;
  178.                 AbsoluteDate                     tFirstObs              = AbsoluteDate.PAST_INFINITY;
  179.                 AbsoluteDate                     tLastObs               = AbsoluteDate.FUTURE_INFINITY;
  180.                 TimeScale                        timeScale              = null;
  181.                 String                           timeScaleStr           = null;
  182.                 int                              leapSeconds            = 0;
  183.                 AbsoluteDate                     tObs                   = AbsoluteDate.PAST_INFINITY;
  184.                 String[]                         satsObsList            = null;
  185.                 String                           strYear                = null;
  186.                 int                              eventFlag              = -1;
  187.                 int                              nbSatObs               = -1;
  188.                 int                              nbLinesSat             = -1;
  189.                 double                           rcvrClkOffset          = 0;
  190.                 boolean                          inRunBy                = false;
  191.                 boolean                          inMarkerName           = false;
  192.                 boolean                          inMarkerType           = false;
  193.                 boolean                          inObserver             = false;
  194.                 boolean                          inRecType              = false;
  195.                 boolean                          inAntType              = false;
  196.                 boolean                          inAproxPos             = false;
  197.                 boolean                          inAntDelta             = false;
  198.                 boolean                          inTypesObs             = false;
  199.                 boolean                          inFirstObs             = false;
  200.                 boolean                          inPhaseShift           = false;
  201.                 boolean                          inGlonassSlot          = false;
  202.                 boolean                          inGlonassCOD           = false;
  203.                 List<ObservationDataSet>         observationsList       = null;

  204.                 //First line must  always contain Rinex Version, File Type and Satellite Systems Observed
  205.                 String line = reader.readLine();
  206.                 lineNumber++;
  207.                 formatVersion = parseDouble(line, 0, 9);
  208.                 int format100 = (int) FastMath.rint(100 * formatVersion);

  209.                 if ((format100 != 200) && (format100 != 210) && (format100 != 211) &&
  210.                     (format100 != 300) && (format100 != 301) && (format100 != 302) && (format100 != 303)) {
  211.                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  212.                 }

  213.                 //File Type must be Observation_Data
  214.                 if (!(parseString(line, 20, 1)).equals(FILE_TYPE)) {
  215.                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  216.                 }
  217.                 satelliteSystem = SatelliteSystem.parseSatelliteSystem(parseString(line, 40, 1));
  218.                 inRinexVersion = true;

  219.                 switch (format100 / 100) {
  220.                     case 2:

  221.                         final int                   MAX_OBS_TYPES_PER_LINE_RNX2 = 9;
  222.                         final int                   MAX_N_SAT_OBSERVATION       = 12;
  223.                         final int                   MAX_N_TYPES_OBSERVATION     = 5;
  224.                         final List<ObservationType> typesObs = new ArrayList<>();

  225.                         for (line = reader.readLine(); line != null; line = reader.readLine()) {
  226.                             ++lineNumber;

  227.                             if (observationsList == null) {
  228.                                 switch(line.substring(LABEL_START).trim()) {
  229.                                     case RINEX_VERSION_TYPE :

  230.                                         formatVersion = parseDouble(line, 0, 9);
  231.                                         //File Type must be Observation_Data
  232.                                         if (!(parseString(line, 20, 1)).equals(FILE_TYPE)) {
  233.                                             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  234.                                         }
  235.                                         satelliteSystem = SatelliteSystem.parseSatelliteSystem(parseString(line, 40, 1));
  236.                                         inRinexVersion = true;
  237.                                         break;
  238.                                     case COMMENT :
  239.                                         // nothing to do
  240.                                         break;
  241.                                     case PGM_RUN_BY_DATE :
  242.                                         inRunBy = true;
  243.                                         break;
  244.                                     case MARKER_NAME :
  245.                                         markerName = parseString(line, 0, 60);
  246.                                         inMarkerName = true;
  247.                                         break;
  248.                                     case MARKER_NUMBER :
  249.                                         markerNumber = parseString(line, 0, 20);
  250.                                         break;
  251.                                     case OBSERVER_AGENCY :
  252.                                         observerName = parseString(line, 0, 20);
  253.                                         agencyName   = parseString(line, 20, 40);
  254.                                         inObserver = true;
  255.                                         break;
  256.                                     case REC_NB_TYPE_VERS :
  257.                                         receiverNumber  = parseString(line, 0, 20);
  258.                                         receiverType    = parseString(line, 20, 20);
  259.                                         receiverVersion = parseString(line, 40, 20);
  260.                                         inRecType = true;
  261.                                         break;
  262.                                     case ANT_NB_TYPE :
  263.                                         antennaNumber = parseString(line, 0, 20);
  264.                                         antennaType   = parseString(line, 20, 20);
  265.                                         inAntType = true;
  266.                                         break;
  267.                                     case APPROX_POSITION_XYZ :
  268.                                         approxPos = new Vector3D(parseDouble(line, 0, 14), parseDouble(line, 14, 14),
  269.                                                                  parseDouble(line, 28, 14));
  270.                                         inAproxPos = true;
  271.                                         break;
  272.                                     case ANTENNA_DELTA_H_E_N :
  273.                                         antHeight = parseDouble(line, 0, 14);
  274.                                         eccentricities = new Vector2D(parseDouble(line, 14, 14), parseDouble(line, 28, 14));
  275.                                         inAntDelta = true;
  276.                                         break;
  277.                                     case NB_OF_SATELLITES :
  278.                                         nbSat = parseInt(line, 0, 6);
  279.                                         break;
  280.                                     case WAVELENGTH_FACT_L1_2 :
  281.                                         //Optional line in header
  282.                                         //Not stored for now
  283.                                         break;
  284.                                     case RCV_CLOCK_OFFS_APPL :
  285.                                         clkOffset = parseInt(line, 0, 6);
  286.                                         break;
  287.                                     case INTERVAL :
  288.                                         interval = parseDouble(line, 0, 10);
  289.                                         break;
  290.                                     case TIME_OF_FIRST_OBS :
  291.                                         switch (satelliteSystem) {
  292.                                             case GPS:
  293.                                                 timeScale = TimeScalesFactory.getGPS();
  294.                                                 break;
  295.                                             case GALILEO:
  296.                                                 timeScale = TimeScalesFactory.getGST();
  297.                                                 break;
  298.                                             case GLONASS:
  299.                                                 timeScale = TimeScalesFactory.getGLONASS();
  300.                                                 break;
  301.                                             case MIXED:
  302.                                                 //in Case of Mixed data, Timescale must be specified in the Time of First line
  303.                                                 timeScaleStr = parseString(line, 48, 3);

  304.                                                 if (timeScaleStr.equals(GPS)) {
  305.                                                     timeScale = TimeScalesFactory.getGPS();
  306.                                                 } else if (timeScaleStr.equals(GAL)) {
  307.                                                     timeScale = TimeScalesFactory.getGST();
  308.                                                 } else if (timeScaleStr.equals(GLO)) {
  309.                                                     timeScale = TimeScalesFactory.getGLONASS();
  310.                                                 } else {
  311.                                                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  312.                                                 }
  313.                                                 break;
  314.                                             default :
  315.                                                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  316.                                                                           lineNumber, name, line);
  317.                                         }

  318.                                         tFirstObs = new AbsoluteDate(parseInt(line, 0, 6),
  319.                                                                      parseInt(line, 6, 6),
  320.                                                                      parseInt(line, 12, 6),
  321.                                                                      parseInt(line, 18, 6),
  322.                                                                      parseInt(line, 24, 6),
  323.                                                                      parseDouble(line, 30, 13), timeScale);
  324.                                         inFirstObs = true;
  325.                                         break;
  326.                                     case TIME_OF_LAST_OBS :
  327.                                         tLastObs = new AbsoluteDate(parseInt(line, 0, 6),
  328.                                                                     parseInt(line, 6, 6),
  329.                                                                     parseInt(line, 12, 6),
  330.                                                                     parseInt(line, 18, 6),
  331.                                                                     parseInt(line, 24, 6),
  332.                                                                     parseDouble(line, 30, 13), timeScale);
  333.                                         break;
  334.                                     case LEAP_SECONDS :
  335.                                         leapSeconds = parseInt(line, 0, 6);
  336.                                         break;
  337.                                     case PRN_NB_OF_OBS :
  338.                                         //Optional line in header, indicates number of Observations par Satellite
  339.                                         //Not stored for now
  340.                                         break;
  341.                                     case NB_TYPES_OF_OBSERV :
  342.                                         nbTypes = parseInt(line, 0, 6);
  343.                                         final int nbLinesTypesObs = (nbTypes + MAX_OBS_TYPES_PER_LINE_RNX2 - 1 ) / MAX_OBS_TYPES_PER_LINE_RNX2;

  344.                                         for (int j = 0; j < nbLinesTypesObs; j++) {
  345.                                             if (j > 0) {
  346.                                                 line = reader.readLine(); //Next line
  347.                                                 lineNumber++;
  348.                                             }
  349.                                             final int iMax = FastMath.min(MAX_OBS_TYPES_PER_LINE_RNX2, nbTypes - typesObs.size());
  350.                                             for (int i = 0; i < iMax; i++) {
  351.                                                 try {
  352.                                                     typesObs.add(ObservationType.valueOf(parseString(line, 10 + (6 * i), 2)));
  353.                                                 } catch (IllegalArgumentException iae) {
  354.                                                     throw new OrekitException(OrekitMessages.UNKNOWN_RINEX_FREQUENCY,
  355.                                                                               parseString(line, 10 + (6 * i), 2), name, lineNumber);
  356.                                                 }
  357.                                             }
  358.                                         }
  359.                                         inTypesObs = true;
  360.                                         break;
  361.                                     case END_OF_HEADER :
  362.                                         //We make sure that we have read all the mandatory fields inside the header of the Rinex
  363.                                         if (!inRinexVersion || !inRunBy || !inMarkerName ||
  364.                                             !inObserver || !inRecType || !inAntType ||
  365.                                             !inAproxPos || !inAntDelta || !inTypesObs || !inFirstObs) {
  366.                                             throw new OrekitException(OrekitMessages.INCOMPLETE_HEADER, name);
  367.                                         }

  368.                                         //Header information gathered
  369.                                         observationsList = addHeader(new RinexHeader(formatVersion, satelliteSystem,
  370.                                                                                      markerName, markerNumber, observerName,
  371.                                                                                      agencyName, receiverNumber, receiverType,
  372.                                                                                      receiverVersion, antennaNumber, antennaType,
  373.                                                                                      approxPos, antHeight, eccentricities, interval,
  374.                                                                                      tFirstObs, tLastObs, clkOffset, leapSeconds));
  375.                                         break;
  376.                                     default :
  377.                                         if (observationsList == null) {
  378.                                             //There must be an error due to an unknown Label inside the Header
  379.                                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  380.                                                                       lineNumber, name, line);
  381.                                         }
  382.                                 }
  383.                             } else {

  384.                                 //Start of a new Observation
  385.                                 rcvrClkOffset     =  0;
  386.                                 nbLinesSat        = -1;
  387.                                 eventFlag         = -1;
  388.                                 nbSatObs          = -1;
  389.                                 satsObsList       = null;
  390.                                 tObs              = null;
  391.                                 strYear           = null;

  392.                                 eventFlag = parseInt(line, 28, 1);
  393.                                 //If eventFlag>1, we skip the corresponding lines to the next observation
  394.                                 if (eventFlag != 0) {
  395.                                     if (eventFlag == 6) {
  396.                                         nbSatObs  = parseInt(line, 29, 3);
  397.                                         nbLinesSat = (nbSatObs + 12 - 1) / 12;
  398.                                         final int nbLinesObs = (nbTypes + 5 - 1) / 5;
  399.                                         final int nbLinesSkip = (nbLinesSat - 1) + nbSatObs * nbLinesObs;
  400.                                         for (int i = 0; i < nbLinesSkip; i++) {
  401.                                             line = reader.readLine(); //Next line
  402.                                             lineNumber++;
  403.                                         }
  404.                                     } else {
  405.                                         final int nbLinesSkip = parseInt(line, 29, 3);
  406.                                         for (int i = 0; i < nbLinesSkip; i++) {
  407.                                             line = reader.readLine(); //Next line
  408.                                             lineNumber++;
  409.                                         }
  410.                                     }
  411.                                 } else {

  412.                                     final int y = Integer.parseInt(parseString(line, 0, 3));
  413.                                     if (79 < y && y <= 99) {
  414.                                         strYear = "19" + y;
  415.                                     } else if (0 <= y && y <= 79) {
  416.                                         strYear = "20" + parseString(line, 0, 3);
  417.                                     }
  418.                                     tObs = new AbsoluteDate(Integer.parseInt(strYear),
  419.                                                             parseInt(line, 3, 3),
  420.                                                             parseInt(line, 6, 3),
  421.                                                             parseInt(line, 9, 3),
  422.                                                             parseInt(line, 12, 3),
  423.                                                             parseDouble(line, 15, 11), timeScale);

  424.                                     nbSatObs  = parseInt(line, 29, 3);
  425.                                     satsObsList   = new String[nbSatObs];
  426.                                     //If the total number of satellites was indicated in the Header
  427.                                     if (nbSat != -1 && nbSatObs > nbSat) {
  428.                                         //we check that the number of Sat in the observation is consistent
  429.                                         throw new OrekitException(OrekitMessages.INCONSISTENT_NUMBER_OF_SATS,
  430.                                                                   lineNumber, name, nbSatObs, nbSat);
  431.                                     }

  432.                                     nbLinesSat = (nbSatObs + MAX_N_SAT_OBSERVATION - 1) / MAX_N_SAT_OBSERVATION;
  433.                                     for (int j = 0; j < nbLinesSat; j++) {
  434.                                         if (j > 0) {
  435.                                             line = reader.readLine(); //Next line
  436.                                             lineNumber++;
  437.                                         }
  438.                                         final int iMax = FastMath.min(MAX_N_SAT_OBSERVATION, nbSatObs  - j * MAX_N_SAT_OBSERVATION);
  439.                                         for (int i = 0; i < iMax; i++) {
  440.                                             satsObsList[i + MAX_N_SAT_OBSERVATION * j] = parseString(line, 32 + 3 * i, 3);
  441.                                         }

  442.                                         //Read the Receiver Clock offset, if present
  443.                                         rcvrClkOffset = parseDouble(line, 68, 12);
  444.                                         if (Double.isNaN(rcvrClkOffset)) {
  445.                                             rcvrClkOffset = 0.0;
  446.                                         }

  447.                                     }

  448.                                     //For each one of the Satellites in this observation
  449.                                     final int nbLinesObs = (nbTypes + MAX_N_TYPES_OBSERVATION - 1) / MAX_N_TYPES_OBSERVATION;
  450.                                     for (int k = 0; k < nbSatObs; k++) {


  451.                                         //Once the Date and Satellites list is read:
  452.                                         //  - to read the Data for each satellite
  453.                                         //  - 5 Observations per line
  454.                                         final List<ObservationData> observationData = new ArrayList<>(nbSatObs);
  455.                                         for (int j = 0; j < nbLinesObs; j++) {
  456.                                             line = reader.readLine(); //Next line
  457.                                             lineNumber++;
  458.                                             final int iMax = FastMath.min(MAX_N_TYPES_OBSERVATION, nbTypes - observationData.size());
  459.                                             for (int i = 0; i < iMax; i++) {
  460.                                                 observationData.add(new ObservationData(typesObs.get(observationData.size()),
  461.                                                                                         parseDouble(line, 16 * i, 14),
  462.                                                                                         parseInt(line, 14 + 16 * i, 1),
  463.                                                                                         parseInt(line, 15 + 16 * i, 1)));
  464.                                             }
  465.                                         }

  466.                                         //We check that the Satellite type is consistent with Satellite System in the top of the file
  467.                                         final SatelliteSystem satelliteSystemSat = SatelliteSystem.parseSatelliteSystem(satsObsList[k]);
  468.                                         if (!satelliteSystem.equals(SatelliteSystem.MIXED)) {
  469.                                             if (!satelliteSystemSat.equals(satelliteSystem)) {
  470.                                                 throw new OrekitException(OrekitMessages.INCONSISTENT_SATELLITE_SYSTEM,
  471.                                                                           lineNumber, name, satelliteSystem, satelliteSystemSat);
  472.                                             }
  473.                                         }

  474.                                         final int prnNumber;
  475.                                         switch (satelliteSystemSat) {
  476.                                             case GPS:
  477.                                             case GLONASS:
  478.                                             case GALILEO:
  479.                                                 prnNumber = Integer.parseInt(satsObsList[k].substring(1, 3).trim());
  480.                                                 break;
  481.                                             case SBAS:
  482.                                                 prnNumber = Integer.parseInt(satsObsList[k].substring(1, 3).trim()) + 100;
  483.                                                 break;
  484.                                             default:
  485.                                                 // MIXED satellite system is not allowed here
  486.                                                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  487.                                                                           lineNumber, name, line);
  488.                                         }

  489.                                         observationsList.add(new ObservationDataSet(satelliteSystemSat, prnNumber, tObs, rcvrClkOffset,
  490.                                                                                     observationData));

  491.                                     }
  492.                                 }
  493.                             }
  494.                         }
  495.                         break;
  496.                     case 3:

  497.                         final int                   MAX_OBS_TYPES_PER_LINE_RNX3 = 13;
  498.                         final int           MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE = 12;
  499.                         final int                    MAX_N_SAT_PHSHIFT_PER_LINE = 10;

  500.                         final Map<SatelliteSystem, List<ObservationType>> listTypeObs            = new HashMap<>();
  501.                         final List<ObservationType>                       typeObs                = new ArrayList<>();
  502.                         String                                           sigStrengthUnit        = null;
  503.                         int                                              leapSecondsFuture      = 0;
  504.                         int                                              leapSecondsWeekNum     = 0;
  505.                         int                                              leapSecondsDayNum      = 0;
  506.                         final List<AppliedDCBS>                          listAppliedDCBs        = new ArrayList<>();
  507.                         final List<AppliedPCVS>                          listAppliedPCVS        = new ArrayList<>();
  508.                         SatelliteSystem                                  satSystemScaleFactor   = null;
  509.                         int                                              scaleFactor            = 1;
  510.                         int                                              nbObsScaleFactor       = 0;
  511.                         final List<ObservationType>                       typesObsScaleFactor    = new ArrayList<>();
  512.                         final List<ScaleFactorCorrection>                scaleFactorCorrections = new ArrayList<>();
  513.                         String[]                                         satsPhaseShift         = null;
  514.                         int                                              nbSatPhaseShift        = 0;
  515.                         SatelliteSystem                                  satSystemPhaseShift    = null;
  516.                         double                                           corrPhaseShift         = 0.0;
  517.                         final List<PhaseShiftCorrection>                 phaseShiftCorrections  = new ArrayList<>();
  518.                         ObservationType                                   phaseShiftTypeObs      = null;


  519.                         for (line = reader.readLine(); line != null; line = reader.readLine()) {
  520.                             ++lineNumber;
  521.                             if (observationsList == null) {
  522.                                 switch(line.substring(LABEL_START).trim()) {
  523.                                     case RINEX_VERSION_TYPE : {
  524.                                         formatVersion = parseDouble(line, 0, 9);
  525.                                         format100     = (int) FastMath.rint(100 * formatVersion);
  526.                                         if ((format100 != 300) && (format100 != 301) && (format100 != 302) && (format100 != 303)) {
  527.                                             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  528.                                         }
  529.                                         //File Type must be Observation_Data
  530.                                         if (!(parseString(line, 20, 1)).equals(FILE_TYPE)) {
  531.                                             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  532.                                         }
  533.                                         satelliteSystem = SatelliteSystem.parseSatelliteSystem(parseString(line, 40, 1));
  534.                                         inRinexVersion = true;
  535.                                     }
  536.                                         break;
  537.                                     case COMMENT :
  538.                                         // nothing to do
  539.                                         break;
  540.                                     case PGM_RUN_BY_DATE :
  541.                                         inRunBy = true;
  542.                                         break;
  543.                                     case MARKER_NAME :
  544.                                         markerName = parseString(line, 0, 60);
  545.                                         inMarkerName = true;
  546.                                         break;
  547.                                     case MARKER_NUMBER :
  548.                                         markerNumber = parseString(line, 0, 20);
  549.                                         break;
  550.                                     case MARKER_TYPE :
  551.                                         markerType = parseString(line, 0, 20);
  552.                                         inMarkerType = true;
  553.                                         //Could be done with an Enumeration
  554.                                         break;
  555.                                     case OBSERVER_AGENCY :
  556.                                         observerName = parseString(line, 0, 20);
  557.                                         agencyName   = parseString(line, 20, 40);
  558.                                         inObserver = true;
  559.                                         break;
  560.                                     case REC_NB_TYPE_VERS :
  561.                                         receiverNumber  = parseString(line, 0, 20);
  562.                                         receiverType    = parseString(line, 20, 20);
  563.                                         receiverVersion = parseString(line, 40, 20);
  564.                                         inRecType = true;
  565.                                         break;
  566.                                     case ANT_NB_TYPE :
  567.                                         antennaNumber = parseString(line, 0, 20);
  568.                                         antennaType   = parseString(line, 20, 20);
  569.                                         inAntType = true;
  570.                                         break;
  571.                                     case APPROX_POSITION_XYZ :
  572.                                         approxPos = new Vector3D(parseDouble(line, 0, 14),
  573.                                                                  parseDouble(line, 14, 14),
  574.                                                                  parseDouble(line, 28, 14));
  575.                                         inAproxPos = true;
  576.                                         break;
  577.                                     case ANTENNA_DELTA_H_E_N :
  578.                                         antHeight = parseDouble(line, 0, 14);
  579.                                         eccentricities = new Vector2D(parseDouble(line, 14, 14),
  580.                                                                       parseDouble(line, 28, 14));
  581.                                         inAntDelta = true;
  582.                                         break;
  583.                                     case ANTENNA_DELTA_X_Y_Z :
  584.                                         antRefPoint = new Vector3D(parseDouble(line, 0, 14),
  585.                                                                    parseDouble(line, 14, 14),
  586.                                                                    parseDouble(line, 28, 14));
  587.                                         break;
  588.                                     case ANTENNA_PHASECENTER :
  589.                                         obsCode = parseString(line, 2, 3);
  590.                                         antPhaseCenter = new Vector3D(parseDouble(line, 5, 9),
  591.                                                                       parseDouble(line, 14, 14),
  592.                                                                       parseDouble(line, 28, 14));
  593.                                         break;
  594.                                     case ANTENNA_B_SIGHT_XYZ :
  595.                                         antBSight = new Vector3D(parseDouble(line, 0, 14),
  596.                                                                  parseDouble(line, 14, 14),
  597.                                                                  parseDouble(line, 28, 14));
  598.                                         break;
  599.                                     case ANTENNA_ZERODIR_AZI :
  600.                                         antAzi = parseDouble(line, 0, 14);
  601.                                         break;
  602.                                     case ANTENNA_ZERODIR_XYZ :
  603.                                         antZeroDir = new Vector3D(parseDouble(line, 0, 14),
  604.                                                                   parseDouble(line, 14, 14),
  605.                                                                   parseDouble(line, 28, 14));
  606.                                         break;
  607.                                     case CENTER_OF_MASS_XYZ :
  608.                                         centerMass = new Vector3D(parseDouble(line, 0, 14),
  609.                                                                   parseDouble(line, 14, 14),
  610.                                                                   parseDouble(line, 28, 14));
  611.                                         break;
  612.                                     case NB_OF_SATELLITES :
  613.                                         nbSat = parseInt(line, 0, 6);
  614.                                         break;
  615.                                     case RCV_CLOCK_OFFS_APPL :
  616.                                         clkOffset = parseInt(line, 0, 6);
  617.                                         break;
  618.                                     case INTERVAL :
  619.                                         interval = parseDouble(line, 0, 10);
  620.                                         break;
  621.                                     case TIME_OF_FIRST_OBS :
  622.                                         switch(satelliteSystem) {
  623.                                             case GPS:
  624.                                                 timeScale = TimeScalesFactory.getGPS();
  625.                                                 break;
  626.                                             case GALILEO:
  627.                                                 timeScale = TimeScalesFactory.getGST();
  628.                                                 break;
  629.                                             case GLONASS:
  630.                                                 timeScale = TimeScalesFactory.getGLONASS();
  631.                                                 break;
  632.                                             case QZSS:
  633.                                                 timeScale = TimeScalesFactory.getQZSS();
  634.                                                 break;
  635.                                             case BEIDOU:
  636.                                                 timeScale = TimeScalesFactory.getBDT();
  637.                                                 break;
  638.                                             case IRNSS:
  639.                                                 timeScale = TimeScalesFactory.getIRNSS();
  640.                                                 break;
  641.                                             case MIXED:
  642.                                                 //in Case of Mixed data, Timescale must be specified in the Time of First line
  643.                                                 timeScaleStr = parseString(line, 48, 3);

  644.                                                 if (timeScaleStr.equals(GPS)) {
  645.                                                     timeScale = TimeScalesFactory.getGPS();
  646.                                                 } else if (timeScaleStr.equals(GAL)) {
  647.                                                     timeScale = TimeScalesFactory.getGST();
  648.                                                 } else if (timeScaleStr.equals(GLO)) {
  649.                                                     timeScale = TimeScalesFactory.getGLONASS();
  650.                                                 } else if (timeScaleStr.equals(QZS)) {
  651.                                                     timeScale = TimeScalesFactory.getQZSS();
  652.                                                 } else if (timeScaleStr.equals(BDT)) {
  653.                                                     timeScale = TimeScalesFactory.getBDT();
  654.                                                 } else if (timeScaleStr.equals(IRN)) {
  655.                                                     timeScale = TimeScalesFactory.getIRNSS();
  656.                                                 } else {
  657.                                                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  658.                                                 }
  659.                                                 break;
  660.                                             default :
  661.                                                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  662.                                                                           lineNumber, name, line);
  663.                                         }

  664.                                         tFirstObs = new AbsoluteDate(parseInt(line, 0, 6),
  665.                                                                      parseInt(line, 6, 6),
  666.                                                                      parseInt(line, 12, 6),
  667.                                                                      parseInt(line, 18, 6),
  668.                                                                      parseInt(line, 24, 6),
  669.                                                                      parseDouble(line, 30, 13), timeScale);
  670.                                         inFirstObs = true;
  671.                                         break;
  672.                                     case TIME_OF_LAST_OBS :
  673.                                         tLastObs = new AbsoluteDate(parseInt(line, 0, 6),
  674.                                                                     parseInt(line, 6, 6),
  675.                                                                     parseInt(line, 12, 6),
  676.                                                                     parseInt(line, 18, 6),
  677.                                                                     parseInt(line, 24, 6),
  678.                                                                     parseDouble(line, 30, 13), timeScale);
  679.                                         break;
  680.                                     case LEAP_SECONDS :
  681.                                         leapSeconds = parseInt(line, 0, 6);
  682.                                         leapSecondsFuture = parseInt(line, 6, 6);
  683.                                         leapSecondsWeekNum = parseInt(line, 12, 6);
  684.                                         leapSecondsDayNum = parseInt(line, 18, 6);
  685.                                         //Time System Identifier must be added, last A3 String
  686.                                         break;
  687.                                     case PRN_NB_OF_OBS :
  688.                                         //Optional line in header, indicates number of Observations par Satellite
  689.                                         //Not stored for now
  690.                                         break;
  691.                                     case SYS_NB_OBS_TYPES :
  692.                                         obsTypesSystem = null;
  693.                                         typeObs.clear();

  694.                                         obsTypesSystem = SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1));
  695.                                         nbTypes = parseInt(line, 3, 3);

  696.                                         final int nbLinesTypesObs = (nbTypes + MAX_OBS_TYPES_PER_LINE_RNX3 - 1) / MAX_OBS_TYPES_PER_LINE_RNX3;
  697.                                         for (int j = 0; j < nbLinesTypesObs; j++) {
  698.                                             if (j > 0) {
  699.                                                 line = reader.readLine(); //Next line
  700.                                                 lineNumber++;
  701.                                             }
  702.                                             final int iMax = FastMath.min(MAX_OBS_TYPES_PER_LINE_RNX3, nbTypes - typeObs.size());
  703.                                             for (int i = 0; i < iMax; i++) {
  704.                                                 try {
  705.                                                     typeObs.add(ObservationType.valueOf(parseString(line, 7 + (4 * i), 3)));
  706.                                                 } catch (IllegalArgumentException iae) {
  707.                                                     throw new OrekitException(OrekitMessages.UNKNOWN_RINEX_FREQUENCY,
  708.                                                                               parseString(line, 7 + (4 * i), 3), name, lineNumber);
  709.                                                 }
  710.                                             }
  711.                                         }
  712.                                         listTypeObs.put(obsTypesSystem, new ArrayList<>(typeObs));
  713.                                         inTypesObs = true;
  714.                                         break;
  715.                                     case SIGNAL_STRENGTH_UNIT :
  716.                                         sigStrengthUnit = parseString(line, 0, 20);
  717.                                         break;
  718.                                     case SYS_DCBS_APPLIED :

  719.                                         listAppliedDCBs.add(new AppliedDCBS(SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1)),
  720.                                                                             parseString(line, 2, 17), parseString(line, 20, 40)));
  721.                                         break;
  722.                                     case SYS_PCVS_APPLIED :

  723.                                         listAppliedPCVS.add(new AppliedPCVS(SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1)),
  724.                                                                             parseString(line, 2, 17), parseString(line, 20, 40)));
  725.                                         break;
  726.                                     case SYS_SCALE_FACTOR :
  727.                                         satSystemScaleFactor  = null;
  728.                                         scaleFactor           = 1;
  729.                                         nbObsScaleFactor      = 0;

  730.                                         satSystemScaleFactor = SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1));
  731.                                         scaleFactor          = parseInt(line, 2, 4);
  732.                                         nbObsScaleFactor     = parseInt(line, 8, 2);

  733.                                         if (nbObsScaleFactor == 0) {
  734.                                             typesObsScaleFactor.addAll(listTypeObs.get(satSystemScaleFactor));
  735.                                         } else {
  736.                                             final int nbLinesTypesObsScaleFactor = (nbObsScaleFactor + MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE - 1) /
  737.                                                                                    MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE;
  738.                                             for (int j = 0; j < nbLinesTypesObsScaleFactor; j++) {
  739.                                                 if ( j > 0) {
  740.                                                     line = reader.readLine(); //Next line
  741.                                                     lineNumber++;
  742.                                                 }
  743.                                                 final int iMax = FastMath.min(MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE, nbObsScaleFactor - typesObsScaleFactor.size());
  744.                                                 for (int i = 0; i < iMax; i++) {
  745.                                                     typesObsScaleFactor.add(ObservationType.valueOf(parseString(line, 11 + (4 * i), 3)));
  746.                                                 }
  747.                                             }
  748.                                         }

  749.                                         scaleFactorCorrections.add(new ScaleFactorCorrection(satSystemScaleFactor,
  750.                                                                                              scaleFactor, typesObsScaleFactor));
  751.                                         break;
  752.                                     case SYS_PHASE_SHIFT :

  753.                                         nbSatPhaseShift     = 0;
  754.                                         satsPhaseShift      = null;
  755.                                         corrPhaseShift      = 0.0;
  756.                                         phaseShiftTypeObs   = null;
  757.                                         satSystemPhaseShift = null;

  758.                                         satSystemPhaseShift = SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1));
  759.                                         phaseShiftTypeObs = ObservationType.valueOf(parseString(line, 2, 3));
  760.                                         nbSatPhaseShift = parseInt(line, 16, 2);
  761.                                         corrPhaseShift = parseDouble(line, 6, 8);

  762.                                         if (nbSatPhaseShift == 0) {
  763.                                             //If nbSat with Phase Shift is not indicated: all the satellites are affected for this Obs Type
  764.                                         } else {
  765.                                             satsPhaseShift = new String[nbSatPhaseShift];
  766.                                             final int nbLinesSatPhaseShift = (nbSatPhaseShift + MAX_N_SAT_PHSHIFT_PER_LINE - 1) / MAX_N_SAT_PHSHIFT_PER_LINE;
  767.                                             for (int j = 0; j < nbLinesSatPhaseShift; j++) {
  768.                                                 if (j > 0) {
  769.                                                     line = reader.readLine(); //Next line
  770.                                                     lineNumber++;
  771.                                                 }
  772.                                                 final int iMax = FastMath.min(MAX_N_SAT_PHSHIFT_PER_LINE, nbSatPhaseShift - j * MAX_N_SAT_PHSHIFT_PER_LINE);
  773.                                                 for (int i = 0; i < iMax; i++) {
  774.                                                     satsPhaseShift[i + 10 * j] = parseString(line, 19 + 4 * i, 3);
  775.                                                 }
  776.                                             }
  777.                                         }
  778.                                         phaseShiftCorrections.add(new PhaseShiftCorrection(satSystemPhaseShift,
  779.                                                                                            phaseShiftTypeObs,
  780.                                                                                            corrPhaseShift,
  781.                                                                                            satsPhaseShift));
  782.                                         inPhaseShift = true;
  783.                                         break;
  784.                                     case GLONASS_SLOT_FRQ_NB :
  785.                                         //Not defined yet
  786.                                         inGlonassSlot = true;
  787.                                         break;
  788.                                     case GLONASS_COD_PHS_BIS :
  789.                                         //Not defined yet
  790.                                         inGlonassCOD = true;
  791.                                         break;
  792.                                     case END_OF_HEADER :
  793.                                         //We make sure that we have read all the mandatory fields inside the header of the Rinex
  794.                                         if (!inRinexVersion || !inRunBy || !inMarkerName ||
  795.                                             !inMarkerType || !inObserver || !inRecType || !inAntType ||
  796.                                             !inAproxPos || !inAntDelta || !inTypesObs || !inFirstObs ||
  797.                                             (formatVersion >= 3.01 && !inPhaseShift) ||
  798.                                             (formatVersion >= 3.03 && (!inGlonassSlot || !inGlonassCOD))) {
  799.                                             throw new OrekitException(OrekitMessages.INCOMPLETE_HEADER, name);
  800.                                         }

  801.                                         //Header information gathered
  802.                                         observationsList = addHeader(new RinexHeader(formatVersion, satelliteSystem,
  803.                                                                                      markerName, markerNumber, markerType,
  804.                                                                                      observerName, agencyName, receiverNumber,
  805.                                                                                      receiverType, receiverVersion, antennaNumber,
  806.                                                                                      antennaType, approxPos, antHeight, eccentricities,
  807.                                                                                      antRefPoint, obsCode, antPhaseCenter, antBSight,
  808.                                                                                      antAzi, antZeroDir, centerMass, sigStrengthUnit,
  809.                                                                                      interval, tFirstObs, tLastObs, clkOffset, listAppliedDCBs,
  810.                                                                                      listAppliedPCVS, phaseShiftCorrections, leapSeconds,
  811.                                                                                      leapSecondsFuture, leapSecondsWeekNum, leapSecondsDayNum));
  812.                                         break;
  813.                                     default :
  814.                                         if (observationsList == null) {
  815.                                             //There must be an error due to an unknown Label inside the Header
  816.                                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  817.                                                                       lineNumber, name, line);
  818.                                         }
  819.                                 }
  820.                             } else {
  821.                                 //If End of Header

  822.                                 //Start of a new Observation
  823.                                 rcvrClkOffset     =  0;
  824.                                 eventFlag         = -1;
  825.                                 nbSatObs          = -1;
  826.                                 tObs              = null;

  827.                                 //A line that starts with ">" correspond to a new observation epoch
  828.                                 if (parseString(line, 0, 1).equals(">")) {

  829.                                     eventFlag = parseInt(line, 31, 1);
  830.                                     //If eventFlag>1, we skip the corresponding lines to the next observation
  831.                                     if (eventFlag != 0) {
  832.                                         final int nbLinesSkip = parseInt(line, 32, 3);
  833.                                         for (int i = 0; i < nbLinesSkip; i++) {
  834.                                             line = reader.readLine();
  835.                                             lineNumber++;
  836.                                         }
  837.                                     } else {

  838.                                         tObs = new AbsoluteDate(parseInt(line, 2, 4),
  839.                                                                 parseInt(line, 6, 3),
  840.                                                                 parseInt(line, 9, 3),
  841.                                                                 parseInt(line, 12, 3),
  842.                                                                 parseInt(line, 15, 3),
  843.                                                                 parseDouble(line, 18, 11), timeScale);

  844.                                         nbSatObs  = parseInt(line, 32, 3);
  845.                                         //If the total number of satellites was indicated in the Header
  846.                                         if (nbSat != -1 && nbSatObs > nbSat) {
  847.                                             //we check that the number of Sat in the observation is consistent
  848.                                             throw new OrekitException(OrekitMessages.INCONSISTENT_NUMBER_OF_SATS,
  849.                                                                       lineNumber, name, nbSatObs, nbSat);
  850.                                         }
  851.                                         //Read the Receiver Clock offset, if present
  852.                                         rcvrClkOffset = parseDouble(line, 41, 15);
  853.                                         if (Double.isNaN(rcvrClkOffset)) {
  854.                                             rcvrClkOffset = 0.0;
  855.                                         }

  856.                                         //For each one of the Satellites in this Observation
  857.                                         for (int i = 0; i < nbSatObs; i++) {

  858.                                             line = reader.readLine();
  859.                                             lineNumber++;

  860.                                             //We check that the Satellite type is consistent with Satellite System in the top of the file
  861.                                             final SatelliteSystem satelliteSystemSat = SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1));
  862.                                             if (!satelliteSystem.equals(SatelliteSystem.MIXED)) {
  863.                                                 if (!satelliteSystemSat.equals(satelliteSystem)) {
  864.                                                     throw new OrekitException(OrekitMessages.INCONSISTENT_SATELLITE_SYSTEM,
  865.                                                                               lineNumber, name, satelliteSystem, satelliteSystemSat);
  866.                                                 }
  867.                                             }

  868.                                             final int prn = parseInt(line, 1, 2);
  869.                                             final int prnNumber;
  870.                                             switch (satelliteSystemSat) {
  871.                                                 case GPS:
  872.                                                 case GLONASS:
  873.                                                 case GALILEO:
  874.                                                 case BEIDOU:
  875.                                                 case IRNSS:
  876.                                                     prnNumber = prn;
  877.                                                     break;
  878.                                                 case QZSS:
  879.                                                     prnNumber = prn + 192;
  880.                                                     break;
  881.                                                 case SBAS:
  882.                                                     prnNumber = prn + 100;
  883.                                                     break;
  884.                                                 default:
  885.                                                     // MIXED satellite system is not allowed here
  886.                                                     throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  887.                                                                               lineNumber, name, line);
  888.                                             }
  889.                                             final List<ObservationData> observationData = new ArrayList<>(nbSatObs);
  890.                                             for (int j = 0; j < listTypeObs.get(satelliteSystemSat).size(); j++) {
  891.                                                 final ObservationType rf = listTypeObs.get(satelliteSystemSat).get(j);
  892.                                                 boolean scaleFactorFound = false;
  893.                                                 //We look for the lines of ScaledFactorCorrections that correspond to this SatSystem
  894.                                                 int k = 0;
  895.                                                 double value = parseDouble(line, 3 + j * 16, 14);
  896.                                                 while (k < scaleFactorCorrections.size() && !scaleFactorFound) {
  897.                                                     if (scaleFactorCorrections.get(k).getSatelliteSystem().equals(satelliteSystemSat)) {
  898.                                                         //We check if the next Observation Type to read needs to be scaled
  899.                                                         if (scaleFactorCorrections.get(k).getTypesObsScaled().contains(rf)) {
  900.                                                             value /= scaleFactorCorrections.get(k).getCorrection();
  901.                                                             scaleFactorFound = true;
  902.                                                         }
  903.                                                     }
  904.                                                     k++;
  905.                                                 }
  906.                                                 observationData.add(new ObservationData(rf,
  907.                                                                                         value,
  908.                                                                                         parseInt(line, 17 + j * 16, 1),
  909.                                                                                         parseInt(line, 18 + j * 16, 1)));
  910.                                             }
  911.                                             observationsList.add(new ObservationDataSet(satelliteSystemSat, prnNumber, tObs, rcvrClkOffset,
  912.                                                                                         observationData));

  913.                                         }
  914.                                     }
  915.                                 }
  916.                             }
  917.                         }
  918.                         break;
  919.                     default:
  920.                         //If RINEX Version is neither 2 nor 3
  921.                         throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  922.                 }
  923.             }
  924.         }


  925.         /** Extract a string from a line.
  926.          * @param line to parse
  927.          * @param start start index of the string
  928.          * @param length length of the string
  929.          * @return parsed string
  930.          */
  931.         private String parseString(final String line, final int start, final int length) {
  932.             if (line.length() > start) {
  933.                 return line.substring(start, FastMath.min(line.length(), start + length)).trim();
  934.             } else {
  935.                 return null;
  936.             }
  937.         }

  938.         /** Extract an integer from a line.
  939.          * @param line to parse
  940.          * @param start start index of the integer
  941.          * @param length length of the integer
  942.          * @return parsed integer
  943.          */
  944.         private int parseInt(final String line, final int start, final int length) {
  945.             if (line.length() > start && !parseString(line, start, length).isEmpty()) {
  946.                 return Integer.parseInt(parseString(line, start, length));
  947.             } else {
  948.                 return 0;
  949.             }
  950.         }

  951.         /** Extract a double from a line.
  952.          * @param line to parse
  953.          * @param start start index of the real
  954.          * @param length length of the real
  955.          * @return parsed real, or {@code Double.NaN} if field was empty
  956.          */
  957.         private double parseDouble(final String line, final int start, final int length) {
  958.             if (line.length() > start && !parseString(line, start, length).isEmpty()) {
  959.                 return Double.parseDouble(parseString(line, start, length));
  960.             } else {
  961.                 return Double.NaN;
  962.             }
  963.         }

  964.         /** Phase Shift corrections.
  965.          * Contains the phase shift corrections used to
  966.          * generate phases consistent with respect to cycle shifts.
  967.          */
  968.         public class PhaseShiftCorrection {

  969.             /** Satellite System. */
  970.             private final SatelliteSystem satSystemPhaseShift;
  971.             /** Carrier Phase Observation Code. */
  972.             private final ObservationType typeObsPhaseShift;
  973.             /** Phase Shift Corrections (cycles). */
  974.             private final double phaseShiftCorrection;
  975.             /** List of satellites involved. */
  976.             private final String[] satsPhaseShift;

  977.             /** Simple constructor.
  978.              * @param satSystemPhaseShift Satellite System
  979.              * @param typeObsPhaseShift Carrier Phase Observation Code
  980.              * @param phaseShiftCorrection Phase Shift Corrections (cycles)
  981.              * @param satsPhaseShift List of satellites involved
  982.              */
  983.             private PhaseShiftCorrection(final SatelliteSystem satSystemPhaseShift,
  984.                                          final ObservationType typeObsPhaseShift,
  985.                                          final double phaseShiftCorrection, final String[] satsPhaseShift) {
  986.                 this.satSystemPhaseShift = satSystemPhaseShift;
  987.                 this.typeObsPhaseShift = typeObsPhaseShift;
  988.                 this.phaseShiftCorrection = phaseShiftCorrection;
  989.                 this.satsPhaseShift = satsPhaseShift;
  990.             }

  991.             /** Get the Satellite System.
  992.              * @return Satellite System.
  993.              */
  994.             public SatelliteSystem getSatelliteSystem() {
  995.                 return satSystemPhaseShift;
  996.             }
  997.             /** Get the Carrier Phase Observation Code.
  998.              * @return Carrier Phase Observation Code.
  999.              */
  1000.             public ObservationType getTypeObs() {
  1001.                 return typeObsPhaseShift;
  1002.             }
  1003.             /** Get the Phase Shift Corrections.
  1004.              * @return Phase Shift Corrections (cycles)
  1005.              */
  1006.             public double getCorrection() {
  1007.                 return phaseShiftCorrection;
  1008.             }
  1009.             /** Get the list of satellites involved.
  1010.              * @return List of satellites involved (if null, all the sats are involved)
  1011.              */
  1012.             public String[] getSatsCorrected() {
  1013.                 //If empty, all the satellites of this constellation are affected for this Observation type
  1014.                 return satsPhaseShift;
  1015.             }
  1016.         }

  1017.         /** Scale Factor to be applied.
  1018.          * Contains the scale factors of 10 applied to the data before
  1019.          * being stored into the RINEX file.
  1020.          */
  1021.         public class ScaleFactorCorrection {

  1022.             /** Satellite System. */
  1023.             private final SatelliteSystem satSystemScaleFactor;
  1024.             /** List of Observations types that have been scaled. */
  1025.             private final List<ObservationType> typesObsScaleFactor;
  1026.             /** Factor to divide stored observations with before use. */
  1027.             private final double scaleFactor;

  1028.             /** Simple constructor.
  1029.              * @param satSystemScaleFactor Satellite System
  1030.              * @param scaleFactor Factor to divide stored observations (1,10,100,1000)
  1031.              * @param typesObsScaleFactor List of Observations types that have been scaled
  1032.              */
  1033.             private ScaleFactorCorrection(final SatelliteSystem satSystemScaleFactor,
  1034.                                           final double scaleFactor,
  1035.                                           final List<ObservationType> typesObsScaleFactor) {
  1036.                 this.satSystemScaleFactor = satSystemScaleFactor;
  1037.                 this.scaleFactor = scaleFactor;
  1038.                 this.typesObsScaleFactor = typesObsScaleFactor;
  1039.             }
  1040.             /** Get the Satellite System.
  1041.              * @return Satellite System
  1042.              */
  1043.             public SatelliteSystem getSatelliteSystem() {
  1044.                 return satSystemScaleFactor;
  1045.             }
  1046.             /** Get the Scale Factor.
  1047.              * @return Scale Factor
  1048.              */
  1049.             public double getCorrection() {
  1050.                 return scaleFactor;
  1051.             }
  1052.             /** Get the list of Observation Types scaled.
  1053.              * @return List of Observation types scaled
  1054.              */
  1055.             public List<ObservationType> getTypesObsScaled() {
  1056.                 return typesObsScaleFactor;
  1057.             }
  1058.         }

  1059.         /** Corrections of Differential Code Biases (DCBs) applied.
  1060.          * Contains information on the programs used to correct the observations
  1061.          * in RINEX files for differential code biases.
  1062.          */
  1063.         public class AppliedDCBS {

  1064.             /** Satellite system. */
  1065.             private final SatelliteSystem satelliteSystem;

  1066.             /** Program name used to apply differential code bias corrections. */
  1067.             private final String progDCBS;

  1068.             /** Source of corrections (URL). */
  1069.             private final String sourceDCBS;

  1070.             /** Simple constructor.
  1071.              * @param satelliteSystem satellite system
  1072.              * @param progDCBS Program name used to apply DCBs
  1073.              * @param sourceDCBS Source of corrections (URL)
  1074.              */
  1075.             private AppliedDCBS(final SatelliteSystem satelliteSystem,
  1076.                                 final String progDCBS, final String sourceDCBS) {
  1077.                 this.satelliteSystem = satelliteSystem;
  1078.                 this.progDCBS        = progDCBS;
  1079.                 this.sourceDCBS      = sourceDCBS;
  1080.             }

  1081.             /** Get the satellite system.
  1082.              * @return satellite system
  1083.              */
  1084.             public SatelliteSystem getSatelliteSystem() {
  1085.                 return satelliteSystem;
  1086.             }

  1087.             /** Get the program name used to apply DCBs.
  1088.              * @return  Program name used to apply DCBs
  1089.              */
  1090.             public String getProgDCBS() {
  1091.                 return progDCBS;
  1092.             }

  1093.             /** Get the source of corrections.
  1094.              * @return Source of corrections (URL)
  1095.              */
  1096.             public String getSourceDCBS() {
  1097.                 return sourceDCBS;
  1098.             }

  1099.         }

  1100.         /** Corrections of antenna phase center variations (PCVs) applied.
  1101.          * Contains information on the programs used to correct the observations
  1102.          * in RINEX files for antenna phase center variations.
  1103.          */
  1104.         public class AppliedPCVS {

  1105.             /** Satellite system. */
  1106.             private final SatelliteSystem satelliteSystem;

  1107.             /** Program name used to antenna center variation corrections. */
  1108.             private final String progPCVS;

  1109.             /** Source of corrections (URL). */
  1110.             private final String sourcePCVS;

  1111.             /** Simple constructor.
  1112.              * @param satelliteSystem satellite system
  1113.              * @param progPCVS Program name used for PCVs
  1114.              * @param sourcePCVS Source of corrections (URL)
  1115.              */
  1116.             private AppliedPCVS(final SatelliteSystem satelliteSystem,
  1117.                                 final String progPCVS, final String sourcePCVS) {
  1118.                 this.satelliteSystem = satelliteSystem;
  1119.                 this.progPCVS        = progPCVS;
  1120.                 this.sourcePCVS      = sourcePCVS;
  1121.             }

  1122.             /** Get the satellite system.
  1123.              * @return satellite system
  1124.              */
  1125.             public SatelliteSystem getSatelliteSystem() {
  1126.                 return satelliteSystem;
  1127.             }

  1128.             /** Get the program name used to apply PCVs.
  1129.              * @return  Program name used to apply PCVs
  1130.              */
  1131.             public String getProgPCVS() {
  1132.                 return progPCVS;
  1133.             }

  1134.             /** Get the source of corrections.
  1135.              * @return Source of corrections (URL)
  1136.              */
  1137.             public String getSourcePCVS() {
  1138.                 return sourcePCVS;
  1139.             }

  1140.         }
  1141.     }

  1142. }