RapidDataAndPredictionColumnsLoader.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.List;
  24. import java.util.SortedSet;
  25. import java.util.regex.Matcher;
  26. import java.util.regex.Pattern;

  27. import org.hipparchus.util.MathUtils;
  28. import org.orekit.data.DataLoader;
  29. import org.orekit.data.DataProvidersManager;
  30. import org.orekit.errors.OrekitException;
  31. import org.orekit.errors.OrekitMessages;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.time.DateComponents;
  34. import org.orekit.time.TimeScalesFactory;
  35. import org.orekit.utils.IERSConventions;

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

  61.     /** Conversion factor. */
  62.     private static final double  ARC_SECONDS_TO_RADIANS       = MathUtils.TWO_PI / 1296000;

  63.     /** Conversion factor. */
  64.     private static final double  MILLI_ARC_SECONDS_TO_RADIANS = ARC_SECONDS_TO_RADIANS / 1000;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  105.     /** Type of nutation corrections. */
  106.     private final boolean isNonRotatingOrigin;

  107.     /** File supported name. */
  108.     private final String  supportedNames;

  109.     /** Build a loader for IERS bulletins B files.
  110.      * @param isNonRotatingOrigin if true the supported files <em>must</em>
  111.      * contain δX/δY nutation corrections, otherwise they
  112.      * <em>must</em> contain δΔψ/δΔε nutation
  113.      * corrections
  114.      * @param supportedNames regular expression for supported files names
  115.      */
  116.     RapidDataAndPredictionColumnsLoader(final boolean isNonRotatingOrigin,
  117.                                                final String supportedNames) {
  118.         this.isNonRotatingOrigin = isNonRotatingOrigin;
  119.         this.supportedNames      = supportedNames;
  120.     }

  121.     /** {@inheritDoc} */
  122.     public void fillHistory(final IERSConventions.NutationCorrectionConverter converter,
  123.                             final SortedSet<EOPEntry> history)
  124.         throws OrekitException {
  125.         final Parser parser = new Parser(converter, isNonRotatingOrigin);
  126.         DataProvidersManager.getInstance().feed(supportedNames, parser);
  127.         history.addAll(parser.history);
  128.     }

  129.     /** Internal class performing the parsing. */
  130.     private static class Parser implements DataLoader {

  131.         /** Converter for nutation corrections. */
  132.         private final IERSConventions.NutationCorrectionConverter converter;

  133.         /** Configuration for ITRF versions. */
  134.         private final ITRFVersionLoader itrfVersionLoader;

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

  137.         /** History entries. */
  138.         private final List<EOPEntry> history;

  139.         /** Current line number. */
  140.         private int lineNumber;

  141.         /** Current line. */
  142.         private String line;

  143.         /** Simple constructor.
  144.          * @param converter converter to use
  145.          * @param isNonRotatingOrigin type of nutation correction
  146.          * @exception OrekitException if ITRF version loader cannot be parsed
  147.          */
  148.         Parser(final IERSConventions.NutationCorrectionConverter converter,
  149.                       final boolean isNonRotatingOrigin)
  150.             throws OrekitException {
  151.             this.converter           = converter;
  152.             this.itrfVersionLoader   = new ITRFVersionLoader(ITRFVersionLoader.SUPPORTED_NAMES);
  153.             this.isNonRotatingOrigin = isNonRotatingOrigin;
  154.             this.history             = new ArrayList<EOPEntry>();
  155.             this.lineNumber          = 0;
  156.         }

  157.         /** {@inheritDoc} */
  158.         public boolean stillAcceptsData() {
  159.             return true;
  160.         }

  161.         /** {@inheritDoc} */
  162.         public void loadData(final InputStream input, final String name)
  163.             throws OrekitException, IOException {

  164.             ITRFVersionLoader.ITRFVersionConfiguration configuration = null;

  165.             // set up a reader for line-oriented bulletin B files
  166.             final BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));

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

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

  170.                 lineNumber++;

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

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

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

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

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

  241.                 // parse the nutation part
  242.                 final double[] nro;
  243.                 final double[] equinox;
  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.                     final AbsoluteDate mjdDate =
  251.                             new AbsoluteDate(new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd),
  252.                                              TimeScalesFactory.getUTC());
  253.                     if (nutationMatcher.matches()) {
  254.                         if (isNonRotatingOrigin) {
  255.                             nro = new double[] {
  256.                                 MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(1)),
  257.                                 MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(3))
  258.                             };
  259.                             equinox = converter.toEquinox(mjdDate, nro[0], nro[1]);
  260.                         } else {
  261.                             equinox = new double[] {
  262.                                 MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(1)),
  263.                                 MILLI_ARC_SECONDS_TO_RADIANS * Double.parseDouble(nutationMatcher.group(3))
  264.                             };
  265.                             nro = converter.toNonRotating(mjdDate, equinox[0], equinox[1]);
  266.                         }
  267.                     } else {
  268.                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  269.                                                   lineNumber, name, line);
  270.                     }
  271.                 }

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

  278.             }

  279.         }

  280.     }

  281. }