RapidDataAndPredictionColumnsLoader.java

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

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

  30. import org.hipparchus.util.MathUtils;
  31. import org.orekit.data.DataProvidersManager;
  32. import org.orekit.errors.OrekitException;
  33. import org.orekit.errors.OrekitMessages;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.time.DateComponents;
  36. import org.orekit.time.TimeScale;
  37. import org.orekit.utils.IERSConventions;
  38. import org.orekit.utils.IERSConventions.NutationCorrectionConverter;

  39. /** Loader for IERS rapid data and prediction files in columns format (finals file).
  40.  * <p>Rapid data and prediction files contain {@link EOPEntry
  41.  * Earth Orientation Parameters} for several years periods, in one file
  42.  * only that is updated regularly.</p>
  43.  * <p>
  44.  * These files contain both the data from IERS Bulletin A and IERS bulletin B.
  45.  * This class parses only the part from Bulletin A.
  46.  * </p>
  47.  * <p>The rapid data and prediction file is recognized thanks to its base name,
  48.  * which must match one of the the patterns <code>finals.*</code> or
  49.  * <code>finals2000A.*</code> (or the same ending with <code>.gz</code>
  50.  * for gzip-compressed files) where * stands for a word like "all", "daily",
  51.  * or "data". The file with 2000A in their name correspond to the
  52.  * IAU-2000 precession-nutation model whereas the files without any identifier
  53.  * correspond to the IAU-1980 precession-nutation model. The files with the all
  54.  * suffix start from 1973-01-01, the file with the data suffix start
  55.  * from 1992-01-01 and the files with the daily suffix.</p>
  56.  * <p>
  57.  * This class is immutable and hence thread-safe
  58.  * </p>
  59.  * @author Romain Di Costanzo
  60.  * @see <a href="http://maia.usno.navy.mil/ser7/readme.finals2000A">finals2000A file format description at USNO</a>
  61.  * @see <a href="http://maia.usno.navy.mil/ser7/readme.finals">finals file format description at USNO</a>
  62.  */
  63. class RapidDataAndPredictionColumnsLoader extends AbstractEopLoader
  64.         implements EOPHistoryLoader {

  65.     /** Conversion factor. */
  66.     private static final double  ARC_SECONDS_TO_RADIANS       = MathUtils.TWO_PI / 1296000;

  67.     /** Conversion factor. */
  68.     private static final double  MILLI_ARC_SECONDS_TO_RADIANS = ARC_SECONDS_TO_RADIANS / 1000;

  69.     /** Conversion factor. */
  70.     private static final double  MILLI_SECONDS_TO_SECONDS     = 1.0e-3;

  71.     /** Field for year, month and day parsing. */
  72.     private static final String  INTEGER2_FIELD               = "((?:\\p{Blank}|\\p{Digit})\\p{Digit})";

  73.     /** Field for modified Julian day parsing. */
  74.     private static final String  MJD_FIELD                    = "\\p{Blank}+(\\p{Digit}+)(?:\\.00*)";

  75.     /** Field for separator parsing. */
  76.     private static final String  SEPARATOR                    = "\\p{Blank}*[IP]";

  77.     /** Field for real parsing. */
  78.     private static final String  REAL_FIELD                   = "\\p{Blank}*(-?\\p{Digit}*\\.\\p{Digit}*)";

  79.     /** Start index of the date part of the line. */
  80.     private static int DATE_START = 0;

  81.     /** end index of the date part of the line. */
  82.     private static int DATE_END   = 15;

  83.     /** Pattern to match the date part of the line (always present). */
  84.     private static final Pattern DATE_PATTERN = Pattern.compile(INTEGER2_FIELD + INTEGER2_FIELD + INTEGER2_FIELD + MJD_FIELD);

  85.     /** Start index of the pole part of the line. */
  86.     private static int POLE_START = 16;

  87.     /** end index of the pole part of the line. */
  88.     private static int POLE_END   = 55;

  89.     /** Pattern to match the pole part of the line. */
  90.     private static final Pattern POLE_PATTERN = Pattern.compile(SEPARATOR + REAL_FIELD + REAL_FIELD + REAL_FIELD + REAL_FIELD);

  91.     /** Start index of the UT1-UTC part of the line. */
  92.     private static int UT1_UTC_START = 57;

  93.     /** end index of the UT1-UTC part of the line. */
  94.     private static int UT1_UTC_END   = 78;

  95.     /** Pattern to match the UT1-UTC part of the line. */
  96.     private static final Pattern UT1_UTC_PATTERN = Pattern.compile(SEPARATOR + REAL_FIELD + REAL_FIELD);

  97.     /** Start index of the LOD part of the line. */
  98.     private static int LOD_START = 79;

  99.     /** end index of the LOD part of the line. */
  100.     private static int LOD_END   = 93;

  101.     /** Pattern to match the LOD part of the line. */
  102.     private static final Pattern LOD_PATTERN = Pattern.compile(REAL_FIELD + REAL_FIELD);

  103.     /** Start index of the nutation part of the line. */
  104.     private static int NUTATION_START = 95;

  105.     /** end index of the nutation part of the line. */
  106.     private static int NUTATION_END   = 134;

  107.     /** Pattern to match the nutation part of the line. */
  108.     private static final Pattern NUTATION_PATTERN = Pattern.compile(SEPARATOR + REAL_FIELD + REAL_FIELD + REAL_FIELD + REAL_FIELD);

  109.     /** Type of nutation corrections. */
  110.     private final boolean isNonRotatingOrigin;

  111.     /** Build a loader for IERS bulletins B files.
  112.      * @param isNonRotatingOrigin if true the supported files <em>must</em>
  113.      * contain δX/δY nutation corrections, otherwise they
  114.      * <em>must</em> contain δΔψ/δΔε nutation
  115.      * corrections
  116.      * @param supportedNames regular expression for supported files names
  117.      * @param manager provides access to EOP data files.
  118.      * @param utcSupplier UTC time scale.
  119.      */
  120.     RapidDataAndPredictionColumnsLoader(final boolean isNonRotatingOrigin,
  121.                                         final String supportedNames,
  122.                                         final DataProvidersManager manager,
  123.                                         final Supplier<TimeScale> utcSupplier) {
  124.         super(supportedNames, manager, utcSupplier);
  125.         this.isNonRotatingOrigin = isNonRotatingOrigin;
  126.     }

  127.     /** {@inheritDoc} */
  128.     public void fillHistory(final IERSConventions.NutationCorrectionConverter converter,
  129.                             final SortedSet<EOPEntry> history) {
  130.         final ItrfVersionProvider itrfVersionProvider = new ITRFVersionLoader(
  131.                 ITRFVersionLoader.SUPPORTED_NAMES,
  132.                 getDataProvidersManager());
  133.         final Parser parser =
  134.                 new Parser(converter, itrfVersionProvider, getUtc(), isNonRotatingOrigin);
  135.         final EopParserLoader loader = new EopParserLoader(parser);
  136.         this.feed(loader);
  137.         history.addAll(loader.getEop());
  138.     }

  139.     /** Internal class performing the parsing. */
  140.     static class Parser extends AbstractEopParser {

  141.         /** Indicator for Non-Rotating Origin. */
  142.         private final boolean isNonRotatingOrigin;

  143.         /** Simple constructor.
  144.          * @param converter converter to use
  145.          * @param itrfVersionProvider to use for determining the ITRF version of the EOP.
  146.          * @param utc time scale for parsing dates.
  147.          * @param isNonRotatingOrigin type of nutation correction
  148.          */
  149.         Parser(final NutationCorrectionConverter converter,
  150.                final ItrfVersionProvider itrfVersionProvider,
  151.                final TimeScale utc,
  152.                final boolean isNonRotatingOrigin) {
  153.             super(converter, itrfVersionProvider, utc);
  154.             this.isNonRotatingOrigin = isNonRotatingOrigin;
  155.         }

  156.         /** {@inheritDoc} */
  157.         @Override
  158.         public Collection<EOPEntry> parse(final InputStream input, final String name)
  159.             throws IOException {

  160.             final List<EOPEntry> history = new ArrayList<>();
  161.             ITRFVersionLoader.ITRFVersionConfiguration configuration = null;

  162.             // reset parse info to start new file (do not clear history!)
  163.             int lineNumber = 0;

  164.             // set up a reader for line-oriented bulletin B files
  165.             try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {

  166.                 for (String line = reader.readLine(); line != null; line = reader.readLine()) {

  167.                     lineNumber++;

  168.                     // split the lines in its various columns (some of them can be blank)
  169.                     final String datePart      = (line.length() >= DATE_END)     ? line.substring(DATE_START,       DATE_END)     : "";
  170.                     final String polePart      = (line.length() >= POLE_END)     ? line.substring(POLE_START,       POLE_END)     : "";
  171.                     final String ut1utcPart    = (line.length() >= UT1_UTC_END ) ? line.substring(UT1_UTC_START,    UT1_UTC_END)  : "";
  172.                     final String lodPart       = (line.length() >= LOD_END)      ? line.substring(LOD_START,        LOD_END)      : "";
  173.                     final String nutationPart  = (line.length() >= NUTATION_END) ? line.substring(NUTATION_START,   NUTATION_END) : "";

  174.                     // parse the date part
  175.                     final Matcher dateMatcher = DATE_PATTERN.matcher(datePart);
  176.                     final int mjd;
  177.                     if (dateMatcher.matches()) {
  178.                         final int yy = Integer.parseInt(dateMatcher.group(1).trim());
  179.                         final int mm = Integer.parseInt(dateMatcher.group(2).trim());
  180.                         final int dd = Integer.parseInt(dateMatcher.group(3).trim());
  181.                         mjd = Integer.parseInt(dateMatcher.group(4).trim());
  182.                         final DateComponents reconstructedDate = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd);
  183.                         if ((reconstructedDate.getYear() % 100) != yy ||
  184.                              reconstructedDate.getMonth()       != mm ||
  185.                              reconstructedDate.getDay()         != dd) {
  186.                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  187.                                                       lineNumber, name, line);
  188.                         }
  189.                     } else {
  190.                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  191.                                                   lineNumber, name, line);
  192.                     }

  193.                     // parse the pole part
  194.                     final double x;
  195.                     final double y;
  196.                     if (polePart.trim().length() == 0) {
  197.                         // pole part is blank
  198.                         x = 0;
  199.                         y = 0;
  200.                     } else {
  201.                         final Matcher poleMatcher = POLE_PATTERN.matcher(polePart);
  202.                         if (poleMatcher.matches()) {
  203.                             x = ARC_SECONDS_TO_RADIANS * Double.parseDouble(poleMatcher.group(1));
  204.                             y = ARC_SECONDS_TO_RADIANS * Double.parseDouble(poleMatcher.group(3));
  205.                         } else {
  206.                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  207.                                                       lineNumber, name, line);
  208.                         }
  209.                     }

  210.                     // parse the UT1-UTC part
  211.                     final double dtu1;
  212.                     if (ut1utcPart.trim().length() == 0) {
  213.                         // UT1-UTC part is blank
  214.                         dtu1 = 0;
  215.                     } else {
  216.                         final Matcher ut1utcMatcher = UT1_UTC_PATTERN.matcher(ut1utcPart);
  217.                         if (ut1utcMatcher.matches()) {
  218.                             dtu1 = Double.parseDouble(ut1utcMatcher.group(1));
  219.                         } else {
  220.                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  221.                                                       lineNumber, name, line);
  222.                         }
  223.                     }

  224.                     // parse the lod part
  225.                     final double lod;
  226.                     if (lodPart.trim().length() == 0) {
  227.                         // lod part is blank
  228.                         lod = 0;
  229.                     } else {
  230.                         final Matcher lodMatcher = LOD_PATTERN.matcher(lodPart);
  231.                         if (lodMatcher.matches()) {
  232.                             lod = MILLI_SECONDS_TO_SECONDS * Double.parseDouble(lodMatcher.group(1));
  233.                         } else {
  234.                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  235.                                                       lineNumber, name, line);
  236.                         }
  237.                     }

  238.                     // parse the nutation part
  239.                     final double[] nro;
  240.                     final double[] equinox;
  241.                     final AbsoluteDate mjdDate =
  242.                             new AbsoluteDate(new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd),
  243.                                     getUtc());
  244.                     if (nutationPart.trim().length() == 0) {
  245.                         // nutation part is blank
  246.                         nro     = new double[2];
  247.                         equinox = new double[2];
  248.                     } else {
  249.                         final Matcher nutationMatcher = NUTATION_PATTERN.matcher(nutationPart);
  250.                         if (nutationMatcher.matches()) {
  251.                             if (isNonRotatingOrigin) {
  252.                                 nro = new double[] {
  253.                                     MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(1)),
  254.                                     MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(3))
  255.                                 };
  256.                                 equinox = getConverter().toEquinox(mjdDate, nro[0], nro[1]);
  257.                             } else {
  258.                                 equinox = new double[] {
  259.                                     MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(1)),
  260.                                     MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(3))
  261.                                 };
  262.                                 nro = getConverter().toNonRotating(mjdDate, equinox[0], equinox[1]);
  263.                             }
  264.                         } else {
  265.                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  266.                                                       lineNumber, name, line);
  267.                         }
  268.                     }

  269.                     if (configuration == null || !configuration.isValid(mjd)) {
  270.                         // get a configuration for current name and date range
  271.                         configuration = getItrfVersionProvider().getConfiguration(name, mjd);
  272.                     }
  273.                     history.add(new EOPEntry(mjd, dtu1, lod, x, y, equinox[0], equinox[1], nro[0], nro[1],
  274.                                              configuration.getVersion(), mjdDate));

  275.                 }

  276.             }

  277.             return history;
  278.         }

  279.     }

  280. }