BulletinBFilesLoader.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.frames;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.SortedSet;
  29. import java.util.function.Supplier;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;

  32. import org.hipparchus.util.FastMath;
  33. import org.orekit.data.DataProvidersManager;
  34. import org.orekit.errors.OrekitException;
  35. import org.orekit.errors.OrekitMessages;
  36. import org.orekit.time.AbsoluteDate;
  37. import org.orekit.time.DateComponents;
  38. import org.orekit.time.Month;
  39. import org.orekit.time.TimeScale;
  40. import org.orekit.utils.Constants;
  41. import org.orekit.utils.IERSConventions;
  42. import org.orekit.utils.IERSConventions.NutationCorrectionConverter;
  43. import org.orekit.utils.units.UnitsConverter;

  44. /** Loader for bulletin B files.
  45.  * <p>Bulletin B files contain {@link EOPEntry
  46.  * Earth Orientation Parameters} for a few months periods.
  47.  * They correspond to finalized data, suitable for long term
  48.  * a posteriori analysis.</p>
  49.  * <p>The bulletin B files are recognized thanks to their base names,
  50.  * which must match one of the patterns <code>bulletinb_IAU2000-###.txt</code>,
  51.  * <code>bulletinb_IAU2000.###</code>, <code>bulletinb-###.txt</code> or
  52.  * <code>bulletinb.###</code> (or the same ending with <code>.gz</code>
  53.  * for gzip-compressed files) where # stands for a digit character.</p>
  54.  * <p>
  55.  * Starting with bulletin B 252 published in February 2009, buletins B are
  56.  * written in a format containing nutation corrections for both the
  57.  * new IAU2000 nutation model as dx, dy entries in its section 1 and nutation
  58.  * corrections for the old IAU1976 nutation model as dPsi, dEpsilon entries in
  59.  * its section 2. These bulletins are available from IERS <a
  60.  * href="ftp://ftp.iers.org/products/eop/bulletinb/format_2009/">
  61.  *  FTP site</a>. They are also available with exactly the same content
  62.  * (but a different naming convention) from <a
  63.  * href="http://hpiers.obspm.fr/eoppc/bul/bulb_new/">Paris-Meudon
  64.  * observatory site</a>.
  65.  * </p>
  66.  * <p>
  67.  * Ending with bulletin B 263 published in January 2010, bulletins B were
  68.  * written in a format containing only one type of nutation corrections in its
  69.  * section 1, either for new IAU2000 nutation model as dx, dy entries or the old
  70.  * IAU1976 nutation model as dPsi, dEpsilon entries, depending on the file (a pair of
  71.  * files with different name was published each month between March 2003 and January 2010).
  72.  * </p>
  73.  * <p>
  74.  * This class handles both the old and the new format.
  75.  * </p>
  76.  * <p>
  77.  * This class is immutable and hence thread-safe
  78.  * </p>
  79.  * @author Luc Maisonobe
  80.  */
  81. class BulletinBFilesLoader extends AbstractEopLoader implements EOPHistoryLoader {

  82.     /** Section 1 header pattern. */
  83.     private static final Pattern SECTION_1_HEADER;

  84.     /** Section 2 header pattern for old format. */
  85.     private static final Pattern SECTION_2_HEADER_OLD;

  86.     /** Section 3 header pattern. */
  87.     private static final Pattern SECTION_3_HEADER;

  88.     /** Pattern for line introducing the final bulletin B values. */
  89.     private static final Pattern FINAL_VALUES_START;

  90.     /** Pattern for line introducing the bulletin B preliminary extension. */
  91.     private static final Pattern FINAL_VALUES_END;

  92.     /** Data line pattern in section 1 (old format). */
  93.     private static final Pattern SECTION_1_DATA_OLD_FORMAT;

  94.     /** Data line pattern in section 2. */
  95.     private static final Pattern SECTION_2_DATA_OLD_FORMAT;

  96.     /** Data line pattern in section 1 (new format). */
  97.     private static final Pattern SECTION_1_DATA_NEW_FORMAT;

  98.     /** Data line pattern in section 3 (new format). */
  99.     private static final Pattern SECTION_3_DATA_NEW_FORMAT;

  100.     static {

  101.         // the section headers lines in the old bulletin B monthly data files have
  102.         // the following form (the indentation discrepancy for section 6 is really
  103.         // present in the available files):
  104.         // 1 - EARTH ORIENTATION PARAMETERS (IERS evaluation).
  105.         // either
  106.         // 2 - SMOOTHED VALUES OF x, y, UT1, D, DPSI, DEPSILON (IERS EVALUATION)
  107.         // or
  108.         // 2 - SMOOTHED VALUES OF x, y, UT1, D, dX, dY (IERS EVALUATION)
  109.         // 3 - NORMAL VALUES OF THE EARTH ORIENTATION PARAMETERS AT FIVE-DAY INTERVALS
  110.         // 4 - DURATION OF THE DAY AND ANGULAR VELOCITY OF THE EARTH (IERS evaluation).
  111.         // 5 - INFORMATION ON TIME SCALES
  112.         //       6 - SUMMARY OF CONTRIBUTED EARTH ORIENTATION PARAMETERS SERIES
  113.         //
  114.         // the section headers lines in the new bulletin B monthly data files have
  115.         // the following form:
  116.         // 1 - DAILY FINAL VALUES OF  x, y, UT1-UTC, dX, dY
  117.         // 2 - DAILY FINAL VALUES OF CELESTIAL POLE OFFSETS dPsi1980 & dEps1980
  118.         // 3 - EARTH ANGULAR VELOCITY : DAILY FINAL VALUES OF LOD, OMEGA AT 0hUTC
  119.         // 4 - INFORMATION ON TIME SCALES
  120.         // 5 - SUMMARY OF CONTRIBUTED EARTH ORIENTATION PARAMETERS SERIES
  121.         SECTION_1_HEADER     = Pattern.compile("^ +1 - (\\p{Upper}+) \\p{Upper}+ \\p{Upper}+.*");
  122.         SECTION_2_HEADER_OLD = Pattern.compile("^ +2 - SMOOTHED \\p{Upper}+ \\p{Upper}+.*((?:DPSI, DEPSILON)|(?:dX, dY)).*");
  123.         SECTION_3_HEADER     = Pattern.compile("^ +3 - \\p{Upper}+ \\p{Upper}+ \\p{Upper}+.*");

  124.         // the markers bracketing the final values in section 1 in the old bulletin B
  125.         // monthly data files have the following form:
  126.         //
  127.         //  Final Bulletin B values.
  128.         //   ...
  129.         //  Preliminary extension, to be updated weekly in Bulletin A and monthly
  130.         //  in Bulletin B.
  131.         //
  132.         // the markers bracketing the final values in section 1 in the new bulletin B
  133.         // monthly data files have the following form:
  134.         //
  135.         //  Final values
  136.         //   ...
  137.         //  Preliminary extension
  138.         //
  139.         FINAL_VALUES_START = Pattern.compile("^\\p{Blank}+Final( Bulletin B)? values.*");
  140.         FINAL_VALUES_END   = Pattern.compile("^\\p{Blank}+Preliminary extension.*");

  141.         // the data lines in the old bulletin B monthly data files have the following form:
  142.         // in section 1:
  143.         // AUG   1  55044  0.22176 0.49302  0.231416  -33.768584   -69.1    -8.9
  144.         // AUG   6  55049  0.23202 0.48003  0.230263  -33.769737   -69.5    -8.5
  145.         // in section 2:
  146.         // AUG   1   55044  0.22176  0.49302  0.230581 -0.835  -0.310  -69.1   -8.9
  147.         // AUG   2   55045  0.22395  0.49041  0.230928 -0.296  -0.328  -69.5   -8.9
  148.         //
  149.         // the data lines in the new bulletin B monthly data files have the following form:
  150.         // in section 1:
  151.         // 2009   8   2   55045  223.954  490.410  230.9277    0.214 -0.056    0.008    0.009    0.0641  0.048  0.121
  152.         // 2009   8   3   55046  225.925  487.700  231.2186    0.300 -0.138    0.010    0.012    0.0466  0.099  0.248
  153.         // 2009   8   4   55047  227.931  485.078  231.3929    0.347 -0.231    0.019    0.023    0.0360  0.099  0.249
  154.         // 2009   8   5   55048  230.016  482.445  231.4601    0.321 -0.291    0.025    0.028    0.0441  0.095  0.240
  155.         // 2009   8   6   55049  232.017  480.026  231.3619    0.267 -0.273    0.025    0.029    0.0477  0.038  0.095
  156.         // in section 2:
  157.         // 2009   8   2   55045   -69.474    -8.929     0.199     0.121
  158.         // 2009   8   3   55046   -69.459    -9.016     0.250     0.248
  159.         // 2009   8   4   55047   -69.401    -9.039     0.250     0.249
  160.         // 2009   8   5   55048   -69.425    -8.864     0.247     0.240
  161.         // 2009   8   6   55049   -69.510    -8.539     0.153     0.095
  162.         // in section 3:
  163.         // 2009   8   2   55045 -0.3284  0.0013  15.04106723584    0.00000000023
  164.         // 2009   8   3   55046 -0.2438  0.0013  15.04106722111    0.00000000023
  165.         // 2009   8   4   55047 -0.1233  0.0013  15.04106720014    0.00000000023
  166.         // 2009   8   5   55048  0.0119  0.0013  15.04106717660    0.00000000023
  167.         // 2009   8   6   55049  0.1914  0.0013  15.04106714535    0.00000000023
  168.         final StringBuilder builder = new StringBuilder("^\\p{Blank}+(?:");
  169.         for (final Month month : Month.values()) {
  170.             builder.append(month.getUpperCaseAbbreviation());
  171.             builder.append('|');
  172.         }
  173.         builder.delete(builder.length() - 1, builder.length());
  174.         builder.append(")");
  175.         final String integerPattern      = "[-+]?\\p{Digit}+";
  176.         final String realPattern         = "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";
  177.         final String monthNameField      = builder.toString();
  178.         final String ignoredIntegerField = "\\p{Blank}*" + integerPattern;
  179.         final String storedIntegerField  = "\\p{Blank}*(" + integerPattern + ")";
  180.         final String mjdField            = "\\p{Blank}+(\\p{Digit}\\p{Digit}\\p{Digit}\\p{Digit}\\p{Digit})";
  181.         final String storedRealField     = "\\p{Blank}+(" + realPattern + ")";
  182.         final String ignoredRealField    = "\\p{Blank}+" + realPattern;
  183.         final String finalBlanks         = "\\p{Blank}*$";
  184.         SECTION_1_DATA_OLD_FORMAT = Pattern.compile(monthNameField + ignoredIntegerField + mjdField +
  185.                                                     ignoredRealField + ignoredRealField + ignoredRealField +
  186.                                                     ignoredRealField + ignoredRealField + ignoredRealField +
  187.                                                     finalBlanks);
  188.         SECTION_2_DATA_OLD_FORMAT = Pattern.compile(monthNameField + ignoredIntegerField + mjdField +
  189.                                                     storedRealField  + storedRealField  + storedRealField +
  190.                                                     ignoredRealField +
  191.                                                     storedRealField + storedRealField + storedRealField +
  192.                                                     finalBlanks);
  193.         SECTION_1_DATA_NEW_FORMAT = Pattern.compile(storedIntegerField + storedIntegerField + storedIntegerField + mjdField +
  194.                                                     storedRealField + storedRealField + storedRealField +
  195.                                                     storedRealField + storedRealField + ignoredRealField + ignoredRealField +
  196.                                                     ignoredRealField + ignoredRealField + ignoredRealField +
  197.                                                     finalBlanks);
  198.         SECTION_3_DATA_NEW_FORMAT = Pattern.compile(ignoredIntegerField + ignoredIntegerField + ignoredIntegerField + mjdField +
  199.                                                     storedRealField +
  200.                                                     ignoredRealField + ignoredRealField + ignoredRealField +
  201.                                                     finalBlanks);

  202.     }

  203.     /** Build a loader for IERS bulletins B files.
  204.      * @param supportedNames regular expression for supported files names
  205.      * @param manager provides access to the bulletin B files.
  206.      * @param utcSupplier UTC time scale.
  207.      */
  208.     BulletinBFilesLoader(final String supportedNames,
  209.                          final DataProvidersManager manager,
  210.                          final Supplier<TimeScale> utcSupplier) {
  211.         super(supportedNames, manager, utcSupplier);
  212.     }

  213.     /** {@inheritDoc} */
  214.     public void fillHistory(final IERSConventions.NutationCorrectionConverter converter,
  215.                             final SortedSet<EOPEntry> history) {
  216.         final ItrfVersionProvider itrfVersionProvider = new ITRFVersionLoader(
  217.                 ITRFVersionLoader.SUPPORTED_NAMES,
  218.                 getDataProvidersManager());
  219.         final Parser parser = new Parser(converter, itrfVersionProvider, getUtc());
  220.         final EopParserLoader loader = new EopParserLoader(parser);
  221.         this.feed(loader);
  222.         history.addAll(loader.getEop());
  223.     }

  224.     /** Internal class performing the parsing. */
  225.     static class Parser extends AbstractEopParser {

  226.         /** ITRF version configuration. */
  227.         private ITRFVersionLoader.ITRFVersionConfiguration configuration;

  228.         /** History entries. */
  229.         private List<EOPEntry> history;

  230.         /** Map for fields read in different sections. */
  231.         private final Map<Integer, double[]> fieldsMap;

  232.         /** Current line number. */
  233.         private int lineNumber;

  234.         /** Current line. */
  235.         private String line;

  236.         /** Start of final data. */
  237.         private int mjdMin;

  238.         /** End of final data. */
  239.         private int mjdMax;

  240.         /**
  241.          * Simple constructor.
  242.          *
  243.          * @param converter           converter to use
  244.          * @param itrfVersionProvider to use for determining the ITRF version of the EOP.
  245.          * @param utc                 time scale for parsing dates.
  246.          */
  247.         Parser(final NutationCorrectionConverter converter,
  248.                final ItrfVersionProvider itrfVersionProvider,
  249.                final TimeScale utc) {
  250.             super(converter, itrfVersionProvider, utc);
  251.             this.fieldsMap         = new HashMap<>();
  252.             this.lineNumber        = 0;
  253.             this.mjdMin            = Integer.MAX_VALUE;
  254.             this.mjdMax            = Integer.MIN_VALUE;
  255.         }

  256.         /** {@inheritDoc} */
  257.         @Override
  258.         public Collection<EOPEntry> parse(final InputStream input, final String name)
  259.             throws IOException {

  260.             // set up a reader for line-oriented bulletin B files
  261.             try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
  262.                 // reset parse info to start new file
  263.                 fieldsMap.clear();
  264.                 lineNumber = 0;
  265.                 mjdMin     = Integer.MAX_VALUE;
  266.                 mjdMax     = Integer.MIN_VALUE;
  267.                 history = new ArrayList<>();
  268.                 configuration = null;

  269.                 // skip header up to section 1 and check if we are parsing an old or new format file
  270.                 final Matcher section1Matcher = seekToLine(SECTION_1_HEADER, reader, name);
  271.                 final boolean isOldFormat = "EARTH".equals(section1Matcher.group(1));

  272.                 if (isOldFormat) {

  273.                     // extract MJD bounds for final data from section 1
  274.                     loadMJDBoundsOldFormat(reader, name);

  275.                     final Matcher section2Matcher = seekToLine(SECTION_2_HEADER_OLD, reader, name);
  276.                     final boolean isNonRotatingOrigin = section2Matcher.group(1).startsWith("dX");
  277.                     loadEOPOldFormat(isNonRotatingOrigin, reader, name);

  278.                 } else {

  279.                     // extract x, y, UT1-UTC, dx, dy from section 1
  280.                     loadXYDTDxDyNewFormat(reader, name);

  281.                     // skip to section 3
  282.                     seekToLine(SECTION_3_HEADER, reader, name);

  283.                     // extract LOD data from section 3
  284.                     loadLODNewFormat(reader, name);

  285.                     // set up the EOP entries
  286.                     for (Map.Entry<Integer, double[]> entry : fieldsMap.entrySet()) {
  287.                         final int mjd = entry.getKey();
  288.                         final double[] array = entry.getValue();
  289.                         if (Double.isNaN(array[0] + array[1] + array[2] + array[3] + array[4] + array[5])) {
  290.                             throw notifyUnexpectedErrorEncountered(name);
  291.                         }
  292.                         final AbsoluteDate mjdDate =
  293.                                 new AbsoluteDate(new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd),
  294.                                                  getUtc());
  295.                         final double[] equinox = getConverter().toEquinox(mjdDate, array[4], array[5]);
  296.                         if (configuration == null || !configuration.isValid(mjd)) {
  297.                             // get a configuration for current name and date range
  298.                             configuration = getItrfVersionProvider().getConfiguration(name, mjd);
  299.                         }
  300.                         history.add(new EOPEntry(mjd, array[0], array[1], array[2], array[3],
  301.                                                  equinox[0], equinox[1], array[4], array[5],
  302.                                                  configuration.getVersion(), mjdDate));
  303.                     }

  304.                 }
  305.             }

  306.             return history;

  307.         }

  308.         /** Read until a line matching a pattern is found.
  309.          * @param pattern pattern to look for
  310.          * @param reader reader from where file content is obtained
  311.          * @param name name of the file (or zip entry)
  312.          * @return the matching matcher for the line
  313.          * @exception IOException if data can't be read
  314.          */
  315.         private Matcher seekToLine(final Pattern pattern, final BufferedReader reader, final String name)
  316.             throws IOException {

  317.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  318.                 ++lineNumber;
  319.                 final Matcher matcher = pattern.matcher(line);
  320.                 if (matcher.matches()) {
  321.                     return matcher;
  322.                 }
  323.             }

  324.             // we have reached end of file and not found a matching line
  325.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  326.                                       name, lineNumber);

  327.         }

  328.         /** Read MJD bounds of the final data part from section 1 in the old bulletin B format.
  329.          * @param reader reader from where file content is obtained
  330.          * @param name name of the file (or zip entry)
  331.          * @exception IOException if data can't be read
  332.          */
  333.         private void loadMJDBoundsOldFormat(final BufferedReader reader, final String name)
  334.             throws IOException {

  335.             boolean inFinalValuesPart = false;
  336.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  337.                 lineNumber++;
  338.                 Matcher matcher = FINAL_VALUES_START.matcher(line);
  339.                 if (matcher.matches()) {
  340.                     // we are entering final values part (in section 1)
  341.                     inFinalValuesPart = true;
  342.                 } else if (inFinalValuesPart) {
  343.                     matcher = SECTION_1_DATA_OLD_FORMAT.matcher(line);
  344.                     if (matcher.matches()) {
  345.                         // this is a data line, build an entry from the extracted fields
  346.                         final int mjd = Integer.parseInt(matcher.group(1));
  347.                         mjdMin = FastMath.min(mjdMin, mjd);
  348.                         mjdMax = FastMath.max(mjdMax, mjd);
  349.                     } else {
  350.                         matcher = FINAL_VALUES_END.matcher(line);
  351.                         if (matcher.matches()) {
  352.                             // we leave final values part
  353.                             return;
  354.                         }
  355.                     }
  356.                 }
  357.             }

  358.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  359.                                       name, lineNumber);

  360.         }

  361.         /** Read EOP data from section 2 in the old bulletin B format.
  362.          * @param isNonRotatingOrigin if true, the file contain Non-Rotating Origin nutation corrections
  363.          * @param reader reader from where file content is obtained
  364.          * @param name name of the file (or zip entry)
  365.          * @exception IOException if data can't be read
  366.          */
  367.         private void loadEOPOldFormat(final boolean isNonRotatingOrigin,
  368.                                       final BufferedReader reader, final String name)
  369.             throws IOException {

  370.             // read the data lines in the final values part inside section 2
  371.             line = reader.readLine();
  372.             while (line != null) {
  373.                 lineNumber++;
  374.                 final Matcher matcher = SECTION_2_DATA_OLD_FORMAT.matcher(line);
  375.                 if (matcher.matches()) {
  376.                     // this is a data line, build an entry from the extracted fields
  377.                     final int    mjd   = Integer.parseInt(matcher.group(1));
  378.                     final double x     = Double.parseDouble(matcher.group(2)) * Constants.ARC_SECONDS_TO_RADIANS;
  379.                     final double y     = Double.parseDouble(matcher.group(3)) * Constants.ARC_SECONDS_TO_RADIANS;
  380.                     final double dtu1  = Double.parseDouble(matcher.group(4));
  381.                     final double lod   = UnitsConverter.MILLI_SECONDS_TO_SECONDS.convert(Double.parseDouble(matcher.group(5)));
  382.                     if (mjd >= mjdMin) {
  383.                         final AbsoluteDate mjdDate =
  384.                                 new AbsoluteDate(new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd),
  385.                                                  getUtc());
  386.                         final double[] equinox;
  387.                         final double[] nro;
  388.                         if (isNonRotatingOrigin) {
  389.                             nro = new double[] {
  390.                                 UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(6))),
  391.                                 UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(7)))
  392.                             };
  393.                             equinox = getConverter().toEquinox(mjdDate, nro[0], nro[1]);
  394.                         } else {
  395.                             equinox = new double[] {
  396.                                 UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(6))),
  397.                                 UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(7)))
  398.                             };
  399.                             nro = getConverter().toNonRotating(mjdDate, equinox[0], equinox[1]);
  400.                         }
  401.                         if (configuration == null || !configuration.isValid(mjd)) {
  402.                             // get a configuration for current name and date range
  403.                             configuration = getItrfVersionProvider().getConfiguration(name, mjd);
  404.                         }
  405.                         history.add(new EOPEntry(mjd, dtu1, lod, x, y, equinox[0], equinox[1], nro[0], nro[1],
  406.                                                  configuration.getVersion(), mjdDate));
  407.                         line = mjd < mjdMax ? reader.readLine() : null;
  408.                     } else {
  409.                         line = reader.readLine();
  410.                     }
  411.                 } else {
  412.                     line = reader.readLine();
  413.                 }
  414.             }

  415.         }

  416.         /** Read X, Y, UT1-UTC, dx, dy from section 1 in the new bulletin B format.
  417.          * @param reader reader from where file content is obtained
  418.          * @param name name of the file (or zip entry)
  419.          * @exception IOException if data can't be read
  420.          */
  421.         private void loadXYDTDxDyNewFormat(final BufferedReader reader, final String name)
  422.             throws IOException {

  423.             boolean inFinalValuesPart = false;
  424.             line = reader.readLine();
  425.             while (line != null) {
  426.                 lineNumber++;
  427.                 Matcher matcher = FINAL_VALUES_START.matcher(line);
  428.                 if (matcher.matches()) {
  429.                     // we are entering final values part (in section 1)
  430.                     inFinalValuesPart = true;
  431.                     line = reader.readLine();
  432.                 } else if (inFinalValuesPart) {
  433.                     matcher = SECTION_1_DATA_NEW_FORMAT.matcher(line);
  434.                     if (matcher.matches()) {
  435.                         // this is a data line, build an entry from the extracted fields
  436.                         final int year  = Integer.parseInt(matcher.group(1));
  437.                         final int month = Integer.parseInt(matcher.group(2));
  438.                         final int day   = Integer.parseInt(matcher.group(3));
  439.                         final int mjd   = Integer.parseInt(matcher.group(4));
  440.                         if (new DateComponents(year, month, day).getMJD() != mjd) {
  441.                             throw new OrekitException(OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE,
  442.                                                       name, year, month, day, mjd);
  443.                         }
  444.                         mjdMin = FastMath.min(mjdMin, mjd);
  445.                         mjdMax = FastMath.max(mjdMax, mjd);
  446.                         final double x    = UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(5)));
  447.                         final double y    = UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(6)));
  448.                         final double dtu1 = UnitsConverter.MILLI_SECONDS_TO_SECONDS.convert(Double.parseDouble(matcher.group(7)));
  449.                         final double dx   = UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(8)));
  450.                         final double dy   = UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(matcher.group(9)));
  451.                         fieldsMap.put(mjd,
  452.                                       new double[] {
  453.                                           dtu1, Double.NaN, x, y, dx, dy
  454.                                       });
  455.                         line = reader.readLine();
  456.                     } else {
  457.                         matcher = FINAL_VALUES_END.matcher(line);
  458.                         line = matcher.matches() ? null : reader.readLine();
  459.                     }
  460.                 } else {
  461.                     line = reader.readLine();
  462.                 }
  463.             }
  464.         }

  465.         /** Read LOD from section 3 in the new bulletin B format.
  466.          * @param reader reader from where file content is obtained
  467.          * @param name name of the file (or zip entry)
  468.          * @exception IOException if data can't be read
  469.          */
  470.         private void loadLODNewFormat(final BufferedReader reader, final String name)
  471.             throws IOException {
  472.             line = reader.readLine();
  473.             while (line != null) {
  474.                 lineNumber++;
  475.                 final Matcher matcher = SECTION_3_DATA_NEW_FORMAT.matcher(line);
  476.                 if (matcher.matches()) {
  477.                     // this is a data line, build an entry from the extracted fields
  478.                     final int    mjd = Integer.parseInt(matcher.group(1));
  479.                     if (mjd >= mjdMin) {
  480.                         final double lod = UnitsConverter.MILLI_SECONDS_TO_SECONDS.convert(Double.parseDouble(matcher.group(2)));
  481.                         final double[] array = fieldsMap.get(mjd);
  482.                         if (array == null) {
  483.                             throw notifyUnexpectedErrorEncountered(name);
  484.                         }
  485.                         array[1] = lod;
  486.                         line = mjd >= mjdMax ? null : reader.readLine();
  487.                     } else {
  488.                         line = reader.readLine();
  489.                     }
  490.                 } else {
  491.                     line = reader.readLine();
  492.                 }
  493.             }
  494.         }

  495.         /** Create an exception to be thrown.
  496.          * @param name name of the file (or zip entry)
  497.          * @return OrekitException always thrown to notify an unexpected error has been
  498.          * encountered by the caller
  499.          */
  500.         private OrekitException notifyUnexpectedErrorEncountered(final String name) {
  501.             String loaderName = BulletinBFilesLoader.class.getName();
  502.             loaderName = loaderName.substring(loaderName.lastIndexOf('.') + 1);
  503.             return new OrekitException(OrekitMessages.UNEXPECTED_FILE_FORMAT_ERROR_FOR_LOADER,
  504.                                        name, loaderName);
  505.         }

  506.     }

  507. }