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. /** Used to read an interpolation table from a data file.
  28.  * @author Thomas Neidhart
  29.  */
  30. public class InterpolationTableLoader implements DataLoader {

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

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

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

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

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

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

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

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

  79.         final List<Double> xValues = new LinkedList<Double>();
  80.         final List<Double> yValues = new LinkedList<Double>();
  81.         final LinkedList<List<Double>> cellValues = new LinkedList<List<Double>>();

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

  84.         // ignore comments starting with a #
  85.         tokenizer.commentChar('#');
  86.         tokenizer.eolIsSignificant(true);

  87.         int tokenCount = 0;
  88.         boolean headerRow = false;
  89.         boolean done = false;

  90.         do {
  91.             switch (tokenizer.nextToken()) {

  92.                 case StreamTokenizer.TT_EOF:
  93.                     done = true;
  94.                     break;

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

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

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

  121.                 default:
  122.                     break;
  123.             }

  124.         } while (!done);

  125.         xArr = toPrimitiveArray(xValues);
  126.         yArr = toPrimitiveArray(yValues);
  127.         fArr = new double[cellValues.size()][];
  128.         int idx = 0;

  129.         for (List<Double> row : cellValues) {
  130.             fArr[idx++] = toPrimitiveArray(row);
  131.         }

  132.     }

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