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