BulletinAFilesLoader.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.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.Arrays;
  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.DataLoader;
  34. import org.orekit.data.DataProvidersManager;
  35. import org.orekit.errors.OrekitException;
  36. import org.orekit.errors.OrekitInternalError;
  37. import org.orekit.errors.OrekitMessages;
  38. import org.orekit.time.AbsoluteDate;
  39. import org.orekit.time.DateComponents;
  40. import org.orekit.time.TimeScale;
  41. import org.orekit.utils.IERSConventions;
  42. import org.orekit.utils.units.UnitsConverter;

  43. /** Loader for bulletin A files.
  44.  * <p>Bulletin A files contain {@link EOPEntry
  45.  * Earth Orientation Parameters} for a few days periods, they
  46.  * correspond to rapid data estimations, suitable for near-real time
  47.  * and prediction purposes. Prediction series are only available for
  48.  * pole motion xp, yp and UT1-UTC, they are not available for
  49.  * pole offsets (Δδψ/Δδε and x/y).</p>
  50.  * <p>A bulletin A published on Modified Julian Day mjd (nominally a
  51.  * Thursday) will generally contain:
  52.  * </p>
  53.  * <ul>
  54.  *   <li>rapid service xp, yp and UT1-UTC data from mjd-6 to mjd</li>
  55.  *   <li>prediction xp, yp and UT1-UTC data from mjd+1 to mjd+365</li>
  56.  *   <li>if it is first bulletin of month m, final values xp, yp and
  57.  *       UT1-UTC data from day 2 of month m-2 to day 1 of month m-1</li>
  58.  *   <li>rapid service pole offsets Δδψ/Δδε and x/y if available, for some
  59.  *       varying period somewhere from mjd-30 to mjd-10 (see below)</li>
  60.  *   <li>if it is first bulletin of month m, final values pole offsets
  61.  *       Δδψ/Δδε and x/y data from day 2 of month m-2 to day 1 of month
  62.  *       m-1</li>
  63.  * </ul>
  64.  * <p>
  65.  * There are some discrepancies in the rapid service time range above,
  66.  * mainly when the nominal publication Thursday corresponds to holidays.
  67.  * In this case a bulletin may be published the day before and have a 6
  68.  * days span only for rapid data, and a later bulletin will have an 8 days
  69.  * span to recover the normal schedule. This occurred for bulletin A Vol.
  70.  * XVIII No. 047, bulletin A Vol. XVIII No. 048, bulletin A Vol. XXI No.
  71.  * 052 and bulletin A Vol. XXII No. 001.
  72.  * </p>
  73.  * <p>Rapid service for pole offsets appears irregular. As extreme examples
  74.  * bulletin A Vol. XXVI No. 037 from 2013-09-12 contained 15 entries
  75.  * for pole offsets, from mjd-22 to mjd-8, bulletin A Vol. XXVI No. 039
  76.  * from 2013-09-26 contained only 3 entries for pole offsets, from mjd-15
  77.  * to mjd-13, and bulletin A Vol. XXVI No. 040 from 2013-10-03 contained no
  78.  * rapid service pole offsets at all, it contained only final values. Despite
  79.  * this irregularity, rapid service data is continuous over consecutive files,
  80.  * so the mean number of entries is 7 as the files are published on a weekly
  81.  * basis.
  82.  * </p>
  83.  * <p>
  84.  * There are no prediction data for pole offsets.
  85.  * </p>
  86.  * <p>
  87.  * This loader reads both the rapid service, the prediction and the final
  88.  * values parts. As successive files have overlaps between all these sections,
  89.  * values extracted from latest files (with respect to the covered dates)
  90.  * override values extracted from earlier files, regardless of the files
  91.  * reading order. If numerous bulletins A covering more than one year are read,
  92.  * one particular date will typically appear in the prediction section of
  93.  * 52 or 53 files, then in the rapid data section of one file, then it will
  94.  * be missing in a few files, and will finally appear a last time in the
  95.  * final values sections of a last file. In this case, the value retained
  96.  * will be the one extracted from the final values section in the more
  97.  * recent file.
  98.  * </p>
  99.  * <p>
  100.  * If only one bulletin A file is read and it correspond to the first bulletin
  101.  * of a month, it will have a roughly one month wide hole between the
  102.  * final data and the rapid data. This hole will trigger an error as EOP
  103.  * continuity is checked by default for at most 5 days holes. In this case,
  104.  * users should call something like {@link Frames#setEOPContinuityThreshold(double)
  105.  * FramesFactory.setEOPContinuityThreshold(Constants.JULIAN_YEAR)} to prevent
  106.  * the error to be triggered.
  107.  * </p>
  108.  * <p>The bulletin A files are recognized thanks to their base names,
  109.  * which must match the pattern <code>bulletina-xxxx-###.txt</code>,
  110.  * (or the same ending with <code>.gz</code> for gzip-compressed files)
  111.  * where x stands for a roman numeral character and # stands for a digit
  112.  * character.</p>
  113.  * <p>
  114.  * Bulletin A in csv format must be read using {@link EopCsvFilesLoader} rather
  115.  * than using this loader. Bulletin A in xml format must be read using {@link EopXmlLoader}
  116.  * rather than using this loader.
  117.  * </p>
  118.  * <p>
  119.  * This class is immutable and hence thread-safe
  120.  * </p>
  121.  * @author Luc Maisonobe
  122.  * @since 7.0
  123.  * @see EopCsvFilesLoader
  124.  * @see EopXmlLoader
  125.  */
  126. class BulletinAFilesLoader extends AbstractEopLoader implements EopHistoryLoader {

  127.     /** Regular expression matching blanks at start of line. */
  128.     private static final String LINE_START_REGEXP     = "^\\p{Blank}+";

  129.     /** Regular expression matching blanks at end of line. */
  130.     private static final String LINE_END_REGEXP       = "\\p{Blank}*$";

  131.     /** Regular expression matching integers. */
  132.     private static final String INTEGER_REGEXP        = "[-+]?\\p{Digit}+";

  133.     /** Regular expression matching real numbers. */
  134.     private static final String REAL_REGEXP           = "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";

  135.     /** Regular expression matching an integer field to store. */
  136.     private static final String STORED_INTEGER_FIELD  = "\\p{Blank}*(" + INTEGER_REGEXP + ")";

  137.     /** regular expression matching a Modified Julian Day field to store. */
  138.     private static final String STORED_MJD_FIELD      = "\\p{Blank}+(\\p{Digit}\\p{Digit}\\p{Digit}\\p{Digit}\\p{Digit})";

  139.     /** Regular expression matching a real field to store. */
  140.     private static final String STORED_REAL_FIELD     = "\\p{Blank}+(" + REAL_REGEXP + ")";

  141.     /** Regular expression matching a real field to ignore. */
  142.     private static final String IGNORED_REAL_FIELD    = "\\p{Blank}+" + REAL_REGEXP;

  143.     /** Enum for files sections, in expected order.
  144.      * <p>The bulletin A weekly data files contain several sections,
  145.      * each introduced with some fixed header text and followed by tabular data.
  146.      * </p>
  147.      */
  148.     private enum Section {

  149.         /** Earth Orientation Parameters rapid service. */
  150.         // section 2 always contain rapid service data including error fields
  151.         //      COMBINED EARTH ORIENTATION PARAMETERS:
  152.         //
  153.         //                              IERS Rapid Service
  154.         //              MJD      x    error     y    error   UT1-UTC   error
  155.         //                       "      "       "      "        s        s
  156.         //   13  8 30  56534 0.16762 .00009 0.32705 .00009  0.038697 0.000019
  157.         //   13  8 31  56535 0.16669 .00010 0.32564 .00010  0.038471 0.000019
  158.         //   13  9  1  56536 0.16592 .00009 0.32410 .00010  0.038206 0.000024
  159.         //   13  9  2  56537 0.16557 .00009 0.32270 .00009  0.037834 0.000024
  160.         //   13  9  3  56538 0.16532 .00009 0.32147 .00010  0.037351 0.000024
  161.         //   13  9  4  56539 0.16488 .00009 0.32044 .00010  0.036756 0.000023
  162.         //   13  9  5  56540 0.16435 .00009 0.31948 .00009  0.036036 0.000024
  163.         EOP_RAPID_SERVICE("^ *COMBINED EARTH ORIENTATION PARAMETERS: *$",
  164.                           LINE_START_REGEXP +
  165.                           STORED_INTEGER_FIELD + STORED_INTEGER_FIELD + STORED_INTEGER_FIELD +
  166.                           STORED_MJD_FIELD +
  167.                           STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  168.                           STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  169.                           STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  170.                           LINE_END_REGEXP),

  171.         /** Earth Orientation Parameters final values. */
  172.         // the first bulletin A of each month also includes final values for the
  173.         // period covering from day 2 of month m-2 to day 1 of month m-1.
  174.         //                                IERS Final Values
  175.         //                                 MJD        x        y      UT1-UTC
  176.         //                                            "        "         s
  177.         //             13  7  2           56475    0.1441   0.3901   0.05717
  178.         //             13  7  3           56476    0.1457   0.3895   0.05716
  179.         //             13  7  4           56477    0.1467   0.3887   0.05728
  180.         //             13  7  5           56478    0.1477   0.3875   0.05755
  181.         //             13  7  6           56479    0.1490   0.3862   0.05793
  182.         //             13  7  7           56480    0.1504   0.3849   0.05832
  183.         //             13  7  8           56481    0.1516   0.3835   0.05858
  184.         //             13  7  9           56482    0.1530   0.3822   0.05877
  185.         EOP_FINAL_VALUES("^ *IERS Final Values *$",
  186.                          LINE_START_REGEXP +
  187.                          STORED_INTEGER_FIELD + STORED_INTEGER_FIELD + STORED_INTEGER_FIELD +
  188.                          STORED_MJD_FIELD +
  189.                          STORED_REAL_FIELD +
  190.                          STORED_REAL_FIELD +
  191.                          STORED_REAL_FIELD +
  192.                          LINE_END_REGEXP),

  193.         /** Earth Orientation Parameters prediction. */
  194.         // section 3 always contain prediction data without error fields
  195.         //
  196.         //         PREDICTIONS:
  197.         //         The following formulas will not reproduce the predictions given below,
  198.         //         but may be used to extend the predictions beyond the end of this table.
  199.         //
  200.         //         x =  0.0969 + 0.1110 cos A - 0.0103 sin A - 0.0435 cos C - 0.0171 sin C
  201.         //         y =  0.3457 - 0.0061 cos A - 0.1001 sin A - 0.0171 cos C + 0.0435 sin C
  202.         //            UT1-UTC = -0.0052 - 0.00104 (MJD - 56548) - (UT2-UT1)
  203.         //
  204.         //         where A = 2*pi*(MJD-56540)/365.25 and C = 2*pi*(MJD-56540)/435.
  205.         //
  206.         //            TAI-UTC(MJD 56541) = 35.0
  207.         //         The accuracy may be estimated from the expressions:
  208.         //         S x,y = 0.00068 (MJD-56540)**0.80   S t = 0.00025 (MJD-56540)**0.75
  209.         //         Estimated accuracies are:  Predictions     10 d   20 d   30 d   40 d
  210.         //                                    Polar coord's  0.004  0.007  0.010  0.013
  211.         //                                    UT1-UTC        0.0014 0.0024 0.0032 0.0040
  212.         //
  213.         //                       MJD      x(arcsec)   y(arcsec)   UT1-UTC(sec)
  214.         //          2013  9  6  56541       0.1638      0.3185      0.03517
  215.         //          2013  9  7  56542       0.1633      0.3175      0.03420
  216.         //          2013  9  8  56543       0.1628      0.3164      0.03322
  217.         //          2013  9  9  56544       0.1623      0.3153      0.03229
  218.         //          2013  9 10  56545       0.1618      0.3142      0.03144
  219.         //          2013  9 11  56546       0.1612      0.3131      0.03071
  220.         //          2013  9 12  56547       0.1607      0.3119      0.03008
  221.         EOP_PREDICTION("^ *PREDICTIONS: *$",
  222.                        LINE_START_REGEXP +
  223.                        STORED_INTEGER_FIELD + STORED_INTEGER_FIELD + STORED_INTEGER_FIELD +
  224.                        STORED_MJD_FIELD +
  225.                        STORED_REAL_FIELD +
  226.                        STORED_REAL_FIELD +
  227.                        STORED_REAL_FIELD +
  228.                        LINE_END_REGEXP),

  229.         /** Pole offsets, IAU-1980. */
  230.         // section 4 may contain rapid service pole offset series including error fields
  231.         //        CELESTIAL POLE OFFSET SERIES:
  232.         //                             NEOS Celestial Pole Offset Series
  233.         //                         MJD      dpsi    error     deps    error
  234.         //                                          (msec. of arc)
  235.         //                        56519   -87.47     0.13   -12.96     0.08
  236.         //                        56520   -87.72     0.13   -13.20     0.08
  237.         //                        56521   -87.79     0.19   -13.56     0.11
  238.         POLE_OFFSETS_IAU_1980_RAPID_SERVICE("^ *NEOS Celestial Pole Offset Series *$",
  239.                                             LINE_START_REGEXP +
  240.                                             STORED_MJD_FIELD +
  241.                                             STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  242.                                             STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  243.                                             LINE_END_REGEXP),

  244.         /** Pole offsets, IAU-1980 final values. */
  245.         // the first bulletin A of each month also includes final values for the
  246.         // period covering from day 2 of month m-2 to day 1 of month m-1.
  247.         //                    IERS Celestial Pole Offset Final Series
  248.         //                          MJD          dpsi      deps
  249.         //                                       (msec. of arc)
  250.         //                         56475       -81.0     -13.3
  251.         //                         56476       -81.2     -13.4
  252.         //                         56477       -81.6     -13.4
  253.         //                         56478       -82.2     -13.5
  254.         //                         56479       -82.5     -13.6
  255.         //                         56480       -82.5     -13.7
  256.         POLE_OFFSETS_IAU_1980_FINAL_VALUES("^ *IERS Celestial Pole Offset Final Series *$",
  257.                                            LINE_START_REGEXP +
  258.                                            STORED_MJD_FIELD +
  259.                                            STORED_REAL_FIELD +
  260.                                            STORED_REAL_FIELD +
  261.                                            LINE_END_REGEXP),

  262.         /** Pole offsets, IAU-2000. */
  263.         // the format for the IAU-2000 series is similar, but the meanings of the fields
  264.         // are different
  265.         //                       IAU2000A Celestial Pole Offset Series
  266.         //                        MJD      dX     error     dY     error
  267.         //                                      (msec. of arc)
  268.         //                       56519   -0.246   0.052   -0.223   0.080
  269.         //                       56520   -0.239   0.052   -0.248   0.080
  270.         //                       56521   -0.224   0.076   -0.277   0.110
  271.         POLE_OFFSETS_IAU_2000_RAPID_SERVICE("^ *IAU2000A Celestial Pole Offset Series *$",
  272.                                             LINE_START_REGEXP +
  273.                                             STORED_MJD_FIELD +
  274.                                             STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  275.                                             STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  276.                                             LINE_END_REGEXP),

  277.         /** Pole offsets, IAU-2000 final values. */
  278.         // the format for the IAU-2000 series is similar, but the meanings of the fields
  279.         // are different
  280.         //                   IAU2000A Celestial Pole Offset Final Series
  281.         //                            MJD     dX         dY
  282.         //                            (msec. of arc)
  283.         //                          56475     0.00      -0.28
  284.         //                          56476    -0.06      -0.29
  285.         //                          56477    -0.07      -0.27
  286.         //                          56478    -0.12      -0.33
  287.         //                          56479    -0.12      -0.33
  288.         //                          56480    -0.13      -0.36
  289.         POLE_OFFSETS_IAU_2000_FINAL_VALUES("^ *IAU2000A Celestial Pole Offset Final Series *$",
  290.                                            LINE_START_REGEXP +
  291.                                            STORED_MJD_FIELD +
  292.                                            STORED_REAL_FIELD +
  293.                                            STORED_REAL_FIELD +
  294.                                            LINE_END_REGEXP);

  295.         /** Header pattern. */
  296.         private final Pattern header;

  297.         /** Data pattern. */
  298.         private final Pattern data;

  299.         /** Simple constructor.
  300.          * @param headerRegExp regular expression for header
  301.          * @param dataRegExp regular expression for data
  302.          */
  303.         Section(final String headerRegExp, final String dataRegExp) {
  304.             this.header = Pattern.compile(headerRegExp);
  305.             this.data   = Pattern.compile(dataRegExp);
  306.         }

  307.         /** Check if a line matches the section header.
  308.          * @param line line to check
  309.          * @return true if the line matches the header
  310.          */
  311.         public boolean matchesHeader(final String line) {
  312.             return header.matcher(line).matches();
  313.         }

  314.         /** Get the data fields from a line.
  315.          * @param line line to parse
  316.          * @return extracted fields, or null if line does not match data format
  317.          */
  318.         public String[] getFields(final String line) {
  319.             final Matcher matcher = data.matcher(line);
  320.             if (matcher.matches()) {
  321.                 final String[] fields = new String[matcher.groupCount()];
  322.                 for (int i = 0; i < fields.length; ++i) {
  323.                     fields[i] = matcher.group(i + 1);
  324.                 }
  325.                 return fields;
  326.             } else {
  327.                 return null;
  328.             }
  329.         }

  330.     }

  331.     /** Build a loader for IERS bulletins A files.
  332.      * @param supportedNames regular expression for supported files names
  333.      * @param manager provides access to the bulletin A files.
  334.      * @param utcSupplier UTC time scale.
  335.      */
  336.     BulletinAFilesLoader(final String supportedNames,
  337.                          final DataProvidersManager manager,
  338.                          final Supplier<TimeScale> utcSupplier) {
  339.         super(supportedNames, manager, utcSupplier);
  340.     }

  341.     /** {@inheritDoc} */
  342.     public void fillHistory(final IERSConventions.NutationCorrectionConverter converter,
  343.                             final SortedSet<EOPEntry> history) {
  344.         final Parser parser = new Parser();
  345.         this.feed(parser);
  346.         parser.fill(history);
  347.     }

  348.     /** Internal class performing the parsing. */
  349.     private class Parser implements DataLoader {

  350.         /** Map for xp, yp, dut1 fields read in different sections. */
  351.         private final Map<Integer, double[]> eopFieldsMap;

  352.         /** Map for pole offsets fields read in different sections. */
  353.         private final Map<Integer, double[]> poleOffsetsFieldsMap;

  354.         /** Configuration for ITRF versions. */
  355.         private final ItrfVersionProvider itrfVersionProvider;

  356.         /** ITRF version configuration. */
  357.         private ITRFVersionLoader.ITRFVersionConfiguration configuration;

  358.         /** File name. */
  359.         private String fileName;

  360.         /** Current line number. */
  361.         private int lineNumber;

  362.         /** Current line. */
  363.         private String line;

  364.         /** Earliest parsed data. */
  365.         private int mjdMin;

  366.         /** Latest parsed data. */
  367.         private int mjdMax;

  368.         /** First MJD parsed in current file. */
  369.         private int firstMJD;

  370.         /** Simple constructor.
  371.          */
  372.         Parser() {
  373.             this.eopFieldsMap         = new HashMap<>();
  374.             this.poleOffsetsFieldsMap = new HashMap<>();
  375.             this.itrfVersionProvider = new ITRFVersionLoader(
  376.                     ITRFVersionLoader.SUPPORTED_NAMES,
  377.                     getDataProvidersManager());
  378.             this.lineNumber           = 0;
  379.             this.mjdMin               = Integer.MAX_VALUE;
  380.             this.mjdMax               = Integer.MIN_VALUE;
  381.             this.firstMJD             = -1;
  382.         }

  383.         /** {@inheritDoc} */
  384.         public boolean stillAcceptsData() {
  385.             return true;
  386.         }

  387.         /** {@inheritDoc} */
  388.         public void loadData(final InputStream input, final String name)
  389.             throws IOException {

  390.             this.configuration = null;
  391.             this.fileName      = name;

  392.             // set up a reader for line-oriented bulletin A files
  393.             try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
  394.                 lineNumber =  0;
  395.                 firstMJD   = -1;

  396.                 // loop over sections
  397.                 final List<Section> remaining = new ArrayList<>(Arrays.asList(Section.values()));
  398.                 for (Section section = nextSection(remaining, reader);
  399.                      section != null;
  400.                      section = nextSection(remaining, reader)) {

  401.                     switch (section) {
  402.                         case EOP_RAPID_SERVICE :
  403.                         case EOP_FINAL_VALUES  :
  404.                         case EOP_PREDICTION    :
  405.                             loadXYDT(section, reader, name);
  406.                             break;
  407.                         case POLE_OFFSETS_IAU_1980_RAPID_SERVICE :
  408.                         case POLE_OFFSETS_IAU_1980_FINAL_VALUES  :
  409.                             loadPoleOffsets(section, false, reader, name);
  410.                             break;
  411.                         case POLE_OFFSETS_IAU_2000_RAPID_SERVICE :
  412.                         case POLE_OFFSETS_IAU_2000_FINAL_VALUES  :
  413.                             loadPoleOffsets(section, true, reader, name);
  414.                             break;
  415.                         default :
  416.                             // this should never happen
  417.                             throw new OrekitInternalError(null);
  418.                     }

  419.                     // remove the already parsed section from the list
  420.                     remaining.remove(section);

  421.                 }

  422.                 // check that the mandatory sections have been parsed
  423.                 if (remaining.contains(Section.EOP_RAPID_SERVICE) ||
  424.                     remaining.contains(Section.EOP_PREDICTION) ||
  425.                     (remaining.contains(Section.POLE_OFFSETS_IAU_1980_RAPID_SERVICE) ^
  426.                      remaining.contains(Section.POLE_OFFSETS_IAU_2000_RAPID_SERVICE)) ||
  427.                     (remaining.contains(Section.POLE_OFFSETS_IAU_1980_FINAL_VALUES) ^
  428.                      remaining.contains(Section.POLE_OFFSETS_IAU_2000_FINAL_VALUES))) {
  429.                     throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_IERS_DATA_FILE, name);
  430.                 }

  431.             }
  432.         }

  433.         /** Fill EOP history obtained after reading several files.
  434.          * @param history history to fill up
  435.          */
  436.         public void fill(final SortedSet<EOPEntry> history) {

  437.             double[] currentEOP = null;
  438.             double[] nextEOP    = eopFieldsMap.get(mjdMin);
  439.             for (int mjd = mjdMin; mjd <= mjdMax; ++mjd) {

  440.                 final AbsoluteDate mjdDate = AbsoluteDate.createMJDDate(mjd, 0, getUtc());
  441.                 final double[] currentPole = poleOffsetsFieldsMap.get(mjd);

  442.                 currentEOP = nextEOP;
  443.                 nextEOP    = eopFieldsMap.get(mjd + 1);

  444.                 if (currentEOP == null) {
  445.                     if (currentPole != null) {
  446.                         // we have only pole offsets for this date
  447.                         if (configuration == null || !configuration.isValid(mjd)) {
  448.                             // get a configuration for current name and date range
  449.                             configuration = itrfVersionProvider.getConfiguration(fileName, mjd);
  450.                         }
  451.                         history.add(new EOPEntry(mjd,
  452.                                                  0.0, Double.NaN, 0.0, 0.0, Double.NaN, Double.NaN,
  453.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[1]),
  454.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[2]),
  455.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[3]),
  456.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[4]),
  457.                                                  configuration.getVersion(),
  458.                                                  mjdDate));
  459.                     }
  460.                 } else {

  461.                     if (configuration == null || !configuration.isValid(mjd)) {
  462.                         // get a configuration for current name and date range
  463.                         configuration = itrfVersionProvider.getConfiguration(fileName, mjd);
  464.                     }
  465.                     if (currentPole == null) {
  466.                         // we have only EOP for this date
  467.                         history.add(new EOPEntry(mjd,
  468.                                                  currentEOP[3], Double.NaN,
  469.                                                  UnitsConverter.ARC_SECONDS_TO_RADIANS.convert(currentEOP[1]),
  470.                                                  UnitsConverter.ARC_SECONDS_TO_RADIANS.convert(currentEOP[2]),
  471.                                                  Double.NaN, Double.NaN,
  472.                                                  0.0, 0.0, 0.0, 0.0,
  473.                                                  configuration.getVersion(),
  474.                                                  mjdDate));
  475.                     } else {
  476.                         // we have complete data
  477.                         history.add(new EOPEntry(mjd,
  478.                                                  currentEOP[3], Double.NaN,
  479.                                                  UnitsConverter.ARC_SECONDS_TO_RADIANS.convert(currentEOP[1] ),
  480.                                                  UnitsConverter.ARC_SECONDS_TO_RADIANS.convert(currentEOP[2] ),
  481.                                                  Double.NaN, Double.NaN,
  482.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[1]),
  483.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[2]),
  484.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[3]),
  485.                                                  UnitsConverter.MILLI_ARC_SECONDS_TO_RADIANS.convert(currentPole[4]),
  486.                                                  configuration.getVersion(),
  487.                                                  mjdDate));
  488.                     }
  489.                 }

  490.             }

  491.         }

  492.         /** Skip to next section header.
  493.          * @param sections sections to check for
  494.          * @param reader reader from where file content is obtained
  495.          * @return the next section or null if no section is found until end of file
  496.          * @exception IOException if data can't be read
  497.          */
  498.         private Section nextSection(final List<Section> sections,
  499.                                     final BufferedReader reader)
  500.             throws IOException {

  501.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  502.                 ++lineNumber;
  503.                 for (Section section : sections) {
  504.                     if (section.matchesHeader(line)) {
  505.                         return section;
  506.                     }
  507.                 }
  508.             }

  509.             // we have reached end of file and not found a matching section header
  510.             return null;

  511.         }

  512.         /** Read X, Y, UT1-UTC.
  513.          * @param section section to parse
  514.          * @param reader reader from where file content is obtained
  515.          * @param name name of the file (or zip entry)
  516.          * @exception IOException if data can't be read
  517.          */
  518.         private void loadXYDT(final Section section, final BufferedReader reader, final String name)
  519.             throws IOException {

  520.             boolean inValuesPart = false;
  521.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  522.                 lineNumber++;
  523.                 final String[] fields = section.getFields(line);
  524.                 if (fields != null) {

  525.                     // we are within the values part
  526.                     inValuesPart = true;

  527.                     // this is a data line, build an entry from the extracted fields
  528.                     final int year  = Integer.parseInt(fields[0]);
  529.                     final int month = Integer.parseInt(fields[1]);
  530.                     final int day   = Integer.parseInt(fields[2]);
  531.                     final int mjd   = Integer.parseInt(fields[3]);
  532.                     final DateComponents dc = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd);
  533.                     if ((dc.getYear() % 100) != (year % 100) ||
  534.                          dc.getMonth() != month ||
  535.                          dc.getDay() != day) {
  536.                         throw new OrekitException(OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE,
  537.                                                   name, year, month, day, mjd);
  538.                     }
  539.                     mjdMin = FastMath.min(mjdMin, mjd);
  540.                     mjdMax = FastMath.max(mjdMax, mjd);
  541.                     if (firstMJD < 0) {
  542.                         // store the first mjd parsed
  543.                         firstMJD = mjd;
  544.                     }

  545.                     // get the entry at the same date if it was already parsed
  546.                     final double[] eop;
  547.                     if (eopFieldsMap.containsKey(mjd)) {
  548.                         eop = eopFieldsMap.get(mjd);
  549.                     } else {
  550.                         eop = new double[4];
  551.                         eopFieldsMap.put(mjd, eop);
  552.                     }

  553.                     if (eop[0] <= firstMJD) {
  554.                         // either it is the first time we parse this date (eop[0] = 0),
  555.                         // or the new parsed data is from a more recent file
  556.                         // in both case, we should update the array
  557.                         eop[0] = firstMJD;
  558.                         eop[1] = Double.parseDouble(fields[4]);
  559.                         eop[2] = Double.parseDouble(fields[5]);
  560.                         eop[3] = Double.parseDouble(fields[6]);
  561.                     }

  562.                 } else if (inValuesPart) {
  563.                     // we leave values part
  564.                     return;
  565.                 }
  566.             }

  567.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  568.                                       name, lineNumber);

  569.         }

  570.         /** Read EOP data.
  571.          * @param section section to parse
  572.          * @param isNonRotatingOrigin if true, the file contain Non-Rotating Origin nutation corrections
  573.          * @param reader reader from where file content is obtained
  574.          * @param name name of the file (or zip entry)
  575.          * @exception IOException if data can't be read
  576.          */
  577.         private void loadPoleOffsets(final Section section, final boolean isNonRotatingOrigin,
  578.                                      final BufferedReader reader, final String name)
  579.             throws IOException {

  580.             boolean inValuesPart = false;
  581.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  582.                 lineNumber++;
  583.                 final String[] fields = section.getFields(line);
  584.                 if (fields != null) {

  585.                     // we are within the values part
  586.                     inValuesPart = true;

  587.                     // this is a data line, build an entry from the extracted fields
  588.                     final int mjd = Integer.parseInt(fields[0]);
  589.                     mjdMin = FastMath.min(mjdMin, mjd);
  590.                     mjdMax = FastMath.max(mjdMax, mjd);

  591.                     // get the entry at the same date if it was already parsed
  592.                     final double[] pole;
  593.                     if (poleOffsetsFieldsMap.containsKey(mjd)) {
  594.                         pole = poleOffsetsFieldsMap.get(mjd);
  595.                     } else {
  596.                         pole = new double[5];
  597.                         poleOffsetsFieldsMap.put(mjd, pole);
  598.                     }

  599.                     if (pole[0] <= firstMJD) {
  600.                         // either it is the first time we parse this date (pole[0] = 0),
  601.                         // or the new parsed data is from a more recent file
  602.                         // in both case, we should update the array
  603.                         pole[0] = firstMJD;
  604.                         if (isNonRotatingOrigin) {
  605.                             pole[1] = Double.parseDouble(fields[1]);
  606.                             pole[2] = Double.parseDouble(fields[2]);
  607.                         } else {
  608.                             pole[3] = Double.parseDouble(fields[1]);
  609.                             pole[4] = Double.parseDouble(fields[2]);
  610.                         }
  611.                     }

  612.                 } else if (inValuesPart) {
  613.                     // we leave values part
  614.                     return;
  615.                 }
  616.             }

  617.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  618.                                       name, lineNumber);

  619.         }

  620.     }

  621. }