]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Add cylinder data to cylinder model as we record each dive
[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 /*
144  * If you have more than 32 cylinders, you'd better have a 64-bit build.
145  * And if you have more than 64 cylinders, you need to use another tool,
146  * or fix this up to do something odd.
147  */
148 static unsigned long fixup_pressure(struct dive *dive, struct sample *sample, unsigned long flags)
149 {
150         unsigned long mask;
151         unsigned int pressure, index;
152         cylinder_t *cyl;
153
154         pressure = sample->cylinderpressure.mbar;
155         if (!pressure)
156                 return flags;
157         index = sample->cylinderindex;
158         if (index >= MAX_CYLINDERS)
159                 return flags;
160         cyl = dive->cylinder + index;
161         if (!cyl->start.mbar)
162                 cyl->start.mbar = pressure;
163         /*
164          * If we already have an end pressure, without
165          * ever having seen a sample for this cylinder,
166          * that means that somebody set the end pressure
167          * by hand
168          */
169         mask = 1ul << index;
170         if (cyl->end.mbar) {
171                 if (!(flags & mask))
172                         return flags;
173         }
174         flags |= mask;
175
176         /* we need to handle the user entering beginning and end tank pressures
177          * - maybe even IF we have samples. But for now if we have air pressure
178          * data in the samples, we use that instead of the minimum
179          * if (!cyl->end.mbar || pressure < cyl->end.mbar)
180          */
181         cyl->end.mbar = pressure;
182         return flags;
183 }
184
185 struct dive *fixup_dive(struct dive *dive)
186 {
187         int i;
188         double depthtime = 0;
189         int lasttime = 0;
190         int start = -1, end = -1;
191         int maxdepth = 0, mintemp = 0;
192         int lastdepth = 0;
193         int lasttemp = 0;
194         unsigned long flags = 0;
195
196         for (i = 0; i < dive->samples; i++) {
197                 struct sample *sample = dive->sample + i;
198                 int time = sample->time.seconds;
199                 int depth = sample->depth.mm;
200                 int temp = sample->temperature.mkelvin;
201
202                 if (lastdepth)
203                         end = time;
204
205                 if (depth) {
206                         if (start < 0)
207                                 start = lasttime;
208                         if (depth > maxdepth)
209                                 maxdepth = depth;
210                 }
211
212                 flags = fixup_pressure(dive, sample, flags);
213
214                 if (temp) {
215                         /*
216                          * If we have consecutive identical
217                          * temperature readings, throw away
218                          * the redundant ones.
219                          */
220                         if (lasttemp == temp)
221                                 sample->temperature.mkelvin = 0;
222                         else
223                                 lasttemp = temp;
224
225                         if (!mintemp || temp < mintemp)
226                                 mintemp = temp;
227                 }
228                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
229                 lastdepth = depth;
230                 lasttime = time;
231         }
232         if (end < 0)
233                 return dive;
234
235         update_duration(&dive->duration, end - start);
236         if (start != end)
237                 depthtime /= (end - start);
238
239         update_depth(&dive->meandepth, depthtime);
240         update_temperature(&dive->watertemp, mintemp);
241         update_depth(&dive->maxdepth, maxdepth);
242
243         for (i = 0; i < MAX_CYLINDERS; i++) {
244                 cylinder_type_t *type = &dive->cylinder[i].type;
245                 add_cylinder_description(type);
246         }
247
248         return dive;
249 }
250
251 /* Don't pick a zero for MERGE_MIN() */
252 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
253 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
254 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
255 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
256
257 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
258 {
259         struct sample *p = prepare_sample(&dive);
260
261         if (!p)
262                 return NULL;
263         *p = *sample;
264         p->time.seconds = time;
265         finish_sample(dive, p);
266         return dive;
267 }
268
269 /*
270  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
271  */
272 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
273 {
274         int asamples = a->samples;
275         int bsamples = b->samples;
276         struct sample *as = a->sample;
277         struct sample *bs = b->sample;
278
279         for (;;) {
280                 int at, bt;
281                 struct sample sample;
282
283                 if (!res)
284                         return NULL;
285
286                 at = asamples ? as->time.seconds : -1;
287                 bt = bsamples ? bs->time.seconds + offset : -1;
288
289                 /* No samples? All done! */
290                 if (at < 0 && bt < 0)
291                         return fixup_dive(res);
292
293                 /* Only samples from a? */
294                 if (bt < 0) {
295 add_sample_a:
296                         res = add_sample(as, at, res);
297                         as++;
298                         asamples--;
299                         continue;
300                 }
301
302                 /* Only samples from b? */
303                 if (at < 0) {
304 add_sample_b:
305                         res = add_sample(bs, bt, res);
306                         bs++;
307                         bsamples--;
308                         continue;
309                 }
310
311                 if (at < bt)
312                         goto add_sample_a;
313                 if (at > bt)
314                         goto add_sample_b;
315
316                 /* same-time sample: add a merged sample. Take the non-zero ones */
317                 sample = *bs;
318                 if (as->depth.mm)
319                         sample.depth = as->depth;
320                 if (as->temperature.mkelvin)
321                         sample.temperature = as->temperature;
322                 if (as->cylinderpressure.mbar)
323                         sample.cylinderpressure = as->cylinderpressure;
324                 if (as->cylinderindex)
325                         sample.cylinderindex = as->cylinderindex;
326
327                 res = add_sample(&sample, at, res);
328
329                 as++;
330                 bs++;
331                 asamples--;
332                 bsamples--;
333         }
334 }
335
336 static char *merge_text(const char *a, const char *b)
337 {
338         char *res;
339
340         if (!a || !*a)
341                 return (char *)b;
342         if (!b || !*b)
343                 return (char *)a;
344         if (!strcmp(a,b))
345                 return (char *)a;
346         res = malloc(strlen(a) + strlen(b) + 9);
347         if (!res)
348                 return (char *)a;
349         sprintf(res, "(%s) or (%s)", a, b);
350         return res;
351 }
352
353 #define SORT(a,b,field) \
354         if (a->field != b->field) return a->field < b->field ? -1 : 1
355
356 static int sort_event(struct event *a, struct event *b)
357 {
358         SORT(a,b,time.seconds);
359         SORT(a,b,type);
360         SORT(a,b,flags);
361         SORT(a,b,value);
362         return strcmp(a->name, b->name);
363 }
364
365 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
366 {
367         struct event *a, *b;
368         struct event **p = &res->events;
369
370         a = src1->events;
371         b = src2->events;
372         while (b) {
373                 b->time.seconds += offset;
374                 b = b->next;
375         }
376         b = src2->events;
377
378         while (a || b) {
379                 int s;
380                 if (!b) {
381                         *p = a;
382                         break;
383                 }
384                 if (!a) {
385                         *p = b;
386                         break;
387                 }
388                 s = sort_event(a, b);
389                 /* Pick b */
390                 if (s > 0) {
391                         *p = b;
392                         p = &b->next;
393                         b = b->next;
394                         continue;
395                 }
396                 /* Pick 'a' or neither */
397                 if (s < 0) {
398                         *p = a;
399                         p = &a->next;
400                 }
401                 a = a->next;
402                 continue;
403         }
404 }
405
406 /* Pick whichever has any info (if either). Prefer 'a' */
407 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
408 {
409         if (a->size.mliter)
410                 b = a;
411         *res = *b;
412 }
413
414 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
415 {
416         if (a->o2.permille)
417                 b = a;
418         *res = *b;
419 }
420
421 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
422 {
423         merge_cylinder_type(&res->type, &a->type, &b->type);
424         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
425         MERGE_MAX(res, a, b, start.mbar);
426         MERGE_MIN(res, a, b, end.mbar);
427 }
428
429 /*
430  * This could do a lot more merging. Right now it really only
431  * merges almost exact duplicates - something that happens easily
432  * with overlapping dive downloads.
433  */
434 struct dive *try_to_merge(struct dive *a, struct dive *b)
435 {
436         int i;
437         struct dive *res;
438
439         if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
440                 return NULL;
441
442         res = alloc_dive();
443
444         res->when = a->when;
445         MERGE_NONZERO(res, a, b, latitude);
446         MERGE_NONZERO(res, a, b, longitude);
447         MERGE_TXT(res, a, b, location);
448         MERGE_TXT(res, a, b, notes);
449         MERGE_TXT(res, a, b, buddy);
450         MERGE_TXT(res, a, b, divemaster);
451         MERGE_MAX(res, a, b, number);
452         MERGE_MAX(res, a, b, maxdepth.mm);
453         res->meandepth.mm = 0;
454         MERGE_MAX(res, a, b, duration.seconds);
455         MERGE_MAX(res, a, b, surfacetime.seconds);
456         MERGE_MAX(res, a, b, airtemp.mkelvin);
457         MERGE_MIN(res, a, b, watertemp.mkelvin);
458         for (i = 0; i < MAX_CYLINDERS; i++)
459                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
460         merge_events(res, a, b, 0);
461         return merge_samples(res, a, b, 0);
462 }