-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathDataFile.java
More file actions
414 lines (398 loc) · 14.2 KB
/
DataFile.java
File metadata and controls
414 lines (398 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import org.opensourcephysics.controls.OSPLog;
import org.opensourcephysics.controls.XML;
import org.opensourcephysics.controls.XMLControlElement;
import org.opensourcephysics.tools.Resource;
import org.opensourcephysics.tools.ResourceLoader;
public class DataFile extends DataAdapter {
java.util.List<Data> dataList = null;
protected static String[] delimiters = new String[] {" ", "\t", ",", ";"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
/**
* Creates a DataFile using data in the given file.
*
* @param fileName
*/
public DataFile(String fileName) {
super(null);
if(fileName!=null) {
open(fileName);
}
}
/**
* Some objects (eg, a Group) do not contain data, but a list of Data
* objects that do. This method is used by Data displaying tools to create
* as many pages as needed.
*
* @return a list of Data objects, or null if this object contains data
*/
public java.util.List<Data> getDataList() {
return dataList;
}
/**
* Opens an xml or data file specified by name.
*
* @param fileName the file name
* @return the file name, if successfully opened
*/
public String open(String fileName) {
dataList = null;
data = null;
OSPLog.fine("opening "+fileName); //$NON-NLS-1$
Resource res = ResourceLoader.getResource(fileName);
if(res!=null) {
Reader in = res.openReader();
String firstLine = readFirstLine(in);
// if xml, read the file into an XML control and add tab
if(firstLine.startsWith("<?xml")) { //$NON-NLS-1$
XMLControlElement control = new XMLControlElement(fileName);
dataList = control.getObjects(Data.class);
return fileName;
}
// if not xml, attempt to import data and add tab
else if(res.getString()!=null) {
data = parseData(res.getString(), fileName);
if(data!=null) {
return fileName;
}
}
}
OSPLog.finest("no data found"); //$NON-NLS-1$
return null;
}
/**
* Parses character-delimited data from a string. This attempts to extract
* the following information from the string:
*
* 1. A title to be used for the tab name 2. One or more columns of double
* data values 3. Column names for the data columns
*
* @param dataString the data string
* @param fileName name of file containing the data string (may be null)
* @return DatasetManager with parsed data, or null if none found
*/
public double[][] parseData(String dataString, String fileName) {
BufferedReader input = new BufferedReader(new StringReader(dataString));
String gnuPlotComment = "#"; //$NON-NLS-1$
try {
String textLine = input.readLine();
for(int i = 0; i<DataFile.delimiters.length; i++) {
ArrayList<double[]> rows = new ArrayList<double[]>();
int columns = Integer.MAX_VALUE;
String[] columnNames = null;
String title = null;
int lineCount = 0;
while(textLine!=null) { // process each line of text
// look for gnuPlot-commented name and/or columnNames
if(textLine.contains(gnuPlotComment)) {
textLine = textLine.trim();
}
if(textLine.startsWith(gnuPlotComment)) {
int k = textLine.indexOf("name:"); //$NON-NLS-1$
if(k>-1) {
title = textLine.substring(k+5).trim();
}
k = textLine.indexOf("columnNames:"); //$NON-NLS-1$
if(k>-1) {
textLine = textLine.substring(k+12).trim();
} else {
textLine = input.readLine();
continue;
}
}
// skip Vernier Format 2 header lines
if((textLine.indexOf("Vernier Format")>-1 //$NON-NLS-1$
)||(textLine.indexOf(".cmbl")>-1)) { //$NON-NLS-1$
textLine = input.readLine();
continue;
}
String[] strings = DataFile.parseStrings(textLine, DataFile.delimiters[i]);
double[] rowData = DataFile.parseDoubles(strings);
// set null title if String[] length > 0, all entries
// are NaN and only one entry is not ""
if(rows.isEmpty()&&(strings.length>0)&&(title==null)) {
String s = ""; //$NON-NLS-1$
for(int k = 0; k<strings.length; k++) {
if(Double.isNaN(rowData[k])&&!strings[k].equals("")) { //$NON-NLS-1$
if(s.equals("")) { //$NON-NLS-1$
s = strings[k];
} else {
s = ""; //$NON-NLS-1$
break;
}
}
}
if(!s.equals("")) { //$NON-NLS-1$
title = s;
textLine = input.readLine();
continue;
}
}
// set null column names if String[] length > 0,
// all entries are NaN and none is ""
if(rows.isEmpty()&&(strings.length>0)&&(columnNames==null)) {
boolean valid = true;
for(int k = 0; k<strings.length; k++) {
if(!Double.isNaN(rowData[k])||strings[k].equals("")) { //$NON-NLS-1$
valid = false;
break;
}
}
if(valid) {
columnNames = strings;
textLine = input.readLine();
continue;
}
}
// add double[] of length 1 or longer to rows
if(strings.length>0) {
lineCount++;
boolean validData = true;
boolean emptyData = true;
for(int k = 0; k<strings.length; k++) {
// invalid if any NaN entries other than ""
if(Double.isNaN(rowData[k])&&!strings[k].equals("")) { //$NON-NLS-1$
validData = false;
}
// look for empty row--every entry is ""
if(!strings[k].equals("")) { //$NON-NLS-1$
emptyData = false;
}
}
if(rows.isEmpty()&&emptyData) {
validData = false;
}
// add valid data, but ignore blank lines
// that may precede real data
if(validData) {
rows.add(rowData);
columns = Math.min(rowData.length, columns);
}
}
// abort processing if no data found in first several lines
if(rows.isEmpty()&&(lineCount>10)) {
break;
}
textLine = input.readLine();
}
// create array if data found
if(!rows.isEmpty()&&(columns>0)) {
input.close();
// first reassemble data from rows into columns
double[][] dataArray = new double[columns][rows.size()];
for(int row = 0; row<rows.size(); row++) {
double[] next = rows.get(row);
for(int j = 0; j<columns; j++) {
dataArray[j][row] = next[j];
}
}
setName((title==null) ? XML.getName(fileName) : title);
this.setColumnNames(columnNames);
OSPLog.finest("data found using delimiter \"" //$NON-NLS-1$
+DataFile.delimiters[i]+"\""); //$NON-NLS-1$
return dataArray;
}
// close the reader and open a new one
input.close();
input = new BufferedReader(new StringReader(dataString));
textLine = input.readLine();
}
} catch(IOException e) {
e.printStackTrace();
}
try {
input.close();
} catch(IOException ex) {
ex.printStackTrace();
}
return null;
}
// ______________________________ protected methods ________________________
protected String readFirstLine(Reader in) {
BufferedReader input = null;
if(in instanceof BufferedReader) {
input = (BufferedReader) in;
} else {
input = new BufferedReader(in);
}
String openingLine;
try {
openingLine = input.readLine();
while((openingLine==null)||openingLine.equals("")) { //$NON-NLS-1$
openingLine = input.readLine();
}
} catch(IOException e) {
e.printStackTrace();
return null;
}
try {
input.close();
} catch(IOException ex) {
ex.printStackTrace();
}
return openingLine;
}
/**
* Parses a String into tokens separated by a specified delimiter. A token
* may be "".
*
* @param text the text to parse
* @param delimiter the delimiter
* @return an array of String tokens
*/
protected static String[] parseStrings(String text, String delimiter) {
Collection<String> tokens = new ArrayList<String>();
if(text!=null) {
// get the first token
String next = text;
int i = text.indexOf(delimiter);
if(i==-1) { // no delimiter
tokens.add(stripQuotes(next));
text = null;
} else {
next = text.substring(0, i);
text = text.substring(i+1);
}
// iterate thru the tokens and add to token list
while(text!=null) {
tokens.add(stripQuotes(next));
i = text.indexOf(delimiter);
if(i==-1) { // no delimiter
next = text;
tokens.add(stripQuotes(next));
text = null;
} else {
next = text.substring(0, i).trim();
text = text.substring(i+1);
}
}
}
return tokens.toArray(new String[0]);
}
/**
* Parses a String into tokens separated by specified row and column
* delimiters.
*
* @param text the text to parse
* @param rowDelimiter the column delimiter
* @param colDelimiter the column delimiter
* @return a 2D array of String tokens
*/
protected static String[][] parseStrings(String text, String rowDelimiter, String colDelimiter) {
String[] rows = parseStrings(text, rowDelimiter);
String[][] tokens = new String[rows.length][0];
for(int i = 0; i<rows.length; i++) {
tokens[i] = parseStrings(rows[i], colDelimiter);
}
return tokens;
}
/**
* Parses a String array into doubles. Unparsable strings are set to
* Double.NaN.
*
* @param strings the String array to parse
* @return an array of doubles
*/
protected static double[] parseDoubles(String[] strings) {
double[] doubles = new double[strings.length];
for(int i = 0; i<strings.length; i++) {
if(strings[i].indexOf("\t")>-1) { //$NON-NLS-1$
doubles[i] = Double.NaN;
} else {
try {
doubles[i] = Double.parseDouble(strings[i]);
} catch(NumberFormatException e) {
doubles[i] = Double.NaN;
}
}
}
return doubles;
}
/**
* Parses a String into doubles separated by specified row and column
* delimiters.
*
* @param text the text to parse
* @param rowDelimiter the column delimiter
* @param colDelimiter the column delimiter
* @return a 2D array of doubles
*/
protected static double[][] parseDoubles(String text, String rowDelimiter, String colDelimiter) {
String[][] strings = parseStrings(text, rowDelimiter, colDelimiter);
double[][] doubles = new double[strings.length][0];
for(int i = 0; i<strings.length; i++) {
double[] row = new double[strings[i].length];
for(int j = 0; j<row.length; j++) {
try {
row[j] = Double.parseDouble(strings[i][j]);
} catch(NumberFormatException e) {
row[j] = Double.NaN;
}
}
doubles[i] = row;
}
return doubles;
}
/**
* Strips quotation marks around a string.
*
* @param text the text to strip
* @return the stripped string
*/
private static String stripQuotes(String text) {
if(text.startsWith("\"")) { //$NON-NLS-1$
String stripped = text.substring(1);
int n = stripped.indexOf("\""); //$NON-NLS-1$
if(n==stripped.length()-1) {
return stripped.substring(0, n);
}
}
return text;
}
/**
* Returns an array of row numbers.
*
* @param rowCount length of the array
* @return the array
*/
protected static double[] getRowArray(int rowCount) {
double[] rows = new double[rowCount];
for(int i = 0; i<rowCount; i++) {
rows[i] = i;
}
return rows;
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/