SimpleTimeStampedTableParser.java

  1. /* Copyright 2002-2025 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.data;

  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.exception.DummyLocalizable;
  28. import org.orekit.errors.OrekitException;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.time.TimeStamped;

  31. /**
  32.  * Parser for simple tables containing {@link TimeStamped time stamped} data.
  33.  * @param <T> the type of time stamped data (i.e. parsed table rows)
  34.  * @author Luc Maisonobe
  35.  * @since 6.1
  36.  */
  37. public class SimpleTimeStampedTableParser<T extends TimeStamped> {

  38.     /** Interface for converting a table row into time-stamped data.
  39.      * @param <S> the type of time stamped data (i.e. parsed table rows)
  40.      */
  41.     public interface RowConverter<S extends TimeStamped> {

  42.         /** Convert a row.
  43.          * @param rawFields raw row fields, as read from the file
  44.          * @return converted row
  45.          */
  46.         S convert(double[] rawFields);
  47.     }

  48.     /** Pattern for fields with real type. */
  49.     private static final String  REAL_TYPE_PATTERN =
  50.             "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";

  51.     /** Number of columns. */
  52.     private final int columns;

  53.     /** Converter for rows. */
  54.     private final RowConverter<T> converter;

  55.     /** Simple constructor.
  56.      * @param columns number of columns
  57.      * @param converter converter for rows
  58.      */
  59.     public SimpleTimeStampedTableParser(final int columns, final RowConverter<T> converter) {
  60.         this.columns   = columns;
  61.         this.converter = converter;
  62.     }

  63.     /** Parse a stream.
  64.      * @param stream stream containing the table
  65.      * @param name name of the resource file (for error messages only)
  66.      * @return parsed table
  67.      */
  68.     public List<T> parse(final InputStream stream, final String name) {

  69.         if (stream == null) {
  70.             throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_FILE, name);
  71.         }

  72.         // regular lines are simply a space separated list of numbers
  73.         final StringBuilder builder = new StringBuilder("^\\p{Space}*");
  74.         for (int i = 0; i < columns; ++i) {
  75.             builder.append("(");
  76.             builder.append(REAL_TYPE_PATTERN);
  77.             builder.append(")");
  78.             builder.append((i < columns - 1) ? "\\p{Space}+" : "\\p{Space}*$");
  79.         }
  80.         final Pattern regularLinePattern = Pattern.compile(builder.toString());

  81.         // setup the reader
  82.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {

  83.             final List<T> table = new ArrayList<>();

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

  85.                 // replace unicode minus sign ('−') by regular hyphen ('-') for parsing
  86.                 // such unicode characters occur in tables that are copy-pasted from PDF files
  87.                 line = line.replace('\u2212', '-');

  88.                 final Matcher regularMatcher = regularLinePattern.matcher(line);
  89.                 if (regularMatcher.matches()) {
  90.                     // we have found a regular data line

  91.                     final double[] rawFields = new double[columns];
  92.                     for (int i = 0; i < columns; ++i) {
  93.                         rawFields[i] = Double.parseDouble(regularMatcher.group(i + 1));
  94.                     }

  95.                     table.add(converter.convert(rawFields));

  96.                 }

  97.             }

  98.             if (table.isEmpty()) {
  99.                 throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_IERS_DATA_FILE, name);
  100.             }

  101.             return table;

  102.         } catch (IOException ioe) {
  103.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  104.         }

  105.     }

  106. }