RinexLoader.java

  1. /* Copyright 2002-2019 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. /** Loader for Rinex measurements files.
  39.  * <p>
  40.  * Supported versions are: 2.00, 2.10, 2.11, 2.12 (unofficial), 2.20 (unofficial),
  41.  * 3.00, 3.01, 3.02, and 3.03.
  42.  * </p>
  43.  * @see <a href="ftp://igs.org/pub/data/format/rinex2.txt">rinex 2.0</a>
  44.  * @see <a href="ftp://igs.org/pub/data/forma/rinex210.txt">rinex 2.10</a>
  45.  * @see <a href="ftp://igs.org/pub/data/forma/rinex211.txt">rinex 2.11</a>
  46.  * @see <a href="http://www.aiub.unibe.ch/download/rinex/rinex212.txt">unofficial rinex 2.12</a>
  47.  * @see <a href="http://www.aiub.unibe.ch/download/rinex/rnx_leo.txt">unofficial rinex 2.20</a>
  48.  * @see <a href="ftp://igs.org/pub/data/format/rinex300.pdf">rinex 3.00</a>
  49.  * @see <a href="ftp://igs.org/pub/data/format/rinex301.pdf">rinex 3.01</a>
  50.  * @see <a href="ftp://igs.org/pub/data/format/rinex302.pdf">rinex 3.02</a>
  51.  * @see <a href="ftp://igs.org/pub/data/format/rinex303.pdf">rinex 3.03</a>
  52.  * @since 9.2
  53.  */
  54. public class RinexLoader {

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

  57.     /** Default supported files name pattern for rinex 3 observation files. */
  58.     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$";

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

  96.     private static final String GPS                  = "GPS";
  97.     private static final String GAL                  = "GAL";
  98.     private static final String GLO                  = "GLO";
  99.     private static final String QZS                  = "QZS";
  100.     private static final String BDT                  = "BDT";
  101.     private static final String IRN                  = "IRN";
  102.     // CHECKSTYLE: resume JavadocVariable check

  103.     /** Rinex Observations. */
  104.     private final List<ObservationDataSet> observationDataSets;

  105.     /** Simple constructor.
  106.      * <p>
  107.      * This constructor is used when the rinex files are managed by the
  108.      * global {@link DataProvidersManager DataProvidersManager}.
  109.      * </p>
  110.      * @param supportedNames regular expression for supported files names
  111.      */
  112.     public RinexLoader(final String supportedNames) {
  113.         observationDataSets = new ArrayList<>();
  114.         DataProvidersManager.getInstance().feed(supportedNames, new Parser());
  115.     }

  116.     /** Simple constructor.
  117.      * @param input data input stream
  118.      * @param name name of the file (or zip entry)
  119.      */
  120.     public RinexLoader(final InputStream input, final String name) {
  121.         try {
  122.             observationDataSets = new ArrayList<>();
  123.             new Parser().loadData(input, name);
  124.         } catch (IOException ioe) {
  125.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  126.         }
  127.     }

  128.     /** Get parsed rinex observations.
  129.      * @return unmodifiable view of parsed rinex observations
  130.      * @deprecated as of 9.3 replaced by {@link #getObservationDataSets()}
  131.      */
  132.     @Deprecated
  133.     public Map<RinexHeader, List<ObservationDataSet>> getObservations() {
  134.         final Map<RinexHeader, List<ObservationDataSet>> map = new HashMap<>();
  135.         for (final ObservationDataSet dataSet : observationDataSets) {
  136.             List<ObservationDataSet> l = map.get(dataSet.getHeader());
  137.             if (l == null) {
  138.                 // first time we see this header
  139.                 l = new ArrayList<>();
  140.                 map.put(dataSet.getHeader(), l);
  141.             }
  142.             l.add(dataSet);
  143.         }
  144.         return map;
  145.     }

  146.     /** Get parsed rinex observations data sets.
  147.      * @return unmodifiable view of parsed rinex observations
  148.      * @since 9.3
  149.      */
  150.     public List<ObservationDataSet> getObservationDataSets() {
  151.         return Collections.unmodifiableList(observationDataSets);
  152.     }

  153.     /** Parser for rinex files.
  154.      */
  155.     public class Parser implements DataLoader {

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

  158.         /** File type accepted (only Observation Data). */
  159.         private static final String FILE_TYPE = "O"; //Only Observation Data files

  160.         /** {@inheritDoc} */
  161.         @Override
  162.         public boolean stillAcceptsData() {
  163.             // we load all rinex files we can find
  164.             return true;
  165.         }

  166.         /** {@inheritDoc} */
  167.         @Override
  168.         public void loadData(final InputStream input, final String name)
  169.             throws IOException, OrekitException {

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

  171.                 // placeholders for parsed data
  172.                 SatelliteSystem                  satelliteSystem        = null;
  173.                 double                           formatVersion          = Double.NaN;
  174.                 boolean                          inRinexVersion         = false;
  175.                 int                              lineNumber             = 0;
  176.                 SatelliteSystem                  obsTypesSystem         = null;
  177.                 String                           markerName             = null;
  178.                 String                           markerNumber           = null;
  179.                 String                           markerType             = null;
  180.                 String                           observerName           = null;
  181.                 String                           agencyName             = null;
  182.                 String                           receiverNumber         = null;
  183.                 String                           receiverType           = null;
  184.                 String                           receiverVersion        = null;
  185.                 String                           antennaNumber          = null;
  186.                 String                           antennaType            = null;
  187.                 Vector3D                         approxPos              = null;
  188.                 Vector3D                         antRefPoint            = null;
  189.                 String                           obsCode                = null;
  190.                 Vector3D                         antPhaseCenter         = null;
  191.                 Vector3D                         antBSight              = null;
  192.                 double                           antAzi                 = Double.NaN;
  193.                 Vector3D                         antZeroDir             = null;
  194.                 Vector3D                         centerMass             = null;
  195.                 double                           antHeight              = Double.NaN;
  196.                 Vector2D                         eccentricities         = Vector2D.ZERO;
  197.                 int                              clkOffset              = -1;
  198.                 int                              nbTypes                = -1;
  199.                 int                              nbSat                  = -1;
  200.                 double                           interval               = Double.NaN;
  201.                 AbsoluteDate                     tFirstObs              = AbsoluteDate.PAST_INFINITY;
  202.                 AbsoluteDate                     tLastObs               = AbsoluteDate.FUTURE_INFINITY;
  203.                 TimeScale                        timeScale              = null;
  204.                 String                           timeScaleStr           = null;
  205.                 int                              leapSeconds            = 0;
  206.                 AbsoluteDate                     tObs                   = AbsoluteDate.PAST_INFINITY;
  207.                 String[]                         satsObsList            = null;
  208.                 String                           strYear                = null;
  209.                 int                              eventFlag              = -1;
  210.                 int                              nbSatObs               = -1;
  211.                 int                              nbLinesSat             = -1;
  212.                 double                           rcvrClkOffset          = 0;
  213.                 boolean                          inRunBy                = false;
  214.                 boolean                          inMarkerName           = false;
  215.                 boolean                          inMarkerType           = false;
  216.                 boolean                          inObserver             = false;
  217.                 boolean                          inRecType              = false;
  218.                 boolean                          inAntType              = false;
  219.                 boolean                          inAproxPos             = false;
  220.                 boolean                          inAntDelta             = false;
  221.                 boolean                          inTypesObs             = false;
  222.                 boolean                          inFirstObs             = false;
  223.                 boolean                          inPhaseShift           = false;
  224.                 boolean                          inGlonassSlot          = false;
  225.                 boolean                          inGlonassCOD           = false;
  226.                 RinexHeader                      rinexHeader            = null;
  227.                 int                               scaleFactor            = 1;
  228.                 int                               nbObsScaleFactor       = 0;
  229.                 final List<ScaleFactorCorrection> scaleFactorCorrections = new ArrayList<>();
  230.                 final Map<SatelliteSystem, List<ObservationType>> listTypeObs = new HashMap<>();

  231.                 //First line must  always contain Rinex Version, File Type and Satellite Systems Observed
  232.                 String line = reader.readLine();
  233.                 lineNumber++;
  234.                 formatVersion = parseDouble(line, 0, 9);
  235.                 int format100 = (int) FastMath.rint(100 * formatVersion);

  236.                 if ((format100 != 200) && (format100 != 210) && (format100 != 211) &&
  237.                     (format100 != 212) && (format100 != 220) && (format100 != 300) &&
  238.                     (format100 != 301) && (format100 != 302) && (format100 != 303)) {
  239.                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  240.                 }

  241.                 //File Type must be Observation_Data
  242.                 if (!(parseString(line, 20, 1)).equals(FILE_TYPE)) {
  243.                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  244.                 }
  245.                 satelliteSystem = SatelliteSystem.parseSatelliteSystem(parseString(line, 40, 1));
  246.                 inRinexVersion = true;

  247.                 switch (format100 / 100) {
  248.                     case 2: {

  249.                         final int                   MAX_OBS_TYPES_PER_LINE_RNX2 = 9;
  250.                         final int                   MAX_N_SAT_OBSERVATION       = 12;
  251.                         final int                   MAX_N_TYPES_OBSERVATION     = 5;
  252.                         final int                   MAX_OBS_TYPES_SCALE_FACTOR  = 8;
  253.                         final List<ObservationType> typesObs = new ArrayList<>();

  254.                         for (line = reader.readLine(); line != null; line = reader.readLine()) {
  255.                             ++lineNumber;

  256.                             if (rinexHeader == null) {
  257.                                 switch(line.substring(LABEL_START).trim()) {
  258.                                     case RINEX_VERSION_TYPE :

  259.                                         formatVersion = parseDouble(line, 0, 9);
  260.                                         //File Type must be Observation_Data
  261.                                         if (!(parseString(line, 20, 1)).equals(FILE_TYPE)) {
  262.                                             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  263.                                         }
  264.                                         satelliteSystem = SatelliteSystem.parseSatelliteSystem(parseString(line, 40, 1));
  265.                                         inRinexVersion = true;
  266.                                         break;
  267.                                     case COMMENT :
  268.                                         // nothing to do
  269.                                         break;
  270.                                     case PGM_RUN_BY_DATE :
  271.                                         inRunBy = true;
  272.                                         break;
  273.                                     case MARKER_NAME :
  274.                                         markerName = parseString(line, 0, 60);
  275.                                         inMarkerName = true;
  276.                                         break;
  277.                                     case MARKER_NUMBER :
  278.                                         markerNumber = parseString(line, 0, 20);
  279.                                         break;
  280.                                     case MARKER_TYPE :
  281.                                         markerType = parseString(line, 0, 20);
  282.                                         break;
  283.                                     case OBSERVER_AGENCY :
  284.                                         observerName = parseString(line, 0, 20);
  285.                                         agencyName   = parseString(line, 20, 40);
  286.                                         inObserver = true;
  287.                                         break;
  288.                                     case REC_NB_TYPE_VERS :
  289.                                         receiverNumber  = parseString(line, 0, 20);
  290.                                         receiverType    = parseString(line, 20, 20);
  291.                                         receiverVersion = parseString(line, 40, 20);
  292.                                         inRecType = true;
  293.                                         break;
  294.                                     case ANT_NB_TYPE :
  295.                                         antennaNumber = parseString(line, 0, 20);
  296.                                         antennaType   = parseString(line, 20, 20);
  297.                                         inAntType = true;
  298.                                         break;
  299.                                     case APPROX_POSITION_XYZ :
  300.                                         approxPos = new Vector3D(parseDouble(line, 0, 14), parseDouble(line, 14, 14),
  301.                                                                  parseDouble(line, 28, 14));
  302.                                         inAproxPos = true;
  303.                                         break;
  304.                                     case ANTENNA_DELTA_H_E_N :
  305.                                         antHeight = parseDouble(line, 0, 14);
  306.                                         eccentricities = new Vector2D(parseDouble(line, 14, 14), parseDouble(line, 28, 14));
  307.                                         inAntDelta = true;
  308.                                         break;
  309.                                     case ANTENNA_DELTA_X_Y_Z :
  310.                                         antRefPoint = new Vector3D(parseDouble(line, 0, 14),
  311.                                                                    parseDouble(line, 14, 14),
  312.                                                                    parseDouble(line, 28, 14));
  313.                                         break;
  314.                                     case ANTENNA_B_SIGHT_XYZ :
  315.                                         antBSight = new Vector3D(parseDouble(line, 0, 14),
  316.                                                                  parseDouble(line, 14, 14),
  317.                                                                  parseDouble(line, 28, 14));
  318.                                         break;
  319.                                     case CENTER_OF_MASS_XYZ :
  320.                                         centerMass = new Vector3D(parseDouble(line, 0, 14),
  321.                                                                   parseDouble(line, 14, 14),
  322.                                                                   parseDouble(line, 28, 14));
  323.                                         break;
  324.                                     case NB_OF_SATELLITES :
  325.                                         nbSat = parseInt(line, 0, 6);
  326.                                         break;
  327.                                     case WAVELENGTH_FACT_L1_2 :
  328.                                         //Optional line in header
  329.                                         //Not stored for now
  330.                                         break;
  331.                                     case RCV_CLOCK_OFFS_APPL :
  332.                                         clkOffset = parseInt(line, 0, 6);
  333.                                         break;
  334.                                     case INTERVAL :
  335.                                         interval = parseDouble(line, 0, 10);
  336.                                         break;
  337.                                     case TIME_OF_FIRST_OBS :
  338.                                         switch (satelliteSystem) {
  339.                                             case GPS:
  340.                                                 timeScale = TimeScalesFactory.getGPS();
  341.                                                 break;
  342.                                             case GALILEO:
  343.                                                 timeScale = TimeScalesFactory.getGST();
  344.                                                 break;
  345.                                             case GLONASS:
  346.                                                 timeScale = TimeScalesFactory.getGLONASS();
  347.                                                 break;
  348.                                             case MIXED:
  349.                                                 //in Case of Mixed data, Timescale must be specified in the Time of First line
  350.                                                 timeScaleStr = parseString(line, 48, 3);

  351.                                                 if (timeScaleStr.equals(GPS)) {
  352.                                                     timeScale = TimeScalesFactory.getGPS();
  353.                                                 } else if (timeScaleStr.equals(GAL)) {
  354.                                                     timeScale = TimeScalesFactory.getGST();
  355.                                                 } else if (timeScaleStr.equals(GLO)) {
  356.                                                     timeScale = TimeScalesFactory.getGLONASS();
  357.                                                 } else {
  358.                                                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  359.                                                 }
  360.                                                 break;
  361.                                             default :
  362.                                                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  363.                                                                           lineNumber, name, line);
  364.                                         }

  365.                                         tFirstObs = new AbsoluteDate(parseInt(line, 0, 6),
  366.                                                                      parseInt(line, 6, 6),
  367.                                                                      parseInt(line, 12, 6),
  368.                                                                      parseInt(line, 18, 6),
  369.                                                                      parseInt(line, 24, 6),
  370.                                                                      parseDouble(line, 30, 13), timeScale);
  371.                                         inFirstObs = true;
  372.                                         break;
  373.                                     case TIME_OF_LAST_OBS :
  374.                                         tLastObs = new AbsoluteDate(parseInt(line, 0, 6),
  375.                                                                     parseInt(line, 6, 6),
  376.                                                                     parseInt(line, 12, 6),
  377.                                                                     parseInt(line, 18, 6),
  378.                                                                     parseInt(line, 24, 6),
  379.                                                                     parseDouble(line, 30, 13), timeScale);
  380.                                         break;
  381.                                     case LEAP_SECONDS :
  382.                                         leapSeconds = parseInt(line, 0, 6);
  383.                                         break;
  384.                                     case PRN_NB_OF_OBS :
  385.                                         //Optional line in header, indicates number of Observations par Satellite
  386.                                         //Not stored for now
  387.                                         break;
  388.                                     case NB_TYPES_OF_OBSERV :
  389.                                         nbTypes = parseInt(line, 0, 6);
  390.                                         final int nbLinesTypesObs = (nbTypes + MAX_OBS_TYPES_PER_LINE_RNX2 - 1 ) / MAX_OBS_TYPES_PER_LINE_RNX2;

  391.                                         for (int j = 0; j < nbLinesTypesObs; j++) {
  392.                                             if (j > 0) {
  393.                                                 line = reader.readLine(); //Next line
  394.                                                 lineNumber++;
  395.                                             }
  396.                                             final int iMax = FastMath.min(MAX_OBS_TYPES_PER_LINE_RNX2, nbTypes - typesObs.size());
  397.                                             for (int i = 0; i < iMax; i++) {
  398.                                                 try {
  399.                                                     typesObs.add(ObservationType.valueOf(parseString(line, 10 + (6 * i), 2)));
  400.                                                 } catch (IllegalArgumentException iae) {
  401.                                                     throw new OrekitException(OrekitMessages.UNKNOWN_RINEX_FREQUENCY,
  402.                                                                               parseString(line, 10 + (6 * i), 2), name, lineNumber);
  403.                                                 }
  404.                                             }
  405.                                         }
  406.                                         inTypesObs = true;
  407.                                         break;
  408.                                     case OBS_SCALE_FACTOR :
  409.                                         scaleFactor      = FastMath.max(1, parseInt(line, 0,  6));
  410.                                         nbObsScaleFactor = parseInt(line, 6, 6);
  411.                                         if (nbObsScaleFactor > MAX_OBS_TYPES_SCALE_FACTOR) {
  412.                                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  413.                                                                       lineNumber, name, line);
  414.                                         }
  415.                                         final List<ObservationType> typesObsScaleFactor = new ArrayList<>(nbObsScaleFactor);
  416.                                         for (int i = 0; i < nbObsScaleFactor; i++) {
  417.                                             typesObsScaleFactor.add(ObservationType.valueOf(parseString(line, 16 + (6 * i), 2)));
  418.                                         }
  419.                                         scaleFactorCorrections.add(new ScaleFactorCorrection(satelliteSystem,
  420.                                                                                              scaleFactor, typesObsScaleFactor));
  421.                                         break;
  422.                                     case END_OF_HEADER :
  423.                                         //We make sure that we have read all the mandatory fields inside the header of the Rinex
  424.                                         if (!inRinexVersion || !inRunBy || !inMarkerName ||
  425.                                             !inObserver || !inRecType || !inAntType ||
  426.                                             (formatVersion < 2.20 && !inAproxPos) ||
  427.                                             (formatVersion < 2.20 && !inAntDelta) ||
  428.                                             !inTypesObs || !inFirstObs) {
  429.                                             throw new OrekitException(OrekitMessages.INCOMPLETE_HEADER, name);
  430.                                         }

  431.                                         //Header information gathered
  432.                                         rinexHeader = new RinexHeader(formatVersion, satelliteSystem,
  433.                                                                       markerName, markerNumber, markerType, observerName,
  434.                                                                       agencyName, receiverNumber, receiverType,
  435.                                                                       receiverVersion, antennaNumber, antennaType,
  436.                                                                       approxPos, antHeight, eccentricities,
  437.                                                                       antRefPoint, antBSight, centerMass, interval,
  438.                                                                       tFirstObs, tLastObs, clkOffset, leapSeconds);
  439.                                         break;
  440.                                     default :
  441.                                         if (rinexHeader == null) {
  442.                                             //There must be an error due to an unknown Label inside the Header
  443.                                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  444.                                                                       lineNumber, name, line);
  445.                                         }
  446.                                 }
  447.                             } else {

  448.                                 //Start of a new Observation
  449.                                 rcvrClkOffset     =  0;
  450.                                 nbLinesSat        = -1;
  451.                                 eventFlag         = -1;
  452.                                 nbSatObs          = -1;
  453.                                 satsObsList       = null;
  454.                                 tObs              = null;
  455.                                 strYear           = null;

  456.                                 eventFlag = parseInt(line, 28, 1);
  457.                                 //If eventFlag>1, we skip the corresponding lines to the next observation
  458.                                 if (eventFlag != 0) {
  459.                                     if (eventFlag == 6) {
  460.                                         nbSatObs  = parseInt(line, 29, 3);
  461.                                         nbLinesSat = (nbSatObs + 12 - 1) / 12;
  462.                                         final int nbLinesObs = (nbTypes + 5 - 1) / 5;
  463.                                         final int nbLinesSkip = (nbLinesSat - 1) + nbSatObs * nbLinesObs;
  464.                                         for (int i = 0; i < nbLinesSkip; i++) {
  465.                                             line = reader.readLine(); //Next line
  466.                                             lineNumber++;
  467.                                         }
  468.                                     } else {
  469.                                         final int nbLinesSkip = parseInt(line, 29, 3);
  470.                                         for (int i = 0; i < nbLinesSkip; i++) {
  471.                                             line = reader.readLine(); //Next line
  472.                                             lineNumber++;
  473.                                         }
  474.                                     }
  475.                                 } else {

  476.                                     final int y = Integer.parseInt(parseString(line, 0, 3));
  477.                                     if (79 < y && y <= 99) {
  478.                                         strYear = "19" + y;
  479.                                     } else if (0 <= y && y <= 79) {
  480.                                         strYear = "20" + parseString(line, 0, 3);
  481.                                     }
  482.                                     tObs = new AbsoluteDate(Integer.parseInt(strYear),
  483.                                                             parseInt(line, 3, 3),
  484.                                                             parseInt(line, 6, 3),
  485.                                                             parseInt(line, 9, 3),
  486.                                                             parseInt(line, 12, 3),
  487.                                                             parseDouble(line, 15, 11), timeScale);

  488.                                     nbSatObs  = parseInt(line, 29, 3);
  489.                                     satsObsList   = new String[nbSatObs];
  490.                                     //If the total number of satellites was indicated in the Header
  491.                                     if (nbSat != -1 && nbSatObs > nbSat) {
  492.                                         //we check that the number of Sat in the observation is consistent
  493.                                         throw new OrekitException(OrekitMessages.INCONSISTENT_NUMBER_OF_SATS,
  494.                                                                   lineNumber, name, nbSatObs, nbSat);
  495.                                     }

  496.                                     nbLinesSat = (nbSatObs + MAX_N_SAT_OBSERVATION - 1) / MAX_N_SAT_OBSERVATION;
  497.                                     for (int j = 0; j < nbLinesSat; j++) {
  498.                                         if (j > 0) {
  499.                                             line = reader.readLine(); //Next line
  500.                                             lineNumber++;
  501.                                         }
  502.                                         final int iMax = FastMath.min(MAX_N_SAT_OBSERVATION, nbSatObs  - j * MAX_N_SAT_OBSERVATION);
  503.                                         for (int i = 0; i < iMax; i++) {
  504.                                             satsObsList[i + MAX_N_SAT_OBSERVATION * j] = parseString(line, 32 + 3 * i, 3);
  505.                                         }

  506.                                         //Read the Receiver Clock offset, if present
  507.                                         rcvrClkOffset = parseDouble(line, 68, 12);
  508.                                         if (Double.isNaN(rcvrClkOffset)) {
  509.                                             rcvrClkOffset = 0.0;
  510.                                         }

  511.                                     }

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


  515.                                         //Once the Date and Satellites list is read:
  516.                                         //  - to read the Data for each satellite
  517.                                         //  - 5 Observations per line
  518.                                         final List<ObservationData> observationData = new ArrayList<>(nbSatObs);
  519.                                         for (int j = 0; j < nbLinesObs; j++) {
  520.                                             line = reader.readLine(); //Next line
  521.                                             lineNumber++;
  522.                                             final int iMax = FastMath.min(MAX_N_TYPES_OBSERVATION, nbTypes - observationData.size());
  523.                                             for (int i = 0; i < iMax; i++) {
  524.                                                 final ObservationType type = typesObs.get(observationData.size());
  525.                                                 double value = parseDouble(line, 16 * i, 14);
  526.                                                 boolean scaleFactorFound = false;
  527.                                                 //We look for the lines of ScaledFactorCorrections
  528.                                                 for (int l = 0; l < scaleFactorCorrections.size() && !scaleFactorFound; ++l) {
  529.                                                     //We check if the next Observation Type to read needs to be scaled
  530.                                                     if (scaleFactorCorrections.get(l).getTypesObsScaled().contains(type)) {
  531.                                                         value /= scaleFactorCorrections.get(l).getCorrection();
  532.                                                         scaleFactorFound = true;
  533.                                                     }
  534.                                                 }
  535.                                                 observationData.add(new ObservationData(type,
  536.                                                                                         value,
  537.                                                                                         parseInt(line, 14 + 16 * i, 1),
  538.                                                                                         parseInt(line, 15 + 16 * i, 1)));
  539.                                             }
  540.                                         }

  541.                                         //We check that the Satellite type is consistent with Satellite System in the top of the file
  542.                                         final SatelliteSystem satelliteSystemSat;
  543.                                         final int id;
  544.                                         if (satsObsList[k].length() < 3) {
  545.                                             // missing satellite system, we use the global one
  546.                                             satelliteSystemSat = satelliteSystem;
  547.                                             id                 = Integer.parseInt(satsObsList[k]);
  548.                                         } else {
  549.                                             satelliteSystemSat = SatelliteSystem.parseSatelliteSystem(satsObsList[k]);
  550.                                             id                 = Integer.parseInt(satsObsList[k].substring(1, 3).trim());
  551.                                         }
  552.                                         if (!satelliteSystem.equals(SatelliteSystem.MIXED)) {
  553.                                             if (!satelliteSystemSat.equals(satelliteSystem)) {
  554.                                                 throw new OrekitException(OrekitMessages.INCONSISTENT_SATELLITE_SYSTEM,
  555.                                                                           lineNumber, name, satelliteSystem, satelliteSystemSat);
  556.                                             }
  557.                                         }

  558.                                         final int prnNumber;
  559.                                         switch (satelliteSystemSat) {
  560.                                             case GPS:
  561.                                             case GLONASS:
  562.                                             case GALILEO:
  563.                                                 prnNumber = id;
  564.                                                 break;
  565.                                             case SBAS:
  566.                                                 prnNumber = id + 100;
  567.                                                 break;
  568.                                             default:
  569.                                                 // MIXED satellite system is not allowed here
  570.                                                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  571.                                                                           lineNumber, name, line);
  572.                                         }

  573.                                         observationDataSets.add(new ObservationDataSet(rinexHeader, satelliteSystemSat, prnNumber,
  574.                                                                                        tObs, rcvrClkOffset, observationData));

  575.                                     }
  576.                                 }
  577.                             }
  578.                         }
  579.                         break;
  580.                     }
  581.                     case 3: {

  582.                         final int                   MAX_OBS_TYPES_PER_LINE_RNX3 = 13;
  583.                         final int           MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE = 12;
  584.                         final int                    MAX_N_SAT_PHSHIFT_PER_LINE = 10;

  585.                         final List<ObservationType>                       typeObs                = new ArrayList<>();
  586.                         String                                            sigStrengthUnit        = null;
  587.                         int                                               leapSecondsFuture      = 0;
  588.                         int                                               leapSecondsWeekNum     = 0;
  589.                         int                                               leapSecondsDayNum      = 0;
  590.                         final List<AppliedDCBS>                           listAppliedDCBs        = new ArrayList<>();
  591.                         final List<AppliedPCVS>                           listAppliedPCVS        = new ArrayList<>();
  592.                         SatelliteSystem                                   satSystemScaleFactor   = null;
  593.                         String[]                                          satsPhaseShift         = null;
  594.                         int                                               nbSatPhaseShift        = 0;
  595.                         SatelliteSystem                                   satSystemPhaseShift    = null;
  596.                         double                                            corrPhaseShift         = 0.0;
  597.                         final List<PhaseShiftCorrection>                  phaseShiftCorrections  = new ArrayList<>();
  598.                         ObservationType                                   phaseShiftTypeObs      = null;


  599.                         for (line = reader.readLine(); line != null; line = reader.readLine()) {
  600.                             ++lineNumber;
  601.                             if (rinexHeader == null) {
  602.                                 switch(line.substring(LABEL_START).trim()) {
  603.                                     case RINEX_VERSION_TYPE : {
  604.                                         formatVersion = parseDouble(line, 0, 9);
  605.                                         format100     = (int) FastMath.rint(100 * formatVersion);
  606.                                         if ((format100 != 300) && (format100 != 301) && (format100 != 302) && (format100 != 303)) {
  607.                                             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  608.                                         }
  609.                                         //File Type must be Observation_Data
  610.                                         if (!(parseString(line, 20, 1)).equals(FILE_TYPE)) {
  611.                                             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  612.                                         }
  613.                                         satelliteSystem = SatelliteSystem.parseSatelliteSystem(parseString(line, 40, 1));
  614.                                         inRinexVersion = true;
  615.                                     }
  616.                                         break;
  617.                                     case COMMENT :
  618.                                         // nothing to do
  619.                                         break;
  620.                                     case PGM_RUN_BY_DATE :
  621.                                         inRunBy = true;
  622.                                         break;
  623.                                     case MARKER_NAME :
  624.                                         markerName = parseString(line, 0, 60);
  625.                                         inMarkerName = true;
  626.                                         break;
  627.                                     case MARKER_NUMBER :
  628.                                         markerNumber = parseString(line, 0, 20);
  629.                                         break;
  630.                                     case MARKER_TYPE :
  631.                                         markerType = parseString(line, 0, 20);
  632.                                         inMarkerType = true;
  633.                                         //Could be done with an Enumeration
  634.                                         break;
  635.                                     case OBSERVER_AGENCY :
  636.                                         observerName = parseString(line, 0, 20);
  637.                                         agencyName   = parseString(line, 20, 40);
  638.                                         inObserver = true;
  639.                                         break;
  640.                                     case REC_NB_TYPE_VERS :
  641.                                         receiverNumber  = parseString(line, 0, 20);
  642.                                         receiverType    = parseString(line, 20, 20);
  643.                                         receiverVersion = parseString(line, 40, 20);
  644.                                         inRecType = true;
  645.                                         break;
  646.                                     case ANT_NB_TYPE :
  647.                                         antennaNumber = parseString(line, 0, 20);
  648.                                         antennaType   = parseString(line, 20, 20);
  649.                                         inAntType = true;
  650.                                         break;
  651.                                     case APPROX_POSITION_XYZ :
  652.                                         approxPos = new Vector3D(parseDouble(line, 0, 14),
  653.                                                                  parseDouble(line, 14, 14),
  654.                                                                  parseDouble(line, 28, 14));
  655.                                         inAproxPos = true;
  656.                                         break;
  657.                                     case ANTENNA_DELTA_H_E_N :
  658.                                         antHeight = parseDouble(line, 0, 14);
  659.                                         eccentricities = new Vector2D(parseDouble(line, 14, 14),
  660.                                                                       parseDouble(line, 28, 14));
  661.                                         inAntDelta = true;
  662.                                         break;
  663.                                     case ANTENNA_DELTA_X_Y_Z :
  664.                                         antRefPoint = new Vector3D(parseDouble(line, 0, 14),
  665.                                                                    parseDouble(line, 14, 14),
  666.                                                                    parseDouble(line, 28, 14));
  667.                                         break;
  668.                                     case ANTENNA_PHASECENTER :
  669.                                         obsCode = parseString(line, 2, 3);
  670.                                         antPhaseCenter = new Vector3D(parseDouble(line, 5, 9),
  671.                                                                       parseDouble(line, 14, 14),
  672.                                                                       parseDouble(line, 28, 14));
  673.                                         break;
  674.                                     case ANTENNA_B_SIGHT_XYZ :
  675.                                         antBSight = new Vector3D(parseDouble(line, 0, 14),
  676.                                                                  parseDouble(line, 14, 14),
  677.                                                                  parseDouble(line, 28, 14));
  678.                                         break;
  679.                                     case ANTENNA_ZERODIR_AZI :
  680.                                         antAzi = parseDouble(line, 0, 14);
  681.                                         break;
  682.                                     case ANTENNA_ZERODIR_XYZ :
  683.                                         antZeroDir = new Vector3D(parseDouble(line, 0, 14),
  684.                                                                   parseDouble(line, 14, 14),
  685.                                                                   parseDouble(line, 28, 14));
  686.                                         break;
  687.                                     case CENTER_OF_MASS_XYZ :
  688.                                         centerMass = new Vector3D(parseDouble(line, 0, 14),
  689.                                                                   parseDouble(line, 14, 14),
  690.                                                                   parseDouble(line, 28, 14));
  691.                                         break;
  692.                                     case NB_OF_SATELLITES :
  693.                                         nbSat = parseInt(line, 0, 6);
  694.                                         break;
  695.                                     case RCV_CLOCK_OFFS_APPL :
  696.                                         clkOffset = parseInt(line, 0, 6);
  697.                                         break;
  698.                                     case INTERVAL :
  699.                                         interval = parseDouble(line, 0, 10);
  700.                                         break;
  701.                                     case TIME_OF_FIRST_OBS :
  702.                                         switch(satelliteSystem) {
  703.                                             case GPS:
  704.                                                 timeScale = TimeScalesFactory.getGPS();
  705.                                                 break;
  706.                                             case GALILEO:
  707.                                                 timeScale = TimeScalesFactory.getGST();
  708.                                                 break;
  709.                                             case GLONASS:
  710.                                                 timeScale = TimeScalesFactory.getGLONASS();
  711.                                                 break;
  712.                                             case QZSS:
  713.                                                 timeScale = TimeScalesFactory.getQZSS();
  714.                                                 break;
  715.                                             case BEIDOU:
  716.                                                 timeScale = TimeScalesFactory.getBDT();
  717.                                                 break;
  718.                                             case IRNSS:
  719.                                                 timeScale = TimeScalesFactory.getIRNSS();
  720.                                                 break;
  721.                                             case MIXED:
  722.                                                 //in Case of Mixed data, Timescale must be specified in the Time of First line
  723.                                                 timeScaleStr = parseString(line, 48, 3);

  724.                                                 if (timeScaleStr.equals(GPS)) {
  725.                                                     timeScale = TimeScalesFactory.getGPS();
  726.                                                 } else if (timeScaleStr.equals(GAL)) {
  727.                                                     timeScale = TimeScalesFactory.getGST();
  728.                                                 } else if (timeScaleStr.equals(GLO)) {
  729.                                                     timeScale = TimeScalesFactory.getGLONASS();
  730.                                                 } else if (timeScaleStr.equals(QZS)) {
  731.                                                     timeScale = TimeScalesFactory.getQZSS();
  732.                                                 } else if (timeScaleStr.equals(BDT)) {
  733.                                                     timeScale = TimeScalesFactory.getBDT();
  734.                                                 } else if (timeScaleStr.equals(IRN)) {
  735.                                                     timeScale = TimeScalesFactory.getIRNSS();
  736.                                                 } else {
  737.                                                     throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  738.                                                 }
  739.                                                 break;
  740.                                             default :
  741.                                                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  742.                                                                           lineNumber, name, line);
  743.                                         }

  744.                                         tFirstObs = new AbsoluteDate(parseInt(line, 0, 6),
  745.                                                                      parseInt(line, 6, 6),
  746.                                                                      parseInt(line, 12, 6),
  747.                                                                      parseInt(line, 18, 6),
  748.                                                                      parseInt(line, 24, 6),
  749.                                                                      parseDouble(line, 30, 13), timeScale);
  750.                                         inFirstObs = true;
  751.                                         break;
  752.                                     case TIME_OF_LAST_OBS :
  753.                                         tLastObs = new AbsoluteDate(parseInt(line, 0, 6),
  754.                                                                     parseInt(line, 6, 6),
  755.                                                                     parseInt(line, 12, 6),
  756.                                                                     parseInt(line, 18, 6),
  757.                                                                     parseInt(line, 24, 6),
  758.                                                                     parseDouble(line, 30, 13), timeScale);
  759.                                         break;
  760.                                     case LEAP_SECONDS :
  761.                                         leapSeconds = parseInt(line, 0, 6);
  762.                                         leapSecondsFuture = parseInt(line, 6, 6);
  763.                                         leapSecondsWeekNum = parseInt(line, 12, 6);
  764.                                         leapSecondsDayNum = parseInt(line, 18, 6);
  765.                                         //Time System Identifier must be added, last A3 String
  766.                                         break;
  767.                                     case PRN_NB_OF_OBS :
  768.                                         //Optional line in header, indicates number of Observations par Satellite
  769.                                         //Not stored for now
  770.                                         break;
  771.                                     case SYS_NB_OBS_TYPES :
  772.                                         obsTypesSystem = null;
  773.                                         typeObs.clear();

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

  776.                                         final int nbLinesTypesObs = (nbTypes + MAX_OBS_TYPES_PER_LINE_RNX3 - 1) / MAX_OBS_TYPES_PER_LINE_RNX3;
  777.                                         for (int j = 0; j < nbLinesTypesObs; j++) {
  778.                                             if (j > 0) {
  779.                                                 line = reader.readLine(); //Next line
  780.                                                 lineNumber++;
  781.                                             }
  782.                                             final int iMax = FastMath.min(MAX_OBS_TYPES_PER_LINE_RNX3, nbTypes - typeObs.size());
  783.                                             for (int i = 0; i < iMax; i++) {
  784.                                                 try {
  785.                                                     typeObs.add(ObservationType.valueOf(parseString(line, 7 + (4 * i), 3)));
  786.                                                 } catch (IllegalArgumentException iae) {
  787.                                                     throw new OrekitException(OrekitMessages.UNKNOWN_RINEX_FREQUENCY,
  788.                                                                               parseString(line, 7 + (4 * i), 3), name, lineNumber);
  789.                                                 }
  790.                                             }
  791.                                         }
  792.                                         listTypeObs.put(obsTypesSystem, new ArrayList<>(typeObs));
  793.                                         inTypesObs = true;
  794.                                         break;
  795.                                     case SIGNAL_STRENGTH_UNIT :
  796.                                         sigStrengthUnit = parseString(line, 0, 20);
  797.                                         break;
  798.                                     case SYS_DCBS_APPLIED :

  799.                                         listAppliedDCBs.add(new AppliedDCBS(SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1)),
  800.                                                                             parseString(line, 2, 17), parseString(line, 20, 40)));
  801.                                         break;
  802.                                     case SYS_PCVS_APPLIED :

  803.                                         listAppliedPCVS.add(new AppliedPCVS(SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1)),
  804.                                                                             parseString(line, 2, 17), parseString(line, 20, 40)));
  805.                                         break;
  806.                                     case SYS_SCALE_FACTOR :
  807.                                         satSystemScaleFactor  = null;
  808.                                         scaleFactor           = 1;
  809.                                         nbObsScaleFactor      = 0;

  810.                                         satSystemScaleFactor = SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1));
  811.                                         scaleFactor          = parseInt(line, 2, 4);
  812.                                         nbObsScaleFactor     = parseInt(line, 8, 2);
  813.                                         final List<ObservationType> typesObsScaleFactor = new ArrayList<>(nbObsScaleFactor);

  814.                                         if (nbObsScaleFactor == 0) {
  815.                                             typesObsScaleFactor.addAll(listTypeObs.get(satSystemScaleFactor));
  816.                                         } else {
  817.                                             final int nbLinesTypesObsScaleFactor = (nbObsScaleFactor + MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE - 1) /
  818.                                                                                    MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE;
  819.                                             for (int j = 0; j < nbLinesTypesObsScaleFactor; j++) {
  820.                                                 if ( j > 0) {
  821.                                                     line = reader.readLine(); //Next line
  822.                                                     lineNumber++;
  823.                                                 }
  824.                                                 final int iMax = FastMath.min(MAX_OBS_TYPES_SCALE_FACTOR_PER_LINE, nbObsScaleFactor - typesObsScaleFactor.size());
  825.                                                 for (int i = 0; i < iMax; i++) {
  826.                                                     typesObsScaleFactor.add(ObservationType.valueOf(parseString(line, 11 + (4 * i), 3)));
  827.                                                 }
  828.                                             }
  829.                                         }

  830.                                         scaleFactorCorrections.add(new ScaleFactorCorrection(satSystemScaleFactor,
  831.                                                                                              scaleFactor, typesObsScaleFactor));
  832.                                         break;
  833.                                     case SYS_PHASE_SHIFT :

  834.                                         nbSatPhaseShift     = 0;
  835.                                         satsPhaseShift      = null;
  836.                                         corrPhaseShift      = 0.0;
  837.                                         phaseShiftTypeObs   = null;
  838.                                         satSystemPhaseShift = null;

  839.                                         satSystemPhaseShift = SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1));
  840.                                         phaseShiftTypeObs = ObservationType.valueOf(parseString(line, 2, 3));
  841.                                         nbSatPhaseShift = parseInt(line, 16, 2);
  842.                                         corrPhaseShift = parseDouble(line, 6, 8);

  843.                                         if (nbSatPhaseShift == 0) {
  844.                                             //If nbSat with Phase Shift is not indicated: all the satellites are affected for this Obs Type
  845.                                         } else {
  846.                                             satsPhaseShift = new String[nbSatPhaseShift];
  847.                                             final int nbLinesSatPhaseShift = (nbSatPhaseShift + MAX_N_SAT_PHSHIFT_PER_LINE - 1) / MAX_N_SAT_PHSHIFT_PER_LINE;
  848.                                             for (int j = 0; j < nbLinesSatPhaseShift; j++) {
  849.                                                 if (j > 0) {
  850.                                                     line = reader.readLine(); //Next line
  851.                                                     lineNumber++;
  852.                                                 }
  853.                                                 final int iMax = FastMath.min(MAX_N_SAT_PHSHIFT_PER_LINE, nbSatPhaseShift - j * MAX_N_SAT_PHSHIFT_PER_LINE);
  854.                                                 for (int i = 0; i < iMax; i++) {
  855.                                                     satsPhaseShift[i + 10 * j] = parseString(line, 19 + 4 * i, 3);
  856.                                                 }
  857.                                             }
  858.                                         }
  859.                                         phaseShiftCorrections.add(new PhaseShiftCorrection(satSystemPhaseShift,
  860.                                                                                            phaseShiftTypeObs,
  861.                                                                                            corrPhaseShift,
  862.                                                                                            satsPhaseShift));
  863.                                         inPhaseShift = true;
  864.                                         break;
  865.                                     case GLONASS_SLOT_FRQ_NB :
  866.                                         //Not defined yet
  867.                                         inGlonassSlot = true;
  868.                                         break;
  869.                                     case GLONASS_COD_PHS_BIS :
  870.                                         //Not defined yet
  871.                                         inGlonassCOD = true;
  872.                                         break;
  873.                                     case END_OF_HEADER :
  874.                                         //We make sure that we have read all the mandatory fields inside the header of the Rinex
  875.                                         if (!inRinexVersion || !inRunBy || !inMarkerName ||
  876.                                             !inMarkerType || !inObserver || !inRecType || !inAntType ||
  877.                                             !inAproxPos || !inAntDelta || !inTypesObs || !inFirstObs ||
  878.                                             (formatVersion >= 3.01 && !inPhaseShift) ||
  879.                                             (formatVersion >= 3.03 && (!inGlonassSlot || !inGlonassCOD))) {
  880.                                             throw new OrekitException(OrekitMessages.INCOMPLETE_HEADER, name);
  881.                                         }

  882.                                         //Header information gathered
  883.                                         rinexHeader = new RinexHeader(formatVersion, satelliteSystem,
  884.                                                                       markerName, markerNumber, markerType,
  885.                                                                       observerName, agencyName, receiverNumber,
  886.                                                                       receiverType, receiverVersion, antennaNumber,
  887.                                                                       antennaType, approxPos, antHeight, eccentricities,
  888.                                                                       antRefPoint, obsCode, antPhaseCenter, antBSight,
  889.                                                                       antAzi, antZeroDir, centerMass, sigStrengthUnit,
  890.                                                                       interval, tFirstObs, tLastObs, clkOffset, listAppliedDCBs,
  891.                                                                       listAppliedPCVS, phaseShiftCorrections, leapSeconds,
  892.                                                                       leapSecondsFuture, leapSecondsWeekNum, leapSecondsDayNum);
  893.                                         break;
  894.                                     default :
  895.                                         if (rinexHeader == null) {
  896.                                             //There must be an error due to an unknown Label inside the Header
  897.                                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  898.                                                                       lineNumber, name, line);
  899.                                         }
  900.                                 }
  901.                             } else {
  902.                                 //If End of Header

  903.                                 //Start of a new Observation
  904.                                 rcvrClkOffset     =  0;
  905.                                 eventFlag         = -1;
  906.                                 nbSatObs          = -1;
  907.                                 tObs              = null;

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

  910.                                     eventFlag = parseInt(line, 31, 1);
  911.                                     //If eventFlag>1, we skip the corresponding lines to the next observation
  912.                                     if (eventFlag != 0) {
  913.                                         final int nbLinesSkip = parseInt(line, 32, 3);
  914.                                         for (int i = 0; i < nbLinesSkip; i++) {
  915.                                             line = reader.readLine();
  916.                                             lineNumber++;
  917.                                         }
  918.                                     } else {

  919.                                         tObs = new AbsoluteDate(parseInt(line, 2, 4),
  920.                                                                 parseInt(line, 6, 3),
  921.                                                                 parseInt(line, 9, 3),
  922.                                                                 parseInt(line, 12, 3),
  923.                                                                 parseInt(line, 15, 3),
  924.                                                                 parseDouble(line, 18, 11), timeScale);

  925.                                         nbSatObs  = parseInt(line, 32, 3);
  926.                                         //If the total number of satellites was indicated in the Header
  927.                                         if (nbSat != -1 && nbSatObs > nbSat) {
  928.                                             //we check that the number of Sat in the observation is consistent
  929.                                             throw new OrekitException(OrekitMessages.INCONSISTENT_NUMBER_OF_SATS,
  930.                                                                       lineNumber, name, nbSatObs, nbSat);
  931.                                         }
  932.                                         //Read the Receiver Clock offset, if present
  933.                                         rcvrClkOffset = parseDouble(line, 41, 15);
  934.                                         if (Double.isNaN(rcvrClkOffset)) {
  935.                                             rcvrClkOffset = 0.0;
  936.                                         }

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

  939.                                             line = reader.readLine();
  940.                                             lineNumber++;

  941.                                             //We check that the Satellite type is consistent with Satellite System in the top of the file
  942.                                             final SatelliteSystem satelliteSystemSat = SatelliteSystem.parseSatelliteSystem(parseString(line, 0, 1));
  943.                                             if (!satelliteSystem.equals(SatelliteSystem.MIXED)) {
  944.                                                 if (!satelliteSystemSat.equals(satelliteSystem)) {
  945.                                                     throw new OrekitException(OrekitMessages.INCONSISTENT_SATELLITE_SYSTEM,
  946.                                                                               lineNumber, name, satelliteSystem, satelliteSystemSat);
  947.                                                 }
  948.                                             }

  949.                                             final int prn = parseInt(line, 1, 2);
  950.                                             final int prnNumber;
  951.                                             switch (satelliteSystemSat) {
  952.                                                 case GPS:
  953.                                                 case GLONASS:
  954.                                                 case GALILEO:
  955.                                                 case BEIDOU:
  956.                                                 case IRNSS:
  957.                                                     prnNumber = prn;
  958.                                                     break;
  959.                                                 case QZSS:
  960.                                                     prnNumber = prn + 192;
  961.                                                     break;
  962.                                                 case SBAS:
  963.                                                     prnNumber = prn + 100;
  964.                                                     break;
  965.                                                 default:
  966.                                                     // MIXED satellite system is not allowed here
  967.                                                     throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  968.                                                                               lineNumber, name, line);
  969.                                             }
  970.                                             final List<ObservationData> observationData = new ArrayList<>(nbSatObs);
  971.                                             for (int j = 0; j < listTypeObs.get(satelliteSystemSat).size(); j++) {
  972.                                                 final ObservationType rf = listTypeObs.get(satelliteSystemSat).get(j);
  973.                                                 boolean scaleFactorFound = false;
  974.                                                 //We look for the lines of ScaledFactorCorrections that correspond to this SatSystem
  975.                                                 int k = 0;
  976.                                                 double value = parseDouble(line, 3 + j * 16, 14);
  977.                                                 while (k < scaleFactorCorrections.size() && !scaleFactorFound) {
  978.                                                     if (scaleFactorCorrections.get(k).getSatelliteSystem().equals(satelliteSystemSat)) {
  979.                                                         //We check if the next Observation Type to read needs to be scaled
  980.                                                         if (scaleFactorCorrections.get(k).getTypesObsScaled().contains(rf)) {
  981.                                                             value /= scaleFactorCorrections.get(k).getCorrection();
  982.                                                             scaleFactorFound = true;
  983.                                                         }
  984.                                                     }
  985.                                                     k++;
  986.                                                 }
  987.                                                 observationData.add(new ObservationData(rf,
  988.                                                                                         value,
  989.                                                                                         parseInt(line, 17 + j * 16, 1),
  990.                                                                                         parseInt(line, 18 + j * 16, 1)));
  991.                                             }
  992.                                             observationDataSets.add(new ObservationDataSet(rinexHeader, satelliteSystemSat, prnNumber,
  993.                                                                                            tObs, rcvrClkOffset, observationData));

  994.                                         }
  995.                                     }
  996.                                 }
  997.                             }
  998.                         }
  999.                         break;
  1000.                     }
  1001.                     default:
  1002.                         //If RINEX Version is neither 2 nor 3
  1003.                         throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, name);
  1004.                 }
  1005.             }
  1006.         }


  1007.         /** Extract a string from a line.
  1008.          * @param line to parse
  1009.          * @param start start index of the string
  1010.          * @param length length of the string
  1011.          * @return parsed string
  1012.          */
  1013.         private String parseString(final String line, final int start, final int length) {
  1014.             if (line.length() > start) {
  1015.                 return line.substring(start, FastMath.min(line.length(), start + length)).trim();
  1016.             } else {
  1017.                 return null;
  1018.             }
  1019.         }

  1020.         /** Extract an integer from a line.
  1021.          * @param line to parse
  1022.          * @param start start index of the integer
  1023.          * @param length length of the integer
  1024.          * @return parsed integer
  1025.          */
  1026.         private int parseInt(final String line, final int start, final int length) {
  1027.             if (line.length() > start && !parseString(line, start, length).isEmpty()) {
  1028.                 return Integer.parseInt(parseString(line, start, length));
  1029.             } else {
  1030.                 return 0;
  1031.             }
  1032.         }

  1033.         /** Extract a double from a line.
  1034.          * @param line to parse
  1035.          * @param start start index of the real
  1036.          * @param length length of the real
  1037.          * @return parsed real, or {@code Double.NaN} if field was empty
  1038.          */
  1039.         private double parseDouble(final String line, final int start, final int length) {
  1040.             if (line.length() > start && !parseString(line, start, length).isEmpty()) {
  1041.                 return Double.parseDouble(parseString(line, start, length));
  1042.             } else {
  1043.                 return Double.NaN;
  1044.             }
  1045.         }

  1046.         /** Phase Shift corrections.
  1047.          * Contains the phase shift corrections used to
  1048.          * generate phases consistent with respect to cycle shifts.
  1049.          */
  1050.         public class PhaseShiftCorrection {

  1051.             /** Satellite System. */
  1052.             private final SatelliteSystem satSystemPhaseShift;
  1053.             /** Carrier Phase Observation Code. */
  1054.             private final ObservationType typeObsPhaseShift;
  1055.             /** Phase Shift Corrections (cycles). */
  1056.             private final double phaseShiftCorrection;
  1057.             /** List of satellites involved. */
  1058.             private final String[] satsPhaseShift;

  1059.             /** Simple constructor.
  1060.              * @param satSystemPhaseShift Satellite System
  1061.              * @param typeObsPhaseShift Carrier Phase Observation Code
  1062.              * @param phaseShiftCorrection Phase Shift Corrections (cycles)
  1063.              * @param satsPhaseShift List of satellites involved
  1064.              */
  1065.             private PhaseShiftCorrection(final SatelliteSystem satSystemPhaseShift,
  1066.                                          final ObservationType typeObsPhaseShift,
  1067.                                          final double phaseShiftCorrection, final String[] satsPhaseShift) {
  1068.                 this.satSystemPhaseShift = satSystemPhaseShift;
  1069.                 this.typeObsPhaseShift = typeObsPhaseShift;
  1070.                 this.phaseShiftCorrection = phaseShiftCorrection;
  1071.                 this.satsPhaseShift = satsPhaseShift;
  1072.             }

  1073.             /** Get the Satellite System.
  1074.              * @return Satellite System.
  1075.              */
  1076.             public SatelliteSystem getSatelliteSystem() {
  1077.                 return satSystemPhaseShift;
  1078.             }
  1079.             /** Get the Carrier Phase Observation Code.
  1080.              * @return Carrier Phase Observation Code.
  1081.              */
  1082.             public ObservationType getTypeObs() {
  1083.                 return typeObsPhaseShift;
  1084.             }
  1085.             /** Get the Phase Shift Corrections.
  1086.              * @return Phase Shift Corrections (cycles)
  1087.              */
  1088.             public double getCorrection() {
  1089.                 return phaseShiftCorrection;
  1090.             }
  1091.             /** Get the list of satellites involved.
  1092.              * @return List of satellites involved (if null, all the sats are involved)
  1093.              */
  1094.             public String[] getSatsCorrected() {
  1095.                 //If empty, all the satellites of this constellation are affected for this Observation type
  1096.                 return satsPhaseShift;
  1097.             }
  1098.         }

  1099.         /** Scale Factor to be applied.
  1100.          * Contains the scale factors of 10 applied to the data before
  1101.          * being stored into the RINEX file.
  1102.          */
  1103.         public class ScaleFactorCorrection {

  1104.             /** Satellite System. */
  1105.             private final SatelliteSystem satSystemScaleFactor;
  1106.             /** List of Observations types that have been scaled. */
  1107.             private final List<ObservationType> typesObsScaleFactor;
  1108.             /** Factor to divide stored observations with before use. */
  1109.             private final double scaleFactor;

  1110.             /** Simple constructor.
  1111.              * @param satSystemScaleFactor Satellite System
  1112.              * @param scaleFactor Factor to divide stored observations (1,10,100,1000)
  1113.              * @param typesObsScaleFactor List of Observations types that have been scaled
  1114.              */
  1115.             private ScaleFactorCorrection(final SatelliteSystem satSystemScaleFactor,
  1116.                                           final double scaleFactor,
  1117.                                           final List<ObservationType> typesObsScaleFactor) {
  1118.                 this.satSystemScaleFactor = satSystemScaleFactor;
  1119.                 this.scaleFactor = scaleFactor;
  1120.                 this.typesObsScaleFactor = typesObsScaleFactor;
  1121.             }
  1122.             /** Get the Satellite System.
  1123.              * @return Satellite System
  1124.              */
  1125.             public SatelliteSystem getSatelliteSystem() {
  1126.                 return satSystemScaleFactor;
  1127.             }
  1128.             /** Get the Scale Factor.
  1129.              * @return Scale Factor
  1130.              */
  1131.             public double getCorrection() {
  1132.                 return scaleFactor;
  1133.             }
  1134.             /** Get the list of Observation Types scaled.
  1135.              * @return List of Observation types scaled
  1136.              */
  1137.             public List<ObservationType> getTypesObsScaled() {
  1138.                 return typesObsScaleFactor;
  1139.             }
  1140.         }

  1141.         /** Corrections of Differential Code Biases (DCBs) applied.
  1142.          * Contains information on the programs used to correct the observations
  1143.          * in RINEX files for differential code biases.
  1144.          */
  1145.         public class AppliedDCBS {

  1146.             /** Satellite system. */
  1147.             private final SatelliteSystem satelliteSystem;

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

  1150.             /** Source of corrections (URL). */
  1151.             private final String sourceDCBS;

  1152.             /** Simple constructor.
  1153.              * @param satelliteSystem satellite system
  1154.              * @param progDCBS Program name used to apply DCBs
  1155.              * @param sourceDCBS Source of corrections (URL)
  1156.              */
  1157.             private AppliedDCBS(final SatelliteSystem satelliteSystem,
  1158.                                 final String progDCBS, final String sourceDCBS) {
  1159.                 this.satelliteSystem = satelliteSystem;
  1160.                 this.progDCBS        = progDCBS;
  1161.                 this.sourceDCBS      = sourceDCBS;
  1162.             }

  1163.             /** Get the satellite system.
  1164.              * @return satellite system
  1165.              */
  1166.             public SatelliteSystem getSatelliteSystem() {
  1167.                 return satelliteSystem;
  1168.             }

  1169.             /** Get the program name used to apply DCBs.
  1170.              * @return  Program name used to apply DCBs
  1171.              */
  1172.             public String getProgDCBS() {
  1173.                 return progDCBS;
  1174.             }

  1175.             /** Get the source of corrections.
  1176.              * @return Source of corrections (URL)
  1177.              */
  1178.             public String getSourceDCBS() {
  1179.                 return sourceDCBS;
  1180.             }

  1181.         }

  1182.         /** Corrections of antenna phase center variations (PCVs) applied.
  1183.          * Contains information on the programs used to correct the observations
  1184.          * in RINEX files for antenna phase center variations.
  1185.          */
  1186.         public class AppliedPCVS {

  1187.             /** Satellite system. */
  1188.             private final SatelliteSystem satelliteSystem;

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

  1191.             /** Source of corrections (URL). */
  1192.             private final String sourcePCVS;

  1193.             /** Simple constructor.
  1194.              * @param satelliteSystem satellite system
  1195.              * @param progPCVS Program name used for PCVs
  1196.              * @param sourcePCVS Source of corrections (URL)
  1197.              */
  1198.             private AppliedPCVS(final SatelliteSystem satelliteSystem,
  1199.                                 final String progPCVS, final String sourcePCVS) {
  1200.                 this.satelliteSystem = satelliteSystem;
  1201.                 this.progPCVS        = progPCVS;
  1202.                 this.sourcePCVS      = sourcePCVS;
  1203.             }

  1204.             /** Get the satellite system.
  1205.              * @return satellite system
  1206.              */
  1207.             public SatelliteSystem getSatelliteSystem() {
  1208.                 return satelliteSystem;
  1209.             }

  1210.             /** Get the program name used to apply PCVs.
  1211.              * @return  Program name used to apply PCVs
  1212.              */
  1213.             public String getProgPCVS() {
  1214.                 return progPCVS;
  1215.             }

  1216.             /** Get the source of corrections.
  1217.              * @return Source of corrections (URL)
  1218.              */
  1219.             public String getSourcePCVS() {
  1220.                 return sourcePCVS;
  1221.             }

  1222.         }
  1223.     }

  1224. }