altoslib: Finish AltosState changes. Update version number.
[fw/altos] / micropeak / MicroData.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.micropeak;
19
20 import java.lang.*;
21 import java.io.*;
22 import java.util.*;
23 import org.altusmetrum.altoslib_2.*;
24 import org.altusmetrum.altosuilib_1.*;
25
26 class MicroIterator implements Iterator<MicroDataPoint> {
27         int             i;
28         MicroData       data;
29
30         public boolean hasNext() {
31                 return i < data.pressures.length;
32         }
33
34         public MicroDataPoint next() {
35                 return new MicroDataPoint(data, i++);
36         }
37
38         public MicroIterator (MicroData data) {
39                 this.data = data;
40                 i = 0;
41         }
42
43         public void remove() {
44         }
45 }
46
47 class MicroIterable implements Iterable<MicroDataPoint> {
48
49         MicroData       data;
50
51         public Iterator<MicroDataPoint> iterator() {
52                 return new MicroIterator(data);
53         }
54
55         public MicroIterable(MicroData data) {
56                 this.data = data;
57         }
58 }
59
60 class MicroUIIterator implements Iterator<AltosUIDataPoint> {
61         int             i;
62         MicroData       data;
63
64         public boolean hasNext() {
65                 return i < data.pressures.length;
66         }
67
68         public AltosUIDataPoint next() {
69                 return new MicroDataPoint(data, i++);
70         }
71
72         public MicroUIIterator (MicroData data) {
73                 this.data = data;
74                 i = 0;
75         }
76
77         public void remove() {
78         }
79 }
80
81 class MicroUIIterable implements Iterable<AltosUIDataPoint> {
82         MicroData       data;
83
84         public Iterator<AltosUIDataPoint> iterator() {
85                 return new MicroUIIterator(data);
86         }
87
88         public MicroUIIterable(MicroData data) {
89                 this.data = data;
90         }
91 }
92
93 public class MicroData implements AltosUIDataSet {
94         public int              ground_pressure;
95         public int              min_pressure;
96         public int[]            pressures;
97         private double          time_step;
98         private double          ground_altitude;
99         private ArrayList<Integer>      bytes;
100         String                  name;
101         MicroStats              stats;
102         
103         class FileEndedException extends Exception {
104         }
105
106         class NonHexcharException extends Exception {
107         }
108
109         class InvalidCrcException extends Exception {
110         }
111
112         private int getc(InputStream f) throws IOException, FileEndedException {
113                 int     c = f.read();
114
115                 if (c == -1)
116                         throw new FileEndedException();
117                 bytes.add(c);
118                 return c;
119         }
120
121         private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
122                 int     c;
123
124                 for (;;) {
125                         c = getc(f);
126                         if (!Character.isWhitespace(c))
127                                 return c;
128                 }
129         }
130
131         private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
132                 int     c = get_nonwhite(f);
133
134                 if ('0' <= c && c <= '9')
135                         return c - '0';
136                 if ('a' <= c && c <= 'f')
137                         return c - 'a' + 10;
138                 if ('A' <= c && c <= 'F')
139                         return c - 'A' + 10;
140                 throw new NonHexcharException();
141         }
142
143         private static final int POLY = 0x8408;
144
145         private int log_crc(int crc, int b) {
146                 int     i;
147
148                 for (i = 0; i < 8; i++) {
149                         if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
150                                 crc = (crc >> 1) ^ POLY;
151                         else
152                                 crc = crc >> 1;
153                         b >>= 1;
154                 }
155                 return crc & 0xffff;
156         }
157
158         int     file_crc;
159
160         private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
161                 int     a = get_hexc(f);
162                 int     b = get_hexc(f);
163
164                 int h = (a << 4) + b;
165
166                 file_crc = log_crc(file_crc, h);
167                 return h;
168         }
169
170         private boolean find_header(InputStream f) throws IOException {
171                 try {
172                         for (;;) {
173                                 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
174                                         return true;
175                         }
176                 } catch (FileEndedException fe) {
177                         return false;
178                 }
179         } 
180
181         private int get_32(InputStream f)  throws IOException, FileEndedException, NonHexcharException {
182                 int     v = 0;
183                 for (int i = 0; i < 4; i++) {
184                         v += get_hex(f) << (i * 8);
185                 }
186                 return v;
187         }
188
189         private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
190                 int     v = 0;
191                 for (int i = 0; i < 2; i++) {
192                         v += get_hex(f) << (i * 8);
193                 }
194                 return v;
195         }
196
197         private int swap16(int i) {
198                 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
199         }
200
201         public boolean  crc_valid;
202
203         int mix_in (int high, int low) {
204                 return  high - (high & 0xffff) + low;
205         }
206
207         boolean closer (int target, int a, int b) {
208                 return Math.abs (target - a) < Math.abs(target - b);
209         }
210
211         public double altitude(int i) {
212                 return AltosConvert.pressure_to_altitude(pressures[i]);
213         }
214
215         public String name() {
216                 return name;
217         }
218
219         public Iterable<AltosUIDataPoint> dataPoints() {
220                 return new MicroUIIterable(this);
221         }
222
223         public Iterable<MicroDataPoint> points() {
224                 return new MicroIterable(this);
225         }
226
227         int fact(int n) {
228                 if (n == 0)
229                         return 1;
230                 return n * fact(n-1);
231         }
232
233         int choose(int n, int k) {
234                 return fact(n) / (fact(k) * fact(n-k));
235         }
236
237
238         public double avg_altitude(int center, int dist) {
239                 int     start = center - dist;
240                 int     stop = center + dist;
241
242                 if (start < 0)
243                         start = 0;
244                 if (stop >= pressures.length)
245                         stop = pressures.length - 1;
246
247                 double  sum = 0;
248                 double  div = 0;
249
250                 int     n = dist * 2;
251
252                 for (int i = start; i <= stop; i++) {
253                         int     k = i - (center - dist);
254                         int     c = choose (n, k);
255
256                         sum += c * pressures[i];
257                         div += c;
258                 }
259
260                 double pres = sum / div;
261
262                 double alt = AltosConvert.pressure_to_altitude(pres);
263                 return alt;
264         }
265
266         public double pressure(int i) {
267                 return pressures[i];
268         }
269
270         public double height(int i) {
271                 return altitude(i) - ground_altitude;
272         }
273
274         public double apogee_pressure() {
275                 return min_pressure;
276         }
277
278         public double apogee_altitude() {
279                 return AltosConvert.pressure_to_altitude(apogee_pressure());
280         }
281
282         public double apogee_height() {
283                 return apogee_altitude() - ground_altitude;
284         }
285
286         static final int speed_avg = 3;
287         static final int accel_avg = 5;
288
289         private double avg_speed(int center, int dist) {
290                 if (center == 0)
291                         return 0;
292
293                 double ai = avg_altitude(center, dist);
294                 double aj = avg_altitude(center - 1, dist);
295                 double s = (ai - aj) / time_step;
296
297                 return s;
298         }
299
300         public double speed(int i) {
301                 return avg_speed(i, speed_avg);
302         }
303
304         public double acceleration(int i) {
305                 if (i == 0)
306                         return 0;
307                 return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step;
308         }
309
310         public double time(int i) {
311                 return i * time_step;
312         }
313
314         public void save (OutputStream f) throws IOException {
315                 for (int c : bytes)
316                         f.write(c);
317                 f.write('\n');
318         }
319
320         public void export (Writer f) throws IOException {
321                 PrintWriter     pw = new PrintWriter(f);
322                 pw.printf("  Time, Press(Pa), Height(m), Height(f), Speed(m/s), Speed(mph), Speed(mach), Accel(m/s²), Accel(ft/s²),  Accel(g)\n");
323                 for (MicroDataPoint point : points()) {
324                         pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
325                                   point.time,
326                                   point.pressure,
327                                   point.height,
328                                   AltosConvert.meters_to_feet(point.height),
329                                   point.speed,
330                                   AltosConvert.meters_to_mph(point.speed),
331                                   AltosConvert.meters_to_mach(point.speed),
332                                   point.accel,
333                                   AltosConvert.meters_to_feet(point.accel),
334                                   AltosConvert.meters_to_g(point.accel));
335                 }
336         }
337
338         public void set_name(String name) {
339                 this.name = name;
340         }
341
342         public MicroData (InputStream f, String name) throws IOException, InterruptedException {
343                 this.name = name;
344                 bytes = new ArrayList<Integer>();
345                 if (!find_header(f))
346                         throw new IOException("No MicroPeak data header found");
347                 try {
348                         file_crc = 0xffff;
349                         ground_pressure = get_32(f);
350                         min_pressure = get_32(f);
351                         int nsamples = get_16(f);
352                         pressures = new int[nsamples + 1];
353
354                         ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
355                         int cur = ground_pressure;
356                         pressures[0] = cur;
357                         for (int i = 0; i < nsamples; i++) {
358                                 int     k = get_16(f);
359                                 int     same = mix_in(cur, k);
360                                 int     up = mix_in(cur + 0x10000, k);
361                                 int     down = mix_in(cur - 0x10000, k);
362
363                                 if (closer (cur, same, up)) {
364                                         if (closer (cur, same, down))
365                                                 cur = same;
366                                         else
367                                                 cur = down;
368                                 } else {
369                                         if (closer (cur, up, down))
370                                                 cur = up;
371                                         else
372                                                 cur = down;
373                                 }
374                                 
375                                 pressures[i+1] = cur;
376                         }
377
378                         int current_crc = swap16(~file_crc & 0xffff);
379                         int crc = get_16(f);
380
381                         crc_valid = crc == current_crc;
382
383                         time_step = 0.192;
384                         stats = new MicroStats(this);
385                 } catch (FileEndedException fe) {
386                         throw new IOException("File Ended Unexpectedly");
387                 } catch (NonHexcharException ne) {
388                         throw new IOException("Non hexadecimal character found");
389                 }
390         }
391
392         public MicroData() {
393                 ground_pressure = 101000;
394                 min_pressure = 101000;
395                 pressures = new int[1];
396                 pressures[0] = 101000;
397         }
398         
399 }