]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Add typical 0 to 5 star rating for dives
[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 static void fixup_pressure(struct dive *dive, struct sample *sample)
211 {
212         unsigned int pressure, index;
213         cylinder_t *cyl;
214
215         pressure = sample->cylinderpressure.mbar;
216         if (!pressure)
217                 return;
218         index = sample->cylinderindex;
219         if (index >= MAX_CYLINDERS)
220                 return;
221         cyl = dive->cylinder + index;
222         if (!cyl->sample_start.mbar)
223                 cyl->sample_start.mbar = pressure;
224         cyl->sample_end.mbar = pressure;
225 }
226
227 /*
228  * If the cylinder tank pressures are within half a bar
229  * (about 8 PSI) of the sample pressures, we consider it
230  * to be a rounding error, and throw them away as redundant.
231  */
232 static int same_rounded_pressure(pressure_t a, pressure_t b)
233 {
234         return abs(a.mbar - b.mbar) <= 500;
235 }
236
237 struct dive *fixup_dive(struct dive *dive)
238 {
239         int i,j;
240         double depthtime = 0;
241         int lasttime = 0;
242         int lastindex = -1;
243         int start = -1, end = -1;
244         int maxdepth = 0, mintemp = 0;
245         int lastdepth = 0;
246         int lasttemp = 0, lastpressure = 0;
247         int pressure_delta[MAX_CYLINDERS] = {INT_MAX, };
248
249         for (i = 0; i < dive->samples; i++) {
250                 struct sample *sample = dive->sample + i;
251                 int time = sample->time.seconds;
252                 int depth = sample->depth.mm;
253                 int temp = sample->temperature.mkelvin;
254                 int pressure = sample->cylinderpressure.mbar;
255                 int index = sample->cylinderindex;
256
257                 if (index == lastindex) {
258                         /* Remove duplicate redundant pressure information */
259                         if (pressure == lastpressure)
260                                 sample->cylinderpressure.mbar = 0;
261                         /* check for simply linear data in the samples
262                            +INT_MAX means uninitialized, -INT_MAX means not linear */
263                         if (pressure_delta[index] != -INT_MAX && lastpressure) {
264                                 if (pressure_delta[index] == INT_MAX) {
265                                         pressure_delta[index] = abs(pressure - lastpressure);
266                                 } else {
267                                         int cur_delta = abs(pressure - lastpressure);
268                                         if (cur_delta && abs(cur_delta - pressure_delta[index]) > 150) {
269                                                 /* ok the samples aren't just a linearisation
270                                                  * between start and end */
271                                                 pressure_delta[index] = -INT_MAX;
272                                         }
273                                 }
274                         }
275                 }
276                 lastindex = index;
277                 lastpressure = pressure;
278
279                 if (lastdepth)
280                         end = time;
281
282                 if (depth) {
283                         if (start < 0)
284                                 start = lasttime;
285                         if (depth > maxdepth)
286                                 maxdepth = depth;
287                 }
288
289                 fixup_pressure(dive, sample);
290
291                 if (temp) {
292                         /*
293                          * If we have consecutive identical
294                          * temperature readings, throw away
295                          * the redundant ones.
296                          */
297                         if (lasttemp == temp)
298                                 sample->temperature.mkelvin = 0;
299                         else
300                                 lasttemp = temp;
301
302                         if (!mintemp || temp < mintemp)
303                                 mintemp = temp;
304                 }
305                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
306                 lastdepth = depth;
307                 lasttime = time;
308         }
309         /* if all the samples for a cylinder have pressure data that
310          * is basically equidistant throw out the sample cylinder pressure
311          * information but make sure we still have a valid start and end
312          * pressure
313          * this happens when DivingLog decides to linearalize the
314          * pressure between beginning and end and for strange reasons
315          * decides to put that in the sample data as if it came from
316          * the dive computer; we don't want that (we'll visualize with
317          * constant SAC rate instead)
318          * WARNING WARNING - I have only seen this in single tank dives
319          * --- maybe I should try to create a multi tank dive and see what
320          * --- divinglog does there - but the code right now is only tested
321          * --- for the single tank case */
322         for (j = 0; j < MAX_CYLINDERS; j++) {
323                 if (abs(pressure_delta[j]) != INT_MAX) {
324                         cylinder_t *cyl = dive->cylinder + j;
325                         for (i = 0; i < dive->samples; i++)
326                                 if (dive->sample[i].cylinderindex == j)
327                                         dive->sample[i].cylinderpressure.mbar = 0;
328                         if (! cyl->start.mbar)
329                                 cyl->start.mbar = cyl->sample_start.mbar;
330                         if (! cyl->end.mbar)
331                                 cyl->end.mbar = cyl->sample_end.mbar;
332                         cyl->sample_start.mbar = 0;
333                         cyl->sample_end.mbar = 0;
334                 }
335         }
336         if (end < 0)
337                 return dive;
338
339         update_duration(&dive->duration, end - start);
340         if (start != end)
341                 depthtime /= (end - start);
342
343         update_depth(&dive->meandepth, depthtime);
344         update_temperature(&dive->watertemp, mintemp);
345         update_depth(&dive->maxdepth, maxdepth);
346
347         add_people(dive->buddy);
348         add_people(dive->divemaster);
349         add_location(dive->location);
350         for (i = 0; i < MAX_CYLINDERS; i++) {
351                 cylinder_t *cyl = dive->cylinder + i;
352                 add_cylinder_description(&cyl->type);
353                 if (same_rounded_pressure(cyl->sample_start, cyl->start))
354                         cyl->start.mbar = 0;
355                 if (same_rounded_pressure(cyl->sample_end, cyl->end))
356                         cyl->end.mbar = 0;
357         }
358
359         return dive;
360 }
361
362 /* Don't pick a zero for MERGE_MIN() */
363 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
364 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
365 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
366 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
367
368 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
369 {
370         struct sample *p = prepare_sample(&dive);
371
372         if (!p)
373                 return NULL;
374         *p = *sample;
375         p->time.seconds = time;
376         finish_sample(dive, p);
377         return dive;
378 }
379
380 /*
381  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
382  */
383 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
384 {
385         int asamples = a->samples;
386         int bsamples = b->samples;
387         struct sample *as = a->sample;
388         struct sample *bs = b->sample;
389
390         for (;;) {
391                 int at, bt;
392                 struct sample sample;
393
394                 if (!res)
395                         return NULL;
396
397                 at = asamples ? as->time.seconds : -1;
398                 bt = bsamples ? bs->time.seconds + offset : -1;
399
400                 /* No samples? All done! */
401                 if (at < 0 && bt < 0)
402                         return fixup_dive(res);
403
404                 /* Only samples from a? */
405                 if (bt < 0) {
406 add_sample_a:
407                         res = add_sample(as, at, res);
408                         as++;
409                         asamples--;
410                         continue;
411                 }
412
413                 /* Only samples from b? */
414                 if (at < 0) {
415 add_sample_b:
416                         res = add_sample(bs, bt, res);
417                         bs++;
418                         bsamples--;
419                         continue;
420                 }
421
422                 if (at < bt)
423                         goto add_sample_a;
424                 if (at > bt)
425                         goto add_sample_b;
426
427                 /* same-time sample: add a merged sample. Take the non-zero ones */
428                 sample = *bs;
429                 if (as->depth.mm)
430                         sample.depth = as->depth;
431                 if (as->temperature.mkelvin)
432                         sample.temperature = as->temperature;
433                 if (as->cylinderpressure.mbar)
434                         sample.cylinderpressure = as->cylinderpressure;
435                 if (as->cylinderindex)
436                         sample.cylinderindex = as->cylinderindex;
437
438                 res = add_sample(&sample, at, res);
439
440                 as++;
441                 bs++;
442                 asamples--;
443                 bsamples--;
444         }
445 }
446
447 static char *merge_text(const char *a, const char *b)
448 {
449         char *res;
450
451         if (!a || !*a)
452                 return (char *)b;
453         if (!b || !*b)
454                 return (char *)a;
455         if (!strcmp(a,b))
456                 return (char *)a;
457         res = malloc(strlen(a) + strlen(b) + 9);
458         if (!res)
459                 return (char *)a;
460         sprintf(res, "(%s) or (%s)", a, b);
461         return res;
462 }
463
464 #define SORT(a,b,field) \
465         if (a->field != b->field) return a->field < b->field ? -1 : 1
466
467 static int sort_event(struct event *a, struct event *b)
468 {
469         SORT(a,b,time.seconds);
470         SORT(a,b,type);
471         SORT(a,b,flags);
472         SORT(a,b,value);
473         return strcmp(a->name, b->name);
474 }
475
476 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
477 {
478         struct event *a, *b;
479         struct event **p = &res->events;
480
481         a = src1->events;
482         b = src2->events;
483         while (b) {
484                 b->time.seconds += offset;
485                 b = b->next;
486         }
487         b = src2->events;
488
489         while (a || b) {
490                 int s;
491                 if (!b) {
492                         *p = a;
493                         break;
494                 }
495                 if (!a) {
496                         *p = b;
497                         break;
498                 }
499                 s = sort_event(a, b);
500                 /* Pick b */
501                 if (s > 0) {
502                         *p = b;
503                         p = &b->next;
504                         b = b->next;
505                         continue;
506                 }
507                 /* Pick 'a' or neither */
508                 if (s < 0) {
509                         *p = a;
510                         p = &a->next;
511                 }
512                 a = a->next;
513                 continue;
514         }
515 }
516
517 /* Pick whichever has any info (if either). Prefer 'a' */
518 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
519 {
520         if (a->size.mliter)
521                 b = a;
522         *res = *b;
523 }
524
525 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
526 {
527         if (a->o2.permille)
528                 b = a;
529         *res = *b;
530 }
531
532 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
533 {
534         merge_cylinder_type(&res->type, &a->type, &b->type);
535         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
536         MERGE_MAX(res, a, b, start.mbar);
537         MERGE_MIN(res, a, b, end.mbar);
538 }
539
540 /*
541  * This could do a lot more merging. Right now it really only
542  * merges almost exact duplicates - something that happens easily
543  * with overlapping dive downloads.
544  */
545 struct dive *try_to_merge(struct dive *a, struct dive *b)
546 {
547         int i;
548         struct dive *res;
549
550         if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
551                 return NULL;
552
553         res = alloc_dive();
554
555         res->when = a->when;
556         MERGE_NONZERO(res, a, b, latitude);
557         MERGE_NONZERO(res, a, b, longitude);
558         MERGE_TXT(res, a, b, location);
559         MERGE_TXT(res, a, b, notes);
560         MERGE_TXT(res, a, b, buddy);
561         MERGE_TXT(res, a, b, divemaster);
562         MERGE_MAX(res, a, b, rating);
563         MERGE_MAX(res, a, b, number);
564         MERGE_MAX(res, a, b, maxdepth.mm);
565         res->meandepth.mm = 0;
566         MERGE_MAX(res, a, b, duration.seconds);
567         MERGE_MAX(res, a, b, surfacetime.seconds);
568         MERGE_MAX(res, a, b, airtemp.mkelvin);
569         MERGE_MIN(res, a, b, watertemp.mkelvin);
570         for (i = 0; i < MAX_CYLINDERS; i++)
571                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
572         merge_events(res, a, b, 0);
573         return merge_samples(res, a, b, 0);
574 }