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.*;
25 abstract class MicroIterator implements Iterator<Double> {
29 public boolean hasNext() {
30 return i < data.pressures.length;
33 public MicroIterator (MicroData data) {
38 public void remove() {
42 class MicroHeightIterator extends MicroIterator {
43 public Double next() {
44 return data.height(i++);
47 public MicroHeightIterator(MicroData data) {
52 class MicroHeightIterable implements Iterable<Double> {
55 public Iterator<Double> iterator() {
56 return new MicroHeightIterator(data);
59 public MicroHeightIterable(MicroData data) {
64 class MicroSpeedIterator extends MicroIterator {
65 public Double next() {
66 return data.speed(i++);
68 public MicroSpeedIterator(MicroData data) {
73 class MicroSpeedIterable implements Iterable<Double> {
76 public Iterator<Double> iterator() {
77 return new MicroSpeedIterator(data);
80 public MicroSpeedIterable(MicroData data) {
85 class MicroAccelIterator extends MicroIterator {
86 public Double next() {
87 return data.acceleration(i++);
89 public MicroAccelIterator(MicroData data) {
94 class MicroAccelIterable implements Iterable<Double> {
97 public Iterator<Double> iterator() {
98 return new MicroAccelIterator(data);
101 public MicroAccelIterable(MicroData data) {
106 public class MicroData {
107 public int ground_pressure;
108 public int min_pressure;
109 public int[] pressures;
110 private double time_step;
111 private double ground_altitude;
112 private ArrayList<Integer> bytes;
115 class FileEndedException extends Exception {
118 class NonHexcharException extends Exception {
121 class InvalidCrcException extends Exception {
124 private int getc(InputStream f) throws IOException, FileEndedException {
128 throw new FileEndedException();
133 private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
138 if (!Character.isWhitespace(c))
143 private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
144 int c = get_nonwhite(f);
146 if ('0' <= c && c <= '9')
148 if ('a' <= c && c <= 'f')
150 if ('A' <= c && c <= 'F')
152 throw new NonHexcharException();
155 private static final int POLY = 0x8408;
157 private int log_crc(int crc, int b) {
160 for (i = 0; i < 8; i++) {
161 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
162 crc = (crc >> 1) ^ POLY;
172 private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
176 int h = (a << 4) + b;
178 file_crc = log_crc(file_crc, h);
182 private boolean find_header(InputStream f) throws IOException {
185 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
188 } catch (FileEndedException fe) {
193 private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException {
195 for (int i = 0; i < 4; i++) {
196 v += get_hex(f) << (i * 8);
201 private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
203 for (int i = 0; i < 2; i++) {
204 v += get_hex(f) << (i * 8);
209 private int swap16(int i) {
210 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
213 public boolean crc_valid;
215 int mix_in (int high, int low) {
216 return high - (high & 0xffff) + low;
219 boolean closer (int target, int a, int b) {
220 return Math.abs (target - a) < Math.abs(target - b);
223 public double altitude(int i) {
224 return AltosConvert.pressure_to_altitude(pressures[i]);
227 public Iterable<Double> heights() {
228 return new MicroHeightIterable(this);
231 public Iterable<Double> speeds() {
232 return new MicroSpeedIterable(this);
235 public Iterable<Double> accels() {
236 return new MicroAccelIterable(this);
242 return n * fact(n-1);
245 int choose(int n, int k) {
246 return fact(n) / (fact(k) * fact(n-k));
250 public double avg_altitude(int center, int dist) {
251 int start = center - dist;
252 int stop = center + dist;
256 if (stop >= pressures.length)
257 stop = pressures.length - 1;
264 for (int i = start; i <= stop; i++) {
265 int k = i - (center - dist);
266 int c = choose (n, k);
268 sum += c * pressures[i];
272 double pres = sum / div;
274 double alt = AltosConvert.pressure_to_altitude(pres);
278 public double height(int i) {
279 return altitude(i) - 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 {
315 public MicroData (InputStream f) throws IOException {
316 bytes = new ArrayList<Integer>();
318 throw new IOException();
321 ground_pressure = get_32(f);
322 min_pressure = get_32(f);
323 int nsamples = get_16(f);
324 pressures = new int[nsamples + 1];
326 ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
327 int cur = ground_pressure;
329 for (int i = 0; i < nsamples; i++) {
331 int same = mix_in(cur, k);
332 int up = mix_in(cur + 0x10000, k);
333 int down = mix_in(cur - 0x10000, k);
335 if (closer (cur, same, up)) {
336 if (closer (cur, same, down))
341 if (closer (cur, up, down))
347 pressures[i+1] = cur;
350 int current_crc = swap16(~file_crc & 0xffff);
353 crc_valid = crc == current_crc;
356 } catch (FileEndedException fe) {
357 throw new IOException();
358 } catch (NonHexcharException ne) {
359 throw new IOException();
364 ground_pressure = 101000;
365 min_pressure = 101000;
366 pressures = new int[1];
367 pressures[0] = 101000;