-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatTable.pde
196 lines (161 loc) · 4.52 KB
/
FloatTable.pde
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
// Modified version of Ben Fry's data table
// first line of the file should be the column headers
// first column should be the row titles
// all other values are expected to be floats
// getFloat(0, 0) returns the first data value in the upper lefthand corner
// files should be saved as "text, tab-delimited"
// empty rows are ignored
// extra whitespace is ignored
// Modified to read CSV; modified so that first two columns are coordinates
class FloatTable {
int rowCount;
int columnCount;
float[][] data;
int[] eastings;
int[] northings;
String[] columnNames;
FloatTable(String filename) {
String[] rows = loadStrings(filename);
String[] columns = split(rows[0], ',');
columnNames = subset(columns, 2); // Start at 2 to skip coordinates
scrubQuotes(columnNames);
columnCount = columnNames.length;
data = new float[rows.length-1][];
eastings = new int[rows.length-1];
northings = new int[rows.length-1];
// start reading at row 1, because the first row was only the column headers
for (int i = 1; i < rows.length; i++) {
if (trim(rows[i]).length() == 0) {
continue; // skip empty rows
}
if (rows[i].startsWith("#")) {
continue; // skip comment lines
}
// split the row on the commas
String[] pieces = split(rows[i], ',');
scrubQuotes(pieces);
// copy row title
eastings[rowCount] = parseInt(pieces[0]);
northings[rowCount]= parseInt(pieces[1]);
// copy data into the table starting at pieces[1]
data[rowCount] = parseFloat(subset(pieces, 2));
// increment the number of valid rows found so far
rowCount++;
}
// resize the 'data' array as necessary
data = (float[][]) subset(data, 0, rowCount);
}
void scrubQuotes(String[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i].length() > 2) {
// remove quotes at start and end, if present
if (array[i].startsWith("\"") && array[i].endsWith("\"")) {
array[i] = array[i].substring(1, array[i].length() - 1);
}
}
// make double quotes into single quotes
array[i] = array[i].replaceAll("\"\"", "\"");
}
}
int getRowCount() {
return rowCount;
}
int[] getEastings(){
return eastings;
}
int[] getNorthings(){
return northings;
}
// technically, this only returns the number of columns
// in the very first row (which will be most accurate)
int getColumnCount() {
return columnCount;
}
String getColumnName(int colIndex) {
return columnNames[colIndex];
}
String[] getColumnNames() {
return columnNames;
}
float getFloat(int rowIndex, int col) {
return data[rowIndex][col];
}
boolean isValid(int row, int col) {
if (row < 0) return false;
if (row >= rowCount) return false;
if (col >= data[row].length) return false;
if (col < 0) return false;
if(data[row][col] == -9) return false;
return !Float.isNaN(data[row][col]);
}
float getColumnMin(int col) {
float m = Float.MAX_VALUE;
for (int i = 0; i < rowCount; i++) {
if (isValid(i, col)) { // Try with isValid
if (data[i][col] < m) {
m = data[i][col];
}
}
}
return m;
}
float getColumnMax(int col) {
float m = -Float.MAX_VALUE;
for (int i = 0; i < rowCount; i++) {
if (isValid(i, col)) {
if (data[i][col] > m) {
m = data[i][col];
}
}
}
return m;
}
float getRowMin(int row) {
float m = Float.MAX_VALUE;
for (int i = 0; i < columnCount; i++) {
if (isValid(row, i)) {
if (data[row][i] < m) {
m = data[row][i];
}
}
}
return m;
}
float getRowMax(int row) {
float m = -Float.MAX_VALUE;
for (int i = 1; i < columnCount; i++) {
if (isValid(row, i)) {
if (data[row][i] > m) {
m = data[row][i];
}
}
}
return m;
}
float getTableMin() {
float m = Float.MAX_VALUE;
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
if (isValid(i, j)) {
if (data[i][j] < m) {
m = data[i][j];
}
}
}
}
return m;
}
float getTableMax() {
float m = -Float.MAX_VALUE;
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
if (isValid(i, j)) {
if (data[i][j] > m) {
m = data[i][j];
}
}
}
}
return m;
}
}