1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.utils;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.StreamTokenizer;
24 import java.text.ParseException;
25 import java.util.LinkedList;
26 import java.util.List;
27
28 import org.orekit.data.DataLoader;
29 import org.orekit.errors.OrekitException;
30
31
32
33
34 public class InterpolationTableLoader implements DataLoader {
35
36
37 private double[] xArr;
38
39
40 private double[] yArr;
41
42
43 private double[][] fArr;
44
45
46
47
48
49 public double[] getAbscissaGrid() {
50 return xArr.clone();
51 }
52
53
54
55
56
57 public double[] getOrdinateGrid() {
58 return yArr.clone();
59 }
60
61
62
63
64
65 public double[][] getValuesSamples() {
66 return fArr.clone();
67 }
68
69
70 public boolean stillAcceptsData() {
71 return xArr == null;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public void loadData(final InputStream input, final String name)
92 throws IOException, ParseException, OrekitException {
93
94 final List<Double> xValues = new LinkedList<Double>();
95 final List<Double> yValues = new LinkedList<Double>();
96 final LinkedList<List<Double>> cellValues = new LinkedList<List<Double>>();
97
98 final StreamTokenizer tokenizer =
99 new StreamTokenizer(new BufferedReader(new InputStreamReader(input, "UTF-8")));
100
101
102 tokenizer.commentChar('#');
103 tokenizer.eolIsSignificant(true);
104
105 int tokenCount = 0;
106 boolean headerRow = false;
107 boolean done = false;
108
109 do {
110 switch (tokenizer.nextToken()) {
111
112 case StreamTokenizer.TT_EOF:
113 done = true;
114 break;
115
116 case StreamTokenizer.TT_EOL:
117
118 if (yValues.size() > 0) {
119 headerRow = false;
120 }
121 tokenCount = 0;
122 break;
123
124 case StreamTokenizer.TT_NUMBER:
125 if (headerRow) {
126 yValues.add(tokenizer.nval);
127 } else {
128 if (tokenCount == 0) {
129 xValues.add(tokenizer.nval);
130 cellValues.add(new LinkedList<Double>());
131 } else {
132 cellValues.getLast().add(tokenizer.nval);
133 }
134 }
135 tokenCount++;
136 break;
137
138 case StreamTokenizer.TT_WORD:
139
140 if (tokenizer.sval.startsWith("Table")) {
141 headerRow = true;
142 }
143 break;
144
145 default:
146 break;
147 }
148
149 } while (!done);
150
151 xArr = toPrimitiveArray(xValues);
152 yArr = toPrimitiveArray(yValues);
153 fArr = new double[cellValues.size()][];
154 int idx = 0;
155
156 for (List<Double> row : cellValues) {
157 fArr[idx++] = toPrimitiveArray(row);
158 }
159
160 }
161
162
163
164
165
166 private double[] toPrimitiveArray(final List<Double> list) {
167 final double[] result = new double[list.size()];
168 int idx = 0;
169 for (Double element : list) {
170 result[idx++] = element;
171 }
172 return result;
173 }
174 }