]> git.tdb.fi Git - ext/subsurface.git/blob - statistics.c
Separate out single dive statistics and total statistics
[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 } single_stat_widget_t;
34
35 static single_stat_widget_t single_w;
36
37 typedef struct {
38         GtkWidget *total_time,
39                 *avg_time,
40                 *max_overall_depth,
41                 *avg_overall_depth,
42                 *min_sac,
43                 *avg_sac,
44                 *max_sac;
45 } total_stats_widget_t;
46
47 static total_stats_widget_t stats_w;
48
49 typedef struct {
50         duration_t total_time;
51         /* avg_time is simply total_time / nr -- let's not keep this */
52         depth_t max_depth;
53         depth_t avg_depth;
54         volume_t max_sac;
55         volume_t min_sac;
56         volume_t avg_sac;
57 } stats_t;
58
59 static stats_t stats;
60
61 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
62 {
63         int idx;
64         struct dive *dp;
65         int old_tt, sac_time = 0;
66
67         *prev_dive = NULL;
68         memset(&stats, 0, sizeof(stats));
69         /* this relies on the fact that the dives in the dive_table
70          * are in chronological order */
71         for (idx = 0; idx < dive_table.nr; idx++) {
72                 dp = dive_table.dives[idx];
73                 if (dp->when == dive->when) {
74                         /* that's the one we are showing */
75                         if (idx > 0)
76                                 *prev_dive = dive_table.dives[idx-1];
77                 }
78                 old_tt = stats.total_time.seconds;
79                 stats.total_time.seconds += dp->duration.seconds;
80                 if (dp->maxdepth.mm > stats.max_depth.mm)
81                         stats.max_depth.mm = dp->maxdepth.mm;
82                 stats.avg_depth.mm = (1.0 * old_tt * stats.avg_depth.mm +
83                                 dp->duration.seconds * dp->meandepth.mm) / stats.total_time.seconds;
84                 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
85                         int old_sac_time = sac_time;
86                         sac_time += dp->duration.seconds;
87                         stats.avg_sac.mliter = (1.0 * old_sac_time * stats.avg_sac.mliter +
88                                                 dp->duration.seconds * dp->sac) / sac_time ;
89                         if (dp->sac > stats.max_sac.mliter)
90                                 stats.max_sac.mliter = dp->sac;
91                         if (stats.min_sac.mliter == 0 || dp->sac < stats.min_sac.mliter)
92                                 stats.min_sac.mliter = dp->sac;
93                 }
94         }
95 }
96
97 static void set_label(GtkWidget *w, const char *fmt, ...)
98 {
99         char buf[80];
100         va_list args;
101
102         va_start(args, fmt);
103         vsnprintf(buf, sizeof(buf), fmt, args);
104         va_end(args);
105         gtk_label_set_text(GTK_LABEL(w), buf);
106 }
107
108 static char * get_time_string(int seconds, int maxdays)
109 {
110         static char buf[80];
111         if (maxdays && seconds > 3600 * 24 * maxdays)
112                 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
113         else {
114                 int days = seconds / 3600 / 24;
115                 int hours = (seconds - days * 3600 * 24) / 3600;
116                 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
117                 if (days > 0)
118                         snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
119                 else
120                         snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
121         }
122         return buf;
123 }
124
125 static void show_single_dive_stats(struct dive *dive)
126 {
127         char buf[80];
128         double value;
129         int decimals;
130         const char *unit;
131         int idx, offset, gas_used;
132         struct dive *prev_dive;
133         struct tm *tm;
134
135         process_all_dives(dive, &prev_dive);
136
137         tm = gmtime(&dive->when);
138         snprintf(buf, sizeof(buf),
139                 "%s, %s %d, %d %2d:%02d",
140                 weekday(tm->tm_wday),
141                 monthname(tm->tm_mon),
142                 tm->tm_mday, tm->tm_year + 1900,
143                 tm->tm_hour, tm->tm_min);
144
145         set_label(single_w.date, buf);
146         set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
147         if (prev_dive)
148                 set_label(single_w.surf_intv, 
149                         get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
150         else
151                 set_label(single_w.surf_intv, "unknown");
152         value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
153         set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
154         value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
155         set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
156         if (dive->watertemp.mkelvin) {
157                 value = get_temp_units(dive->watertemp.mkelvin, &unit);
158                 set_label(single_w.water_temp, "%.1f %s", value, unit);
159         } else
160                 set_label(single_w.water_temp, "");
161         value = get_volume_units(dive->sac, &decimals, &unit);
162         if (value > 0) {
163                 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
164         } else
165                 set_label(single_w.sac, "");
166         set_label(single_w.otu, "%d", dive->otu);
167         offset = 0;
168         gas_used = 0;
169         buf[0] = '\0';
170         /* for the O2/He readings just create a list of them */
171         for (idx = 0; idx < MAX_CYLINDERS; idx++) {
172                 cylinder_t *cyl = &dive->cylinder[idx];
173                 unsigned int start, end;
174
175                 start = cyl->start.mbar ? : cyl->sample_start.mbar;
176                 end = cyl->end.mbar ? : cyl->sample_end.mbar;
177                 if (!cylinder_none(cyl)) {
178                         /* 0% O2 strangely means air, so 21% - I don't like that at all */
179                         int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
180                         if (offset > 0) {
181                                 snprintf(buf+offset, 80-offset, ", ");
182                                 offset += 2;
183                         }
184                         snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
185                                 (cyl->gasmix.he.permille + 5) / 10);
186                         offset = strlen(buf);
187                 }
188                 /* and if we have size, start and end pressure, we can
189                  * calculate the total gas used */
190                 if (cyl->type.size.mliter && start && end)
191                         gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
192         }
193         set_label(single_w.o2he, buf);
194         if (gas_used) {
195                 value = get_volume_units(gas_used, &decimals, &unit);
196                 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
197         } else
198                 set_label(single_w.gas_used, "");
199 }
200
201 static void show_total_dive_stats(struct dive *dive)
202 {
203         double value;
204         int decimals;
205         const char *unit;
206
207         set_label(stats_w.total_time, get_time_string(stats.total_time.seconds, 0));
208         set_label(stats_w.avg_time, get_time_string(stats.total_time.seconds / dive_table.nr, 0));
209         value = get_depth_units(stats.max_depth.mm, &decimals, &unit);
210         set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
211         value = get_depth_units(stats.avg_depth.mm, &decimals, &unit);
212         set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
213         value = get_volume_units(stats.max_sac.mliter, &decimals, &unit);
214         set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
215         value = get_volume_units(stats.min_sac.mliter, &decimals, &unit);
216         set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
217         value = get_volume_units(stats.avg_sac.mliter, &decimals, &unit);
218         set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
219 }
220
221 void show_dive_stats(struct dive *dive)
222 {
223         show_single_dive_stats(dive);
224         show_total_dive_stats(dive);
225 }
226
227 void flush_dive_stats_changes(struct dive *dive)
228 {
229         /* We do nothing: we require the "Ok" button press */
230 }
231
232 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
233 {
234         GtkWidget *label_widget;
235         GtkWidget *frame;
236
237         frame = gtk_frame_new(label);
238         label_widget = gtk_label_new(NULL);
239         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
240         gtk_container_add(GTK_CONTAINER(frame), label_widget);
241
242         return label_widget;
243 }
244
245 static GtkWidget *total_stats_widget(GtkWidget *vbox)
246 {
247
248         GtkWidget *hbox, *statsframe, *framebox;
249
250         statsframe = gtk_frame_new("Statistics");
251         gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
252         framebox = gtk_vbox_new(FALSE, 3);
253         gtk_container_add(GTK_CONTAINER(statsframe), framebox);
254
255         /* first row */
256         hbox = gtk_hbox_new(FALSE, 3);
257         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
258
259         stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
260         stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
261         stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
262         stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
263
264         /* second row */
265         hbox = gtk_hbox_new(FALSE, 3);
266         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
267
268         stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
269         stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
270         stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
271
272         return vbox;
273 }
274
275 static GtkWidget *single_stats_widget(void)
276 {
277
278         GtkWidget *vbox, *hbox, *infoframe, *framebox;
279
280         vbox = gtk_vbox_new(FALSE, 3);
281
282         infoframe = gtk_frame_new("Dive Info");
283         gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
284         framebox = gtk_vbox_new(FALSE, 3);
285         gtk_container_add(GTK_CONTAINER(infoframe), framebox);
286
287         /* first row */
288         hbox = gtk_hbox_new(FALSE, 3);
289         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
290
291         single_w.date = new_info_label_in_frame(hbox, "Date");
292         single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
293         single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
294
295         /* second row */
296         hbox = gtk_hbox_new(FALSE, 3);
297         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
298
299         single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
300         single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
301         single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
302
303         /* third row */
304         hbox = gtk_hbox_new(FALSE, 3);
305         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
306
307         single_w.sac = new_info_label_in_frame(hbox, "SAC");
308         single_w.otu = new_info_label_in_frame(hbox, "OTU");
309         single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
310         single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");
311
312         return vbox;
313 }
314
315 GtkWidget* stats_widget(void)
316 {
317   GtkWidget *vbox = single_stats_widget();
318   return total_stats_widget(vbox);
319 }