BulletinAFilesLoader.java

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

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.SortedSet;
  28. import java.util.regex.Matcher;
  29. import java.util.regex.Pattern;

  30. import org.hipparchus.util.FastMath;
  31. import org.orekit.data.DataLoader;
  32. import org.orekit.data.DataProvidersManager;
  33. import org.orekit.errors.OrekitException;
  34. import org.orekit.errors.OrekitInternalError;
  35. import org.orekit.errors.OrekitMessages;
  36. import org.orekit.time.DateComponents;
  37. import org.orekit.utils.Constants;
  38. import org.orekit.utils.IERSConventions;

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

  116.     /** Conversion factor. */
  117.     private static final double MILLI_ARC_SECONDS_TO_RADIANS = Constants.ARC_SECONDS_TO_RADIANS / 1000;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  286.         /** Header pattern. */
  287.         private final Pattern header;

  288.         /** Data pattern. */
  289.         private final Pattern data;

  290.         /** Simple constructor.
  291.          * @param headerRegExp regular expression for header
  292.          * @param dataRegExp regular expression for data
  293.          */
  294.         Section(final String headerRegExp, final String dataRegExp) {
  295.             this.header = Pattern.compile(headerRegExp);
  296.             this.data   = Pattern.compile(dataRegExp);
  297.         }

  298.         /** Check if a line matches the section header.
  299.          * @param line line to check
  300.          * @return true if the line matches the header
  301.          */
  302.         public boolean matchesHeader(final String line) {
  303.             return header.matcher(line).matches();
  304.         }

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

  321.     }

  322.     /** Regular expression for supported files names. */
  323.     private final String supportedNames;

  324.     /** Build a loader for IERS bulletins A files.
  325.     * @param supportedNames regular expression for supported files names
  326.     */
  327.     BulletinAFilesLoader(final String supportedNames) {
  328.         this.supportedNames = supportedNames;
  329.     }

  330.     /** {@inheritDoc} */
  331.     public void fillHistory(final IERSConventions.NutationCorrectionConverter converter,
  332.                             final SortedSet<EOPEntry> history)
  333.         throws OrekitException {
  334.         final Parser parser = new Parser();
  335.         DataProvidersManager.getInstance().feed(supportedNames, parser);
  336.         parser.fill(history);
  337.     }

  338.     /** Internal class performing the parsing. */
  339.     private static class Parser implements DataLoader {

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

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

  344.         /** Configuration for ITRF versions. */
  345.         private final ITRFVersionLoader itrfVersionLoader;

  346.         /** ITRF version configuration. */
  347.         private ITRFVersionLoader.ITRFVersionConfiguration configuration;

  348.         /** File name. */
  349.         private String fileName;

  350.         /** Current line number. */
  351.         private int lineNumber;

  352.         /** Current line. */
  353.         private String line;

  354.         /** Earliest parsed data. */
  355.         private int mjdMin;

  356.         /** Latest parsed data. */
  357.         private int mjdMax;

  358.         /** First MJD parsed in current file. */
  359.         private int firstMJD;

  360.         /** Simple constructor.
  361.          * @exception OrekitException if ITRF version loader cannot be parsed
  362.          */
  363.         Parser()
  364.             throws OrekitException {
  365.             this.eopFieldsMap         = new HashMap<Integer, double[]>();
  366.             this.poleOffsetsFieldsMap = new HashMap<Integer, double[]>();
  367.             this.itrfVersionLoader    = new ITRFVersionLoader(ITRFVersionLoader.SUPPORTED_NAMES);
  368.             this.lineNumber           = 0;
  369.             this.mjdMin               = Integer.MAX_VALUE;
  370.             this.mjdMax               = Integer.MIN_VALUE;
  371.             this.firstMJD             = -1;
  372.         }

  373.         /** {@inheritDoc} */
  374.         public boolean stillAcceptsData() {
  375.             return true;
  376.         }

  377.         /** {@inheritDoc} */
  378.         public void loadData(final InputStream input, final String name)
  379.             throws OrekitException, IOException {

  380.             this.configuration = null;
  381.             this.fileName      = name;

  382.             // set up a reader for line-oriented bulletin A files
  383.             final BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
  384.             lineNumber =  0;
  385.             firstMJD   = -1;

  386.             // loop over sections
  387.             final List<Section> remaining = new ArrayList<Section>();
  388.             remaining.addAll(Arrays.asList(Section.values()));
  389.             for (Section section = nextSection(remaining, reader, name);
  390.                  section != null;
  391.                  section = nextSection(remaining, reader, name)) {

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

  410.                 // remove the already parsed section from the list
  411.                 remaining.remove(section);

  412.             }

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

  422.         }

  423.         /** Fill EOP history obtained after reading several files.
  424.          * @param history history to fill up
  425.          * @exception OrekitException if UTC time scale cannot be retrieved
  426.          */
  427.         public void fill(final SortedSet<EOPEntry> history)
  428.             throws OrekitException {

  429.             double[] currentEOP = null;
  430.             double[] nextEOP    = eopFieldsMap.get(mjdMin);
  431.             for (int mjd = mjdMin; mjd <= mjdMax; ++mjd) {

  432.                 final double[] currentPole = poleOffsetsFieldsMap.get(mjd);

  433.                 final double[] previousEOP = currentEOP;
  434.                 currentEOP = nextEOP;
  435.                 nextEOP    = eopFieldsMap.get(mjd + 1);

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

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

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

  496.             }

  497.         }

  498.         /** Skip to next section header.
  499.          * @param sections sections to check for
  500.          * @param reader reader from where file content is obtained
  501.          * @param name name of the file (or zip entry)
  502.          * @return the next section or null if no section is found until end of file
  503.          * @exception IOException if data can't be read
  504.          */
  505.         private Section nextSection(final List<Section> sections,
  506.                                     final BufferedReader reader, final String name)
  507.             throws IOException {

  508.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  509.                 ++lineNumber;
  510.                 for (Section section : sections) {
  511.                     if (section.matchesHeader(line)) {
  512.                         return section;
  513.                     }
  514.                 }
  515.             }

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

  518.         }

  519.         /** Read X, Y, UT1-UTC.
  520.          * @param section section to parse
  521.          * @param reader reader from where file content is obtained
  522.          * @param name name of the file (or zip entry)
  523.          * @exception IOException if data can't be read
  524.          * @exception OrekitException if some data is missing or if some loader specific error occurs
  525.          */
  526.         private void loadXYDT(final Section section, final BufferedReader reader, final String name)
  527.             throws OrekitException, IOException {

  528.             boolean inValuesPart = false;
  529.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  530.                 lineNumber++;
  531.                 final String[] fields = section.getFields(line);
  532.                 if (fields != null) {

  533.                     // we are within the values part
  534.                     inValuesPart = true;

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

  553.                     // get the entry at the same date if it was already parsed
  554.                     final double[] eop;
  555.                     if (eopFieldsMap.containsKey(mjd)) {
  556.                         eop = eopFieldsMap.get(mjd);
  557.                     } else {
  558.                         eop = new double[4];
  559.                         eopFieldsMap.put(mjd, eop);
  560.                     }

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

  570.                 } else if (inValuesPart) {
  571.                     // we leave values part
  572.                     return;
  573.                 }
  574.             }

  575.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  576.                                       name, lineNumber);

  577.         }

  578.         /** Read EOP data.
  579.          * @param section section to parse
  580.          * @param isNonRotatingOrigin if true, the file contain Non-Rotating Origin nutation corrections
  581.          * @param reader reader from where file content is obtained
  582.          * @param name name of the file (or zip entry)
  583.          * @exception IOException if data can't be read
  584.          * @exception OrekitException if some data is missing or if some loader specific error occurs
  585.          */
  586.         private void loadPoleOffsets(final Section section, final boolean isNonRotatingOrigin,
  587.                                      final BufferedReader reader, final String name)
  588.             throws OrekitException, IOException {

  589.             boolean inValuesPart = false;
  590.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  591.                 lineNumber++;
  592.                 final String[] fields = section.getFields(line);
  593.                 if (fields != null) {

  594.                     // we are within the values part
  595.                     inValuesPart = true;

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

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

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

  621.                 } else if (inValuesPart) {
  622.                     // we leave values part
  623.                     return;
  624.                 }
  625.             }

  626.             throw new OrekitException(OrekitMessages.UNEXPECTED_END_OF_FILE_AFTER_LINE,
  627.                                       name, lineNumber);

  628.         }

  629.     }

  630. }