altos: Shrink ao_config_callsign_set
[fw/altos] / src / kalman / load_csv.5c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 namespace load_csv {
19         string[*] parse_data(file f) {
20                 while (!File::end(f)) {
21                         string l = File::fgets(f);
22                         if (l[0] == '#')
23                                 continue;
24                         return String::parse_csv(l);
25                 }
26                 return (string[0]) {};
27         }
28
29         public typedef struct {
30                 bool    done;
31                 real    time;
32                 real    height;
33                 real    acceleration;
34         } record_t;
35
36         public record_t parse_record(file f, real accel_scale) {
37                 string[*] data = parse_data(f);
38                 if (dim(data) == 0)
39                         return (record_t) { .done = true };
40                 int time_off = 4;
41                 int height_off = 11;
42                 int accel_off = 8;
43                 if (string_to_integer(data[0]) == 2) {
44                         time_off = 4;
45                         accel_off = 9;
46                         height_off = 12;
47                 }
48                 return (record_t) {
49                         .done = false,
50                                 .time = string_to_real(data[time_off]),
51                                 .height = imprecise(string_to_real(data[height_off])),
52                                 .acceleration = imprecise(string_to_real(data[accel_off]) * accel_scale) };
53         }
54
55         public void dump(file f) {
56                 for (;;) {
57                         record_t        r = parse_record(f, 1);
58                         if (r.done)
59                                 break;
60                         printf ("%f %f %f\n", r.time, r.height, r.acceleration);
61                 }
62         }
63 }