Merge branch 'master' into telescience-v0.2
[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                 real    pressure;
35         } record_t;
36
37         public record_t parse_record(file f, real accel_scale) {
38                 string[*] data = parse_data(f);
39                 if (dim(data) == 0)
40                         return (record_t) { .done = true };
41                 int time_off = 4;
42                 int height_off = 11;
43                 int accel_off = 8;
44                 int pres_off = 9;
45                 switch (string_to_integer(data[0])) {
46                 case 2:
47                         time_off = 4;
48                         accel_off = 9;
49                         pres_off = 10;
50                         height_off = 12;
51                         break;
52                 case 5:
53                         time_off = 4;
54                         accel_off = 10;
55                         pres_off = 11;
56                         height_off = 13;
57                         break;
58                 }
59                 return (record_t) {
60                         .done = false,
61                         .time = string_to_real(data[time_off]),
62                         .height = imprecise(string_to_real(data[height_off])),
63                         .acceleration = imprecise(string_to_real(data[accel_off]) * accel_scale),
64                         .pressure = imprecise(string_to_real(data[pres_off]))
65                 };
66         }
67
68         public void dump(file f) {
69                 for (;;) {
70                         record_t        r = parse_record(f, 1);
71                         if (r.done)
72                                 break;
73                         printf ("%f %f %f\n", r.time, r.height, r.acceleration);
74                 }
75         }
76 }