]> git.tdb.fi Git - ext/subsurface.git/blob - statistics.c
Exclude obviously bogus SAC values from statistics calculations
[ext/subsurface.git] / statistics.c
1 /* statistics.c */
2 /* creates the UI for the Info & Stats page -
3  * controlled through the following interfaces:
4  *
5  * void show_dive_stats(struct dive *dive)
6  * void flush_dive_stats_changes(struct dive *dive)
7  *
8  * called from gtk-ui:
9  * GtkWidget *stats_widget(void)
10  */
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <time.h>
16
17 #include "dive.h"
18 #include "display.h"
19 #include "display-gtk.h"
20 #include "divelist.h"
21
22 typedef struct {
23         GtkWidget *date,
24                 *dive_time,
25                 *surf_intv,
26                 *max_depth,
27                 *avg_depth,
28                 *water_temp,
29                 *sac,
30                 *otu,
31                 *o2he,
32                 *gas_used,
33                 *total_time,
34                 *avg_time,
35                 *max_overall_depth,
36                 *avg_overall_depth,
37                 *min_sac,
38                 *avg_sac,
39                 *max_sac;
40 } info_stat_widget_t;
41
42 static info_stat_widget_t info_stat_w;
43
44 typedef struct {
45         duration_t total_time;
46         /* avg_time is simply total_time / nr -- let's not keep this */
47         depth_t max_depth;
48         depth_t avg_depth;
49         volume_t max_sac;
50         volume_t min_sac;
51         volume_t avg_sac;
52 } info_stat_t;
53
54 static info_stat_t info_stat;
55
56 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
57 {
58         int idx;
59         struct dive *dp;
60         int old_tt, sac_time = 0;
61
62         *prev_dive = NULL;
63         memset(&info_stat, 0, sizeof(info_stat));
64         /* this relies on the fact that the dives in the dive_table
65          * are in chronological order */
66         for (idx = 0; idx < dive_table.nr; idx++) {
67                 dp = dive_table.dives[idx];
68                 if (dp->when == dive->when) {
69                         /* that's the one we are showing */
70                         if (idx > 0)
71                                 *prev_dive = dive_table.dives[idx-1];
72                 }
73                 old_tt = info_stat.total_time.seconds;
74                 info_stat.total_time.seconds += dp->duration.seconds;
75                 if (dp->maxdepth.mm > info_stat.max_depth.mm)
76                         info_stat.max_depth.mm = dp->maxdepth.mm;
77                 info_stat.avg_depth.mm = (1.0 * old_tt * info_stat.avg_depth.mm +
78                                 dp->duration.seconds * dp->meandepth.mm) / info_stat.total_time.seconds;
79                 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
80                         int old_sac_time = sac_time;
81                         sac_time += dp->duration.seconds;
82                         info_stat.avg_sac.mliter = (1.0 * old_sac_time * info_stat.avg_sac.mliter +
83                                                 dp->duration.seconds * dp->sac) / sac_time ;
84                         if (dp->sac > info_stat.max_sac.mliter)
85                                 info_stat.max_sac.mliter = dp->sac;
86                         if (info_stat.min_sac.mliter == 0 || dp->sac < info_stat.min_sac.mliter)
87                                 info_stat.min_sac.mliter = dp->sac;
88                 }
89         }
90 }
91
92 static void set_label(GtkWidget *w, const char *fmt, ...)
93 {
94         char buf[80];
95         va_list args;
96
97         va_start(args, fmt);
98         vsnprintf(buf, sizeof(buf), fmt, args);
99         va_end(args);
100         gtk_label_set_text(GTK_LABEL(w), buf);
101 }
102
103 static char * get_time_string(int seconds, int maxdays)
104 {
105         static char buf[80];
106         if (maxdays && seconds > 3600 * 24 * maxdays)
107                 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
108         else {
109                 int days = seconds / 3600 / 24;
110                 int hours = (seconds - days * 3600 * 24) / 3600;
111                 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
112                 if (days > 0)
113                         snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
114                 else
115                         snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
116         }
117         return buf;
118 }
119
120 void show_dive_stats(struct dive *dive)
121 {
122         char buf[80];
123         double value;
124         int decimals;
125         const char *unit;
126         int idx, offset, gas_used;
127         struct dive *prev_dive;
128
129         process_all_dives(dive, &prev_dive);
130
131         strftime(buf, 80, "%a, %b %d, %Y, %k:%M", gmtime(&dive->when));
132         set_label(info_stat_w.date, buf);
133         set_label(info_stat_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
134         if (prev_dive)
135                 set_label(info_stat_w.surf_intv, 
136                         get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
137         else
138                 set_label(info_stat_w.surf_intv, "unknown");
139         value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
140         set_label(info_stat_w.max_depth, "%.*f %s", decimals, value, unit);
141         value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
142         set_label(info_stat_w.avg_depth, "%.*f %s", decimals, value, unit);
143         value = get_temp_units(dive->watertemp.mkelvin, &unit);
144         set_label(info_stat_w.water_temp, "%.1f %s", value, unit);
145         value = get_volume_units(dive->sac, &decimals, &unit);
146         if (value > 0) {
147                 set_label(info_stat_w.sac, "%.*f %s/min", decimals, value, unit);
148         } else
149                 set_label(info_stat_w.sac, "");
150         set_label(info_stat_w.otu, "%d", dive->otu);
151         offset = 0;
152         gas_used = 0;
153         buf[0] = '\0';
154         /* for the O2/He readings just create a list of them */
155         for (idx = 0; idx < MAX_CYLINDERS; idx++) {
156                 cylinder_t *cyl = &dive->cylinder[idx];
157                 /* we assume that every valid cylinder has either a working pressure
158                  * or a size; but for good measure let's also accept cylinders with
159                  * a starting or ending pressure*/
160                 if (cyl->type.workingpressure.mbar || cyl->type.size.mliter ||
161                         cyl->start.mbar || cyl->end.mbar) {
162                         /* 0% O2 strangely means air, so 21% - I don't like that at all */
163                         int o2 = cyl->gasmix.o2.permille ? : 209;
164                         if (offset > 0) {
165                                 snprintf(buf+offset, 80-offset, ", ");
166                                 offset += 2;
167                         }
168                         snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
169                                 (cyl->gasmix.he.permille + 5) / 10);
170                         offset = strlen(buf);
171                 }
172                 /* and if we have size, start and end pressure, we can
173                  * calculate the total gas used */
174                 if (cyl->type.size.mliter && cyl->start.mbar && cyl->end.mbar)
175                         gas_used += cyl->type.size.mliter / 1000.0 *
176                                 (cyl->start.mbar - cyl->end.mbar);
177         }
178         if (offset)
179                 set_label(info_stat_w.o2he, buf);
180         if (gas_used) {
181                 value = get_volume_units(gas_used, &decimals, &unit);
182                 set_label(info_stat_w.gas_used, "%.*f %s", decimals, value, unit);
183         } else
184                 set_label(info_stat_w.gas_used, "");
185         /* and now do the statistics */
186         set_label(info_stat_w.total_time, get_time_string(info_stat.total_time.seconds, 0));
187         set_label(info_stat_w.avg_time, get_time_string(info_stat.total_time.seconds / dive_table.nr, 0));
188         value = get_depth_units(info_stat.max_depth.mm, &decimals, &unit);
189         set_label(info_stat_w.max_overall_depth, "%.*f %s", decimals, value, unit);
190         value = get_depth_units(info_stat.avg_depth.mm, &decimals, &unit);
191         set_label(info_stat_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
192         value = get_volume_units(info_stat.max_sac.mliter, &decimals, &unit);
193         set_label(info_stat_w.max_sac, "%.*f %s/min", decimals, value, unit);
194         value = get_volume_units(info_stat.min_sac.mliter, &decimals, &unit);
195         set_label(info_stat_w.min_sac, "%.*f %s/min", decimals, value, unit);
196         value = get_volume_units(info_stat.avg_sac.mliter, &decimals, &unit);
197         set_label(info_stat_w.avg_sac, "%.*f %s/min", decimals, value, unit);
198 }
199
200 void flush_dive_stats_changes(struct dive *dive)
201 {
202         /* We do nothing: we require the "Ok" button press */
203 }
204
205 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
206 {
207         GtkWidget *label_widget;
208         GtkWidget *frame;
209
210         frame = gtk_frame_new(label);
211         label_widget = gtk_label_new(NULL);
212         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
213         gtk_container_add(GTK_CONTAINER(frame), label_widget);
214
215         return label_widget;
216 }
217
218 GtkWidget *stats_widget(void)
219 {
220
221         GtkWidget *vbox, *hbox, *infoframe, *statsframe, *framebox;
222
223         vbox = gtk_vbox_new(FALSE, 3);
224
225         infoframe = gtk_frame_new("Dive Info");
226         gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
227         framebox = gtk_vbox_new(FALSE, 3);
228         gtk_container_add(GTK_CONTAINER(infoframe), framebox);
229
230         /* first row */
231         hbox = gtk_hbox_new(FALSE, 3);
232         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
233
234         info_stat_w.date = new_info_label_in_frame(hbox, "Date");
235         info_stat_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
236         info_stat_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
237
238         /* second row */
239         hbox = gtk_hbox_new(FALSE, 3);
240         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
241
242         info_stat_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
243         info_stat_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
244         info_stat_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
245
246         /* third row */
247         hbox = gtk_hbox_new(FALSE, 3);
248         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
249
250         info_stat_w.sac = new_info_label_in_frame(hbox, "SAC");
251         info_stat_w.otu = new_info_label_in_frame(hbox, "OTU");
252         info_stat_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
253         info_stat_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");
254
255         statsframe = gtk_frame_new("Statistics");
256         gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
257         framebox = gtk_vbox_new(FALSE, 3);
258         gtk_container_add(GTK_CONTAINER(statsframe), framebox);
259
260         /* first row */
261         hbox = gtk_hbox_new(FALSE, 3);
262         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
263
264         info_stat_w.total_time = new_info_label_in_frame(hbox, "Total Time");
265         info_stat_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
266         info_stat_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
267         info_stat_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
268
269         /* second row */
270         hbox = gtk_hbox_new(FALSE, 3);
271         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
272
273         info_stat_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
274         info_stat_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
275         info_stat_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
276
277         return vbox;
278 }