TAIUTCDatFilesLoader.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 tai-utc.dat file from USNO.
  35.  * <p>
  36.  * This class is immutable and hence thread-safe
  37.  * </p>
  38.  * @author Luc Maisonobe
  39.  * @since 7.1
  40.  */
  41. public class TAIUTCDatFilesLoader extends AbstractSelfFeedingLoader
  42.         implements UTCTAIOffsetsLoader {

  43.     /** Default supported files name pattern. */
  44.     public static final String DEFAULT_SUPPORTED_NAMES = "^tai-utc\\.dat$";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  100.             // data lines read:
  101.             // 1965 SEP  1 =JD 2439004.5  TAI-UTC=   3.8401300 S + (MJD - 38761.) X 0.001296 S
  102.             // 1966 JAN  1 =JD 2439126.5  TAI-UTC=   4.3131700 S + (MJD - 39126.) X 0.002592 S
  103.             // 1968 FEB  1 =JD 2439887.5  TAI-UTC=   4.2131700 S + (MJD - 39126.) X 0.002592 S
  104.             // 1972 JAN  1 =JD 2441317.5  TAI-UTC=  10.0       S + (MJD - 41317.) X 0.0      S
  105.             // 1972 JUL  1 =JD 2441499.5  TAI-UTC=  11.0       S + (MJD - 41317.) X 0.0      S
  106.             // 1973 JAN  1 =JD 2441683.5  TAI-UTC=  12.0       S + (MJD - 41317.) X 0.0      S
  107.             // 1974 JAN  1 =JD 2442048.5  TAI-UTC=  13.0       S + (MJD - 41317.) X 0.0      S

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

  117.             dataPattern = Pattern.compile(LINE_START_REGEXP +
  118.                                           STORED_INTEGER_FIELD + monthField + STORED_INTEGER_FIELD +
  119.                                           "\\p{Blank}+=JD" + STORED_REAL_FIELD +
  120.                                           "\\p{Blank}+TAI-UTC=" + STORED_REAL_FIELD +
  121.                                           "\\p{Blank}+S\\p{Blank}+\\+\\p{Blank}+\\(MJD\\p{Blank}+-" + STORED_REAL_FIELD +
  122.                                           "\\p{Blank}*\\)\\p{Blank}+X" + STORED_REAL_FIELD +
  123.                                           "\\p{Blank}*S" + LINE_END_REGEXP);


  124.         }

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

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

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

  142.                 // read all file, ignoring not recognized lines
  143.                 for (line = reader.readLine(); line != null; line = reader.readLine()) {
  144.                     ++lineNumber;

  145.                     // check matching for data lines
  146.                     final Matcher matcher = dataPattern.matcher(line);
  147.                     if (matcher.matches()) {

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

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

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

  167.                     }
  168.                 }

  169.             } catch (NumberFormatException nfe) {
  170.                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  171.                                           lineNumber, name, line);
  172.             }

  173.             if (offsets.isEmpty()) {
  174.                 throw new OrekitException(OrekitMessages.NO_ENTRIES_IN_IERS_UTC_TAI_HISTORY_FILE, name);
  175.             }

  176.             return offsets;

  177.         }

  178.     }

  179. }