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_4.*;
24 import org.altusmetrum.altosuilib_2.*;
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 public class FileEndedException extends Exception {
106 public class NonHexcharException extends Exception {
109 public 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, FileEndedException {
172 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
177 private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException {
179 for (int i = 0; i < 4; i++) {
180 v += get_hex(f) << (i * 8);
185 private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
187 for (int i = 0; i < 2; i++) {
188 v += get_hex(f) << (i * 8);
193 private int swap16(int i) {
194 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
197 public boolean crc_valid;
199 int mix_in (int high, int low) {
200 return high - (high & 0xffff) + low;
203 boolean closer (int target, int a, int b) {
204 return Math.abs (target - a) < Math.abs(target - b);
207 public double altitude(int i) {
208 return AltosConvert.pressure_to_altitude(pressures[i]);
211 public String name() {
215 public Iterable<AltosUIDataPoint> dataPoints() {
216 return new MicroUIIterable(this);
219 public Iterable<MicroDataPoint> points() {
220 return new MicroIterable(this);
226 return n * fact(n-1);
229 int choose(int n, int k) {
230 return fact(n) / (fact(k) * fact(n-k));
234 public double avg_altitude(int center, int dist) {
235 int start = center - dist;
236 int stop = center + dist;
240 if (stop >= pressures.length)
241 stop = pressures.length - 1;
248 for (int i = start; i <= stop; i++) {
249 int k = i - (center - dist);
250 int c = choose (n, k);
252 sum += c * pressures[i];
256 double pres = sum / div;
258 double alt = AltosConvert.pressure_to_altitude(pres);
262 public double pressure(int i) {
266 public double height(int i) {
267 return altitude(i) - ground_altitude;
270 public double apogee_pressure() {
274 public double apogee_altitude() {
275 return AltosConvert.pressure_to_altitude(apogee_pressure());
278 public double apogee_height() {
279 return apogee_altitude() - ground_altitude;
282 static final int speed_avg = 3;
283 static final int accel_avg = 5;
285 private double avg_speed(int center, int dist) {
289 double ai = avg_altitude(center, dist);
290 double aj = avg_altitude(center - 1, dist);
291 double s = (ai - aj) / time_step;
296 public double speed(int i) {
297 return avg_speed(i, speed_avg);
300 public double acceleration(int i) {
303 return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step;
306 public double time(int i) {
307 return i * time_step;
310 public void save (OutputStream f) throws IOException {
316 public void export (Writer f) throws IOException {
317 PrintWriter pw = new PrintWriter(f);
318 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");
319 for (MicroDataPoint point : points()) {
320 pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
324 AltosConvert.meters_to_feet(point.height),
326 AltosConvert.meters_to_mph(point.speed),
327 AltosConvert.meters_to_mach(point.speed),
329 AltosConvert.meters_to_feet(point.accel),
330 AltosConvert.meters_to_g(point.accel));
334 public void set_name(String name) {
338 public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
340 bytes = new ArrayList<Integer>();
342 throw new IOException("No MicroPeak data header found");
345 ground_pressure = get_32(f);
346 min_pressure = get_32(f);
347 int nsamples = get_16(f);
348 pressures = new int[nsamples + 1];
350 ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
351 int cur = ground_pressure;
353 for (int i = 0; i < nsamples; i++) {
355 int same = mix_in(cur, k);
356 int up = mix_in(cur + 0x10000, k);
357 int down = mix_in(cur - 0x10000, k);
359 if (closer (cur, same, up)) {
360 if (closer (cur, same, down))
365 if (closer (cur, up, down))
371 pressures[i+1] = cur;
374 int current_crc = swap16(~file_crc & 0xffff);
377 crc_valid = crc == current_crc;
380 stats = new MicroStats(this);
381 } catch (FileEndedException fe) {
382 throw new IOException("File Ended Unexpectedly");
387 ground_pressure = 101000;
388 min_pressure = 101000;
389 pressures = new int[1];
390 pressures[0] = 101000;