comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java @ 3651:06a65baae494

merged flys-artifacts/2.9
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:43 +0200
parents 1df6984628c3
children bcf25d8c183e
comparison
equal deleted inserted replaced
3549:6a8f83c538e3 3651:06a65baae494
1 package de.intevation.flys.artifacts.model;
2
3 import de.intevation.flys.artifacts.math.Linear;
4
5 import gnu.trove.TDoubleArrayList;
6
7 import java.io.Serializable;
8
9 import org.apache.log4j.Logger;
10
11 public class Parameters
12 implements Serializable
13 {
14 private static Logger log = Logger.getLogger(Parameters.class);
15
16 public interface Visitor {
17
18 void visit(double [] row);
19
20 } // interface Visitor
21
22 public static final double EPSILON = 1e-4;
23
24 protected String [] columnNames;
25 protected TDoubleArrayList [] columns;
26
27 public Parameters() {
28 }
29
30 public Parameters(String [] columnNames) {
31 if (columnNames == null || columnNames.length < 1) {
32 throw new IllegalArgumentException("columnNames too short.");
33 }
34 this.columnNames = columnNames;
35 columns = new TDoubleArrayList[columnNames.length];
36 for (int i = 0; i < columns.length; ++i) {
37 columns[i] = new TDoubleArrayList();
38 }
39 }
40
41 public int columnIndex(String name) {
42 for (int i = 0; i < columnNames.length; ++i) {
43 if (columnNames[i].equals(name)) {
44 return i;
45 }
46 }
47 if (log.isDebugEnabled()) {
48 log.debug("columnIndex: " + name + " not found in columnNames");
49 }
50 return -1;
51 }
52
53 public int newRow() {
54
55 int N = columns[0].size();
56
57 for (int i = 0; i < columns.length; ++i) {
58 columns[i].add(Double.NaN);
59 }
60
61 return N;
62 }
63
64 public double get(int row, int index) {
65 return columns[index].getQuick(row);
66 }
67
68 public double get(int i, String columnName) {
69 int index = columnIndex(columnName);
70 return index >= 0
71 ? columns[index].getQuick(i)
72 : Double.NaN;
73 }
74
75 public void set(int row, int index, double value) {
76 columns[index].setQuick(row, value);
77 }
78
79 public void set(int i, String columnName, double value) {
80 int idx = columnIndex(columnName);
81 if (idx >= 0) {
82 columns[idx].setQuick(i, value);
83 }
84 }
85
86 public boolean set(int row, int [] indices, double [] values) {
87 boolean invalid = false;
88 for (int i = 0; i < indices.length; ++i) {
89 double v = values[i];
90 if (Double.isNaN(v)) {
91 invalid = true;
92 }
93 else {
94 columns[indices[i]].setQuick(row, v);
95 }
96 }
97 return invalid;
98 }
99
100 public boolean set(int row, String [] names, double [] values) {
101 boolean success = true;
102 for (int i = 0; i < names.length; ++i) {
103 int idx = columnIndex(names[i]);
104 if (idx >= 0) {
105 columns[idx].setQuick(row, values[i]);
106 }
107 else {
108 success = false;
109 }
110 }
111 return success;
112 }
113
114 public int size() {
115 return columns[0].size();
116 }
117
118 public int getNumberColumns() {
119 return columnNames.length;
120 }
121
122 public String [] getColumnNames() {
123 return columnNames;
124 }
125
126 public void removeNaNs() {
127 W.removeNaNs(columns);
128 }
129
130 public int [] columnIndices(String [] columns) {
131 int [] indices = new int[columns.length];
132 for (int i = 0; i < columns.length; ++i) {
133 indices[i] = columnIndex(columns[i]);
134 }
135 return indices;
136 }
137
138 public double getValue(int row, String column) {
139 int idx = columnIndex(column);
140 return idx >= 0
141 ? columns[idx].getQuick(row)
142 : Double.NaN;
143 }
144
145 public double [] get(int row, String [] columns) {
146 return get(row, columns, new double[columns.length]);
147 }
148
149 public double [] get(int row, String [] columns, double [] values) {
150 for (int i = 0; i < columns.length; ++i) {
151 int idx = columnIndex(columns[i]);
152 values[i] = idx < 0
153 ? Double.NaN
154 : this.columns[idx].getQuick(row);
155 }
156
157 return values;
158 }
159
160 public void get(int row, int [] columnIndices, double [] values) {
161 for (int i = 0; i < columnIndices.length; ++i) {
162 int index = columnIndices[i];
163 values[i] = index >= 0 && index < columns.length
164 ? columns[index].getQuick(row)
165 : Double.NaN;
166 }
167 }
168
169 public int binarySearch(String columnName, double value) {
170 return binarySearch(columnIndex(columnName), value);
171 }
172
173 /**
174 * Performes a binary search in the column identified by its
175 * index.
176 * @return Index of found element or negative insertion point (shifted by one)
177 */
178 public int binarySearch(int columnIndex, double value) {
179 TDoubleArrayList column = columns[columnIndex];
180 return column.binarySearch(value);
181 }
182
183 public int binarySearch(String columnName, double value, double epsilon) {
184 return binarySearch(columnIndex(columnName), value, epsilon);
185 }
186
187 public int binarySearch(int columnIndex, double value, double epsilon) {
188 if (epsilon < 0d) epsilon = -epsilon;
189 double vl = value - epsilon;
190 double vh = value + epsilon;
191
192 TDoubleArrayList column = columns[columnIndex];
193 int lo = 0, hi = column.size()-1;
194 while (hi >= lo) {
195 int mid = (lo + hi) >> 1;
196 double v = column.getQuick(mid);
197 if (v < vl) lo = mid + 1;
198 else if (v > vh) hi = mid - 1;
199 else return mid;
200 }
201
202 return -(lo + 1);
203 }
204
205 public double [] interpolate(int columnIndex, double key) {
206 return interpolate(columnIndex, key, new double[columns.length]);
207 }
208
209 public double [] interpolate(String columnName, double key) {
210 return interpolate(
211 columnIndex(columnName), key, new double[columns.length]);
212 }
213
214 public double [] interpolate(
215 String columnName,
216 double key,
217 double [] values
218 ) {
219 return interpolate(columnIndex(columnName), key, values);
220 }
221
222 public double [] interpolate(
223 int columnIndex,
224 double key,
225 double [] values
226 ) {
227 int row = binarySearch(columnIndex, key, EPSILON);
228
229 if (row >= 0) { // direct hit
230 for (int i = 0; i < values.length; ++i) {
231 values[i] = columns[i].getQuick(row);
232 }
233 }
234 else {
235 row = -row - 1;
236 if (row < 1 || row >= size()) {
237 return null;
238 }
239 double v1 = columns[columnIndex].getQuick(row-1);
240 double v2 = columns[columnIndex].getQuick(row);
241 double factor = Linear.factor(key, v1, v2);
242 for (int i = 0; i < values.length; ++i) {
243 values[i] = Linear.weight(
244 factor,
245 columns[i].getQuick(row-1),
246 columns[i].getQuick(row));
247 }
248 }
249 return values;
250 }
251
252
253 public double [] interpolate(
254 String keyName,
255 double key,
256 String [] columnNames
257 ) {
258 int keyIndex = columnIndex(keyName);
259 return keyIndex < 0
260 ? null
261 : interpolate(keyIndex, key, columnNames);
262 }
263
264 public double [] interpolate(
265 int keyIndex,
266 double key,
267 String [] columnNames
268 ) {
269 int row = binarySearch(keyIndex, key, EPSILON);
270
271 if (row >= 0) { // direct match
272 double [] values = new double[columnNames.length];
273 for (int i = 0; i < values.length; ++i) {
274 int ci = columnIndex(columnNames[i]);
275 values[i] = ci < 0
276 ? Double.NaN
277 : columns[ci].getQuick(row);
278 }
279 return values;
280 }
281
282 row = -row - 1;
283 if (row < 1 || row >= size()) {
284 log.debug("interpolate: row is out of bounds");
285 return null;
286 }
287
288 double v1 = columns[keyIndex].getQuick(row-1);
289 double v2 = columns[keyIndex].getQuick(row);
290 double factor = Linear.factor(key, v1, v2);
291
292 double [] values = new double[columnNames.length];
293
294 for (int i = 0; i < values.length; ++i) {
295 int ci = columnIndex(columnNames[i]);
296 values[i] = ci < 0
297 ? Double.NaN
298 : Linear.weight(
299 factor,
300 columns[ci].getQuick(row-1),
301 columns[ci].getQuick(row));
302 }
303
304 return values;
305 }
306
307 public boolean isSorted(String columnName) {
308 return isSorted(columnIndex(columnName));
309 }
310
311 public boolean isSorted(int columnIndex) {
312 TDoubleArrayList column = columns[columnIndex];
313 for (int i = 1, N = column.size(); i < N; ++i) {
314 if (column.getQuick(i-1) > column.getQuick(i)) {
315 return false;
316 }
317 }
318 return true;
319 }
320
321 public void visit(Visitor visitor) {
322 visit(visitor, new double[columns.length]);
323 }
324
325 public void visit(Visitor visitor, double [] data) {
326 for (int i = 0, R = size(); i < R; ++i) {
327 for (int j = 0; j < data.length; ++j) {
328 data[j] = columns[j].getQuick(i);
329 }
330 visitor.visit(data);
331 }
332 }
333 }
334 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org