]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Remove redundant duplicate pressure samples
[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;
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
248         for (i = 0; i < dive->samples; i++) {
249                 struct sample *sample = dive->sample + i;
250                 int time = sample->time.seconds;
251                 int depth = sample->depth.mm;
252                 int temp = sample->temperature.mkelvin;
253                 int pressure = sample->cylinderpressure.mbar;
254                 int index = sample->cylinderindex;
255
256                 /* Remove duplicate redundant pressure information */
257                 if (pressure == lastpressure && index == lastindex)
258                         sample->cylinderpressure.mbar = 0;
259
260                 lastindex = index;
261                 lastpressure = pressure;
262
263                 if (lastdepth)
264                         end = time;
265
266                 if (depth) {
267                         if (start < 0)
268                                 start = lasttime;
269                         if (depth > maxdepth)
270                                 maxdepth = depth;
271                 }
272
273                 fixup_pressure(dive, sample);
274
275                 if (temp) {
276                         /*
277                          * If we have consecutive identical
278                          * temperature readings, throw away
279                          * the redundant ones.
280                          */
281                         if (lasttemp == temp)
282                                 sample->temperature.mkelvin = 0;
283                         else
284                                 lasttemp = temp;
285
286                         if (!mintemp || temp < mintemp)
287                                 mintemp = temp;
288                 }
289                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
290                 lastdepth = depth;
291                 lasttime = time;
292         }
293         if (end < 0)
294                 return dive;
295
296         update_duration(&dive->duration, end - start);
297         if (start != end)
298                 depthtime /= (end - start);
299
300         update_depth(&dive->meandepth, depthtime);
301         update_temperature(&dive->watertemp, mintemp);
302         update_depth(&dive->maxdepth, maxdepth);
303
304         add_people(dive->buddy);
305         add_people(dive->divemaster);
306         add_location(dive->location);
307         for (i = 0; i < MAX_CYLINDERS; i++) {
308                 cylinder_t *cyl = dive->cylinder + i;
309                 add_cylinder_description(&cyl->type);
310                 if (same_rounded_pressure(cyl->sample_start, cyl->start))
311                         cyl->start.mbar = 0;
312                 if (same_rounded_pressure(cyl->sample_end, cyl->end))
313                         cyl->end.mbar = 0;
314         }
315
316         return dive;
317 }
318
319 /* Don't pick a zero for MERGE_MIN() */
320 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
321 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
322 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
323 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
324
325 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
326 {
327         struct sample *p = prepare_sample(&dive);
328
329         if (!p)
330                 return NULL;
331         *p = *sample;
332         p->time.seconds = time;
333         finish_sample(dive, p);
334         return dive;
335 }
336
337 /*
338  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
339  */
340 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
341 {
342         int asamples = a->samples;
343         int bsamples = b->samples;
344         struct sample *as = a->sample;
345         struct sample *bs = b->sample;
346
347         for (;;) {
348                 int at, bt;
349                 struct sample sample;
350
351                 if (!res)
352                         return NULL;
353
354                 at = asamples ? as->time.seconds : -1;
355                 bt = bsamples ? bs->time.seconds + offset : -1;
356
357                 /* No samples? All done! */
358                 if (at < 0 && bt < 0)
359                         return fixup_dive(res);
360
361                 /* Only samples from a? */
362                 if (bt < 0) {
363 add_sample_a:
364                         res = add_sample(as, at, res);
365                         as++;
366                         asamples--;
367                         continue;
368                 }
369
370                 /* Only samples from b? */
371                 if (at < 0) {
372 add_sample_b:
373                         res = add_sample(bs, bt, res);
374                         bs++;
375                         bsamples--;
376                         continue;
377                 }
378
379                 if (at < bt)
380                         goto add_sample_a;
381                 if (at > bt)
382                         goto add_sample_b;
383
384                 /* same-time sample: add a merged sample. Take the non-zero ones */
385                 sample = *bs;
386                 if (as->depth.mm)
387                         sample.depth = as->depth;
388                 if (as->temperature.mkelvin)
389                         sample.temperature = as->temperature;
390                 if (as->cylinderpressure.mbar)
391                         sample.cylinderpressure = as->cylinderpressure;
392                 if (as->cylinderindex)
393                         sample.cylinderindex = as->cylinderindex;
394
395                 res = add_sample(&sample, at, res);
396
397                 as++;
398                 bs++;
399                 asamples--;
400                 bsamples--;
401         }
402 }
403
404 static char *merge_text(const char *a, const char *b)
405 {
406         char *res;
407
408         if (!a || !*a)
409                 return (char *)b;
410         if (!b || !*b)
411                 return (char *)a;
412         if (!strcmp(a,b))
413                 return (char *)a;
414         res = malloc(strlen(a) + strlen(b) + 9);
415         if (!res)
416                 return (char *)a;
417         sprintf(res, "(%s) or (%s)", a, b);
418         return res;
419 }
420
421 #define SORT(a,b,field) \
422         if (a->field != b->field) return a->field < b->field ? -1 : 1
423
424 static int sort_event(struct event *a, struct event *b)
425 {
426         SORT(a,b,time.seconds);
427         SORT(a,b,type);
428         SORT(a,b,flags);
429         SORT(a,b,value);
430         return strcmp(a->name, b->name);
431 }
432
433 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
434 {
435         struct event *a, *b;
436         struct event **p = &res->events;
437
438         a = src1->events;
439         b = src2->events;
440         while (b) {
441                 b->time.seconds += offset;
442                 b = b->next;
443         }
444         b = src2->events;
445
446         while (a || b) {
447                 int s;
448                 if (!b) {
449                         *p = a;
450                         break;
451                 }
452                 if (!a) {
453                         *p = b;
454                         break;
455                 }
456                 s = sort_event(a, b);
457                 /* Pick b */
458                 if (s > 0) {
459                         *p = b;
460                         p = &b->next;
461                         b = b->next;
462                         continue;
463                 }
464                 /* Pick 'a' or neither */
465                 if (s < 0) {
466                         *p = a;
467                         p = &a->next;
468                 }
469                 a = a->next;
470                 continue;
471         }
472 }
473
474 /* Pick whichever has any info (if either). Prefer 'a' */
475 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
476 {
477         if (a->size.mliter)
478                 b = a;
479         *res = *b;
480 }
481
482 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
483 {
484         if (a->o2.permille)
485                 b = a;
486         *res = *b;
487 }
488
489 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
490 {
491         merge_cylinder_type(&res->type, &a->type, &b->type);
492         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
493         MERGE_MAX(res, a, b, start.mbar);
494         MERGE_MIN(res, a, b, end.mbar);
495 }
496
497 /*
498  * This could do a lot more merging. Right now it really only
499  * merges almost exact duplicates - something that happens easily
500  * with overlapping dive downloads.
501  */
502 struct dive *try_to_merge(struct dive *a, struct dive *b)
503 {
504         int i;
505         struct dive *res;
506
507         if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
508                 return NULL;
509
510         res = alloc_dive();
511
512         res->when = a->when;
513         MERGE_NONZERO(res, a, b, latitude);
514         MERGE_NONZERO(res, a, b, longitude);
515         MERGE_TXT(res, a, b, location);
516         MERGE_TXT(res, a, b, notes);
517         MERGE_TXT(res, a, b, buddy);
518         MERGE_TXT(res, a, b, divemaster);
519         MERGE_MAX(res, a, b, number);
520         MERGE_MAX(res, a, b, maxdepth.mm);
521         res->meandepth.mm = 0;
522         MERGE_MAX(res, a, b, duration.seconds);
523         MERGE_MAX(res, a, b, surfacetime.seconds);
524         MERGE_MAX(res, a, b, airtemp.mkelvin);
525         MERGE_MIN(res, a, b, watertemp.mkelvin);
526         for (i = 0; i < MAX_CYLINDERS; i++)
527                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
528         merge_events(res, a, b, 0);
529         return merge_samples(res, a, b, 0);
530 }