BulletinBFilesLoader.java

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

  81.     /** Conversion factor. */
  82.     private static final double MILLI_ARC_SECONDS_TO_RADIANS = Constants.ARC_SECONDS_TO_RADIANS / 1000;

  83.     /** Conversion factor. */
  84.     private static final double MILLI_SECONDS_TO_SECONDS = 1.e-3;

  85.     /** Section 1 header pattern. */
  86.     private static final Pattern SECTION_1_HEADER;

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

  89.     /** Section 3 header pattern. */
  90.     private static final Pattern SECTION_3_HEADER;

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

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

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

  97.     /** Data line pattern in section 2. */
  98.     private static final Pattern SECTION_2_DATA_OLD_FORMAT;

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

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

  103.     static {

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

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

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

  205.     }

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

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

  227.     /** Internal class performing the parsing. */
  228.     static class Parser extends AbstractEopParser {

  229.         /** ITRF version configuration. */
  230.         private ITRFVersionLoader.ITRFVersionConfiguration configuration;

  231.         /** History entries. */
  232.         private List<EOPEntry> history;

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

  235.         /** Current line number. */
  236.         private int lineNumber;

  237.         /** Current line. */
  238.         private String line;

  239.         /** Start of final data. */
  240.         private int mjdMin;

  241.         /** End of final data. */
  242.         private int mjdMax;

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

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

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

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

  275.                 if (isOldFormat) {

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

  278.                     final Matcher section2Matcher = seekToLine(SECTION_2_HEADER_OLD, reader, name);
  279.                     final boolean isNonRotatingOrigin = section2Matcher.group(1).startsWith("dX");
  280.                     loadEOPOldFormat(isNonRotatingOrigin, reader, name);

  281.                 } else {

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

  284.                     // skip to section 3
  285.                     seekToLine(SECTION_3_HEADER, reader, name);

  286.                     // extract LOD data from section 3
  287.                     loadLODNewFormat(reader, name);

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

  307.                 }
  308.             }

  309.             return history;

  310.         }

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

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

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

  330.         }

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

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

  361.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  362.                                       name, lineNumber);

  363.         }

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

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

  418.         }

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

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

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

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

  509.     }

  510. }