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_1.*;
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;
102 class FileEndedException extends Exception {
105 class NonHexcharException extends Exception {
108 class InvalidCrcException extends Exception {
111 private int getc(InputStream f) throws IOException, FileEndedException {
115 throw new FileEndedException();
120 private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
125 if (!Character.isWhitespace(c))
130 private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
131 int c = get_nonwhite(f);
133 if ('0' <= c && c <= '9')
135 if ('a' <= c && c <= 'f')
137 if ('A' <= c && c <= 'F')
139 throw new NonHexcharException();
142 private static final int POLY = 0x8408;
144 private int log_crc(int crc, int b) {
147 for (i = 0; i < 8; i++) {
148 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
149 crc = (crc >> 1) ^ POLY;
159 private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
163 int h = (a << 4) + b;
165 file_crc = log_crc(file_crc, h);
169 private boolean find_header(InputStream f) throws IOException {
172 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
175 } catch (FileEndedException fe) {
180 private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException {
182 for (int i = 0; i < 4; i++) {
183 v += get_hex(f) << (i * 8);
188 private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
190 for (int i = 0; i < 2; i++) {
191 v += get_hex(f) << (i * 8);
196 private int swap16(int i) {
197 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
200 public boolean crc_valid;
202 int mix_in (int high, int low) {
203 return high - (high & 0xffff) + low;
206 boolean closer (int target, int a, int b) {
207 return Math.abs (target - a) < Math.abs(target - b);
210 public double altitude(int i) {
211 return AltosConvert.pressure_to_altitude(pressures[i]);
214 public String name() {
218 public Iterable<AltosUIDataPoint> dataPoints() {
219 return new MicroUIIterable(this);
222 public Iterable<MicroDataPoint> points() {
223 return new MicroIterable(this);
229 return n * fact(n-1);
232 int choose(int n, int k) {
233 return fact(n) / (fact(k) * fact(n-k));
237 public double avg_altitude(int center, int dist) {
238 int start = center - dist;
239 int stop = center + dist;
243 if (stop >= pressures.length)
244 stop = pressures.length - 1;
251 for (int i = start; i <= stop; i++) {
252 int k = i - (center - dist);
253 int c = choose (n, k);
255 sum += c * pressures[i];
259 double pres = sum / div;
261 double alt = AltosConvert.pressure_to_altitude(pres);
265 public double pressure(int i) {
269 public double height(int i) {
270 return altitude(i) - ground_altitude;
273 public double apogee_pressure() {
277 public double apogee_altitude() {
278 return AltosConvert.pressure_to_altitude(apogee_pressure());
281 public double apogee_height() {
282 return apogee_altitude() - ground_altitude;
285 static final int speed_avg = 3;
286 static final int accel_avg = 5;
288 private double avg_speed(int center, int dist) {
292 double ai = avg_altitude(center, dist);
293 double aj = avg_altitude(center - 1, dist);
294 double s = (ai - aj) / time_step;
299 public double speed(int i) {
300 return avg_speed(i, speed_avg);
303 public double acceleration(int i) {
306 return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step;
309 public double time(int i) {
310 return i * time_step;
313 public void save (OutputStream f) throws IOException {
319 public void export (Writer f) throws IOException {
320 PrintWriter pw = new PrintWriter(f);
321 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");
322 for (MicroDataPoint point : points()) {
323 pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
327 AltosConvert.meters_to_feet(point.height),
329 AltosConvert.meters_to_mph(point.speed),
330 AltosConvert.meters_to_mach(point.speed),
332 AltosConvert.meters_to_feet(point.accel),
333 AltosConvert.meters_to_g(point.accel));
337 public void set_name(String name) {
341 public MicroData (InputStream f, String name) throws IOException, InterruptedException {
343 bytes = new ArrayList<Integer>();
345 throw new IOException("No MicroPeak data header found");
348 ground_pressure = get_32(f);
349 min_pressure = get_32(f);
350 int nsamples = get_16(f);
351 pressures = new int[nsamples + 1];
353 ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
354 int cur = ground_pressure;
356 for (int i = 0; i < nsamples; i++) {
358 int same = mix_in(cur, k);
359 int up = mix_in(cur + 0x10000, k);
360 int down = mix_in(cur - 0x10000, k);
362 if (closer (cur, same, up)) {
363 if (closer (cur, same, down))
368 if (closer (cur, up, down))
374 pressures[i+1] = cur;
377 int current_crc = swap16(~file_crc & 0xffff);
380 crc_valid = crc == current_crc;
383 } catch (FileEndedException fe) {
384 throw new IOException("File Ended Unexpectedly");
385 } catch (NonHexcharException ne) {
386 throw new IOException("Non hexadecimal character found");
391 ground_pressure = 101000;
392 min_pressure = 101000;
393 pressures = new int[1];
394 pressures[0] = 101000;