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