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_5.*;
24 import org.altusmetrum.altosuilib_3.*;
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;
104 public static final int LOG_ID_MICROPEAK = 0;
105 public static final int LOG_ID_MICROKITE = 1;
107 public static final double CLOCK = 0.096;
109 public class FileEndedException extends Exception {
112 public class NonHexcharException extends Exception {
115 public class InvalidCrcException extends Exception {
118 private int getc(InputStream f) throws IOException, FileEndedException {
122 throw new FileEndedException();
127 private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
132 if (!Character.isWhitespace(c))
137 private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
138 int c = get_nonwhite(f);
140 if ('0' <= c && c <= '9')
142 if ('a' <= c && c <= 'f')
144 if ('A' <= c && c <= 'F')
146 throw new NonHexcharException();
149 private static final int POLY = 0x8408;
151 private int log_crc(int crc, int b) {
154 for (i = 0; i < 8; i++) {
155 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
156 crc = (crc >> 1) ^ POLY;
166 private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
170 int h = (a << 4) + b;
172 file_crc = log_crc(file_crc, h);
176 private boolean find_header(InputStream f) throws IOException, FileEndedException {
178 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
183 private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException {
185 for (int i = 0; i < 4; i++) {
186 v += get_hex(f) << (i * 8);
191 private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
193 for (int i = 0; i < 2; i++) {
194 v += get_hex(f) << (i * 8);
199 private int swap16(int i) {
200 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
203 public boolean crc_valid;
205 int mix_in (int high, int low) {
206 return high - (high & 0xffff) + low;
209 boolean closer (int target, int a, int b) {
210 return Math.abs (target - a) < Math.abs(target - b);
213 public double altitude(int i) {
214 return AltosConvert.pressure_to_altitude(pressures[i]);
217 public String name() {
221 public Iterable<AltosUIDataPoint> dataPoints() {
222 return new MicroUIIterable(this);
225 public Iterable<MicroDataPoint> points() {
226 return new MicroIterable(this);
232 return n * fact(n-1);
235 int choose(int n, int k) {
236 return fact(n) / (fact(k) * fact(n-k));
240 public double avg_altitude(int center, int dist) {
241 int start = center - dist;
242 int stop = center + dist;
246 if (stop >= pressures.length)
247 stop = pressures.length - 1;
254 for (int i = start; i <= stop; i++) {
255 int k = i - (center - dist);
256 int c = choose (n, k);
258 sum += c * pressures[i];
262 double pres = sum / div;
264 double alt = AltosConvert.pressure_to_altitude(pres);
268 public double pressure(int i) {
272 public double height(int i) {
273 return altitude(i) - ground_altitude;
276 public double apogee_pressure() {
280 public double apogee_altitude() {
281 return AltosConvert.pressure_to_altitude(apogee_pressure());
284 public double apogee_height() {
285 return apogee_altitude() - ground_altitude;
288 static final int speed_avg = 3;
289 static final int accel_avg = 5;
291 private double avg_speed(int center, int dist) {
295 double ai = avg_altitude(center, dist);
296 double aj = avg_altitude(center - 1, dist);
297 double s = (ai - aj) / time_step;
302 public double speed(int i) {
303 return avg_speed(i, speed_avg);
306 public double acceleration(int i) {
309 return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step;
312 public double time(int i) {
313 return i * time_step;
316 public void save (OutputStream f) throws IOException {
322 public void export (Writer f) throws IOException {
323 PrintWriter pw = new PrintWriter(f);
324 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");
325 for (MicroDataPoint point : points()) {
326 pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
330 AltosConvert.meters_to_feet(point.height),
332 AltosConvert.meters_to_mph(point.speed),
333 AltosConvert.meters_to_mach(point.speed),
335 AltosConvert.meters_to_feet(point.accel),
336 AltosConvert.meters_to_g(point.accel));
340 public void set_name(String name) {
344 public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
346 bytes = new ArrayList<Integer>();
348 throw new IOException("No MicroPeak data header found");
351 ground_pressure = get_32(f);
352 min_pressure = get_32(f);
353 int nsamples = get_16(f);
355 log_id = nsamples >> 12;
357 pressures = new int[nsamples + 1];
359 ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
360 int cur = ground_pressure;
362 for (int i = 0; i < nsamples; i++) {
364 int same = mix_in(cur, k);
365 int up = mix_in(cur + 0x10000, k);
366 int down = mix_in(cur - 0x10000, k);
368 if (closer (cur, same, up)) {
369 if (closer (cur, same, down))
374 if (closer (cur, up, down))
380 pressures[i+1] = cur;
383 int current_crc = swap16(~file_crc & 0xffff);
386 crc_valid = crc == current_crc;
389 case LOG_ID_MICROPEAK:
390 time_step = 2 * CLOCK;
392 case LOG_ID_MICROKITE:
393 time_step = 200 * CLOCK;
396 stats = new MicroStats(this);
397 } catch (FileEndedException fe) {
398 throw new IOException("File Ended Unexpectedly");
403 ground_pressure = 101000;
404 min_pressure = 101000;
405 pressures = new int[1];
406 pressures[0] = 101000;