RapidDataAndPredictionColumnsLoader.java

  1. /* Copyright 2002-2021 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.orekit.data.DataProvidersManager;
  31. import org.orekit.errors.OrekitException;
  32. import org.orekit.errors.OrekitMessages;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.time.DateComponents;
  35. import org.orekit.time.TimeScale;
  36. import org.orekit.utils.IERSConventions;
  37. import org.orekit.utils.IERSConventions.NutationCorrectionConverter;
  38. import org.orekit.utils.units.UnitsConverter;

  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.     /** Field for year, month and day parsing. */
  66.     private static final String  INTEGER2_FIELD               = "((?:\\p{Blank}|\\p{Digit})\\p{Digit})";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  103.     /** Type of nutation corrections. */
  104.     private final boolean isNonRotatingOrigin;

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

  121.     /** {@inheritDoc} */
  122.     public void fillHistory(final IERSConventions.NutationCorrectionConverter converter,
  123.                             final SortedSet<EOPEntry> history) {
  124.         final ItrfVersionProvider itrfVersionProvider = new ITRFVersionLoader(
  125.                 ITRFVersionLoader.SUPPORTED_NAMES,
  126.                 getDataProvidersManager());
  127.         final Parser parser =
  128.                 new Parser(converter, itrfVersionProvider, getUtc(), isNonRotatingOrigin);
  129.         final EopParserLoader loader = new EopParserLoader(parser);
  130.         this.feed(loader);
  131.         history.addAll(loader.getEop());
  132.     }

  133.     /** Internal class performing the parsing. */
  134.     static class Parser extends AbstractEopParser {

  135.         /** Indicator for Non-Rotating Origin. */
  136.         private final boolean isNonRotatingOrigin;

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

  150.         /** {@inheritDoc} */
  151.         @Override
  152.         public Collection<EOPEntry> parse(final InputStream input, final String name)
  153.             throws IOException {

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

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

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

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

  161.                     lineNumber++;

  162.                     // split the lines in its various columns (some of them can be blank)
  163.                     final String datePart      = (line.length() >= DATE_END)     ? line.substring(DATE_START,       DATE_END)     : "";
  164.                     final String polePart      = (line.length() >= POLE_END)     ? line.substring(POLE_START,       POLE_END)     : "";
  165.                     final String ut1utcPart    = (line.length() >= UT1_UTC_END ) ? line.substring(UT1_UTC_START,    UT1_UTC_END)  : "";
  166.                     final String lodPart       = (line.length() >= LOD_END)      ? line.substring(LOD_START,        LOD_END)      : "";
  167.                     final String nutationPart  = (line.length() >= NUTATION_END) ? line.substring(NUTATION_START,   NUTATION_END) : "";

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

  187.                     // parse the pole part
  188.                     final double x;
  189.                     final double y;
  190.                     if (polePart.trim().length() == 0) {
  191.                         // pole part is blank
  192.                         x = 0;
  193.                         y = 0;
  194.                     } else {
  195.                         final Matcher poleMatcher = POLE_PATTERN.matcher(polePart);
  196.                         if (poleMatcher.matches()) {
  197.                             x = UnitsConverter.ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(poleMatcher.group(1)));
  198.                             y = UnitsConverter.ARC_SECONDS_TO_RADIANS.convert(Double.parseDouble(poleMatcher.group(3)));
  199.                         } else {
  200.                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  201.                                                       lineNumber, name, line);
  202.                         }
  203.                     }

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

  218.                     // parse the lod part
  219.                     final double lod;
  220.                     if (lodPart.trim().length() == 0) {
  221.                         // lod part is blank
  222.                         lod = 0;
  223.                     } else {
  224.                         final Matcher lodMatcher = LOD_PATTERN.matcher(lodPart);
  225.                         if (lodMatcher.matches()) {
  226.                             lod = UnitsConverter.MILLI_SECONDS_TO_SECONDS.convert(Double.parseDouble(lodMatcher.group(1)));
  227.                         } else {
  228.                             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  229.                                                       lineNumber, name, line);
  230.                         }
  231.                     }

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

  263.                     if (configuration == null || !configuration.isValid(mjd)) {
  264.                         // get a configuration for current name and date range
  265.                         configuration = getItrfVersionProvider().getConfiguration(name, mjd);
  266.                     }
  267.                     history.add(new EOPEntry(mjd, dtu1, lod, x, y, equinox[0], equinox[1], nro[0], nro[1],
  268.                                              configuration.getVersion(), mjdDate));

  269.                 }

  270.             }

  271.             return history;
  272.         }

  273.     }

  274. }