]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Merge branch 'add-info-stats-page' of git://github.com/dirkhh/subsurface
[ext/subsurface.git] / dive.c
1 /* dive.c */
2 /* maintains the internal dive list structure */
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "dive.h"
7
8 void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name)
9 {
10         struct event *ev, **p;
11         unsigned int size, len = strlen(name);
12
13         size = sizeof(*ev) + len + 1;
14         ev = malloc(size);
15         if (!ev)
16                 return;
17         memset(ev, 0, size);
18         memcpy(ev->name, name, len);
19         ev->time.seconds = time;
20         ev->type = type;
21         ev->flags = flags;
22         ev->value = value;
23         ev->next = NULL;
24
25         p = &dive->events;
26         while (*p)
27                 p = &(*p)->next;
28         *p = ev;
29         remember_event(name);
30 }
31
32 int get_pressure_units(unsigned int mb, const char **units)
33 {
34         int pressure;
35         const char* unit;
36
37         switch (output_units.pressure) {
38         case PASCAL:
39                 pressure = mb * 100;
40                 unit = "pascal";
41                 break;
42         case BAR:
43                 pressure = (mb + 500) / 1000;
44                 unit = "bar";
45                 break;
46         case PSI:
47                 pressure = mbar_to_PSI(mb);
48                 unit = "psi";
49                 break;
50         }
51         if (units)
52                 *units = unit;
53         return pressure;
54 }
55
56 double get_temp_units(unsigned int mk, const char **units)
57 {
58         double deg;
59         const char *unit;
60
61         if (output_units.temperature == FAHRENHEIT) {
62                 deg = mkelvin_to_F(mk);
63                 unit = UTF8_DEGREE "F";
64         } else {
65                 deg = mkelvin_to_C(mk);
66                 unit = UTF8_DEGREE "C";
67         }
68         if (units)
69                 *units = unit;
70         return deg;
71 }
72
73 double get_volume_units(unsigned int ml, int *frac, const char **units)
74 {
75         int decimals;
76         double vol;
77         const char *unit;
78
79         switch (output_units.volume) {
80         case LITER:
81                 vol = ml / 1000.0;
82                 unit = "l";
83                 decimals = 1;
84                 break;
85         case CUFT:
86                 vol = ml_to_cuft(ml);
87                 unit = "cuft";
88                 decimals = 2;
89                 break;
90         }
91         if (frac)
92                 *frac = decimals;
93         if (units)
94                 *units = unit;
95         return vol;
96 }
97
98 double get_depth_units(unsigned int mm, int *frac, const char **units)
99 {
100         int decimals;
101         double d;
102         const char *unit;
103
104         switch (output_units.length) {
105         case METERS:
106                 d = mm / 1000.0;
107                 unit = "m";
108                 decimals = d < 20;
109                 break;
110         case FEET:
111                 d = mm_to_feet(mm);
112                 unit = "ft";
113                 decimals = 0;
114                 break;
115         }
116         if (frac)
117                 *frac = decimals;
118         if (units)
119                 *units = unit;
120         return d;
121 }
122
123 struct dive *alloc_dive(void)
124 {
125         const int initial_samples = 5;
126         unsigned int size;
127         struct dive *dive;
128
129         size = dive_size(initial_samples);
130         dive = malloc(size);
131         if (!dive)
132                 exit(1);
133         memset(dive, 0, size);
134         dive->alloc_samples = initial_samples;
135         return dive;
136 }
137
138 struct sample *prepare_sample(struct dive **divep)
139 {
140         struct dive *dive = *divep;
141         if (dive) {
142                 int nr = dive->samples;
143                 int alloc_samples = dive->alloc_samples;
144                 struct sample *sample;
145                 if (nr >= alloc_samples) {
146                         unsigned int size;
147
148                         alloc_samples = (alloc_samples * 3)/2 + 10;
149                         size = dive_size(alloc_samples);
150                         dive = realloc(dive, size);
151                         if (!dive)
152                                 return NULL;
153                         dive->alloc_samples = alloc_samples;
154                         *divep = dive;
155                 }
156                 sample = dive->sample + nr;
157                 memset(sample, 0, sizeof(*sample));
158                 return sample;
159         }
160         return NULL;
161 }
162
163 void finish_sample(struct dive *dive, struct sample *sample)
164 {
165         dive->samples++;
166 }
167
168 /*
169  * So when we re-calculate maxdepth and meandepth, we will
170  * not override the old numbers if they are close to the
171  * new ones.
172  *
173  * Why? Because a dive computer may well actually track the
174  * max depth and mean depth at finer granularity than the
175  * samples it stores. So it's possible that the max and mean
176  * have been reported more correctly originally.
177  *
178  * Only if the values calculated from the samples are clearly
179  * different do we override the normal depth values.
180  *
181  * This considers 1m to be "clearly different". That's
182  * a totally random number.
183  */
184 static void update_depth(depth_t *depth, int new)
185 {
186         if (new) {
187                 int old = depth->mm;
188
189                 if (abs(old - new) > 1000)
190                         depth->mm = new;
191         }
192 }
193
194 static void update_duration(duration_t *duration, int new)
195 {
196         if (new)
197                 duration->seconds = new;
198 }
199
200 static void update_temperature(temperature_t *temperature, int new)
201 {
202         if (new) {
203                 int old = temperature->mkelvin;
204
205                 if (abs(old - new) > 1000)
206                         temperature->mkelvin = new;
207         }
208 }
209
210 /*
211  * If you have more than 32 cylinders, you'd better have a 64-bit build.
212  * And if you have more than 64 cylinders, you need to use another tool,
213  * or fix this up to do something odd.
214  */
215 static unsigned long fixup_pressure(struct dive *dive, struct sample *sample, unsigned long flags)
216 {
217         unsigned long mask;
218         unsigned int pressure, index;
219         cylinder_t *cyl;
220
221         pressure = sample->cylinderpressure.mbar;
222         if (!pressure)
223                 return flags;
224         index = sample->cylinderindex;
225         if (index >= MAX_CYLINDERS)
226                 return flags;
227         cyl = dive->cylinder + index;
228         if (!cyl->start.mbar)
229                 cyl->start.mbar = pressure;
230         /*
231          * If we already have an end pressure, without
232          * ever having seen a sample for this cylinder,
233          * that means that somebody set the end pressure
234          * by hand
235          */
236         mask = 1ul << index;
237         if (cyl->end.mbar) {
238                 if (!(flags & mask))
239                         return flags;
240         }
241         flags |= mask;
242
243         /* we need to handle the user entering beginning and end tank pressures
244          * - maybe even IF we have samples. But for now if we have air pressure
245          * data in the samples, we use that instead of the minimum
246          * if (!cyl->end.mbar || pressure < cyl->end.mbar)
247          */
248         cyl->end.mbar = pressure;
249         return flags;
250 }
251
252 struct dive *fixup_dive(struct dive *dive)
253 {
254         int i;
255         double depthtime = 0;
256         int lasttime = 0;
257         int start = -1, end = -1;
258         int maxdepth = 0, mintemp = 0;
259         int lastdepth = 0;
260         int lasttemp = 0;
261         unsigned long flags = 0;
262
263         for (i = 0; i < dive->samples; i++) {
264                 struct sample *sample = dive->sample + i;
265                 int time = sample->time.seconds;
266                 int depth = sample->depth.mm;
267                 int temp = sample->temperature.mkelvin;
268
269                 if (lastdepth)
270                         end = time;
271
272                 if (depth) {
273                         if (start < 0)
274                                 start = lasttime;
275                         if (depth > maxdepth)
276                                 maxdepth = depth;
277                 }
278
279                 flags = fixup_pressure(dive, sample, flags);
280
281                 if (temp) {
282                         /*
283                          * If we have consecutive identical
284                          * temperature readings, throw away
285                          * the redundant ones.
286                          */
287                         if (lasttemp == temp)
288                                 sample->temperature.mkelvin = 0;
289                         else
290                                 lasttemp = temp;
291
292                         if (!mintemp || temp < mintemp)
293                                 mintemp = temp;
294                 }
295                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
296                 lastdepth = depth;
297                 lasttime = time;
298         }
299         if (end < 0)
300                 return dive;
301
302         update_duration(&dive->duration, end - start);
303         if (start != end)
304                 depthtime /= (end - start);
305
306         update_depth(&dive->meandepth, depthtime);
307         update_temperature(&dive->watertemp, mintemp);
308         update_depth(&dive->maxdepth, maxdepth);
309
310         add_people(dive->buddy);
311         add_people(dive->divemaster);
312         add_location(dive->location);
313         for (i = 0; i < MAX_CYLINDERS; i++) {
314                 cylinder_type_t *type = &dive->cylinder[i].type;
315                 add_cylinder_description(type);
316         }
317
318         return dive;
319 }
320
321 /* Don't pick a zero for MERGE_MIN() */
322 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
323 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
324 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
325 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
326
327 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
328 {
329         struct sample *p = prepare_sample(&dive);
330
331         if (!p)
332                 return NULL;
333         *p = *sample;
334         p->time.seconds = time;
335         finish_sample(dive, p);
336         return dive;
337 }
338
339 /*
340  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
341  */
342 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
343 {
344         int asamples = a->samples;
345         int bsamples = b->samples;
346         struct sample *as = a->sample;
347         struct sample *bs = b->sample;
348
349         for (;;) {
350                 int at, bt;
351                 struct sample sample;
352
353                 if (!res)
354                         return NULL;
355
356                 at = asamples ? as->time.seconds : -1;
357                 bt = bsamples ? bs->time.seconds + offset : -1;
358
359                 /* No samples? All done! */
360                 if (at < 0 && bt < 0)
361                         return fixup_dive(res);
362
363                 /* Only samples from a? */
364                 if (bt < 0) {
365 add_sample_a:
366                         res = add_sample(as, at, res);
367                         as++;
368                         asamples--;
369                         continue;
370                 }
371
372                 /* Only samples from b? */
373                 if (at < 0) {
374 add_sample_b:
375                         res = add_sample(bs, bt, res);
376                         bs++;
377                         bsamples--;
378                         continue;
379                 }
380
381                 if (at < bt)
382                         goto add_sample_a;
383                 if (at > bt)
384                         goto add_sample_b;
385
386                 /* same-time sample: add a merged sample. Take the non-zero ones */
387                 sample = *bs;
388                 if (as->depth.mm)
389                         sample.depth = as->depth;
390                 if (as->temperature.mkelvin)
391                         sample.temperature = as->temperature;
392                 if (as->cylinderpressure.mbar)
393                         sample.cylinderpressure = as->cylinderpressure;
394                 if (as->cylinderindex)
395                         sample.cylinderindex = as->cylinderindex;
396
397                 res = add_sample(&sample, at, res);
398
399                 as++;
400                 bs++;
401                 asamples--;
402                 bsamples--;
403         }
404 }
405
406 static char *merge_text(const char *a, const char *b)
407 {
408         char *res;
409
410         if (!a || !*a)
411                 return (char *)b;
412         if (!b || !*b)
413                 return (char *)a;
414         if (!strcmp(a,b))
415                 return (char *)a;
416         res = malloc(strlen(a) + strlen(b) + 9);
417         if (!res)
418                 return (char *)a;
419         sprintf(res, "(%s) or (%s)", a, b);
420         return res;
421 }
422
423 #define SORT(a,b,field) \
424         if (a->field != b->field) return a->field < b->field ? -1 : 1
425
426 static int sort_event(struct event *a, struct event *b)
427 {
428         SORT(a,b,time.seconds);
429         SORT(a,b,type);
430         SORT(a,b,flags);
431         SORT(a,b,value);
432         return strcmp(a->name, b->name);
433 }
434
435 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
436 {
437         struct event *a, *b;
438         struct event **p = &res->events;
439
440         a = src1->events;
441         b = src2->events;
442         while (b) {
443                 b->time.seconds += offset;
444                 b = b->next;
445         }
446         b = src2->events;
447
448         while (a || b) {
449                 int s;
450                 if (!b) {
451                         *p = a;
452                         break;
453                 }
454                 if (!a) {
455                         *p = b;
456                         break;
457                 }
458                 s = sort_event(a, b);
459                 /* Pick b */
460                 if (s > 0) {
461                         *p = b;
462                         p = &b->next;
463                         b = b->next;
464                         continue;
465                 }
466                 /* Pick 'a' or neither */
467                 if (s < 0) {
468                         *p = a;
469                         p = &a->next;
470                 }
471                 a = a->next;
472                 continue;
473         }
474 }
475
476 /* Pick whichever has any info (if either). Prefer 'a' */
477 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
478 {
479         if (a->size.mliter)
480                 b = a;
481         *res = *b;
482 }
483
484 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
485 {
486         if (a->o2.permille)
487                 b = a;
488         *res = *b;
489 }
490
491 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
492 {
493         merge_cylinder_type(&res->type, &a->type, &b->type);
494         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
495         MERGE_MAX(res, a, b, start.mbar);
496         MERGE_MIN(res, a, b, end.mbar);
497 }
498
499 /*
500  * This could do a lot more merging. Right now it really only
501  * merges almost exact duplicates - something that happens easily
502  * with overlapping dive downloads.
503  */
504 struct dive *try_to_merge(struct dive *a, struct dive *b)
505 {
506         int i;
507         struct dive *res;
508
509         if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
510                 return NULL;
511
512         res = alloc_dive();
513
514         res->when = a->when;
515         MERGE_NONZERO(res, a, b, latitude);
516         MERGE_NONZERO(res, a, b, longitude);
517         MERGE_TXT(res, a, b, location);
518         MERGE_TXT(res, a, b, notes);
519         MERGE_TXT(res, a, b, buddy);
520         MERGE_TXT(res, a, b, divemaster);
521         MERGE_MAX(res, a, b, number);
522         MERGE_MAX(res, a, b, maxdepth.mm);
523         res->meandepth.mm = 0;
524         MERGE_MAX(res, a, b, duration.seconds);
525         MERGE_MAX(res, a, b, surfacetime.seconds);
526         MERGE_MAX(res, a, b, airtemp.mkelvin);
527         MERGE_MIN(res, a, b, watertemp.mkelvin);
528         for (i = 0; i < MAX_CYLINDERS; i++)
529                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
530         merge_events(res, a, b, 0);
531         return merge_samples(res, a, b, 0);
532 }