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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.micropeak;
24 import org.altusmetrum.altoslib_11.*;
25 import org.altusmetrum.altosuilib_11.*;
27 class MicroIterator implements Iterator<MicroDataPoint> {
31 public boolean hasNext() {
32 return i < data.pressures.length;
35 public MicroDataPoint next() {
36 return new MicroDataPoint(data, i++);
39 public MicroIterator (MicroData data) {
44 public void remove() {
48 class MicroIterable implements Iterable<MicroDataPoint> {
52 public Iterator<MicroDataPoint> iterator() {
53 return new MicroIterator(data);
56 public MicroIterable(MicroData data) {
61 class MicroUIIterator implements Iterator<AltosUIDataPoint> {
65 public boolean hasNext() {
66 return i < data.pressures.length;
69 public AltosUIDataPoint next() {
70 return new MicroDataPoint(data, i++);
73 public MicroUIIterator (MicroData data) {
78 public void remove() {
82 class MicroUIIterable implements Iterable<AltosUIDataPoint> {
85 public Iterator<AltosUIDataPoint> iterator() {
86 return new MicroUIIterator(data);
89 public MicroUIIterable(MicroData data) {
94 public class MicroData implements AltosUIDataSet {
95 public int ground_pressure;
96 public int min_pressure;
97 public int[] pressures;
98 private double time_step;
99 private double ground_altitude;
100 private ArrayList<Integer> bytes;
105 public static final int LOG_ID_MICROPEAK = 0;
106 public static final int LOG_ID_MICROKITE = 1;
108 public static final double CLOCK = 0.096;
110 public class FileEndedException extends Exception {
113 public class NonHexcharException extends Exception {
116 public class InvalidCrcException extends Exception {
119 private int getc(InputStream f) throws IOException, FileEndedException {
123 throw new FileEndedException();
128 private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
133 if (!Character.isWhitespace(c))
138 private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
139 int c = get_nonwhite(f);
141 if ('0' <= c && c <= '9')
143 if ('a' <= c && c <= 'f')
145 if ('A' <= c && c <= 'F')
147 throw new NonHexcharException();
150 private static final int POLY = 0x8408;
152 private int log_crc(int crc, int b) {
155 for (i = 0; i < 8; i++) {
156 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
157 crc = (crc >> 1) ^ POLY;
167 private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
171 int h = (a << 4) + b;
173 file_crc = log_crc(file_crc, h);
177 private boolean find_header(InputStream f) throws IOException, FileEndedException {
179 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
184 private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException {
186 for (int i = 0; i < 4; i++) {
187 v += get_hex(f) << (i * 8);
192 private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
194 for (int i = 0; i < 2; i++) {
195 v += get_hex(f) << (i * 8);
200 private int swap16(int i) {
201 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
204 public boolean crc_valid;
206 int mix_in (int high, int low) {
207 return high - (high & 0xffff) + low;
210 boolean closer (int target, int a, int b) {
211 return Math.abs (target - a) < Math.abs(target - b);
214 public double altitude(int i) {
215 return AltosConvert.pressure_to_altitude(pressures[i]);
218 public String name() {
222 public Iterable<AltosUIDataPoint> dataPoints() {
223 return new MicroUIIterable(this);
226 public Iterable<MicroDataPoint> points() {
227 return new MicroIterable(this);
233 return n * fact(n-1);
236 int choose(int n, int k) {
237 return fact(n) / (fact(k) * fact(n-k));
241 public double avg_altitude(int center, int dist) {
242 int start = center - dist;
243 int stop = center + dist;
247 if (stop >= pressures.length)
248 stop = pressures.length - 1;
255 for (int i = start; i <= stop; i++) {
256 int k = i - (center - dist);
257 int c = choose (n, k);
259 sum += c * pressures[i];
263 double pres = sum / div;
265 double alt = AltosConvert.pressure_to_altitude(pres);
269 public double pressure(int i) {
273 public double height(int i) {
274 return altitude(i) - ground_altitude;
277 public double apogee_pressure() {
281 public double apogee_altitude() {
282 return AltosConvert.pressure_to_altitude(apogee_pressure());
285 public double apogee_height() {
286 return apogee_altitude() - ground_altitude;
289 static final int speed_avg = 3;
290 static final int accel_avg = 5;
292 private double avg_speed(int center, int dist) {
296 double ai = avg_altitude(center, dist);
297 double aj = avg_altitude(center - 1, dist);
298 double s = (ai - aj) / time_step;
303 public double speed(int i) {
304 return avg_speed(i, speed_avg);
307 public double acceleration(int i) {
310 return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step;
313 public double time(int i) {
314 return i * time_step;
317 public void save (OutputStream f) throws IOException {
323 public void export (Writer f) throws IOException {
324 PrintWriter pw = new PrintWriter(f);
325 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");
326 for (MicroDataPoint point : points()) {
327 pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
331 AltosConvert.meters_to_feet(point.height),
333 AltosConvert.meters_to_mph(point.speed),
334 AltosConvert.meters_to_mach(point.speed),
336 AltosConvert.meters_to_feet(point.accel),
337 AltosConvert.meters_to_g(point.accel));
341 public void set_name(String name) {
345 public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
347 bytes = new ArrayList<Integer>();
349 throw new IOException("No MicroPeak data header found");
352 ground_pressure = get_32(f);
353 min_pressure = get_32(f);
354 int nsamples = get_16(f);
356 log_id = nsamples >> 12;
358 pressures = new int[nsamples + 1];
360 ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
361 int cur = ground_pressure;
363 for (int i = 0; i < nsamples; i++) {
365 int same = mix_in(cur, k);
366 int up = mix_in(cur + 0x10000, k);
367 int down = mix_in(cur - 0x10000, k);
369 if (closer (cur, same, up)) {
370 if (closer (cur, same, down))
375 if (closer (cur, up, down))
381 pressures[i+1] = cur;
384 int current_crc = swap16(~file_crc & 0xffff);
387 crc_valid = crc == current_crc;
390 case LOG_ID_MICROPEAK:
391 time_step = 2 * CLOCK;
393 case LOG_ID_MICROKITE:
394 time_step = 200 * CLOCK;
397 stats = new MicroStats(this);
398 } catch (FileEndedException fe) {
399 throw new IOException("File Ended Unexpectedly");
404 ground_pressure = 101000;
405 min_pressure = 101000;
406 pressures = new int[1];
407 pressures[0] = 101000;