AGILeapSecondFilesLoader.java

  1. /* Copyright 2002-2023 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.time;

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

  27. import org.hipparchus.util.FastMath;
  28. import org.orekit.annotation.DefaultDataContext;
  29. import org.orekit.data.AbstractSelfFeedingLoader;
  30. import org.orekit.data.DataContext;
  31. import org.orekit.data.DataProvidersManager;
  32. import org.orekit.errors.OrekitException;
  33. import org.orekit.errors.OrekitMessages;

  34. /** Loader for UTC-TAI extracted from LeapSecond file from AGI.
  35.  * <p>
  36.  * This class is immutable and hence thread-safe
  37.  * </p>
  38.  * @see <a href="ftp://ftp.agi.com/pub/STKData/Astro/LeapSecond.dat">LeapSecond.dat</a>
  39.  * @author Luc Maisonobe
  40.  * @since 10.3
  41.  */
  42. public class AGILeapSecondFilesLoader extends AbstractSelfFeedingLoader
  43.         implements UTCTAIOffsetsLoader {

  44.     /** Default supported files name pattern. */
  45.     public static final String DEFAULT_SUPPORTED_NAMES = "^LeapSecond\\.dat$";

  46.     /**
  47.      * Build a loader for LeapSecond.dat file from AGI. This constructor uses the {@link
  48.      * DataContext#getDefault() default data context}.
  49.      *
  50.      * @param supportedNames regular expression for supported files names
  51.      * @see #AGILeapSecondFilesLoader(String, DataProvidersManager)
  52.      */
  53.     @DefaultDataContext
  54.     public AGILeapSecondFilesLoader(final String supportedNames) {
  55.         this(supportedNames, DataContext.getDefault().getDataProvidersManager());
  56.     }

  57.     /**
  58.      * Build a loader for LeapSecond.dat file from AGI.
  59.      *
  60.      * @param supportedNames regular expression for supported files names
  61.      * @param manager        provides access to the {@code tai-utc.dat} file.
  62.      */
  63.     public AGILeapSecondFilesLoader(final String supportedNames,
  64.                                     final DataProvidersManager manager) {
  65.         super(supportedNames, manager);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public List<OffsetModel> loadOffsets() {
  70.         final UtcTaiOffsetLoader parser = new UtcTaiOffsetLoader(new Parser());
  71.         this.feed(parser);
  72.         return parser.getOffsets();
  73.     }

  74.     /** Internal class performing the parsing. */
  75.     public static class Parser implements UTCTAIOffsetsLoader.Parser {

  76.         /** Regular expression for optional blanks. */
  77.         private static final String BLANKS               = "\\p{Blank}*";

  78.         /** Regular expression for storage start. */
  79.         private static final String STORAGE_START        = "(";

  80.         /** Regular expression for storage end. */
  81.         private static final String STORAGE_END          = ")";

  82.         /** Regular expression for alternative. */
  83.         private static final String ALTERNATIVE          = "|";

  84.         /** Regular expression matching blanks at start of line. */
  85.         private static final String LINE_START_REGEXP     = "^" + BLANKS;

  86.         /** Regular expression matching blanks at end of line. */
  87.         private static final String LINE_END_REGEXP       = BLANKS + "$";

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

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

  92.         /** Regular expression matching an integer field to store. */
  93.         private static final String STORED_INTEGER_FIELD  = BLANKS + STORAGE_START + INTEGER_REGEXP + STORAGE_END;

  94.         /** Regular expression matching a real field to store. */
  95.         private static final String STORED_REAL_FIELD     = BLANKS + STORAGE_START + REAL_REGEXP + STORAGE_END;

  96.         /** Data lines pattern. */
  97.         private Pattern dataPattern;

  98.         /** Simple constructor.
  99.          */
  100.         public Parser() {

  101.             // data lines read:
  102.             // 28
  103.             // 1972 JAN  1   2441317.5     10.0        41317.  0.0
  104.             // 1972 JUL  1   2441499.5     11.0        41317.  0.0
  105.             // 1973 JAN  1   2441683.5     12.0        41317.  0.0
  106.             // 1974 JAN  1   2442048.5     13.0        41317.  0.0
  107.             // 1975 JAN  1   2442413.5     14.0        41317.  0.0
  108.             // 1976 JAN  1   2442778.5     15.0        41317.  0.0
  109.             // 1977 JAN  1   2443144.5     16.0        41317.  0.0
  110.             // 1978 JAN  1   2443509.5     17.0        41317.  0.0

  111.             // month as a three letters upper case abbreviation
  112.             final StringBuilder builder = new StringBuilder(BLANKS + STORAGE_START);
  113.             for (final Month month : Month.values()) {
  114.                 builder.append(month.getUpperCaseAbbreviation());
  115.                 builder.append(ALTERNATIVE);
  116.             }
  117.             builder.delete(builder.length() - 1, builder.length());
  118.             builder.append(STORAGE_END);
  119.             final String monthField = builder.toString();

  120.             dataPattern = Pattern.compile(LINE_START_REGEXP +
  121.                                           STORED_INTEGER_FIELD + monthField + STORED_INTEGER_FIELD +
  122.                                           BLANKS + STORED_REAL_FIELD +
  123.                                           BLANKS + STORED_REAL_FIELD +
  124.                                           BLANKS + STORED_REAL_FIELD +
  125.                                           BLANKS + STORED_REAL_FIELD +
  126.                                           LINE_END_REGEXP);


  127.         }

  128.         /** Load UTC-TAI offsets entries read from some file.
  129.          * <p>The time steps are extracted from some {@code LeapSecond.dat} file.
  130.          * Since entries are stored in a {@link java.util.SortedMap SortedMap},
  131.          * they are chronologically sorted and only one entry remains for a given date.</p>
  132.          * @param input data input stream
  133.          * @param name name of the file (or zip entry)
  134.          * @exception IOException if data can't be read
  135.          */
  136.         @Override
  137.         public List<OffsetModel> parse(final InputStream input, final String name)
  138.             throws IOException {

  139.             final List<OffsetModel> offsets = new ArrayList<>();

  140.             int lineNumber = 0;
  141.             DateComponents lastDate = null;
  142.             String line = null;
  143.             // set up a reader for line-oriented file
  144.             try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {

  145.                 // read all file, ignoring not recognized lines
  146.                 for (line = reader.readLine(); line != null; line = reader.readLine()) {
  147.                     ++lineNumber;

  148.                     // check matching for data lines
  149.                     final Matcher matcher = dataPattern.matcher(line);
  150.                     if (matcher.matches()) {

  151.                         // build an entry from the extracted fields
  152.                         final DateComponents dc1 = new DateComponents(Integer.parseInt(matcher.group(1)),
  153.                                                                       Month.parseMonth(matcher.group(2)),
  154.                                                                       Integer.parseInt(matcher.group(3)));
  155.                         final DateComponents dc2 = new DateComponents(DateComponents.JULIAN_EPOCH,
  156.                                                                       (int) FastMath.ceil(Double.parseDouble(matcher.group(4))));
  157.                         if (!dc1.equals(dc2)) {
  158.                             throw new OrekitException(OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE,
  159.                                                       name, dc1.getYear(), dc1.getMonth(), dc1.getDay(), dc2.getMJD());
  160.                         }

  161.                         if (lastDate != null && dc1.compareTo(lastDate) <= 0) {
  162.                             throw new OrekitException(OrekitMessages.NON_CHRONOLOGICAL_DATES_IN_FILE,
  163.                                                       name, lineNumber);
  164.                         }
  165.                         lastDate = dc1;

  166.                         final double offset = Double.parseDouble(matcher.group(5));
  167.                         final double mjdRef = Double.parseDouble(matcher.group(6));
  168.                         final double slope  = Double.parseDouble(matcher.group(7));
  169.                         offsets.add(new OffsetModel(dc1, (int) FastMath.rint(mjdRef), offset, slope));

  170.                     }
  171.                 }

  172.             } catch (NumberFormatException nfe) {
  173.                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  174.                                           lineNumber, name, line);
  175.             }

  176.             if (offsets.isEmpty()) {
  177.                 throw new OrekitException(OrekitMessages.NO_ENTRIES_IN_IERS_UTC_TAI_HISTORY_FILE, name);
  178.             }

  179.             return offsets;

  180.         }

  181.     }

  182. }