InterpolationTableLoader.java

  1. /* Copyright 2011-2012 Space Applications Services
  2.  * Licensed to CS Communication & Systèmes (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.utils;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.StreamTokenizer;
  23. import java.text.ParseException;
  24. import java.util.LinkedList;
  25. import java.util.List;

  26. import org.orekit.data.DataLoader;
  27. import org.orekit.errors.OrekitException;

  28. /** Used to read an interpolation table from a data file.
  29.  * @author Thomas Neidhart
  30.  */
  31. public class InterpolationTableLoader implements DataLoader {

  32.     /** Abscissa grid for the bi-variate interpolation function read from the file. */
  33.     private double[] xArr;

  34.     /** Ordinate grid for the bi-variate interpolation function read from the file. */
  35.     private double[] yArr;

  36.     /** Values samples for the bi-variate interpolation function read from the file. */
  37.     private double[][] fArr;

  38.     /** Returns a copy of the abscissa grid for the interpolation function.
  39.      * @return the abscissa grid for the interpolation function,
  40.      *         or <code>null</code> if the file could not be read
  41.      */
  42.     public double[] getAbscissaGrid() {
  43.         return xArr.clone();
  44.     }

  45.     /** Returns a copy of the ordinate grid for the interpolation function.
  46.      * @return the ordinate grid for the interpolation function,
  47.      *         or <code>null</code> if the file could not be read
  48.      */
  49.     public double[] getOrdinateGrid() {
  50.         return yArr.clone();
  51.     }

  52.     /** Returns a copy of the values samples for the interpolation function.
  53.      * @return the values samples for the interpolation function,
  54.      *         or <code>null</code> if the file could not be read
  55.      */
  56.     public double[][] getValuesSamples() {
  57.         return fArr.clone();
  58.     }

  59.     /** {@inheritDoc} */
  60.     public boolean stillAcceptsData() {
  61.         return xArr == null;
  62.     }

  63.     /** Loads an bi-variate interpolation table from the given {@link InputStream}.
  64.      * The format of the table is as follows (number of rows/columns can be extended):
  65.      * <pre>
  66.      *  Table: tableName
  67.      *
  68.      *      | 0.0 |  60.0 |  66.0
  69.      *  -------------------------
  70.      *    0 | 0.0 | 0.003 | 0.006
  71.      *  500 | 0.0 | 0.003 | 0.006
  72.      * </pre>
  73.      * @param input the input stream to read data from
  74.      * @param name  the name of the input file
  75.      * @exception IOException if data can't be read
  76.      * @exception ParseException if data can't be parsed
  77.      * @exception OrekitException if some data is missing or unexpected
  78.      *                            data is encountered
  79.      */
  80.     public void loadData(final InputStream input, final String name)
  81.         throws IOException, ParseException, OrekitException {

  82.         final List<Double> xValues = new LinkedList<Double>();
  83.         final List<Double> yValues = new LinkedList<Double>();
  84.         final LinkedList<List<Double>> cellValues = new LinkedList<List<Double>>();

  85.         final StreamTokenizer tokenizer =
  86.             new StreamTokenizer(new BufferedReader(new InputStreamReader(input, "UTF-8")));

  87.         // ignore comments starting with a #
  88.         tokenizer.commentChar('#');
  89.         tokenizer.eolIsSignificant(true);

  90.         int tokenCount = 0;
  91.         boolean headerRow = false;
  92.         boolean done = false;

  93.         do {
  94.             switch (tokenizer.nextToken()) {

  95.                 case StreamTokenizer.TT_EOF:
  96.                     done = true;
  97.                     break;

  98.                 case StreamTokenizer.TT_EOL:
  99.                     // end of header row
  100.                     if (yValues.size() > 0) {
  101.                         headerRow = false;
  102.                     }
  103.                     tokenCount = 0;
  104.                     break;

  105.                 case StreamTokenizer.TT_NUMBER:
  106.                     if (headerRow) {
  107.                         yValues.add(tokenizer.nval);
  108.                     } else {
  109.                         if (tokenCount == 0) {
  110.                             xValues.add(tokenizer.nval);
  111.                             cellValues.add(new LinkedList<Double>());
  112.                         } else {
  113.                             cellValues.getLast().add(tokenizer.nval);
  114.                         }
  115.                     }
  116.                     tokenCount++;
  117.                     break;

  118.                 case StreamTokenizer.TT_WORD:
  119.                     // we are in the header row now
  120.                     if (tokenizer.sval.startsWith("Table")) {
  121.                         headerRow = true;
  122.                     }
  123.                     break;

  124.                 default:
  125.                     break;
  126.             }

  127.         } while (!done);

  128.         xArr = toPrimitiveArray(xValues);
  129.         yArr = toPrimitiveArray(yValues);
  130.         fArr = new double[cellValues.size()][];
  131.         int idx = 0;

  132.         for (List<Double> row : cellValues) {
  133.             fArr[idx++] = toPrimitiveArray(row);
  134.         }

  135.     }

  136.     /** Converts a list of {@link Double} objects into an array of double primitives.
  137.      * @param list the list of {@link Double} objects
  138.      * @return the double array containing the list elements
  139.      */
  140.     private double[] toPrimitiveArray(final List<Double> list) {
  141.         final double[] result = new double[list.size()];
  142.         int idx = 0;
  143.         for (Double element : list) {
  144.             result[idx++] = element;
  145.         }
  146.         return result;
  147.     }
  148. }