BulletinAFilesLoader.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.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.Constants;
  42. import org.orekit.utils.IERSConventions;

  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.  * This class is immutable and hence thread-safe
  115.  * </p>
  116.  * @author Luc Maisonobe
  117.  * @since 7.0
  118.  */
  119. class BulletinAFilesLoader extends AbstractEopLoader implements EOPHistoryLoader {

  120.     /** Conversion factor. */
  121.     private static final double MILLI_ARC_SECONDS_TO_RADIANS = Constants.ARC_SECONDS_TO_RADIANS / 1000;

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

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

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

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

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

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

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

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

  138.     /** Enum for files sections, in expected order.
  139.      * <p>The bulletin A weekly data files contain several sections,
  140.      * each introduced with some fixed header text and followed by tabular data.
  141.      * </p>
  142.      */
  143.     private enum Section {

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

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

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

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

  239.         /** Pole offsets, IAU-1980 final values. */
  240.         // the format for the IAU-2000 series is similar, but the meanings of the fields
  241.         // are different
  242.         //                       IAU2000A Celestial Pole Offset Series
  243.         //                        MJD      dX     error     dY     error
  244.         //                                      (msec. of arc)
  245.         //                       56519   -0.246   0.052   -0.223   0.080
  246.         //                       56520   -0.239   0.052   -0.248   0.080
  247.         //                       56521   -0.224   0.076   -0.277   0.110
  248.         POLE_OFFSETS_IAU_1980_FINAL_VALUES("^ *IERS Celestial Pole Offset Final Series *$",
  249.                                            LINE_START_REGEXP +
  250.                                            STORED_MJD_FIELD +
  251.                                            STORED_REAL_FIELD +
  252.                                            STORED_REAL_FIELD +
  253.                                            LINE_END_REGEXP),

  254.         /** Pole offsets, IAU-2000. */
  255.         // the first bulletin A of each month also includes final values for the
  256.         // period covering from day 2 of month m-2 to day 1 of month m-1.
  257.         //                    IERS Celestial Pole Offset Final Series
  258.         //                          MJD          dpsi      deps
  259.         //                                       (msec. of arc)
  260.         //                         56475       -81.0     -13.3
  261.         //                         56476       -81.2     -13.4
  262.         //                         56477       -81.6     -13.4
  263.         //                         56478       -82.2     -13.5
  264.         //                         56479       -82.5     -13.6
  265.         //                         56480       -82.5     -13.7
  266.         POLE_OFFSETS_IAU_2000_RAPID_SERVICE("^ *IAU2000A Celestial Pole Offset Series *$",
  267.                                             LINE_START_REGEXP +
  268.                                             STORED_MJD_FIELD +
  269.                                             STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  270.                                             STORED_REAL_FIELD + IGNORED_REAL_FIELD +
  271.                                             LINE_END_REGEXP),

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

  290.         /** Header pattern. */
  291.         private final Pattern header;

  292.         /** Data pattern. */
  293.         private final Pattern data;

  294.         /** Simple constructor.
  295.          * @param headerRegExp regular expression for header
  296.          * @param dataRegExp regular expression for data
  297.          */
  298.         Section(final String headerRegExp, final String dataRegExp) {
  299.             this.header = Pattern.compile(headerRegExp);
  300.             this.data   = Pattern.compile(dataRegExp);
  301.         }

  302.         /** Check if a line matches the section header.
  303.          * @param line line to check
  304.          * @return true if the line matches the header
  305.          */
  306.         public boolean matchesHeader(final String line) {
  307.             return header.matcher(line).matches();
  308.         }

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

  325.     }

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

  336.     /** {@inheritDoc} */
  337.     public void fillHistory(final IERSConventions.NutationCorrectionConverter converter,
  338.                             final SortedSet<EOPEntry> history) {
  339.         final Parser parser = new Parser();
  340.         this.feed(parser);
  341.         parser.fill(history);
  342.     }

  343.     /** Internal class performing the parsing. */
  344.     private class Parser implements DataLoader {

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

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

  349.         /** Configuration for ITRF versions. */
  350.         private final ItrfVersionProvider itrfVersionProvider;

  351.         /** ITRF version configuration. */
  352.         private ITRFVersionLoader.ITRFVersionConfiguration configuration;

  353.         /** File name. */
  354.         private String fileName;

  355.         /** Current line number. */
  356.         private int lineNumber;

  357.         /** Current line. */
  358.         private String line;

  359.         /** Earliest parsed data. */
  360.         private int mjdMin;

  361.         /** Latest parsed data. */
  362.         private int mjdMax;

  363.         /** First MJD parsed in current file. */
  364.         private int firstMJD;

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

  378.         /** {@inheritDoc} */
  379.         public boolean stillAcceptsData() {
  380.             return true;
  381.         }

  382.         /** {@inheritDoc} */
  383.         public void loadData(final InputStream input, final String name)
  384.             throws IOException {

  385.             this.configuration = null;
  386.             this.fileName      = name;

  387.             // set up a reader for line-oriented bulletin A files
  388.             try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
  389.                 lineNumber =  0;
  390.                 firstMJD   = -1;

  391.                 // loop over sections
  392.                 final List<Section> remaining = new ArrayList<>(Arrays.asList(Section.values()));
  393.                 for (Section section = nextSection(remaining, reader);
  394.                      section != null;
  395.                      section = nextSection(remaining, reader)) {

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

  414.                     // remove the already parsed section from the list
  415.                     remaining.remove(section);

  416.                 }

  417.                 // check that the mandatory sections have been parsed
  418.                 if (remaining.contains(Section.EOP_RAPID_SERVICE) ||
  419.                     remaining.contains(Section.EOP_PREDICTION) ||
  420.                     (remaining.contains(Section.POLE_OFFSETS_IAU_1980_RAPID_SERVICE) ^
  421.                      remaining.contains(Section.POLE_OFFSETS_IAU_2000_RAPID_SERVICE)) ||
  422.                     (remaining.contains(Section.POLE_OFFSETS_IAU_1980_FINAL_VALUES) ^
  423.                      remaining.contains(Section.POLE_OFFSETS_IAU_2000_FINAL_VALUES))) {
  424.                     throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_IERS_DATA_FILE, name);
  425.                 }

  426.             }
  427.         }

  428.         /** Fill EOP history obtained after reading several files.
  429.          * @param history history to fill up
  430.          */
  431.         public void fill(final SortedSet<EOPEntry> history) {

  432.             double[] currentEOP = null;
  433.             double[] nextEOP    = eopFieldsMap.get(mjdMin);
  434.             for (int mjd = mjdMin; mjd <= mjdMax; ++mjd) {

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

  437.                 final double[] previousEOP = currentEOP;
  438.                 currentEOP = nextEOP;
  439.                 nextEOP    = eopFieldsMap.get(mjd + 1);

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

  457.                     // compute LOD as the opposite of the time derivative of UT1-UTC
  458.                     final double lod;
  459.                     if (previousEOP == null) {
  460.                         if (nextEOP == null) {
  461.                             // isolated point
  462.                             lod = 0;
  463.                         } else {
  464.                             // first entry, we use a forward difference
  465.                             lod = currentEOP[3] - nextEOP[3];
  466.                         }
  467.                     } else {
  468.                         if (nextEOP == null) {
  469.                             // last entry, we use a backward difference
  470.                             lod = previousEOP[3] - currentEOP[3];
  471.                         } else {
  472.                             // regular entry, we use a centered difference
  473.                             lod = 0.5 * (previousEOP[3] - nextEOP[3]);
  474.                         }
  475.                     }

  476.                     if (configuration == null || !configuration.isValid(mjd)) {
  477.                         // get a configuration for current name and date range
  478.                         configuration = itrfVersionProvider.getConfiguration(fileName, mjd);
  479.                     }
  480.                     if (currentPole == null) {
  481.                         // we have only EOP for this date
  482.                         history.add(new EOPEntry(mjd,
  483.                                                  currentEOP[3], lod,
  484.                                                  currentEOP[1] * Constants.ARC_SECONDS_TO_RADIANS,
  485.                                                  currentEOP[2] * Constants.ARC_SECONDS_TO_RADIANS,
  486.                                                  0.0, 0.0, 0.0, 0.0,
  487.                                                  configuration.getVersion(),
  488.                                                  mjdDate));
  489.                     } else {
  490.                         // we have complete data
  491.                         history.add(new EOPEntry(mjd,
  492.                                                  currentEOP[3], lod,
  493.                                                  currentEOP[1]  * Constants.ARC_SECONDS_TO_RADIANS,
  494.                                                  currentEOP[2]  * Constants.ARC_SECONDS_TO_RADIANS,
  495.                                                  currentPole[1] * MILLI_ARC_SECONDS_TO_RADIANS,
  496.                                                  currentPole[2] * MILLI_ARC_SECONDS_TO_RADIANS,
  497.                                                  currentPole[3] * MILLI_ARC_SECONDS_TO_RADIANS,
  498.                                                  currentPole[4] * MILLI_ARC_SECONDS_TO_RADIANS,
  499.                                                  configuration.getVersion(),
  500.                                                  mjdDate));
  501.                     }
  502.                 }

  503.             }

  504.         }

  505.         /** Skip to next section header.
  506.          * @param sections sections to check for
  507.          * @param reader reader from where file content is obtained
  508.          * @return the next section or null if no section is found until end of file
  509.          * @exception IOException if data can't be read
  510.          */
  511.         private Section nextSection(final List<Section> sections,
  512.                                     final BufferedReader reader)
  513.             throws IOException {

  514.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  515.                 ++lineNumber;
  516.                 for (Section section : sections) {
  517.                     if (section.matchesHeader(line)) {
  518.                         return section;
  519.                     }
  520.                 }
  521.             }

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

  524.         }

  525.         /** Read X, Y, UT1-UTC.
  526.          * @param section section to parse
  527.          * @param reader reader from where file content is obtained
  528.          * @param name name of the file (or zip entry)
  529.          * @exception IOException if data can't be read
  530.          */
  531.         private void loadXYDT(final Section section, final BufferedReader reader, final String name)
  532.             throws IOException {

  533.             boolean inValuesPart = false;
  534.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  535.                 lineNumber++;
  536.                 final String[] fields = section.getFields(line);
  537.                 if (fields != null) {

  538.                     // we are within the values part
  539.                     inValuesPart = true;

  540.                     // this is a data line, build an entry from the extracted fields
  541.                     final int year  = Integer.parseInt(fields[0]);
  542.                     final int month = Integer.parseInt(fields[1]);
  543.                     final int day   = Integer.parseInt(fields[2]);
  544.                     final int mjd   = Integer.parseInt(fields[3]);
  545.                     final DateComponents dc = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd);
  546.                     if ((dc.getYear() % 100) != (year % 100) ||
  547.                          dc.getMonth() != month ||
  548.                          dc.getDay() != day) {
  549.                         throw new OrekitException(OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE,
  550.                                                   name, year, month, day, mjd);
  551.                     }
  552.                     mjdMin = FastMath.min(mjdMin, mjd);
  553.                     mjdMax = FastMath.max(mjdMax, mjd);
  554.                     if (firstMJD < 0) {
  555.                         // store the first mjd parsed
  556.                         firstMJD = mjd;
  557.                     }

  558.                     // get the entry at the same date if it was already parsed
  559.                     final double[] eop;
  560.                     if (eopFieldsMap.containsKey(mjd)) {
  561.                         eop = eopFieldsMap.get(mjd);
  562.                     } else {
  563.                         eop = new double[4];
  564.                         eopFieldsMap.put(mjd, eop);
  565.                     }

  566.                     if (eop[0] <= firstMJD) {
  567.                         // either it is the first time we parse this date (eop[0] = 0),
  568.                         // or the new parsed data is from a more recent file
  569.                         // in both case, we should update the array
  570.                         eop[0] = firstMJD;
  571.                         eop[1] = Double.parseDouble(fields[4]);
  572.                         eop[2] = Double.parseDouble(fields[5]);
  573.                         eop[3] = Double.parseDouble(fields[6]);
  574.                     }

  575.                 } else if (inValuesPart) {
  576.                     // we leave values part
  577.                     return;
  578.                 }
  579.             }

  580.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  581.                                       name, lineNumber);

  582.         }

  583.         /** Read EOP data.
  584.          * @param section section to parse
  585.          * @param isNonRotatingOrigin if true, the file contain Non-Rotating Origin nutation corrections
  586.          * @param reader reader from where file content is obtained
  587.          * @param name name of the file (or zip entry)
  588.          * @exception IOException if data can't be read
  589.          */
  590.         private void loadPoleOffsets(final Section section, final boolean isNonRotatingOrigin,
  591.                                      final BufferedReader reader, final String name)
  592.             throws IOException {

  593.             boolean inValuesPart = false;
  594.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  595.                 lineNumber++;
  596.                 final String[] fields = section.getFields(line);
  597.                 if (fields != null) {

  598.                     // we are within the values part
  599.                     inValuesPart = true;

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

  604.                     // get the entry at the same date if it was already parsed
  605.                     final double[] pole;
  606.                     if (poleOffsetsFieldsMap.containsKey(mjd)) {
  607.                         pole = poleOffsetsFieldsMap.get(mjd);
  608.                     } else {
  609.                         pole = new double[5];
  610.                         poleOffsetsFieldsMap.put(mjd, pole);
  611.                     }

  612.                     if (pole[0] <= firstMJD) {
  613.                         // either it is the first time we parse this date (pole[0] = 0),
  614.                         // or the new parsed data is from a more recent file
  615.                         // in both case, we should update the array
  616.                         pole[0] = firstMJD;
  617.                         if (isNonRotatingOrigin) {
  618.                             pole[1] = Double.parseDouble(fields[1]);
  619.                             pole[2] = Double.parseDouble(fields[2]);
  620.                         } else {
  621.                             pole[3] = Double.parseDouble(fields[1]);
  622.                             pole[4] = Double.parseDouble(fields[2]);
  623.                         }
  624.                     }

  625.                 } else if (inValuesPart) {
  626.                     // we leave values part
  627.                     return;
  628.                 }
  629.             }

  630.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  631.                                       name, lineNumber);

  632.         }

  633.     }

  634. }