2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.micropeak;
23 import org.altusmetrum.altoslib_3.*;
24 import org.altusmetrum.altosuilib_1.*;
26 class MicroIterator implements Iterator<MicroDataPoint> {
30 public boolean hasNext() {
31 return i < data.pressures.length;
34 public MicroDataPoint next() {
35 return new MicroDataPoint(data, i++);
38 public MicroIterator (MicroData data) {
43 public void remove() {
47 class MicroIterable implements Iterable<MicroDataPoint> {
51 public Iterator<MicroDataPoint> iterator() {
52 return new MicroIterator(data);
55 public MicroIterable(MicroData data) {
60 class MicroUIIterator implements Iterator<AltosUIDataPoint> {
64 public boolean hasNext() {
65 return i < data.pressures.length;
68 public AltosUIDataPoint next() {
69 return new MicroDataPoint(data, i++);
72 public MicroUIIterator (MicroData data) {
77 public void remove() {
81 class MicroUIIterable implements Iterable<AltosUIDataPoint> {
84 public Iterator<AltosUIDataPoint> iterator() {
85 return new MicroUIIterator(data);
88 public MicroUIIterable(MicroData data) {
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;
103 class FileEndedException extends Exception {
106 class NonHexcharException extends Exception {
109 class InvalidCrcException extends Exception {
112 private int getc(InputStream f) throws IOException, FileEndedException {
116 throw new FileEndedException();
121 private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
126 if (!Character.isWhitespace(c))
131 private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
132 int c = get_nonwhite(f);
134 if ('0' <= c && c <= '9')
136 if ('a' <= c && c <= 'f')
138 if ('A' <= c && c <= 'F')
140 throw new NonHexcharException();
143 private static final int POLY = 0x8408;
145 private int log_crc(int crc, int b) {
148 for (i = 0; i < 8; i++) {
149 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
150 crc = (crc >> 1) ^ POLY;
160 private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
164 int h = (a << 4) + b;
166 file_crc = log_crc(file_crc, h);
170 private boolean find_header(InputStream f) throws IOException {
173 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
176 } catch (FileEndedException fe) {
181 private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException {
183 for (int i = 0; i < 4; i++) {
184 v += get_hex(f) << (i * 8);
189 private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
191 for (int i = 0; i < 2; i++) {
192 v += get_hex(f) << (i * 8);
197 private int swap16(int i) {
198 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
201 public boolean crc_valid;
203 int mix_in (int high, int low) {
204 return high - (high & 0xffff) + low;
207 boolean closer (int target, int a, int b) {
208 return Math.abs (target - a) < Math.abs(target - b);
211 public double altitude(int i) {
212 return AltosConvert.pressure_to_altitude(pressures[i]);
215 public String name() {
219 public Iterable<AltosUIDataPoint> dataPoints() {
220 return new MicroUIIterable(this);
223 public Iterable<MicroDataPoint> points() {
224 return new MicroIterable(this);
230 return n * fact(n-1);
233 int choose(int n, int k) {
234 return fact(n) / (fact(k) * fact(n-k));
238 public double avg_altitude(int center, int dist) {
239 int start = center - dist;
240 int stop = center + dist;
244 if (stop >= pressures.length)
245 stop = pressures.length - 1;
252 for (int i = start; i <= stop; i++) {
253 int k = i - (center - dist);
254 int c = choose (n, k);
256 sum += c * pressures[i];
260 double pres = sum / div;
262 double alt = AltosConvert.pressure_to_altitude(pres);
266 public double pressure(int i) {
270 public double height(int i) {
271 return altitude(i) - ground_altitude;
274 public double apogee_pressure() {
278 public double apogee_altitude() {
279 return AltosConvert.pressure_to_altitude(apogee_pressure());
282 public double apogee_height() {
283 return apogee_altitude() - ground_altitude;
286 static final int speed_avg = 3;
287 static final int accel_avg = 5;
289 private double avg_speed(int center, int dist) {
293 double ai = avg_altitude(center, dist);
294 double aj = avg_altitude(center - 1, dist);
295 double s = (ai - aj) / time_step;
300 public double speed(int i) {
301 return avg_speed(i, speed_avg);
304 public double acceleration(int i) {
307 return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step;
310 public double time(int i) {
311 return i * time_step;
314 public void save (OutputStream f) throws IOException {
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",
328 AltosConvert.meters_to_feet(point.height),
330 AltosConvert.meters_to_mph(point.speed),
331 AltosConvert.meters_to_mach(point.speed),
333 AltosConvert.meters_to_feet(point.accel),
334 AltosConvert.meters_to_g(point.accel));
338 public void set_name(String name) {
342 public MicroData (InputStream f, String name) throws IOException, InterruptedException {
344 bytes = new ArrayList<Integer>();
346 throw new IOException("No MicroPeak data header found");
349 ground_pressure = get_32(f);
350 min_pressure = get_32(f);
351 int nsamples = get_16(f);
352 pressures = new int[nsamples + 1];
354 ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
355 int cur = ground_pressure;
357 for (int i = 0; i < nsamples; i++) {
359 int same = mix_in(cur, k);
360 int up = mix_in(cur + 0x10000, k);
361 int down = mix_in(cur - 0x10000, k);
363 if (closer (cur, same, up)) {
364 if (closer (cur, same, down))
369 if (closer (cur, up, down))
375 pressures[i+1] = cur;
378 int current_crc = swap16(~file_crc & 0xffff);
381 crc_valid = crc == current_crc;
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");
393 ground_pressure = 101000;
394 min_pressure = 101000;
395 pressures = new int[1];
396 pressures[0] = 101000;