]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Make the dive merging code more tolerant
[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 }
30
31 double get_depth_units(unsigned int mm, int *frac, const char **units)
32 {
33         int decimals;
34         double d;
35         const char *unit;
36
37         switch (output_units.length) {
38         case METERS:
39                 d = mm / 1000.0;
40                 unit = "m";
41                 decimals = d < 20;
42                 break;
43         case FEET:
44                 d = mm_to_feet(mm);
45                 unit = "ft";
46                 decimals = 0;
47                 break;
48         }
49         if (frac)
50                 *frac = decimals;
51         if (units)
52                 *units = unit;
53         return d;
54 }
55
56 struct dive *alloc_dive(void)
57 {
58         const int initial_samples = 5;
59         unsigned int size;
60         struct dive *dive;
61
62         size = dive_size(initial_samples);
63         dive = malloc(size);
64         if (!dive)
65                 exit(1);
66         memset(dive, 0, size);
67         dive->alloc_samples = initial_samples;
68         return dive;
69 }
70
71 struct sample *prepare_sample(struct dive **divep)
72 {
73         struct dive *dive = *divep;
74         if (dive) {
75                 int nr = dive->samples;
76                 int alloc_samples = dive->alloc_samples;
77                 struct sample *sample;
78                 if (nr >= alloc_samples) {
79                         unsigned int size;
80
81                         alloc_samples = (alloc_samples * 3)/2 + 10;
82                         size = dive_size(alloc_samples);
83                         dive = realloc(dive, size);
84                         if (!dive)
85                                 return NULL;
86                         dive->alloc_samples = alloc_samples;
87                         *divep = dive;
88                 }
89                 sample = dive->sample + nr;
90                 memset(sample, 0, sizeof(*sample));
91                 return sample;
92         }
93         return NULL;
94 }
95
96 void finish_sample(struct dive *dive, struct sample *sample)
97 {
98         dive->samples++;
99 }
100
101 /*
102  * So when we re-calculate maxdepth and meandepth, we will
103  * not override the old numbers if they are close to the
104  * new ones.
105  *
106  * Why? Because a dive computer may well actually track the
107  * max depth and mean depth at finer granularity than the
108  * samples it stores. So it's possible that the max and mean
109  * have been reported more correctly originally.
110  *
111  * Only if the values calculated from the samples are clearly
112  * different do we override the normal depth values.
113  *
114  * This considers 1m to be "clearly different". That's
115  * a totally random number.
116  */
117 static void update_depth(depth_t *depth, int new)
118 {
119         if (new) {
120                 int old = depth->mm;
121
122                 if (abs(old - new) > 1000)
123                         depth->mm = new;
124         }
125 }
126
127 static void update_duration(duration_t *duration, int new)
128 {
129         if (new)
130                 duration->seconds = new;
131 }
132
133 static void update_temperature(temperature_t *temperature, int new)
134 {
135         if (new) {
136                 int old = temperature->mkelvin;
137
138                 if (abs(old - new) > 1000)
139                         temperature->mkelvin = new;
140         }
141 }
142
143 static void fixup_pressure(struct dive *dive, struct sample *sample)
144 {
145         unsigned int pressure, index;
146         cylinder_t *cyl;
147
148         pressure = sample->cylinderpressure.mbar;
149         if (!pressure)
150                 return;
151         index = sample->cylinderindex;
152         if (index >= MAX_CYLINDERS)
153                 return;
154         cyl = dive->cylinder + index;
155         if (!cyl->start.mbar)
156                 cyl->start.mbar = pressure;
157         /* we need to handle the user entering beginning and end tank pressures
158          * - maybe even IF we have samples. But for now if we have air pressure
159          * data in the samples, we use that instead of the minimum
160          * if (!cyl->end.mbar || pressure < cyl->end.mbar)
161          */
162          cyl->end.mbar = pressure;
163 }
164
165 struct dive *fixup_dive(struct dive *dive)
166 {
167         int i;
168         double depthtime = 0;
169         int lasttime = 0;
170         int start = -1, end = -1;
171         int maxdepth = 0, mintemp = 0;
172         int lastdepth = 0;
173         int lasttemp = 0;
174
175         for (i = 0; i < dive->samples; i++) {
176                 struct sample *sample = dive->sample + i;
177                 int time = sample->time.seconds;
178                 int depth = sample->depth.mm;
179                 int temp = sample->temperature.mkelvin;
180
181                 if (lastdepth)
182                         end = time;
183
184                 if (depth) {
185                         if (start < 0)
186                                 start = lasttime;
187                         if (depth > maxdepth)
188                                 maxdepth = depth;
189                 }
190
191                 fixup_pressure(dive, sample);
192
193                 if (temp) {
194                         /*
195                          * If we have consecutive identical
196                          * temperature readings, throw away
197                          * the redundant ones.
198                          */
199                         if (lasttemp == temp)
200                                 sample->temperature.mkelvin = 0;
201                         else
202                                 lasttemp = temp;
203
204                         if (!mintemp || temp < mintemp)
205                                 mintemp = temp;
206                 }
207                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
208                 lastdepth = depth;
209                 lasttime = time;
210         }
211         if (end < 0)
212                 return dive;
213
214         update_duration(&dive->duration, end - start);
215         if (start != end)
216                 depthtime /= (end - start);
217
218         update_depth(&dive->meandepth, depthtime);
219         update_temperature(&dive->watertemp, mintemp);
220         update_depth(&dive->maxdepth, maxdepth);
221
222         return dive;
223 }
224
225 /* Don't pick a zero for MERGE_MIN() */
226 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
227 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
228 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
229 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
230
231 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
232 {
233         struct sample *p = prepare_sample(&dive);
234
235         if (!p)
236                 return NULL;
237         *p = *sample;
238         p->time.seconds = time;
239         finish_sample(dive, p);
240         return dive;
241 }
242
243 /*
244  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
245  */
246 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
247 {
248         int asamples = a->samples;
249         int bsamples = b->samples;
250         struct sample *as = a->sample;
251         struct sample *bs = b->sample;
252
253         for (;;) {
254                 int at, bt;
255                 struct sample sample;
256
257                 if (!res)
258                         return NULL;
259
260                 at = asamples ? as->time.seconds : -1;
261                 bt = bsamples ? bs->time.seconds + offset : -1;
262
263                 /* No samples? All done! */
264                 if (at < 0 && bt < 0)
265                         return fixup_dive(res);
266
267                 /* Only samples from a? */
268                 if (bt < 0) {
269 add_sample_a:
270                         res = add_sample(as, at, res);
271                         as++;
272                         asamples--;
273                         continue;
274                 }
275
276                 /* Only samples from b? */
277                 if (at < 0) {
278 add_sample_b:
279                         res = add_sample(bs, bt, res);
280                         bs++;
281                         bsamples--;
282                         continue;
283                 }
284
285                 if (at < bt)
286                         goto add_sample_a;
287                 if (at > bt)
288                         goto add_sample_b;
289
290                 /* same-time sample: add a merged sample. Take the non-zero ones */
291                 sample = *bs;
292                 if (as->depth.mm)
293                         sample.depth = as->depth;
294                 if (as->temperature.mkelvin)
295                         sample.temperature = as->temperature;
296                 if (as->cylinderpressure.mbar)
297                         sample.cylinderpressure = as->cylinderpressure;
298                 if (as->cylinderindex)
299                         sample.cylinderindex = as->cylinderindex;
300
301                 res = add_sample(&sample, at, res);
302
303                 as++;
304                 bs++;
305                 asamples--;
306                 bsamples--;
307         }
308 }
309
310 static char *merge_text(const char *a, const char *b)
311 {
312         char *res;
313
314         if (!a || !*a)
315                 return (char *)b;
316         if (!b || !*b)
317                 return (char *)a;
318         if (!strcmp(a,b))
319                 return (char *)a;
320         res = malloc(strlen(a) + strlen(b) + 9);
321         if (!res)
322                 return (char *)a;
323         sprintf(res, "(%s) or (%s)", a, b);
324         return res;
325 }
326
327 #define SORT(a,b,field) \
328         if (a->field != b->field) return a->field < b->field ? -1 : 1
329
330 static int sort_event(struct event *a, struct event *b)
331 {
332         SORT(a,b,time.seconds);
333         SORT(a,b,type);
334         SORT(a,b,flags);
335         SORT(a,b,value);
336         return strcmp(a->name, b->name);
337 }
338
339 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
340 {
341         struct event *a, *b;
342         struct event **p = &res->events;
343
344         a = src1->events;
345         b = src2->events;
346         while (b) {
347                 b->time.seconds += offset;
348                 b = b->next;
349         }
350         b = src2->events;
351
352         while (a || b) {
353                 int s;
354                 if (!b) {
355                         *p = a;
356                         break;
357                 }
358                 if (!a) {
359                         *p = b;
360                         break;
361                 }
362                 s = sort_event(a, b);
363                 /* Pick b */
364                 if (s > 0) {
365                         *p = b;
366                         p = &b->next;
367                         b = b->next;
368                         continue;
369                 }
370                 /* Pick 'a' or neither */
371                 if (s < 0) {
372                         *p = a;
373                         p = &a->next;
374                 }
375                 a = a->next;
376                 continue;
377         }
378 }
379
380 /* Pick whichever has any info (if either). Prefer 'a' */
381 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
382 {
383         if (a->size.mliter)
384                 b = a;
385         *res = *b;
386 }
387
388 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
389 {
390         if (a->o2.permille)
391                 b = a;
392         *res = *b;
393 }
394
395 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
396 {
397         merge_cylinder_type(&res->type, &a->type, &b->type);
398         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
399         MERGE_MAX(res, a, b, start.mbar);
400         MERGE_MIN(res, a, b, end.mbar);
401 }
402
403 /*
404  * This could do a lot more merging. Right now it really only
405  * merges almost exact duplicates - something that happens easily
406  * with overlapping dive downloads.
407  */
408 struct dive *try_to_merge(struct dive *a, struct dive *b)
409 {
410         int i;
411         struct dive *res;
412
413         if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
414                 return NULL;
415
416         res = alloc_dive();
417
418         res->when = a->when;
419         MERGE_NONZERO(res, a, b, latitude);
420         MERGE_NONZERO(res, a, b, longitude);
421         MERGE_TXT(res, a, b, location);
422         MERGE_TXT(res, a, b, notes);
423         MERGE_TXT(res, a, b, buddy);
424         MERGE_TXT(res, a, b, divemaster);
425         MERGE_MAX(res, a, b, number);
426         MERGE_MAX(res, a, b, maxdepth.mm);
427         res->meandepth.mm = 0;
428         MERGE_MAX(res, a, b, duration.seconds);
429         MERGE_MAX(res, a, b, surfacetime.seconds);
430         MERGE_MAX(res, a, b, airtemp.mkelvin);
431         MERGE_MIN(res, a, b, watertemp.mkelvin);
432         for (i = 0; i < MAX_CYLINDERS; i++)
433                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
434         merge_events(res, a, b, 0);
435         return merge_samples(res, a, b, 0);
436 }