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